X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-socket.c;fp=src%2Fttt-socket.c;h=765cab1104d7c568b22758a76e6c7c1a508289c8;hp=0000000000000000000000000000000000000000;hb=152ea9285ef3804783540d46263a7fc9802bc7db;hpb=49f7f9e3dc198c3593021bff5514f19090a4834c diff --git a/src/ttt-socket.c b/src/ttt-socket.c new file mode 100644 index 0000000..765cab1 --- /dev/null +++ b/src/ttt-socket.c @@ -0,0 +1,156 @@ +/* ttt-socket.c - Simple interface for creating sockets. + * + * Copyright © 2005 Carl Worth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Author: Carl Worth + */ + +#include "ttt-socket.h" + +#include +#include + +/* Initialize a TCP/IP address structure with the given host and port. + * + * Lookup for host (e.g. /etc/hosts and DNA) and port (e.g /etc/services) + * will be performed if necessary. + * + * A special host value of "0.0.0.0" may be used to prepare a server + * socket to bind to all available addresses. + * + * Errors: If any lookup error occurs, this function will not return. + */ +static void +_sockaddr_init (struct sockaddr_in *addr, + const char *host, + const char *port) +{ + struct hostent *hostent; + struct servent *servent; + char *endptr; + long port_long; + uint16_t port_short; + + addr->sin_family = AF_INET; + + hostent = gethostbyname (host); + if (hostent == NULL) + { + fprintf (stderr, "Error: Lookup failed for host %s: %s\n", + host, hstrerror (h_errno)); + exit (1); + } + memcpy (&addr->sin_addr.s_addr, hostent->h_addr_list[0], + sizeof (addr->sin_addr.s_addr)); + + assert (*port != '\0'); + port_long = strtol (port, &endptr, 10); + if (*endptr == '\0') + { + /* Numeric port*/ + if (port_long <= 0 || port_long >= (1 << 16)) { + fprintf (stderr, "Error: Port %ld out of range.\n", port_long); + exit (1); + } + port_short = port_long; + addr->sin_port = htons (port_short); + } else { + /* Named port */ + servent = getservbyname (port, "tcp"); + if (port == NULL) { + fprintf (stderr, "Error: Lookup failed for port %s: %s\n", + port, hstrerror (h_errno)); + exit (1); + } + addr->sin_port = servent->s_port; + } +} + +static int +_wait_for_connection (int listen_socket) +{ + fd_set read_set; + int num_selected; + int flags, err; + int connected_socket; + + FD_ZERO (&read_set); + FD_SET (listen_socket, &read_set); + + while (1) { + num_selected = select (listen_socket + 1, + &read_set, NULL, NULL, + NULL); + + flags = fcntl (listen_socket, F_GETFL); + flags |= O_NONBLOCK; + xfcntl (listen_socket, F_SETFL, flags); + + connected_socket = accept (listen_socket, 0, 0); + err = errno; + + flags &= ~O_NONBLOCK; + xfcntl (listen_socket, F_SETFL, flags); + + if (connected_socket != -1) + return connected_socket; + + if (err == EWOULDBLOCK || err == EAGAIN) + continue; + + fprintf (stderr, "Error: accept failed: %s. Aborting.\n", + strerror (err)); + exit (1); + } +} + +/* Exported: see ttt-socket.h for documentation. */ +int +ttt_socket_create_server (const char *host, const char *port) +{ + int listen_socket; + struct sockaddr_in addr; + + listen_socket = xsocket (PF_INET, SOCK_STREAM, 0); + + _sockaddr_init (&addr, host, port); + +#ifdef SO_REUSEADDR + { + int one = 1; + setsockopt (listen_socket, SOL_SOCKET, + SO_REUSEADDR, (char *) &one, sizeof (int)); + } +#endif + + xbind (listen_socket, (struct sockaddr *) &addr, sizeof (addr)); + + xlisten (listen_socket, SOMAXCONN); + + return listen_socket; +} + +/* Exported: see ttt-socket.h for documentation. */ +void +ttt_socket_accept (int listen_socket, ttt_socket_dispatch_func_t dispatch) +{ + int connected_socket; + + connected_socket = _wait_for_connection (listen_socket); + + (dispatch) (connected_socket); +}