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