Mercurial > hg > nnchat
comparison libnnchat.c @ 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 | b9f0bdad6285 |
children | 4523fc0941e8 |
comparison
equal
deleted
inserted
replaced
373:ebbe97f2a2a5 | 374:812af6823eb7 |
---|---|
104 conn->msgfunc = msgfunc; | 104 conn->msgfunc = msgfunc; |
105 | 105 |
106 return conn; | 106 return conn; |
107 } | 107 } |
108 | 108 |
109 static BOOL nn_get_addr(struct in_addr *addr, struct hostent *hst) | |
110 { | |
111 if (hst != NULL) { | |
112 *addr = *(struct in_addr *) (hst->h_addr); | |
113 return TRUE; | |
114 } else { | |
115 addr->s_addr = 0; | |
116 return FALSE; | |
117 } | |
118 } | |
119 | |
109 int nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host) | 120 int nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host) |
110 { | 121 { |
111 if (conn == NULL) | 122 if (conn == NULL) |
112 return -1; | 123 return -1; |
113 | 124 |
115 conn->proxy.port = port; | 126 conn->proxy.port = port; |
116 conn->proxy.host = th_strdup(host); | 127 conn->proxy.host = th_strdup(host); |
117 | 128 |
118 if (host != NULL) { | 129 if (host != NULL) { |
119 conn->proxy.hst = nn_resolve_host(conn, host); | 130 conn->proxy.hst = nn_resolve_host(conn, host); |
120 if (conn->proxy.hst != NULL) | 131 nn_get_addr(&(conn->proxy.addr), conn->proxy.hst); |
121 conn->proxy.addr = *(struct in_addr *) (conn->proxy.hst->h_addr); | |
122 else { | |
123 conn->proxy.addr.s_addr = 0; | |
124 } | |
125 } else | 132 } else |
126 return -2; | 133 return -2; |
127 | 134 |
128 return 0; | 135 return 0; |
129 } | 136 } |
140 if (host != NULL) { | 147 if (host != NULL) { |
141 conn->host = th_strdup(host); | 148 conn->host = th_strdup(host); |
142 conn->hst = nn_resolve_host(conn, host); | 149 conn->hst = nn_resolve_host(conn, host); |
143 } | 150 } |
144 | 151 |
145 if (conn->hst != NULL) | 152 nn_get_addr(&(conn->addr), conn->hst); |
146 conn->addr = *(struct in_addr *) (conn->hst->h_addr); | |
147 else { | |
148 conn->addr.s_addr = 0; | |
149 } | |
150 | |
151 | 153 |
152 /* Prepare for connection */ | 154 /* Prepare for connection */ |
155 dest.sin_family = AF_INET; | |
156 | |
153 if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST) { | 157 if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST) { |
154 dest.sin_family = AF_INET; | |
155 dest.sin_port = htons(conn->proxy.port); | 158 dest.sin_port = htons(conn->proxy.port); |
156 dest.sin_addr = conn->proxy.addr; | 159 dest.sin_addr = conn->proxy.addr; |
157 | 160 |
158 nn_conn_msg(conn, "Connecting to %s proxy %s:%d ...\n", | 161 nn_conn_msg(conn, "Connecting to %s proxy %s:%d ...\n", |
159 nn_proxy_types[conn->proxy.type], | 162 nn_proxy_types[conn->proxy.type], |
160 inet_ntoa(dest.sin_addr), port); | 163 inet_ntoa(conn->proxy.addr), conn->proxy.port); |
161 } else { | 164 } else { |
162 dest.sin_family = AF_INET; | 165 dest.sin_port = htons(conn->port); |
163 dest.sin_port = htons(port); | |
164 dest.sin_addr = conn->addr; | 166 dest.sin_addr = conn->addr; |
165 | 167 |
166 nn_conn_msg(conn, "Connecting to %s:%d ...\n", | 168 nn_conn_msg(conn, "Connecting to %s:%d ...\n", |
167 inet_ntoa(dest.sin_addr), port); | 169 inet_ntoa(conn->addr), conn->port); |
168 } | 170 } |
169 | 171 |
170 if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) { | 172 if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) { |
171 conn->err = nn_get_socket_errno(); | 173 conn->err = nn_get_socket_errno(); |
172 nn_conn_err(conn, "Could not open socket: %s\n", nn_get_socket_errstr(conn->err)); | 174 nn_conn_err(conn, "Could not open socket: %s\n", nn_get_socket_errstr(conn->err)); |