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