36 #include <sys/socket.h>
37 #include <sys/types.h>
43 #include "UnixSocket.h"
44 #include "BESInternalError.h"
45 #include "SocketUtilities.h"
51 void UnixSocket::connect()
54 string err(
"Socket is already listening");
59 string err(
"Socket is already connected");
63 struct sockaddr_un client_addr;
64 struct sockaddr_un server_addr;
67 unsigned int max_len =
sizeof(client_addr.sun_path);
70 getcwd(path,
sizeof(path));
74 _tempSocket +=
".unixSocket";
78 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) {
84 string msg =
"path to unix socket ";
85 msg += _unixSocket +
" is too long";
89 strncpy(server_addr.sun_path, _unixSocket.c_str(), _unixSocket.size());
90 server_addr.sun_path[_unixSocket.size()] =
'\0';
91 server_addr.sun_family = AF_UNIX;
93 int descript = socket( AF_UNIX, SOCK_STREAM, 0);
95 strncpy(client_addr.sun_path, _tempSocket.c_str(), _tempSocket.size());
96 client_addr.sun_path[_tempSocket.size()] =
'\0';
97 client_addr.sun_family = AF_UNIX;
99 int clen =
sizeof(client_addr.sun_family);
100 clen += strlen(client_addr.sun_path) + 1;
102 if (bind(descript, (
struct sockaddr*) &client_addr, clen + 1) != -1) {
103 int slen =
sizeof(server_addr.sun_family);
104 slen += strlen(server_addr.sun_path) + 1;
109 if (::connect(descript, (
struct sockaddr*) &server_addr, slen) != -1) {
115 string msg =
"could not connect via ";
117 char *err = strerror( errno);
119 msg = msg +
"\n" + err;
121 msg = msg +
"\nCould not retrieve error message";
127 string msg =
"could not bind to Unix socket ";
129 char *err = strerror( errno);
131 msg = msg +
"\n" + err;
133 msg = msg +
"\nCould not retrieve error message";
138 string msg =
"could not create a Unix socket";
139 char *err = strerror( errno);
141 msg = msg +
"\n" + err;
143 msg = msg +
"\nCould not retrieve error message";
148 void UnixSocket::listen()
151 string err(
"Socket is already connected");
156 string err(
"Socket is already listening");
161 static struct sockaddr_un server_add;
162 _socket = socket( AF_UNIX, SOCK_STREAM, 0);
164 server_add.sun_family = AF_UNIX;
167 strncpy(server_add.sun_path, _unixSocket.c_str(), 103);
168 server_add.sun_path[103] =
'\0';
170 (void) unlink(_unixSocket.c_str());
171 if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (
char*) &on,
sizeof(on))) {
172 string error(
"could not set SO_REUSEADDR on Unix socket");
173 const char *error_info = strerror( errno);
174 if (error_info) error +=
" " + (string) error_info;
182 if (bind(_socket, (
struct sockaddr*) &server_add,
183 sizeof(server_add.sun_family) + strlen(server_add.sun_path) + 1) != -1) {
184 if (::listen(_socket, 5) == 0) {
188 string error(
"could not listen Unix socket");
189 const char* error_info = strerror( errno);
190 if (error_info) error +=
" " + (string) error_info;
195 string error(
"could not bind Unix socket");
196 const char* error_info = strerror( errno);
197 if (error_info) error +=
" " + (string) error_info;
202 string error(
"could not get Unix socket");
203 const char *error_info = strerror( errno);
204 if (error_info) error +=
" " + (string) error_info;
209 void UnixSocket::close()
212 if (_tempSocket !=
"") {
218 if (!access(_tempSocket.c_str(), F_OK)) {
219 (void) remove(_tempSocket.c_str());
222 (void) remove(_tempSocket.c_str());
225 if (_listening && _unixSocket !=
"") {
227 if (!access(_unixSocket.c_str(), F_OK)) {
228 (void) remove(_unixSocket.c_str());
231 (void) remove(_unixSocket.c_str());
252 strm << BESIndent::LMarg <<
"UnixSocket::dump - (" << (
void *)
this <<
")" << endl;
254 strm << BESIndent::LMarg <<
"unix socket: " << _unixSocket << endl;
255 strm << BESIndent::LMarg <<
"temp socket: " << _tempSocket << endl;
257 BESIndent::UnIndent();