diff options
Diffstat (limited to 'src/nat/nat_mini.c')
-rw-r--r-- | src/nat/nat_mini.c | 712 |
1 files changed, 0 insertions, 712 deletions
diff --git a/src/nat/nat_mini.c b/src/nat/nat_mini.c deleted file mode 100644 index 915bcbdb6..000000000 --- a/src/nat/nat_mini.c +++ /dev/null | |||
@@ -1,712 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011-2014 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file nat/nat_mini.c | ||
23 | * @brief functions for interaction with miniupnp; tested with miniupnpc 1.5 | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_nat_lib.h" | ||
29 | #include "nat.h" | ||
30 | |||
31 | #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__) | ||
32 | |||
33 | /** | ||
34 | * How long do we give upnpc to create a mapping? | ||
35 | */ | ||
36 | #define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) | ||
37 | |||
38 | /** | ||
39 | * How long do we give upnpc to remove a mapping? | ||
40 | */ | ||
41 | #define UNMAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) | ||
42 | |||
43 | /** | ||
44 | * How often do we check for changes in the mapping? | ||
45 | */ | ||
46 | #define MAP_REFRESH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) | ||
47 | |||
48 | |||
49 | |||
50 | /** | ||
51 | * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation. | ||
52 | */ | ||
53 | struct GNUNET_NAT_ExternalHandle | ||
54 | { | ||
55 | |||
56 | /** | ||
57 | * Function to call with the result. | ||
58 | */ | ||
59 | GNUNET_NAT_IPCallback cb; | ||
60 | |||
61 | /** | ||
62 | * Closure for @e cb. | ||
63 | */ | ||
64 | void *cb_cls; | ||
65 | |||
66 | /** | ||
67 | * Read task. | ||
68 | */ | ||
69 | struct GNUNET_SCHEDULER_Task * task; | ||
70 | |||
71 | /** | ||
72 | * Handle to 'external-ip' process. | ||
73 | */ | ||
74 | struct GNUNET_OS_Process *eip; | ||
75 | |||
76 | /** | ||
77 | * Handle to stdout pipe of 'external-ip'. | ||
78 | */ | ||
79 | struct GNUNET_DISK_PipeHandle *opipe; | ||
80 | |||
81 | /** | ||
82 | * Read handle of @e opipe. | ||
83 | */ | ||
84 | const struct GNUNET_DISK_FileHandle *r; | ||
85 | |||
86 | /** | ||
87 | * When should this operation time out? | ||
88 | */ | ||
89 | struct GNUNET_TIME_Absolute timeout; | ||
90 | |||
91 | /** | ||
92 | * Number of bytes in 'buf' that are valid. | ||
93 | */ | ||
94 | size_t off; | ||
95 | |||
96 | /** | ||
97 | * Destination of our read operation (output of 'external-ip'). | ||
98 | */ | ||
99 | char buf[17]; | ||
100 | |||
101 | /** | ||
102 | * Error code for better debugging and user feedback | ||
103 | */ | ||
104 | enum GNUNET_NAT_StatusCode ret; | ||
105 | }; | ||
106 | |||
107 | |||
108 | /** | ||
109 | * Read the output of 'external-ip' into buf. When complete, parse the | ||
110 | * address and call our callback. | ||
111 | * | ||
112 | * @param cls the `struct GNUNET_NAT_ExternalHandle` | ||
113 | */ | ||
114 | static void | ||
115 | read_external_ipv4 (void *cls) | ||
116 | { | ||
117 | struct GNUNET_NAT_ExternalHandle *eh = cls; | ||
118 | ssize_t ret; | ||
119 | struct in_addr addr; | ||
120 | const struct GNUNET_SCHEDULER_TaskContext *tc; | ||
121 | |||
122 | eh->task = NULL; | ||
123 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
124 | if (GNUNET_YES == | ||
125 | GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r)) | ||
126 | { | ||
127 | ret = | ||
128 | GNUNET_DISK_file_read (eh->r, &eh->buf[eh->off], | ||
129 | sizeof (eh->buf) - eh->off); | ||
130 | } | ||
131 | else | ||
132 | { | ||
133 | eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; | ||
134 | ret = -1; /* error reading, timeout, etc. */ | ||
135 | } | ||
136 | if (ret > 0) | ||
137 | { | ||
138 | /* try to read more */ | ||
139 | eh->off += ret; | ||
140 | eh->task = | ||
141 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining | ||
142 | (eh->timeout), eh->r, | ||
143 | &read_external_ipv4, eh); | ||
144 | return; | ||
145 | } | ||
146 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID; | ||
147 | if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n')) | ||
148 | { | ||
149 | eh->buf[eh->off - 1] = '\0'; | ||
150 | if (1 == inet_pton (AF_INET, eh->buf, &addr)) | ||
151 | { | ||
152 | if (0 != addr.s_addr) | ||
153 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */ | ||
154 | else | ||
155 | eh->ret = GNUNET_NAT_ERROR_SUCCESS; | ||
156 | } | ||
157 | } | ||
158 | eh->cb (eh->cb_cls, | ||
159 | (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL, | ||
160 | eh->ret); | ||
161 | GNUNET_NAT_mini_get_external_ipv4_cancel (eh); | ||
162 | } | ||
163 | |||
164 | |||
165 | /** | ||
166 | * (Asynchronously) signal error invoking "external-ip" to client. | ||
167 | * | ||
168 | * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed) | ||
169 | */ | ||
170 | static void | ||
171 | signal_external_ip_error (void *cls) | ||
172 | { | ||
173 | struct GNUNET_NAT_ExternalHandle *eh = cls; | ||
174 | |||
175 | eh->task = NULL; | ||
176 | eh->cb (eh->cb_cls, | ||
177 | NULL, | ||
178 | eh->ret); | ||
179 | GNUNET_free (eh); | ||
180 | } | ||
181 | |||
182 | |||
183 | /** | ||
184 | * Try to get the external IPv4 address of this peer. | ||
185 | * | ||
186 | * @param timeout when to fail | ||
187 | * @param cb function to call with result | ||
188 | * @param cb_cls closure for @a cb | ||
189 | * @return handle for cancellation (can only be used until @a cb is called), never NULL | ||
190 | */ | ||
191 | struct GNUNET_NAT_ExternalHandle * | ||
192 | GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout, | ||
193 | GNUNET_NAT_IPCallback cb, void *cb_cls) | ||
194 | { | ||
195 | struct GNUNET_NAT_ExternalHandle *eh; | ||
196 | |||
197 | eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle); | ||
198 | eh->cb = cb; | ||
199 | eh->cb_cls = cb_cls; | ||
200 | eh->ret = GNUNET_NAT_ERROR_SUCCESS; | ||
201 | if (GNUNET_SYSERR == | ||
202 | GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL)) | ||
203 | { | ||
204 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
205 | _("`external-ip' command not found\n")); | ||
206 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; | ||
207 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, | ||
208 | eh); | ||
209 | return eh; | ||
210 | } | ||
211 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
212 | "Running `external-ip' to determine our external IP\n"); | ||
213 | eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); | ||
214 | if (NULL == eh->opipe) | ||
215 | { | ||
216 | eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; | ||
217 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, | ||
218 | eh); | ||
219 | return eh; | ||
220 | } | ||
221 | eh->eip = | ||
222 | GNUNET_OS_start_process (GNUNET_NO, 0, NULL, eh->opipe, NULL, | ||
223 | "external-ip", "external-ip", | ||
224 | NULL); | ||
225 | if (NULL == eh->eip) | ||
226 | { | ||
227 | GNUNET_DISK_pipe_close (eh->opipe); | ||
228 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; | ||
229 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, | ||
230 | eh); | ||
231 | return eh; | ||
232 | } | ||
233 | GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); | ||
234 | eh->timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
235 | eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); | ||
236 | eh->task = | ||
237 | GNUNET_SCHEDULER_add_read_file (timeout, | ||
238 | eh->r, | ||
239 | &read_external_ipv4, eh); | ||
240 | return eh; | ||
241 | } | ||
242 | |||
243 | |||
244 | /** | ||
245 | * Cancel operation. | ||
246 | * | ||
247 | * @param eh operation to cancel | ||
248 | */ | ||
249 | void | ||
250 | GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh) | ||
251 | { | ||
252 | if (NULL != eh->eip) | ||
253 | { | ||
254 | (void) GNUNET_OS_process_kill (eh->eip, SIGKILL); | ||
255 | GNUNET_OS_process_destroy (eh->eip); | ||
256 | } | ||
257 | if (NULL != eh->opipe) | ||
258 | GNUNET_DISK_pipe_close (eh->opipe); | ||
259 | if (NULL != eh->task) | ||
260 | GNUNET_SCHEDULER_cancel (eh->task); | ||
261 | GNUNET_free (eh); | ||
262 | } | ||
263 | |||
264 | |||
265 | /** | ||
266 | * Handle to a mapping created with upnpc. | ||
267 | */ | ||
268 | struct GNUNET_NAT_MiniHandle | ||
269 | { | ||
270 | |||
271 | /** | ||
272 | * Function to call on mapping changes. | ||
273 | */ | ||
274 | GNUNET_NAT_MiniAddressCallback ac; | ||
275 | |||
276 | /** | ||
277 | * Closure for @e ac. | ||
278 | */ | ||
279 | void *ac_cls; | ||
280 | |||
281 | /** | ||
282 | * Command used to install the map. | ||
283 | */ | ||
284 | struct GNUNET_OS_CommandHandle *map_cmd; | ||
285 | |||
286 | /** | ||
287 | * Command used to refresh our map information. | ||
288 | */ | ||
289 | struct GNUNET_OS_CommandHandle *refresh_cmd; | ||
290 | |||
291 | /** | ||
292 | * Command used to remove the mapping. | ||
293 | */ | ||
294 | struct GNUNET_OS_CommandHandle *unmap_cmd; | ||
295 | |||
296 | /** | ||
297 | * Our current external mapping (if we have one). | ||
298 | */ | ||
299 | struct sockaddr_in current_addr; | ||
300 | |||
301 | /** | ||
302 | * We check the mapping periodically to see if it | ||
303 | * still works. This task triggers the check. | ||
304 | */ | ||
305 | struct GNUNET_SCHEDULER_Task * refresh_task; | ||
306 | |||
307 | /** | ||
308 | * Are we mapping TCP or UDP? | ||
309 | */ | ||
310 | int is_tcp; | ||
311 | |||
312 | /** | ||
313 | * Did we succeed with creating a mapping? | ||
314 | */ | ||
315 | int did_map; | ||
316 | |||
317 | /** | ||
318 | * Did we find our mapping during refresh scan? | ||
319 | */ | ||
320 | int found; | ||
321 | |||
322 | /** | ||
323 | * Which port are we mapping? | ||
324 | */ | ||
325 | uint16_t port; | ||
326 | |||
327 | }; | ||
328 | |||
329 | |||
330 | /** | ||
331 | * Run "upnpc -l" to find out if our mapping changed. | ||
332 | * | ||
333 | * @param cls the `struct GNUNET_NAT_MiniHandle` | ||
334 | */ | ||
335 | static void | ||
336 | do_refresh (void *cls); | ||
337 | |||
338 | |||
339 | /** | ||
340 | * Process the output from the "upnpc -r" command. | ||
341 | * | ||
342 | * @param cls the `struct GNUNET_NAT_MiniHandle` | ||
343 | * @param line line of output, NULL at the end | ||
344 | */ | ||
345 | static void | ||
346 | process_map_output (void *cls, const char *line); | ||
347 | |||
348 | |||
349 | /** | ||
350 | * Run "upnpc -r" to map our internal port. | ||
351 | * | ||
352 | * @param mini our handle | ||
353 | */ | ||
354 | static void | ||
355 | run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini) | ||
356 | { | ||
357 | char pstr[6]; | ||
358 | |||
359 | GNUNET_snprintf (pstr, | ||
360 | sizeof (pstr), | ||
361 | "%u", | ||
362 | (unsigned int) mini->port); | ||
363 | mini->map_cmd = | ||
364 | GNUNET_OS_command_run (&process_map_output, mini, MAP_TIMEOUT, | ||
365 | "upnpc", "upnpc", "-r", pstr, | ||
366 | mini->is_tcp ? "tcp" : "udp", NULL); | ||
367 | if (NULL == mini->map_cmd) | ||
368 | { | ||
369 | mini->ac (mini->ac_cls, | ||
370 | GNUNET_SYSERR, | ||
371 | NULL, 0, | ||
372 | GNUNET_NAT_ERROR_UPNPC_FAILED); | ||
373 | return; | ||
374 | } | ||
375 | } | ||
376 | |||
377 | |||
378 | /** | ||
379 | * Process the output from "upnpc -l" to see if our | ||
380 | * external mapping changed. If so, do the notifications. | ||
381 | * | ||
382 | * @param cls the `struct GNUNET_NAT_MiniHandle` | ||
383 | * @param line line of output, NULL at the end | ||
384 | */ | ||
385 | static void | ||
386 | process_refresh_output (void *cls, const char *line) | ||
387 | { | ||
388 | struct GNUNET_NAT_MiniHandle *mini = cls; | ||
389 | char pstr[9]; | ||
390 | const char *s; | ||
391 | unsigned int nport; | ||
392 | struct in_addr exip; | ||
393 | |||
394 | if (NULL == line) | ||
395 | { | ||
396 | GNUNET_OS_command_stop (mini->refresh_cmd); | ||
397 | mini->refresh_cmd = NULL; | ||
398 | if (GNUNET_NO == mini->found) | ||
399 | { | ||
400 | /* mapping disappeared, try to re-create */ | ||
401 | if (GNUNET_YES == mini->did_map) | ||
402 | { | ||
403 | mini->ac (mini->ac_cls, | ||
404 | GNUNET_NO, | ||
405 | (const struct sockaddr *) &mini->current_addr, | ||
406 | sizeof (mini->current_addr), | ||
407 | GNUNET_NAT_ERROR_SUCCESS); | ||
408 | mini->did_map = GNUNET_NO; | ||
409 | } | ||
410 | run_upnpc_r (mini); | ||
411 | } | ||
412 | return; | ||
413 | } | ||
414 | if (!mini->did_map) | ||
415 | return; /* never mapped, won't find our mapping anyway */ | ||
416 | |||
417 | /* we're looking for output of the form: | ||
418 | * "ExternalIPAddress = 12.134.41.124" */ | ||
419 | |||
420 | s = strstr (line, "ExternalIPAddress = "); | ||
421 | if (NULL != s) | ||
422 | { | ||
423 | s += strlen ("ExternalIPAddress = "); | ||
424 | if (1 != inet_pton (AF_INET, s, &exip)) | ||
425 | return; /* skip */ | ||
426 | if (exip.s_addr == mini->current_addr.sin_addr.s_addr) | ||
427 | return; /* no change */ | ||
428 | /* update mapping */ | ||
429 | mini->ac (mini->ac_cls, GNUNET_NO, | ||
430 | (const struct sockaddr *) &mini->current_addr, | ||
431 | sizeof (mini->current_addr), | ||
432 | GNUNET_NAT_ERROR_SUCCESS); | ||
433 | mini->current_addr.sin_addr = exip; | ||
434 | mini->ac (mini->ac_cls, GNUNET_YES, | ||
435 | (const struct sockaddr *) &mini->current_addr, | ||
436 | sizeof (mini->current_addr), | ||
437 | GNUNET_NAT_ERROR_SUCCESS); | ||
438 | return; | ||
439 | } | ||
440 | /* | ||
441 | * we're looking for output of the form: | ||
442 | * | ||
443 | * "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''" | ||
444 | * "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''" | ||
445 | * | ||
446 | * the pattern we look for is: | ||
447 | * | ||
448 | * "%s TCP PORT->STRING:OURPORT *" or | ||
449 | * "%s UDP PORT->STRING:OURPORT *" | ||
450 | */ | ||
451 | GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port); | ||
452 | if (NULL == (s = strstr (line, "->"))) | ||
453 | return; /* skip */ | ||
454 | if (NULL == strstr (s, pstr)) | ||
455 | return; /* skip */ | ||
456 | if (1 != | ||
457 | SSCANF (line, | ||
458 | (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" : | ||
459 | "%*u UDP %u->%*s:%*u %*s", &nport)) | ||
460 | return; /* skip */ | ||
461 | mini->found = GNUNET_YES; | ||
462 | if (nport == ntohs (mini->current_addr.sin_port)) | ||
463 | return; /* no change */ | ||
464 | |||
465 | /* external port changed, update mapping */ | ||
466 | mini->ac (mini->ac_cls, GNUNET_NO, | ||
467 | (const struct sockaddr *) &mini->current_addr, | ||
468 | sizeof (mini->current_addr), | ||
469 | GNUNET_NAT_ERROR_SUCCESS); | ||
470 | mini->current_addr.sin_port = htons ((uint16_t) nport); | ||
471 | mini->ac (mini->ac_cls, GNUNET_YES, | ||
472 | (const struct sockaddr *) &mini->current_addr, | ||
473 | sizeof (mini->current_addr), | ||
474 | GNUNET_NAT_ERROR_SUCCESS); | ||
475 | } | ||
476 | |||
477 | |||
478 | /** | ||
479 | * Run "upnpc -l" to find out if our mapping changed. | ||
480 | * | ||
481 | * @param cls the 'struct GNUNET_NAT_MiniHandle' | ||
482 | */ | ||
483 | static void | ||
484 | do_refresh (void *cls) | ||
485 | { | ||
486 | struct GNUNET_NAT_MiniHandle *mini = cls; | ||
487 | int ac; | ||
488 | |||
489 | mini->refresh_task = | ||
490 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | ||
491 | &do_refresh, mini); | ||
492 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
493 | "Running `upnpc' to check if our mapping still exists\n"); | ||
494 | mini->found = GNUNET_NO; | ||
495 | ac = GNUNET_NO; | ||
496 | if (NULL != mini->map_cmd) | ||
497 | { | ||
498 | /* took way too long, abort it! */ | ||
499 | GNUNET_OS_command_stop (mini->map_cmd); | ||
500 | mini->map_cmd = NULL; | ||
501 | ac = GNUNET_YES; | ||
502 | } | ||
503 | if (NULL != mini->refresh_cmd) | ||
504 | { | ||
505 | /* took way too long, abort it! */ | ||
506 | GNUNET_OS_command_stop (mini->refresh_cmd); | ||
507 | mini->refresh_cmd = NULL; | ||
508 | ac = GNUNET_YES; | ||
509 | } | ||
510 | mini->refresh_cmd = | ||
511 | GNUNET_OS_command_run (&process_refresh_output, mini, MAP_TIMEOUT, | ||
512 | "upnpc", "upnpc", "-l", NULL); | ||
513 | if (GNUNET_YES == ac) | ||
514 | mini->ac (mini->ac_cls, | ||
515 | GNUNET_SYSERR, | ||
516 | NULL, 0, | ||
517 | GNUNET_NAT_ERROR_UPNPC_TIMEOUT); | ||
518 | } | ||
519 | |||
520 | |||
521 | /** | ||
522 | * Process the output from the 'upnpc -r' command. | ||
523 | * | ||
524 | * @param cls the `struct GNUNET_NAT_MiniHandle` | ||
525 | * @param line line of output, NULL at the end | ||
526 | */ | ||
527 | static void | ||
528 | process_map_output (void *cls, | ||
529 | const char *line) | ||
530 | { | ||
531 | struct GNUNET_NAT_MiniHandle *mini = cls; | ||
532 | const char *ipaddr; | ||
533 | char *ipa; | ||
534 | const char *pstr; | ||
535 | unsigned int port; | ||
536 | |||
537 | if (NULL == line) | ||
538 | { | ||
539 | GNUNET_OS_command_stop (mini->map_cmd); | ||
540 | mini->map_cmd = NULL; | ||
541 | if (GNUNET_YES != mini->did_map) | ||
542 | mini->ac (mini->ac_cls, | ||
543 | GNUNET_SYSERR, | ||
544 | NULL, 0, | ||
545 | GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED); | ||
546 | if (NULL == mini->refresh_task) | ||
547 | mini->refresh_task = | ||
548 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | ||
549 | &do_refresh, | ||
550 | mini); | ||
551 | return; | ||
552 | } | ||
553 | /* | ||
554 | * The upnpc output we're after looks like this: | ||
555 | * | ||
556 | * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000" | ||
557 | */ | ||
558 | if ((NULL == (ipaddr = strstr (line, " "))) || | ||
559 | (NULL == (pstr = strstr (ipaddr, ":"))) || | ||
560 | (1 != SSCANF (pstr + 1, "%u", &port))) | ||
561 | { | ||
562 | return; /* skip line */ | ||
563 | } | ||
564 | ipa = GNUNET_strdup (ipaddr + 1); | ||
565 | strstr (ipa, ":")[0] = '\0'; | ||
566 | if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr)) | ||
567 | { | ||
568 | GNUNET_free (ipa); | ||
569 | return; /* skip line */ | ||
570 | } | ||
571 | GNUNET_free (ipa); | ||
572 | |||
573 | mini->current_addr.sin_port = htons (port); | ||
574 | mini->current_addr.sin_family = AF_INET; | ||
575 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
576 | mini->current_addr.sin_len = sizeof (struct sockaddr_in); | ||
577 | #endif | ||
578 | mini->did_map = GNUNET_YES; | ||
579 | mini->ac (mini->ac_cls, GNUNET_YES, | ||
580 | (const struct sockaddr *) &mini->current_addr, | ||
581 | sizeof (mini->current_addr), | ||
582 | GNUNET_NAT_ERROR_SUCCESS); | ||
583 | } | ||
584 | |||
585 | |||
586 | /** | ||
587 | * Start mapping the given port using (mini)upnpc. This function | ||
588 | * should typically not be used directly (it is used within the | ||
589 | * general-purpose #GNUNET_NAT_register() code). However, it can be | ||
590 | * used if specifically UPnP-based NAT traversal is to be used or | ||
591 | * tested. | ||
592 | * | ||
593 | * @param port port to map | ||
594 | * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP | ||
595 | * @param ac function to call with mapping result | ||
596 | * @param ac_cls closure for @a ac | ||
597 | * @return NULL on error (no 'upnpc' installed) | ||
598 | */ | ||
599 | struct GNUNET_NAT_MiniHandle * | ||
600 | GNUNET_NAT_mini_map_start (uint16_t port, | ||
601 | int is_tcp, | ||
602 | GNUNET_NAT_MiniAddressCallback ac, | ||
603 | void *ac_cls) | ||
604 | { | ||
605 | struct GNUNET_NAT_MiniHandle *ret; | ||
606 | |||
607 | if (GNUNET_SYSERR == | ||
608 | GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)) | ||
609 | { | ||
610 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
611 | _("`upnpc' command not found\n")); | ||
612 | ac (ac_cls, | ||
613 | GNUNET_SYSERR, | ||
614 | NULL, 0, | ||
615 | GNUNET_NAT_ERROR_UPNPC_NOT_FOUND); | ||
616 | return NULL; | ||
617 | } | ||
618 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
619 | "Running `upnpc' to install mapping\n"); | ||
620 | ret = GNUNET_new (struct GNUNET_NAT_MiniHandle); | ||
621 | ret->ac = ac; | ||
622 | ret->ac_cls = ac_cls; | ||
623 | ret->is_tcp = is_tcp; | ||
624 | ret->port = port; | ||
625 | ret->refresh_task = | ||
626 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | ||
627 | &do_refresh, | ||
628 | ret); | ||
629 | run_upnpc_r (ret); | ||
630 | return ret; | ||
631 | } | ||
632 | |||
633 | |||
634 | /** | ||
635 | * Process output from our 'unmap' command. | ||
636 | * | ||
637 | * @param cls the `struct GNUNET_NAT_MiniHandle` | ||
638 | * @param line line of output, NULL at the end | ||
639 | */ | ||
640 | static void | ||
641 | process_unmap_output (void *cls, const char *line) | ||
642 | { | ||
643 | struct GNUNET_NAT_MiniHandle *mini = cls; | ||
644 | |||
645 | if (NULL == line) | ||
646 | { | ||
647 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
648 | "UPnP unmap done\n"); | ||
649 | GNUNET_OS_command_stop (mini->unmap_cmd); | ||
650 | mini->unmap_cmd = NULL; | ||
651 | GNUNET_free (mini); | ||
652 | return; | ||
653 | } | ||
654 | /* we don't really care about the output... */ | ||
655 | } | ||
656 | |||
657 | |||
658 | /** | ||
659 | * Remove a mapping created with (mini)upnpc. Calling | ||
660 | * this function will give 'upnpc' 1s to remove tha mapping, | ||
661 | * so while this function is non-blocking, a task will be | ||
662 | * left with the scheduler for up to 1s past this call. | ||
663 | * | ||
664 | * @param mini the handle | ||
665 | */ | ||
666 | void | ||
667 | GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini) | ||
668 | { | ||
669 | char pstr[6]; | ||
670 | |||
671 | if (NULL != mini->refresh_task) | ||
672 | { | ||
673 | GNUNET_SCHEDULER_cancel (mini->refresh_task); | ||
674 | mini->refresh_task = NULL; | ||
675 | } | ||
676 | if (NULL != mini->refresh_cmd) | ||
677 | { | ||
678 | GNUNET_OS_command_stop (mini->refresh_cmd); | ||
679 | mini->refresh_cmd = NULL; | ||
680 | } | ||
681 | if (NULL != mini->map_cmd) | ||
682 | { | ||
683 | GNUNET_OS_command_stop (mini->map_cmd); | ||
684 | mini->map_cmd = NULL; | ||
685 | } | ||
686 | if (GNUNET_NO == mini->did_map) | ||
687 | { | ||
688 | GNUNET_free (mini); | ||
689 | return; | ||
690 | } | ||
691 | mini->ac (mini->ac_cls, GNUNET_NO, | ||
692 | (const struct sockaddr *) &mini->current_addr, | ||
693 | sizeof (mini->current_addr), | ||
694 | GNUNET_NAT_ERROR_SUCCESS); | ||
695 | /* Note: oddly enough, deletion uses the external port whereas | ||
696 | * addition uses the internal port; this rarely matters since they | ||
697 | * often are the same, but it might... */ | ||
698 | GNUNET_snprintf (pstr, | ||
699 | sizeof (pstr), | ||
700 | "%u", | ||
701 | (unsigned int) ntohs (mini->current_addr.sin_port)); | ||
702 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
703 | "Unmapping port %u with UPnP\n", | ||
704 | ntohs (mini->current_addr.sin_port)); | ||
705 | mini->unmap_cmd = | ||
706 | GNUNET_OS_command_run (&process_unmap_output, mini, UNMAP_TIMEOUT, | ||
707 | "upnpc", "upnpc", "-d", pstr, | ||
708 | mini->is_tcp ? "tcp" : "udp", NULL); | ||
709 | } | ||
710 | |||
711 | |||
712 | /* end of nat_mini.c */ | ||