1 #include <opensrf/socket_bundle.h>
3 /* buffer used to read from the sockets */
6 static socket_node* _socket_add_node(socket_manager* mgr,
7 int endpoint, int addr_type, int sock_fd, int parent_id );
8 static socket_node* socket_find_node(socket_manager* mgr, int sock_fd);
9 static void socket_remove_node(socket_manager*, int sock_fd);
10 static int _socket_send(int sock_fd, const char* data, int flags);
11 static int _socket_route_data(socket_manager* mgr, int num_active, fd_set* read_set);
12 static int _socket_route_data_id( socket_manager* mgr, int sock_id);
13 static int _socket_handle_new_client(socket_manager* mgr, socket_node* node);
14 static int _socket_handle_client_data(socket_manager* mgr, socket_node* node);
17 /* --------------------------------------------------------------------
19 -------------------------------------------------------------------- */
22 void printme(void* blob, socket_manager* mgr,
23 int sock_fd, char* data, int parent_id) {
25 fprintf(stderr, "Got data from socket %d with parent %d => %s",
26 sock_fd, parent_id, data );
28 socket_send(sock_fd, data);
31 socket_disconnect(mgr, sock_fd);
32 _socket_print_list(mgr);
36 int main(int argc, char* argv[]) {
37 socket_manager manager;
38 memset(&manager, 0, sizeof(socket_manager));
43 manager.data_received = &printme;
44 socket_open_tcp_server(&manager, port);
47 socket_wait_all(&manager, -1);
52 /* -------------------------------------------------------------------- */
55 /* allocates and inserts a new socket node into the nodeset.
56 if parent_id is positive and non-zero, it will be set */
57 static socket_node* _socket_add_node(socket_manager* mgr,
58 int endpoint, int addr_type, int sock_fd, int parent_id ) {
60 if(mgr == NULL) return NULL;
61 osrfLogInternal( OSRF_LOG_MARK, "Adding socket node with fd %d", sock_fd);
62 socket_node* new_node = safe_malloc(sizeof(socket_node));
64 new_node->endpoint = endpoint;
65 new_node->addr_type = addr_type;
66 new_node->sock_fd = sock_fd;
67 new_node->next = NULL;
68 new_node->parent_id = 0;
70 new_node->parent_id = parent_id;
72 new_node->next = mgr->socket;
73 mgr->socket = new_node;
77 /* creates a new server socket node and adds it to the socket set.
78 returns new socket fd on success. -1 on failure.
79 socket_type is one of INET or UNIX */
80 int socket_open_tcp_server(socket_manager* mgr, int port, char* listen_ip) {
83 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_server(): NULL mgr");
88 struct sockaddr_in server_addr;
91 sock_fd = socket(AF_INET, SOCK_STREAM, 0);
93 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_server(): Unable to create TCP socket: %s",
98 server_addr.sin_family = AF_INET;
100 if(listen_ip != NULL) {
101 server_addr.sin_addr.s_addr = inet_addr(listen_ip);
103 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
106 server_addr.sin_port = htons(port);
109 if(bind( sock_fd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) {
110 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_server(): cannot bind to port %d: %s",
111 port, strerror( errno ) );
116 if(listen(sock_fd, 20) == -1) {
117 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_server(): listen() returned error: %s",
122 _socket_add_node(mgr, SERVER_SOCKET, INET, sock_fd, 0);
126 int socket_open_unix_server(socket_manager* mgr, char* path) {
127 if(mgr == NULL || path == NULL) return -1;
129 osrfLogDebug( OSRF_LOG_MARK, "opening unix socket at %s", path);
131 struct sockaddr_un server_addr;
134 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
136 osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_server(): socket() failed: %s",
141 server_addr.sun_family = AF_UNIX;
142 strcpy(server_addr.sun_path, path);
145 if( bind(sock_fd, (struct sockaddr*) &server_addr,
146 sizeof(struct sockaddr_un)) < 0) {
147 osrfLogWarning( OSRF_LOG_MARK,
148 "socket_open_unix_server(): cannot bind to unix port %s: %s",
149 path, strerror( errno ) );
154 if(listen(sock_fd, 20) == -1) {
155 osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_server(): listen() returned error: %s",
160 osrfLogDebug( OSRF_LOG_MARK, "unix socket successfully opened");
164 /* causing problems with router for some reason ... */
165 //osrfLogDebug( OSRF_LOG_MARK, "Setting SO_REUSEADDR");
166 //setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
168 //osrfLogDebug( OSRF_LOG_MARK, "Setting TCP_NODELAY");
169 setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i));
171 _socket_add_node(mgr, SERVER_SOCKET, UNIX, sock_fd, 0);
177 int socket_open_udp_server(
178 socket_manager* mgr, int port, char* listen_ip ) {
181 struct sockaddr_in server_addr;
184 if( (sockfd = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
185 osrfLogWarning( OSRF_LOG_MARK, "Unable to create UDP socket: %s", strerror( errno ) );
189 server_addr.sin_family = AF_INET;
190 server_addr.sin_port = htons(port);
191 if(listen_ip) server_addr.sin_addr.s_addr = inet_addr(listen_ip);
192 else server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
195 if( (bind (sockfd, (struct sockaddr *) &server_addr,sizeof(server_addr))) ) {
196 osrfLogWarning( OSRF_LOG_MARK, "Unable to bind to UDP port %d: %s",
197 port, strerror( errno ) );
201 _socket_add_node(mgr, SERVER_SOCKET, INET, sockfd, 0);
206 int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) {
208 struct sockaddr_in remoteAddr, localAddr;
209 struct hostent *hptr;
212 // ------------------------------------------------------------------
214 // ------------------------------------------------------------------
216 if( (sock_fd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
217 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_client(): Cannot create TCP socket: %s",
223 //osrfLogDebug( OSRF_LOG_MARK, "Setting TCP_NODELAY");
224 setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i));
227 // ------------------------------------------------------------------
229 // ------------------------------------------------------------------
231 if( (hptr = gethostbyname( dest_addr ) ) == NULL ) {
232 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_client(): Unknown Host => %s: %s",
233 dest_addr, strerror( errno ) );
237 // ------------------------------------------------------------------
238 // Construct server info struct
239 // ------------------------------------------------------------------
240 memset( &remoteAddr, 0, sizeof(remoteAddr));
241 remoteAddr.sin_family = AF_INET;
242 remoteAddr.sin_port = htons( port );
243 memcpy( (char*) &remoteAddr.sin_addr.s_addr,
244 hptr->h_addr_list[0], hptr->h_length );
246 // ------------------------------------------------------------------
247 // Construct local info struct
248 // ------------------------------------------------------------------
249 memset( &localAddr, 0, sizeof( localAddr ) );
250 localAddr.sin_family = AF_INET;
251 localAddr.sin_addr.s_addr = htonl( INADDR_ANY );
252 localAddr.sin_port = htons(0);
254 // ------------------------------------------------------------------
255 // Bind to a local port
256 // ------------------------------------------------------------------
258 if( bind( sock_fd, (struct sockaddr *) &localAddr, sizeof( localAddr ) ) < 0 ) {
259 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_client(): Cannot bind to local port: %s",
264 // ------------------------------------------------------------------
266 // ------------------------------------------------------------------
268 if( connect( sock_fd, (struct sockaddr*) &remoteAddr, sizeof( struct sockaddr_in ) ) < 0 ) {
269 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_client(): Cannot connect to server %s: %s",
270 dest_addr, strerror(errno) );
274 _socket_add_node(mgr, CLIENT_SOCKET, INET, sock_fd, -1 );
280 int socket_open_udp_client(
281 socket_manager* mgr, int port, char* dest_addr) {
284 struct sockaddr_in client_addr, server_addr;
285 struct hostent* host;
288 if( (host = gethostbyname(dest_addr)) == NULL) {
289 osrfLogWarning( OSRF_LOG_MARK, "Unable to resolve host: %s: %s",
290 dest_addr, strerror( errno ) );
294 server_addr.sin_family = host->h_addrtype;
295 memcpy((char *) &server_addr.sin_addr.s_addr,
296 host->h_addr_list[0], host->h_length);
297 server_addr.sin_port = htons(port);
300 if( (sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0 ) {
301 osrfLogWarning( OSRF_LOG_MARK, "socket_open_udp_client(): Unable to create UDP socket: %s", strerror( errno ) );
305 client_addr.sin_family = AF_INET;
306 client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
307 client_addr.sin_port = htons(0);
310 if( (bind(sockfd, (struct sockaddr *) &client_addr, sizeof(client_addr))) < 0 ) {
311 osrfLogWarning( OSRF_LOG_MARK, "Unable to bind UDP socket: %s", strerror( errno ) );
315 _socket_add_node(mgr, CLIENT_SOCKET, INET, sockfd, -1 );
321 int socket_open_unix_client(socket_manager* mgr, char* sock_path) {
324 struct sockaddr_un usock;
327 if( (sock_fd = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
328 osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_client(): Cannot create UNIX socket: %s", strerror( errno ) );
332 usock.sun_family = AF_UNIX;
333 strcpy( usock.sun_path, sock_path );
335 len = sizeof( usock.sun_family ) + strlen( usock.sun_path );
338 if( connect( sock_fd, (struct sockaddr *) &usock, len ) < 0 ) {
339 osrfLogWarning( OSRF_LOG_MARK, "Error connecting to unix socket: %s",
344 _socket_add_node(mgr, CLIENT_SOCKET, UNIX, sock_fd, -1 );
350 /* returns the socket_node with the given sock_fd */
351 static socket_node* socket_find_node(socket_manager* mgr, int sock_fd) {
352 if(mgr == NULL) return NULL;
353 socket_node* node = mgr->socket;
355 if(node->sock_fd == sock_fd)
362 /* removes the node with the given sock_fd from the list and frees it */
363 static void socket_remove_node(socket_manager* mgr, int sock_fd) {
365 if(mgr == NULL) return;
367 osrfLogDebug( OSRF_LOG_MARK, "removing socket %d", sock_fd);
369 socket_node* head = mgr->socket;
370 socket_node* tail = head;
371 if(head == NULL) return;
373 /* if removing the first node in the list */
374 if(head->sock_fd == sock_fd) {
375 mgr->socket = head->next;
382 /* if removing any other node */
384 if(head->sock_fd == sock_fd) {
385 tail->next = head->next;
395 void _socket_print_list(socket_manager* mgr) {
396 if(mgr == NULL) return;
397 socket_node* node = mgr->socket;
398 osrfLogDebug( OSRF_LOG_MARK, "socket_node list: [");
400 osrfLogDebug( OSRF_LOG_MARK, "sock_fd: %d | parent_id: %d",
401 node->sock_fd, node->parent_id);
404 osrfLogDebug( OSRF_LOG_MARK, "]");
407 /* sends the given data to the given socket */
408 int socket_send(int sock_fd, const char* data) {
409 return _socket_send( sock_fd, data, 0);
413 static int _socket_send(int sock_fd, const char* data, int flags) {
415 signal(SIGPIPE, SIG_IGN); /* in case a unix socket was closed */
418 size_t r = send( sock_fd, data, strlen(data), flags );
419 int local_errno = errno;
422 osrfLogWarning( OSRF_LOG_MARK, "_socket_send(): Error sending data with return %d", r );
423 osrfLogWarning( OSRF_LOG_MARK, "Last Sys Error: %s", strerror(local_errno));
431 /* sends the given data to the given socket.
432 * sets the send flag MSG_DONTWAIT which will allow the
433 * process to continue even if the socket buffer is full
434 * returns 0 on success, -1 otherwise */
435 //int socket_send_nowait( int sock_fd, const char* data) {
436 // return _socket_send( sock_fd, data, MSG_DONTWAIT);
441 * Waits at most usecs microseconds for the send buffer of the given
442 * socket to accept new data. This does not guarantee that the
443 * socket will accept all the data we want to give it.
445 int socket_send_timeout( int sock_fd, const char* data, int usecs ) {
448 FD_ZERO( &write_set );
449 FD_SET( sock_fd, &write_set );
452 int secs = (int) usecs / mil;
453 usecs = usecs - (secs * mil);
460 int ret = select( sock_fd + 1, NULL, &write_set, NULL, &tv);
461 if( ret > 0 ) return _socket_send( sock_fd, data, 0);
463 osrfLogError(OSRF_LOG_MARK, "socket_send_timeout(): "
464 "timed out on send for socket %d after %d secs, %d usecs: %s",
465 sock_fd, secs, usecs, strerror( errno ) );
471 /* disconnects the node with the given sock_fd and removes
472 it from the socket set */
473 void socket_disconnect(socket_manager* mgr, int sock_fd) {
474 osrfLogInternal( OSRF_LOG_MARK, "Closing socket %d", sock_fd);
476 socket_remove_node(mgr, sock_fd);
480 /* we assume that if select() fails, the socket is no longer valid */
481 int socket_connected(int sock_fd) {
483 FD_ZERO( &read_set );
484 FD_SET( sock_fd, &read_set );
485 if( select( sock_fd + 1, &read_set, NULL, NULL, NULL) == -1 )
491 /* this only waits on the server socket and does not handle the actual
492 data coming in from the client..... XXX */
493 int socket_wait(socket_manager* mgr, int timeout, int sock_fd) {
497 FD_ZERO( &read_set );
498 FD_SET( sock_fd, &read_set );
507 // If timeout is -1, we block indefinitely
508 if( (retval = select( sock_fd + 1, &read_set, NULL, NULL, NULL)) == -1 ) {
509 osrfLogDebug( OSRF_LOG_MARK, "Call to select() interrupted: Sys Error: %s", strerror(errno));
513 } else if( timeout > 0 ) { /* timeout of 0 means don't block */
515 if( (retval = select( sock_fd + 1, &read_set, NULL, NULL, &tv)) == -1 ) {
516 osrfLogDebug( OSRF_LOG_MARK, "Call to select() interrupted: Sys Error: %s", strerror(errno));
521 osrfLogInternal( OSRF_LOG_MARK, "%d active sockets after select()", retval);
522 return _socket_route_data_id(mgr, sock_fd);
526 int socket_wait_all(socket_manager* mgr, int timeout) {
529 osrfLogWarning( OSRF_LOG_MARK, "socket_wait_all(): null mgr" );
535 FD_ZERO( &read_set );
537 socket_node* node = mgr->socket;
540 osrfLogInternal( OSRF_LOG_MARK, "Adding socket fd %d to select set",node->sock_fd);
541 FD_SET( node->sock_fd, &read_set );
542 if(node->sock_fd > max_fd) max_fd = node->sock_fd;
554 // If timeout is -1, there is no timeout passed to the call to select
555 if( (retval = select( max_fd, &read_set, NULL, NULL, NULL)) == -1 ) {
556 osrfLogWarning( OSRF_LOG_MARK, "select() call aborted: %s", strerror(errno));
560 } else if( timeout != 0 ) { /* timeout of 0 means don't block */
562 if( (retval = select( max_fd, &read_set, NULL, NULL, &tv)) == -1 ) {
563 osrfLogWarning( OSRF_LOG_MARK, "select() call aborted: %s", strerror(errno));
568 osrfLogDebug( OSRF_LOG_MARK, "%d active sockets after select()", retval);
569 return _socket_route_data(mgr, retval, &read_set);
572 /* iterates over the sockets in the set and handles active sockets.
573 new sockets connecting to server sockets cause the creation
574 of a new socket node.
575 Any new data read is is passed off to the data_received callback
577 /* determines if we're receiving a new client or data
578 on an existing client */
579 static int _socket_route_data(
580 socket_manager* mgr, int num_active, fd_set* read_set) {
582 if(!(mgr && read_set)) return -1;
584 int last_failed_id = -1;
587 /* come back here if someone yanks a socket_node from beneath us */
590 socket_node* node = mgr->socket;
594 while(node && (handled < num_active)) {
596 int sock_fd = node->sock_fd;
598 if(last_failed_id != -1) {
599 /* in case it was not removed by our overlords */
600 osrfLogInternal( OSRF_LOG_MARK, "Attempting to remove last_failed_id of %d", last_failed_id);
601 socket_remove_node( mgr, last_failed_id );
607 /* does this socket have data? */
608 if( FD_ISSET( sock_fd, read_set ) ) {
610 osrfLogInternal( OSRF_LOG_MARK, "Socket %d active", sock_fd);
612 FD_CLR(sock_fd, read_set);
614 if(node->endpoint == SERVER_SOCKET)
615 _socket_handle_new_client(mgr, node);
618 status = _socket_handle_client_data(mgr, node);
620 /* someone may have yanked a socket_node out from under
621 us...start over with the first socket */
623 last_failed_id = sock_fd;
624 osrfLogInternal( OSRF_LOG_MARK, "Backtracking back to start of loop because "
625 "of -1 return code from _socket_handle_client_data()");
629 if(status == -1) break;
634 if(status == 0) break;
635 if(status == -1) status = 0;
642 /* routes data from a single known socket */
643 static int _socket_route_data_id( socket_manager* mgr, int sock_id) {
644 socket_node* node = socket_find_node(mgr, sock_id);
648 if(node->endpoint == SERVER_SOCKET)
649 _socket_handle_new_client(mgr, node);
651 if(node->endpoint == CLIENT_SOCKET )
652 status = _socket_handle_client_data(mgr, node);
655 socket_remove_node(mgr, sock_id);
665 static int _socket_handle_new_client(socket_manager* mgr, socket_node* node) {
666 if(mgr == NULL || node == NULL) return -1;
670 new_sock_fd = accept(node->sock_fd, NULL, NULL);
671 if(new_sock_fd < 0) {
672 osrfLogWarning( OSRF_LOG_MARK, "_socket_handle_new_client(): accept() failed: %s",
677 if(node->addr_type == INET) {
678 _socket_add_node(mgr, CLIENT_SOCKET, INET, new_sock_fd, node->sock_fd);
679 osrfLogDebug( OSRF_LOG_MARK, "Adding new INET client for %d", node->sock_fd);
681 } else if(node->addr_type == UNIX) {
682 _socket_add_node(mgr, CLIENT_SOCKET, UNIX, new_sock_fd, node->sock_fd);
683 osrfLogDebug( OSRF_LOG_MARK, "Adding new UNIX client for %d", node->sock_fd);
690 static int _socket_handle_client_data(socket_manager* mgr, socket_node* node) {
691 if(mgr == NULL || node == NULL) return -1;
695 int sock_fd = node->sock_fd;
697 memset(buf, 0, RBUFSIZE);
698 set_fl(sock_fd, O_NONBLOCK);
700 osrfLogInternal( OSRF_LOG_MARK, "%ld : Received data at %f\n", (long) getpid(), get_timestamp_millis());
702 while( (read_bytes = recv(sock_fd, buf, RBUFSIZE-1, 0) ) > 0 ) {
703 osrfLogInternal( OSRF_LOG_MARK, "Socket %d Read %d bytes and data: %s", sock_fd, read_bytes, buf);
704 if(mgr->data_received)
705 mgr->data_received(mgr->blob, mgr, sock_fd, buf, node->parent_id);
707 memset(buf, 0, RBUFSIZE);
709 int local_errno = errno; /* capture errno as set by recv() */
711 if(socket_find_node(mgr, sock_fd)) { /* someone may have closed this socket */
712 clr_fl(sock_fd, O_NONBLOCK);
714 if(local_errno != EAGAIN)
715 osrfLogWarning(OSRF_LOG_MARK, " * Error reading socket with error %s", strerror(local_errno));
718 } else { return -1; } /* inform the caller that this node has been tampered with */
720 if(read_bytes == 0) { /* socket closed by client */
721 if(mgr->on_socket_closed) {
722 mgr->on_socket_closed(mgr->blob, sock_fd);
732 void socket_manager_free(socket_manager* mgr) {
733 if(mgr == NULL) return;
736 tmp = mgr->socket->next;
737 socket_disconnect(mgr, mgr->socket->sock_fd);