Mercurial > hg > nnchat
comparison network.c @ 604:37ab4725e4f9
In preparation for SOCKS 5 support, move some structs etc. into network.c
from the header file and rename them to socks4 specific.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 20 May 2014 02:10:52 +0300 |
parents | 4bae14092b78 |
children | ad00f2bbb615 |
comparison
equal
deleted
inserted
replaced
603:0a30bf8db004 | 604:37ab4725e4f9 |
---|---|
4 * (C) Copyright 2008-2013 Tecnic Software productions (TNSP) | 4 * (C) Copyright 2008-2013 Tecnic Software productions (TNSP) |
5 */ | 5 */ |
6 #include "network.h" | 6 #include "network.h" |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 struct nn_socks4_t | |
10 { | |
11 uint8_t version; | |
12 uint8_t command; | |
13 in_port_t port; | |
14 in_addr_t addr; | |
15 } __attribute__((__packed__)); | |
16 | |
17 struct nn_socks4_res_t | |
18 { | |
19 uint8_t nb; | |
20 uint8_t result; | |
21 in_port_t port; | |
22 in_addr_t addr; | |
23 } __attribute__((__packed__)); | |
24 | |
25 | |
9 static BOOL nn_network_inited = FALSE; | 26 static BOOL nn_network_inited = FALSE; |
27 | |
10 | 28 |
11 static const char *nn_proxy_types[] = | 29 static const char *nn_proxy_types[] = |
12 { | 30 { |
13 "none", | 31 "none", |
14 "SOCKS 4", | 32 "SOCKS 4", |
220 FD_SET(conn->socket, &(conn->sockfds)); | 238 FD_SET(conn->socket, &(conn->sockfds)); |
221 | 239 |
222 // Proxy-specific setup | 240 // Proxy-specific setup |
223 if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) | 241 if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) |
224 { | 242 { |
225 struct nn_socks_t *socksh; | 243 struct nn_socks4_t *socksh; |
226 size_t bufsiz; | 244 size_t bufsiz; |
227 char *ptr, *buf; | 245 char *ptr, *buf; |
228 int tries, status = -1; | 246 int tries, status = -1; |
229 | 247 |
230 nn_conn_msg(conn, "Initializing proxy negotiation.\n"); | 248 nn_conn_msg(conn, "Initializing SOCKS 4/a proxy negotiation.\n"); |
231 | 249 |
232 bufsiz = sizeof(struct nn_socks_t) + strlen(conn->proxy.userid) + 1; | 250 bufsiz = sizeof(struct nn_socks4_t) + strlen(conn->proxy.userid) + 1; |
233 if (conn->proxy.type == NN_PROXY_SOCKS4A) | 251 if (conn->proxy.type == NN_PROXY_SOCKS4A) |
234 bufsiz += strlen(conn->host) + 1; | 252 bufsiz += strlen(conn->host) + 1; |
235 | 253 |
236 ptr = buf = th_malloc(bufsiz); | 254 if ((ptr = buf = th_malloc(bufsiz)) == NULL) |
237 if (buf == NULL) | |
238 { | 255 { |
239 conn->err = -1; | 256 conn->err = -1; |
240 nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz); | 257 nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz); |
241 goto error; | 258 goto error; |
242 } | 259 } |
243 | 260 |
244 // Create SOCKS 4/4A request | 261 // Create SOCKS 5 handshake |
245 socksh = (struct nn_socks_t *) buf; | 262 socksh = (struct nn_socks4_t *) buf; |
246 socksh->version = 4; | 263 socksh->version = 4; |
247 socksh->command = SOCKS_CMD_CONNECT; | 264 socksh->command = NN_PROXY_CMD_CONNECT; |
248 socksh->port = htons(port); | 265 socksh->port = htons(port); |
249 socksh->addr = (conn->proxy.type == NN_PROXY_SOCKS4A) ? htonl(0x00000032) : conn->addr.s_addr; | 266 socksh->addr = (conn->proxy.type == NN_PROXY_SOCKS4A) ? htonl(0x00000032) : conn->addr.s_addr; |
250 ptr += sizeof(struct nn_socks_t); | 267 ptr += sizeof(struct nn_socks4_t); |
251 | 268 |
252 strcpy(ptr, conn->proxy.userid); | 269 strcpy(ptr, conn->proxy.userid); |
253 | 270 |
254 if (conn->proxy.type == NN_PROXY_SOCKS4A) | 271 if (conn->proxy.type == NN_PROXY_SOCKS4A) |
255 { | 272 { |
280 } | 297 } |
281 | 298 |
282 // Check results | 299 // Check results |
283 if (status == 0) | 300 if (status == 0) |
284 { | 301 { |
285 struct nn_socks_res_t *res = (struct nn_socks_res_t *) &(conn->buf); | 302 struct nn_socks4_res_t *res = (struct nn_socks4_res_t *) &(conn->buf); |
286 if (res->nb != 0) | 303 if (res->nb != 0) |
287 { | 304 { |
288 nn_conn_err(conn, "Invalid SOCKS server reply, does not begin with NUL byte (%d).\n", res->nb); | 305 nn_conn_err(conn, "Invalid SOCKS server reply, does not begin with NUL byte (%d).\n", res->nb); |
289 goto error; | 306 goto error; |
290 } | 307 } |