# HG changeset patch # User Matti Hamalainen # Date 1308831552 -10800 # Node ID 812af6823eb79f924bdf941718c50ba8fee1431a # Parent ebbe97f2a2a596bc0f8bd82d1e0c31eee46daf00 Clean up address handling a tiny, tiny bit. diff -r ebbe97f2a2a5 -r 812af6823eb7 libnnchat.c --- 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) {