summaryrefslogtreecommitdiff
path: root/src/util/socks.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/socks.c')
-rw-r--r--src/util/socks.c534
1 files changed, 268 insertions, 266 deletions
diff --git a/src/util/socks.c b/src/util/socks.c
index ca3d1e8c4..c2e012f1c 100644
--- a/src/util/socks.c
+++ b/src/util/socks.c
@@ -29,10 +29,10 @@
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30 30
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "util-socks", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-socks", __VA_ARGS__)
33 33
34#define LOG_STRERROR(kind, syscall) \ 34#define LOG_STRERROR(kind, syscall) \
35 GNUNET_log_from_strerror(kind, "util-socks", syscall) 35 GNUNET_log_from_strerror (kind, "util-socks", syscall)
36 36
37 37
38/* SOCKS5 authentication methods */ 38/* SOCKS5 authentication methods */
@@ -58,43 +58,43 @@
58#define SOCKS5_REP_INVADDR 0x09 /* Inalid address */ 58#define SOCKS5_REP_INVADDR 0x09 /* Inalid address */
59 59
60const char * 60const char *
61SOCKS5_REP_names(int rep) 61SOCKS5_REP_names (int rep)
62{ 62{
63 switch (rep) 63 switch (rep)
64 { 64 {
65 case SOCKS5_REP_SUCCEEDED: 65 case SOCKS5_REP_SUCCEEDED:
66 return "succeeded"; 66 return "succeeded";
67 67
68 case SOCKS5_REP_FAIL: 68 case SOCKS5_REP_FAIL:
69 return "general SOCKS server failure"; 69 return "general SOCKS server failure";
70 70
71 case SOCKS5_REP_NALLOWED: 71 case SOCKS5_REP_NALLOWED:
72 return "connection not allowed by ruleset"; 72 return "connection not allowed by ruleset";
73 73
74 case SOCKS5_REP_NUNREACH: 74 case SOCKS5_REP_NUNREACH:
75 return "Network unreachable"; 75 return "Network unreachable";
76 76
77 case SOCKS5_REP_HUNREACH: 77 case SOCKS5_REP_HUNREACH:
78 return "Host unreachable"; 78 return "Host unreachable";
79 79
80 case SOCKS5_REP_REFUSED: 80 case SOCKS5_REP_REFUSED:
81 return "connection refused"; 81 return "connection refused";
82 82
83 case SOCKS5_REP_EXPIRED: 83 case SOCKS5_REP_EXPIRED:
84 return "TTL expired"; 84 return "TTL expired";
85 85
86 case SOCKS5_REP_CNOTSUP: 86 case SOCKS5_REP_CNOTSUP:
87 return "Command not supported"; 87 return "Command not supported";
88 88
89 case SOCKS5_REP_ANOTSUP: 89 case SOCKS5_REP_ANOTSUP:
90 return "Address not supported"; 90 return "Address not supported";
91 91
92 case SOCKS5_REP_INVADDR: 92 case SOCKS5_REP_INVADDR:
93 return "Invalid address"; 93 return "Invalid address";
94 94
95 default: 95 default:
96 return NULL; 96 return NULL;
97 } 97 }
98}; 98};
99 99
100 100
@@ -108,18 +108,18 @@ SOCKS5_REP_names(int rep)
108 * @return pointer to the end of the encoded string in the buffer 108 * @return pointer to the end of the encoded string in the buffer
109 */ 109 */
110unsigned char * 110unsigned char *
111SOCK5_proto_string(unsigned char *b, const char *s) 111SOCK5_proto_string (unsigned char *b, const char *s)
112{ 112{
113 size_t l = strlen(s); 113 size_t l = strlen (s);
114 114
115 if (l > 255) 115 if (l > 255)
116 { 116 {
117 LOG(GNUNET_ERROR_TYPE_WARNING, 117 LOG (GNUNET_ERROR_TYPE_WARNING,
118 "SOCKS5 cannot handle hostnames, usernames, or passwords over 255 bytes, truncating.\n"); 118 "SOCKS5 cannot handle hostnames, usernames, or passwords over 255 bytes, truncating.\n");
119 l = 255; 119 l = 255;
120 } 120 }
121 *(b++) = (unsigned char)l; 121 *(b++) = (unsigned char) l;
122 memcpy(b, s, l); 122 memcpy (b, s, l);
123 return b + l; 123 return b + l;
124} 124}
125 125
@@ -132,7 +132,8 @@ SOCK5_proto_string(unsigned char *b, const char *s)
132/** 132/**
133 * State of the SOCKS5 handshake. 133 * State of the SOCKS5 handshake.
134 */ 134 */
135struct GNUNET_SOCKS_Handshake { 135struct GNUNET_SOCKS_Handshake
136{
136 /** 137 /**
137 * Connection handle used for SOCKS5 138 * Connection handle used for SOCKS5
138 */ 139 */
@@ -179,7 +180,7 @@ struct GNUNET_SOCKS_Handshake {
179/* Regitering prototypes */ 180/* Regitering prototypes */
180 181
181void 182void
182register_reciever(struct GNUNET_SOCKS_Handshake *ih, int want); 183register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want);
183 184
184/* In fact, the client sends first rule in GNUnet suggests one could take 185/* In fact, the client sends first rule in GNUnet suggests one could take
185 * large mac read sizes without fear of screwing up the proxied protocol, 186 * large mac read sizes without fear of screwing up the proxied protocol,
@@ -188,7 +189,7 @@ register_reciever(struct GNUNET_SOCKS_Handshake *ih, int want);
188 189
189 190
190struct GNUNET_CONNECTION_TransmitHandle * 191struct GNUNET_CONNECTION_TransmitHandle *
191register_sender(struct GNUNET_SOCKS_Handshake *ih); 192register_sender (struct GNUNET_SOCKS_Handshake *ih);
192 193
193 194
194/** 195/**
@@ -199,9 +200,9 @@ register_sender(struct GNUNET_SOCKS_Handshake *ih);
199 * @return Connection handle that becomes usable when the handshake completes. 200 * @return Connection handle that becomes usable when the handshake completes.
200 */ 201 */
201void 202void
202SOCKS5_handshake_done(struct GNUNET_SOCKS_Handshake *ih) 203SOCKS5_handshake_done (struct GNUNET_SOCKS_Handshake *ih)
203{ 204{
204 GNUNET_CONNECTION_acivate_proxied(ih->target_connection); 205 GNUNET_CONNECTION_acivate_proxied (ih->target_connection);
205} 206}
206 207
207 208
@@ -211,109 +212,109 @@ SOCKS5_handshake_done(struct GNUNET_SOCKS_Handshake *ih)
211 * @param ih SOCKS5 Handshake 212 * @param ih SOCKS5 Handshake
212 */ 213 */
213void 214void
214SOCKS5_handshake_step(struct GNUNET_SOCKS_Handshake *ih) 215SOCKS5_handshake_step (struct GNUNET_SOCKS_Handshake *ih)
215{ 216{
216 unsigned char *b = ih->instart; 217 unsigned char *b = ih->instart;
217 size_t available = ih->inend - b; 218 size_t available = ih->inend - b;
218 219
219 int want = register_reciever_wants(ih); 220 int want = register_reciever_wants (ih);
220 221
221 if (available < want) 222 if (available < want)
223 {
224 register_reciever (ih, want - available);
225 return;
226 }
227 GNUNET_assert (SOCKS5_step_done > ih->step && ih->step >= 0);
228 switch (ih->step)
229 {
230 case SOCKS5_step_greet: /* SOCKS5 server's greeting */
231 if (b[0] != 5)
222 { 232 {
223 register_reciever(ih, want - available); 233 LOG (GNUNET_ERROR_TYPE_ERROR, "Not a SOCKS5 server\n");
224 return; 234 GNUNET_assert (0);
225 } 235 }
226 GNUNET_assert(SOCKS5_step_done > ih->step && ih->step >= 0); 236 switch (b[1])
227 switch (ih->step)
228 { 237 {
229 case SOCKS5_step_greet: /* SOCKS5 server's greeting */ 238 case SOCKS5_AUTH_NOAUTH:
230 if (b[0] != 5) 239 ih->step = SOCKS5_step_cmd; /* no authentication to do */
231 {
232 LOG(GNUNET_ERROR_TYPE_ERROR, "Not a SOCKS5 server\n");
233 GNUNET_assert(0);
234 }
235 switch (b[1])
236 {
237 case SOCKS5_AUTH_NOAUTH:
238 ih->step = SOCKS5_step_cmd; /* no authentication to do */
239 break;
240
241 case SOCKS5_AUTH_USERPASS:
242 ih->step = SOCKS5_step_auth;
243 break;
244
245 case SOCKS5_AUTH_REJECT:
246 LOG(GNUNET_ERROR_TYPE_ERROR, "No authentication method accepted\n");
247 return;
248
249 default:
250 LOG(GNUNET_ERROR_TYPE_ERROR,
251 "Not a SOCKS5 server / Nonsensical authentication\n");
252 return;
253 }
254 b += 2;
255 break; 240 break;
256 241
257 case SOCKS5_step_auth: /* SOCKS5 server's responce to authentication */ 242 case SOCKS5_AUTH_USERPASS:
258 if (b[1] != 0) 243 ih->step = SOCKS5_step_auth;
259 {
260 LOG(GNUNET_ERROR_TYPE_ERROR, "SOCKS5 authentication failed\n");
261 GNUNET_assert(0);
262 }
263 ih->step = SOCKS5_step_cmd;
264 b += 2;
265 break; 244 break;
266 245
267 case SOCKS5_step_cmd: /* SOCKS5 server's responce to command */ 246 case SOCKS5_AUTH_REJECT:
268 if (b[0] != 5) 247 LOG (GNUNET_ERROR_TYPE_ERROR, "No authentication method accepted\n");
269 { 248 return;
270 LOG(GNUNET_ERROR_TYPE_ERROR, "SOCKS5 protocol error\n"); 249
271 GNUNET_assert(0); 250 default:
272 } 251 LOG (GNUNET_ERROR_TYPE_ERROR,
273 if (0 != b[1]) 252 "Not a SOCKS5 server / Nonsensical authentication\n");
274 {
275 LOG(GNUNET_ERROR_TYPE_ERROR,
276 "SOCKS5 connection error : %s\n",
277 SOCKS5_REP_names(b[1]));
278 return;
279 }
280 b += 3;
281 /* There is no reason to verify host and port afaik. */
282 switch (*(b++))
283 {
284 case 1: /* IPv4 */
285 b += sizeof(struct in_addr); /* 4 */
286 break;
287
288 case 4: /* IPv6 */
289 b += sizeof(struct in6_addr); /* 16 */
290 break;
291
292 case 3: /* hostname */
293 b += *b;
294 break;
295 }
296 b += 2; /* port */
297 if (b > ih->inend)
298 {
299 register_reciever(ih, b - ih->inend);
300 return;
301 }
302 ih->step = SOCKS5_step_done;
303 LOG(GNUNET_ERROR_TYPE_DEBUG,
304 "SOCKS5 server : %s\n",
305 SOCKS5_REP_names(b[1]));
306 ih->instart = b;
307 SOCKS5_handshake_done(ih);
308 return; 253 return;
254 }
255 b += 2;
256 break;
309 257
310 case SOCKS5_step_done: 258 case SOCKS5_step_auth: /* SOCKS5 server's responce to authentication */
311 GNUNET_assert(0); 259 if (b[1] != 0)
260 {
261 LOG (GNUNET_ERROR_TYPE_ERROR, "SOCKS5 authentication failed\n");
262 GNUNET_assert (0);
312 } 263 }
264 ih->step = SOCKS5_step_cmd;
265 b += 2;
266 break;
267
268 case SOCKS5_step_cmd: /* SOCKS5 server's responce to command */
269 if (b[0] != 5)
270 {
271 LOG (GNUNET_ERROR_TYPE_ERROR, "SOCKS5 protocol error\n");
272 GNUNET_assert (0);
273 }
274 if (0 != b[1])
275 {
276 LOG (GNUNET_ERROR_TYPE_ERROR,
277 "SOCKS5 connection error : %s\n",
278 SOCKS5_REP_names (b[1]));
279 return;
280 }
281 b += 3;
282 /* There is no reason to verify host and port afaik. */
283 switch (*(b++))
284 {
285 case 1: /* IPv4 */
286 b += sizeof(struct in_addr); /* 4 */
287 break;
288
289 case 4: /* IPv6 */
290 b += sizeof(struct in6_addr); /* 16 */
291 break;
292
293 case 3: /* hostname */
294 b += *b;
295 break;
296 }
297 b += 2; /* port */
298 if (b > ih->inend)
299 {
300 register_reciever (ih, b - ih->inend);
301 return;
302 }
303 ih->step = SOCKS5_step_done;
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "SOCKS5 server : %s\n",
306 SOCKS5_REP_names (b[1]));
307 ih->instart = b;
308 SOCKS5_handshake_done (ih);
309 return;
310
311 case SOCKS5_step_done:
312 GNUNET_assert (0);
313 }
313 ih->instart = b; 314 ih->instart = b;
314 /* Do not reschedule the sender unless we're done reading. 315 /* Do not reschedule the sender unless we're done reading.
315 * I imagine this lets us avoid ever cancelling the transmit handle. */ 316 * I imagine this lets us avoid ever cancelling the transmit handle. */
316 register_sender(ih); 317 register_sender (ih);
317} 318}
318 319
319 320
@@ -325,19 +326,19 @@ SOCKS5_handshake_step(struct GNUNET_SOCKS_Handshake *ih)
325 * @param handler_cls closure for @a handler 326 * @param handler_cls closure for @a handler
326 */ 327 */
327void 328void
328reciever(void *cls, 329reciever (void *cls,
329 const void *buf, 330 const void *buf,
330 size_t available, 331 size_t available,
331 const struct sockaddr *addr, 332 const struct sockaddr *addr,
332 socklen_t addrlen, 333 socklen_t addrlen,
333 int errCode) 334 int errCode)
334{ 335{
335 struct GNUNET_SOCKS_Handshake *ih = cls; 336 struct GNUNET_SOCKS_Handshake *ih = cls;
336 337
337 GNUNET_assert(&ih->inend[available] < &ih->inbuf[1024]); 338 GNUNET_assert (&ih->inend[available] < &ih->inbuf[1024]);
338 GNUNET_memcpy(ih->inend, buf, available); 339 GNUNET_memcpy (ih->inend, buf, available);
339 ih->inend += available; 340 ih->inend += available;
340 SOCKS5_handshake_step(ih); 341 SOCKS5_handshake_step (ih);
341} 342}
342 343
343 344
@@ -349,13 +350,13 @@ reciever(void *cls,
349 * @param handler_cls closure for @a handler 350 * @param handler_cls closure for @a handler
350 */ 351 */
351void 352void
352register_reciever(struct GNUNET_SOCKS_Handshake *ih, int want) 353register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want)
353{ 354{
354 GNUNET_CONNECTION_receive(ih->socks5_connection, 355 GNUNET_CONNECTION_receive (ih->socks5_connection,
355 want, 356 want,
356 GNUNET_TIME_relative_get_minute_(), 357 GNUNET_TIME_relative_get_minute_ (),
357 &reciever, 358 &reciever,
358 ih); 359 ih);
359} 360}
360 361
361 362
@@ -368,7 +369,7 @@ register_reciever(struct GNUNET_SOCKS_Handshake *ih, int want)
368 * @return number of bytes written to @a buf 369 * @return number of bytes written to @a buf
369 */ 370 */
370size_t 371size_t
371transmit_ready(void *cls, size_t size, void *buf) 372transmit_ready (void *cls, size_t size, void *buf)
372{ 373{
373 struct GNUNET_SOCKS_Handshake *ih = cls; 374 struct GNUNET_SOCKS_Handshake *ih = cls;
374 375
@@ -388,32 +389,32 @@ transmit_ready(void *cls, size_t size, void *buf)
388 * maybe that should change for error handling pruposes. It appears that 389 * maybe that should change for error handling pruposes. It appears that
389 * successful operations, including DNS resolution, do not use this. */ 390 * successful operations, including DNS resolution, do not use this. */
390 if (NULL == buf) 391 if (NULL == buf)
392 {
393 if (0 == ih->step)
394 {
395 LOG (GNUNET_ERROR_TYPE_WARNING,
396 "Timeout contacting SOCKS server, retrying indefinitely, but probably hopeless.\n");
397 register_sender (ih);
398 }
399 else
391 { 400 {
392 if (0 == ih->step) 401 LOG (GNUNET_ERROR_TYPE_ERROR,
393 { 402 "Timeout during mid SOCKS handshake (step %u), probably not a SOCKS server.\n",
394 LOG(GNUNET_ERROR_TYPE_WARNING, 403 ih->step);
395 "Timeout contacting SOCKS server, retrying indefinitely, but probably hopeless.\n"); 404 GNUNET_break (0);
396 register_sender(ih);
397 }
398 else
399 {
400 LOG(GNUNET_ERROR_TYPE_ERROR,
401 "Timeout during mid SOCKS handshake (step %u), probably not a SOCKS server.\n",
402 ih->step);
403 GNUNET_break(0);
404 }
405 return 0;
406 } 405 }
406 return 0;
407 }
407 408
408 GNUNET_assert((1024 >= size) && (size > 0)); 409 GNUNET_assert ((1024 >= size) && (size > 0));
409 GNUNET_assert((SOCKS5_step_done > ih->step) && (ih->step >= 0)); 410 GNUNET_assert ((SOCKS5_step_done > ih->step) && (ih->step >= 0));
410 unsigned char *b = ih->outstep[ih->step]; 411 unsigned char *b = ih->outstep[ih->step];
411 unsigned char *e = ih->outstep[ih->step + 1]; 412 unsigned char *e = ih->outstep[ih->step + 1];
412 GNUNET_assert(e <= &ih->outbuf[1024]); 413 GNUNET_assert (e <= &ih->outbuf[1024]);
413 unsigned int l = e - b; 414 unsigned int l = e - b;
414 GNUNET_assert(size >= l); 415 GNUNET_assert (size >= l);
415 GNUNET_memcpy(buf, b, l); 416 GNUNET_memcpy (buf, b, l);
416 register_reciever(ih, register_reciever_wants(ih)); 417 register_reciever (ih, register_reciever_wants (ih));
417 return l; 418 return l;
418} 419}
419 420
@@ -426,22 +427,22 @@ transmit_ready(void *cls, size_t size, void *buf)
426 * NULL if we are already going to notify someone else (busy) 427 * NULL if we are already going to notify someone else (busy)
427 */ 428 */
428struct GNUNET_CONNECTION_TransmitHandle * 429struct GNUNET_CONNECTION_TransmitHandle *
429register_sender(struct GNUNET_SOCKS_Handshake *ih) 430register_sender (struct GNUNET_SOCKS_Handshake *ih)
430{ 431{
431 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_MINUTES; 432 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_MINUTES;
432 433
433 GNUNET_assert(SOCKS5_step_done > ih->step); 434 GNUNET_assert (SOCKS5_step_done > ih->step);
434 GNUNET_assert(ih->step >= 0); 435 GNUNET_assert (ih->step >= 0);
435 if (0 == ih->step) 436 if (0 == ih->step)
436 timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 3); 437 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 3);
437 unsigned char *b = ih->outstep[ih->step]; 438 unsigned char *b = ih->outstep[ih->step];
438 unsigned char *e = ih->outstep[ih->step + 1]; 439 unsigned char *e = ih->outstep[ih->step + 1];
439 GNUNET_assert(ih->outbuf <= b && b < e && e < &ih->outbuf[1024]); 440 GNUNET_assert (ih->outbuf <= b && b < e && e < &ih->outbuf[1024]);
440 ih->th = GNUNET_CONNECTION_notify_transmit_ready(ih->socks5_connection, 441 ih->th = GNUNET_CONNECTION_notify_transmit_ready (ih->socks5_connection,
441 e - b, 442 e - b,
442 timeout, 443 timeout,
443 &transmit_ready, 444 &transmit_ready,
444 ih); 445 ih);
445 return ih->th; 446 return ih->th;
446} 447}
447 448
@@ -456,10 +457,10 @@ register_sender(struct GNUNET_SOCKS_Handshake *ih)
456 * @return Valid SOCKS5 hanbdshake handle 457 * @return Valid SOCKS5 hanbdshake handle
457 */ 458 */
458struct GNUNET_SOCKS_Handshake * 459struct GNUNET_SOCKS_Handshake *
459GNUNET_SOCKS_init_handshake(const char *user, const char *pass) 460GNUNET_SOCKS_init_handshake (const char *user, const char *pass)
460{ 461{
461 struct GNUNET_SOCKS_Handshake *ih = 462 struct GNUNET_SOCKS_Handshake *ih =
462 GNUNET_new(struct GNUNET_SOCKS_Handshake); 463 GNUNET_new (struct GNUNET_SOCKS_Handshake);
463 unsigned char *b = ih->outbuf; 464 unsigned char *b = ih->outbuf;
464 465
465 ih->outstep[SOCKS5_step_greet] = b; 466 ih->outstep[SOCKS5_step_greet] = b;
@@ -471,10 +472,10 @@ GNUNET_SOCKS_init_handshake(const char *user, const char *pass)
471 * And some SOCKS5 servers might require this. */ 472 * And some SOCKS5 servers might require this. */
472 *(b++) = SOCKS5_AUTH_NOAUTH; 473 *(b++) = SOCKS5_AUTH_NOAUTH;
473 if (NULL != user) 474 if (NULL != user)
474 { 475 {
475 *(b++) = SOCKS5_AUTH_USERPASS; 476 *(b++) = SOCKS5_AUTH_USERPASS;
476 (*n)++; 477 (*n)++;
477 } 478 }
478 /* There is no apperent reason to support authentication methods beyond 479 /* There is no apperent reason to support authentication methods beyond
479 * username and password since afaik Tor does not support them. */ 480 * username and password since afaik Tor does not support them. */
480 481
@@ -487,8 +488,8 @@ GNUNET_SOCKS_init_handshake(const char *user, const char *pass)
487 488
488 ih->outstep[SOCKS5_step_auth] = b; 489 ih->outstep[SOCKS5_step_auth] = b;
489 *(b++) = 1; /* subnegotiation ver.: 1 */ 490 *(b++) = 1; /* subnegotiation ver.: 1 */
490 b = SOCK5_proto_string(b, user); 491 b = SOCK5_proto_string (b, user);
491 b = SOCK5_proto_string(b, pass); 492 b = SOCK5_proto_string (b, pass);
492 493
493 ih->outstep[SOCKS5_step_cmd] = b; 494 ih->outstep[SOCKS5_step_cmd] = b;
494 495
@@ -505,9 +506,9 @@ GNUNET_SOCKS_init_handshake(const char *user, const char *pass)
505 * @return Valid SOCKS5 hanbdshake handle 506 * @return Valid SOCKS5 hanbdshake handle
506 */ 507 */
507struct GNUNET_SOCKS_Handshake * 508struct GNUNET_SOCKS_Handshake *
508GNUNET_SOCKS_init_handshake_noauth() 509GNUNET_SOCKS_init_handshake_noauth ()
509{ 510{
510 return GNUNET_SOCKS_init_handshake(NULL, NULL); 511 return GNUNET_SOCKS_init_handshake (NULL, NULL);
511} 512}
512 513
513 514
@@ -520,11 +521,12 @@ GNUNET_SOCKS_init_handshake_noauth()
520 * @param port 521 * @param port
521 */ 522 */
522void 523void
523GNUNET_SOCKS_set_handshake_destination(struct GNUNET_SOCKS_Handshake *ih, 524GNUNET_SOCKS_set_handshake_destination (struct GNUNET_SOCKS_Handshake *ih,
524 const char *host, 525 const char *host,
525 uint16_t port) 526 uint16_t port)
526{ 527{
527 union { 528 union
529 {
528 struct in_addr in4; 530 struct in_addr in4;
529 struct in6_addr in6; 531 struct in6_addr in6;
530 } ia; 532 } ia;
@@ -535,26 +537,26 @@ GNUNET_SOCKS_set_handshake_destination(struct GNUNET_SOCKS_Handshake *ih,
535 *(b++) = 0; /* reserved */ 537 *(b++) = 0; /* reserved */
536 538
537 /* Specify destination */ 539 /* Specify destination */
538 if (1 == inet_pton(AF_INET, host, &ia.in4)) 540 if (1 == inet_pton (AF_INET, host, &ia.in4))
539 { 541 {
540 *(b++) = 1; /* IPv4 */ 542 *(b++) = 1; /* IPv4 */
541 GNUNET_memcpy(b, &ia.in4, sizeof(struct in_addr)); 543 GNUNET_memcpy (b, &ia.in4, sizeof(struct in_addr));
542 b += sizeof(struct in_addr); /* 4 */ 544 b += sizeof(struct in_addr); /* 4 */
543 } 545 }
544 else if (1 == inet_pton(AF_INET6, host, &ia.in6)) 546 else if (1 == inet_pton (AF_INET6, host, &ia.in6))
545 { 547 {
546 *(b++) = 4; /* IPv6 */ 548 *(b++) = 4; /* IPv6 */
547 GNUNET_memcpy(b, &ia.in6, sizeof(struct in6_addr)); 549 GNUNET_memcpy (b, &ia.in6, sizeof(struct in6_addr));
548 b += sizeof(struct in6_addr); /* 16 */ 550 b += sizeof(struct in6_addr); /* 16 */
549 } 551 }
550 else 552 else
551 { 553 {
552 *(b++) = 3; /* hostname */ 554 *(b++) = 3; /* hostname */
553 b = SOCK5_proto_string(b, host); 555 b = SOCK5_proto_string (b, host);
554 } 556 }
555 557
556 /* Specify port */ 558 /* Specify port */
557 *(uint16_t *)b = htons(port); 559 *(uint16_t *) b = htons (port);
558 b += 2; 560 b += 2;
559 561
560 ih->outstep[SOCKS5_step_done] = b; 562 ih->outstep[SOCKS5_step_done] = b;
@@ -569,12 +571,12 @@ GNUNET_SOCKS_set_handshake_destination(struct GNUNET_SOCKS_Handshake *ih,
569 * @return Connection handle that becomes usable when the SOCKS5 handshake completes. 571 * @return Connection handle that becomes usable when the SOCKS5 handshake completes.
570 */ 572 */
571struct GNUNET_CONNECTION_Handle * 573struct GNUNET_CONNECTION_Handle *
572GNUNET_SOCKS_run_handshake(struct GNUNET_SOCKS_Handshake *ih, 574GNUNET_SOCKS_run_handshake (struct GNUNET_SOCKS_Handshake *ih,
573 struct GNUNET_CONNECTION_Handle *c) 575 struct GNUNET_CONNECTION_Handle *c)
574{ 576{
575 ih->socks5_connection = c; 577 ih->socks5_connection = c;
576 ih->target_connection = GNUNET_CONNECTION_create_proxied_from_handshake(c); 578 ih->target_connection = GNUNET_CONNECTION_create_proxied_from_handshake (c);
577 register_sender(ih); 579 register_sender (ih);
578 580
579 return ih->target_connection; 581 return ih->target_connection;
580} 582}
@@ -589,11 +591,11 @@ GNUNET_SOCKS_run_handshake(struct GNUNET_SOCKS_Handshake *ih,
589 * @return GNUNET_YES if so, GNUNET_NO if not 591 * @return GNUNET_YES if so, GNUNET_NO if not
590 */ 592 */
591int 593int
592GNUNET_SOCKS_check_service(const char *service_name, 594GNUNET_SOCKS_check_service (const char *service_name,
593 const struct GNUNET_CONFIGURATION_Handle *cfg) 595 const struct GNUNET_CONFIGURATION_Handle *cfg)
594{ 596{
595 return GNUNET_CONFIGURATION_have_value(cfg, service_name, "SOCKSPORT") || 597 return GNUNET_CONFIGURATION_have_value (cfg, service_name, "SOCKSPORT") ||
596 GNUNET_CONFIGURATION_have_value(cfg, service_name, "SOCKSHOST"); 598 GNUNET_CONFIGURATION_have_value (cfg, service_name, "SOCKSHOST");
597} 599}
598 600
599 601
@@ -606,8 +608,8 @@ GNUNET_SOCKS_check_service(const char *service_name,
606 * NULL if SOCKS not configured or not configured properly 608 * NULL if SOCKS not configured or not configured properly
607 */ 609 */
608struct GNUNET_CONNECTION_Handle * 610struct GNUNET_CONNECTION_Handle *
609GNUNET_SOCKS_do_connect(const char *service_name, 611GNUNET_SOCKS_do_connect (const char *service_name,
610 const struct GNUNET_CONFIGURATION_Handle *cfg) 612 const struct GNUNET_CONFIGURATION_Handle *cfg)
611{ 613{
612 struct GNUNET_SOCKS_Handshake *ih; 614 struct GNUNET_SOCKS_Handshake *ih;
613 struct GNUNET_CONNECTION_Handle *socks5; /* *proxied */ 615 struct GNUNET_CONNECTION_Handle *socks5; /* *proxied */
@@ -618,70 +620,70 @@ GNUNET_SOCKS_do_connect(const char *service_name,
618 unsigned long long port0; 620 unsigned long long port0;
619 unsigned long long port1; 621 unsigned long long port1;
620 622
621 if (GNUNET_YES != GNUNET_SOCKS_check_service(service_name, cfg)) 623 if (GNUNET_YES != GNUNET_SOCKS_check_service (service_name, cfg))
622 return NULL; 624 return NULL;
623 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, 625 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
624 service_name, 626 service_name,
625 "SOCKSPORT", 627 "SOCKSPORT",
626 &port0)) 628 &port0))
627 port0 = 9050; 629 port0 = 9050;
628 /* A typical Tor client should usually try port 9150 for the TBB too, but 630 /* A typical Tor client should usually try port 9150 for the TBB too, but
629 * GNUnet can probably assume a system Tor installation. */ 631 * GNUnet can probably assume a system Tor installation. */
630 if (port0 > 65535 || port0 <= 0) 632 if ((port0 > 65535)||(port0 <= 0))
631 { 633 {
632 LOG(GNUNET_ERROR_TYPE_WARNING, 634 LOG (GNUNET_ERROR_TYPE_WARNING,
633 _( 635 _ (
634 "Attempting to use invalid port %d as SOCKS proxy for service `%s'.\n"), 636 "Attempting to use invalid port %d as SOCKS proxy for service `%s'.\n"),
635 port0, 637 port0,
636 service_name); 638 service_name);
637 return NULL; 639 return NULL;
638 } 640 }
639 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, 641 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
640 service_name, 642 service_name,
641 "PORT", 643 "PORT",
642 &port1)) || 644 &port1)) ||
643 (port1 > 65535) || (port1 <= 0) || 645 (port1 > 65535) || (port1 <= 0) ||
644 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, 646 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
645 service_name, 647 service_name,
646 "HOSTNAME", 648 "HOSTNAME",
647 &host1))) 649 &host1)))
648 { 650 {
649 LOG(GNUNET_ERROR_TYPE_WARNING, 651 LOG (GNUNET_ERROR_TYPE_WARNING,
650 _( 652 _ (
651 "Attempting to proxy service `%s' to invalid port %d or hostname.\n"), 653 "Attempting to proxy service `%s' to invalid port %d or hostname.\n"),
652 service_name, 654 service_name,
653 port1); 655 port1);
654 return NULL; 656 return NULL;
655 } 657 }
656 /* Appeared to still work after host0 corrupted, so either test case is broken, or 658 /* Appeared to still work after host0 corrupted, so either test case is broken, or
657 this whole routine is not being called. */ 659 this whole routine is not being called. */
658 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, 660 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
659 service_name, 661 service_name,
660 "SOCKSHOST", 662 "SOCKSHOST",
661 &host0)) 663 &host0))
662 host0 = NULL; 664 host0 = NULL;
663 socks5 = GNUNET_CONNECTION_create_from_connect(cfg, 665 socks5 = GNUNET_CONNECTION_create_from_connect (cfg,
664 (host0 != NULL) ? host0 666 (host0 != NULL) ? host0
665 : "127.0.0.1", 667 : "127.0.0.1",
666 port0); 668 port0);
667 GNUNET_free_non_null(host0); 669 GNUNET_free_non_null (host0);
668 670
669 /* Sets to NULL if they do not exist */ 671 /* Sets to NULL if they do not exist */
670 (void)GNUNET_CONFIGURATION_get_value_string(cfg, 672 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
671 service_name, 673 service_name,
672 "SOCKSUSER", 674 "SOCKSUSER",
673 &user); 675 &user);
674 (void)GNUNET_CONFIGURATION_get_value_string(cfg, 676 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
675 service_name, 677 service_name,
676 "SOCKSPASS", 678 "SOCKSPASS",
677 &pass); 679 &pass);
678 ih = GNUNET_SOCKS_init_handshake(user, pass); 680 ih = GNUNET_SOCKS_init_handshake (user, pass);
679 GNUNET_free_non_null(user); 681 GNUNET_free_non_null (user);
680 GNUNET_free_non_null(pass); 682 GNUNET_free_non_null (pass);
681 683
682 GNUNET_SOCKS_set_handshake_destination(ih, host1, port1); 684 GNUNET_SOCKS_set_handshake_destination (ih, host1, port1);
683 GNUNET_free(host1); 685 GNUNET_free (host1);
684 return GNUNET_SOCKS_run_handshake(ih, socks5); 686 return GNUNET_SOCKS_run_handshake (ih, socks5);
685} 687}
686 688
687/* socks.c */ 689/* socks.c */