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, const 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) {
102 if( inet_aton( listen_ip, &addr ) )
103 server_addr.sin_addr.s_addr = addr.s_addr;
105 osrfLogError( OSRF_LOG_MARK, "Listener address is invalid: %s", listen_ip );
109 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
112 server_addr.sin_port = htons(port);
115 if(bind( sock_fd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) {
116 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_server(): cannot bind to port %d: %s",
117 port, strerror( errno ) );
122 if(listen(sock_fd, 20) == -1) {
123 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_server(): listen() returned error: %s",
128 _socket_add_node(mgr, SERVER_SOCKET, INET, sock_fd, 0);
132 int socket_open_unix_server(socket_manager* mgr, char* path) {
133 if(mgr == NULL || path == NULL) return -1;
135 osrfLogDebug( OSRF_LOG_MARK, "opening unix socket at %s", path);
137 struct sockaddr_un server_addr;
140 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
142 osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_server(): socket() failed: %s",
147 server_addr.sun_family = AF_UNIX;
148 strcpy(server_addr.sun_path, path);
151 if( bind(sock_fd, (struct sockaddr*) &server_addr,
152 sizeof(struct sockaddr_un)) < 0) {
153 osrfLogWarning( OSRF_LOG_MARK,
154 "socket_open_unix_server(): cannot bind to unix port %s: %s",
155 path, strerror( errno ) );
160 if(listen(sock_fd, 20) == -1) {
161 osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_server(): listen() returned error: %s",
166 osrfLogDebug( OSRF_LOG_MARK, "unix socket successfully opened");
170 /* causing problems with router for some reason ... */
171 //osrfLogDebug( OSRF_LOG_MARK, "Setting SO_REUSEADDR");
172 //setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
174 //osrfLogDebug( OSRF_LOG_MARK, "Setting TCP_NODELAY");
175 setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i));
177 _socket_add_node(mgr, SERVER_SOCKET, UNIX, sock_fd, 0);
183 int socket_open_udp_server(
184 socket_manager* mgr, int port, const char* listen_ip ) {
187 struct sockaddr_in server_addr;
190 if( (sockfd = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
191 osrfLogWarning( OSRF_LOG_MARK, "Unable to create UDP socket: %s", strerror( errno ) );
195 server_addr.sin_family = AF_INET;
196 server_addr.sin_port = htons(port);
199 if( inet_aton( listen_ip, &addr ) )
200 server_addr.sin_addr.s_addr = addr.s_addr;
202 osrfLogError( OSRF_LOG_MARK, "UDP listener address is invalid: %s", listen_ip );
205 } else server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
208 if( (bind (sockfd, (struct sockaddr *) &server_addr,sizeof(server_addr))) ) {
209 osrfLogWarning( OSRF_LOG_MARK, "Unable to bind to UDP port %d: %s",
210 port, strerror( errno ) );
214 _socket_add_node(mgr, SERVER_SOCKET, INET, sockfd, 0);
219 int socket_open_tcp_client(socket_manager* mgr, int port, const char* dest_addr) {
221 struct sockaddr_in remoteAddr, localAddr;
222 struct hostent *hptr;
225 // ------------------------------------------------------------------
227 // ------------------------------------------------------------------
229 if( (sock_fd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
230 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_client(): Cannot create TCP socket: %s",
236 //osrfLogDebug( OSRF_LOG_MARK, "Setting TCP_NODELAY");
237 setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i));
240 // ------------------------------------------------------------------
242 // ------------------------------------------------------------------
244 if( (hptr = gethostbyname( dest_addr ) ) == NULL ) {
245 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_client(): Unknown Host => %s: %s",
246 dest_addr, strerror( errno ) );
250 // ------------------------------------------------------------------
251 // Construct server info struct
252 // ------------------------------------------------------------------
253 memset( &remoteAddr, 0, sizeof(remoteAddr));
254 remoteAddr.sin_family = AF_INET;
255 remoteAddr.sin_port = htons( port );
256 memcpy( (char*) &remoteAddr.sin_addr.s_addr,
257 hptr->h_addr_list[0], hptr->h_length );
259 // ------------------------------------------------------------------
260 // Construct local info struct
261 // ------------------------------------------------------------------
262 memset( &localAddr, 0, sizeof( localAddr ) );
263 localAddr.sin_family = AF_INET;
264 localAddr.sin_addr.s_addr = htonl( INADDR_ANY );
265 localAddr.sin_port = htons(0);
267 // ------------------------------------------------------------------
268 // Bind to a local port
269 // ------------------------------------------------------------------
271 if( bind( sock_fd, (struct sockaddr *) &localAddr, sizeof( localAddr ) ) < 0 ) {
272 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_client(): Cannot bind to local port: %s",
277 // ------------------------------------------------------------------
279 // ------------------------------------------------------------------
281 if( connect( sock_fd, (struct sockaddr*) &remoteAddr, sizeof( struct sockaddr_in ) ) < 0 ) {
282 osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_client(): Cannot connect to server %s: %s",
283 dest_addr, strerror(errno) );
287 _socket_add_node(mgr, CLIENT_SOCKET, INET, sock_fd, -1 );
293 int socket_open_udp_client(
294 socket_manager* mgr, int port, char* dest_addr) {
297 struct sockaddr_in client_addr, server_addr;
298 struct hostent* host;
301 if( (host = gethostbyname(dest_addr)) == NULL) {
302 osrfLogWarning( OSRF_LOG_MARK, "Unable to resolve host: %s: %s",
303 dest_addr, strerror( errno ) );
307 server_addr.sin_family = host->h_addrtype;
308 memcpy((char *) &server_addr.sin_addr.s_addr,
309 host->h_addr_list[0], host->h_length);
310 server_addr.sin_port = htons(port);
313 if( (sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0 ) {
314 osrfLogWarning( OSRF_LOG_MARK, "socket_open_udp_client(): Unable to create UDP socket: %s", strerror( errno ) );
318 client_addr.sin_family = AF_INET;
319 client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
320 client_addr.sin_port = htons(0);
323 if( (bind(sockfd, (struct sockaddr *) &client_addr, sizeof(client_addr))) < 0 ) {
324 osrfLogWarning( OSRF_LOG_MARK, "Unable to bind UDP socket: %s", strerror( errno ) );
328 _socket_add_node(mgr, CLIENT_SOCKET, INET, sockfd, -1 );
334 int socket_open_unix_client(socket_manager* mgr, const char* sock_path) {
337 struct sockaddr_un usock;
340 if( (sock_fd = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
341 osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_client(): Cannot create UNIX socket: %s", strerror( errno ) );
345 usock.sun_family = AF_UNIX;
346 strcpy( usock.sun_path, sock_path );
348 len = sizeof( usock.sun_family ) + strlen( usock.sun_path );
351 if( connect( sock_fd, (struct sockaddr *) &usock, len ) < 0 ) {
352 osrfLogWarning( OSRF_LOG_MARK, "Error connecting to unix socket: %s",
357 _socket_add_node(mgr, CLIENT_SOCKET, UNIX, sock_fd, -1 );
363 /* returns the socket_node with the given sock_fd */
364 static socket_node* socket_find_node(socket_manager* mgr, int sock_fd) {
365 if(mgr == NULL) return NULL;
366 socket_node* node = mgr->socket;
368 if(node->sock_fd == sock_fd)
375 /* removes the node with the given sock_fd from the list and frees it */
376 static void socket_remove_node(socket_manager* mgr, int sock_fd) {
378 if(mgr == NULL) return;
380 osrfLogDebug( OSRF_LOG_MARK, "removing socket %d", sock_fd);
382 socket_node* head = mgr->socket;
383 socket_node* tail = head;
384 if(head == NULL) return;
386 /* if removing the first node in the list */
387 if(head->sock_fd == sock_fd) {
388 mgr->socket = head->next;
395 /* if removing any other node */
397 if(head->sock_fd == sock_fd) {
398 tail->next = head->next;
408 void _socket_print_list(socket_manager* mgr) {
409 if(mgr == NULL) return;
410 socket_node* node = mgr->socket;
411 osrfLogDebug( OSRF_LOG_MARK, "socket_node list: [");
413 osrfLogDebug( OSRF_LOG_MARK, "sock_fd: %d | parent_id: %d",
414 node->sock_fd, node->parent_id);
417 osrfLogDebug( OSRF_LOG_MARK, "]");
420 /* sends the given data to the given socket */
421 int socket_send(int sock_fd, const char* data) {
422 return _socket_send( sock_fd, data, 0);
426 static int _socket_send(int sock_fd, const char* data, int flags) {
428 signal(SIGPIPE, SIG_IGN); /* in case a unix socket was closed */
431 size_t r = send( sock_fd, data, strlen(data), flags );
432 int local_errno = errno;
435 osrfLogWarning( OSRF_LOG_MARK, "_socket_send(): Error sending data with return %d", r );
436 osrfLogWarning( OSRF_LOG_MARK, "Last Sys Error: %s", strerror(local_errno));
444 /* sends the given data to the given socket.
445 * sets the send flag MSG_DONTWAIT which will allow the
446 * process to continue even if the socket buffer is full
447 * returns 0 on success, -1 otherwise */
448 //int socket_send_nowait( int sock_fd, const char* data) {
449 // return _socket_send( sock_fd, data, MSG_DONTWAIT);
454 * Waits at most usecs microseconds for the send buffer of the given
455 * socket to accept new data. This does not guarantee that the
456 * socket will accept all the data we want to give it.
458 int socket_send_timeout( int sock_fd, const char* data, int usecs ) {
461 FD_ZERO( &write_set );
462 FD_SET( sock_fd, &write_set );
465 int secs = (int) usecs / mil;
466 usecs = usecs - (secs * mil);
473 int ret = select( sock_fd + 1, NULL, &write_set, NULL, &tv);
474 if( ret > 0 ) return _socket_send( sock_fd, data, 0);
476 osrfLogError(OSRF_LOG_MARK, "socket_send_timeout(): "
477 "timed out on send for socket %d after %d secs, %d usecs: %s",
478 sock_fd, secs, usecs, strerror( errno ) );
484 /* disconnects the node with the given sock_fd and removes
485 it from the socket set */
486 void socket_disconnect(socket_manager* mgr, int sock_fd) {
487 osrfLogInternal( OSRF_LOG_MARK, "Closing socket %d", sock_fd);
489 socket_remove_node(mgr, sock_fd);
493 /* we assume that if select() fails, the socket is no longer valid */
494 int socket_connected(int sock_fd) {
496 FD_ZERO( &read_set );
497 FD_SET( sock_fd, &read_set );
498 if( select( sock_fd + 1, &read_set, NULL, NULL, NULL) == -1 )
504 /* this only waits on the server socket and does not handle the actual
505 data coming in from the client..... XXX */
506 int socket_wait(socket_manager* mgr, int timeout, int sock_fd) {
510 FD_ZERO( &read_set );
511 FD_SET( sock_fd, &read_set );
520 // If timeout is -1, we block indefinitely
521 if( (retval = select( sock_fd + 1, &read_set, NULL, NULL, NULL)) == -1 ) {
522 osrfLogDebug( OSRF_LOG_MARK, "Call to select() interrupted: Sys Error: %s", strerror(errno));
526 } else if( timeout > 0 ) { /* timeout of 0 means don't block */
528 if( (retval = select( sock_fd + 1, &read_set, NULL, NULL, &tv)) == -1 ) {
529 osrfLogDebug( OSRF_LOG_MARK, "Call to select() interrupted: Sys Error: %s", strerror(errno));
534 osrfLogInternal( OSRF_LOG_MARK, "%d active sockets after select()", retval);
535 return _socket_route_data_id(mgr, sock_fd);
539 int socket_wait_all(socket_manager* mgr, int timeout) {
542 osrfLogWarning( OSRF_LOG_MARK, "socket_wait_all(): null mgr" );
548 FD_ZERO( &read_set );
550 socket_node* node = mgr->socket;
553 osrfLogInternal( OSRF_LOG_MARK, "Adding socket fd %d to select set",node->sock_fd);
554 FD_SET( node->sock_fd, &read_set );
555 if(node->sock_fd > max_fd) max_fd = node->sock_fd;
567 // If timeout is -1, there is no timeout passed to the call to select
568 if( (retval = select( max_fd, &read_set, NULL, NULL, NULL)) == -1 ) {
569 osrfLogWarning( OSRF_LOG_MARK, "select() call aborted: %s", strerror(errno));
573 } else if( timeout != 0 ) { /* timeout of 0 means don't block */
575 if( (retval = select( max_fd, &read_set, NULL, NULL, &tv)) == -1 ) {
576 osrfLogWarning( OSRF_LOG_MARK, "select() call aborted: %s", strerror(errno));
581 osrfLogDebug( OSRF_LOG_MARK, "%d active sockets after select()", retval);
582 return _socket_route_data(mgr, retval, &read_set);
585 /* iterates over the sockets in the set and handles active sockets.
586 new sockets connecting to server sockets cause the creation
587 of a new socket node.
588 Any new data read is is passed off to the data_received callback
590 /* determines if we're receiving a new client or data
591 on an existing client */
592 static int _socket_route_data(
593 socket_manager* mgr, int num_active, fd_set* read_set) {
595 if(!(mgr && read_set)) return -1;
597 int last_failed_id = -1;
600 /* come back here if someone yanks a socket_node from beneath us */
603 socket_node* node = mgr->socket;
607 while(node && (handled < num_active)) {
609 int sock_fd = node->sock_fd;
611 if(last_failed_id != -1) {
612 /* in case it was not removed by our overlords */
613 osrfLogInternal( OSRF_LOG_MARK, "Attempting to remove last_failed_id of %d", last_failed_id);
614 socket_remove_node( mgr, last_failed_id );
620 /* does this socket have data? */
621 if( FD_ISSET( sock_fd, read_set ) ) {
623 osrfLogInternal( OSRF_LOG_MARK, "Socket %d active", sock_fd);
625 FD_CLR(sock_fd, read_set);
627 if(node->endpoint == SERVER_SOCKET)
628 _socket_handle_new_client(mgr, node);
631 status = _socket_handle_client_data(mgr, node);
633 /* someone may have yanked a socket_node out from under
634 us...start over with the first socket */
636 last_failed_id = sock_fd;
637 osrfLogInternal( OSRF_LOG_MARK, "Backtracking back to start of loop because "
638 "of -1 return code from _socket_handle_client_data()");
642 if(status == -1) break;
647 if(status == 0) break;
648 if(status == -1) status = 0;
655 /* routes data from a single known socket */
656 static int _socket_route_data_id( socket_manager* mgr, int sock_id) {
657 socket_node* node = socket_find_node(mgr, sock_id);
661 if(node->endpoint == SERVER_SOCKET)
662 _socket_handle_new_client(mgr, node);
664 if(node->endpoint == CLIENT_SOCKET )
665 status = _socket_handle_client_data(mgr, node);
668 socket_remove_node(mgr, sock_id);
678 static int _socket_handle_new_client(socket_manager* mgr, socket_node* node) {
679 if(mgr == NULL || node == NULL) return -1;
683 new_sock_fd = accept(node->sock_fd, NULL, NULL);
684 if(new_sock_fd < 0) {
685 osrfLogWarning( OSRF_LOG_MARK, "_socket_handle_new_client(): accept() failed: %s",
690 if(node->addr_type == INET) {
691 _socket_add_node(mgr, CLIENT_SOCKET, INET, new_sock_fd, node->sock_fd);
692 osrfLogDebug( OSRF_LOG_MARK, "Adding new INET client for %d", node->sock_fd);
694 } else if(node->addr_type == UNIX) {
695 _socket_add_node(mgr, CLIENT_SOCKET, UNIX, new_sock_fd, node->sock_fd);
696 osrfLogDebug( OSRF_LOG_MARK, "Adding new UNIX client for %d", node->sock_fd);
703 static int _socket_handle_client_data(socket_manager* mgr, socket_node* node) {
704 if(mgr == NULL || node == NULL) return -1;
708 int sock_fd = node->sock_fd;
710 osrf_clearbuf(buf, sizeof(buf));
711 set_fl(sock_fd, O_NONBLOCK);
713 osrfLogInternal( OSRF_LOG_MARK, "%ld : Received data at %f\n", (long) getpid(), get_timestamp_millis());
715 while( (read_bytes = recv(sock_fd, buf, RBUFSIZE-1, 0) ) > 0 ) {
716 osrfLogInternal( OSRF_LOG_MARK, "Socket %d Read %d bytes and data: %s", sock_fd, read_bytes, buf);
717 if(mgr->data_received)
718 mgr->data_received(mgr->blob, mgr, sock_fd, buf, node->parent_id);
720 osrf_clearbuf(buf, sizeof(buf));
722 int local_errno = errno; /* capture errno as set by recv() */
724 if(socket_find_node(mgr, sock_fd)) { /* someone may have closed this socket */
725 clr_fl(sock_fd, O_NONBLOCK);
727 if(local_errno != EAGAIN)
728 osrfLogWarning(OSRF_LOG_MARK, " * Error reading socket with error %s", strerror(local_errno));
731 } else { return -1; } /* inform the caller that this node has been tampered with */
733 if(read_bytes == 0) { /* socket closed by client */
734 if(mgr->on_socket_closed) {
735 mgr->on_socket_closed(mgr->blob, sock_fd);
745 void socket_manager_free(socket_manager* mgr) {
746 if(mgr == NULL) return;
749 tmp = mgr->socket->next;
750 socket_disconnect(mgr, mgr->socket->sock_fd);