changeset 374:812af6823eb7

Clean up address handling a tiny, tiny bit.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 23 Jun 2011 15:19:12 +0300
parents ebbe97f2a2a5
children c300ed52886c
files libnnchat.c
diffstat 1 files changed, 18 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/libnnchat.c	Thu Jun 23 14:54:11 2011 +0300
+++ b/libnnchat.c	Thu Jun 23 15:19:12 2011 +0300
@@ -106,6 +106,17 @@
     return conn;
 }
 
+static BOOL nn_get_addr(struct in_addr *addr, struct hostent *hst)
+{
+    if (hst != NULL) {
+        *addr = *(struct in_addr *) (hst->h_addr);
+        return TRUE;
+    } else {
+        addr->s_addr = 0;
+        return FALSE;
+    }
+}
+
 int nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host)
 {
     if (conn == NULL)
@@ -117,11 +128,7 @@
 
     if (host != NULL) {
         conn->proxy.hst = nn_resolve_host(conn, host);
-        if (conn->proxy.hst != NULL)
-            conn->proxy.addr = *(struct in_addr *) (conn->proxy.hst->h_addr);
-        else {
-            conn->proxy.addr.s_addr = 0;
-        }
+        nn_get_addr(&(conn->proxy.addr), conn->proxy.hst);
     } else
         return -2;
 
@@ -142,29 +149,24 @@
         conn->hst = nn_resolve_host(conn, host);
     }
 
-    if (conn->hst != NULL)
-        conn->addr = *(struct in_addr *) (conn->hst->h_addr);
-    else {
-        conn->addr.s_addr = 0;
-    }
-
+    nn_get_addr(&(conn->addr), conn->hst);
 
     /* Prepare for connection */
+    dest.sin_family = AF_INET;
+
     if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST) {
-        dest.sin_family = AF_INET;
         dest.sin_port = htons(conn->proxy.port);
         dest.sin_addr = conn->proxy.addr;
 
         nn_conn_msg(conn, "Connecting to %s proxy %s:%d ...\n",
             nn_proxy_types[conn->proxy.type],
-            inet_ntoa(dest.sin_addr), port);
+            inet_ntoa(conn->proxy.addr), conn->proxy.port);
     } else {
-        dest.sin_family = AF_INET;
-        dest.sin_port = htons(port);
+        dest.sin_port = htons(conn->port);
         dest.sin_addr = conn->addr;
 
         nn_conn_msg(conn, "Connecting to %s:%d ...\n",
-            inet_ntoa(dest.sin_addr), port);
+            inet_ntoa(conn->addr), conn->port);
     }
 
     if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {