+2005-11-29 Carl Worth <cworth@cworth.org>
+
+ * 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 <kevin@theworths.org>
* PROTOCOL: Changed board cell numbering to start with zero
(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,
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: <obvious meaning>
+ *
+ * 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.
#define TTT_PRINTF_FORMAT(fmt_index, va_index)
#endif
-#include "x.h"
-
typedef int ttt_bool_t;
#define FALSE 0
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
}
}
+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)
{
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);