36 #include <sys/socket.h>
37 #include <sys/types.h>
52 string err(
"Socket is already listening" ) ;
58 string err(
"Socket is already connected" ) ;
62 struct sockaddr_un client_addr ;
63 struct sockaddr_un server_addr ;
66 unsigned int max_len =
sizeof( client_addr.sun_path ) ;
69 getcwd( path,
sizeof( path ) ) ;
73 _tempSocket +=
".unixSocket" ;
77 if( _tempSocket.length() > max_len - 1 )
79 string msg =
"path to temporary unix socket " ;
80 msg += _tempSocket +
" is too long" ;
83 if( _unixSocket.length() > max_len - 1 )
85 string msg =
"path to unix socket " ;
86 msg += _unixSocket +
" is too long" ;
90 strncpy(server_addr.sun_path, _unixSocket.c_str(), _unixSocket.size());
91 server_addr.sun_path[_unixSocket.size()] =
'\0';
92 server_addr.sun_family = AF_UNIX ;
94 int descript = socket( AF_UNIX, SOCK_STREAM, 0 ) ;
97 strncpy( client_addr.sun_path, _tempSocket.c_str(), _tempSocket.size());
98 client_addr.sun_path[_tempSocket.size()] =
'\0';
99 client_addr.sun_family = AF_UNIX ;
101 int clen =
sizeof( client_addr.sun_family ) ;
102 clen += strlen( client_addr.sun_path ) + 1;
104 if( bind( descript, (
struct sockaddr*)&client_addr, clen + 1) != -1 )
106 int slen =
sizeof( server_addr.sun_family ) ;
107 slen += strlen( server_addr.sun_path) + 1;
112 if( ::
connect( descript, (
struct sockaddr*)&server_addr, slen ) != -1)
120 string msg =
"could not connect via " ;
122 char *err = strerror( errno ) ;
124 msg = msg +
"\n" + err ;
126 msg = msg +
"\nCould not retrieve error message" ;
133 string msg =
"could not bind to Unix socket " ;
135 char *err = strerror( errno ) ;
137 msg = msg +
"\n" + err ;
139 msg = msg +
"\nCould not retrieve error message" ;
145 string msg =
"could not create a Unix socket" ;
146 char *err = strerror( errno ) ;
148 msg = msg +
"\n" + err ;
150 msg = msg +
"\nCould not retrieve error message" ;
160 string err(
"Socket is already connected" ) ;
166 string err(
"Socket is already listening" ) ;
171 static struct sockaddr_un server_add ;
172 _socket = socket( AF_UNIX,SOCK_STREAM, 0 ) ;
175 server_add.sun_family = AF_UNIX;
178 strncpy( server_add.sun_path, _unixSocket.c_str(), 103) ;
179 server_add.sun_path[103] =
'\0';
181 (void)unlink( _unixSocket.c_str() ) ;
182 if( setsockopt( _socket, SOL_SOCKET, SO_REUSEADDR,
183 (
char*)&on,
sizeof( on ) ) )
185 string error(
"could not set SO_REUSEADDR on Unix socket" ) ;
186 const char *error_info = strerror( errno ) ;
188 error +=
" " + (string)error_info ;
196 if( bind( _socket, (
struct sockaddr*)&server_add,
sizeof( server_add.sun_family ) + strlen( server_add.sun_path ) + 1) != -1)
198 if( ::
listen( _socket, 5 ) == 0 )
204 string error(
"could not listen Unix socket" ) ;
205 const char* error_info = strerror( errno ) ;
207 error +=
" " + (string)error_info ;
213 string error(
"could not bind Unix socket" ) ;
214 const char* error_info = strerror( errno ) ;
216 error +=
" " + (string)error_info ;
222 string error(
"could not get Unix socket" ) ;
223 const char *error_info = strerror( errno ) ;
225 error +=
" " + (string)error_info ;
234 if( _tempSocket !=
"" )
236 if( !access( _tempSocket.c_str(), F_OK ) )
238 (void)
remove( _tempSocket.c_str() ) ;
244 if( !access( _unixSocket.c_str(), F_OK ) )
246 (void)
remove( _unixSocket.c_str() ) ;
271 << (
void *)
this <<
")" << endl ;
exception thrown if inernal error encountered
virtual void dump(ostream &strm) const
dumps information about this object
static ostream & LMarg(ostream &strm)
virtual void dump(ostream &strm) const
dumps information about this object
virtual bool allowConnection()
is there any wrapper code for unix sockets
static string create_temp_name()
Create a uniq name which is used to create a unique name for a Unix socket in a client ...