From: Carl Worth Date: Tue, 29 Nov 2005 13:44:54 +0000 (+0000) Subject: 2005-11-29 Carl Worth X-Git-Url: https://git.cworth.org/git?p=ttt;a=commitdiff_plain;h=7de44ed398081656115dce6db7d0a04c1a50bb34 2005-11-29 Carl Worth * src/ttt-socket.h: * src/ttt-socket.c: (ttt_socket_create_client): New function for creating a client socket. * src/x.h: * src/x.c: (xconnect): Add xconnect. Return a couple of useful error status values so that callers can recover on connection refused or host unreachable. * src/ttt.h: Add TTT_STATUS_CONNECTION_REFUSED and TTT_STATUS_NETWORK_UNREACHABLE. --- diff --git a/ChangeLog b/ChangeLog index 3e62744..27e0d4b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2005-11-29 Carl Worth + + * src/ttt-socket.h: + * src/ttt-socket.c: (ttt_socket_create_client): New function for + creating a client socket. + + * src/x.h: + * src/x.c: (xconnect): Add xconnect. Return a couple of useful + error status values so that callers can recover on connection + refused or host unreachable. + + * src/ttt.h: Add TTT_STATUS_CONNECTION_REFUSED and + TTT_STATUS_NETWORK_UNREACHABLE. + 2005-11-28 Kevin Worth * PROTOCOL: Changed board cell numbering to start with zero diff --git a/src/ttt-socket.c b/src/ttt-socket.c index 199535f..20b96e2 100644 --- a/src/ttt-socket.c +++ b/src/ttt-socket.c @@ -157,6 +157,32 @@ ttt_socket_accept (int listen_socket, (accept) (closure, connected_socket); } +/* Exported: see wdo-socket.h for documentation. */ +ttt_status_t +ttt_socket_create_client (const char *host, + const char *port, + int *socket_ret) +{ + ttt_status_t status; + struct sockaddr_in addr; + int socket; + + *socket_ret = -1; + + socket = xsocket (PF_INET, SOCK_STREAM, 0); + + _sockaddr_init (&addr, host, port); + + status = xconnect (socket, (struct sockaddr *) &addr, sizeof (addr)); + if (status) { + shutdown (socket, SHUT_RDWR); + return status; + } + + *socket_ret = socket; + return TTT_STATUS_SUCCESS; +} + /* Exported: see ttt-socket.h for documentation. */ void ttt_socket_read (int socket, diff --git a/src/ttt-socket.h b/src/ttt-socket.h index 6b35ef2..c3ca0bd 100644 --- a/src/ttt-socket.h +++ b/src/ttt-socket.h @@ -55,7 +55,25 @@ ttt_socket_accept (int listen_socket, ttt_socket_accept_func_t accept, void *closure); -/* Performa a blocking read, until all count bytes are read from the +/* Create a socket, and connect it to the given host and port, + * returing it in socket_ret. + * + * Lookup for host (e.g. /etc/hosts and DNS) and port (e.g /etc/services) + * will be performed if necessary. + * + * Return value: TTT_STATUS_SUCCESS if successful. + * TTT_STATUS_CONNECTION_REFUSED: no server listening. + * TTT_STATUS_NETWORK_UNREACHABLE: + * + * Errors: If any error other than those listed above occurs, this + * function will not return. + */ +ttt_status_t +ttt_socket_create_client (const char *host, + const char *port, + int *socket_ret); + +/* Perform a blocking read, until all count bytes are read from the * socket to buf, which must be of size count or larger. * * Errors: If any errors occur, this function does not return. diff --git a/src/ttt.h b/src/ttt.h index c3e8c5c..1b22ba0 100644 --- a/src/ttt.h +++ b/src/ttt.h @@ -56,8 +56,6 @@ do { \ #define TTT_PRINTF_FORMAT(fmt_index, va_index) #endif -#include "x.h" - typedef int ttt_bool_t; #define FALSE 0 @@ -66,10 +64,14 @@ typedef int ttt_bool_t; typedef enum { TTT_STATUS_SUCCESS = 0, TTT_STATUS_FAILURE, - TTT_STATUS_EOF + TTT_STATUS_EOF, + TTT_STATUS_CONNECTION_REFUSED, + TTT_STATUS_NETWORK_UNREACHABLE } ttt_status_t; typedef struct _ttt_server ttt_server_t; typedef struct _ttt_client ttt_client_t; +#include "x.h" + #endif diff --git a/src/x.c b/src/x.c index dc5c1dd..a95fedf 100644 --- a/src/x.c +++ b/src/x.c @@ -205,6 +205,29 @@ xbind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen) } } +ttt_status_t +xconnect (int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) +{ + int ret; + + ret = connect (sockfd, serv_addr, addrlen); + if (ret == -1) { + switch (errno) { + case ECONNREFUSED: + return TTT_STATUS_CONNECTION_REFUSED; + case EHOSTUNREACH: + case ENETUNREACH: + return TTT_STATUS_NETWORK_UNREACHABLE; + default: + fprintf (stderr, "Error: connect failed (errno = %d): %s. Aborting.\n", + errno, strerror (errno)); + exit (1); + } + } + + return TTT_STATUS_SUCCESS; +} + void xlisten (int s, int backlog) { diff --git a/src/x.h b/src/x.h index b3384af..3b7f208 100644 --- a/src/x.h +++ b/src/x.h @@ -63,6 +63,9 @@ xsocket (int domain, int type, int protocol); void xbind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); +ttt_status_t +xconnect (int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); + void xlisten (int s, int backlog);