]> git.cworth.org Git - ttt/commitdiff
2005-11-29 Carl Worth <cworth@cworth.org>
authorCarl Worth <carl@theworths.org>
Tue, 29 Nov 2005 13:44:54 +0000 (13:44 +0000)
committerCarl Worth <carl@theworths.org>
Tue, 29 Nov 2005 13:44:54 +0000 (13:44 +0000)
        * 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.

ChangeLog
src/ttt-socket.c
src/ttt-socket.h
src/ttt.h
src/x.c
src/x.h

index 3e627443e50a169a81d3d1326a312b25b45789b2..27e0d4bb1894aa23844e751a30463bd06526a022 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+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
 2005-11-28  Kevin Worth  <kevin@theworths.org>
 
        * PROTOCOL: Changed board cell numbering to start with zero
index 199535f300e200d0970341ea6177e53cf35bbf70..20b96e263056103b87752fe6da432b5c8a89b63b 100644 (file)
@@ -157,6 +157,32 @@ ttt_socket_accept (int                              listen_socket,
     (accept) (closure, connected_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,
 /* Exported: see ttt-socket.h for documentation. */
 void
 ttt_socket_read (int    socket,
index 6b35ef23649111dca03457b95925b4640b075ab6..c3ca0bdd604263e65e49af91f1a106ad36b79b03 100644 (file)
@@ -55,7 +55,25 @@ ttt_socket_accept (int                                listen_socket,
                   ttt_socket_accept_func_t      accept,
                   void                         *closure);
 
                   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.
  * socket to buf, which must be of size count or larger.
  *
  * Errors: If any errors occur, this function does not return.
index c3e8c5c25fa88abb9c1b91df910ebc889bc7f536..1b22ba0bc0d33db5c1e9a427df7c593512a07d57 100644 (file)
--- a/src/ttt.h
+++ b/src/ttt.h
@@ -56,8 +56,6 @@ do {                                  \
 #define TTT_PRINTF_FORMAT(fmt_index, va_index)
 #endif
 
 #define TTT_PRINTF_FORMAT(fmt_index, va_index)
 #endif
 
-#include "x.h"
-
 typedef int ttt_bool_t;
 
 #define FALSE 0
 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,
 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;
 
 } ttt_status_t;
 
 typedef struct _ttt_server ttt_server_t;
 typedef struct _ttt_client ttt_client_t;
 
+#include "x.h"
+
 #endif
 #endif
diff --git a/src/x.c b/src/x.c
index dc5c1dd38e807ee300f9a1d77cc1ef187ef79f7d..a95fedfbc5e8a142606698cf4ab629368e1e2e8d 100644 (file)
--- 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)
 {
 void
 xlisten (int s, int backlog)
 {
diff --git a/src/x.h b/src/x.h
index b3384af43d93b37aad0a96523cef97d2f616ab66..3b7f208d324e10b2582040779c36d78f443b72d4 100644 (file)
--- 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);
 
 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);
 
 void
 xlisten (int s, int backlog);