36 #include <sys/socket.h> 37 #include <sys/types.h> 43 #include "UnixSocket.h" 44 #include "BESInternalError.h" 45 #include "SocketUtilities.h" 47 void UnixSocket::connect()
50 string err(
"Socket is already listening");
55 string err(
"Socket is already connected");
59 struct sockaddr_un client_addr;
60 struct sockaddr_un server_addr;
63 unsigned int max_len =
sizeof(client_addr.sun_path);
66 getcwd(path,
sizeof(path));
70 _tempSocket +=
".unixSocket";
74 if (_tempSocket.length() > max_len - 1) {
75 string msg =
"path to temporary unix socket ";
76 msg += _tempSocket +
" is too long";
79 if (_unixSocket.length() > max_len - 1) {
80 string msg =
"path to unix socket ";
81 msg += _unixSocket +
" is too long";
85 strncpy(server_addr.sun_path, _unixSocket.c_str(), _unixSocket.size());
86 server_addr.sun_path[_unixSocket.size()] =
'\0';
87 server_addr.sun_family = AF_UNIX;
89 int descript = socket( AF_UNIX, SOCK_STREAM, 0);
91 strncpy(client_addr.sun_path, _tempSocket.c_str(), _tempSocket.size());
92 client_addr.sun_path[_tempSocket.size()] =
'\0';
93 client_addr.sun_family = AF_UNIX;
95 int clen =
sizeof(client_addr.sun_family);
96 clen += strlen(client_addr.sun_path) + 1;
98 if (bind(descript, (
struct sockaddr*) &client_addr, clen + 1) != -1) {
99 int slen =
sizeof(server_addr.sun_family);
100 slen += strlen(server_addr.sun_path) + 1;
105 if (::connect(descript, (
struct sockaddr*) &server_addr, slen) != -1) {
111 string msg =
"could not connect via ";
113 char *err = strerror( errno);
115 msg = msg +
"\n" + err;
117 msg = msg +
"\nCould not retrieve error message";
123 string msg =
"could not bind to Unix socket ";
125 char *err = strerror( errno);
127 msg = msg +
"\n" + err;
129 msg = msg +
"\nCould not retrieve error message";
134 string msg =
"could not create a Unix socket";
135 char *err = strerror( errno);
137 msg = msg +
"\n" + err;
139 msg = msg +
"\nCould not retrieve error message";
144 void UnixSocket::listen()
147 string err(
"Socket is already connected");
152 string err(
"Socket is already listening");
157 static struct sockaddr_un server_add;
158 _socket = socket( AF_UNIX, SOCK_STREAM, 0);
160 server_add.sun_family = AF_UNIX;
163 strncpy(server_add.sun_path, _unixSocket.c_str(), 103);
164 server_add.sun_path[103] =
'\0';
166 (void) unlink(_unixSocket.c_str());
167 if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (
char*) &on,
sizeof(on))) {
168 string error(
"could not set SO_REUSEADDR on Unix socket");
169 const char *error_info = strerror( errno);
170 if (error_info) error +=
" " + (string) error_info;
178 if (bind(_socket, (
struct sockaddr*) &server_add,
179 sizeof(server_add.sun_family) + strlen(server_add.sun_path) + 1) != -1) {
180 if (::listen(_socket, 5) == 0) {
184 string error(
"could not listen Unix socket");
185 const char* error_info = strerror( errno);
186 if (error_info) error +=
" " + (string) error_info;
191 string error(
"could not bind Unix socket");
192 const char* error_info = strerror( errno);
193 if (error_info) error +=
" " + (string) error_info;
198 string error(
"could not get Unix socket");
199 const char *error_info = strerror( errno);
200 if (error_info) error +=
" " + (string) error_info;
205 void UnixSocket::close()
208 if (_tempSocket !=
"") {
214 if (!access(_tempSocket.c_str(), F_OK)) {
215 (void) remove(_tempSocket.c_str());
218 (void) remove(_tempSocket.c_str());
221 if (_listening && _unixSocket !=
"") {
223 if (!access(_unixSocket.c_str(), F_OK)) {
224 (void) remove(_unixSocket.c_str());
227 (void) remove(_unixSocket.c_str());
248 strm << BESIndent::LMarg <<
"UnixSocket::dump - (" << (
void *)
this <<
")" << endl;
250 strm << BESIndent::LMarg <<
"unix socket: " << _unixSocket << endl;
251 strm << BESIndent::LMarg <<
"temp socket: " << _tempSocket << endl;
253 BESIndent::UnIndent();
exception thrown if inernal error encountered
virtual bool allowConnection()
is there any wrapper code for unix sockets
virtual void dump(ostream &strm) const
dumps information about this object
static string create_temp_name()
virtual void dump(ostream &strm) const
dumps information about this object