diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-06-25 06:24:01 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-06-25 06:24:01 +0000 |
commit | 8a3016481ba6aeb36de3950a56e641dda53ca544 (patch) | |
tree | 0c5468ec6e5d2165ad5931940e0b8439d09398de /src/nat | |
parent | b6d4eeb39fd0cd41df164bd0959f58b08aa45e83 (diff) | |
download | gnunet-8a3016481ba6aeb36de3950a56e641dda53ca544.tar.gz gnunet-8a3016481ba6aeb36de3950a56e641dda53ca544.zip |
the big NAT change
Diffstat (limited to 'src/nat')
-rw-r--r-- | src/nat/Makefile.am | 20 | ||||
-rw-r--r-- | src/nat/bsdqueue.h | 157 | ||||
-rw-r--r-- | src/nat/nat.c | 1307 | ||||
-rw-r--r-- | src/nat/nat.h | 72 | ||||
-rw-r--r-- | src/nat/natpmp.c | 325 | ||||
-rw-r--r-- | src/nat/natpmp.h | 46 | ||||
-rw-r--r-- | src/nat/test-nat.conf | 355 | ||||
-rw-r--r-- | src/nat/test_nat.c | 114 | ||||
-rw-r--r-- | src/nat/test_nat_data.conf | 136 | ||||
-rw-r--r-- | src/nat/upnp-commands.c | 877 | ||||
-rw-r--r-- | src/nat/upnp-commands.h | 271 | ||||
-rw-r--r-- | src/nat/upnp-discover.c | 1270 | ||||
-rw-r--r-- | src/nat/upnp-discover.h | 82 | ||||
-rw-r--r-- | src/nat/upnp-igd-parse.c | 207 | ||||
-rw-r--r-- | src/nat/upnp-igd-parse.h | 99 | ||||
-rw-r--r-- | src/nat/upnp-minixml.c | 239 | ||||
-rw-r--r-- | src/nat/upnp-minixml.h | 131 | ||||
-rw-r--r-- | src/nat/upnp-reply-parse.c | 166 | ||||
-rw-r--r-- | src/nat/upnp-reply-parse.h | 107 | ||||
-rw-r--r-- | src/nat/upnp.c | 422 | ||||
-rw-r--r-- | src/nat/upnp.h | 52 |
21 files changed, 1244 insertions, 5211 deletions
diff --git a/src/nat/Makefile.am b/src/nat/Makefile.am index 8542af8fa..2f30d3185 100644 --- a/src/nat/Makefile.am +++ b/src/nat/Makefile.am | |||
@@ -11,26 +11,12 @@ endif | |||
11 | lib_LTLIBRARIES = libgnunetnat.la | 11 | lib_LTLIBRARIES = libgnunetnat.la |
12 | 12 | ||
13 | libgnunetnat_la_SOURCES = \ | 13 | libgnunetnat_la_SOURCES = \ |
14 | upnp.c upnp.h \ | 14 | nat.c |
15 | upnp-commands.c upnp-commands.h \ | ||
16 | upnp-discover.c upnp-discover.h \ | ||
17 | upnp-igd-parse.c upnp-igd-parse.h \ | ||
18 | upnp-minixml.c upnp-minixml.h \ | ||
19 | upnp-reply-parse.c upnp-reply-parse.h bsdqueue.h \ | ||
20 | nat.c nat.h \ | ||
21 | natpmp.h | ||
22 | |||
23 | libgnunetnat_la_CFLAGS = \ | ||
24 | -DDEBUG_UPNP -g -O0 | ||
25 | 15 | ||
26 | libgnunetnat_la_LIBADD = \ | 16 | libgnunetnat_la_LIBADD = \ |
27 | $(top_builddir)/src/util/libgnunetutil.la \ | 17 | $(top_builddir)/src/util/libgnunetutil.la \ |
28 | @LIBCURL@ \ | ||
29 | $(GN_LIBINTL) @EXT_LIBS@ | 18 | $(GN_LIBINTL) @EXT_LIBS@ |
30 | 19 | ||
31 | libgnunetnat_la_CPPFLAGS = \ | ||
32 | @LIBCURL_CPPFLAGS@ | ||
33 | |||
34 | libgnunetnat_la_LDFLAGS = \ | 20 | libgnunetnat_la_LDFLAGS = \ |
35 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ | 21 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ |
36 | -version-info 0:0:0 | 22 | -version-info 0:0:0 |
@@ -39,7 +25,7 @@ check_PROGRAMS = \ | |||
39 | test-nat | 25 | test-nat |
40 | 26 | ||
41 | if ENABLE_TEST_RUN | 27 | if ENABLE_TEST_RUN |
42 | TESTS = $(check_PROGRAMS) | 28 | TESTS = $(check_PROGRAMS) |
43 | endif | 29 | endif |
44 | 30 | ||
45 | test_nat_SOURCES = \ | 31 | test_nat_SOURCES = \ |
@@ -51,4 +37,4 @@ test_nat_LDADD = \ | |||
51 | @LIBCURL@ | 37 | @LIBCURL@ |
52 | 38 | ||
53 | EXTRA_DIST = \ | 39 | EXTRA_DIST = \ |
54 | test-nat.conf | 40 | test_nat_data.conf |
diff --git a/src/nat/bsdqueue.h b/src/nat/bsdqueue.h deleted file mode 100644 index 7b6cb60b3..000000000 --- a/src/nat/bsdqueue.h +++ /dev/null | |||
@@ -1,157 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file nat/bsdqueue.h | ||
23 | * @brief BSD implementation of simple lists | ||
24 | * | ||
25 | * @author Milan Bouchet-Valat | ||
26 | */ | ||
27 | |||
28 | /* | ||
29 | * Copyright (c) 1991, 1993 | ||
30 | * The Regents of the University of California. All rights reserved. | ||
31 | * | ||
32 | * Redistribution and use in source and binary forms, with or without | ||
33 | * modification, are permitted provided that the following conditions | ||
34 | * are met: | ||
35 | * 1. Redistributions of source code must retain the above copyright | ||
36 | * notice, this list of conditions and the following disclaimer. | ||
37 | * 2. Redistributions in binary form must reproduce the above copyright | ||
38 | * notice, this list of conditions and the following disclaimer in the | ||
39 | * documentation and/or other materials provided with the distribution. | ||
40 | * 3. Neither the name of the University nor the names of its contributors | ||
41 | * may be used to endorse or promote products derived from this software | ||
42 | * without specific prior written permission. | ||
43 | * | ||
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
54 | * SUCH DAMAGE. | ||
55 | * | ||
56 | * @(#)queue.h 8.5 (Berkeley) 8/20/94 | ||
57 | */ | ||
58 | |||
59 | #ifndef BSD_QUEUE_H | ||
60 | #define BSD_QUEUE_H | ||
61 | |||
62 | /* | ||
63 | * A list is headed by a single forward pointer (or an array of forward | ||
64 | * pointers for a hash table header). The elements are doubly linked | ||
65 | * so that an arbitrary element can be removed without a need to | ||
66 | * traverse the list. New elements can be added to the list before | ||
67 | * or after an existing element or at the head of the list. A list | ||
68 | * may only be traversed in the forward direction. | ||
69 | */ | ||
70 | |||
71 | #ifdef QUEUE_MACRO_DEBUG | ||
72 | #define _Q_INVALIDATE(a) (a) = ((void *)-1) | ||
73 | #else | ||
74 | #define _Q_INVALIDATE(a) | ||
75 | #endif | ||
76 | |||
77 | /* | ||
78 | * List definitions. | ||
79 | */ | ||
80 | #define LIST_HEAD(name, type) \ | ||
81 | struct name { \ | ||
82 | struct type *lh_first; /* first element */ \ | ||
83 | } | ||
84 | |||
85 | #define LIST_HEAD_INITIALIZER(head) \ | ||
86 | { NULL } | ||
87 | |||
88 | #define LIST_ENTRY(type) \ | ||
89 | struct { \ | ||
90 | struct type *le_next; /* next element */ \ | ||
91 | struct type **le_prev; /* address of previous next element */ \ | ||
92 | } | ||
93 | |||
94 | /* | ||
95 | * List access methods | ||
96 | */ | ||
97 | #define LIST_FIRST(head) ((head)->lh_first) | ||
98 | #define LIST_END(head) NULL | ||
99 | #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head)) | ||
100 | #define LIST_NEXT(elm, field) ((elm)->field.le_next) | ||
101 | |||
102 | #define LIST_FOREACH(var, head, field) \ | ||
103 | for((var) = LIST_FIRST(head); \ | ||
104 | (var)!= LIST_END(head); \ | ||
105 | (var) = LIST_NEXT(var, field)) | ||
106 | |||
107 | /* | ||
108 | * List functions. | ||
109 | */ | ||
110 | #define LIST_INIT(head) do { \ | ||
111 | LIST_FIRST(head) = LIST_END(head); \ | ||
112 | } while (0) | ||
113 | |||
114 | #define LIST_INSERT_AFTER(listelm, elm, field) do { \ | ||
115 | if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ | ||
116 | (listelm)->field.le_next->field.le_prev = \ | ||
117 | &(elm)->field.le_next; \ | ||
118 | (listelm)->field.le_next = (elm); \ | ||
119 | (elm)->field.le_prev = &(listelm)->field.le_next; \ | ||
120 | } while (0) | ||
121 | |||
122 | #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ | ||
123 | (elm)->field.le_prev = (listelm)->field.le_prev; \ | ||
124 | (elm)->field.le_next = (listelm); \ | ||
125 | *(listelm)->field.le_prev = (elm); \ | ||
126 | (listelm)->field.le_prev = &(elm)->field.le_next; \ | ||
127 | } while (0) | ||
128 | |||
129 | #define LIST_INSERT_HEAD(head, elm, field) do { \ | ||
130 | if (((elm)->field.le_next = (head)->lh_first) != NULL) \ | ||
131 | (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ | ||
132 | (head)->lh_first = (elm); \ | ||
133 | (elm)->field.le_prev = &(head)->lh_first; \ | ||
134 | } while (0) | ||
135 | |||
136 | #define LIST_REMOVE(elm, field) do { \ | ||
137 | if ((elm)->field.le_next != NULL) \ | ||
138 | (elm)->field.le_next->field.le_prev = \ | ||
139 | (elm)->field.le_prev; \ | ||
140 | *(elm)->field.le_prev = (elm)->field.le_next; \ | ||
141 | _Q_INVALIDATE((elm)->field.le_prev); \ | ||
142 | _Q_INVALIDATE((elm)->field.le_next); \ | ||
143 | } while (0) | ||
144 | |||
145 | #define LIST_REPLACE(elm, elm2, field) do { \ | ||
146 | if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ | ||
147 | (elm2)->field.le_next->field.le_prev = \ | ||
148 | &(elm2)->field.le_next; \ | ||
149 | (elm2)->field.le_prev = (elm)->field.le_prev; \ | ||
150 | *(elm2)->field.le_prev = (elm2); \ | ||
151 | _Q_INVALIDATE((elm)->field.le_prev); \ | ||
152 | _Q_INVALIDATE((elm)->field.le_next); \ | ||
153 | } while (0) | ||
154 | |||
155 | #endif | ||
156 | |||
157 | /* end of bsd-queue.h */ | ||
diff --git a/src/nat/nat.c b/src/nat/nat.c index 7e83134bd..e92f57259 100644 --- a/src/nat/nat.c +++ b/src/nat/nat.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | 3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -18,345 +18,841 @@ | |||
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /* | ||
22 | * Parts of this file have been adapted from the Transmission project: | ||
23 | * Originally licensed by the GPL version 2. | ||
24 | * Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com> | ||
25 | */ | ||
26 | |||
27 | /** | 21 | /** |
28 | * @file nat/nat.c | 22 | * @file nat/nat.c |
29 | * @brief Library handling UPnP and NAT-PMP port forwarding and | 23 | * @brief Library handling UPnP and NAT-PMP port forwarding and |
30 | * external IP address retrieval | 24 | * external IP address retrieval |
31 | * | ||
32 | * @author Milan Bouchet-Valat | 25 | * @author Milan Bouchet-Valat |
26 | * @author Christian Grothoff | ||
27 | * | ||
28 | * TODO: | ||
29 | * - implement UPnP/PMP support | ||
30 | * - repeatedly perform certain checks again to notice changes | ||
33 | */ | 31 | */ |
34 | #include "platform.h" | 32 | #include "platform.h" |
35 | #include "gnunet_util_lib.h" | 33 | #include "gnunet_util_lib.h" |
34 | #include "gnunet_resolver_service.h" | ||
36 | #include "gnunet_nat_lib.h" | 35 | #include "gnunet_nat_lib.h" |
37 | #include "nat.h" | 36 | |
38 | #include "natpmp.h" | 37 | |
39 | #include "upnp.h" | 38 | /** |
39 | * How long until we give up on transmitting the welcome message? | ||
40 | */ | ||
41 | #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
42 | |||
43 | |||
44 | /** | ||
45 | * Where did the given local address originate from? | ||
46 | * To be used for debugging as well as in the future | ||
47 | * to remove all addresses from a certain source when | ||
48 | * we reevaluate the source. | ||
49 | */ | ||
50 | enum LocalAddressSource | ||
51 | { | ||
52 | /** | ||
53 | * Address was obtained by DNS resolution of the external hostname | ||
54 | * given in the configuration (i.e. hole-punched DynDNS setup). | ||
55 | * FIXME: repeatedly do the lookup to notice changes! | ||
56 | */ | ||
57 | LAL_EXTERNAL_IP, | ||
58 | |||
59 | /** | ||
60 | * Address was obtained by looking up our own hostname in DNS. | ||
61 | * FIXME: repeatedly do the lookup to notice changes! | ||
62 | */ | ||
63 | LAL_HOSTNAME_DNS, | ||
64 | |||
65 | /** | ||
66 | * Address was obtained by scanning our hosts's network interfaces | ||
67 | * and taking their address (no DNS involved). | ||
68 | * FIXME: repeatedly do the lookup to notice changes! | ||
69 | */ | ||
70 | LAL_INTERFACE_ADDRESS, | ||
71 | |||
72 | /* TODO: add UPnP, etc. */ | ||
73 | |||
74 | /** | ||
75 | * End of the list. | ||
76 | */ | ||
77 | LAL_END | ||
78 | |||
79 | }; | ||
80 | |||
81 | |||
82 | /** | ||
83 | * List of local addresses that we currently deem valid. Actual | ||
84 | * struct is followed by the 'struct sockaddr'. Note that the code | ||
85 | * intentionally makes no attempt to ensure that a particular address | ||
86 | * is only listed once (especially since it may come from different | ||
87 | * sources, and the source is an "internal" construct). | ||
88 | */ | ||
89 | struct LocalAddressList | ||
90 | { | ||
91 | /** | ||
92 | * This is a linked list. | ||
93 | */ | ||
94 | struct LocalAddressList *next; | ||
95 | |||
96 | /** | ||
97 | * Previous entry. | ||
98 | */ | ||
99 | struct LocalAddressList *prev; | ||
100 | |||
101 | /** | ||
102 | * Number of bytes of address that follow. | ||
103 | */ | ||
104 | socklen_t addrlen; | ||
105 | |||
106 | /** | ||
107 | * Origin of the local address. | ||
108 | */ | ||
109 | enum LocalAddressSource source; | ||
110 | }; | ||
111 | |||
40 | 112 | ||
41 | /** | 113 | /** |
42 | * Handle for active NAT registrations. | 114 | * Handle for active NAT registrations. |
43 | */ | 115 | */ |
44 | struct GNUNET_NAT_Handle | 116 | struct GNUNET_NAT_Handle |
45 | { | 117 | { |
118 | |||
119 | /** | ||
120 | * Configuration to use. | ||
121 | */ | ||
122 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
123 | |||
124 | /** | ||
125 | * Function to call when we learn about a new address. | ||
126 | */ | ||
127 | GNUNET_NAT_AddressCallback address_callback; | ||
128 | |||
46 | /** | 129 | /** |
47 | * Handle for UPnP operations. | 130 | * Function to call when we notice another peer asking for |
131 | * connection reversal. | ||
48 | */ | 132 | */ |
49 | struct GNUNET_NAT_UPNP_Handle *upnp; | 133 | GNUNET_NAT_ReversalCallback reversal_callback; |
50 | 134 | ||
51 | /** | 135 | /** |
52 | * Handle for NAT PMP operations. | 136 | * Closure for 'callback'. |
53 | */ | 137 | */ |
54 | struct GNUNET_NAT_NATPMP_Handle *natpmp; | 138 | void *callback_cls; |
55 | 139 | ||
56 | /** | 140 | /** |
57 | * LAN address as passed by the caller | 141 | * Handle for (DYN)DNS lookup of our external IP. |
58 | */ | 142 | */ |
59 | struct sockaddr *local_addr; | 143 | struct GNUNET_RESOLVER_RequestHandle *ext_dns; |
60 | 144 | ||
61 | /** | 145 | /** |
62 | * External address as reported by found NAT box | 146 | * Handle for request of hostname resolution, non-NULL if pending. |
63 | */ | 147 | */ |
64 | struct sockaddr *ext_addr; | 148 | struct GNUNET_RESOLVER_RequestHandle *hostname_dns; |
65 | 149 | ||
66 | /** | 150 | /** |
67 | * External address as reported by each type of NAT box | 151 | * stdout pipe handle for the gnunet-nat-server process |
68 | */ | 152 | */ |
69 | struct sockaddr *ext_addr_upnp; | 153 | struct GNUNET_DISK_PipeHandle *server_stdout; |
70 | struct sockaddr *ext_addr_natpmp; | ||
71 | 154 | ||
72 | /** | 155 | /** |
73 | * External address and port where packets are redirected | 156 | * stdout file handle (for reading) for the gnunet-nat-server process |
74 | */ | 157 | */ |
75 | struct sockaddr *contact_addr; | 158 | const struct GNUNET_DISK_FileHandle *server_stdout_handle; |
76 | 159 | ||
77 | GNUNET_NAT_AddressCallback callback; | 160 | /** |
161 | * Linked list of currently valid addresses (head). | ||
162 | */ | ||
163 | struct LocalAddressList *lal_head; | ||
78 | 164 | ||
79 | /** | 165 | /** |
80 | * Closure for 'callback'. | 166 | * Linked list of currently valid addresses (tail). |
81 | */ | 167 | */ |
82 | void *callback_cls; | 168 | struct LocalAddressList *lal_tail; |
83 | 169 | ||
84 | GNUNET_SCHEDULER_TaskIdentifier pulse_timer; | 170 | /** |
171 | * How long do we wait for restarting a crashed gnunet-nat-server? | ||
172 | */ | ||
173 | struct GNUNET_TIME_Relative server_retry_delay; | ||
85 | 174 | ||
86 | enum GNUNET_NAT_PortState natpmp_status; | 175 | /** |
176 | * ID of select gnunet-nat-server stdout read task | ||
177 | */ | ||
178 | GNUNET_SCHEDULER_TaskIdentifier server_read_task; | ||
87 | 179 | ||
88 | enum GNUNET_NAT_PortState upnp_status; | 180 | /** |
181 | * The process id of the server process (if behind NAT) | ||
182 | */ | ||
183 | struct GNUNET_OS_Process *server_proc; | ||
89 | 184 | ||
90 | int is_enabled; | 185 | /** |
186 | * LAN address as passed by the caller (array). | ||
187 | */ | ||
188 | struct sockaddr **local_addrs; | ||
91 | 189 | ||
92 | int should_change; | 190 | /** |
191 | * Length of the 'local_addrs'. | ||
192 | */ | ||
193 | socklen_t *local_addrlens; | ||
194 | |||
195 | /** | ||
196 | * Number of entries in 'local_addrs' array. | ||
197 | */ | ||
198 | unsigned int num_local_addrs; | ||
199 | |||
200 | /** | ||
201 | * The our external address (according to config, UPnP may disagree...) | ||
202 | */ | ||
203 | char *external_address; | ||
204 | |||
205 | /** | ||
206 | * Presumably our internal address (according to config) | ||
207 | */ | ||
208 | char *internal_address; | ||
209 | |||
210 | /** | ||
211 | * Is this transport configured to be behind a NAT? | ||
212 | */ | ||
213 | int behind_nat; | ||
214 | |||
215 | /** | ||
216 | * Has the NAT been punched? (according to config) | ||
217 | */ | ||
218 | int nat_punched; | ||
219 | |||
220 | /** | ||
221 | * Is this transport configured to allow connections to NAT'd peers? | ||
222 | */ | ||
223 | int enable_nat_client; | ||
93 | 224 | ||
94 | int port_mapped; | 225 | /** |
226 | * Should we run the gnunet-nat-server? | ||
227 | */ | ||
228 | int enable_nat_server; | ||
95 | 229 | ||
96 | int old_status; | 230 | /** |
231 | * Are we allowed to try UPnP/PMP for NAT traversal? | ||
232 | */ | ||
233 | int enable_upnp; | ||
97 | 234 | ||
98 | int new_status; | 235 | /** |
236 | * Should we use local addresses (loopback)? (according to config) | ||
237 | */ | ||
238 | int use_localaddresses; | ||
99 | 239 | ||
100 | int did_warn; | 240 | /** |
241 | * Is using IPv6 disabled? | ||
242 | */ | ||
243 | int disable_ipv6; | ||
101 | 244 | ||
102 | int processing; | 245 | /** |
246 | * Is this TCP or UDP? | ||
247 | */ | ||
248 | int is_tcp; | ||
103 | 249 | ||
104 | uint16_t public_port; | 250 | /** |
251 | * Port we advertise to the outside. | ||
252 | */ | ||
253 | uint16_t adv_port; | ||
105 | 254 | ||
106 | }; | 255 | }; |
107 | 256 | ||
108 | #ifdef DEBUG | ||
109 | static const char * | ||
110 | get_nat_state_str (enum GNUNET_NAT_PortState state) | ||
111 | { | ||
112 | switch (state) | ||
113 | { | ||
114 | case GNUNET_NAT_PORT_MAPPING: | ||
115 | return "Starting"; | ||
116 | case GNUNET_NAT_PORT_MAPPED: | ||
117 | return "Forwarded"; | ||
118 | case GNUNET_NAT_PORT_UNMAPPING: | ||
119 | return "Stopping"; | ||
120 | case GNUNET_NAT_PORT_UNMAPPED: | ||
121 | return "Not forwarded"; | ||
122 | case GNUNET_NAT_PORT_ERROR: | ||
123 | return "Redirection failed"; | ||
124 | default: | ||
125 | return "not found"; | ||
126 | } | ||
127 | } | ||
128 | #endif | ||
129 | 257 | ||
258 | /** | ||
259 | * Try to start the gnunet-nat-server (if it is not | ||
260 | * already running). | ||
261 | * | ||
262 | * @param h handle to NAT | ||
263 | */ | ||
264 | static void | ||
265 | start_gnunet_nat_server (struct GNUNET_NAT_Handle *h); | ||
130 | 266 | ||
131 | static int | 267 | |
132 | get_traversal_status (const struct GNUNET_NAT_Handle *h) | 268 | /** |
269 | * Add the given address to the list of 'local' addresses, thereby | ||
270 | * making it a 'legal' address for this peer to have. | ||
271 | * | ||
272 | * @param plugin the plugin | ||
273 | * @param src where did the local address originate from? | ||
274 | * @param arg the address, some 'struct sockaddr' | ||
275 | * @param arg_size number of bytes in arg | ||
276 | */ | ||
277 | static void | ||
278 | add_to_address_list_as_is (struct GNUNET_NAT_Handle *h, | ||
279 | enum LocalAddressSource src, | ||
280 | const struct sockaddr *arg, | ||
281 | socklen_t arg_size) | ||
133 | { | 282 | { |
134 | return GNUNET_MAX (h->natpmp_status, h->upnp_status); | 283 | struct LocalAddressList *lal; |
284 | |||
285 | lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size); | ||
286 | memcpy (&lal[1], arg, arg_size); | ||
287 | lal->addrlen = arg_size; | ||
288 | lal->source = src; | ||
289 | GNUNET_CONTAINER_DLL_insert (h->lal_head, | ||
290 | h->lal_tail, | ||
291 | lal); | ||
292 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
293 | "nat", | ||
294 | "Adding address `%s' from source %d\n", | ||
295 | GNUNET_a2s (arg, arg_size), | ||
296 | src); | ||
297 | h->address_callback (h->callback_cls, | ||
298 | GNUNET_YES, | ||
299 | arg, | ||
300 | arg_size); | ||
135 | } | 301 | } |
136 | 302 | ||
137 | 303 | ||
138 | /** | 304 | /** |
139 | * Compare the sin(6)_addr fields of AF_INET or AF_INET(6) sockaddr. | 305 | * Add the given address to the list of 'local' addresses, thereby |
140 | * @param a first sockaddr | 306 | * making it a 'legal' address for this peer to have. Set the |
141 | * @param b second sockaddr | 307 | * port number in the process to the advertised port and possibly |
142 | * @return 0 if addresses are equal, non-null value otherwise */ | 308 | * also to zero (if we have the gnunet-nat-server). |
143 | int | 309 | * |
144 | GNUNET_NAT_cmp_addr (const struct sockaddr *a, const struct sockaddr *b) | 310 | * @param plugin the plugin |
311 | * @param src where did the local address originate from? | ||
312 | * @param arg the address, some 'struct sockaddr' | ||
313 | * @param arg_size number of bytes in arg | ||
314 | */ | ||
315 | static void | ||
316 | add_to_address_list (struct GNUNET_NAT_Handle *h, | ||
317 | enum LocalAddressSource src, | ||
318 | const struct sockaddr *arg, | ||
319 | socklen_t arg_size) | ||
145 | { | 320 | { |
146 | if (!(a && b)) | 321 | struct sockaddr_in s4; |
147 | return -1; | 322 | const struct sockaddr_in *in4; |
148 | if ((a->sa_family == AF_INET) && (b->sa_family == AF_INET)) | 323 | struct sockaddr_in6 s6; |
149 | return memcmp (&(((struct sockaddr_in *) a)->sin_addr), | 324 | const struct sockaddr_in6 *in6; |
150 | &(((struct sockaddr_in *) b)->sin_addr), | 325 | |
151 | sizeof (struct in_addr)); | 326 | if (arg_size == sizeof (struct sockaddr_in)) |
152 | if ((a->sa_family == AF_INET6) && (b->sa_family == AF_INET6)) | 327 | { |
153 | return memcmp (&(((struct sockaddr_in6 *) a)->sin6_addr), | 328 | in4 = (const struct sockaddr_in *) arg; |
154 | &(((struct sockaddr_in6 *) b)->sin6_addr), | 329 | s4 = *in4; |
155 | sizeof (struct in6_addr)); | 330 | s4.sin_port = htons (h->adv_port); |
156 | return -1; | 331 | add_to_address_list_as_is (h, |
332 | src, | ||
333 | (const struct sockaddr*) &s4, | ||
334 | sizeof (struct sockaddr_in)); | ||
335 | if (GNUNET_YES == h->enable_nat_server) | ||
336 | { | ||
337 | /* also add with PORT = 0 to indicate NAT server is enabled */ | ||
338 | s4.sin_port = htons(0); | ||
339 | add_to_address_list_as_is (h, | ||
340 | src, | ||
341 | (const struct sockaddr*) &s4, | ||
342 | sizeof (struct sockaddr_in)); | ||
343 | } | ||
344 | } | ||
345 | else if (arg_size == sizeof (struct sockaddr_in6)) | ||
346 | { | ||
347 | if (GNUNET_YES != h->disable_ipv6) | ||
348 | { | ||
349 | in6 = (const struct sockaddr_in6 *) arg; | ||
350 | s6 = *in6; | ||
351 | s6.sin6_port = htons(h->adv_port); | ||
352 | add_to_address_list_as_is (h, | ||
353 | src, | ||
354 | (const struct sockaddr*) &s6, | ||
355 | sizeof (struct sockaddr_in6)); | ||
356 | } | ||
357 | } | ||
358 | else | ||
359 | { | ||
360 | GNUNET_assert (0); | ||
361 | } | ||
157 | } | 362 | } |
158 | 363 | ||
159 | 364 | ||
160 | /** | 365 | /** |
161 | * Deal with a new IP address or port redirection: | 366 | * Add the given IP address to the list of 'local' addresses, thereby |
162 | * Send signals with the appropriate sockaddr (IP and port), free and changes | 367 | * making it a 'legal' address for this peer to have. |
163 | * or nullify the previous sockaddr. Change the port if needed. | 368 | * |
369 | * @param plugin the plugin | ||
370 | * @param src where did the local address originate from? | ||
371 | * @param arg the address, some 'struct in_addr' or 'struct in6_addr' | ||
372 | * @param arg_size number of bytes in arg | ||
164 | */ | 373 | */ |
165 | static void | 374 | static void |
166 | notify_change (struct GNUNET_NAT_Handle *h, | 375 | add_ip_to_address_list (struct GNUNET_NAT_Handle *h, |
167 | struct sockaddr *addr, size_t addrlen, int new_port_mapped) | 376 | enum LocalAddressSource src, |
377 | const void *addr, | ||
378 | socklen_t addrlen) | ||
168 | { | 379 | { |
169 | if (new_port_mapped == h->port_mapped) | 380 | struct sockaddr_in s4; |
170 | return; | 381 | const struct in_addr *in4; |
171 | h->port_mapped = new_port_mapped; | 382 | struct sockaddr_in6 s6; |
172 | 383 | const struct in6_addr *in6; | |
173 | if ((NULL != h->contact_addr) && (NULL != h->callback)) | ||
174 | h->callback (h->callback_cls, | ||
175 | GNUNET_NO, h->contact_addr, sizeof (h->contact_addr)); | ||
176 | GNUNET_free_non_null (h->contact_addr); | ||
177 | h->contact_addr = NULL; | ||
178 | GNUNET_free_non_null (h->ext_addr); | ||
179 | h->ext_addr = NULL; | ||
180 | if (NULL == addr) | ||
181 | return; | ||
182 | h->ext_addr = GNUNET_malloc (addrlen); | ||
183 | memcpy (h->ext_addr, addr, addrlen); | ||
184 | 384 | ||
185 | /* Recreate the ext_addr:public_port bogus address to pass to the callback */ | 385 | if (addrlen == sizeof (struct in_addr)) |
186 | if (h->ext_addr->sa_family == AF_INET) | ||
187 | { | 386 | { |
188 | struct sockaddr_in *tmp_addr; | 387 | in4 = (const struct in_addr *) addr; |
189 | 388 | memset (&s4, 0, sizeof (s4)); | |
190 | tmp_addr = GNUNET_malloc (sizeof (struct sockaddr_in)); | 389 | s4.sin_family = AF_INET; |
191 | tmp_addr->sin_family = AF_INET; | 390 | s4.sin_port = 0; |
192 | #ifdef HAVE_SOCKADDR_IN_SIN_LEN | 391 | #if HAVE_SOCKADDR_IN_SIN_LEN |
193 | tmp_addr->sin_len = sizeof (struct sockaddr_in); | 392 | s4.sin_len = (u_char) sizeof (struct sockaddr_in); |
194 | #endif | 393 | #endif |
195 | tmp_addr->sin_port = h->port_mapped ? htons (h->public_port) : 0; | 394 | s4.sin_addr = *in4; |
196 | tmp_addr->sin_addr = ((struct sockaddr_in *) h->ext_addr)->sin_addr; | 395 | add_to_address_list (h, |
197 | h->contact_addr = (struct sockaddr *) tmp_addr; | 396 | src, |
198 | 397 | (const struct sockaddr*) &s4, | |
199 | if (NULL != h->callback) | 398 | sizeof (struct sockaddr_in)); |
200 | h->callback (h->callback_cls, | 399 | if (GNUNET_YES == h->enable_nat_server) |
201 | GNUNET_YES, | 400 | { |
202 | h->contact_addr, sizeof (struct sockaddr_in)); | 401 | /* also add with PORT = 0 to indicate NAT server is enabled */ |
402 | s4.sin_port = htons(0); | ||
403 | add_to_address_list (h, | ||
404 | src, | ||
405 | (const struct sockaddr*) &s4, | ||
406 | sizeof (struct sockaddr_in)); | ||
407 | |||
408 | } | ||
203 | } | 409 | } |
204 | else if (h->ext_addr->sa_family == AF_INET6) | 410 | else if (addrlen == sizeof (struct in6_addr)) |
205 | { | 411 | { |
206 | struct sockaddr_in6 *tmp_addr; | 412 | if (GNUNET_YES != h->disable_ipv6) |
207 | 413 | { | |
208 | tmp_addr = GNUNET_malloc (sizeof (struct sockaddr_in6)); | 414 | in6 = (const struct in6_addr *) addr; |
209 | tmp_addr->sin6_family = AF_INET6; | 415 | memset (&s6, 0, sizeof (s6)); |
210 | #ifdef HAVE_SOCKADDR_IN_SIN_LEN | 416 | s6.sin6_family = AF_INET6; |
211 | tmp_addr->sin6_len = sizeof (struct sockaddr_in6); | 417 | s6.sin6_port = htons(h->adv_port); |
418 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
419 | s6.sin6_len = (u_char) sizeof (struct sockaddr_in6); | ||
212 | #endif | 420 | #endif |
213 | tmp_addr->sin6_port = h->port_mapped ? htons (h->public_port) : 0; | 421 | s6.sin6_addr = *in6; |
214 | tmp_addr->sin6_addr = ((struct sockaddr_in6 *) h->ext_addr)->sin6_addr; | 422 | add_to_address_list (h, |
215 | h->contact_addr = (struct sockaddr *) tmp_addr; | 423 | src, |
216 | 424 | (const struct sockaddr*) &s6, | |
217 | if (NULL != h->callback) | 425 | sizeof (struct sockaddr_in6)); |
218 | h->callback (h->callback_cls, | 426 | } |
219 | GNUNET_YES, | ||
220 | h->contact_addr, sizeof (struct sockaddr_in6)); | ||
221 | } | 427 | } |
222 | else | 428 | else |
223 | { | 429 | { |
224 | GNUNET_break (0); | 430 | GNUNET_assert (0); |
225 | } | 431 | } |
226 | } | 432 | } |
227 | 433 | ||
228 | static void nat_pulse (void *cls, | ||
229 | const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
230 | 434 | ||
435 | /** | ||
436 | * Our (external) hostname was resolved and the configuration says that | ||
437 | * the NAT was hole-punched. | ||
438 | * | ||
439 | * @param cls the 'struct Plugin' | ||
440 | * @param addr NULL on error, otherwise result of DNS lookup | ||
441 | * @param addrlen number of bytes in addr | ||
442 | */ | ||
231 | static void | 443 | static void |
232 | pulse_cb (struct GNUNET_NAT_Handle *h) | 444 | process_external_ip (void *cls, |
445 | const struct sockaddr *addr, | ||
446 | socklen_t addrlen) | ||
233 | { | 447 | { |
234 | socklen_t addrlen; | 448 | struct GNUNET_NAT_Handle *h = cls; |
235 | int port_mapped; | ||
236 | 449 | ||
237 | /* One of the protocols is still working, wait for it to complete */ | 450 | if (addr == NULL) |
238 | if (h->processing) | 451 | { |
239 | return; | 452 | h->ext_dns = NULL; |
453 | /* FIXME: schedule task to resolve IP again in the | ||
454 | future, and if the result changes, update the | ||
455 | local address list accordingly */ | ||
456 | return; | ||
457 | } | ||
458 | add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen); | ||
459 | } | ||
240 | 460 | ||
241 | h->new_status = get_traversal_status (h); | ||
242 | if ((h->old_status != h->new_status) && | ||
243 | ((h->new_status == GNUNET_NAT_PORT_UNMAPPED) || | ||
244 | (h->new_status == GNUNET_NAT_PORT_ERROR))) | ||
245 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | ||
246 | "NAT", | ||
247 | _ | ||
248 | ("Port redirection failed: no UPnP or NAT-PMP routers supporting this feature found\n")); | ||
249 | #ifdef DEBUG | ||
250 | if (h->new_status != h->old_status) | ||
251 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "NAT", | ||
252 | _("State changed from `%s' to `%s'\n"), | ||
253 | get_nat_state_str (h->old_status), | ||
254 | get_nat_state_str (h->new_status)); | ||
255 | #endif | ||
256 | 461 | ||
257 | port_mapped = (h->new_status == GNUNET_NAT_PORT_MAPPED); | 462 | /** |
258 | if (!(h->ext_addr_upnp || h->ext_addr_natpmp)) | 463 | * Function called by the resolver for each address obtained from DNS |
464 | * for our own hostname. Add the addresses to the list of our IP | ||
465 | * addresses. | ||
466 | * | ||
467 | * @param cls closure | ||
468 | * @param addr one of the addresses of the host, NULL for the last address | ||
469 | * @param addrlen length of the address | ||
470 | */ | ||
471 | static void | ||
472 | process_hostname_ip (void *cls, | ||
473 | const struct sockaddr *addr, socklen_t addrlen) | ||
474 | { | ||
475 | struct GNUNET_NAT_Handle *h = cls; | ||
476 | |||
477 | if (addr == NULL) | ||
259 | { | 478 | { |
260 | /* Address has just changed and we could not get it, or it's the first try, | 479 | h->hostname_dns = NULL; |
261 | * and we're not waiting for a reply from UPnP or NAT-PMP */ | 480 | /* FIXME: schedule task to resolve IP again in the |
262 | if (((NULL != h->ext_addr) || | 481 | future, and if the result changes, update the |
263 | (GNUNET_NO == h->did_warn)) && h->processing != 0) | 482 | address list accordingly */ |
264 | { | 483 | return; |
265 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | ||
266 | "NAT", | ||
267 | _("Could not determine external IP address\n")); | ||
268 | h->did_warn = GNUNET_YES; | ||
269 | } | ||
270 | notify_change (h, NULL, 0, port_mapped); | ||
271 | } | 484 | } |
272 | else if (h->ext_addr_upnp | 485 | add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen); |
273 | && GNUNET_NAT_cmp_addr (h->ext_addr, h->ext_addr_upnp) != 0) | 486 | } |
487 | |||
488 | |||
489 | /** | ||
490 | * Add the IP of our network interface to the list of | ||
491 | * our IP addresses. | ||
492 | * | ||
493 | * @param cls the 'struct GNUNET_NAT_Handle' | ||
494 | * @param name name of the interface | ||
495 | * @param isDefault do we think this may be our default interface | ||
496 | * @param addr address of the interface | ||
497 | * @param addrlen number of bytes in addr | ||
498 | * @return GNUNET_OK to continue iterating | ||
499 | */ | ||
500 | static int | ||
501 | process_interfaces (void *cls, | ||
502 | const char *name, | ||
503 | int isDefault, | ||
504 | const struct sockaddr *addr, socklen_t addrlen) | ||
505 | { | ||
506 | struct GNUNET_NAT_Handle *h = cls; | ||
507 | const struct sockaddr_in *s4; | ||
508 | const struct sockaddr_in6 *s6; | ||
509 | const void *ip; | ||
510 | char buf[INET6_ADDRSTRLEN]; | ||
511 | |||
512 | switch (addr->sa_family) | ||
274 | { | 513 | { |
275 | addrlen = h->ext_addr_upnp->sa_family == AF_INET ? | 514 | case AF_INET: |
276 | sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6); | 515 | s4 = (struct sockaddr_in *) addr; |
277 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | 516 | ip = &s4->sin_addr; |
278 | "NAT", | 517 | if (GNUNET_YES == h->use_localaddresses) |
279 | _("External IP address changed to %s\n"), | 518 | add_ip_to_address_list (h, |
280 | GNUNET_a2s (h->ext_addr_upnp, addrlen)); | 519 | LAL_INTERFACE_ADDRESS, |
281 | notify_change (h, h->ext_addr_upnp, addrlen, port_mapped); | 520 | &s4->sin_addr, |
521 | sizeof (struct in_addr)); | ||
522 | break; | ||
523 | case AF_INET6: | ||
524 | s6 = (struct sockaddr_in6 *) addr; | ||
525 | if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr)) | ||
526 | { | ||
527 | /* skip link local addresses */ | ||
528 | return GNUNET_OK; | ||
529 | } | ||
530 | ip = &s6->sin6_addr; | ||
531 | if (GNUNET_YES == h->use_localaddresses) | ||
532 | add_ip_to_address_list (h, | ||
533 | LAL_INTERFACE_ADDRESS, | ||
534 | &s6->sin6_addr, | ||
535 | sizeof (struct in6_addr)); | ||
536 | break; | ||
537 | default: | ||
538 | GNUNET_break (0); | ||
539 | break; | ||
282 | } | 540 | } |
283 | else if (h->ext_addr_natpmp | 541 | if ( (h->internal_address == NULL) && |
284 | && GNUNET_NAT_cmp_addr (h->ext_addr, h->ext_addr_natpmp) != 0) | 542 | (h->server_proc == NULL) && |
543 | (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) && | ||
544 | (GNUNET_YES == isDefault) && | ||
545 | ( (addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6) ) ) | ||
285 | { | 546 | { |
286 | addrlen = h->ext_addr_natpmp->sa_family == AF_INET ? | 547 | /* no internal address configured, but we found a "default" |
287 | sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6); | 548 | interface, try using that as our 'internal' address */ |
288 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "NAT", | 549 | h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family, |
289 | _("External IP address changed to `%s'\n"), | 550 | ip, |
290 | GNUNET_a2s (h->ext_addr_natpmp, addrlen)); | 551 | buf, |
291 | notify_change (h, h->ext_addr_natpmp, addrlen, port_mapped); | 552 | sizeof (buf))); |
553 | start_gnunet_nat_server (h); | ||
292 | } | 554 | } |
293 | 555 | return GNUNET_OK; | |
294 | h->pulse_timer = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
295 | &nat_pulse, h); | ||
296 | } | 556 | } |
297 | 557 | ||
298 | static void | 558 | |
299 | upnp_pulse_cb (int status, struct sockaddr *ext_addr, void *cls) | 559 | /** |
560 | * Return the actual path to a file found in the current | ||
561 | * PATH environment variable. | ||
562 | * | ||
563 | * @param binary the name of the file to find | ||
564 | * @return path to binary, NULL if not found | ||
565 | */ | ||
566 | static char * | ||
567 | get_path_from_PATH (const char *binary) | ||
300 | { | 568 | { |
301 | struct GNUNET_NAT_Handle *h = cls; | 569 | char *path; |
570 | char *pos; | ||
571 | char *end; | ||
572 | char *buf; | ||
573 | const char *p; | ||
574 | |||
575 | p = getenv ("PATH"); | ||
576 | if (p == NULL) | ||
577 | { | ||
578 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
579 | "tcp", | ||
580 | _("PATH environment variable is unset.\n")); | ||
581 | return NULL; | ||
582 | } | ||
583 | path = GNUNET_strdup (p); /* because we write on it */ | ||
584 | buf = GNUNET_malloc (strlen (path) + 20); | ||
585 | pos = path; | ||
586 | |||
587 | while (NULL != (end = strchr (pos, PATH_SEPARATOR))) | ||
588 | { | ||
589 | *end = '\0'; | ||
590 | sprintf (buf, "%s/%s", pos, binary); | ||
591 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) | ||
592 | { | ||
593 | GNUNET_free (path); | ||
594 | return buf; | ||
595 | } | ||
596 | pos = end + 1; | ||
597 | } | ||
598 | sprintf (buf, "%s/%s", pos, binary); | ||
599 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) | ||
600 | { | ||
601 | GNUNET_free (path); | ||
602 | return buf; | ||
603 | } | ||
604 | GNUNET_free (buf); | ||
605 | GNUNET_free (path); | ||
606 | return NULL; | ||
607 | } | ||
302 | 608 | ||
303 | h->upnp_status = status; | ||
304 | h->ext_addr_upnp = ext_addr; | ||
305 | 609 | ||
306 | h->processing--; | 610 | /** |
307 | pulse_cb (h); | 611 | * Check whether the suid bit is set on a file. |
612 | * Attempts to find the file using the current | ||
613 | * PATH environment variable as a search path. | ||
614 | * | ||
615 | * @param binary the name of the file to check | ||
616 | * @return GNUNET_YES if the file is SUID, | ||
617 | * GNUNET_NO if not, | ||
618 | * GNUNET_SYSERR on error | ||
619 | */ | ||
620 | static int | ||
621 | check_gnunet_nat_binary (const char *binary) | ||
622 | { | ||
623 | struct stat statbuf; | ||
624 | char *p; | ||
625 | #ifdef MINGW | ||
626 | SOCKET rawsock; | ||
627 | char *binaryexe; | ||
628 | |||
629 | GNUNET_asprintf (&binaryexe, "%s.exe", binary); | ||
630 | p = get_path_from_PATH (binaryexe); | ||
631 | free (binaryexe); | ||
632 | #else | ||
633 | p = get_path_from_PATH (binary); | ||
634 | #endif | ||
635 | if (p == NULL) | ||
636 | { | ||
637 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
638 | "tcp", | ||
639 | _("Could not find binary `%s' in PATH!\n"), | ||
640 | binary); | ||
641 | return GNUNET_NO; | ||
642 | } | ||
643 | if (0 != STAT (p, &statbuf)) | ||
644 | { | ||
645 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
646 | _("stat (%s) failed: %s\n"), | ||
647 | p, | ||
648 | STRERROR (errno)); | ||
649 | GNUNET_free (p); | ||
650 | return GNUNET_SYSERR; | ||
651 | } | ||
652 | GNUNET_free (p); | ||
653 | #ifndef MINGW | ||
654 | if ( (0 != (statbuf.st_mode & S_ISUID)) && | ||
655 | (statbuf.st_uid == 0) ) | ||
656 | return GNUNET_YES; | ||
657 | return GNUNET_NO; | ||
658 | #else | ||
659 | rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); | ||
660 | if (INVALID_SOCKET == rawsock) | ||
661 | { | ||
662 | DWORD err = GetLastError (); | ||
663 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
664 | "tcp", | ||
665 | "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err); | ||
666 | return GNUNET_NO; /* not running as administrator */ | ||
667 | } | ||
668 | closesocket (rawsock); | ||
669 | return GNUNET_YES; | ||
670 | #endif | ||
308 | } | 671 | } |
309 | 672 | ||
310 | #if 0 | 673 | |
674 | /** | ||
675 | * Task that restarts the gnunet-nat-server process after a crash | ||
676 | * after a certain delay. | ||
677 | * | ||
678 | * @param cls the 'struct GNUNET_NAT_Handle' | ||
679 | * @param tc scheduler context | ||
680 | */ | ||
311 | static void | 681 | static void |
312 | natpmp_pulse_cb (int status, struct sockaddr *ext_addr, void *cls) | 682 | restart_nat_server (void *cls, |
683 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
313 | { | 684 | { |
314 | struct GNUNET_NAT_Handle *h = cls; | 685 | struct GNUNET_NAT_Handle *h = cls; |
315 | 686 | ||
316 | h->natpmp_status = status; | 687 | h->server_read_task = GNUNET_SCHEDULER_NO_TASK; |
317 | h->ext_addr_natpmp = ext_addr; | 688 | if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) |
318 | 689 | return; | |
319 | h->processing--; | 690 | start_gnunet_nat_server (h); |
320 | pulse_cb (h); | ||
321 | } | 691 | } |
322 | #endif | ||
323 | 692 | ||
693 | |||
694 | /** | ||
695 | * We have been notified that gnunet-nat-server has written something to stdout. | ||
696 | * Handle the output, then reschedule this function to be called again once | ||
697 | * more is available. | ||
698 | * | ||
699 | * @param cls the NAT handle | ||
700 | * @param tc the scheduling context | ||
701 | */ | ||
324 | static void | 702 | static void |
325 | nat_pulse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 703 | nat_server_read (void *cls, |
704 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
326 | { | 705 | { |
327 | struct GNUNET_NAT_Handle *h = cls; | 706 | struct GNUNET_NAT_Handle *h = cls; |
328 | 707 | char mybuf[40]; | |
329 | /* Stop if we're already waiting for an action to complete */ | 708 | ssize_t bytes; |
330 | h->pulse_timer = GNUNET_SCHEDULER_NO_TASK; | 709 | size_t i; |
331 | if (h->processing) | 710 | int port; |
711 | const char *port_start; | ||
712 | struct sockaddr_in sin_addr; | ||
713 | |||
714 | h->server_read_task = GNUNET_SCHEDULER_NO_TASK; | ||
715 | if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
332 | return; | 716 | return; |
333 | h->old_status = get_traversal_status (h); | 717 | memset (mybuf, 0, sizeof(mybuf)); |
334 | 718 | bytes = GNUNET_DISK_file_read(h->server_stdout_handle, | |
335 | /* Only update the protocol that has been successful until now */ | 719 | mybuf, |
336 | if (h->upnp_status >= GNUNET_NAT_PORT_UNMAPPED) | 720 | sizeof(mybuf)); |
721 | if (bytes < 1) | ||
337 | { | 722 | { |
338 | h->processing = 1; | 723 | #if DEBUG_TCP_NAT |
339 | GNUNET_NAT_UPNP_pulse (h->upnp, h->is_enabled, GNUNET_YES); | 724 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
340 | 725 | "nat", | |
341 | /* Wait for the callback to call pulse_cb() to handle changes */ | 726 | "Finished reading from server stdout with code: %d\n", |
727 | bytes); | ||
728 | #endif | ||
729 | if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM)) | ||
730 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
731 | GNUNET_OS_process_wait (h->server_proc); | ||
732 | GNUNET_OS_process_close (h->server_proc); | ||
733 | h->server_proc = NULL; | ||
734 | GNUNET_DISK_pipe_close (h->server_stdout); | ||
735 | h->server_stdout = NULL; | ||
736 | h->server_stdout_handle = NULL; | ||
737 | /* now try to restart it */ | ||
738 | h->server_retry_delay = GNUNET_TIME_relative_multiply (h->server_retry_delay, 2); | ||
739 | h->server_retry_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS, | ||
740 | h->server_retry_delay); | ||
741 | h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay, | ||
742 | &restart_nat_server, | ||
743 | h); | ||
342 | return; | 744 | return; |
343 | } | 745 | } |
344 | else if (h->natpmp_status >= GNUNET_NAT_PORT_UNMAPPED) | 746 | |
747 | port_start = NULL; | ||
748 | for (i = 0; i < sizeof(mybuf); i++) | ||
345 | { | 749 | { |
346 | h->processing = 1; | 750 | if (mybuf[i] == '\n') |
347 | #if 0 | 751 | { |
348 | GNUNET_NAT_NATPMP_pulse (h->natpmp, h->is_enabled); | 752 | mybuf[i] = '\0'; |
349 | #endif | 753 | break; |
754 | } | ||
755 | if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) ) | ||
756 | { | ||
757 | mybuf[i] = '\0'; | ||
758 | port_start = &mybuf[i + 1]; | ||
759 | } | ||
350 | } | 760 | } |
351 | else /* try both */ | ||
352 | { | ||
353 | h->processing = 2; | ||
354 | 761 | ||
355 | GNUNET_NAT_UPNP_pulse (h->upnp, h->is_enabled, GNUNET_YES); | 762 | /* construct socket address of sender */ |
356 | #if 0 | 763 | memset (&sin_addr, 0, sizeof (sin_addr)); |
357 | GNUNET_NAT_NATPMP_pulse (h->natpmp, h->is_enabled, &natpmp_pulse_cb, h); | 764 | sin_addr.sin_family = AF_INET; |
765 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
766 | sin_addr.sin_len = sizeof (sin_addr); | ||
358 | #endif | 767 | #endif |
768 | if ( (NULL == port_start) || | ||
769 | (1 != sscanf (port_start, "%d", &port)) || | ||
770 | (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) ) | ||
771 | { | ||
772 | /* should we restart gnunet-nat-server? */ | ||
773 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
774 | "nat", | ||
775 | _("gnunet-nat-server generated malformed address `%s'\n"), | ||
776 | mybuf); | ||
777 | h->server_read_task | ||
778 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | ||
779 | h->server_stdout_handle, | ||
780 | &nat_server_read, | ||
781 | h); | ||
782 | return; | ||
359 | } | 783 | } |
784 | sin_addr.sin_port = htons((uint16_t) port); | ||
785 | #if DEBUG_NAT | ||
786 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
787 | "nat", | ||
788 | "gnunet-nat-server read: %s:%d\n", | ||
789 | mybuf, port); | ||
790 | #endif | ||
791 | h->reversal_callback (h->callback_cls, | ||
792 | (const struct sockaddr*) &sin_addr, | ||
793 | sizeof (sin_addr)); | ||
794 | h->server_read_task = | ||
795 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | ||
796 | h->server_stdout_handle, | ||
797 | &nat_server_read, | ||
798 | h); | ||
799 | } | ||
800 | |||
801 | |||
802 | /** | ||
803 | * Try to start the gnunet-nat-server (if it is not | ||
804 | * already running). | ||
805 | * | ||
806 | * @param h handle to NAT | ||
807 | */ | ||
808 | static void | ||
809 | start_gnunet_nat_server (struct GNUNET_NAT_Handle *h) | ||
810 | { | ||
811 | if ( (h->behind_nat == GNUNET_YES) && | ||
812 | (h->enable_nat_server == GNUNET_YES) && | ||
813 | (h->internal_address != NULL) && | ||
814 | (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, | ||
815 | GNUNET_NO, | ||
816 | GNUNET_YES))) ) | ||
817 | { | ||
818 | #if DEBUG_NAT | ||
819 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
820 | "nat" | ||
821 | "Starting %s at `%s'\n", | ||
822 | "gnunet-nat-server", | ||
823 | h->internal_address); | ||
824 | #endif | ||
825 | /* Start the server process */ | ||
826 | h->server_proc = GNUNET_OS_start_process (NULL, | ||
827 | h->server_stdout, | ||
828 | "gnunet-nat-server", | ||
829 | "gnunet-nat-server", | ||
830 | h->internal_address, | ||
831 | NULL); | ||
832 | if (h->server_proc == NULL) | ||
833 | { | ||
834 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
835 | "nat", | ||
836 | _("Failed to start %s\n"), | ||
837 | "gnunet-nat-server"); | ||
838 | GNUNET_DISK_pipe_close (h->server_stdout); | ||
839 | h->server_stdout = NULL; | ||
840 | } | ||
841 | else | ||
842 | { | ||
843 | /* Close the write end of the read pipe */ | ||
844 | GNUNET_DISK_pipe_close_end(h->server_stdout, | ||
845 | GNUNET_DISK_PIPE_END_WRITE); | ||
846 | h->server_stdout_handle | ||
847 | = GNUNET_DISK_pipe_handle (h->server_stdout, | ||
848 | GNUNET_DISK_PIPE_END_READ); | ||
849 | h->server_read_task | ||
850 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | ||
851 | h->server_stdout_handle, | ||
852 | &nat_server_read, | ||
853 | h); | ||
854 | } | ||
855 | } | ||
360 | } | 856 | } |
361 | 857 | ||
362 | 858 | ||
@@ -367,55 +863,165 @@ nat_pulse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
367 | * is taken from the corresponding sockaddr_in[6] field. | 863 | * is taken from the corresponding sockaddr_in[6] field. |
368 | * | 864 | * |
369 | * @param cfg configuration to use | 865 | * @param cfg configuration to use |
866 | * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP | ||
867 | * @param adv_port advertised port (port we are either bound to or that our OS | ||
868 | * locally performs redirection from to our bound port). | ||
869 | * @param num_addrs number of addresses in 'addrs' | ||
370 | * @param addr the local address packets should be redirected to | 870 | * @param addr the local address packets should be redirected to |
371 | * @param addrlen actual lenght of the address | 871 | * @param addrlen actual lenght of the address |
372 | * @param callback function to call everytime the public IP address changes | 872 | * @param address_callback function to call everytime the public IP address changes |
373 | * @param callback_cls closure for callback | 873 | * @param reversal_callback function to call if someone wants connection reversal from us |
874 | * @param callback_cls closure for callbacks | ||
374 | * @return NULL on error, otherwise handle that can be used to unregister | 875 | * @return NULL on error, otherwise handle that can be used to unregister |
375 | */ | 876 | */ |
376 | struct GNUNET_NAT_Handle * | 877 | struct GNUNET_NAT_Handle * |
377 | GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | 878 | GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, |
378 | const struct sockaddr *addr, | 879 | int is_tcp, |
379 | socklen_t addrlen, | 880 | uint16_t adv_port, |
380 | GNUNET_NAT_AddressCallback callback, void *callback_cls) | 881 | unsigned int num_addrs, |
882 | const struct sockaddr **addrs, | ||
883 | const socklen_t *addrlens, | ||
884 | GNUNET_NAT_AddressCallback address_callback, | ||
885 | GNUNET_NAT_ReversalCallback reversal_callback, | ||
886 | void *callback_cls) | ||
381 | { | 887 | { |
382 | struct GNUNET_NAT_Handle *h; | 888 | struct GNUNET_NAT_Handle *h; |
889 | struct in_addr in_addr; | ||
890 | unsigned int i; | ||
383 | 891 | ||
384 | h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle)); | 892 | h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle)); |
893 | h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS; | ||
894 | h->cfg = cfg; | ||
895 | h->is_tcp = is_tcp; | ||
896 | h->address_callback = address_callback; | ||
897 | h->reversal_callback = reversal_callback; | ||
898 | h->callback_cls = callback_cls; | ||
899 | h->num_local_addrs = num_addrs; | ||
900 | h->adv_port = adv_port; | ||
901 | if (num_addrs != 0) | ||
902 | { | ||
903 | h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr*)); | ||
904 | h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t)); | ||
905 | for (i=0;i<num_addrs;i++) | ||
906 | { | ||
907 | h->local_addrlens[i] = addrlens[i]; | ||
908 | h->local_addrs[i] = GNUNET_malloc (addrlens[i]); | ||
909 | memcpy (h->local_addrs[i], addrs[i], addrlens[i]); | ||
910 | } | ||
911 | } | ||
385 | 912 | ||
386 | if (addr) | 913 | if (GNUNET_OK == |
914 | GNUNET_CONFIGURATION_have_value (cfg, | ||
915 | "nat", | ||
916 | "INTERNAL_ADDRESS")) | ||
387 | { | 917 | { |
388 | GNUNET_assert ((addr->sa_family == AF_INET) || | 918 | (void) GNUNET_CONFIGURATION_get_value_string (cfg, |
389 | (addr->sa_family == AF_INET6)); | 919 | "nat", |
390 | h->local_addr = GNUNET_malloc (addrlen); | 920 | "INTERNAL_ADDRESS", |
391 | memcpy (h->local_addr, addr, addrlen); | 921 | &h->internal_address); |
392 | if (addr->sa_family == AF_INET) | 922 | } |
393 | { | 923 | if ( (h->internal_address != NULL) && |
394 | h->public_port = ntohs (((struct sockaddr_in *) addr)->sin_port); | 924 | (inet_pton(AF_INET, h->internal_address, &in_addr) != 1) ) |
395 | ((struct sockaddr_in *) h->local_addr)->sin_port = 0; | 925 | { |
396 | } | 926 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, |
397 | else if (addr->sa_family == AF_INET6) | 927 | "nat", |
398 | { | 928 | _("Malformed %s `%s' given in configuration!\n"), |
399 | h->public_port = ntohs (((struct sockaddr_in6 *) addr)->sin6_port); | 929 | "INTERNAL_ADDRESS", |
400 | ((struct sockaddr_in6 *) h->local_addr)->sin6_port = 0; | 930 | h->internal_address); |
401 | } | 931 | GNUNET_free (h->internal_address); |
932 | h->internal_address = NULL; | ||
933 | } | ||
934 | |||
935 | if (GNUNET_OK == | ||
936 | GNUNET_CONFIGURATION_have_value (cfg, | ||
937 | "nat", | ||
938 | "EXTERNAL_ADDRESS")) | ||
939 | { | ||
940 | (void) GNUNET_CONFIGURATION_get_value_string (cfg, | ||
941 | "nat", | ||
942 | "EXTERNAL_ADDRESS", | ||
943 | &h->external_address); | ||
944 | } | ||
945 | if ( (h->external_address != NULL) && | ||
946 | (inet_pton(AF_INET, h->external_address, &in_addr) != 1) ) | ||
947 | { | ||
948 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
949 | "nat", | ||
950 | _("Malformed %s `%s' given in configuration!\n"), | ||
951 | "EXTERNAL_ADDRESS", | ||
952 | h->external_address); | ||
953 | GNUNET_free (h->external_address); | ||
954 | h->external_address = NULL; | ||
955 | } | ||
956 | h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
957 | "nat", | ||
958 | "BEHIND_NAT"); | ||
959 | h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
960 | "nat", | ||
961 | "NAT_PUNCHED"); | ||
962 | h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
963 | "nat", | ||
964 | "ENABLE_NAT_CLIENT"); | ||
965 | h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
966 | "nat", | ||
967 | "ENABLE_NAT_SERVER"); | ||
968 | h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
969 | "nat", | ||
970 | "ENABLE_UPNP"); | ||
971 | h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
972 | "nat", | ||
973 | "USE_LOCALADDR"); | ||
974 | h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno(cfg, | ||
975 | "nat", | ||
976 | "DISABLEV6"); | ||
977 | if (NULL == reversal_callback) | ||
978 | h->enable_nat_server = GNUNET_NO; | ||
979 | |||
980 | /* Check if NAT was hole-punched */ | ||
981 | if ( (NULL != h->address_callback) && | ||
982 | (h->external_address != NULL) && | ||
983 | (h->nat_punched == GNUNET_YES) ) | ||
984 | { | ||
985 | h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address, | ||
986 | AF_INET, | ||
987 | GNUNET_TIME_UNIT_MINUTES, | ||
988 | &process_external_ip, | ||
989 | h); | ||
990 | h->enable_nat_server = GNUNET_NO; | ||
991 | h->enable_upnp = GNUNET_NO; | ||
992 | } | ||
993 | |||
994 | /* Test for SUID binaries */ | ||
995 | if ( (h->behind_nat == GNUNET_YES) && | ||
996 | (GNUNET_YES == h->enable_nat_server) && | ||
997 | (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) ) | ||
998 | { | ||
999 | h->enable_nat_server = GNUNET_NO; | ||
1000 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1001 | _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"), | ||
1002 | "gnunet-nat-server"); | ||
1003 | } | ||
1004 | if ( (GNUNET_YES == h->enable_nat_client) && | ||
1005 | (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) ) | ||
1006 | { | ||
1007 | h->enable_nat_client = GNUNET_NO; | ||
1008 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1009 | _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"), | ||
1010 | "gnunet-nat-client"); | ||
1011 | } | ||
1012 | |||
1013 | start_gnunet_nat_server (h); | ||
1014 | |||
1015 | /* FIXME: add support for UPnP, etc */ | ||
1016 | |||
1017 | if (NULL != h->address_callback) | ||
1018 | { | ||
1019 | GNUNET_OS_network_interfaces_list (&process_interfaces, h); | ||
1020 | h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, | ||
1021 | HOSTNAME_RESOLVE_TIMEOUT, | ||
1022 | &process_hostname_ip, | ||
1023 | h); | ||
402 | } | 1024 | } |
403 | h->should_change = GNUNET_YES; | ||
404 | h->is_enabled = GNUNET_YES; | ||
405 | h->upnp_status = GNUNET_NAT_PORT_UNMAPPED; | ||
406 | h->natpmp_status = GNUNET_NAT_PORT_UNMAPPED; | ||
407 | h->callback = callback; | ||
408 | h->callback_cls = callback_cls; | ||
409 | h->upnp = | ||
410 | GNUNET_NAT_UPNP_init (h->local_addr, addrlen, h->public_port, | ||
411 | &upnp_pulse_cb, h); | ||
412 | #if 0 | ||
413 | h->natpmp = | ||
414 | GNUNET_NAT_NATPMP_init (h->local_addr, addrlen, h->public_port, | ||
415 | &natpmp_pulse_cb, h); | ||
416 | #endif | ||
417 | h->pulse_timer = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
418 | &nat_pulse, h); | ||
419 | return h; | 1025 | return h; |
420 | } | 1026 | } |
421 | 1027 | ||
@@ -429,22 +1035,175 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
429 | void | 1035 | void |
430 | GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h) | 1036 | GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h) |
431 | { | 1037 | { |
432 | GNUNET_NAT_UPNP_pulse (h->upnp, GNUNET_NO, GNUNET_NO); | 1038 | unsigned int i; |
433 | GNUNET_NAT_UPNP_close (h->upnp); | 1039 | struct LocalAddressList *lal; |
434 | 1040 | ||
435 | #if 0 | 1041 | if (h->ext_dns != NULL) |
436 | GNUNET_NAT_NATPMP_pulse (h->natpmp, GNUNET_NO); | 1042 | { |
437 | GNUNET_NAT_NATPMP_close (h->natpmp); | 1043 | GNUNET_RESOLVER_request_cancel (h->ext_dns); |
1044 | h->ext_dns = NULL; | ||
1045 | } | ||
1046 | if (NULL != h->hostname_dns) | ||
1047 | { | ||
1048 | GNUNET_RESOLVER_request_cancel (h->hostname_dns); | ||
1049 | h->hostname_dns = NULL; | ||
1050 | } | ||
1051 | if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task) | ||
1052 | { | ||
1053 | GNUNET_SCHEDULER_cancel (h->server_read_task); | ||
1054 | h->server_read_task = GNUNET_SCHEDULER_NO_TASK; | ||
1055 | } | ||
1056 | if (NULL != h->server_proc) | ||
1057 | { | ||
1058 | if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM)) | ||
1059 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
1060 | GNUNET_OS_process_wait (h->server_proc); | ||
1061 | GNUNET_OS_process_close (h->server_proc); | ||
1062 | h->server_proc = NULL; | ||
1063 | GNUNET_DISK_pipe_close (h->server_stdout); | ||
1064 | h->server_stdout = NULL; | ||
1065 | h->server_stdout_handle = NULL; | ||
1066 | } | ||
1067 | if (NULL != h->server_stdout) | ||
1068 | { | ||
1069 | GNUNET_DISK_pipe_close (h->server_stdout); | ||
1070 | h->server_stdout = NULL; | ||
1071 | h->server_stdout_handle = NULL; | ||
1072 | } | ||
1073 | while (NULL != (lal = h->lal_head)) | ||
1074 | { | ||
1075 | GNUNET_CONTAINER_DLL_remove (h->lal_head, | ||
1076 | h->lal_tail, | ||
1077 | lal); | ||
1078 | h->address_callback (h->callback_cls, | ||
1079 | GNUNET_NO, | ||
1080 | (const struct sockaddr*) &lal[1], | ||
1081 | lal->addrlen); | ||
1082 | GNUNET_free (lal); | ||
1083 | } | ||
1084 | for (i=0;i<h->num_local_addrs;i++) | ||
1085 | GNUNET_free (h->local_addrs[i]); | ||
1086 | GNUNET_free_non_null (h->local_addrs); | ||
1087 | GNUNET_free_non_null (h->local_addrlens); | ||
1088 | GNUNET_free_non_null (h->external_address); | ||
1089 | GNUNET_free_non_null (h->internal_address); | ||
1090 | GNUNET_free (h); | ||
1091 | } | ||
1092 | |||
1093 | |||
1094 | /** | ||
1095 | * We learned about a peer (possibly behind NAT) so run the | ||
1096 | * gnunet-nat-client to send dummy ICMP responses to cause | ||
1097 | * that peer to connect to us (connection reversal). | ||
1098 | * | ||
1099 | * @param h NAT handle for us (largely used for configuration) | ||
1100 | * @param sa the address of the peer (IPv4-only) | ||
1101 | */ | ||
1102 | void | ||
1103 | GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h, | ||
1104 | const struct sockaddr_in *sa) | ||
1105 | { | ||
1106 | char inet4[INET_ADDRSTRLEN]; | ||
1107 | char port_as_string[6]; | ||
1108 | struct GNUNET_OS_Process *proc; | ||
1109 | |||
1110 | if (GNUNET_YES != h->enable_nat_client) | ||
1111 | return; /* not permitted / possible */ | ||
1112 | |||
1113 | if (h->internal_address == NULL) | ||
1114 | { | ||
1115 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
1116 | "nat", | ||
1117 | _("Internal IP address not known, cannot use ICMP NAT traversal method\n")); | ||
1118 | return; | ||
1119 | } | ||
1120 | GNUNET_assert (sa->sin_family == AF_INET); | ||
1121 | if (NULL == inet_ntop (AF_INET, | ||
1122 | &sa->sin_addr, | ||
1123 | inet4, INET_ADDRSTRLEN)) | ||
1124 | { | ||
1125 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); | ||
1126 | return; | ||
1127 | } | ||
1128 | GNUNET_snprintf(port_as_string, | ||
1129 | sizeof (port_as_string), | ||
1130 | "%d", | ||
1131 | h->adv_port); | ||
1132 | #if DEBUG_TCP_NAT | ||
1133 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
1134 | "nat", | ||
1135 | _("Running gnunet-nat-client %s %s %u\n"), | ||
1136 | h->internal_address, | ||
1137 | inet4, | ||
1138 | (unsigned int) h->adv_port); | ||
438 | #endif | 1139 | #endif |
1140 | proc = GNUNET_OS_start_process (NULL, | ||
1141 | NULL, | ||
1142 | "gnunet-nat-client", | ||
1143 | "gnunet-nat-client", | ||
1144 | h->internal_address, | ||
1145 | inet4, | ||
1146 | port_as_string, | ||
1147 | NULL); | ||
1148 | if (NULL == proc) | ||
1149 | return; | ||
1150 | /* we know that the gnunet-nat-client will terminate virtually | ||
1151 | instantly */ | ||
1152 | GNUNET_OS_process_wait (proc); | ||
1153 | GNUNET_OS_process_close (proc); | ||
1154 | } | ||
439 | 1155 | ||
440 | if (GNUNET_SCHEDULER_NO_TASK != h->pulse_timer) | ||
441 | GNUNET_SCHEDULER_cancel (h->pulse_timer); | ||
442 | 1156 | ||
443 | GNUNET_free_non_null (h->local_addr); | 1157 | /** |
444 | GNUNET_free_non_null (h->ext_addr); | 1158 | * Test if the given address is (currently) a plausible IP address for this peer. |
445 | GNUNET_free_non_null (h->ext_addr_upnp); | 1159 | * |
446 | GNUNET_free_non_null (h->ext_addr_natpmp); | 1160 | * @param h the handle returned by register |
447 | GNUNET_free (h); | 1161 | * @param addr IP address to test (IPv4 or IPv6) |
1162 | * @param addrlen number of bytes in addr | ||
1163 | * @return GNUNET_YES if the address is plausible, | ||
1164 | * GNUNET_NO if the address is not plausible, | ||
1165 | * GNUNET_SYSERR if the address is malformed | ||
1166 | */ | ||
1167 | int | ||
1168 | GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, | ||
1169 | const void *addr, | ||
1170 | socklen_t addrlen) | ||
1171 | { | ||
1172 | struct LocalAddressList *pos; | ||
1173 | const struct sockaddr_in *in4; | ||
1174 | const struct sockaddr_in6 *in6; | ||
1175 | |||
1176 | if ( (addrlen != sizeof (struct in_addr)) && | ||
1177 | (addrlen != sizeof (struct in6_addr)) ) | ||
1178 | { | ||
1179 | GNUNET_break (0); | ||
1180 | return GNUNET_SYSERR; | ||
1181 | } | ||
1182 | pos = h->lal_head; | ||
1183 | while (NULL != pos) | ||
1184 | { | ||
1185 | if (pos->addrlen == sizeof (struct sockaddr_in)) | ||
1186 | { | ||
1187 | in4 = (struct sockaddr_in* ) &pos[1]; | ||
1188 | if ( (addrlen == sizeof (struct in_addr)) && | ||
1189 | (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))) ) | ||
1190 | return GNUNET_YES; | ||
1191 | } | ||
1192 | else if (pos->addrlen == sizeof (struct sockaddr_in6)) | ||
1193 | { | ||
1194 | in6 = (struct sockaddr_in6* ) &pos[1]; | ||
1195 | if ( (addrlen == sizeof (struct in6_addr)) && | ||
1196 | (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))) ) | ||
1197 | return GNUNET_YES; | ||
1198 | } | ||
1199 | else | ||
1200 | { | ||
1201 | GNUNET_assert (0); | ||
1202 | } | ||
1203 | pos = pos->next; | ||
1204 | } | ||
1205 | return GNUNET_YES; | ||
448 | } | 1206 | } |
449 | 1207 | ||
1208 | |||
450 | /* end of nat.c */ | 1209 | /* end of nat.c */ |
diff --git a/src/nat/nat.h b/src/nat/nat.h deleted file mode 100644 index 3a0f21e7b..000000000 --- a/src/nat/nat.h +++ /dev/null | |||
@@ -1,72 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2007, 2008, 2009 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file nat/nat.h | ||
23 | * @brief Library handling UPnP and NAT-PMP port forwarding and | ||
24 | * external IP address retrieval | ||
25 | * @author Milan Bouchet-Valat | ||
26 | */ | ||
27 | |||
28 | #ifndef NAT_H | ||
29 | #define NAT_H | ||
30 | |||
31 | #define DEBUG GNUNET_YES | ||
32 | |||
33 | /** | ||
34 | * Used to communicate with the UPnP and NAT-PMP plugins | ||
35 | */ | ||
36 | enum GNUNET_NAT_PortState | ||
37 | { | ||
38 | GNUNET_NAT_PORT_ERROR, | ||
39 | |||
40 | /** | ||
41 | * the port isn't forwarded | ||
42 | */ | ||
43 | GNUNET_NAT_PORT_UNMAPPED, | ||
44 | |||
45 | /** | ||
46 | * we're cancelling the port forwarding | ||
47 | */ | ||
48 | GNUNET_NAT_PORT_UNMAPPING, | ||
49 | |||
50 | /** | ||
51 | * we're in the process of trying to set up port forwarding | ||
52 | */ | ||
53 | GNUNET_NAT_PORT_MAPPING, | ||
54 | |||
55 | /** | ||
56 | * we've successfully forwarded the port | ||
57 | */ | ||
58 | GNUNET_NAT_PORT_MAPPED | ||
59 | }; | ||
60 | |||
61 | |||
62 | /** | ||
63 | * Compare the sin(6)_addr fields of AF_INET or AF_INET(6) sockaddr. | ||
64 | * | ||
65 | * @param a first sockaddr | ||
66 | * @param b second sockaddr | ||
67 | * @return 0 if addresses are equal, non-null value otherwise | ||
68 | */ | ||
69 | int GNUNET_NAT_cmp_addr (const struct sockaddr *a, const struct sockaddr *b); | ||
70 | |||
71 | |||
72 | #endif | ||
diff --git a/src/nat/natpmp.c b/src/nat/natpmp.c deleted file mode 100644 index c6a5604d8..000000000 --- a/src/nat/natpmp.c +++ /dev/null | |||
@@ -1,325 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * This file has been adapted from the Transmission project: | ||
23 | * Originally licensed by the GPL version 2. | ||
24 | * Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com> | ||
25 | */ | ||
26 | |||
27 | /** | ||
28 | * @file nat/natpmp.c | ||
29 | * @brief NAT-PMP support for the NAT library | ||
30 | * | ||
31 | * @author Milan Bouchet-Valat | ||
32 | */ | ||
33 | #include <assert.h> | ||
34 | #include <errno.h> | ||
35 | #include <time.h> | ||
36 | #include <inttypes.h> | ||
37 | |||
38 | #define ENABLE_STRNATPMPERR | ||
39 | #include <libnatpmp/natpmp.h> | ||
40 | |||
41 | #include "platform.h" | ||
42 | #include "gnunet_common.h" | ||
43 | #include "gnunet_nat_lib.h" | ||
44 | #include "nat.h" | ||
45 | #include "natpmp.h" | ||
46 | |||
47 | #define LIFETIME_SECS 3600 | ||
48 | #define COMMAND_WAIT_SECS 8 | ||
49 | /* Component name for logging */ | ||
50 | #define COMP_NAT_NATPMP _("NAT (NAT-PMP))") | ||
51 | |||
52 | enum NATPMP_state | ||
53 | { | ||
54 | NATPMP_IDLE, | ||
55 | NATPMP_ERR, | ||
56 | NATPMP_DISCOVER, | ||
57 | NATPMP_RECV_PUB, | ||
58 | NATPMP_SEND_MAP, | ||
59 | NATPMP_RECV_MAP, | ||
60 | NATPMP_SEND_UNMAP, | ||
61 | NATPMP_RECV_UNMAP | ||
62 | } | ||
63 | ; | ||
64 | |||
65 | struct GNUNET_NAT_NATPMP_Handle | ||
66 | { | ||
67 | const struct sockaddr *addr; | ||
68 | socklen_t addrlen; | ||
69 | struct sockaddr *ext_addr; | ||
70 | int is_mapped; | ||
71 | int has_discovered; | ||
72 | int port; | ||
73 | time_t renew_time; | ||
74 | time_t command_time; | ||
75 | enum NATPMP_state state; | ||
76 | struct natpmp_t natpmp; | ||
77 | }; | ||
78 | |||
79 | |||
80 | static void | ||
81 | log_val (const char *func, int ret) | ||
82 | { | ||
83 | #ifdef DEBUG | ||
84 | if (ret == NATPMP_TRYAGAIN) | ||
85 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
86 | COMP_NAT_NATPMP, _("%s retry (%d)\n"), func, ret); | ||
87 | if (ret >= 0) | ||
88 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | ||
89 | COMP_NAT_NATPMP, _("%s succeeded (%d)\n"), func, ret); | ||
90 | else | ||
91 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
92 | COMP_NAT_NATPMP, | ||
93 | "%s failed. natpmp returned %d (%s); errno is %d (%s)\n", | ||
94 | func, ret, strnatpmperr (ret), errno, strerror (errno)); | ||
95 | #endif | ||
96 | } | ||
97 | |||
98 | struct GNUNET_NAT_NATPMP_Handle * | ||
99 | GNUNET_NAT_NATPMP_init (const struct sockaddr *addr, socklen_t addrlen, | ||
100 | u_short port) | ||
101 | { | ||
102 | struct GNUNET_NAT_NATPMP_Handle *nat; | ||
103 | |||
104 | nat = GNUNET_malloc (sizeof (struct GNUNET_NAT_NATPMP_Handle)); | ||
105 | nat->state = NATPMP_DISCOVER; | ||
106 | nat->port = port; | ||
107 | nat->addr = addr; | ||
108 | nat->addrlen = addrlen; | ||
109 | return nat; | ||
110 | } | ||
111 | |||
112 | void | ||
113 | GNUNET_NAT_NATPMP_close (struct GNUNET_NAT_NATPMP_Handle *nat) | ||
114 | { | ||
115 | if (nat) | ||
116 | { | ||
117 | closenatpmp (&nat->natpmp); | ||
118 | GNUNET_free (nat); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | static int | ||
123 | can_send_command (const struct GNUNET_NAT_NATPMP_Handle *nat) | ||
124 | { | ||
125 | return time (NULL) >= nat->command_time; | ||
126 | } | ||
127 | |||
128 | static void | ||
129 | set_command_time (struct GNUNET_NAT_NATPMP_Handle *nat) | ||
130 | { | ||
131 | nat->command_time = time (NULL) + COMMAND_WAIT_SECS; | ||
132 | } | ||
133 | |||
134 | int | ||
135 | GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled, | ||
136 | struct sockaddr **ext_addr) | ||
137 | { | ||
138 | #if DEBUG | ||
139 | char buf[INET6_ADDRSTRLEN]; | ||
140 | #endif | ||
141 | struct sockaddr_in *v4; | ||
142 | struct sockaddr_in6 *v6; | ||
143 | int ret; | ||
144 | |||
145 | /* Keep to NULL if address could not be found */ | ||
146 | *ext_addr = NULL; | ||
147 | |||
148 | if (is_enabled && (nat->state == NATPMP_DISCOVER)) | ||
149 | { | ||
150 | int val = initnatpmp (&nat->natpmp); | ||
151 | log_val ("initnatpmp", val); | ||
152 | val = sendpublicaddressrequest (&nat->natpmp); | ||
153 | log_val ("sendpublicaddressrequest", val); | ||
154 | nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_PUB; | ||
155 | nat->has_discovered = 1; | ||
156 | set_command_time (nat); | ||
157 | } | ||
158 | |||
159 | if ((nat->state == NATPMP_RECV_PUB) && can_send_command (nat)) | ||
160 | { | ||
161 | struct natpmpresp_t response; | ||
162 | const int val = readnatpmpresponseorretry (&nat->natpmp, | ||
163 | &response); | ||
164 | log_val ("readnatpmpresponseorretry", val); | ||
165 | if (val >= 0) | ||
166 | { | ||
167 | if (NULL != nat->ext_addr) | ||
168 | { | ||
169 | GNUNET_free (nat->ext_addr); | ||
170 | nat->ext_addr = NULL; | ||
171 | } | ||
172 | |||
173 | if (response.pnu.publicaddress.family == AF_INET) | ||
174 | { | ||
175 | v4 = GNUNET_malloc (sizeof (struct sockaddr_in)); | ||
176 | nat->ext_addr = (struct sockaddr *) v4; | ||
177 | v4->sin_family = AF_INET; | ||
178 | v4->sin_port = response.pnu.newportmapping.mappedpublicport; | ||
179 | memcpy (&v4->sin_addr, &response.pnu.publicaddress.addr, | ||
180 | sizeof (struct in_addr)); | ||
181 | #ifdef DEBUG | ||
182 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP, | ||
183 | _("Found public IP address %s\n"), | ||
184 | inet_ntop (AF_INET, | ||
185 | &response.pnu.publicaddress.addr, | ||
186 | buf, sizeof (buf))); | ||
187 | #endif | ||
188 | } | ||
189 | else | ||
190 | { | ||
191 | v6 = GNUNET_malloc (sizeof (struct sockaddr_in6)); | ||
192 | nat->ext_addr = (struct sockaddr *) v6; | ||
193 | v6->sin6_family = AF_INET6; | ||
194 | v6->sin6_port = response.pnu.newportmapping.mappedpublicport; | ||
195 | memcpy (&v6->sin6_addr, | ||
196 | &response.pnu.publicaddress.addr6, | ||
197 | (sizeof (struct in6_addr))); | ||
198 | #ifdef DEBUG | ||
199 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP, | ||
200 | _("Found public IP address %s\n"), | ||
201 | inet_ntop (AF_INET6, | ||
202 | &response.pnu.publicaddress.addr6, | ||
203 | buf, sizeof (buf))); | ||
204 | #endif | ||
205 | } | ||
206 | *ext_addr = nat->ext_addr; | ||
207 | nat->state = NATPMP_IDLE; | ||
208 | } | ||
209 | else if (val != NATPMP_TRYAGAIN) | ||
210 | { | ||
211 | nat->state = NATPMP_ERR; | ||
212 | } | ||
213 | } | ||
214 | |||
215 | if ((nat->state == NATPMP_IDLE) || (nat->state == NATPMP_ERR)) | ||
216 | { | ||
217 | if (nat->is_mapped && !is_enabled) | ||
218 | nat->state = NATPMP_SEND_UNMAP; | ||
219 | } | ||
220 | |||
221 | if ((nat->state == NATPMP_SEND_UNMAP) && can_send_command (nat)) | ||
222 | { | ||
223 | const int val = | ||
224 | sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP, | ||
225 | nat->port, nat->port, | ||
226 | 0); | ||
227 | log_val ("sendnewportmappingrequest", val); | ||
228 | nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_UNMAP; | ||
229 | set_command_time (nat); | ||
230 | } | ||
231 | |||
232 | if (nat->state == NATPMP_RECV_UNMAP) | ||
233 | { | ||
234 | struct natpmpresp_t resp; | ||
235 | const int val = readnatpmpresponseorretry (&nat->natpmp, &resp); | ||
236 | log_val ("readnatpmpresponseorretry", val); | ||
237 | if (val >= 0) | ||
238 | { | ||
239 | const int p = resp.pnu.newportmapping.privateport; | ||
240 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP, | ||
241 | _("No longer forwarding port %d\n"), p); | ||
242 | if (nat->port == p) | ||
243 | { | ||
244 | nat->port = -1; | ||
245 | nat->state = NATPMP_IDLE; | ||
246 | nat->is_mapped = 0; | ||
247 | } | ||
248 | } | ||
249 | else if (val != NATPMP_TRYAGAIN) | ||
250 | { | ||
251 | nat->state = NATPMP_ERR; | ||
252 | } | ||
253 | } | ||
254 | |||
255 | if (nat->state == NATPMP_IDLE) | ||
256 | { | ||
257 | if (is_enabled && !nat->is_mapped && nat->has_discovered) | ||
258 | nat->state = NATPMP_SEND_MAP; | ||
259 | |||
260 | else if (nat->is_mapped && time (NULL) >= nat->renew_time) | ||
261 | nat->state = NATPMP_SEND_MAP; | ||
262 | } | ||
263 | |||
264 | if ((nat->state == NATPMP_SEND_MAP) && can_send_command (nat)) | ||
265 | { | ||
266 | const int val = | ||
267 | sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP, | ||
268 | nat->port, | ||
269 | nat->port, | ||
270 | LIFETIME_SECS); | ||
271 | log_val ("sendnewportmappingrequest", val); | ||
272 | nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_MAP; | ||
273 | set_command_time (nat); | ||
274 | } | ||
275 | |||
276 | if (nat->state == NATPMP_RECV_MAP) | ||
277 | { | ||
278 | struct natpmpresp_t resp; | ||
279 | const int val = readnatpmpresponseorretry (&nat->natpmp, &resp); | ||
280 | log_val ("readnatpmpresponseorretry", val); | ||
281 | if (val >= 0) | ||
282 | { | ||
283 | nat->state = NATPMP_IDLE; | ||
284 | nat->is_mapped = 1; | ||
285 | nat->renew_time = time (NULL) + LIFETIME_SECS; | ||
286 | nat->port = resp.pnu.newportmapping.privateport; | ||
287 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP, | ||
288 | _("Port %d forwarded successfully\n"), nat->port); | ||
289 | } | ||
290 | else if (val != NATPMP_TRYAGAIN) | ||
291 | { | ||
292 | nat->state = NATPMP_ERR; | ||
293 | } | ||
294 | } | ||
295 | |||
296 | switch (nat->state) | ||
297 | { | ||
298 | case NATPMP_IDLE: | ||
299 | ret = | ||
300 | nat->is_mapped ? GNUNET_NAT_PORT_MAPPED : GNUNET_NAT_PORT_UNMAPPED; | ||
301 | break; | ||
302 | |||
303 | case NATPMP_DISCOVER: | ||
304 | ret = GNUNET_NAT_PORT_UNMAPPED; | ||
305 | break; | ||
306 | |||
307 | case NATPMP_RECV_PUB: | ||
308 | case NATPMP_SEND_MAP: | ||
309 | case NATPMP_RECV_MAP: | ||
310 | ret = GNUNET_NAT_PORT_MAPPING; | ||
311 | break; | ||
312 | |||
313 | case NATPMP_SEND_UNMAP: | ||
314 | case NATPMP_RECV_UNMAP: | ||
315 | ret = GNUNET_NAT_PORT_UNMAPPING; | ||
316 | break; | ||
317 | |||
318 | default: | ||
319 | ret = GNUNET_NAT_PORT_ERROR; | ||
320 | break; | ||
321 | } | ||
322 | return ret; | ||
323 | } | ||
324 | |||
325 | /* end of natpmp.c */ | ||
diff --git a/src/nat/natpmp.h b/src/nat/natpmp.h deleted file mode 100644 index 56d5f0034..000000000 --- a/src/nat/natpmp.h +++ /dev/null | |||
@@ -1,46 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file nat/natpmp.h | ||
23 | * @brief NAT-PMP support for the NAT library | ||
24 | * | ||
25 | * @author Milan Bouchet-Valat | ||
26 | */ | ||
27 | |||
28 | #ifndef NATPMP_H | ||
29 | #define NATPMP_H 1 | ||
30 | |||
31 | #include "platform.h" | ||
32 | |||
33 | struct GNUNET_NAT_NATPMP_Handle; | ||
34 | |||
35 | struct GNUNET_NAT_NATPMP_Handle *GNUNET_NAT_NATPMP_init (const struct sockaddr | ||
36 | *addr, | ||
37 | socklen_t addrlen, | ||
38 | unsigned short port); | ||
39 | |||
40 | void GNUNET_NAT_NATPMP_close (struct GNUNET_NAT_NATPMP_Handle *nat); | ||
41 | |||
42 | int GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, | ||
43 | int is_enabled, struct sockaddr **ext_addr); | ||
44 | |||
45 | #endif | ||
46 | /* NATPMP_H */ | ||
diff --git a/src/nat/test-nat.conf b/src/nat/test-nat.conf deleted file mode 100644 index 311eb3c15..000000000 --- a/src/nat/test-nat.conf +++ /dev/null | |||
@@ -1,355 +0,0 @@ | |||
1 | [PATHS] | ||
2 | SERVICEHOME = /tmp/nat-test | ||
3 | # SERVICEHOME = /var/lib/gnunet/ | ||
4 | # DEFAULTCONFIG = /etc/gnunet.conf | ||
5 | # If 'DEFAULTCONFIG' is not defined, the current | ||
6 | # configuration file is assumed to be the default, | ||
7 | # which is what we want by default... | ||
8 | |||
9 | [gnunetd] | ||
10 | HOSTKEY = $SERVICEHOME/.hostkey | ||
11 | |||
12 | [TESTING] | ||
13 | WEAKRANDOM = NO | ||
14 | |||
15 | [client] | ||
16 | HOME = $SERVICEHOME | ||
17 | |||
18 | [transport-tcp] | ||
19 | # Use 0 to ONLY advertise as a peer behind NAT (no port binding) | ||
20 | PORT = 2086 | ||
21 | ADVERTISED_PORT = 2086 | ||
22 | |||
23 | # Are we behind NAT? | ||
24 | BEHIND_NAT = YES | ||
25 | |||
26 | # Is the NAT hole-punched? | ||
27 | PUNCHED_NAT = NO | ||
28 | |||
29 | # Disable UPNP by default until it gets cleaner! | ||
30 | ENABLE_UPNP = NO | ||
31 | |||
32 | # External IP address of the NAT box (if known); IPv4 dotted-decimal ONLY at this time (should allow DynDNS!) | ||
33 | # normal interface IP address for non-NATed peers; | ||
34 | # possibly auto-detected (using UPnP) if possible if not specified | ||
35 | # EXTERNAL_ADDRESS = | ||
36 | |||
37 | # Should we use ICMP-based NAT traversal to try connect to NATed peers | ||
38 | # or, if we are behind NAT, to allow connections to us? | ||
39 | ENABLE_ICMP_CLIENT = YES | ||
40 | ENABLE_ICMP_SERVER = YES | ||
41 | |||
42 | # IP address of the interface connected to the NAT box; IPv4 dotted-decimal ONLY; | ||
43 | # normal interface IP address for non-NATed peers; | ||
44 | # likely auto-detected (via interface list) if not specified (!) | ||
45 | # INTERNAL_ADDRESS = | ||
46 | |||
47 | # Only bind to a specific IP address; if both are given, this should be idential to INTERNAL_ADDRESS | ||
48 | # (but use different: this one restricts our bind!) | ||
49 | # BINDTO = | ||
50 | |||
51 | TIMEOUT = 300000 | ||
52 | |||
53 | # Disable IPv6 support | ||
54 | # DISABLEV6 = | ||
55 | # ACCEPT_FROM = | ||
56 | # ACCEPT_FROM6 = | ||
57 | # REJECT_FROM = | ||
58 | # REJECT_FROM6 = | ||
59 | |||
60 | [transport-udp] | ||
61 | PORT = 2086 | ||
62 | # BEHIND_NAT = | ||
63 | # BINDTO = | ||
64 | # ALLOW_NAT = | ||
65 | # ONLY_NAT_ADDRESSES = | ||
66 | # INTERNAL_ADDRESS = | ||
67 | # EXTERNAL_ADDRESS = | ||
68 | |||
69 | [transport-http] | ||
70 | PORT = 1080 | ||
71 | |||
72 | [transport-https] | ||
73 | PORT = 4433 | ||
74 | |||
75 | [arm] | ||
76 | PORT = 2087 | ||
77 | HOSTNAME = localhost | ||
78 | HOME = $SERVICEHOME | ||
79 | CONFIG = $DEFAULTCONFIG | ||
80 | BINARY = gnunet-service-arm | ||
81 | ACCEPT_FROM = 127.0.0.1; | ||
82 | ACCEPT_FROM6 = ::1; | ||
83 | DEFAULTSERVICES = topology hostlist | ||
84 | UNIXPATH = /tmp/gnunet-service-arm.sock | ||
85 | UNIX_MATCH_UID = YES | ||
86 | UNIX_MATCH_GID = YES | ||
87 | # GLOBAL_POSTFIX = -l $SERVICEHOME/{}-logs | ||
88 | # GLOBAL_PREFIX = | ||
89 | # USERNAME = | ||
90 | # MAXBUF = | ||
91 | # TIMEOUT = | ||
92 | # DISABLEV6 = | ||
93 | # BINDTO = | ||
94 | # REJECT_FROM = | ||
95 | # REJECT_FROM6 = | ||
96 | # PREFIX = | ||
97 | |||
98 | [statistics] | ||
99 | AUTOSTART = YES | ||
100 | PORT = 2088 | ||
101 | HOSTNAME = localhost | ||
102 | HOME = $SERVICEHOME | ||
103 | CONFIG = $DEFAULTCONFIG | ||
104 | BINARY = gnunet-service-statistics | ||
105 | ACCEPT_FROM = 127.0.0.1; | ||
106 | ACCEPT_FROM6 = ::1; | ||
107 | UNIXPATH = /tmp/gnunet-service-statistics.sock | ||
108 | UNIX_MATCH_UID = NO | ||
109 | UNIX_MATCH_GID = YES | ||
110 | # DISABLE_SOCKET_FORWARDING = NO | ||
111 | # USERNAME = | ||
112 | # MAXBUF = | ||
113 | # TIMEOUT = | ||
114 | # DISABLEV6 = | ||
115 | # BINDTO = | ||
116 | # REJECT_FROM = | ||
117 | # REJECT_FROM6 = | ||
118 | # PREFIX = | ||
119 | |||
120 | [resolver] | ||
121 | AUTOSTART = YES | ||
122 | PORT = 2089 | ||
123 | HOSTNAME = localhost | ||
124 | HOME = $SERVICEHOME | ||
125 | CONFIG = $DEFAULTCONFIG | ||
126 | BINARY = gnunet-service-resolver | ||
127 | ACCEPT_FROM = 127.0.0.1; | ||
128 | ACCEPT_FROM6 = ::1; | ||
129 | UNIXPATH = /tmp/gnunet-service-resolver.sock | ||
130 | UNIX_MATCH_UID = NO | ||
131 | UNIX_MATCH_GID = NO | ||
132 | # DISABLE_SOCKET_FORWARDING = NO | ||
133 | # USERNAME = | ||
134 | # MAXBUF = | ||
135 | # TIMEOUT = | ||
136 | # DISABLEV6 = | ||
137 | # BINDTO = | ||
138 | # REJECT_FROM = | ||
139 | # REJECT_FROM6 = | ||
140 | # PREFIX = | ||
141 | |||
142 | [peerinfo] | ||
143 | AUTOSTART = YES | ||
144 | PORT = 2090 | ||
145 | HOSTNAME = localhost | ||
146 | HOME = $SERVICEHOME | ||
147 | CONFIG = $DEFAULTCONFIG | ||
148 | BINARY = gnunet-service-peerinfo | ||
149 | ACCEPT_FROM = 127.0.0.1; | ||
150 | ACCEPT_FROM6 = ::1; | ||
151 | UNIXPATH = /tmp/gnunet-service-peerinfo.sock | ||
152 | UNIX_MATCH_UID = NO | ||
153 | UNIX_MATCH_GID = YES | ||
154 | # DISABLE_SOCKET_FORWARDING = NO | ||
155 | # USERNAME = | ||
156 | # MAXBUF = | ||
157 | # TIMEOUT = | ||
158 | # DISABLEV6 = | ||
159 | # BINDTO = | ||
160 | # REJECT_FROM = | ||
161 | # REJECT_FROM6 = | ||
162 | # PREFIX = | ||
163 | HOSTS = $SERVICEHOME/data/hosts/ | ||
164 | |||
165 | |||
166 | [transport] | ||
167 | AUTOSTART = YES | ||
168 | PORT = 2091 | ||
169 | HOSTNAME = localhost | ||
170 | HOME = $SERVICEHOME | ||
171 | CONFIG = $DEFAULTCONFIG | ||
172 | BINARY = gnunet-service-transport | ||
173 | NEIGHBOUR_LIMIT = 50 | ||
174 | ACCEPT_FROM = 127.0.0.1; | ||
175 | ACCEPT_FROM6 = ::1; | ||
176 | PLUGINS = tcp | ||
177 | UNIXPATH = /tmp/gnunet-service-transport.sock | ||
178 | BLACKLIST_FILE = $SERVICEHOME/blacklist | ||
179 | UNIX_MATCH_UID = YES | ||
180 | UNIX_MATCH_GID = YES | ||
181 | # DISABLE_SOCKET_FORWARDING = NO | ||
182 | # USERNAME = | ||
183 | # MAXBUF = | ||
184 | # TIMEOUT = | ||
185 | # DISABLEV6 = | ||
186 | # BINDTO = | ||
187 | # REJECT_FROM = | ||
188 | # REJECT_FROM6 = | ||
189 | # PREFIX = | ||
190 | |||
191 | [core] | ||
192 | AUTOSTART = YES | ||
193 | PORT = 2092 | ||
194 | HOSTNAME = localhost | ||
195 | HOME = $SERVICEHOME | ||
196 | CONFIG = $DEFAULTCONFIG | ||
197 | BINARY = gnunet-service-core | ||
198 | ACCEPT_FROM = 127.0.0.1; | ||
199 | ACCEPT_FROM6 = ::1; | ||
200 | # quotas are in bytes per second now! | ||
201 | TOTAL_QUOTA_IN = 65536 | ||
202 | TOTAL_QUOTA_OUT = 65536 | ||
203 | UNIXPATH = /tmp/gnunet-service-core.sock | ||
204 | UNIX_MATCH_UID = YES | ||
205 | UNIX_MATCH_GID = YES | ||
206 | # DISABLE_SOCKET_FORWARDING = NO | ||
207 | # DEBUG = YES | ||
208 | # USERNAME = | ||
209 | # MAXBUF = | ||
210 | # TIMEOUT = | ||
211 | # DISABLEV6 = | ||
212 | # BINDTO = | ||
213 | # REJECT_FROM = | ||
214 | # REJECT_FROM6 = | ||
215 | # PREFIX = | ||
216 | |||
217 | |||
218 | [topology] | ||
219 | MINIMUM-FRIENDS = 0 | ||
220 | FRIENDS-ONLY = NO | ||
221 | AUTOCONNECT = YES | ||
222 | TARGET-CONNECTION-COUNT = 16 | ||
223 | FRIENDS = $SERVICEHOME/friends | ||
224 | CONFIG = $DEFAULTCONFIG | ||
225 | BINARY = gnunet-daemon-topology | ||
226 | |||
227 | [hostlist] | ||
228 | # port for hostlist http server | ||
229 | HTTPPORT = 8080 | ||
230 | HOME = $SERVICEHOME | ||
231 | HOSTLISTFILE = $SERVICEHOME/hostlist/learned.data | ||
232 | CONFIG = $DEFAULTCONFIG | ||
233 | BINARY = gnunet-daemon-hostlist | ||
234 | # consider having "-e" as default as well once implemented | ||
235 | OPTIONS = -b | ||
236 | SERVERS = http://v9.gnunet.org:58080/ | ||
237 | # proxy for downloading hostlists | ||
238 | HTTP-PROXY = | ||
239 | |||
240 | |||
241 | [datastore] | ||
242 | AUTOSTART = YES | ||
243 | UNIXPATH = /tmp/gnunet-service-datastore.sock | ||
244 | UNIX_MATCH_UID = YES | ||
245 | UNIX_MATCH_GID = YES | ||
246 | PORT = 2093 | ||
247 | HOSTNAME = localhost | ||
248 | HOME = $SERVICEHOME | ||
249 | CONFIG = $DEFAULTCONFIG | ||
250 | BINARY = gnunet-service-datastore | ||
251 | ACCEPT_FROM = 127.0.0.1; | ||
252 | ACCEPT_FROM6 = ::1; | ||
253 | QUOTA = 100000000 | ||
254 | BLOOMFILTER = $SERVICEHOME/fs/bloomfilter | ||
255 | DATABASE = sqlite | ||
256 | # DISABLE_SOCKET_FORWARDING = NO | ||
257 | |||
258 | [datastore-sqlite] | ||
259 | FILENAME = $SERVICEHOME/datastore/sqlite.db | ||
260 | |||
261 | [datastore-postgres] | ||
262 | CONFIG = connect_timeout=10; dbname=gnunet | ||
263 | |||
264 | [datastore-mysql] | ||
265 | DATABASE = gnunet | ||
266 | CONFIG = ~/.my.cnf | ||
267 | # USER = gnunet | ||
268 | # PASSWORD = | ||
269 | # HOST = localhost | ||
270 | # PORT = 3306 | ||
271 | |||
272 | [datacache-mysql] | ||
273 | DATABASE = gnunetcheck | ||
274 | CONFIG = ~/.my.cnf | ||
275 | # USER = gnunet | ||
276 | # PASSWORD = | ||
277 | # HOST = localhost | ||
278 | # PORT = 3306 | ||
279 | |||
280 | |||
281 | [fs] | ||
282 | AUTOSTART = YES | ||
283 | INDEXDB = $SERVICEHOME/idxinfo.lst | ||
284 | TRUST = $SERVICEHOME/data/credit/ | ||
285 | IDENTITY_DIR = $SERVICEHOME/identities/ | ||
286 | STATE_DIR = $SERVICEHOME/persistence/ | ||
287 | UPDATE_DIR = $SERVICEHOME/updates/ | ||
288 | PORT = 2094 | ||
289 | HOSTNAME = localhost | ||
290 | HOME = $SERVICEHOME | ||
291 | CONFIG = $DEFAULTCONFIG | ||
292 | BINARY = gnunet-service-fs | ||
293 | ACCEPT_FROM = 127.0.0.1; | ||
294 | ACCEPT_FROM6 = ::1; | ||
295 | |||
296 | CONTENT_CACHING = YES | ||
297 | CONTENT_PUSHING = YES | ||
298 | |||
299 | UNIXPATH = /tmp/gnunet-service-fs.sock | ||
300 | UNIX_MATCH_UID = NO | ||
301 | UNIX_MATCH_GID = YES | ||
302 | # DISABLE_SOCKET_FORWARDING = NO | ||
303 | # DEBUG = YES | ||
304 | MAX_PENDING_REQUESTS = 65536 | ||
305 | # Maximum frequency we're allowed to poll the datastore | ||
306 | # for content for migration (can be used to reduce | ||
307 | # GNUnet's disk-IO rate) | ||
308 | MIN_MIGRATION_DELAY = 1000 | ||
309 | EXPECTED_NEIGHBOUR_COUNT = 128 | ||
310 | |||
311 | [dht] | ||
312 | AUTOSTART = YES | ||
313 | PORT = 2095 | ||
314 | HOSTNAME = localhost | ||
315 | HOME = $SERVICEHOME | ||
316 | CONFIG = $DEFAULTCONFIG | ||
317 | BINARY = gnunet-service-dht | ||
318 | ACCEPT_FROM = 127.0.0.1; | ||
319 | ACCEPT_FROM6 = ::1; | ||
320 | BUCKET_SIZE = 4 | ||
321 | UNIXPATH = /tmp/gnunet-service-dht.sock | ||
322 | UNIX_MATCH_UID = YES | ||
323 | UNIX_MATCH_GID = YES | ||
324 | # DISABLE_SOCKET_FORWARDING = NO | ||
325 | # DEBUG = YES | ||
326 | # USERNAME = | ||
327 | # MAXBUF = | ||
328 | # TIMEOUT = | ||
329 | # DISABLEV6 = | ||
330 | # BINDTO = | ||
331 | # REJECT_FROM = | ||
332 | # REJECT_FROM6 = | ||
333 | # PREFIX = | ||
334 | # DO_FIND_PEER = | ||
335 | # STRICT_KADEMLIA = | ||
336 | # USE_MAX_HOPS = | ||
337 | # MAX_HOPS = | ||
338 | # REPUBLISH = YES | ||
339 | # REPLICATION_FREQUENCY = 60 | ||
340 | # STOP_ON_CLOSEST = | ||
341 | # STOP_FOUND = | ||
342 | # CONVERGE_MODIFIER = | ||
343 | |||
344 | |||
345 | [dhtcache] | ||
346 | DATABASE = sqlite | ||
347 | QUOTA = 1024000 | ||
348 | |||
349 | [block] | ||
350 | PLUGINS = fs dht test | ||
351 | |||
352 | [dns] | ||
353 | AUTOSTART = NO | ||
354 | |||
355 | |||
diff --git a/src/nat/test_nat.c b/src/nat/test_nat.c index d1268c93a..72cff0915 100644 --- a/src/nat/test_nat.c +++ b/src/nat/test_nat.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2009 Christian Grothoff (and other contributing authors) | 3 | (C) 2009, 2011 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -19,20 +19,20 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file nat/test_nat.c | ||
23 | * @brief Testcase for NAT library | ||
24 | * @author Milan Bouchet-Valat | ||
25 | */ | ||
26 | |||
27 | /** | ||
28 | * Testcase for port redirection and public IP address retrieval. | 22 | * Testcase for port redirection and public IP address retrieval. |
29 | * This test never fails, because there need to be a NAT box set up for that. | 23 | * This test never fails, because there need to be a NAT box set up for that. |
30 | * So we only get IP address and open the 2086 port using any UPnP and NAT-PMP | 24 | * So we only get IP address and open the 2086 port using any NAT traversal |
31 | * routers found, wait for 30s, close ports and return. | 25 | * method available, wait for 30s, close ports and return. |
32 | * Have a look at the logs and use NMAP to check that it works with your box. | 26 | * Have a look at the logs and use NMAP to check that it works with your box. |
27 | * | ||
28 | * @file nat/test_nat.c | ||
29 | * @brief Testcase for NAT library | ||
30 | * @author Milan Bouchet-Valat | ||
31 | * @author Christian Grothoff | ||
32 | * | ||
33 | * TODO: actually use ARM to start resolver service to make DNS work! | ||
33 | */ | 34 | */ |
34 | 35 | ||
35 | |||
36 | #include "platform.h" | 36 | #include "platform.h" |
37 | #include "gnunet_common.h" | 37 | #include "gnunet_common.h" |
38 | #include "gnunet_util_lib.h" | 38 | #include "gnunet_util_lib.h" |
@@ -40,35 +40,62 @@ | |||
40 | #include "gnunet_scheduler_lib.h" | 40 | #include "gnunet_scheduler_lib.h" |
41 | #include "gnunet_nat_lib.h" | 41 | #include "gnunet_nat_lib.h" |
42 | 42 | ||
43 | |||
44 | #define VERBOSE GNUNET_YES | ||
45 | |||
46 | |||
43 | /* Time to wait before stopping NAT, in seconds */ | 47 | /* Time to wait before stopping NAT, in seconds */ |
44 | #define TIMEOUT 60 | 48 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) |
45 | 49 | ||
46 | struct addr_cls | ||
47 | { | ||
48 | struct sockaddr *addr; | ||
49 | socklen_t addrlen; | ||
50 | }; | ||
51 | 50 | ||
51 | /** | ||
52 | * Function called on each address that the NAT service | ||
53 | * believes to be valid for the transport. | ||
54 | */ | ||
52 | static void | 55 | static void |
53 | addr_callback (void *cls, int add_remove, | 56 | addr_callback (void *cls, int add_remove, |
54 | const struct sockaddr *addr, socklen_t addrlen) | 57 | const struct sockaddr *addr, socklen_t addrlen) |
55 | { | 58 | { |
56 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "External address changed: %s %s\n", | 59 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
60 | "Address changed: %s `%s' (%u bytes)\n", | ||
57 | add_remove == GNUNET_YES ? "added" : "removed", | 61 | add_remove == GNUNET_YES ? "added" : "removed", |
58 | GNUNET_a2s (addr, addrlen)); | 62 | GNUNET_a2s (addr, addrlen), |
63 | (unsigned int) addrlen); | ||
59 | } | 64 | } |
60 | 65 | ||
66 | |||
67 | /** | ||
68 | * Function that terminates the test. | ||
69 | */ | ||
61 | static void | 70 | static void |
62 | stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 71 | stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
63 | { | 72 | { |
64 | struct GNUNET_NAT_Handle *nat = cls; | 73 | struct GNUNET_NAT_Handle *nat = cls; |
65 | 74 | ||
66 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); | 75 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
76 | "Stopping NAT and quitting...\n"); | ||
67 | GNUNET_NAT_unregister (nat); | 77 | GNUNET_NAT_unregister (nat); |
68 | } | 78 | } |
69 | 79 | ||
70 | /* Return the address of the default interface, | 80 | |
71 | * or any interface with a valid address if the default is not valid */ | 81 | struct addr_cls |
82 | { | ||
83 | struct sockaddr *addr; | ||
84 | socklen_t addrlen; | ||
85 | }; | ||
86 | |||
87 | |||
88 | /** | ||
89 | * Return the address of the default interface, | ||
90 | * or any interface with a valid address if the default is not valid | ||
91 | * | ||
92 | * @param cls the 'struct addr_cls' | ||
93 | * @param name name of the interface | ||
94 | * @param isDefault do we think this may be our default interface | ||
95 | * @param addr address of the interface | ||
96 | * @param addrlen number of bytes in addr | ||
97 | * @return GNUNET_OK to continue iterating | ||
98 | */ | ||
72 | static int | 99 | static int |
73 | process_if (void *cls, | 100 | process_if (void *cls, |
74 | const char *name, | 101 | const char *name, |
@@ -76,18 +103,21 @@ process_if (void *cls, | |||
76 | { | 103 | { |
77 | struct addr_cls *data = cls; | 104 | struct addr_cls *data = cls; |
78 | 105 | ||
79 | if (addr && addrlen > 0) | 106 | if (addr == NULL) |
80 | { | 107 | return GNUNET_OK; |
81 | if (data->addr) | 108 | GNUNET_free_non_null (data->addr); |
82 | GNUNET_free (data->addr); | 109 | data->addr = GNUNET_malloc (addrlen); |
83 | data->addr = memcpy (GNUNET_malloc (addrlen), addr, addrlen); | 110 | memcpy (data->addr, addr, addrlen); |
84 | data->addrlen = addrlen; | 111 | data->addrlen = addrlen; |
85 | if (isDefault) | 112 | if (isDefault) |
86 | return GNUNET_SYSERR; | 113 | return GNUNET_SYSERR; |
87 | } | ||
88 | return GNUNET_OK; | 114 | return GNUNET_OK; |
89 | } | 115 | } |
90 | 116 | ||
117 | |||
118 | /** | ||
119 | * Main function run with scheduler. | ||
120 | */ | ||
91 | static void | 121 | static void |
92 | run (void *cls, | 122 | run (void *cls, |
93 | char *const *args, | 123 | char *const *args, |
@@ -98,19 +128,15 @@ run (void *cls, | |||
98 | struct sockaddr *addr; | 128 | struct sockaddr *addr; |
99 | 129 | ||
100 | GNUNET_log_setup ("test-nat", "DEBUG", NULL); | 130 | GNUNET_log_setup ("test-nat", "DEBUG", NULL); |
101 | |||
102 | data.addr = NULL; | 131 | data.addr = NULL; |
103 | GNUNET_OS_network_interfaces_list (process_if, &data); | 132 | GNUNET_OS_network_interfaces_list (process_if, &data); |
104 | if (!data.addr) | 133 | if (NULL == data.addr) |
105 | { | 134 | { |
106 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 135 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
107 | "Could not find a valid interface address!\n"); | 136 | "Could not find a valid interface address!\n"); |
108 | exit (GNUNET_SYSERR); | 137 | exit (GNUNET_SYSERR); |
109 | } | 138 | } |
110 | 139 | addr = data.addr; | |
111 | addr = GNUNET_malloc (data.addrlen); | ||
112 | memcpy (addr, data.addr, data.addrlen); | ||
113 | |||
114 | GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6); | 140 | GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6); |
115 | if (addr->sa_family == AF_INET) | 141 | if (addr->sa_family == AF_INET) |
116 | ((struct sockaddr_in *) addr)->sin_port = htons (2086); | 142 | ((struct sockaddr_in *) addr)->sin_port = htons (2086); |
@@ -121,14 +147,18 @@ run (void *cls, | |||
121 | "Requesting NAT redirection from address %s...\n", | 147 | "Requesting NAT redirection from address %s...\n", |
122 | GNUNET_a2s (addr, data.addrlen)); | 148 | GNUNET_a2s (addr, data.addrlen)); |
123 | 149 | ||
124 | nat = GNUNET_NAT_register (cfg, addr, data.addrlen, addr_callback, NULL); | 150 | nat = GNUNET_NAT_register (cfg, |
151 | GNUNET_YES /* tcp */, | ||
152 | 2086, | ||
153 | 1, | ||
154 | (const struct sockaddr**) &addr, | ||
155 | &data.addrlen, | ||
156 | &addr_callback, NULL, NULL); | ||
125 | GNUNET_free (addr); | 157 | GNUNET_free (addr); |
126 | 158 | GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat); | |
127 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
128 | (GNUNET_TIME_UNIT_SECONDS, TIMEOUT), stop, | ||
129 | nat); | ||
130 | } | 159 | } |
131 | 160 | ||
161 | |||
132 | int | 162 | int |
133 | main (int argc, char *const argv[]) | 163 | main (int argc, char *const argv[]) |
134 | { | 164 | { |
@@ -139,7 +169,7 @@ main (int argc, char *const argv[]) | |||
139 | char *const argv_prog[] = { | 169 | char *const argv_prog[] = { |
140 | "test-nat", | 170 | "test-nat", |
141 | "-c", | 171 | "-c", |
142 | "test-nat.conf", | 172 | "test_nat_data.conf", |
143 | "-L", | 173 | "-L", |
144 | #if VERBOSE | 174 | #if VERBOSE |
145 | "DEBUG", | 175 | "DEBUG", |
diff --git a/src/nat/test_nat_data.conf b/src/nat/test_nat_data.conf new file mode 100644 index 000000000..c312c69fb --- /dev/null +++ b/src/nat/test_nat_data.conf | |||
@@ -0,0 +1,136 @@ | |||
1 | [PATHS] | ||
2 | SERVICEHOME = /tmp/nat-test | ||
3 | # SERVICEHOME = /var/lib/gnunet/ | ||
4 | # DEFAULTCONFIG = /etc/gnunet.conf | ||
5 | # If 'DEFAULTCONFIG' is not defined, the current | ||
6 | # configuration file is assumed to be the default, | ||
7 | # which is what we want by default... | ||
8 | |||
9 | [gnunetd] | ||
10 | HOSTKEY = $SERVICEHOME/.hostkey | ||
11 | |||
12 | [TESTING] | ||
13 | WEAKRANDOM = NO | ||
14 | |||
15 | [client] | ||
16 | HOME = $SERVICEHOME | ||
17 | |||
18 | |||
19 | [nat] | ||
20 | # Are we behind NAT? | ||
21 | BEHIND_NAT = YES | ||
22 | |||
23 | # Is the NAT hole-punched? | ||
24 | PUNCHED_NAT = NO | ||
25 | |||
26 | # Disable UPNP by default until it gets cleaner! | ||
27 | ENABLE_UPNP = YES | ||
28 | |||
29 | # Use addresses from the local network interfaces (inluding loopback, but also others) | ||
30 | USE_LOCALADDR = YES | ||
31 | |||
32 | # External IP address of the NAT box (if known); IPv4 dotted-decimal ONLY at this time (should allow DynDNS!) | ||
33 | # normal interface IP address for non-NATed peers; | ||
34 | # possibly auto-detected (using UPnP) if possible if not specified | ||
35 | # EXTERNAL_ADDRESS = | ||
36 | |||
37 | # Should we use ICMP-based NAT traversal to try connect to NATed peers | ||
38 | # or, if we are behind NAT, to allow connections to us? | ||
39 | ENABLE_ICMP_CLIENT = YES | ||
40 | ENABLE_ICMP_SERVER = YES | ||
41 | |||
42 | # IP address of the interface connected to the NAT box; IPv4 dotted-decimal ONLY; | ||
43 | # normal interface IP address for non-NATed peers; | ||
44 | # likely auto-detected (via interface list) if not specified (!) | ||
45 | # INTERNAL_ADDRESS = | ||
46 | |||
47 | # Disable IPv6 support | ||
48 | DISABLEV6 = NO | ||
49 | |||
50 | [arm] | ||
51 | PORT = 2087 | ||
52 | HOSTNAME = localhost | ||
53 | HOME = $SERVICEHOME | ||
54 | CONFIG = $DEFAULTCONFIG | ||
55 | BINARY = gnunet-service-arm | ||
56 | ACCEPT_FROM = 127.0.0.1; | ||
57 | ACCEPT_FROM6 = ::1; | ||
58 | DEFAULTSERVICES = topology hostlist | ||
59 | UNIXPATH = /tmp/gnunet-service-arm.sock | ||
60 | UNIX_MATCH_UID = YES | ||
61 | UNIX_MATCH_GID = YES | ||
62 | # GLOBAL_POSTFIX = -l $SERVICEHOME/{}-logs | ||
63 | # GLOBAL_PREFIX = | ||
64 | # USERNAME = | ||
65 | # MAXBUF = | ||
66 | # TIMEOUT = | ||
67 | # DISABLEV6 = | ||
68 | # BINDTO = | ||
69 | # REJECT_FROM = | ||
70 | # REJECT_FROM6 = | ||
71 | # PREFIX = | ||
72 | |||
73 | [statistics] | ||
74 | AUTOSTART = YES | ||
75 | PORT = 2088 | ||
76 | HOSTNAME = localhost | ||
77 | HOME = $SERVICEHOME | ||
78 | CONFIG = $DEFAULTCONFIG | ||
79 | BINARY = gnunet-service-statistics | ||
80 | ACCEPT_FROM = 127.0.0.1; | ||
81 | ACCEPT_FROM6 = ::1; | ||
82 | UNIXPATH = /tmp/gnunet-service-statistics.sock | ||
83 | UNIX_MATCH_UID = NO | ||
84 | UNIX_MATCH_GID = YES | ||
85 | # DISABLE_SOCKET_FORWARDING = NO | ||
86 | # USERNAME = | ||
87 | # MAXBUF = | ||
88 | # TIMEOUT = | ||
89 | # DISABLEV6 = | ||
90 | # BINDTO = | ||
91 | # REJECT_FROM = | ||
92 | # REJECT_FROM6 = | ||
93 | # PREFIX = | ||
94 | |||
95 | [resolver] | ||
96 | AUTOSTART = YES | ||
97 | PORT = 2089 | ||
98 | HOSTNAME = localhost | ||
99 | HOME = $SERVICEHOME | ||
100 | CONFIG = $DEFAULTCONFIG | ||
101 | BINARY = gnunet-service-resolver | ||
102 | ACCEPT_FROM = 127.0.0.1; | ||
103 | ACCEPT_FROM6 = ::1; | ||
104 | UNIXPATH = /tmp/gnunet-service-resolver.sock | ||
105 | UNIX_MATCH_UID = NO | ||
106 | UNIX_MATCH_GID = NO | ||
107 | # DISABLE_SOCKET_FORWARDING = NO | ||
108 | # USERNAME = | ||
109 | # MAXBUF = | ||
110 | # TIMEOUT = | ||
111 | # DISABLEV6 = | ||
112 | # BINDTO = | ||
113 | # REJECT_FROM = | ||
114 | # REJECT_FROM6 = | ||
115 | # PREFIX = | ||
116 | |||
117 | [peerinfo] | ||
118 | AUTOSTART = NO | ||
119 | |||
120 | [transport] | ||
121 | AUTOSTART = NO | ||
122 | |||
123 | [core] | ||
124 | AUTOSTART = NO | ||
125 | |||
126 | [datastore] | ||
127 | AUTOSTART = NO | ||
128 | |||
129 | [fs] | ||
130 | AUTOSTART = NO | ||
131 | |||
132 | [dht] | ||
133 | AUTOSTART = NO | ||
134 | |||
135 | [mesh] | ||
136 | AUTOSTART = NO | ||
diff --git a/src/nat/upnp-commands.c b/src/nat/upnp-commands.c deleted file mode 100644 index 2a88f90f4..000000000 --- a/src/nat/upnp-commands.c +++ /dev/null | |||
@@ -1,877 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally based on the miniupnp library. | ||
23 | * Copyright (c) 2005-2009, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-commands.c | ||
53 | * @brief Implementation of a basic set of UPnP commands | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | |||
58 | #include "platform.h" | ||
59 | #include "gnunet_util_lib.h" | ||
60 | |||
61 | #include <stdlib.h> | ||
62 | #include <stdio.h> | ||
63 | #include <string.h> | ||
64 | |||
65 | #include "upnp-reply-parse.h" | ||
66 | #include "upnp-igd-parse.h" | ||
67 | #include "upnp-discover.h" | ||
68 | #include "upnp-commands.h" | ||
69 | |||
70 | #define SOAP_PREFIX "s" | ||
71 | #define SERVICE_PREFIX "u" | ||
72 | #define SERVICE_PREFIX2 'u' | ||
73 | #define MAX_HOSTNAME_LEN 64 | ||
74 | |||
75 | #define PRINT_UPNP_ERROR(a, b) GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "UPnP", _("%s failed at %s:%d: %s\n"), a, __FILE__, __LINE__, b); | ||
76 | |||
77 | |||
78 | /** | ||
79 | * Private closure used by UPNP_command() and its callbacks. | ||
80 | */ | ||
81 | struct UPNP_command_cls | ||
82 | { | ||
83 | /** | ||
84 | * Connection handle used for sending and receiving. | ||
85 | */ | ||
86 | struct GNUNET_CONNECTION_Handle *s; | ||
87 | |||
88 | /** | ||
89 | * Transmission handle used for sending command. | ||
90 | */ | ||
91 | struct GNUNET_CONNECTION_TransmitHandle *th; | ||
92 | |||
93 | /** | ||
94 | * HTML content to send to run command. | ||
95 | */ | ||
96 | char *content; | ||
97 | |||
98 | /** | ||
99 | * Buffer where to copy received data to pass to caller. | ||
100 | */ | ||
101 | char *buffer; | ||
102 | |||
103 | /** | ||
104 | * Size of buffer. | ||
105 | */ | ||
106 | size_t buf_size; | ||
107 | |||
108 | /** | ||
109 | * User callback to trigger when done. | ||
110 | */ | ||
111 | UPNP_command_cb_ caller_cb; | ||
112 | |||
113 | /** | ||
114 | * User closure to pass to caller_cb. | ||
115 | */ | ||
116 | void *caller_cls; | ||
117 | }; | ||
118 | |||
119 | /** | ||
120 | * Get the length of content included in an HTML line. | ||
121 | * | ||
122 | * @param p line to parse | ||
123 | * @param n size of p | ||
124 | * @return the length of the content | ||
125 | */ | ||
126 | static ssize_t | ||
127 | get_content_len_from_line (const char *p, int n) | ||
128 | { | ||
129 | static const char cont_len_str[] = "content-length"; | ||
130 | const char *p2 = cont_len_str; | ||
131 | int a = 0; | ||
132 | |||
133 | while (*p2) | ||
134 | { | ||
135 | if (n == 0) | ||
136 | return -1; | ||
137 | |||
138 | if (*p2 != *p && *p2 != (*p + 32)) | ||
139 | return -1; | ||
140 | |||
141 | p++; | ||
142 | p2++; | ||
143 | n--; | ||
144 | } | ||
145 | |||
146 | if (n == 0) | ||
147 | return -1; | ||
148 | |||
149 | if (*p != ':') | ||
150 | return -1; | ||
151 | |||
152 | p++; | ||
153 | n--; | ||
154 | |||
155 | while (*p == ' ') | ||
156 | { | ||
157 | if (n == 0) | ||
158 | return -1; | ||
159 | |||
160 | p++; | ||
161 | n--; | ||
162 | } | ||
163 | |||
164 | while (*p >= '0' && *p <= '9') | ||
165 | { | ||
166 | if (n == 0) | ||
167 | return -1; | ||
168 | |||
169 | a = (a * 10) + (*p - '0'); | ||
170 | p++; | ||
171 | n--; | ||
172 | } | ||
173 | |||
174 | return a; | ||
175 | } | ||
176 | |||
177 | /** | ||
178 | * Get the respective lengths of content and header from an HTML reply. | ||
179 | * | ||
180 | * @param p HTML to parse | ||
181 | * @param n size of p | ||
182 | * @param content_len pointer to store content length to | ||
183 | * @param header_len pointer to store header length to | ||
184 | */ | ||
185 | static void | ||
186 | get_content_and_header_len (const char *p, int n, | ||
187 | int *content_len, int *header_len) | ||
188 | { | ||
189 | const char *line; | ||
190 | int line_len; | ||
191 | int r; | ||
192 | |||
193 | line = p; | ||
194 | |||
195 | while (line < p + n) | ||
196 | { | ||
197 | line_len = 0; | ||
198 | |||
199 | while (line[line_len] != '\r' && line[line_len] != '\r') | ||
200 | { | ||
201 | if (line + line_len >= p + n) | ||
202 | return; | ||
203 | |||
204 | line_len++; | ||
205 | } | ||
206 | |||
207 | r = get_content_len_from_line (line, line_len); | ||
208 | |||
209 | if (r > 0) | ||
210 | *content_len = r; | ||
211 | |||
212 | line = line + line_len + 2; | ||
213 | |||
214 | if (line[0] == '\r' && line[1] == '\n') | ||
215 | { | ||
216 | *header_len = (line - p) + 2; | ||
217 | return; | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | /** | ||
223 | * Receive reply of the device to our UPnP command. | ||
224 | * | ||
225 | * @param data closure from UPNP_command() | ||
226 | * @param buf struct UPNP_command_cls *cls | ||
227 | * @param available number of bytes in buf | ||
228 | * @param addr address of the sender | ||
229 | * @param addrlen size of addr | ||
230 | * @param errCode value of errno | ||
231 | */ | ||
232 | static void | ||
233 | UPNP_command_receiver (void *data, | ||
234 | const void *buf, | ||
235 | size_t available, | ||
236 | const struct sockaddr *addr, | ||
237 | socklen_t addrlen, int errCode) | ||
238 | { | ||
239 | struct UPNP_command_cls *cls = data; | ||
240 | int content_len; | ||
241 | int header_len; | ||
242 | |||
243 | if (available > 0) | ||
244 | { | ||
245 | content_len = -1; | ||
246 | header_len = -1; | ||
247 | get_content_and_header_len (buf, available, &content_len, &header_len); | ||
248 | |||
249 | strncpy (cls->buffer, (char *) buf, cls->buf_size - 2); | ||
250 | cls->buffer[cls->buf_size - 2] = '\0'; | ||
251 | } | ||
252 | else | ||
253 | { | ||
254 | cls->buffer[0] = '\0'; | ||
255 | } | ||
256 | |||
257 | GNUNET_CONNECTION_destroy (cls->s, GNUNET_NO); | ||
258 | |||
259 | cls->caller_cb (cls->buffer, cls->buf_size, cls->caller_cls); | ||
260 | |||
261 | GNUNET_free (cls->content); | ||
262 | GNUNET_free (cls); | ||
263 | } | ||
264 | |||
265 | /** | ||
266 | * Send UPnP command to device. | ||
267 | */ | ||
268 | static size_t | ||
269 | UPNP_command_transmit (void *data, size_t size, void *buf) | ||
270 | { | ||
271 | struct UPNP_command_cls *cls = data; | ||
272 | int n; | ||
273 | char *content = cls->content; | ||
274 | |||
275 | n = strlen (content); | ||
276 | memcpy (buf, content, size); | ||
277 | |||
278 | GNUNET_CONNECTION_receive (cls->s, cls->buf_size, GNUNET_TIME_UNIT_MINUTES, | ||
279 | UPNP_command_receiver, cls); | ||
280 | |||
281 | return n; | ||
282 | } | ||
283 | |||
284 | /** | ||
285 | * Parse a HTTP URL string to extract hostname, port and path it points to. | ||
286 | * | ||
287 | * @param url source string corresponding to URL | ||
288 | * @param hostname pointer where to store hostname (size of MAX_HOSTNAME_LEN+1) | ||
289 | * @param port pointer where to store port | ||
290 | * @param path pointer where to store path | ||
291 | * | ||
292 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure | ||
293 | */ | ||
294 | int | ||
295 | parse_url (const char *url, char *hostname, unsigned short *port, char **path) | ||
296 | { | ||
297 | char *p1, *p2, *p3; | ||
298 | |||
299 | if (!url) | ||
300 | return GNUNET_SYSERR; | ||
301 | |||
302 | p1 = strstr (url, "://"); | ||
303 | |||
304 | if (!p1) | ||
305 | return GNUNET_SYSERR; | ||
306 | |||
307 | p1 += 3; | ||
308 | |||
309 | if ((url[0] != 'h') || (url[1] != 't') | ||
310 | || (url[2] != 't') || (url[3] != 'p')) | ||
311 | return GNUNET_SYSERR; | ||
312 | |||
313 | p2 = strchr (p1, ':'); | ||
314 | p3 = strchr (p1, '/'); | ||
315 | |||
316 | if (!p3) | ||
317 | return GNUNET_SYSERR; | ||
318 | |||
319 | memset (hostname, 0, MAX_HOSTNAME_LEN + 1); | ||
320 | |||
321 | if (!p2 || (p2 > p3)) | ||
322 | { | ||
323 | strncpy (hostname, p1, GNUNET_MIN (MAX_HOSTNAME_LEN, (int) (p3 - p1))); | ||
324 | *port = 80; | ||
325 | } | ||
326 | else | ||
327 | { | ||
328 | strncpy (hostname, p1, GNUNET_MIN (MAX_HOSTNAME_LEN, (int) (p2 - p1))); | ||
329 | *port = 0; | ||
330 | p2++; | ||
331 | |||
332 | while ((*p2 >= '0') && (*p2 <= '9')) | ||
333 | { | ||
334 | *port *= 10; | ||
335 | *port += (unsigned short) (*p2 - '0'); | ||
336 | p2++; | ||
337 | } | ||
338 | } | ||
339 | |||
340 | *path = p3; | ||
341 | return GNUNET_OK; | ||
342 | } | ||
343 | |||
344 | /** | ||
345 | * Send UPnP command to the device identified by url and service. | ||
346 | * | ||
347 | * @param url control URL of the device | ||
348 | * @param service type of the service corresponding to the command | ||
349 | * @param action action to send | ||
350 | * @param args arguments for action | ||
351 | * @param buffer buffer | ||
352 | * @param buf_size buffer size | ||
353 | * @param caller_cb user callback to trigger when done | ||
354 | * @param caller_cls closure to pass to caller_cb | ||
355 | */ | ||
356 | void | ||
357 | UPNP_command_ (const char *url, const char *service, | ||
358 | const char *action, struct UPNP_Arg_ *args, | ||
359 | char *buffer, size_t buf_size, | ||
360 | UPNP_command_cb_ caller_cb, void *caller_cls) | ||
361 | { | ||
362 | struct GNUNET_CONNECTION_Handle *s; | ||
363 | struct UPNP_command_cls *cls; | ||
364 | struct sockaddr_in dest; | ||
365 | struct sockaddr_in6 dest6; | ||
366 | char hostname[MAX_HOSTNAME_LEN + 1]; | ||
367 | unsigned short port = 0; | ||
368 | char *path; | ||
369 | char soap_act[128]; | ||
370 | char soap_body[2048]; | ||
371 | int body_size; | ||
372 | char *content_buf; | ||
373 | int headers_size; | ||
374 | char port_str[8]; | ||
375 | |||
376 | snprintf (soap_act, sizeof (soap_act), "%s#%s", service, action); | ||
377 | |||
378 | if (args == NULL) | ||
379 | { | ||
380 | snprintf (soap_body, sizeof (soap_body), | ||
381 | "<?xml version=\"1.0\"?>\r\n" | ||
382 | "<" SOAP_PREFIX ":Envelope " | ||
383 | "xmlns:" SOAP_PREFIX | ||
384 | "=\"http://schemas.xmlsoap.org/soap/envelope/\" " | ||
385 | SOAP_PREFIX | ||
386 | ":encodingStyle=\"http://schema GNUNET_free (content_buf);s.xmlsoap.org/soap/encoding/\">" | ||
387 | "<" SOAP_PREFIX ":Body>" "<" SERVICE_PREFIX | ||
388 | ":%s xmlns:" SERVICE_PREFIX "=\"%s\">" "</" | ||
389 | SERVICE_PREFIX ":%s>" "</" SOAP_PREFIX | ||
390 | ":Body></" SOAP_PREFIX ":Envelope>" "\r\n", | ||
391 | action, service, action); | ||
392 | } | ||
393 | else | ||
394 | { | ||
395 | char *p; | ||
396 | const char *pe, *pv; | ||
397 | int soap_body_len; | ||
398 | |||
399 | soap_body_len = snprintf (soap_body, sizeof (soap_body), | ||
400 | "<?xml version=\"1.0\"?>\r\n" | ||
401 | "<" SOAP_PREFIX ":Envelope " | ||
402 | "xmlns:" SOAP_PREFIX | ||
403 | "=\"http://schemas.xmlsoap.org/soap/envelope/\" " | ||
404 | SOAP_PREFIX | ||
405 | ":encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" | ||
406 | "<" SOAP_PREFIX ":Body>" "<" SERVICE_PREFIX | ||
407 | ":%s xmlns:" SERVICE_PREFIX "=\"%s\">", | ||
408 | action, service); | ||
409 | |||
410 | p = soap_body + soap_body_len; | ||
411 | |||
412 | while (args->elt) | ||
413 | { | ||
414 | /* check that we are never overflowing the string... */ | ||
415 | if (soap_body + sizeof (soap_body) <= p + 100) | ||
416 | { | ||
417 | GNUNET_assert (GNUNET_NO); | ||
418 | caller_cb (buffer, 0, caller_cls); | ||
419 | return; | ||
420 | } | ||
421 | *(p++) = '<'; | ||
422 | pe = args->elt; | ||
423 | while (*pe) | ||
424 | *(p++) = *(pe++); | ||
425 | *(p++) = '>'; | ||
426 | if ((pv = args->val)) | ||
427 | { | ||
428 | while (*pv) | ||
429 | *(p++) = *(pv++); | ||
430 | } | ||
431 | *(p++) = '<'; | ||
432 | *(p++) = '/'; | ||
433 | pe = args->elt; | ||
434 | while (*pe) | ||
435 | *(p++) = *(pe++); | ||
436 | *(p++) = '>'; | ||
437 | args++; | ||
438 | } | ||
439 | *(p++) = '<'; | ||
440 | *(p++) = '/'; | ||
441 | *(p++) = SERVICE_PREFIX2; | ||
442 | *(p++) = ':'; | ||
443 | pe = action; | ||
444 | |||
445 | while (*pe) | ||
446 | *(p++) = *(pe++); | ||
447 | |||
448 | strncpy (p, "></" SOAP_PREFIX ":Body></" SOAP_PREFIX ":Envelope>\r\n", | ||
449 | soap_body + sizeof (soap_body) - p); | ||
450 | } | ||
451 | |||
452 | if (GNUNET_OK != parse_url (url, hostname, &port, &path)) | ||
453 | { | ||
454 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "UPnP", | ||
455 | "Invalid URL passed to UPNP_command(): %s\n", url); | ||
456 | caller_cb (buffer, 0, caller_cls); | ||
457 | return; | ||
458 | } | ||
459 | |||
460 | |||
461 | /* Test IPv4 address, else use IPv6 */ | ||
462 | memset (&dest, 0, sizeof (dest)); | ||
463 | memset (&dest6, 0, sizeof (dest6)); | ||
464 | |||
465 | if (inet_pton (AF_INET, hostname, &dest.sin_addr) == 1) | ||
466 | { | ||
467 | dest.sin_family = AF_INET; | ||
468 | dest.sin_port = htons (port); | ||
469 | #ifdef HAVE_SOCKADDR_IN_SIN_LEN | ||
470 | dest.sin_len = sizeof (dest); | ||
471 | #endif | ||
472 | |||
473 | s = GNUNET_CONNECTION_create_from_sockaddr (PF_INET, | ||
474 | (struct sockaddr *) &dest, | ||
475 | sizeof (dest)); | ||
476 | } | ||
477 | else if (inet_pton (AF_INET6, hostname, &dest6.sin6_addr) == 1) | ||
478 | { | ||
479 | dest6.sin6_family = AF_INET6; | ||
480 | dest6.sin6_port = htons (port); | ||
481 | #ifdef HAVE_SOCKADDR_IN_SIN_LEN | ||
482 | dest6.sin6_len = sizeof (dest6); | ||
483 | #endif | ||
484 | |||
485 | s = GNUNET_CONNECTION_create_from_sockaddr (PF_INET6, | ||
486 | (struct sockaddr *) &dest6, | ||
487 | sizeof (dest6)); | ||
488 | } | ||
489 | else | ||
490 | { | ||
491 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d\n"), | ||
492 | "UPnP", "inet_pton", __FILE__, __LINE__); | ||
493 | |||
494 | caller_cb (buffer, 0, caller_cls); | ||
495 | return; | ||
496 | } | ||
497 | |||
498 | body_size = (int) strlen (soap_body); | ||
499 | content_buf = GNUNET_malloc (512 + body_size); | ||
500 | |||
501 | /* We are not using keep-alive HTTP connections. | ||
502 | * HTTP/1.1 needs the header Connection: close to do that. | ||
503 | * This is the default with HTTP/1.0 */ | ||
504 | /* Connection: Close is normally there only in HTTP/1.1 but who knows */ | ||
505 | port_str[0] = '\0'; | ||
506 | |||
507 | if (port != 80) | ||
508 | snprintf (port_str, sizeof (port_str), ":%hu", port); | ||
509 | |||
510 | headers_size = snprintf (content_buf, 512, "POST %s HTTP/1.1\r\n" "Host: %s%s\r\n" "User-Agent: GNU, UPnP/1.0, GNUnet/" PACKAGE_VERSION "\r\n" "Content-Length: %d\r\n" "Content-Type: text/xml\r\n" "SOAPAction: \"%s\"\r\n" "Connection: Close\r\n" "Cache-Control: no-cache\r\n" /* ??? */ | ||
511 | "Pragma: no-cache\r\n" | ||
512 | "\r\n", path, hostname, port_str, body_size, | ||
513 | soap_act); | ||
514 | memcpy (content_buf + headers_size, soap_body, body_size); | ||
515 | |||
516 | #ifdef DEBUG_UPNP | ||
517 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
518 | "Sending command '%s' to '%s' (service '%s')\n", | ||
519 | action, url, service); | ||
520 | #endif | ||
521 | |||
522 | cls = GNUNET_malloc (sizeof (struct UPNP_command_cls)); | ||
523 | cls->s = s; | ||
524 | cls->content = content_buf; | ||
525 | cls->buffer = buffer; | ||
526 | cls->buf_size = buf_size; | ||
527 | cls->caller_cb = caller_cb; | ||
528 | cls->caller_cls = caller_cls; | ||
529 | |||
530 | cls->th = | ||
531 | GNUNET_CONNECTION_notify_transmit_ready (s, body_size + headers_size, | ||
532 | GNUNET_TIME_relative_multiply | ||
533 | (GNUNET_TIME_UNIT_SECONDS, 15), | ||
534 | &UPNP_command_transmit, cls); | ||
535 | |||
536 | |||
537 | if (cls->th == NULL) | ||
538 | { | ||
539 | #ifdef DEBUG_UPNP | ||
540 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "UPnP", | ||
541 | "Error sending SOAP request at %s:%d\n", __FILE__, | ||
542 | __LINE__); | ||
543 | #endif | ||
544 | |||
545 | caller_cb (buffer, 0, caller_cls); | ||
546 | |||
547 | GNUNET_free (content_buf); | ||
548 | GNUNET_free (cls); | ||
549 | GNUNET_CONNECTION_destroy (s, GNUNET_NO); | ||
550 | return; | ||
551 | } | ||
552 | } | ||
553 | |||
554 | struct get_external_ip_address_cls | ||
555 | { | ||
556 | UPNP_get_external_ip_address_cb_ caller_cb; | ||
557 | void *caller_cls; | ||
558 | }; | ||
559 | |||
560 | static void | ||
561 | get_external_ip_address_receiver (char *response, size_t received, void *data) | ||
562 | { | ||
563 | struct get_external_ip_address_cls *cls = data; | ||
564 | struct UPNP_REPLY_NameValueList_ pdata; | ||
565 | char extIpAdd[128]; | ||
566 | char *p; | ||
567 | int ret = UPNP_COMMAND_UNKNOWN_ERROR; | ||
568 | |||
569 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Response: %s", response); | ||
570 | |||
571 | UPNP_REPLY_parse_ (response, received, &pdata); | ||
572 | p = UPNP_REPLY_get_value_ (&pdata, "NewExternalIPAddress"); | ||
573 | if (p) | ||
574 | { | ||
575 | strncpy (extIpAdd, p, 128); | ||
576 | extIpAdd[127] = '\0'; | ||
577 | ret = UPNP_COMMAND_SUCCESS; | ||
578 | } | ||
579 | else | ||
580 | extIpAdd[0] = '\0'; | ||
581 | |||
582 | p = UPNP_REPLY_get_value_ (&pdata, "errorCode"); | ||
583 | if (p) | ||
584 | { | ||
585 | ret = UPNP_COMMAND_UNKNOWN_ERROR; | ||
586 | sscanf (p, "%d", &ret); | ||
587 | } | ||
588 | cls->caller_cb (ret, extIpAdd, cls->caller_cls); | ||
589 | |||
590 | UPNP_REPLY_free_ (&pdata); | ||
591 | GNUNET_free (response); | ||
592 | GNUNET_free (cls); | ||
593 | } | ||
594 | |||
595 | /* UPNP_get_external_ip_address_() call the corresponding UPNP method. | ||
596 | * | ||
597 | * Return values : | ||
598 | * 0 : SUCCESS | ||
599 | * NON ZERO : ERROR Either an UPnP error code or an unknown error. | ||
600 | * | ||
601 | * 402 Invalid Args - See UPnP Device Architecture section on Control. | ||
602 | * 501 Action Failed - See UPnP Device Architecture section on Control. | ||
603 | */ | ||
604 | void | ||
605 | UPNP_get_external_ip_address_ (const char *control_url, | ||
606 | const char *service_type, | ||
607 | UPNP_get_external_ip_address_cb_ caller_cb, | ||
608 | void *caller_cls) | ||
609 | { | ||
610 | struct get_external_ip_address_cls *cls; | ||
611 | char *buffer; | ||
612 | |||
613 | if (!control_url || !service_type) | ||
614 | caller_cb (UPNP_COMMAND_INVALID_ARGS, NULL, caller_cls); | ||
615 | |||
616 | cls = GNUNET_malloc (sizeof (struct get_external_ip_address_cls)); | ||
617 | cls->caller_cb = caller_cb; | ||
618 | cls->caller_cls = caller_cls; | ||
619 | |||
620 | buffer = GNUNET_malloc (UPNP_COMMAND_BUFSIZE); | ||
621 | |||
622 | UPNP_command_ (control_url, service_type, "GetExternalIPAddress", | ||
623 | NULL, buffer, UPNP_COMMAND_BUFSIZE, | ||
624 | (UPNP_command_cb_) get_external_ip_address_receiver, cls); | ||
625 | } | ||
626 | |||
627 | struct PortMapping_cls | ||
628 | { | ||
629 | const char *control_url; | ||
630 | const char *service_type; | ||
631 | const char *ext_port; | ||
632 | const char *in_port; | ||
633 | const char *proto; | ||
634 | const char *remoteHost; | ||
635 | UPNP_port_mapping_cb_ caller_cb; | ||
636 | void *caller_cls; | ||
637 | }; | ||
638 | |||
639 | static void | ||
640 | add_delete_port_mapping_receiver (char *response, size_t received, void *data) | ||
641 | { | ||
642 | struct PortMapping_cls *cls = data; | ||
643 | struct UPNP_REPLY_NameValueList_ pdata; | ||
644 | const char *resVal; | ||
645 | int ret; | ||
646 | |||
647 | UPNP_REPLY_parse_ (response, received, &pdata); | ||
648 | resVal = UPNP_REPLY_get_value_ (&pdata, "errorCode"); | ||
649 | if (resVal) | ||
650 | { | ||
651 | ret = UPNP_COMMAND_UNKNOWN_ERROR; | ||
652 | sscanf (resVal, "%d", &ret); | ||
653 | } | ||
654 | else | ||
655 | { | ||
656 | ret = UPNP_COMMAND_SUCCESS; | ||
657 | } | ||
658 | |||
659 | cls->caller_cb (ret, cls->control_url, cls->service_type, | ||
660 | cls->ext_port, cls->in_port, cls->proto, | ||
661 | cls->remoteHost, cls->caller_cls); | ||
662 | |||
663 | UPNP_REPLY_free_ (&pdata); | ||
664 | GNUNET_free (response); | ||
665 | GNUNET_free (cls); | ||
666 | } | ||
667 | |||
668 | void | ||
669 | UPNP_add_port_mapping_ (const char *control_url, const char *service_type, | ||
670 | const char *ext_port, | ||
671 | const char *in_port, | ||
672 | const char *inClient, | ||
673 | const char *desc, | ||
674 | const char *proto, const char *remoteHost, | ||
675 | UPNP_port_mapping_cb_ caller_cb, void *caller_cls) | ||
676 | { | ||
677 | struct UPNP_Arg_ args[9]; | ||
678 | struct PortMapping_cls *cls; | ||
679 | char *buffer; | ||
680 | |||
681 | if (!in_port || !inClient || !proto || !ext_port) | ||
682 | { | ||
683 | caller_cb (UPNP_COMMAND_INVALID_ARGS, control_url, service_type, | ||
684 | ext_port, in_port, proto, remoteHost, caller_cls); | ||
685 | return; | ||
686 | } | ||
687 | |||
688 | args[0].elt = "NewRemoteHost"; | ||
689 | args[0].val = remoteHost; | ||
690 | args[1].elt = "NewExternalPort"; | ||
691 | args[1].val = ext_port; | ||
692 | args[2].elt = "NewProtocol"; | ||
693 | args[2].val = proto; | ||
694 | args[3].elt = "NewInternalPort"; | ||
695 | args[3].val = in_port; | ||
696 | args[4].elt = "NewInternalClient"; | ||
697 | args[4].val = inClient; | ||
698 | args[5].elt = "NewEnabled"; | ||
699 | args[5].val = "1"; | ||
700 | args[6].elt = "NewPortMappingDescription"; | ||
701 | args[6].val = desc ? desc : "GNUnet"; | ||
702 | args[7].elt = "NewLeaseDuration"; | ||
703 | args[7].val = "0"; | ||
704 | args[8].elt = NULL; | ||
705 | args[8].val = NULL; | ||
706 | |||
707 | cls = GNUNET_malloc (sizeof (struct PortMapping_cls)); | ||
708 | cls->control_url = control_url; | ||
709 | cls->service_type = service_type; | ||
710 | cls->ext_port = ext_port;; | ||
711 | cls->in_port = in_port; | ||
712 | cls->proto = proto; | ||
713 | cls->remoteHost = remoteHost; | ||
714 | cls->caller_cb = caller_cb; | ||
715 | cls->caller_cls = caller_cls; | ||
716 | |||
717 | buffer = GNUNET_malloc (UPNP_COMMAND_BUFSIZE); | ||
718 | |||
719 | UPNP_command_ (control_url, service_type, "AddPortMapping", | ||
720 | args, buffer, UPNP_COMMAND_BUFSIZE, | ||
721 | add_delete_port_mapping_receiver, cls); | ||
722 | } | ||
723 | |||
724 | void | ||
725 | UPNP_delete_port_mapping_ (const char *control_url, const char *service_type, | ||
726 | const char *ext_port, const char *proto, | ||
727 | const char *remoteHost, | ||
728 | UPNP_port_mapping_cb_ caller_cb, void *caller_cls) | ||
729 | { | ||
730 | struct UPNP_Arg_ args[4]; | ||
731 | struct PortMapping_cls *cls; | ||
732 | char *buffer; | ||
733 | |||
734 | if (!ext_port || !proto) | ||
735 | { | ||
736 | caller_cb (UPNP_COMMAND_INVALID_ARGS, control_url, service_type, | ||
737 | ext_port, NULL, proto, remoteHost, caller_cls); | ||
738 | return; | ||
739 | } | ||
740 | |||
741 | args[0].elt = "NewRemoteHost"; | ||
742 | args[0].val = remoteHost; | ||
743 | args[1].elt = "NewExternalPort"; | ||
744 | args[1].val = ext_port; | ||
745 | args[2].elt = "NewProtocol"; | ||
746 | args[2].val = proto; | ||
747 | args[3].elt = NULL; | ||
748 | args[3].val = NULL; | ||
749 | |||
750 | cls = GNUNET_malloc (sizeof (struct PortMapping_cls)); | ||
751 | cls->control_url = control_url; | ||
752 | cls->service_type = service_type; | ||
753 | cls->ext_port = ext_port; | ||
754 | cls->in_port = "0"; | ||
755 | cls->proto = proto; | ||
756 | cls->remoteHost = remoteHost; | ||
757 | cls->caller_cb = caller_cb; | ||
758 | cls->caller_cls = caller_cls; | ||
759 | |||
760 | buffer = GNUNET_malloc (UPNP_COMMAND_BUFSIZE); | ||
761 | |||
762 | UPNP_command_ (control_url, service_type, | ||
763 | "DeletePortMapping", | ||
764 | args, buffer, UPNP_COMMAND_BUFSIZE, | ||
765 | add_delete_port_mapping_receiver, cls); | ||
766 | } | ||
767 | |||
768 | |||
769 | struct get_specific_port_mapping_entry_cls | ||
770 | { | ||
771 | const char *control_url; | ||
772 | const char *service_type; | ||
773 | const char *ext_port; | ||
774 | const char *proto; | ||
775 | UPNP_port_mapping_cb_ caller_cb; | ||
776 | void *caller_cls; | ||
777 | }; | ||
778 | |||
779 | static void | ||
780 | get_specific_port_mapping_entry_receiver (char *response, size_t received, | ||
781 | void *data) | ||
782 | { | ||
783 | struct PortMapping_cls *cls = data; | ||
784 | struct UPNP_REPLY_NameValueList_ pdata; | ||
785 | char *p; | ||
786 | char in_port[128]; | ||
787 | char in_client[128]; | ||
788 | int ret; | ||
789 | |||
790 | UPNP_REPLY_parse_ (response, received, &pdata); | ||
791 | |||
792 | p = UPNP_REPLY_get_value_ (&pdata, "NewInternalClient"); | ||
793 | if (p) | ||
794 | { | ||
795 | strncpy (in_client, p, 128); | ||
796 | in_client[127] = '\0'; | ||
797 | } | ||
798 | else | ||
799 | in_client[0] = '\0'; | ||
800 | |||
801 | p = UPNP_REPLY_get_value_ (&pdata, "NewInternalPort"); | ||
802 | if (p) | ||
803 | { | ||
804 | strncpy (in_port, p, 6); | ||
805 | in_port[5] = '\0'; | ||
806 | } | ||
807 | else | ||
808 | in_port[0] = '\0'; | ||
809 | |||
810 | p = UPNP_REPLY_get_value_ (&pdata, "errorCode"); | ||
811 | if (p) | ||
812 | { | ||
813 | if (p) | ||
814 | { | ||
815 | ret = UPNP_COMMAND_UNKNOWN_ERROR; | ||
816 | sscanf (p, "%d", &ret); | ||
817 | } | ||
818 | #if DEBUG_UPNP | ||
819 | PRINT_UPNP_ERROR ("GetSpecificPortMappingEntry", p); | ||
820 | #endif | ||
821 | } | ||
822 | |||
823 | cls->caller_cb (ret, cls->control_url, cls->service_type, | ||
824 | cls->ext_port, cls->proto, in_port, in_client, | ||
825 | cls->caller_cls); | ||
826 | |||
827 | UPNP_REPLY_free_ (&pdata); | ||
828 | GNUNET_free (response); | ||
829 | GNUNET_free (cls); | ||
830 | } | ||
831 | |||
832 | /* UPNP_get_specific_port_mapping_entry _ retrieves an existing port mapping | ||
833 | * the result is returned in the in_client and in_port strings | ||
834 | * please provide 128 and 6 bytes of data */ | ||
835 | void | ||
836 | UPNP_get_specific_port_mapping_entry_ (const char *control_url, | ||
837 | const char *service_type, | ||
838 | const char *ext_port, | ||
839 | const char *proto, | ||
840 | UPNP_get_specific_port_mapping_entry_cb_ | ||
841 | caller_cb, void *caller_cls) | ||
842 | { | ||
843 | struct UPNP_Arg_ args[4]; | ||
844 | struct get_specific_port_mapping_entry_cls *cls; | ||
845 | char *buffer; | ||
846 | |||
847 | if (!ext_port || !proto) | ||
848 | { | ||
849 | caller_cb (UPNP_COMMAND_INVALID_ARGS, control_url, service_type, | ||
850 | ext_port, proto, NULL, NULL, caller_cls); | ||
851 | return; | ||
852 | } | ||
853 | |||
854 | args[0].elt = "NewRemoteHost"; | ||
855 | args[0].val = NULL; | ||
856 | args[1].elt = "NewExternalPort"; | ||
857 | args[1].val = ext_port; | ||
858 | args[2].elt = "NewProtocol"; | ||
859 | args[2].val = proto; | ||
860 | args[3].elt = NULL; | ||
861 | args[3].val = NULL; | ||
862 | |||
863 | cls = GNUNET_malloc (sizeof (struct PortMapping_cls)); | ||
864 | cls->control_url = control_url; | ||
865 | cls->service_type = service_type; | ||
866 | cls->ext_port = ext_port; | ||
867 | cls->proto = proto; | ||
868 | cls->caller_cb = caller_cb; | ||
869 | cls->caller_cls = caller_cls; | ||
870 | |||
871 | buffer = GNUNET_malloc (UPNP_COMMAND_BUFSIZE); | ||
872 | |||
873 | UPNP_command_ (control_url, service_type, | ||
874 | "GetSpecificPortMappingEntry", | ||
875 | args, buffer, UPNP_COMMAND_BUFSIZE, | ||
876 | get_specific_port_mapping_entry_receiver, cls); | ||
877 | } | ||
diff --git a/src/nat/upnp-commands.h b/src/nat/upnp-commands.h deleted file mode 100644 index ea7b48162..000000000 --- a/src/nat/upnp-commands.h +++ /dev/null | |||
@@ -1,271 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally based on the miniupnp library. | ||
23 | * Copyright (c) 2005-2009, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-commands.h | ||
53 | * @brief Commands to control UPnP IGD devices | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | #ifndef UPNP_COMMANDS_H | ||
58 | #define UPNP_COMMANDS_H | ||
59 | |||
60 | #include "platform.h" | ||
61 | #include "gnunet_scheduler_lib.h" | ||
62 | |||
63 | /** | ||
64 | * Generic UPnP error codes. | ||
65 | */ | ||
66 | #define UPNP_COMMAND_SUCCESS (0) | ||
67 | #define UPNP_COMMAND_UNKNOWN_ERROR (-1) | ||
68 | #define UPNP_COMMAND_INVALID_ARGS (-2) | ||
69 | |||
70 | /** | ||
71 | * Size of the buffer used to store anwsers to UPnP commands. | ||
72 | */ | ||
73 | #define UPNP_COMMAND_BUFSIZE 4096 | ||
74 | |||
75 | /** | ||
76 | * Name-value pair containing an argumeny to a UPnP command. | ||
77 | */ | ||
78 | struct UPNP_Arg_ | ||
79 | { | ||
80 | const char *elt; | ||
81 | const char *val; | ||
82 | }; | ||
83 | |||
84 | /** | ||
85 | * Callback for UPNP_command_(). | ||
86 | * | ||
87 | * @param response the buffer passed to UPNP_command_(), filled with | ||
88 | * NULL-terminated content (if any) | ||
89 | * @param received length of the content received and stored in response | ||
90 | * @param cls closure passed to UPNP_command_() | ||
91 | */ | ||
92 | typedef void (*UPNP_command_cb_) (char *response, size_t received, void *cls); | ||
93 | |||
94 | /** | ||
95 | * Send UPnP command to the device identified by url and service. | ||
96 | * | ||
97 | * @param url control URL of the device | ||
98 | * @param service type of the service corresponding to the command | ||
99 | * @param action action to send | ||
100 | * @param args arguments for action | ||
101 | * @param buffer buffer | ||
102 | * @param buf_size buffer size | ||
103 | * @param caller_cb user callback to trigger when done | ||
104 | * @param caller_cls closure to pass to caller_cb | ||
105 | */ | ||
106 | void UPNP_command_ (const char *url, const char *service, | ||
107 | const char *action, struct UPNP_Arg_ *args, | ||
108 | char *buffer, size_t buf_size, | ||
109 | UPNP_command_cb_ caller_cb, void *caller_cls); | ||
110 | |||
111 | /** | ||
112 | * Callback to UPNP_get_external_ip_address_(). | ||
113 | * | ||
114 | * Possible UPnP Errors : | ||
115 | * 402 Invalid Args - See UPnP Device Architecture section on Control. | ||
116 | * 501 Action Failed - See UPnP Device Architecture section on Control. | ||
117 | * | ||
118 | * @param error GNUNET_OK on success, another value on error (see above) | ||
119 | * @param ext_ip_addr the external IP address reported by the device (IPv4 or v6) | ||
120 | * @param cls the closure passed to UPNP_get_external_ip_address_() | ||
121 | */ | ||
122 | typedef void (*UPNP_get_external_ip_address_cb_) (int error, | ||
123 | char *ext_ip_addr, | ||
124 | void *cls); | ||
125 | |||
126 | /** | ||
127 | * Get the IP address associated with the WAN connection of the device. | ||
128 | * See UPNP_get_external_ip_address_cb_. | ||
129 | * | ||
130 | * @param control_url the control URL corresponding to service_type on the device | ||
131 | * @param service_type service type to call the command on | ||
132 | * @param caller_cb function to call when done | ||
133 | * @param caller_cls closure passed to caller_cb | ||
134 | */ | ||
135 | void | ||
136 | UPNP_get_external_ip_address_ (const char *control_url, | ||
137 | const char *service_type, | ||
138 | UPNP_get_external_ip_address_cb_ caller_cb, | ||
139 | void *caller_cls); | ||
140 | |||
141 | /** | ||
142 | * Callback to UPNP_add_port_mapping_() and UPNP_delete_port_mapping_(). | ||
143 | * | ||
144 | * Possible UPnP Errors with UPNP_add_port_mapping_(): | ||
145 | * 402 Invalid Args - See UPnP Device Architecture section on Control. | ||
146 | * 501 Action Failed - See UPnP Device Architecture section on Control. | ||
147 | * 715 WildCardNotPermittedInSrcIP - The source IP address cannot be | ||
148 | * wild-carded | ||
149 | * 716 WildCardNotPermittedInext_port - The external port cannot be wild-carded | ||
150 | * 718 ConflictInMappingEntry - The port mapping entry specified conflicts | ||
151 | * with a mapping assigned previously to another client | ||
152 | * 724 SamePortValuesRequired - Internal and External port values | ||
153 | * must be the same | ||
154 | * 725 OnlyPermanentLeasesSupported - The NAT implementation only supports | ||
155 | * permanent lease times on port mappings | ||
156 | * 726 RemoteHostOnlySupportsWildcard - RemoteHost must be a wildcard | ||
157 | * and cannot be a specific IP address or DNS name | ||
158 | * 727 ExternalPortOnlySupportsWildcard - ExternalPort must be a wildcard and | ||
159 | * cannot be a specific port value | ||
160 | * | ||
161 | * Possible UPnP Errors with UPNP_delete_port_mapping_(): | ||
162 | * 402 Invalid Args - See UPnP Device Architecture section on Control. | ||
163 | * 714 NoSuchEntryInArray - The specified value does not exist in the array | ||
164 | * | ||
165 | * @param error GNUNET_OK on success, another value on error (see above) | ||
166 | * @param control_url the control URL the command was called on | ||
167 | * @param service_type service the command was called on | ||
168 | * @param ext_port external port | ||
169 | * @param inPort port on the gateway on the LAN side which was requested | ||
170 | * @param proto protocol for which port mapping was requested | ||
171 | * @param remote_host remote host for which port mapping was requested | ||
172 | * @param cls the closure passed to the command function | ||
173 | */ | ||
174 | typedef void (*UPNP_port_mapping_cb_) (int error, | ||
175 | const char *control_url, | ||
176 | const char *service_type, | ||
177 | const char *ext_port, | ||
178 | const char *inPort, const char *proto, | ||
179 | const char *remote_host, void *cls); | ||
180 | |||
181 | |||
182 | /** | ||
183 | * Request opening a port on the IGD device. | ||
184 | * (remote_host is usually NULL because IGDs don't support it.) | ||
185 | * | ||
186 | * @param control_url the control URL corresponding to service_type on the device | ||
187 | * @param service_type service type to call the command on | ||
188 | * @param ext_port port that should be opened on the WAN side | ||
189 | * @param in_port port on the gateway on the LAN side which should map ext_port | ||
190 | * @param in_client address in the LAN to which packets should be redirected | ||
191 | * @param desc description | ||
192 | * @param proto protocol for which to request port mapping | ||
193 | * @param remote_host remote host for which to request port mapping | ||
194 | * @param caller_cb function to call when done | ||
195 | * @param caller_cls closure passed to caller_cb | ||
196 | */ | ||
197 | void | ||
198 | UPNP_add_port_mapping_ (const char *control_url, const char *service_type, | ||
199 | const char *ext_port, | ||
200 | const char *in_port, | ||
201 | const char *in_client, | ||
202 | const char *desc, | ||
203 | const char *proto, const char *remote_host, | ||
204 | UPNP_port_mapping_cb_ caller_cb, void *caller_cls); | ||
205 | |||
206 | /** | ||
207 | * Request closing a a port on the IGD device that was previously opened | ||
208 | * using UPNP_add_port_mapping_(). Use the same argument values that were | ||
209 | * used when opening the port. | ||
210 | * (remote_host is usually NULL because IGDs don't support it.) | ||
211 | * | ||
212 | * @param control_url the control URL the command was called on | ||
213 | * @param service_type service the command was called on | ||
214 | * @param ext_port external port | ||
215 | * @param proto protocol for which port mapping was requested | ||
216 | * @param remote_host remote host for which port mapping was requested | ||
217 | * @param caller_cb function to call when done | ||
218 | * @param caller_cls closure passed to caller_cb | ||
219 | */ | ||
220 | void | ||
221 | UPNP_delete_port_mapping_ (const char *control_url, const char *service_type, | ||
222 | const char *ext_port, const char *proto, | ||
223 | const char *remote_host, | ||
224 | UPNP_port_mapping_cb_ caller_cb, void *caller_cls); | ||
225 | |||
226 | |||
227 | /** | ||
228 | * Callback to UPNP_get_specific_port_mapping_entry _(). | ||
229 | * | ||
230 | * @param error GNUNET_OK if port is currently mapped, another value on error | ||
231 | * @param control_url the control URL the command was called on | ||
232 | * @param service_type service the command was called on | ||
233 | * @param ext_port external port | ||
234 | * @param proto protocol for which port mapping was requested | ||
235 | * @param in_port port on the gateway on the LAN side which was requested | ||
236 | * @param in_client address in the LAN which was requested | ||
237 | * @param cls the closure passed to the command function | ||
238 | */ | ||
239 | typedef void (*UPNP_get_specific_port_mapping_entry_cb_) (int error, | ||
240 | const char | ||
241 | *control_url, | ||
242 | const char | ||
243 | *service_type, | ||
244 | const char | ||
245 | *ext_port, | ||
246 | const char *proto, | ||
247 | const char *in_port, | ||
248 | const char | ||
249 | *in_client, | ||
250 | void *cls); | ||
251 | |||
252 | /** | ||
253 | * Check that a port mapping set up with UPNP_add_port_mapping_() | ||
254 | * is alive. | ||
255 | * | ||
256 | * @param control_url the control URL the command was called on | ||
257 | * @param service_type service the command was called on | ||
258 | * @param ext_port external port | ||
259 | * @param proto protocol for which port mapping was requested | ||
260 | * @param caller_cb function to call when done | ||
261 | * @param caller_cls closure passed to caller_cb | ||
262 | */ | ||
263 | void | ||
264 | UPNP_get_specific_port_mapping_entry_ (const char *control_url, | ||
265 | const char *service_type, | ||
266 | const char *ext_port, | ||
267 | const char *proto, | ||
268 | UPNP_get_specific_port_mapping_entry_cb_ | ||
269 | caller_cb, void *caller_cls); | ||
270 | |||
271 | #endif | ||
diff --git a/src/nat/upnp-discover.c b/src/nat/upnp-discover.c deleted file mode 100644 index 2e609d790..000000000 --- a/src/nat/upnp-discover.c +++ /dev/null | |||
@@ -1,1270 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally based on the miniupnp library. | ||
23 | * Copyright (c) 2005-2009, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original license: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-discover.c | ||
53 | * @brief Look for UPnP IGD devices | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | #include <stdio.h> | ||
58 | #include <stdlib.h> | ||
59 | #include <string.h> | ||
60 | #include <curl/curl.h> | ||
61 | |||
62 | #include "platform.h" | ||
63 | #include "gnunet_util_lib.h" | ||
64 | #include "upnp-discover.h" | ||
65 | #include "upnp-reply-parse.h" | ||
66 | #include "upnp-igd-parse.h" | ||
67 | #include "upnp-minixml.h" | ||
68 | |||
69 | #define DISCOVER_BUFSIZE 512 | ||
70 | #define DESCRIPTION_BUFSIZE 2048 | ||
71 | #define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt(c, a, b); if (ret != CURLE_OK) GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret)); } while (0) | ||
72 | #define PRINT_SOCKET_ERROR(a) GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "UPnP", _("%s failed at %s:%d: '%s'\n"), a, __FILE__, __LINE__, strerror (errno)); | ||
73 | #define PRINT_SOCKET_ERROR_STR(a, b) GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "UPnP", _("%s failed at %s:%d: '%s' on `%s'\n"), a, __FILE__, __LINE__, strerror (errno), b); | ||
74 | |||
75 | /** | ||
76 | * Callback function called when download is finished. | ||
77 | * | ||
78 | * @param data the contents of the downloaded file, or NULL | ||
79 | * @param cls closure passed via download_device_description() | ||
80 | */ | ||
81 | typedef void (*download_cb) (char *data, void *cls); | ||
82 | |||
83 | /** | ||
84 | * Private closure used by download_device_description() and it's callbacks. | ||
85 | */ | ||
86 | struct download_cls | ||
87 | { | ||
88 | /** | ||
89 | * curl_easy handle. | ||
90 | */ | ||
91 | CURL *curl; | ||
92 | |||
93 | /** | ||
94 | * curl_multi handle. | ||
95 | */ | ||
96 | CURLM *multi; | ||
97 | |||
98 | /** | ||
99 | * URL of the file to download. | ||
100 | */ | ||
101 | char *url; | ||
102 | |||
103 | /** | ||
104 | * Time corresponding to timeout wanted by the caller. | ||
105 | */ | ||
106 | struct GNUNET_TIME_Absolute end_time; | ||
107 | |||
108 | /** | ||
109 | * Buffer to store downloaded content. | ||
110 | */ | ||
111 | char download_buffer[DESCRIPTION_BUFSIZE]; | ||
112 | |||
113 | /** | ||
114 | * Size of the already downloaded content. | ||
115 | */ | ||
116 | size_t download_pos; | ||
117 | |||
118 | /** | ||
119 | * User callback to trigger when done. | ||
120 | */ | ||
121 | download_cb caller_cb; | ||
122 | |||
123 | /** | ||
124 | * User closure to pass to caller_cb. | ||
125 | */ | ||
126 | void *caller_cls; | ||
127 | }; | ||
128 | |||
129 | /** | ||
130 | * Clean up the state of CURL multi handle and that of | ||
131 | * the only easy handle it uses. | ||
132 | */ | ||
133 | static void | ||
134 | download_clean_up (struct download_cls *cls) | ||
135 | { | ||
136 | CURLMcode mret; | ||
137 | |||
138 | mret = curl_multi_cleanup (cls->multi); | ||
139 | if (mret != CURLM_OK) | ||
140 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "UPnP", | ||
141 | _("%s failed at %s:%d: `%s'\n"), | ||
142 | "curl_multi_cleanup", __FILE__, __LINE__, | ||
143 | curl_multi_strerror (mret)); | ||
144 | |||
145 | curl_easy_cleanup (cls->curl); | ||
146 | GNUNET_free (cls); | ||
147 | } | ||
148 | |||
149 | /** | ||
150 | * Process downloaded bits by calling callback on each HELLO. | ||
151 | * | ||
152 | * @param ptr buffer with downloaded data | ||
153 | * @param size size of a record | ||
154 | * @param nmemb number of records downloaded | ||
155 | * @param ctx closure | ||
156 | * @return number of bytes that were processed (always size*nmemb) | ||
157 | */ | ||
158 | static size_t | ||
159 | callback_download (void *ptr, size_t size, size_t nmemb, void *ctx) | ||
160 | { | ||
161 | struct download_cls *cls = ctx; | ||
162 | const char *cbuf = ptr; | ||
163 | size_t total; | ||
164 | size_t cpy; | ||
165 | |||
166 | total = size * nmemb; | ||
167 | if (total == 0) | ||
168 | return total; /* ok, no data */ | ||
169 | |||
170 | cpy = GNUNET_MIN (total, DESCRIPTION_BUFSIZE - cls->download_pos - 1); | ||
171 | memcpy (&cls->download_buffer[cls->download_pos], cbuf, cpy); | ||
172 | cbuf += cpy; | ||
173 | cls->download_pos += cpy; | ||
174 | |||
175 | #if DEBUG_UPNP | ||
176 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
177 | "Downloaded %d records of size %d, download position: %d\n", | ||
178 | size, nmemb, cls->download_pos); | ||
179 | #endif | ||
180 | |||
181 | return total; | ||
182 | } | ||
183 | |||
184 | static void | ||
185 | task_download (void *cls, | ||
186 | const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
187 | |||
188 | /** | ||
189 | * Ask CURL for the select set and then schedule the | ||
190 | * receiving task with the scheduler. | ||
191 | */ | ||
192 | static void | ||
193 | download_prepare (struct download_cls *cls) | ||
194 | { | ||
195 | CURLMcode mret; | ||
196 | fd_set rs; | ||
197 | fd_set ws; | ||
198 | fd_set es; | ||
199 | int max; | ||
200 | struct GNUNET_NETWORK_FDSet *grs; | ||
201 | struct GNUNET_NETWORK_FDSet *gws; | ||
202 | long timeout; | ||
203 | struct GNUNET_TIME_Relative rtime; | ||
204 | |||
205 | max = -1; | ||
206 | FD_ZERO (&rs); | ||
207 | FD_ZERO (&ws); | ||
208 | FD_ZERO (&es); | ||
209 | mret = curl_multi_fdset (cls->multi, &rs, &ws, &es, &max); | ||
210 | if (mret != CURLM_OK) | ||
211 | { | ||
212 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "UPnP", | ||
213 | _("%s failed at %s:%d: `%s'\n"), | ||
214 | "curl_multi_fdset", __FILE__, __LINE__, | ||
215 | curl_multi_strerror (mret)); | ||
216 | download_clean_up (cls); | ||
217 | cls->caller_cb (NULL, cls->caller_cls); | ||
218 | return; | ||
219 | } | ||
220 | mret = curl_multi_timeout (cls->multi, &timeout); | ||
221 | if (mret != CURLM_OK) | ||
222 | { | ||
223 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "UPnP", | ||
224 | _("%s failed at %s:%d: `%s'\n"), | ||
225 | "curl_multi_timeout", __FILE__, __LINE__, | ||
226 | curl_multi_strerror (mret)); | ||
227 | download_clean_up (cls); | ||
228 | cls->caller_cb (NULL, cls->caller_cls); | ||
229 | return; | ||
230 | } | ||
231 | rtime = | ||
232 | GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining | ||
233 | (cls->end_time), | ||
234 | GNUNET_TIME_relative_multiply | ||
235 | (GNUNET_TIME_UNIT_MILLISECONDS, timeout)); | ||
236 | grs = GNUNET_NETWORK_fdset_create (); | ||
237 | gws = GNUNET_NETWORK_fdset_create (); | ||
238 | GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1); | ||
239 | GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1); | ||
240 | |||
241 | GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
242 | GNUNET_SCHEDULER_NO_TASK, | ||
243 | rtime, | ||
244 | grs, | ||
245 | gws, | ||
246 | & task_download, cls); | ||
247 | GNUNET_NETWORK_fdset_destroy (gws); | ||
248 | GNUNET_NETWORK_fdset_destroy (grs); | ||
249 | } | ||
250 | |||
251 | /** | ||
252 | * Task that is run when we are ready to receive more data from the device. | ||
253 | * | ||
254 | * @param cls closure | ||
255 | * @param tc task context | ||
256 | */ | ||
257 | static void | ||
258 | task_download (void *cls, | ||
259 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
260 | { | ||
261 | struct download_cls *dc = cls; | ||
262 | int running; | ||
263 | struct CURLMsg *msg; | ||
264 | CURLMcode mret; | ||
265 | |||
266 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
267 | { | ||
268 | #if DEBUG_UPNP | ||
269 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
270 | "Shutdown requested while trying to download device description from `%s'\n", | ||
271 | dc->url); | ||
272 | #endif | ||
273 | dc->caller_cb (NULL, dc->caller_cls); | ||
274 | download_clean_up (dc); | ||
275 | return; | ||
276 | } | ||
277 | if (GNUNET_TIME_absolute_get_remaining (dc->end_time).rel_value == 0) | ||
278 | { | ||
279 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "UPnP", | ||
280 | _ | ||
281 | ("Timeout trying to download UPnP device description from '%s'\n"), | ||
282 | dc->url); | ||
283 | dc->caller_cb (NULL, dc->caller_cls); | ||
284 | download_clean_up (dc); | ||
285 | return; | ||
286 | } | ||
287 | |||
288 | do | ||
289 | { | ||
290 | running = 0; | ||
291 | mret = curl_multi_perform (dc->multi, &running); | ||
292 | |||
293 | if (running == 0) | ||
294 | { | ||
295 | do | ||
296 | { | ||
297 | msg = curl_multi_info_read (dc->multi, &running); | ||
298 | GNUNET_break (msg != NULL); | ||
299 | if (msg == NULL) | ||
300 | break; | ||
301 | |||
302 | if ((msg->data.result != CURLE_OK) && | ||
303 | (msg->data.result != CURLE_GOT_NOTHING)) | ||
304 | { | ||
305 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
306 | _("%s failed for `%s' at %s:%d: `%s'\n"), | ||
307 | "curl_multi_perform", | ||
308 | dc->url, | ||
309 | __FILE__, | ||
310 | __LINE__, | ||
311 | curl_easy_strerror (msg->data.result)); | ||
312 | dc->caller_cb (NULL, dc->caller_cls); | ||
313 | } | ||
314 | else | ||
315 | { | ||
316 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
317 | _ | ||
318 | ("Download of device description `%s' completed.\n"), | ||
319 | dc->url); | ||
320 | dc->caller_cb (GNUNET_strdup (dc->download_buffer), | ||
321 | dc->caller_cls); | ||
322 | } | ||
323 | |||
324 | download_clean_up (dc); | ||
325 | return; | ||
326 | } | ||
327 | while ((running > 0)); | ||
328 | } | ||
329 | } | ||
330 | while (mret == CURLM_CALL_MULTI_PERFORM); | ||
331 | |||
332 | if (mret != CURLM_OK) | ||
333 | { | ||
334 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "UPnP", | ||
335 | _("%s failed at %s:%d: `%s'\n"), | ||
336 | "curl_multi_perform", __FILE__, __LINE__, | ||
337 | curl_multi_strerror (mret)); | ||
338 | download_clean_up (dc); | ||
339 | dc->caller_cb (NULL, dc->caller_cls); | ||
340 | } | ||
341 | |||
342 | download_prepare (dc); | ||
343 | } | ||
344 | |||
345 | |||
346 | /** | ||
347 | * Download description from devices. | ||
348 | * | ||
349 | * @param url URL of the file to download | ||
350 | * @param caller_cb user function to call when done | ||
351 | * @param caller_cls closure to pass to caller_cb | ||
352 | */ | ||
353 | void | ||
354 | download_device_description (char *url, download_cb caller_cb, | ||
355 | void *caller_cls) | ||
356 | { | ||
357 | CURL *curl; | ||
358 | CURLM *multi; | ||
359 | CURLcode ret; | ||
360 | CURLMcode mret; | ||
361 | struct download_cls *cls; | ||
362 | |||
363 | cls = GNUNET_malloc (sizeof (struct download_cls)); | ||
364 | |||
365 | curl = curl_easy_init (); | ||
366 | if (curl == NULL) | ||
367 | goto error; | ||
368 | |||
369 | CURL_EASY_SETOPT (curl, CURLOPT_WRITEFUNCTION, &callback_download); | ||
370 | if (ret != CURLE_OK) | ||
371 | goto error; | ||
372 | |||
373 | CURL_EASY_SETOPT (curl, CURLOPT_WRITEDATA, cls); | ||
374 | if (ret != CURLE_OK) | ||
375 | goto error; | ||
376 | |||
377 | CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1); | ||
378 | CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4); | ||
379 | /* no need to abort if the above failed */ | ||
380 | CURL_EASY_SETOPT (curl, CURLOPT_URL, url); | ||
381 | if (ret != CURLE_OK) | ||
382 | goto error; | ||
383 | |||
384 | CURL_EASY_SETOPT (curl, CURLOPT_FAILONERROR, 1); | ||
385 | CURL_EASY_SETOPT (curl, CURLOPT_BUFFERSIZE, DESCRIPTION_BUFSIZE); | ||
386 | CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet"); | ||
387 | CURL_EASY_SETOPT (curl, CURLOPT_CONNECTTIMEOUT, 60L); | ||
388 | CURL_EASY_SETOPT (curl, CURLOPT_TIMEOUT, 60L); | ||
389 | |||
390 | multi = curl_multi_init (); | ||
391 | if (multi == NULL) | ||
392 | { | ||
393 | GNUNET_break (0); | ||
394 | /* clean_up (); */ | ||
395 | return; | ||
396 | } | ||
397 | mret = curl_multi_add_handle (multi, curl); | ||
398 | if (mret != CURLM_OK) | ||
399 | { | ||
400 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "UPnP", | ||
401 | _("%s failed at %s:%d: `%s'\n"), | ||
402 | "curl_multi_add_handle", __FILE__, __LINE__, | ||
403 | curl_multi_strerror (mret)); | ||
404 | mret = curl_multi_cleanup (multi); | ||
405 | if (mret != CURLM_OK) | ||
406 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "UPnP", | ||
407 | _("%s failed at %s:%d: `%s'\n"), | ||
408 | "curl_multi_cleanup", __FILE__, __LINE__, | ||
409 | curl_multi_strerror (mret)); | ||
410 | goto error; | ||
411 | return; | ||
412 | } | ||
413 | |||
414 | #if DEBUG_UPNP | ||
415 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
416 | "Preparing to download device description from '%s'\n", | ||
417 | url); | ||
418 | #endif | ||
419 | |||
420 | cls->curl = curl; | ||
421 | cls->multi = multi; | ||
422 | cls->url = url; | ||
423 | cls->end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES); | ||
424 | memset (cls->download_buffer, 0, DESCRIPTION_BUFSIZE); | ||
425 | cls->download_pos = 0; | ||
426 | cls->caller_cb = caller_cb; | ||
427 | cls->caller_cls = caller_cls; | ||
428 | download_prepare (cls); | ||
429 | return; | ||
430 | |||
431 | |||
432 | error: | ||
433 | GNUNET_break (0); | ||
434 | GNUNET_free (cls); | ||
435 | curl_easy_cleanup (curl); | ||
436 | caller_cb (NULL, caller_cls); | ||
437 | } | ||
438 | |||
439 | /** | ||
440 | * Parse SSDP packet received in reply to a M-SEARCH message. | ||
441 | * | ||
442 | * @param reply contents of the packet | ||
443 | * @param size length of reply | ||
444 | * @param location address of a pointer that will be set to the start | ||
445 | * of the "location" field | ||
446 | * @param location_size pointer where to store the length of the "location" field | ||
447 | * @param st pointer address of a pointer that will be set to the start | ||
448 | * of the "st" (search target) field | ||
449 | * @param st_size pointer where to store the length of the "st" field | ||
450 | * The strings are NOT null terminated */ | ||
451 | static void | ||
452 | parse_msearch_reply (const char *reply, int size, | ||
453 | const char **location, int *location_size, | ||
454 | const char **st, int *st_size) | ||
455 | { | ||
456 | int a, b, i; | ||
457 | |||
458 | i = 0; | ||
459 | b = 0; | ||
460 | /* Start of the line */ | ||
461 | a = i; | ||
462 | |||
463 | while (i < size) | ||
464 | { | ||
465 | switch (reply[i]) | ||
466 | { | ||
467 | case ':': | ||
468 | if (b == 0) | ||
469 | /* End of the "header" */ | ||
470 | b = i; | ||
471 | break; | ||
472 | case '\x0a': | ||
473 | case '\x0d': | ||
474 | if (b != 0) | ||
475 | { | ||
476 | do | ||
477 | { | ||
478 | b++; | ||
479 | } | ||
480 | while (reply[b] == ' '); | ||
481 | |||
482 | if (0 == strncasecmp (reply + a, "location", 8)) | ||
483 | { | ||
484 | *location = reply + b; | ||
485 | *location_size = i - b; | ||
486 | } | ||
487 | else if (0 == strncasecmp (reply + a, "st", 2)) | ||
488 | { | ||
489 | *st = reply + b; | ||
490 | *st_size = i - b; | ||
491 | } | ||
492 | |||
493 | b = 0; | ||
494 | } | ||
495 | |||
496 | a = i + 1; | ||
497 | break; | ||
498 | default: | ||
499 | break; | ||
500 | } | ||
501 | |||
502 | i++; | ||
503 | } | ||
504 | } | ||
505 | |||
506 | /** | ||
507 | * Standard port for UPnP discovery (SSDP protocol). | ||
508 | */ | ||
509 | #define PORT 1900 | ||
510 | |||
511 | /** | ||
512 | * Convert a constant integer into a string. | ||
513 | */ | ||
514 | #define XSTR(s) STR(s) | ||
515 | #define STR(s) #s | ||
516 | |||
517 | /** | ||
518 | * Standard IPv4 multicast adress for UPnP discovery (SSDP protocol). | ||
519 | */ | ||
520 | #define UPNP_MCAST_ADDR "239.255.255.250" | ||
521 | |||
522 | /** | ||
523 | * Standard IPv6 multicast adress for UPnP discovery (SSDP protocol). | ||
524 | */ | ||
525 | #define UPNP_MCAST_ADDR6 "FF02:0:0:0:0:0:0:F" | ||
526 | |||
527 | /** | ||
528 | * Size of the buffer needed to store SSDP requests we send. | ||
529 | */ | ||
530 | #define UPNP_DISCOVER_BUFSIZE 1536 | ||
531 | |||
532 | /** | ||
533 | * Description of a UPnP device containing everything | ||
534 | * we may need to control it. | ||
535 | * | ||
536 | * Meant to be member of a chained list. | ||
537 | */ | ||
538 | struct UPNP_Dev_ | ||
539 | { | ||
540 | /** | ||
541 | * Next device in the list, if any. | ||
542 | */ | ||
543 | struct UPNP_Dev_ *pNext; | ||
544 | |||
545 | /** | ||
546 | * Path to the file describing the device. | ||
547 | */ | ||
548 | char *desc_url; | ||
549 | |||
550 | /** | ||
551 | * UPnP search target. | ||
552 | */ | ||
553 | char *st; | ||
554 | |||
555 | /** | ||
556 | * Service type associated with the control_url for the device. | ||
557 | */ | ||
558 | char *service_type; | ||
559 | |||
560 | /** | ||
561 | * URL to send commands to. | ||
562 | */ | ||
563 | char *control_url; | ||
564 | |||
565 | /** | ||
566 | * Whether the device is currently connected to the WAN. | ||
567 | */ | ||
568 | int is_connected; | ||
569 | |||
570 | /** | ||
571 | * IGD Data associated with the device. | ||
572 | */ | ||
573 | struct UPNP_IGD_Data_ *data; | ||
574 | }; | ||
575 | |||
576 | /** | ||
577 | * Private closure used by UPNP_discover() and its callbacks. | ||
578 | */ | ||
579 | struct UPNP_discover_cls | ||
580 | { | ||
581 | /** | ||
582 | * Remote address used for multicast emission and reception. | ||
583 | */ | ||
584 | struct sockaddr *multicast_addr; | ||
585 | |||
586 | /** | ||
587 | * Network handle used to send and receive discovery messages. | ||
588 | */ | ||
589 | struct GNUNET_NETWORK_Handle *sudp; | ||
590 | |||
591 | /** | ||
592 | * fdset used with sudp. | ||
593 | */ | ||
594 | struct GNUNET_NETWORK_FDSet *fdset; | ||
595 | |||
596 | /** | ||
597 | * Connection handle used to download device description. | ||
598 | */ | ||
599 | struct GNUNET_CONNECTION_Handle *s; | ||
600 | |||
601 | /** | ||
602 | * Transmission handle used with s. | ||
603 | */ | ||
604 | struct GNUNET_CONNECTION_TransmitHandle *th; | ||
605 | |||
606 | /** | ||
607 | * Index of the UPnP device type we're currently sending discovery messages to. | ||
608 | */ | ||
609 | int type_index; | ||
610 | |||
611 | /** | ||
612 | * List of discovered devices. | ||
613 | */ | ||
614 | struct UPNP_Dev_ *dev_list; | ||
615 | |||
616 | /** | ||
617 | * Device we're currently fetching description from. | ||
618 | */ | ||
619 | struct UPNP_Dev_ *current_dev; | ||
620 | |||
621 | /** | ||
622 | * User callback to trigger when done. | ||
623 | */ | ||
624 | UPNP_discover_cb_ caller_cb; | ||
625 | |||
626 | /** | ||
627 | * Closure passed to caller_cb. | ||
628 | */ | ||
629 | void *caller_cls; | ||
630 | }; | ||
631 | |||
632 | /** | ||
633 | * Check that raw_url is absolute, and if not, use ref_url to resolve it: | ||
634 | * if is_desc_file is GNUNET_YES, the path to the parent of the file is used; | ||
635 | * if it is GNUNET_NO, ref_url will be considered as the base URL for raw URL. | ||
636 | * | ||
637 | * @param ref_url base URL for the device | ||
638 | * @param is_desc_file whether ref_url is a path to the description file | ||
639 | * @param raw_url a possibly relative URL | ||
640 | * @returns a new string with an absolute URL | ||
641 | */ | ||
642 | static char * | ||
643 | get_absolute_url (const char *ref_url, int is_desc_file, const char *raw_url) | ||
644 | { | ||
645 | char *final_url; | ||
646 | |||
647 | if ((raw_url[0] == 'h') | ||
648 | && (raw_url[1] == 't') | ||
649 | && (raw_url[2] == 't') | ||
650 | && (raw_url[3] == 'p') | ||
651 | && (raw_url[4] == ':') && (raw_url[5] == '/') && (raw_url[6] == '/')) | ||
652 | { | ||
653 | final_url = GNUNET_strdup (raw_url); | ||
654 | } | ||
655 | else | ||
656 | { | ||
657 | int n = strlen (raw_url); | ||
658 | int l = strlen (ref_url); | ||
659 | int cpy_len = l; | ||
660 | char *slash; | ||
661 | |||
662 | /* If base URL is a path to the description file, go one level higher */ | ||
663 | if (is_desc_file == GNUNET_YES) | ||
664 | { | ||
665 | slash = strrchr (ref_url, '/'); | ||
666 | cpy_len = slash - ref_url; | ||
667 | } | ||
668 | |||
669 | final_url = GNUNET_malloc (l + n + 1); | ||
670 | |||
671 | /* Add trailing slash to base URL if needed */ | ||
672 | if (raw_url[0] != '/' && ref_url[cpy_len] != '\0') | ||
673 | final_url[cpy_len++] = '/'; | ||
674 | |||
675 | strncpy (final_url, ref_url, cpy_len); | ||
676 | strcpy (final_url + cpy_len, raw_url); | ||
677 | final_url[cpy_len + n] = '\0'; | ||
678 | } | ||
679 | |||
680 | return final_url; | ||
681 | } | ||
682 | |||
683 | |||
684 | /** | ||
685 | * Construct control URL and service type for device from its description URL | ||
686 | * and UPNP_IGD_Data_ information. This involves resolving relative paths | ||
687 | * and choosing between Common Interface Config and interface-specific | ||
688 | * paths. | ||
689 | * | ||
690 | * @param desc_url URL to the description file of the device | ||
691 | * @param data IGD information obtained from the description file | ||
692 | * @param control_url place to store a URL to control the IGD device (will be | ||
693 | * the empty string in case of failure) | ||
694 | * @param service_type place to store the service type corresponding to control_url | ||
695 | * (will be the empty string in case of failure) | ||
696 | */ | ||
697 | static void | ||
698 | format_control_urls (const char *desc_url, struct UPNP_IGD_Data_ *data, char **control_url, char **service_type) | ||
699 | { | ||
700 | const char *ref_url; | ||
701 | int is_desc_file; | ||
702 | |||
703 | if (data->base_url[0] != '\0') | ||
704 | { | ||
705 | ref_url = data->base_url; | ||
706 | is_desc_file = GNUNET_NO; | ||
707 | } | ||
708 | else | ||
709 | { | ||
710 | ref_url = desc_url; | ||
711 | is_desc_file = GNUNET_YES; | ||
712 | } | ||
713 | |||
714 | if (data->control_url[0] != '\0') | ||
715 | { | ||
716 | *control_url = get_absolute_url (ref_url, is_desc_file, data->control_url); | ||
717 | *service_type = GNUNET_strdup (data->service_type); | ||
718 | } | ||
719 | else if (data->control_url_CIF[0] != '\0') | ||
720 | { | ||
721 | *control_url = get_absolute_url (ref_url, is_desc_file, data->control_url_CIF); | ||
722 | *service_type = GNUNET_strdup (data->service_type_CIF); | ||
723 | } | ||
724 | else | ||
725 | { | ||
726 | /* If no suitable URL-service type pair was found, set both to empty | ||
727 | * to avoid pretending things will work */ | ||
728 | *control_url = GNUNET_strdup (""); | ||
729 | *service_type = GNUNET_strdup (""); | ||
730 | } | ||
731 | } | ||
732 | |||
733 | static void get_valid_igd (struct UPNP_discover_cls *cls); | ||
734 | |||
735 | /** | ||
736 | * Called when "GetStatusInfo" command finishes. Check whether IGD device reports | ||
737 | * to be currently connected or not. | ||
738 | * | ||
739 | * @param response content of the UPnP message answered by the device | ||
740 | * @param received number of received bytes stored in response | ||
741 | * @param data closure from UPNP_discover() | ||
742 | */ | ||
743 | static void | ||
744 | get_valid_igd_connected_cb (char *response, size_t received, void *data) | ||
745 | { | ||
746 | struct UPNP_discover_cls *cls = data; | ||
747 | struct UPNP_REPLY_NameValueList_ pdata; | ||
748 | char *status; | ||
749 | char *error; | ||
750 | |||
751 | UPNP_REPLY_parse_ (response, received, &pdata); | ||
752 | |||
753 | status = UPNP_REPLY_get_value_ (&pdata, "NewConnectionStatus"); | ||
754 | error = UPNP_REPLY_get_value_ (&pdata, "errorCode"); | ||
755 | |||
756 | if (status) | ||
757 | cls->current_dev->is_connected = (strcmp ("Connected", status) == 0); | ||
758 | else | ||
759 | cls->current_dev->is_connected = GNUNET_NO; | ||
760 | |||
761 | if (error) | ||
762 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "UPnP", | ||
763 | _("Could not get UPnP device status: error %s\n"), | ||
764 | error); | ||
765 | |||
766 | GNUNET_free (response); | ||
767 | UPNP_REPLY_free_ (&pdata); | ||
768 | |||
769 | /* Go on to next device, or finish discovery process */ | ||
770 | cls->current_dev = cls->current_dev->pNext; | ||
771 | get_valid_igd (cls); | ||
772 | } | ||
773 | |||
774 | /** | ||
775 | * Receive contents of the downloaded UPnP IGD description file, | ||
776 | * and fill UPNP_Dev_ and UPNP_IGD_Data_ structs with this data. | ||
777 | * Then, schedule UPnP command to check whether device is connected. | ||
778 | * | ||
779 | * @param desc UPnP IGD description (in XML) | ||
780 | * @param data closure from UPNP_discover() | ||
781 | */ | ||
782 | static void | ||
783 | get_valid_igd_receive (char *desc, void *data) | ||
784 | { | ||
785 | struct UPNP_discover_cls *cls = data; | ||
786 | struct UPNP_IGD_Data_ *igd_data; | ||
787 | char *buffer; | ||
788 | |||
789 | if (!desc || strlen (desc) == 0) | ||
790 | { | ||
791 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "UPnP", | ||
792 | "Error getting IGD XML description at %s:%d\n", | ||
793 | __FILE__, __LINE__); | ||
794 | |||
795 | /* Skip device */ | ||
796 | cls->current_dev->data = NULL; | ||
797 | cls->current_dev->is_connected = GNUNET_NO; | ||
798 | get_valid_igd (cls); | ||
799 | } | ||
800 | |||
801 | igd_data = GNUNET_malloc (sizeof (struct UPNP_IGD_Data_)); | ||
802 | memset (igd_data, 0, sizeof (struct UPNP_IGD_Data_)); | ||
803 | UPNP_IGD_parse_desc_ (desc, strlen (desc), igd_data); | ||
804 | |||
805 | format_control_urls (cls->current_dev->desc_url, igd_data, | ||
806 | &cls->current_dev->control_url, | ||
807 | &cls->current_dev->service_type); | ||
808 | |||
809 | cls->current_dev->data = igd_data; | ||
810 | |||
811 | /* Check whether device is connected */ | ||
812 | buffer = GNUNET_malloc (UPNP_COMMAND_BUFSIZE); | ||
813 | UPNP_command_ (cls->current_dev->control_url, | ||
814 | cls->current_dev->data->service_type, | ||
815 | "GetStatusInfo", NULL, buffer, UPNP_COMMAND_BUFSIZE, | ||
816 | get_valid_igd_connected_cb, cls); | ||
817 | |||
818 | GNUNET_free (desc); | ||
819 | } | ||
820 | |||
821 | /** | ||
822 | * Free a chained list of UPnP devices. | ||
823 | */ | ||
824 | static void | ||
825 | free_dev_list (struct UPNP_Dev_ *devlist) | ||
826 | { | ||
827 | struct UPNP_Dev_ *next; | ||
828 | |||
829 | while (devlist) | ||
830 | { | ||
831 | next = devlist->pNext; | ||
832 | GNUNET_free (devlist->control_url); | ||
833 | GNUNET_free (devlist->service_type); | ||
834 | GNUNET_free (devlist->desc_url); | ||
835 | GNUNET_free (devlist->data); | ||
836 | GNUNET_free (devlist->st); | ||
837 | GNUNET_free (devlist); | ||
838 | devlist = next; | ||
839 | } | ||
840 | } | ||
841 | |||
842 | /** | ||
843 | * Walk over the list of found devices looking for a connected IGD, | ||
844 | * if present, or at least a disconnected one. | ||
845 | */ | ||
846 | static void | ||
847 | get_valid_igd (struct UPNP_discover_cls *cls) | ||
848 | { | ||
849 | struct UPNP_Dev_ *dev; | ||
850 | int step; | ||
851 | |||
852 | /* No device was discovered */ | ||
853 | if (!cls->dev_list) | ||
854 | { | ||
855 | cls->caller_cb (NULL, NULL, cls->caller_cls); | ||
856 | |||
857 | GNUNET_free (cls); | ||
858 | return; | ||
859 | } | ||
860 | /* We already walked over all devices, see what we got, | ||
861 | * and return the device with the best state we have. */ | ||
862 | else if (cls->current_dev == NULL) | ||
863 | { | ||
864 | for (step = 1; step <= 3; step++) | ||
865 | { | ||
866 | for (dev = cls->dev_list; dev; dev = dev->pNext) | ||
867 | { | ||
868 | #if DEBUG_UPNP | ||
869 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
870 | "Found device: control_url: %s, service_type: %s\n", | ||
871 | dev->control_url, dev->service_type); | ||
872 | #endif | ||
873 | /* Accept connected IGDs on step 1, non-connected IGDs | ||
874 | * on step 2, and other device types on step 3. */ | ||
875 | if ((step == 1 && dev->is_connected) | ||
876 | || (step < 3 && 0 != strcmp (dev->service_type, | ||
877 | "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1"))) | ||
878 | continue; | ||
879 | |||
880 | cls->caller_cb (dev->control_url, | ||
881 | dev->service_type, cls->caller_cls); | ||
882 | |||
883 | free_dev_list (cls->dev_list); | ||
884 | GNUNET_free (cls); | ||
885 | return; | ||
886 | } | ||
887 | } | ||
888 | |||
889 | /* We cannot reach this... */ | ||
890 | GNUNET_assert (GNUNET_NO); | ||
891 | } | ||
892 | |||
893 | /* There are still devices to ask, go on */ | ||
894 | download_device_description (cls->current_dev->desc_url, | ||
895 | get_valid_igd_receive, cls); | ||
896 | } | ||
897 | |||
898 | static const char *const discover_type_list[] = { | ||
899 | "urn:schemas-upnp-org:device:InternetGatewayDevice:1", | ||
900 | "urn:schemas-upnp-org:service:WANIPConnection:1", | ||
901 | "urn:schemas-upnp-org:service:WANPPPConnection:1", | ||
902 | NULL | ||
903 | }; | ||
904 | |||
905 | static void | ||
906 | discover_send (void *data, const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
907 | |||
908 | /** | ||
909 | * Handle response from device. Stop when all device types have been tried, | ||
910 | * and get their descriptions. | ||
911 | * | ||
912 | * @param data closure from UPNP_discover() | ||
913 | * @param tc task context | ||
914 | */ | ||
915 | static void | ||
916 | discover_recv (void *data, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
917 | { | ||
918 | struct UPNP_discover_cls *cls = data; | ||
919 | GNUNET_SCHEDULER_TaskIdentifier task_w; | ||
920 | struct UPNP_Dev_ *tmp; | ||
921 | socklen_t addrlen; | ||
922 | ssize_t received; | ||
923 | char buf[DISCOVER_BUFSIZE]; | ||
924 | const char *desc_url = NULL; | ||
925 | int urlsize = 0; | ||
926 | const char *st = NULL; | ||
927 | int stsize = 0; | ||
928 | |||
929 | /* Free fdset that was used for this sned/receive operation */ | ||
930 | GNUNET_NETWORK_fdset_destroy (cls->fdset); | ||
931 | |||
932 | if (cls->multicast_addr->sa_family == AF_INET) | ||
933 | addrlen = sizeof (struct sockaddr_in); | ||
934 | else | ||
935 | addrlen = sizeof (struct sockaddr_in6); | ||
936 | |||
937 | errno = 0; | ||
938 | received = | ||
939 | GNUNET_NETWORK_socket_recvfrom (cls->sudp, &buf, DISCOVER_BUFSIZE - 1, | ||
940 | (struct sockaddr *) cls->multicast_addr, | ||
941 | &addrlen); | ||
942 | if (received == GNUNET_SYSERR) | ||
943 | { | ||
944 | if (errno != EAGAIN) | ||
945 | PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_recvfrom"); | ||
946 | } | ||
947 | #if DEBUG_UPNP | ||
948 | else | ||
949 | { | ||
950 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
951 | "Received %d bytes from %s\n", received, | ||
952 | GNUNET_a2s (cls->multicast_addr, addrlen)); | ||
953 | } | ||
954 | #endif | ||
955 | |||
956 | parse_msearch_reply (buf, received, &desc_url, &urlsize, &st, &stsize); | ||
957 | |||
958 | if (st && desc_url) | ||
959 | { | ||
960 | tmp = (struct UPNP_Dev_ *) GNUNET_malloc (sizeof (struct UPNP_Dev_)); | ||
961 | tmp->pNext = cls->dev_list; | ||
962 | |||
963 | tmp->desc_url = GNUNET_malloc (urlsize + 1); | ||
964 | strncpy (tmp->desc_url, desc_url, urlsize); | ||
965 | tmp->desc_url[urlsize] = '\0'; | ||
966 | |||
967 | tmp->st = GNUNET_malloc (stsize + 1); | ||
968 | strncpy (tmp->st, st, stsize); | ||
969 | tmp->st[stsize] = '\0'; | ||
970 | cls->dev_list = tmp; | ||
971 | #if DEBUG_UPNP | ||
972 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
973 | "Found device %s when looking for type %s\n", | ||
974 | tmp->desc_url, tmp->st); | ||
975 | #endif | ||
976 | } | ||
977 | |||
978 | /* Continue discovery until all types of devices have been tried */ | ||
979 | if (discover_type_list[cls->type_index]) | ||
980 | { | ||
981 | /* Send queries for each device type and wait for a possible reply. | ||
982 | * receiver callback takes care of trying another device type, | ||
983 | * and eventually calls the caller's callback. */ | ||
984 | cls->fdset = GNUNET_NETWORK_fdset_create (); | ||
985 | GNUNET_NETWORK_fdset_zero (cls->fdset); | ||
986 | GNUNET_NETWORK_fdset_set (cls->fdset, cls->sudp); | ||
987 | |||
988 | task_w = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
989 | GNUNET_SCHEDULER_NO_TASK, | ||
990 | GNUNET_TIME_relative_multiply | ||
991 | (GNUNET_TIME_UNIT_SECONDS, 15), | ||
992 | NULL, cls->fdset, &discover_send, | ||
993 | cls); | ||
994 | |||
995 | GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
996 | task_w, | ||
997 | GNUNET_TIME_relative_multiply | ||
998 | (GNUNET_TIME_UNIT_SECONDS, 5), cls->fdset, | ||
999 | NULL, &discover_recv, cls); | ||
1000 | } | ||
1001 | else | ||
1002 | { | ||
1003 | GNUNET_NETWORK_socket_close (cls->sudp); | ||
1004 | GNUNET_free (cls->multicast_addr); | ||
1005 | cls->current_dev = cls->dev_list; | ||
1006 | get_valid_igd (cls); | ||
1007 | } | ||
1008 | } | ||
1009 | |||
1010 | /** | ||
1011 | * Send the SSDP M-SEARCH packet. | ||
1012 | * | ||
1013 | * @param data closure from UPNP_discover() | ||
1014 | * @param tc task context | ||
1015 | */ | ||
1016 | static void | ||
1017 | discover_send (void *data, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
1018 | { | ||
1019 | struct UPNP_discover_cls *cls = data; | ||
1020 | socklen_t addrlen; | ||
1021 | ssize_t n, sent; | ||
1022 | char buf[DISCOVER_BUFSIZE]; | ||
1023 | static const char msearch_msg[] = | ||
1024 | "M-SEARCH * HTTP/1.1\r\n" | ||
1025 | "HOST: " UPNP_MCAST_ADDR ":" XSTR (PORT) "\r\n" | ||
1026 | "ST: %s\r\n" "MAN: \"ssdp:discover\"\r\n" "MX: 3\r\n" "\r\n"; | ||
1027 | |||
1028 | if (cls->multicast_addr->sa_family == AF_INET) | ||
1029 | addrlen = sizeof (struct sockaddr_in); | ||
1030 | else | ||
1031 | addrlen = sizeof (struct sockaddr_in6); | ||
1032 | |||
1033 | n = | ||
1034 | snprintf (buf, DISCOVER_BUFSIZE, msearch_msg, | ||
1035 | discover_type_list[cls->type_index++]); | ||
1036 | |||
1037 | errno = 0; | ||
1038 | sent = GNUNET_NETWORK_socket_sendto (cls->sudp, buf, n, | ||
1039 | (struct sockaddr *) | ||
1040 | cls->multicast_addr, addrlen); | ||
1041 | if (sent == GNUNET_SYSERR) | ||
1042 | { | ||
1043 | PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_sendto"); | ||
1044 | } | ||
1045 | else if (sent < n) | ||
1046 | { | ||
1047 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
1048 | "Could only send %d bytes to %s, needed %d bytes\n", | ||
1049 | sent, GNUNET_a2s (cls->multicast_addr, addrlen), n); | ||
1050 | } | ||
1051 | #if DEBUG_UPNP | ||
1052 | else | ||
1053 | { | ||
1054 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
1055 | "Sent %d bytes to %s\n", sent, | ||
1056 | GNUNET_a2s (cls->multicast_addr, addrlen)); | ||
1057 | } | ||
1058 | #endif | ||
1059 | } | ||
1060 | |||
1061 | /** | ||
1062 | * Search for UPnP Internet Gateway Devices (IGD) on a given network interface. | ||
1063 | * If several devices are found, a device that is connected to the WAN | ||
1064 | * is returned first (if any). | ||
1065 | * | ||
1066 | * @param multicastif network interface to send discovery messages, or NULL | ||
1067 | * @param addr address used to send messages on multicastif, or NULL | ||
1068 | * @param caller_cb user function to call when done | ||
1069 | * @param caller_cls closure to pass to caller_cb | ||
1070 | */ | ||
1071 | void | ||
1072 | UPNP_discover_ (const char *multicastif, | ||
1073 | const struct sockaddr *addr, | ||
1074 | UPNP_discover_cb_ caller_cb, void *caller_cls) | ||
1075 | { | ||
1076 | int opt = 1; | ||
1077 | int domain = PF_INET; | ||
1078 | int if_index; | ||
1079 | struct in6_addr any_addr = IN6ADDR_ANY_INIT; | ||
1080 | struct sockaddr_in sockudp_r, sockudp_w; | ||
1081 | struct sockaddr_in6 sockudp6_r, sockudp6_w; | ||
1082 | GNUNET_SCHEDULER_TaskIdentifier task_w; | ||
1083 | struct GNUNET_NETWORK_Handle *sudp; | ||
1084 | struct UPNP_discover_cls *cls; | ||
1085 | |||
1086 | |||
1087 | if (addr && addr->sa_family == AF_INET) | ||
1088 | { | ||
1089 | domain = PF_INET; | ||
1090 | } | ||
1091 | else if (addr && addr->sa_family == AF_INET6) | ||
1092 | { | ||
1093 | domain = PF_INET6; | ||
1094 | } | ||
1095 | else if (addr) | ||
1096 | { | ||
1097 | GNUNET_break (0); | ||
1098 | caller_cb (NULL, NULL, caller_cls); | ||
1099 | return; | ||
1100 | } | ||
1101 | |||
1102 | errno = 0; | ||
1103 | sudp = GNUNET_NETWORK_socket_create (domain, SOCK_DGRAM, 0); | ||
1104 | |||
1105 | if (sudp == NULL) | ||
1106 | { | ||
1107 | PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_create"); | ||
1108 | caller_cb (NULL, NULL, caller_cls); | ||
1109 | return; | ||
1110 | } | ||
1111 | |||
1112 | |||
1113 | cls = GNUNET_malloc (sizeof (struct UPNP_discover_cls)); | ||
1114 | cls->sudp = sudp; | ||
1115 | cls->type_index = 0; | ||
1116 | cls->dev_list = NULL; | ||
1117 | cls->current_dev = NULL; | ||
1118 | cls->caller_cb = caller_cb; | ||
1119 | cls->caller_cls = caller_cls; | ||
1120 | |||
1121 | |||
1122 | if (domain == PF_INET) | ||
1123 | { | ||
1124 | /* receive */ | ||
1125 | memset (&sockudp_r, 0, sizeof (struct sockaddr_in)); | ||
1126 | sockudp_r.sin_family = AF_INET; | ||
1127 | #ifdef HAVE_SOCKADDR_IN_SIN_LEN | ||
1128 | sockudp_r.sin_len = sizeof (struct sockaddr_in); | ||
1129 | #endif | ||
1130 | sockudp_r.sin_port = 0; | ||
1131 | sockudp_r.sin_addr.s_addr = INADDR_ANY; | ||
1132 | |||
1133 | /* send */ | ||
1134 | memset (&sockudp_w, 0, sizeof (struct sockaddr_in)); | ||
1135 | sockudp_w.sin_family = AF_INET; | ||
1136 | sockudp_w.sin_port = htons (PORT); | ||
1137 | sockudp_w.sin_addr.s_addr = inet_addr (UPNP_MCAST_ADDR); | ||
1138 | #ifdef HAVE_SOCKADDR_IN_SIN_LEN | ||
1139 | sockudp_w.sin_len = sizeof (struct sockaddr_in); | ||
1140 | #endif | ||
1141 | |||
1142 | cls->multicast_addr = GNUNET_malloc (sizeof (struct sockaddr_in)); | ||
1143 | memcpy (cls->multicast_addr, &sockudp_w, sizeof (struct sockaddr_in)); | ||
1144 | } | ||
1145 | else | ||
1146 | { | ||
1147 | /* receive */ | ||
1148 | memcpy (&sockudp6_r, addr, sizeof (struct sockaddr_in6)); | ||
1149 | sockudp6_r.sin6_port = 0; | ||
1150 | sockudp6_r.sin6_addr = any_addr; | ||
1151 | #ifdef HAVE_SOCKADDR_IN_SIN_LEN | ||
1152 | sockudp6_r.sin6_len = sizeof (struct sockaddr_in6); | ||
1153 | #endif | ||
1154 | |||
1155 | /* send */ | ||
1156 | memset (&sockudp6_w, 0, sizeof (struct sockaddr_in6)); | ||
1157 | sockudp6_w.sin6_family = AF_INET6; | ||
1158 | sockudp6_w.sin6_port = htons (PORT); | ||
1159 | if (inet_pton (AF_INET6, UPNP_MCAST_ADDR6, &sockudp6_w.sin6_addr) != 1) | ||
1160 | { | ||
1161 | PRINT_SOCKET_ERROR ("inet_pton"); | ||
1162 | caller_cb (NULL, NULL, caller_cls); | ||
1163 | return; | ||
1164 | } | ||
1165 | #ifdef HAVE_SOCKADDR_IN_SIN_LEN | ||
1166 | sockudp6_w.sin6_len = sizeof (struct sockaddr_in6); | ||
1167 | #endif | ||
1168 | |||
1169 | cls->multicast_addr = GNUNET_malloc (sizeof (struct sockaddr_in6)); | ||
1170 | memcpy (cls->multicast_addr, &sockudp6_w, sizeof (struct sockaddr_in6)); | ||
1171 | } | ||
1172 | |||
1173 | if (GNUNET_NETWORK_socket_setsockopt | ||
1174 | (sudp, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) == GNUNET_SYSERR) | ||
1175 | { | ||
1176 | PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_setsockopt"); | ||
1177 | GNUNET_NETWORK_socket_close (sudp); | ||
1178 | caller_cb (NULL, NULL, caller_cls); | ||
1179 | return; | ||
1180 | } | ||
1181 | |||
1182 | if (addr) | ||
1183 | { | ||
1184 | if (domain == PF_INET) | ||
1185 | { | ||
1186 | sockudp_r.sin_addr.s_addr = | ||
1187 | ((struct sockaddr_in *) addr)->sin_addr.s_addr; | ||
1188 | if (GNUNET_NETWORK_socket_setsockopt | ||
1189 | (sudp, IPPROTO_IP, IP_MULTICAST_IF, | ||
1190 | (const char *) &sockudp_r.sin_addr, | ||
1191 | sizeof (struct in_addr)) == GNUNET_SYSERR) | ||
1192 | { | ||
1193 | PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_setsockopt"); | ||
1194 | } | ||
1195 | } | ||
1196 | else | ||
1197 | { | ||
1198 | if (multicastif) | ||
1199 | { | ||
1200 | #ifndef MINGW | ||
1201 | if_index = if_nametoindex (multicastif); | ||
1202 | #else | ||
1203 | // FIXME | ||
1204 | if_index = 0; | ||
1205 | #endif | ||
1206 | if (!if_index) | ||
1207 | PRINT_SOCKET_ERROR_STR ("if_nametoindex", multicastif); | ||
1208 | |||
1209 | if (GNUNET_NETWORK_socket_setsockopt | ||
1210 | (sudp, IPPROTO_IPV6, IPV6_MULTICAST_IF, &if_index, | ||
1211 | sizeof (if_index)) == GNUNET_SYSERR) | ||
1212 | { | ||
1213 | PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_setsockopt"); | ||
1214 | } | ||
1215 | } | ||
1216 | |||
1217 | memcpy (&sockudp6_r.sin6_addr, | ||
1218 | &((struct sockaddr_in6 *) addr)->sin6_addr, | ||
1219 | sizeof (sockudp6_r.sin6_addr)); | ||
1220 | } | ||
1221 | } | ||
1222 | |||
1223 | if (domain == PF_INET) | ||
1224 | { | ||
1225 | /* Bind to receive response before sending packet */ | ||
1226 | if (GNUNET_NETWORK_socket_bind | ||
1227 | (sudp, (struct sockaddr *) &sockudp_r, | ||
1228 | sizeof (struct sockaddr_in)) != GNUNET_OK) | ||
1229 | { | ||
1230 | PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_bind"); | ||
1231 | GNUNET_NETWORK_socket_close (sudp); | ||
1232 | GNUNET_free (cls->multicast_addr); | ||
1233 | caller_cb (NULL, NULL, caller_cls); | ||
1234 | return; | ||
1235 | } | ||
1236 | } | ||
1237 | else | ||
1238 | { | ||
1239 | /* Bind to receive response before sending packet */ | ||
1240 | if (GNUNET_NETWORK_socket_bind | ||
1241 | (sudp, (struct sockaddr *) &sockudp6_r, | ||
1242 | sizeof (struct sockaddr_in6)) != GNUNET_OK) | ||
1243 | { | ||
1244 | PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_bind"); | ||
1245 | GNUNET_free (cls->multicast_addr); | ||
1246 | GNUNET_NETWORK_socket_close (sudp); | ||
1247 | caller_cb (NULL, NULL, caller_cls); | ||
1248 | return; | ||
1249 | } | ||
1250 | } | ||
1251 | |||
1252 | /* Send queries for each device type and wait for a possible reply. | ||
1253 | * receiver callback takes care of trying another device type, | ||
1254 | * and eventually calls the caller's callback. */ | ||
1255 | cls->fdset = GNUNET_NETWORK_fdset_create (); | ||
1256 | GNUNET_NETWORK_fdset_zero (cls->fdset); | ||
1257 | GNUNET_NETWORK_fdset_set (cls->fdset, sudp); | ||
1258 | |||
1259 | task_w = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
1260 | GNUNET_SCHEDULER_NO_TASK, | ||
1261 | GNUNET_TIME_relative_multiply | ||
1262 | (GNUNET_TIME_UNIT_SECONDS, 15), NULL, | ||
1263 | cls->fdset, &discover_send, cls); | ||
1264 | |||
1265 | GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
1266 | task_w, | ||
1267 | GNUNET_TIME_relative_multiply | ||
1268 | (GNUNET_TIME_UNIT_SECONDS, 15), cls->fdset, | ||
1269 | NULL, &discover_recv, cls); | ||
1270 | } | ||
diff --git a/src/nat/upnp-discover.h b/src/nat/upnp-discover.h deleted file mode 100644 index 39b704bcf..000000000 --- a/src/nat/upnp-discover.h +++ /dev/null | |||
@@ -1,82 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally based on the miniupnp library. | ||
23 | * Copyright (c) 2005-2009, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-discover.h | ||
53 | * @brief Look for UPnP IGD devices | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | #ifndef UPNPC_H | ||
58 | #define UPNPC_H | ||
59 | |||
60 | #include "platform.h" | ||
61 | #include "gnunet_common.h" | ||
62 | #include "gnunet_util_lib.h" | ||
63 | #include "upnp-commands.h" | ||
64 | |||
65 | typedef void (*UPNP_discover_cb_) (const char *control_urls, | ||
66 | const char *service_type, void *cls); | ||
67 | |||
68 | /** | ||
69 | * Search for UPnP Internet Gateway Devices (IGD) on a given network interface. | ||
70 | * If several devices are found, a device that is connected to the WAN | ||
71 | * is returned first (if any). | ||
72 | * | ||
73 | * @param multicastif network interface to send discovery messages, or NULL | ||
74 | * @param addr address used to send messages on multicastif, or NULL | ||
75 | * @param caller_cb user function to call when done | ||
76 | * @param caller_cls closure to pass to caller_cb | ||
77 | */ | ||
78 | void UPNP_discover_ (const char *multicastif, | ||
79 | const struct sockaddr *addr, | ||
80 | UPNP_discover_cb_ caller_cb, void *caller_cls); | ||
81 | |||
82 | #endif | ||
diff --git a/src/nat/upnp-igd-parse.c b/src/nat/upnp-igd-parse.c deleted file mode 100644 index 0812065ed..000000000 --- a/src/nat/upnp-igd-parse.c +++ /dev/null | |||
@@ -1,207 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally based on the miniupnp library. | ||
23 | * Copyright (c) 2005-2008, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-igd-parse.h | ||
53 | * @brief Parser for XML descriptions of UPnP Internet Gateway Devices | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | #include <stdio.h> | ||
58 | #include <string.h> | ||
59 | |||
60 | #include "platform.h" | ||
61 | #include "gnunet_util_lib.h" | ||
62 | #include "upnp-minixml.h" | ||
63 | #include "upnp-igd-parse.h" | ||
64 | |||
65 | /** | ||
66 | * Start element handler: update nesting level counter | ||
67 | * and copy element name. | ||
68 | */ | ||
69 | static void | ||
70 | start_elt (void *d, const char *name, int l) | ||
71 | { | ||
72 | struct UPNP_IGD_Data_ *datas = (struct UPNP_IGD_Data_ *) d; | ||
73 | |||
74 | memcpy (datas->cur_elt_name, name, l); | ||
75 | datas->cur_elt_name[l] = '\0'; | ||
76 | datas->level++; | ||
77 | if ((l == 7) && !memcmp (name, "service", l)) | ||
78 | { | ||
79 | datas->control_url_tmp[0] = '\0'; | ||
80 | datas->event_sub_url_tmp[0] = '\0'; | ||
81 | datas->scpd_url_tmp[0] = '\0'; | ||
82 | datas->service_type_tmp[0] = '\0'; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * End element handler: update nesting level counter | ||
88 | * and update parser state if service element is parsed. | ||
89 | */ | ||
90 | static void | ||
91 | end_elt (void *d, const char *name, int l) | ||
92 | { | ||
93 | struct UPNP_IGD_Data_ *datas = (struct UPNP_IGD_Data_ *) d; | ||
94 | |||
95 | datas->level--; | ||
96 | |||
97 | if ((l == 7) && !memcmp (name, "service", l)) | ||
98 | { | ||
99 | if (0 == strcmp (datas->service_type_tmp, | ||
100 | "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1")) | ||
101 | { | ||
102 | memcpy (datas->control_url_CIF, datas->control_url_tmp, | ||
103 | MINIUPNPC_URL_MAXSIZE); | ||
104 | memcpy (datas->event_sub_url_CIF, datas->event_sub_url_tmp, | ||
105 | MINIUPNPC_URL_MAXSIZE); | ||
106 | memcpy (datas->scpd_url_CIF, datas->scpd_url_tmp, | ||
107 | MINIUPNPC_URL_MAXSIZE); | ||
108 | memcpy (datas->service_type_CIF, datas->service_type_tmp, | ||
109 | MINIUPNPC_URL_MAXSIZE); | ||
110 | } | ||
111 | else if (0 == strcmp (datas->service_type_tmp, | ||
112 | "urn:schemas-upnp-org:service:WANIPConnection:1") | ||
113 | || 0 == strcmp (datas->service_type_tmp, | ||
114 | "urn:schemas-upnp-org:service:WANPPPConnection:1")) | ||
115 | { | ||
116 | memcpy (datas->control_url, datas->control_url_tmp, | ||
117 | MINIUPNPC_URL_MAXSIZE); | ||
118 | memcpy (datas->event_sub_url, datas->event_sub_url_tmp, | ||
119 | MINIUPNPC_URL_MAXSIZE); | ||
120 | memcpy (datas->scpd_url, datas->scpd_url_tmp, | ||
121 | MINIUPNPC_URL_MAXSIZE); | ||
122 | memcpy (datas->service_type, datas->service_type_tmp, | ||
123 | MINIUPNPC_URL_MAXSIZE); | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * Data handler: copy data depending on the current | ||
130 | * element name and state. | ||
131 | */ | ||
132 | static void | ||
133 | IGDdata (void *d, const char *data, int l) | ||
134 | { | ||
135 | struct UPNP_IGD_Data_ *datas = (struct UPNP_IGD_Data_ *) d; | ||
136 | char *dstmember = NULL; | ||
137 | |||
138 | if (!strcmp (datas->cur_elt_name, "URLBase")) | ||
139 | dstmember = datas->base_url; | ||
140 | else if (!strcmp (datas->cur_elt_name, "serviceType")) | ||
141 | dstmember = datas->service_type_tmp; | ||
142 | else if (!strcmp (datas->cur_elt_name, "controlURL")) | ||
143 | dstmember = datas->control_url_tmp; | ||
144 | else if (!strcmp (datas->cur_elt_name, "eventSubURL")) | ||
145 | dstmember = datas->event_sub_url_tmp; | ||
146 | else if (!strcmp (datas->cur_elt_name, "SCPDURL")) | ||
147 | dstmember = datas->scpd_url_tmp; | ||
148 | |||
149 | /* Copy current element name into destination member */ | ||
150 | if (dstmember) | ||
151 | { | ||
152 | if (l >= MINIUPNPC_URL_MAXSIZE) | ||
153 | l = MINIUPNPC_URL_MAXSIZE - 1; | ||
154 | |||
155 | memcpy (dstmember, data, l); | ||
156 | dstmember[l] = '\0'; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | #ifdef DEBUG_UPNP | ||
161 | static void | ||
162 | print_IGD (struct UPNP_IGD_Data_ *d) | ||
163 | { | ||
164 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
165 | "base_url = %s\n", d->base_url); | ||
166 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
167 | "WAN Device (Common interface config) :\n" | ||
168 | " sevice_type = %s\n" | ||
169 | " control_url = %s\n" | ||
170 | " event_sub_url = %s\n" | ||
171 | " scpd_url = %s\n", | ||
172 | d->service_type_CIF, | ||
173 | d->control_url_CIF, d->event_sub_url_CIF, d->scpd_url_CIF); | ||
174 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
175 | "WAN Connection Device (IP or PPP Connection):\n" | ||
176 | " service_type = %s\n" | ||
177 | " control_url = %s\n" | ||
178 | " event_sub_url = %s\n" | ||
179 | " scpd_url = %s\n", | ||
180 | d->service_type, | ||
181 | d->control_url, d->event_sub_url, d->scpd_url); | ||
182 | } | ||
183 | #endif | ||
184 | |||
185 | /** | ||
186 | * Parse XML description of an IGD device into a UPNP_IGD_Data_ struct. | ||
187 | */ | ||
188 | void | ||
189 | UPNP_IGD_parse_desc_ (const char *buffer, int buf_size, | ||
190 | struct UPNP_IGD_Data_ *data) | ||
191 | { | ||
192 | struct UPNP_xml_parser_ parser; | ||
193 | |||
194 | parser.xml_start = buffer; | ||
195 | parser.xml_size = buf_size; | ||
196 | parser.cls = data; | ||
197 | parser.start_elt_func = start_elt; | ||
198 | parser.end_elt_func = end_elt; | ||
199 | parser.data_func = IGDdata; | ||
200 | parser.att_func = 0; | ||
201 | |||
202 | UPNP_parse_xml_ (&parser); | ||
203 | |||
204 | #ifdef DEBUG_UPNP | ||
205 | print_IGD (data); | ||
206 | #endif | ||
207 | } | ||
diff --git a/src/nat/upnp-igd-parse.h b/src/nat/upnp-igd-parse.h deleted file mode 100644 index 8e0b8510c..000000000 --- a/src/nat/upnp-igd-parse.h +++ /dev/null | |||
@@ -1,99 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally based on the miniupnp library. | ||
23 | * Copyright (c) 2005-2008, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-igd-parse.h | ||
53 | * @brief Parser for XML descriptions of UPnP Internet Gateway Devices | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | #ifndef UPNP_IGD_PARSE_H | ||
58 | #define UPNP_IGD_PARSE_H | ||
59 | |||
60 | #define MINIUPNPC_URL_MAXSIZE (128) | ||
61 | |||
62 | /** | ||
63 | * Structure to store the result of the parsing of UPnP | ||
64 | * descriptions of Internet Gateway Devices. | ||
65 | */ | ||
66 | struct UPNP_IGD_Data_ | ||
67 | { | ||
68 | char cur_elt_name[MINIUPNPC_URL_MAXSIZE]; | ||
69 | char base_url[MINIUPNPC_URL_MAXSIZE]; | ||
70 | int level; | ||
71 | |||
72 | /* "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" */ | ||
73 | char control_url_CIF[MINIUPNPC_URL_MAXSIZE]; | ||
74 | char event_sub_url_CIF[MINIUPNPC_URL_MAXSIZE]; | ||
75 | char scpd_url_CIF[MINIUPNPC_URL_MAXSIZE]; | ||
76 | char service_type_CIF[MINIUPNPC_URL_MAXSIZE]; | ||
77 | |||
78 | /* "urn:schemas-upnp-org:service:WANIPConnection:1" | ||
79 | * "urn:schemas-upnp-org:service:WANPPPConnection:1" */ | ||
80 | char control_url[MINIUPNPC_URL_MAXSIZE]; | ||
81 | char event_sub_url[MINIUPNPC_URL_MAXSIZE]; | ||
82 | char scpd_url[MINIUPNPC_URL_MAXSIZE]; | ||
83 | char service_type[MINIUPNPC_URL_MAXSIZE]; | ||
84 | |||
85 | /* Used temporarily by the parser */ | ||
86 | char control_url_tmp[MINIUPNPC_URL_MAXSIZE]; | ||
87 | char event_sub_url_tmp[MINIUPNPC_URL_MAXSIZE]; | ||
88 | char scpd_url_tmp[MINIUPNPC_URL_MAXSIZE]; | ||
89 | char service_type_tmp[MINIUPNPC_URL_MAXSIZE]; | ||
90 | }; | ||
91 | |||
92 | /** | ||
93 | * Parse UPnP IGD XML description to a UPNP_IGD_Data_ structure. | ||
94 | */ | ||
95 | void | ||
96 | UPNP_IGD_parse_desc_ (const char *buffer, int buf_size, | ||
97 | struct UPNP_IGD_Data_ *data); | ||
98 | |||
99 | #endif | ||
diff --git a/src/nat/upnp-minixml.c b/src/nat/upnp-minixml.c deleted file mode 100644 index cd2d48739..000000000 --- a/src/nat/upnp-minixml.c +++ /dev/null | |||
@@ -1,239 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally based on the miniupnp library. | ||
23 | * Copyright (c) 2005-2009, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-minixml.c | ||
53 | * @brief Simple XML parser used by UPnP | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | |||
58 | #include "platform.h" | ||
59 | #include "gnunet_util_lib.h" | ||
60 | #include "upnp-minixml.h" | ||
61 | |||
62 | /** | ||
63 | * Used to parse the argument list. | ||
64 | * | ||
65 | * @returns GNUNET_OK on success, or GNUNET_SYSERR if the end | ||
66 | * of the xmlbuffer is reached | ||
67 | */ | ||
68 | static int | ||
69 | parse_att (struct UPNP_xml_parser_ *p) | ||
70 | { | ||
71 | const char *att_name; | ||
72 | int att_name_len; | ||
73 | const char *att_value; | ||
74 | int att_value_len; | ||
75 | while (p->xml < p->xml_end) | ||
76 | { | ||
77 | if (*p->xml == '/' || *p->xml == '>') | ||
78 | return GNUNET_OK; | ||
79 | if (!IS_WHITE_SPACE (*p->xml)) | ||
80 | { | ||
81 | char sep; | ||
82 | att_name = p->xml; | ||
83 | att_name_len = 0; | ||
84 | while (*p->xml != '=' && !IS_WHITE_SPACE (*p->xml)) | ||
85 | { | ||
86 | att_name_len++; | ||
87 | p->xml++; | ||
88 | if (p->xml >= p->xml_end) | ||
89 | return GNUNET_SYSERR; | ||
90 | } | ||
91 | while (*(p->xml++) != '=') | ||
92 | { | ||
93 | if (p->xml >= p->xml_end) | ||
94 | return GNUNET_SYSERR; | ||
95 | } | ||
96 | while (IS_WHITE_SPACE (*p->xml)) | ||
97 | { | ||
98 | p->xml++; | ||
99 | if (p->xml >= p->xml_end) | ||
100 | return GNUNET_SYSERR; | ||
101 | } | ||
102 | sep = *p->xml; | ||
103 | if (sep == '\'' || sep == '\"') | ||
104 | { | ||
105 | p->xml++; | ||
106 | if (p->xml >= p->xml_end) | ||
107 | return GNUNET_SYSERR; | ||
108 | att_value = p->xml; | ||
109 | att_value_len = 0; | ||
110 | while (*p->xml != sep) | ||
111 | { | ||
112 | att_value_len++; | ||
113 | p->xml++; | ||
114 | if (p->xml >= p->xml_end) | ||
115 | return GNUNET_SYSERR; | ||
116 | } | ||
117 | } | ||
118 | else | ||
119 | { | ||
120 | att_value = p->xml; | ||
121 | att_value_len = 0; | ||
122 | while (!IS_WHITE_SPACE (*p->xml) | ||
123 | && *p->xml != '>' && *p->xml != '/') | ||
124 | { | ||
125 | att_value_len++; | ||
126 | p->xml++; | ||
127 | if (p->xml >= p->xml_end) | ||
128 | return GNUNET_SYSERR; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | if (p->att_func) | ||
133 | p->att_func (p->cls, att_name, att_name_len, att_value, | ||
134 | att_value_len); | ||
135 | } | ||
136 | p->xml++; | ||
137 | } | ||
138 | return GNUNET_SYSERR; | ||
139 | } | ||
140 | |||
141 | /** | ||
142 | * Parse the xml stream and call the callback | ||
143 | * functions when needed... | ||
144 | */ | ||
145 | void | ||
146 | parse_elt (struct UPNP_xml_parser_ *p) | ||
147 | { | ||
148 | int i; | ||
149 | const char *element_name; | ||
150 | while (p->xml < (p->xml_end - 1)) | ||
151 | { | ||
152 | /* Element name */ | ||
153 | if ((p->xml)[0] == '<' && (p->xml)[1] != '?') | ||
154 | { | ||
155 | i = 0; | ||
156 | element_name = ++p->xml; | ||
157 | while (!IS_WHITE_SPACE (*p->xml) | ||
158 | && (*p->xml != '>') && (*p->xml != '/')) | ||
159 | { | ||
160 | i++; | ||
161 | p->xml++; | ||
162 | if (p->xml >= p->xml_end) | ||
163 | return; | ||
164 | /* to ignore namespace : */ | ||
165 | if (*p->xml == ':') | ||
166 | { | ||
167 | i = 0; | ||
168 | element_name = ++p->xml; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | /* Start of element */ | ||
173 | if (i > 0) | ||
174 | { | ||
175 | if (p->start_elt_func) | ||
176 | p->start_elt_func (p->cls, element_name, i); | ||
177 | if (parse_att (p) != GNUNET_OK) | ||
178 | return; | ||
179 | if (*p->xml != '/') | ||
180 | { | ||
181 | const char *data; | ||
182 | i = 0; | ||
183 | data = ++p->xml; | ||
184 | if (p->xml >= p->xml_end) | ||
185 | return; | ||
186 | while (IS_WHITE_SPACE (*p->xml)) | ||
187 | { | ||
188 | p->xml++; | ||
189 | if (p->xml >= p->xml_end) | ||
190 | return; | ||
191 | } | ||
192 | while (*p->xml != '<') | ||
193 | { | ||
194 | i++; | ||
195 | p->xml++; | ||
196 | if (p->xml >= p->xml_end) | ||
197 | return; | ||
198 | } | ||
199 | if (i > 0 && p->data_func) | ||
200 | p->data_func (p->cls, data, i); | ||
201 | } | ||
202 | } | ||
203 | /* End of element */ | ||
204 | else if (*p->xml == '/') | ||
205 | { | ||
206 | i = 0; | ||
207 | element_name = ++p->xml; | ||
208 | if (p->xml >= p->xml_end) | ||
209 | return; | ||
210 | while ((*p->xml != '>')) | ||
211 | { | ||
212 | i++; | ||
213 | p->xml++; | ||
214 | if (p->xml >= p->xml_end) | ||
215 | return; | ||
216 | } | ||
217 | if (p->end_elt_func) | ||
218 | p->end_elt_func (p->cls, element_name, i); | ||
219 | p->xml++; | ||
220 | } | ||
221 | } | ||
222 | else | ||
223 | { | ||
224 | p->xml++; | ||
225 | } | ||
226 | } | ||
227 | } | ||
228 | |||
229 | /** | ||
230 | * Parse XML content according to the values stored in the parser struct. | ||
231 | * The parser must be initialized before calling this function | ||
232 | */ | ||
233 | void | ||
234 | UPNP_parse_xml_ (struct UPNP_xml_parser_ *parser) | ||
235 | { | ||
236 | parser->xml = parser->xml_start; | ||
237 | parser->xml_end = parser->xml_start + parser->xml_size; | ||
238 | parse_elt (parser); | ||
239 | } | ||
diff --git a/src/nat/upnp-minixml.h b/src/nat/upnp-minixml.h deleted file mode 100644 index 07cf70939..000000000 --- a/src/nat/upnp-minixml.h +++ /dev/null | |||
@@ -1,131 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally based on the miniupnp library. | ||
23 | * Copyright (c) 2005-2008, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-minixml.h | ||
53 | * @brief Simple XML parser used by UPnP | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | |||
58 | #ifndef MINIXML_H | ||
59 | #define MINIXML_H | ||
60 | |||
61 | #define IS_WHITE_SPACE(c) ((c==' ') || (c=='\t') || (c=='\r') || (c=='\n')) | ||
62 | |||
63 | /** | ||
64 | * Structure describing the contents and methods that should be | ||
65 | * used when running parse_xml(); | ||
66 | * | ||
67 | * If a callback function pointer is set to NULL, the function | ||
68 | * is not called */ | ||
69 | struct UPNP_xml_parser_ | ||
70 | { | ||
71 | /** | ||
72 | * Pointer to the XML data to parse | ||
73 | */ | ||
74 | const char *xml_start; | ||
75 | |||
76 | /** | ||
77 | * Pointer to the last character to parse (optional) | ||
78 | */ | ||
79 | const char *xml_end; | ||
80 | |||
81 | /** | ||
82 | * Size of the data stored at xml_start | ||
83 | */ | ||
84 | int xml_size; | ||
85 | |||
86 | /** | ||
87 | * Pointer to current character (private) | ||
88 | */ | ||
89 | const char *xml; | ||
90 | |||
91 | /** | ||
92 | * Closure for user-provided callback functions | ||
93 | */ | ||
94 | void *cls; | ||
95 | |||
96 | /** | ||
97 | * User function called when reaching the start of an XML element. | ||
98 | */ | ||
99 | void (*start_elt_func) (void *cls, const char *elt, int elt_len); | ||
100 | |||
101 | /** | ||
102 | * User function called when reaching the end of an XML element. | ||
103 | */ | ||
104 | void (*end_elt_func) (void *cls, const char *elt, int elt_len); | ||
105 | |||
106 | /** | ||
107 | * User function called when an XML element data is found. | ||
108 | */ | ||
109 | void (*data_func) (void *cls, const char *data, int data_len); | ||
110 | |||
111 | /** | ||
112 | * User function called for every XML element attribute. | ||
113 | */ | ||
114 | void (*att_func) (void *cls, const char *att_name, int att_name_len, | ||
115 | const char *att_value, int att_value_len); | ||
116 | }; | ||
117 | |||
118 | /** | ||
119 | * Parse data provided to the xml_parser structure, using | ||
120 | * user-provided functions. | ||
121 | * | ||
122 | * The xmlparser structure must be initialized before the call; | ||
123 | * the following structure members have to be set: | ||
124 | * xml_start, xml_size, cls, *func. | ||
125 | * The xml member is for internal usage, xml_end is computed | ||
126 | * automatically. | ||
127 | * | ||
128 | * @param parser the structure used for parsing */ | ||
129 | void UPNP_parse_xml_ (struct UPNP_xml_parser_ *parser); | ||
130 | |||
131 | #endif | ||
diff --git a/src/nat/upnp-reply-parse.c b/src/nat/upnp-reply-parse.c deleted file mode 100644 index 398cde834..000000000 --- a/src/nat/upnp-reply-parse.c +++ /dev/null | |||
@@ -1,166 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally inspired by the miniupnp library. | ||
23 | * Copyright (c) 2006, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-reply-parse.c | ||
53 | * @brief Parser for XML replies to UPnP commands | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | |||
58 | #include <stdlib.h> | ||
59 | #include <string.h> | ||
60 | #include <stdio.h> | ||
61 | |||
62 | #include "platform.h" | ||
63 | #include "gnunet_util_lib.h" | ||
64 | #include "upnp-minixml.h" | ||
65 | #include "upnp-reply-parse.h" | ||
66 | |||
67 | static void | ||
68 | start_elt (void *d, const char *name, int l) | ||
69 | { | ||
70 | struct UPNP_REPLY_NameValueList_ *data = | ||
71 | (struct UPNP_REPLY_NameValueList_ *) d; | ||
72 | |||
73 | if (l > 63) | ||
74 | l = 63; | ||
75 | |||
76 | memcpy (data->curelt, name, l); | ||
77 | data->curelt[l] = '\0'; | ||
78 | } | ||
79 | |||
80 | static void | ||
81 | get_data (void *d, const char *datas, int l) | ||
82 | { | ||
83 | struct UPNP_REPLY_NameValueList_ *data = | ||
84 | (struct UPNP_REPLY_NameValueList_ *) d; | ||
85 | struct UPNP_REPLY_NameValue_ *nv; | ||
86 | |||
87 | nv = malloc (sizeof (struct UPNP_REPLY_NameValue_)); | ||
88 | |||
89 | if (l > 63) | ||
90 | l = 63; | ||
91 | |||
92 | strncpy (nv->name, data->curelt, 64); | ||
93 | nv->name[63] = '\0'; | ||
94 | memcpy (nv->value, datas, l); | ||
95 | nv->value[l] = '\0'; | ||
96 | |||
97 | LIST_INSERT_HEAD (&(data->head), nv, entries); | ||
98 | } | ||
99 | |||
100 | void | ||
101 | UPNP_REPLY_parse_ (const char *buffer, int buf_size, | ||
102 | struct UPNP_REPLY_NameValueList_ *data) | ||
103 | { | ||
104 | struct UPNP_xml_parser_ parser; | ||
105 | |||
106 | LIST_INIT (&(data->head)); | ||
107 | |||
108 | /* Init xml_parser object */ | ||
109 | parser.xml_start = buffer; | ||
110 | parser.xml_size = buf_size; | ||
111 | parser.cls = data; | ||
112 | parser.start_elt_func = start_elt; | ||
113 | parser.end_elt_func = 0; | ||
114 | parser.data_func = get_data; | ||
115 | parser.att_func = 0; | ||
116 | |||
117 | UPNP_parse_xml_ (&parser); | ||
118 | } | ||
119 | |||
120 | void | ||
121 | UPNP_REPLY_free_ (struct UPNP_REPLY_NameValueList_ *pdata) | ||
122 | { | ||
123 | struct UPNP_REPLY_NameValue_ *nv; | ||
124 | |||
125 | while ((nv = pdata->head.lh_first) != NULL) | ||
126 | { | ||
127 | LIST_REMOVE (nv, entries); | ||
128 | GNUNET_free (nv); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | char * | ||
133 | UPNP_REPLY_get_value_ (struct UPNP_REPLY_NameValueList_ *pdata, | ||
134 | const char *Name) | ||
135 | { | ||
136 | struct UPNP_REPLY_NameValue_ *nv; | ||
137 | char *p = NULL; | ||
138 | |||
139 | for (nv = pdata->head.lh_first; | ||
140 | (nv != NULL) && (p == NULL); nv = nv->entries.le_next) | ||
141 | { | ||
142 | if (strcmp (nv->name, Name) == 0) | ||
143 | p = nv->value; | ||
144 | } | ||
145 | |||
146 | return p; | ||
147 | } | ||
148 | |||
149 | #if DEBUG_UPNP | ||
150 | void | ||
151 | UPNP_REPLY_print_ (char *buffer, int buf_size) | ||
152 | { | ||
153 | struct UPNP_REPLY_NameValueList_ pdata; | ||
154 | struct UPNP_REPLY_NameValue_ *nv; | ||
155 | |||
156 | UPNP_REPLY_parse_ (buffer, buf_size, &pdata); | ||
157 | |||
158 | for (nv = pdata.head.lh_first; nv != NULL; nv = nv->entries.le_next) | ||
159 | { | ||
160 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UPnP", | ||
161 | "%s = %s", nv->name, nv->value); | ||
162 | } | ||
163 | |||
164 | UPNP_REPLY_free_ (&pdata); | ||
165 | } | ||
166 | #endif | ||
diff --git a/src/nat/upnp-reply-parse.h b/src/nat/upnp-reply-parse.h deleted file mode 100644 index e5bedbd8f..000000000 --- a/src/nat/upnp-reply-parse.h +++ /dev/null | |||
@@ -1,107 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Code in this file is originally inspired by the miniupnp library. | ||
23 | * Copyright (c) 2005-2009, Thomas BERNARD. All rights reserved. | ||
24 | * | ||
25 | * Original licence: | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions are met: | ||
29 | * | ||
30 | * * Redistributions of source code must retain the above copyright notice, | ||
31 | * this list of conditions and the following disclaimer. | ||
32 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
33 | * this list of conditions and the following disclaimer in the documentation | ||
34 | * and/or other materials provided with the distribution. | ||
35 | * * The name of the author may not be used to endorse or promote products | ||
36 | * derived from this software without specific prior written permission. | ||
37 | * | ||
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
39 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
42 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
48 | * POSSIBILITY OF SUCH DAMAGE. | ||
49 | */ | ||
50 | |||
51 | /** | ||
52 | * @file nat/upnp-reply-parse.h | ||
53 | * @brief Parser for XML replies to UPnP commands | ||
54 | * | ||
55 | * @author Milan Bouchet-Valat | ||
56 | */ | ||
57 | |||
58 | #ifndef UPNP_PARSE_REPLY_H | ||
59 | #define UPNP_PARSE_REPLY_H | ||
60 | |||
61 | #include "bsdqueue.h" | ||
62 | |||
63 | /** | ||
64 | * Name-value pair used by UPNP_REPLY_NameValueList. | ||
65 | */ | ||
66 | struct UPNP_REPLY_NameValue_ | ||
67 | { | ||
68 | LIST_ENTRY (UPNP_REPLY_NameValue_) entries; | ||
69 | char name[64]; | ||
70 | char value[64]; | ||
71 | }; | ||
72 | |||
73 | /** | ||
74 | * Name-value list to store data parsed from a UPnP reply. | ||
75 | */ | ||
76 | struct UPNP_REPLY_NameValueList_ | ||
77 | { | ||
78 | LIST_HEAD (listhead, UPNP_REPLY_NameValue_) head; | ||
79 | char curelt[64]; | ||
80 | }; | ||
81 | |||
82 | /** | ||
83 | * Parse UPnP XML reply to a name-value list. | ||
84 | */ | ||
85 | void | ||
86 | UPNP_REPLY_parse_ (const char *buffer, int buf_size, | ||
87 | struct UPNP_REPLY_NameValueList_ *data); | ||
88 | |||
89 | /** | ||
90 | * Free name-value list obtained using UPNP_REPLY_parse(). | ||
91 | */ | ||
92 | void UPNP_REPLY_free_ (struct UPNP_REPLY_NameValueList_ *pdata); | ||
93 | |||
94 | /** | ||
95 | * Get value corresponding to name from a name-value list. | ||
96 | */ | ||
97 | char *UPNP_REPLY_get_value_ (struct UPNP_REPLY_NameValueList_ *pdata, | ||
98 | const char *name); | ||
99 | |||
100 | #if DEBUG_UPNP | ||
101 | /** | ||
102 | * Parse a UPnP XMl reply and print the result as names-value pairs. | ||
103 | */ | ||
104 | void UPNP_REPLY_print_ (char *buffer, int buf_size); | ||
105 | #endif | ||
106 | |||
107 | #endif | ||
diff --git a/src/nat/upnp.c b/src/nat/upnp.c deleted file mode 100644 index 45c7df419..000000000 --- a/src/nat/upnp.c +++ /dev/null | |||
@@ -1,422 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * This file has been adapted from the Transmission project: | ||
23 | * Originally licensed by the GPL version 2. | ||
24 | * Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com> | ||
25 | */ | ||
26 | |||
27 | /** | ||
28 | * @file nat/upnp.c | ||
29 | * @brief UPnP support for the NAT library | ||
30 | * | ||
31 | * @author Milan Bouchet-Valat | ||
32 | */ | ||
33 | #include "platform.h" | ||
34 | #include "gnunet_common.h" | ||
35 | |||
36 | #include <stdlib.h> | ||
37 | #include <assert.h> | ||
38 | #include <errno.h> | ||
39 | #include <string.h> | ||
40 | |||
41 | #include "gnunet_nat_lib.h" | ||
42 | #include "nat.h" | ||
43 | #include "upnp-discover.h" | ||
44 | #include "upnp-commands.h" | ||
45 | #include "upnp.h" | ||
46 | |||
47 | /* Component name for logging */ | ||
48 | #define COMP_NAT_UPNP _("NAT (UPnP)") | ||
49 | |||
50 | enum UPNP_State | ||
51 | { | ||
52 | UPNP_IDLE, | ||
53 | UPNP_ERR, | ||
54 | UPNP_DISCOVER, | ||
55 | UPNP_MAP, | ||
56 | UPNP_UNMAP | ||
57 | }; | ||
58 | |||
59 | struct GNUNET_NAT_UPNP_Handle | ||
60 | { | ||
61 | int hasDiscovered; | ||
62 | char *control_url; | ||
63 | char *service_type; | ||
64 | int port; | ||
65 | const struct sockaddr *addr; | ||
66 | socklen_t addrlen; | ||
67 | unsigned int is_mapped; | ||
68 | enum UPNP_State state; | ||
69 | struct sockaddr *ext_addr; | ||
70 | char *iface; | ||
71 | int processing; | ||
72 | GNUNET_NAT_UPNP_pulse_cb pulse_cb; | ||
73 | void *pulse_cls; | ||
74 | }; | ||
75 | |||
76 | static int | ||
77 | process_if (void *cls, | ||
78 | const char *name, | ||
79 | int isDefault, const struct sockaddr *addr, socklen_t addrlen) | ||
80 | { | ||
81 | struct GNUNET_NAT_UPNP_Handle *upnp = cls; | ||
82 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "UPNP found if `%s'\n", name); | ||
83 | if (addr && GNUNET_NAT_cmp_addr (upnp->addr, addr) == 0) | ||
84 | { | ||
85 | upnp->iface = GNUNET_strdup(name); // BADNESS! | ||
86 | return GNUNET_SYSERR; | ||
87 | } | ||
88 | |||
89 | return GNUNET_OK; | ||
90 | } | ||
91 | |||
92 | |||
93 | struct GNUNET_NAT_UPNP_Handle * | ||
94 | GNUNET_NAT_UPNP_init (const struct sockaddr *addr, | ||
95 | socklen_t addrlen, | ||
96 | u_short port, | ||
97 | GNUNET_NAT_UPNP_pulse_cb pulse_cb, void *pulse_cls) | ||
98 | { | ||
99 | struct GNUNET_NAT_UPNP_Handle *handle; | ||
100 | |||
101 | handle = GNUNET_malloc (sizeof (struct GNUNET_NAT_UPNP_Handle)); | ||
102 | handle->processing = GNUNET_NO; | ||
103 | handle->state = UPNP_DISCOVER; | ||
104 | handle->addr = addr; | ||
105 | handle->addrlen = addrlen; | ||
106 | handle->port = port; | ||
107 | handle->pulse_cb = pulse_cb; | ||
108 | handle->pulse_cls = pulse_cls; | ||
109 | handle->control_url = NULL; | ||
110 | handle->service_type = NULL; | ||
111 | |||
112 | /* Find the interface corresponding to the address, | ||
113 | * on which we should broadcast call for routers */ | ||
114 | GNUNET_OS_network_interfaces_list (&process_if, handle); | ||
115 | if (!handle->iface) | ||
116 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
117 | COMP_NAT_UPNP, | ||
118 | "Could not find an interface matching the wanted address.\n"); | ||
119 | return handle; | ||
120 | } | ||
121 | |||
122 | |||
123 | void | ||
124 | GNUNET_NAT_UPNP_close (struct GNUNET_NAT_UPNP_Handle *handle) | ||
125 | { | ||
126 | GNUNET_assert (!handle->is_mapped); | ||
127 | GNUNET_assert ((handle->state == UPNP_IDLE) | ||
128 | || (handle->state == UPNP_ERR) | ||
129 | || (handle->state == UPNP_DISCOVER)); | ||
130 | |||
131 | GNUNET_free_non_null (handle->control_url); | ||
132 | GNUNET_free_non_null (handle->service_type); | ||
133 | GNUNET_free (handle); | ||
134 | } | ||
135 | |||
136 | static void | ||
137 | pulse_finish (struct GNUNET_NAT_UPNP_Handle *handle) | ||
138 | { | ||
139 | enum GNUNET_NAT_PortState status; | ||
140 | handle->processing = GNUNET_NO; | ||
141 | |||
142 | switch (handle->state) | ||
143 | { | ||
144 | case UPNP_DISCOVER: | ||
145 | status = GNUNET_NAT_PORT_UNMAPPED; | ||
146 | break; | ||
147 | |||
148 | case UPNP_MAP: | ||
149 | status = GNUNET_NAT_PORT_MAPPING; | ||
150 | break; | ||
151 | |||
152 | case UPNP_UNMAP: | ||
153 | status = GNUNET_NAT_PORT_UNMAPPING; | ||
154 | break; | ||
155 | |||
156 | case UPNP_IDLE: | ||
157 | status = | ||
158 | handle->is_mapped ? GNUNET_NAT_PORT_MAPPED : GNUNET_NAT_PORT_UNMAPPED; | ||
159 | break; | ||
160 | |||
161 | default: | ||
162 | status = GNUNET_NAT_PORT_ERROR; | ||
163 | break; | ||
164 | } | ||
165 | |||
166 | handle->pulse_cb (status, handle->ext_addr, handle->pulse_cls); | ||
167 | } | ||
168 | |||
169 | static void | ||
170 | discover_cb (const char *control_url, const char *service_type, void *cls) | ||
171 | { | ||
172 | struct GNUNET_NAT_UPNP_Handle *handle = cls; | ||
173 | |||
174 | if (control_url) | ||
175 | { | ||
176 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP, | ||
177 | _("Found Internet Gateway Device \"%s\"\n"), | ||
178 | control_url); | ||
179 | |||
180 | GNUNET_free_non_null (handle->control_url); | ||
181 | GNUNET_free_non_null (handle->service_type); | ||
182 | |||
183 | handle->control_url = GNUNET_strdup (control_url); | ||
184 | handle->service_type = GNUNET_strdup (service_type); | ||
185 | handle->state = UPNP_IDLE; | ||
186 | handle->hasDiscovered = 1; | ||
187 | } | ||
188 | else | ||
189 | { | ||
190 | handle->control_url = NULL; | ||
191 | handle->service_type = NULL; | ||
192 | handle->state = UPNP_ERR; | ||
193 | #ifdef DEBUG_UPNP | ||
194 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP, | ||
195 | "UPNP device discovery failed\n"); | ||
196 | #endif | ||
197 | } | ||
198 | |||
199 | pulse_finish (handle); | ||
200 | } | ||
201 | |||
202 | static void | ||
203 | check_port_mapping_cb (int error, const char *control_url, | ||
204 | const char *service_type, const char *extPort, | ||
205 | const char *inPort, const char *proto, | ||
206 | const char *remoteHost, void *cls) | ||
207 | { | ||
208 | struct GNUNET_NAT_UPNP_Handle *handle = cls; | ||
209 | |||
210 | if (error) | ||
211 | { | ||
212 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP, | ||
213 | _("Port %d isn't forwarded\n"), handle->port); | ||
214 | handle->is_mapped = GNUNET_NO; | ||
215 | } | ||
216 | |||
217 | pulse_finish (handle); | ||
218 | } | ||
219 | |||
220 | static void | ||
221 | delete_port_mapping_cb (int error, const char *control_url, | ||
222 | const char *service_type, const char *extPort, | ||
223 | const char *inPort, const char *proto, | ||
224 | const char *remoteHost, void *cls) | ||
225 | { | ||
226 | struct GNUNET_NAT_UPNP_Handle *handle = cls; | ||
227 | |||
228 | if (error) | ||
229 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP, | ||
230 | _ | ||
231 | ("Could not stop port forwarding through \"%s\", service \"%s\": error %d\n"), | ||
232 | handle->control_url, handle->service_type, error); | ||
233 | else | ||
234 | { | ||
235 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP, | ||
236 | _ | ||
237 | ("Stopped port forwarding through \"%s\", service \"%s\"\n"), | ||
238 | handle->control_url, handle->service_type); | ||
239 | handle->is_mapped = !error; | ||
240 | handle->state = UPNP_IDLE; | ||
241 | handle->port = -1; | ||
242 | } | ||
243 | |||
244 | pulse_finish (handle); | ||
245 | } | ||
246 | |||
247 | static void | ||
248 | add_port_mapping_cb (int error, const char *control_url, | ||
249 | const char *service_type, const char *extPort, | ||
250 | const char *inPort, const char *proto, | ||
251 | const char *remoteHost, void *cls) | ||
252 | { | ||
253 | struct GNUNET_NAT_UPNP_Handle *handle = cls; | ||
254 | |||
255 | if (error) | ||
256 | { | ||
257 | handle->is_mapped = GNUNET_NO; | ||
258 | handle->state = UPNP_ERR; | ||
259 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP, | ||
260 | _ | ||
261 | ("Port forwarding through \"%s\", service \"%s\" failed with error %d\n"), | ||
262 | handle->control_url, handle->service_type, error); | ||
263 | return; | ||
264 | } | ||
265 | else | ||
266 | { | ||
267 | handle->is_mapped = GNUNET_NO; | ||
268 | handle->state = UPNP_IDLE; | ||
269 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP, | ||
270 | _("Port %d forwarded successfully\n"), handle->port); | ||
271 | } | ||
272 | |||
273 | pulse_finish (handle); | ||
274 | } | ||
275 | |||
276 | static void | ||
277 | get_ip_address_cb (int error, char *ext_addr, void *cls) | ||
278 | { | ||
279 | struct GNUNET_NAT_UPNP_Handle *handle = cls; | ||
280 | |||
281 | if (error) | ||
282 | { | ||
283 | if (handle->ext_addr) | ||
284 | { | ||
285 | GNUNET_free (handle->ext_addr); | ||
286 | handle->ext_addr = NULL; | ||
287 | } | ||
288 | #ifdef DEBUG_UPNP | ||
289 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP, | ||
290 | "UPNP_get_external_ip_address_ failed (error %d)\n", | ||
291 | error); | ||
292 | #endif | ||
293 | } | ||
294 | else | ||
295 | { | ||
296 | struct in_addr addr; | ||
297 | struct in6_addr addr6; | ||
298 | |||
299 | if (handle->ext_addr) | ||
300 | { | ||
301 | GNUNET_free (handle->ext_addr); | ||
302 | handle->ext_addr = NULL; | ||
303 | } | ||
304 | |||
305 | /* Try IPv4 and IPv6 as we don't know what's the format */ | ||
306 | #ifndef MINGW | ||
307 | if (inet_aton (ext_addr, &addr) != 0) | ||
308 | #else | ||
309 | addr.S_un.S_addr = inet_addr(ext_addr); | ||
310 | if (addr.S_un.S_addr == INADDR_NONE) | ||
311 | #endif | ||
312 | { | ||
313 | handle->ext_addr = GNUNET_malloc (sizeof (struct sockaddr_in)); | ||
314 | handle->ext_addr->sa_family = AF_INET; | ||
315 | ((struct sockaddr_in *) handle->ext_addr)->sin_addr = addr; | ||
316 | } | ||
317 | else if (inet_pton (AF_INET6, ext_addr, &addr6) != 1) | ||
318 | { | ||
319 | handle->ext_addr = GNUNET_malloc (sizeof (struct sockaddr_in6)); | ||
320 | handle->ext_addr->sa_family = AF_INET6; | ||
321 | ((struct sockaddr_in6 *) handle->ext_addr)->sin6_addr = addr6; | ||
322 | } | ||
323 | else | ||
324 | GNUNET_assert (GNUNET_YES); | ||
325 | |||
326 | #ifdef DEBUG_UPNP | ||
327 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP, | ||
328 | _("Found public IP address %s\n"), ext_addr); | ||
329 | #endif | ||
330 | } | ||
331 | |||
332 | pulse_finish (handle); | ||
333 | } | ||
334 | |||
335 | /** | ||
336 | * Check state of UPnP NAT: port redirection, external IP address. | ||
337 | * | ||
338 | * | ||
339 | * @param handle the handle for UPnP object | ||
340 | * @param is_enabled whether enable port redirection | ||
341 | * @param doPortCheck do port check | ||
342 | */ | ||
343 | void | ||
344 | GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *handle, | ||
345 | int is_enabled, int doPortCheck) | ||
346 | { | ||
347 | /* Stop if we're already waiting for an action to complete */ | ||
348 | if (handle->processing == GNUNET_YES) | ||
349 | return; | ||
350 | |||
351 | if (is_enabled && (handle->state == UPNP_DISCOVER)) | ||
352 | { | ||
353 | handle->processing = GNUNET_YES; | ||
354 | UPNP_discover_ (handle->iface, handle->addr, discover_cb, | ||
355 | handle); | ||
356 | } | ||
357 | |||
358 | if (handle->state == UPNP_IDLE) | ||
359 | { | ||
360 | if (handle->is_mapped && !is_enabled) | ||
361 | handle->state = UPNP_UNMAP; | ||
362 | } | ||
363 | |||
364 | if (is_enabled && handle->is_mapped && doPortCheck) | ||
365 | { | ||
366 | char portStr[8]; | ||
367 | |||
368 | GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port); | ||
369 | |||
370 | handle->processing = GNUNET_YES; | ||
371 | UPNP_get_specific_port_mapping_entry_ (handle->control_url, | ||
372 | handle->service_type, portStr, | ||
373 | "TCP", check_port_mapping_cb, | ||
374 | handle); | ||
375 | } | ||
376 | |||
377 | if (handle->state == UPNP_UNMAP) | ||
378 | { | ||
379 | char portStr[16]; | ||
380 | GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port); | ||
381 | |||
382 | handle->processing = GNUNET_YES; | ||
383 | UPNP_delete_port_mapping_ (handle->control_url, | ||
384 | handle->service_type, portStr, "TCP", NULL, | ||
385 | delete_port_mapping_cb, handle); | ||
386 | } | ||
387 | |||
388 | if (handle->state == UPNP_IDLE) | ||
389 | { | ||
390 | if (is_enabled && !handle->is_mapped) | ||
391 | handle->state = UPNP_MAP; | ||
392 | } | ||
393 | |||
394 | if (handle->state == UPNP_MAP) | ||
395 | { | ||
396 | if (!handle->control_url) | ||
397 | handle->is_mapped = 0; | ||
398 | else | ||
399 | { | ||
400 | char portStr[16]; | ||
401 | char desc[64]; | ||
402 | GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port); | ||
403 | GNUNET_snprintf (desc, sizeof (desc), "GNUnet at %d", handle->port); | ||
404 | |||
405 | handle->processing = GNUNET_YES; | ||
406 | UPNP_add_port_mapping_ (handle->control_url, | ||
407 | handle->service_type, | ||
408 | portStr, portStr, GNUNET_a2s (handle->addr, | ||
409 | handle->addrlen), | ||
410 | desc, "TCP", NULL, add_port_mapping_cb, | ||
411 | handle); | ||
412 | } | ||
413 | } | ||
414 | |||
415 | if (handle->state != UPNP_DISCOVER) | ||
416 | { | ||
417 | handle->processing = GNUNET_YES; | ||
418 | UPNP_get_external_ip_address_ (handle->control_url, | ||
419 | handle->service_type, | ||
420 | get_ip_address_cb, handle); | ||
421 | } | ||
422 | } | ||
diff --git a/src/nat/upnp.h b/src/nat/upnp.h deleted file mode 100644 index dc6ba0d45..000000000 --- a/src/nat/upnp.h +++ /dev/null | |||
@@ -1,52 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file nat/upnp.h | ||
23 | * @brief UPnP support for the NAT library | ||
24 | * | ||
25 | * @author Milan Bouchet-Valat | ||
26 | */ | ||
27 | |||
28 | #ifndef UPNP_H | ||
29 | #define UPNP_H 1 | ||
30 | |||
31 | #include "platform.h" | ||
32 | |||
33 | struct GNUNET_NAT_UPNP_Handle; | ||
34 | |||
35 | typedef void (*GNUNET_NAT_UPNP_pulse_cb) (int status, | ||
36 | struct sockaddr * ext_addr, | ||
37 | void *cls); | ||
38 | |||
39 | struct GNUNET_NAT_UPNP_Handle *GNUNET_NAT_UPNP_init (const struct sockaddr | ||
40 | *addr, socklen_t addrlen, | ||
41 | unsigned short port, | ||
42 | GNUNET_NAT_UPNP_pulse_cb | ||
43 | pulse_cb, | ||
44 | void *pulse_cls); | ||
45 | |||
46 | void GNUNET_NAT_UPNP_close (struct GNUNET_NAT_UPNP_Handle *h); | ||
47 | |||
48 | void GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *h, | ||
49 | int is_enabled, int do_port_check); | ||
50 | |||
51 | #endif | ||
52 | /* UPNP_H */ | ||