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