summaryrefslogtreecommitdiff
path: root/src/nat-auto
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/nat-auto
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/nat-auto')
-rw-r--r--src/nat-auto/gnunet-nat-auto.c288
-rw-r--r--src/nat-auto/gnunet-nat-auto_legacy.c476
-rw-r--r--src/nat-auto/gnunet-nat-server.c300
-rw-r--r--src/nat-auto/gnunet-service-nat-auto.c327
-rw-r--r--src/nat-auto/gnunet-service-nat-auto_legacy.c1008
-rw-r--r--src/nat-auto/nat-auto.h17
-rw-r--r--src/nat-auto/nat_auto_api.c264
-rw-r--r--src/nat-auto/nat_auto_api_test.c490
8 files changed, 1586 insertions, 1584 deletions
diff --git a/src/nat-auto/gnunet-nat-auto.c b/src/nat-auto/gnunet-nat-auto.c
index 106d60228..87850509d 100644
--- a/src/nat-auto/gnunet-nat-auto.c
+++ b/src/nat-auto/gnunet-nat-auto.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file src/nat/gnunet-nat-auto.c 22 * @file src/nat/gnunet-nat-auto.c
@@ -90,13 +90,13 @@ static uint8_t proto;
90 * terminate. 90 * terminate.
91 */ 91 */
92static void 92static void
93test_finished () 93test_finished()
94{ 94{
95 if (NULL != ah) 95 if (NULL != ah)
96 return; 96 return;
97 if (NULL != nt) 97 if (NULL != nt)
98 return; 98 return;
99 GNUNET_SCHEDULER_shutdown (); 99 GNUNET_SCHEDULER_shutdown();
100} 100}
101 101
102 102
@@ -109,16 +109,16 @@ test_finished ()
109 * @param value value of the option 109 * @param value value of the option
110 */ 110 */
111static void 111static void
112auto_conf_iter (void *cls, 112auto_conf_iter(void *cls,
113 const char *section, 113 const char *section,
114 const char *option, 114 const char *option,
115 const char *value) 115 const char *value)
116{ 116{
117 struct GNUNET_CONFIGURATION_Handle *new_cfg = cls; 117 struct GNUNET_CONFIGURATION_Handle *new_cfg = cls;
118 118
119 printf ("%s: %s\n", option, value); 119 printf("%s: %s\n", option, value);
120 if (NULL != new_cfg) 120 if (NULL != new_cfg)
121 GNUNET_CONFIGURATION_set_value_string (new_cfg, section, option, value); 121 GNUNET_CONFIGURATION_set_value_string(new_cfg, section, option, value);
122} 122}
123 123
124 124
@@ -132,10 +132,10 @@ auto_conf_iter (void *cls,
132 * @param type what the situation of the NAT 132 * @param type what the situation of the NAT
133 */ 133 */
134static void 134static void
135auto_config_cb (void *cls, 135auto_config_cb(void *cls,
136 const struct GNUNET_CONFIGURATION_Handle *diff, 136 const struct GNUNET_CONFIGURATION_Handle *diff,
137 enum GNUNET_NAT_StatusCode result, 137 enum GNUNET_NAT_StatusCode result,
138 enum GNUNET_NAT_Type type) 138 enum GNUNET_NAT_Type type)
139{ 139{
140 const char *nat_type; 140 const char *nat_type;
141 char unknown_type[64]; 141 char unknown_type[64];
@@ -143,80 +143,84 @@ auto_config_cb (void *cls,
143 143
144 ah = NULL; 144 ah = NULL;
145 switch (type) 145 switch (type)
146 { 146 {
147 case GNUNET_NAT_TYPE_NO_NAT: 147 case GNUNET_NAT_TYPE_NO_NAT:
148 nat_type = "NO NAT"; 148 nat_type = "NO NAT";
149 break; 149 break;
150 case GNUNET_NAT_TYPE_UNREACHABLE_NAT: 150
151 nat_type = "NAT but we can traverse"; 151 case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
152 break; 152 nat_type = "NAT but we can traverse";
153 case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT: 153 break;
154 nat_type = "NAT but STUN is able to identify the correct information"; 154
155 break; 155 case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
156 case GNUNET_NAT_TYPE_UPNP_NAT: 156 nat_type = "NAT but STUN is able to identify the correct information";
157 nat_type = "NAT but UPNP opened the ports"; 157 break;
158 break; 158
159 default: 159 case GNUNET_NAT_TYPE_UPNP_NAT:
160 sprintf (unknown_type, "NAT unknown, type %u", type); 160 nat_type = "NAT but UPNP opened the ports";
161 nat_type = unknown_type; 161 break;
162 break; 162
163 } 163 default:
164 164 sprintf(unknown_type, "NAT unknown, type %u", type);
165 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 165 nat_type = unknown_type;
166 "NAT status: %s/%s\n", 166 break;
167 GNUNET_NAT_AUTO_status2string (result), 167 }
168 nat_type); 168
169 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
170 "NAT status: %s/%s\n",
171 GNUNET_NAT_AUTO_status2string(result),
172 nat_type);
169 173
170 if (NULL == diff) 174 if (NULL == diff)
171 return; 175 return;
172 176
173 /* Shortcut: if there are no changes suggested, bail out early. */ 177 /* Shortcut: if there are no changes suggested, bail out early. */
174 if (GNUNET_NO == GNUNET_CONFIGURATION_is_dirty (diff)) 178 if (GNUNET_NO == GNUNET_CONFIGURATION_is_dirty(diff))
175 { 179 {
176 test_finished (); 180 test_finished();
177 return; 181 return;
178 } 182 }
179 183
180 /* Apply diff to original configuration and show changes 184 /* Apply diff to original configuration and show changes
181 to the user */ 185 to the user */
182 new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL; 186 new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup(cfg) : NULL;
183 187
184 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 188 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
185 _ ("Suggested configuration changes:\n")); 189 _("Suggested configuration changes:\n"));
186 GNUNET_CONFIGURATION_iterate_section_values (diff, 190 GNUNET_CONFIGURATION_iterate_section_values(diff,
187 "nat", 191 "nat",
188 &auto_conf_iter, 192 &auto_conf_iter,
189 new_cfg); 193 new_cfg);
190 194
191 /* If desired, write configuration to file; we write only the 195 /* If desired, write configuration to file; we write only the
192 changes to the defaults to keep things compact. */ 196 changes to the defaults to keep things compact. */
193 if (write_cfg) 197 if (write_cfg)
194 {
195 struct GNUNET_CONFIGURATION_Handle *def_cfg;
196
197 GNUNET_CONFIGURATION_set_value_string (new_cfg, "ARM", "CONFIG", NULL);
198 def_cfg = GNUNET_CONFIGURATION_create ();
199 GNUNET_break (GNUNET_OK == GNUNET_CONFIGURATION_load (def_cfg, NULL));
200 if (GNUNET_OK !=
201 GNUNET_CONFIGURATION_write_diffs (def_cfg, new_cfg, cfg_file))
202 { 198 {
203 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 199 struct GNUNET_CONFIGURATION_Handle *def_cfg;
204 _ ("Failed to write configuration to `%s'\n"), 200
205 cfg_file); 201 GNUNET_CONFIGURATION_set_value_string(new_cfg, "ARM", "CONFIG", NULL);
206 global_ret = 1; 202 def_cfg = GNUNET_CONFIGURATION_create();
203 GNUNET_break(GNUNET_OK == GNUNET_CONFIGURATION_load(def_cfg, NULL));
204 if (GNUNET_OK !=
205 GNUNET_CONFIGURATION_write_diffs(def_cfg, new_cfg, cfg_file))
206 {
207 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
208 _("Failed to write configuration to `%s'\n"),
209 cfg_file);
210 global_ret = 1;
211 }
212 else
213 {
214 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
215 _("Wrote updated configuration to `%s'\n"),
216 cfg_file);
217 }
218 GNUNET_CONFIGURATION_destroy(def_cfg);
207 } 219 }
208 else
209 {
210 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
211 _ ("Wrote updated configuration to `%s'\n"),
212 cfg_file);
213 }
214 GNUNET_CONFIGURATION_destroy (def_cfg);
215 }
216 220
217 if (NULL != new_cfg) 221 if (NULL != new_cfg)
218 GNUNET_CONFIGURATION_destroy (new_cfg); 222 GNUNET_CONFIGURATION_destroy(new_cfg);
219 test_finished (); 223 test_finished();
220} 224}
221 225
222 226
@@ -228,11 +232,11 @@ auto_config_cb (void *cls,
228 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code 232 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
229 */ 233 */
230static void 234static void
231test_report_cb (void *cls, enum GNUNET_NAT_StatusCode result) 235test_report_cb(void *cls, enum GNUNET_NAT_StatusCode result)
232{ 236{
233 nt = NULL; 237 nt = NULL;
234 printf ("NAT test result: %s\n", GNUNET_NAT_AUTO_status2string (result)); 238 printf("NAT test result: %s\n", GNUNET_NAT_AUTO_status2string(result));
235 test_finished (); 239 test_finished();
236} 240}
237 241
238 242
@@ -242,18 +246,18 @@ test_report_cb (void *cls, enum GNUNET_NAT_StatusCode result)
242 * @param cls NULL 246 * @param cls NULL
243 */ 247 */
244static void 248static void
245do_shutdown (void *cls) 249do_shutdown(void *cls)
246{ 250{
247 if (NULL != ah) 251 if (NULL != ah)
248 { 252 {
249 GNUNET_NAT_AUTO_autoconfig_cancel (ah); 253 GNUNET_NAT_AUTO_autoconfig_cancel(ah);
250 ah = NULL; 254 ah = NULL;
251 } 255 }
252 if (NULL != nt) 256 if (NULL != nt)
253 { 257 {
254 GNUNET_NAT_AUTO_test_stop (nt); 258 GNUNET_NAT_AUTO_test_stop(nt);
255 nt = NULL; 259 nt = NULL;
256 } 260 }
257} 261}
258 262
259 263
@@ -266,29 +270,29 @@ do_shutdown (void *cls)
266 * @param c configuration 270 * @param c configuration
267 */ 271 */
268static void 272static void
269run (void *cls, 273run(void *cls,
270 char *const *args, 274 char *const *args,
271 const char *cfgfile, 275 const char *cfgfile,
272 const struct GNUNET_CONFIGURATION_Handle *c) 276 const struct GNUNET_CONFIGURATION_Handle *c)
273{ 277{
274 cfg_file = cfgfile; 278 cfg_file = cfgfile;
275 cfg = c; 279 cfg = c;
276 280
277 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); 281 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL);
278 282
279 if (do_auto) 283 if (do_auto)
280 { 284 {
281 ah = GNUNET_NAT_AUTO_autoconfig_start (c, &auto_config_cb, NULL); 285 ah = GNUNET_NAT_AUTO_autoconfig_start(c, &auto_config_cb, NULL);
282 } 286 }
283 287
284 if (use_tcp && use_udp) 288 if (use_tcp && use_udp)
285 { 289 {
286 if (do_auto) 290 if (do_auto)
291 return;
292 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n");
293 global_ret = 1;
287 return; 294 return;
288 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n"); 295 }
289 global_ret = 1;
290 return;
291 }
292 proto = 0; 296 proto = 0;
293 if (use_tcp) 297 if (use_tcp)
294 proto = IPPROTO_TCP; 298 proto = IPPROTO_TCP;
@@ -296,14 +300,14 @@ run (void *cls,
296 proto = IPPROTO_UDP; 300 proto = IPPROTO_UDP;
297 301
298 if (NULL != section_name) 302 if (NULL != section_name)
299 { 303 {
300 nt = GNUNET_NAT_AUTO_test_start (c, 304 nt = GNUNET_NAT_AUTO_test_start(c,
301 proto, 305 proto,
302 section_name, 306 section_name,
303 &test_report_cb, 307 &test_report_cb,
304 NULL); 308 NULL);
305 } 309 }
306 test_finished (); 310 test_finished();
307} 311}
308 312
309 313
@@ -315,47 +319,47 @@ run (void *cls,
315 * @return 0 on success, -1 on error 319 * @return 0 on success, -1 on error
316 */ 320 */
317int 321int
318main (int argc, char *const argv[]) 322main(int argc, char *const argv[])
319{ 323{
320 struct GNUNET_GETOPT_CommandLineOption options[] = 324 struct GNUNET_GETOPT_CommandLineOption options[] =
321 {GNUNET_GETOPT_option_flag ('a', 325 { GNUNET_GETOPT_option_flag('a',
322 "auto", 326 "auto",
323 gettext_noop ("run autoconfiguration"), 327 gettext_noop("run autoconfiguration"),
324 &do_auto), 328 &do_auto),
325 329
326 GNUNET_GETOPT_option_string ( 330 GNUNET_GETOPT_option_string(
327 'S', 331 'S',
328 "section", 332 "section",
329 "NAME", 333 "NAME",
330 gettext_noop ( 334 gettext_noop(
331 "section name providing the configuration for the adapter"), 335 "section name providing the configuration for the adapter"),
332 &section_name), 336 &section_name),
333 337
334 GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp), 338 GNUNET_GETOPT_option_flag('t', "tcp", gettext_noop("use TCP"), &use_tcp),
335 339
336 GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp), 340 GNUNET_GETOPT_option_flag('u', "udp", gettext_noop("use UDP"), &use_udp),
337 341
338 GNUNET_GETOPT_option_flag ( 342 GNUNET_GETOPT_option_flag(
339 'w', 343 'w',
340 "write", 344 "write",
341 gettext_noop ("write configuration file (for autoconfiguration)"), 345 gettext_noop("write configuration file (for autoconfiguration)"),
342 &write_cfg), 346 &write_cfg),
343 GNUNET_GETOPT_OPTION_END}; 347 GNUNET_GETOPT_OPTION_END };
344 348
345 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 349 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
346 return 2; 350 return 2;
347 if (GNUNET_OK != 351 if (GNUNET_OK !=
348 GNUNET_PROGRAM_run (argc, 352 GNUNET_PROGRAM_run(argc,
349 argv, 353 argv,
350 "gnunet-nat-auto [options]", 354 "gnunet-nat-auto [options]",
351 _ ("GNUnet NAT traversal autoconfiguration"), 355 _("GNUnet NAT traversal autoconfiguration"),
352 options, 356 options,
353 &run, 357 &run,
354 NULL)) 358 NULL))
355 { 359 {
356 global_ret = 1; 360 global_ret = 1;
357 } 361 }
358 GNUNET_free ((void *) argv); 362 GNUNET_free((void *)argv);
359 return global_ret; 363 return global_ret;
360} 364}
361 365
diff --git a/src/nat-auto/gnunet-nat-auto_legacy.c b/src/nat-auto/gnunet-nat-auto_legacy.c
index 93b544343..333e75529 100644
--- a/src/nat-auto/gnunet-nat-auto_legacy.c
+++ b/src/nat-auto/gnunet-nat-auto_legacy.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file nat/nat_test.c 22 * @file nat/nat_test.c
@@ -28,16 +28,15 @@
28#include "gnunet_nat_lib.h" 28#include "gnunet_nat_lib.h"
29#include "nat.h" 29#include "nat.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__)
32 32
33#define NAT_SERVER_TIMEOUT \ 33#define NAT_SERVER_TIMEOUT \
34 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) 34 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
35 35
36/** 36/**
37 * Entry we keep for each incoming connection. 37 * Entry we keep for each incoming connection.
38 */ 38 */
39struct NatActivity 39struct NatActivity {
40{
41 /** 40 /**
42 * This is a doubly-linked list. 41 * This is a doubly-linked list.
43 */ 42 */
@@ -68,8 +67,7 @@ struct NatActivity
68/** 67/**
69 * Entry we keep for each connection to the gnunet-nat-service. 68 * Entry we keep for each connection to the gnunet-nat-service.
70 */ 69 */
71struct ClientActivity 70struct ClientActivity {
72{
73 /** 71 /**
74 * This is a doubly-linked list. 72 * This is a doubly-linked list.
75 */ 73 */
@@ -95,9 +93,7 @@ struct ClientActivity
95/** 93/**
96 * Handle to a NAT test. 94 * Handle to a NAT test.
97 */ 95 */
98struct GNUNET_NAT_Test 96struct GNUNET_NAT_Test {
99{
100
101 /** 97 /**
102 * Configuration used 98 * Configuration used
103 */ 99 */
@@ -184,22 +180,22 @@ struct GNUNET_NAT_Test
184 * @param addrlen actual lenght of the @a addr 180 * @param addrlen actual lenght of the @a addr
185 */ 181 */
186static void 182static void
187reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen) 183reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen)
188{ 184{
189 struct GNUNET_NAT_Test *h = cls; 185 struct GNUNET_NAT_Test *h = cls;
190 const struct sockaddr_in *sa; 186 const struct sockaddr_in *sa;
191 187
192 if (sizeof (struct sockaddr_in) != addrlen) 188 if (sizeof(struct sockaddr_in) != addrlen)
193 return; 189 return;
194 sa = (const struct sockaddr_in *) addr; 190 sa = (const struct sockaddr_in *)addr;
195 if (h->data != sa->sin_port) 191 if (h->data != sa->sin_port)
196 { 192 {
197 LOG (GNUNET_ERROR_TYPE_DEBUG, 193 LOG(GNUNET_ERROR_TYPE_DEBUG,
198 "Received connection reversal request for wrong port\n"); 194 "Received connection reversal request for wrong port\n");
199 return; /* wrong port */ 195 return; /* wrong port */
200 } 196 }
201 /* report success */ 197 /* report success */
202 h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS); 198 h->report(h->report_cls, GNUNET_NAT_ERROR_SUCCESS);
203} 199}
204 200
205 201
@@ -210,31 +206,31 @@ reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
210 * @param cls the `struct GNUNET_NAT_Test` 206 * @param cls the `struct GNUNET_NAT_Test`
211 */ 207 */
212static void 208static void
213do_udp_read (void *cls) 209do_udp_read(void *cls)
214{ 210{
215 struct GNUNET_NAT_Test *tst = cls; 211 struct GNUNET_NAT_Test *tst = cls;
216 uint16_t data; 212 uint16_t data;
217 const struct GNUNET_SCHEDULER_TaskContext *tc; 213 const struct GNUNET_SCHEDULER_TaskContext *tc;
218 214
219 tc = GNUNET_SCHEDULER_get_task_context (); 215 tc = GNUNET_SCHEDULER_get_task_context();
220 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 216 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
221 tst->lsock, 217 tst->lsock,
222 &do_udp_read, 218 &do_udp_read,
223 tst); 219 tst);
224 if ((NULL != tc->write_ready) && 220 if ((NULL != tc->write_ready) &&
225 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) && 221 (GNUNET_NETWORK_fdset_isset(tc->read_ready, tst->lsock)) &&
226 (sizeof (data) == 222 (sizeof(data) ==
227 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data)))) 223 GNUNET_NETWORK_socket_recv(tst->lsock, &data, sizeof(data))))
228 { 224 {
229 if (data == tst->data) 225 if (data == tst->data)
230 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 226 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
231 else 227 else
232 LOG (GNUNET_ERROR_TYPE_DEBUG, 228 LOG(GNUNET_ERROR_TYPE_DEBUG,
233 "Received data mismatches expected value\n"); 229 "Received data mismatches expected value\n");
234 } 230 }
235 else 231 else
236 LOG (GNUNET_ERROR_TYPE_DEBUG, 232 LOG(GNUNET_ERROR_TYPE_DEBUG,
237 "Failed to receive data from inbound connection\n"); 233 "Failed to receive data from inbound connection\n");
238} 234}
239 235
240 236
@@ -245,33 +241,33 @@ do_udp_read (void *cls)
245 * @param cls the `struct NatActivity` 241 * @param cls the `struct NatActivity`
246 */ 242 */
247static void 243static void
248do_read (void *cls) 244do_read(void *cls)
249{ 245{
250 struct NatActivity *na = cls; 246 struct NatActivity *na = cls;
251 struct GNUNET_NAT_Test *tst; 247 struct GNUNET_NAT_Test *tst;
252 uint16_t data; 248 uint16_t data;
253 const struct GNUNET_SCHEDULER_TaskContext *tc; 249 const struct GNUNET_SCHEDULER_TaskContext *tc;
254 250
255 tc = GNUNET_SCHEDULER_get_task_context (); 251 tc = GNUNET_SCHEDULER_get_task_context();
256 na->rtask = NULL; 252 na->rtask = NULL;
257 tst = na->h; 253 tst = na->h;
258 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na); 254 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, na);
259 if ((NULL != tc->write_ready) && 255 if ((NULL != tc->write_ready) &&
260 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) && 256 (GNUNET_NETWORK_fdset_isset(tc->read_ready, na->sock)) &&
261 (sizeof (data) == 257 (sizeof(data) ==
262 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data)))) 258 GNUNET_NETWORK_socket_recv(na->sock, &data, sizeof(data))))
263 { 259 {
264 if (data == tst->data) 260 if (data == tst->data)
265 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 261 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
266 else 262 else
267 LOG (GNUNET_ERROR_TYPE_DEBUG, 263 LOG(GNUNET_ERROR_TYPE_DEBUG,
268 "Received data does not match expected value\n"); 264 "Received data does not match expected value\n");
269 } 265 }
270 else 266 else
271 LOG (GNUNET_ERROR_TYPE_DEBUG, 267 LOG(GNUNET_ERROR_TYPE_DEBUG,
272 "Failed to receive data from inbound connection\n"); 268 "Failed to receive data from inbound connection\n");
273 GNUNET_NETWORK_socket_close (na->sock); 269 GNUNET_NETWORK_socket_close(na->sock);
274 GNUNET_free (na); 270 GNUNET_free(na);
275} 271}
276 272
277 273
@@ -282,32 +278,32 @@ do_read (void *cls)
282 * @param cls the `struct GNUNET_NAT_Test` 278 * @param cls the `struct GNUNET_NAT_Test`
283 */ 279 */
284static void 280static void
285do_accept (void *cls) 281do_accept(void *cls)
286{ 282{
287 struct GNUNET_NAT_Test *tst = cls; 283 struct GNUNET_NAT_Test *tst = cls;
288 struct GNUNET_NETWORK_Handle *s; 284 struct GNUNET_NETWORK_Handle *s;
289 struct NatActivity *wl; 285 struct NatActivity *wl;
290 286
291 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 287 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
292 tst->lsock, 288 tst->lsock,
293 &do_accept, 289 &do_accept,
294 tst); 290 tst);
295 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL); 291 s = GNUNET_NETWORK_socket_accept(tst->lsock, NULL, NULL);
296 if (NULL == s) 292 if (NULL == s)
297 { 293 {
298 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept"); 294 GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO, "accept");
299 return; /* odd error */ 295 return; /* odd error */
300 } 296 }
301 LOG (GNUNET_ERROR_TYPE_DEBUG, 297 LOG(GNUNET_ERROR_TYPE_DEBUG,
302 "Got an inbound connection, waiting for data\n"); 298 "Got an inbound connection, waiting for data\n");
303 wl = GNUNET_new (struct NatActivity); 299 wl = GNUNET_new(struct NatActivity);
304 wl->sock = s; 300 wl->sock = s;
305 wl->h = tst; 301 wl->h = tst;
306 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 302 wl->rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
307 wl->sock, 303 wl->sock,
308 &do_read, 304 &do_read,
309 wl); 305 wl);
310 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl); 306 GNUNET_CONTAINER_DLL_insert(tst->na_head, tst->na_tail, wl);
311} 307}
312 308
313 309
@@ -319,14 +315,14 @@ do_accept (void *cls)
319 * @param error error code 315 * @param error error code
320 */ 316 */
321static void 317static void
322mq_error_handler (void *cls, enum GNUNET_MQ_Error error) 318mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
323{ 319{
324 struct ClientActivity *ca = cls; 320 struct ClientActivity *ca = cls;
325 struct GNUNET_NAT_Test *tst = ca->h; 321 struct GNUNET_NAT_Test *tst = ca->h;
326 322
327 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, ca); 323 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, ca);
328 GNUNET_MQ_destroy (ca->mq); 324 GNUNET_MQ_destroy(ca->mq);
329 GNUNET_free (ca); 325 GNUNET_free(ca);
330} 326}
331 327
332 328
@@ -340,10 +336,10 @@ mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
340 * @param addrlen actual length of the @a addr 336 * @param addrlen actual length of the @a addr
341 */ 337 */
342static void 338static void
343addr_cb (void *cls, 339addr_cb(void *cls,
344 int add_remove, 340 int add_remove,
345 const struct sockaddr *addr, 341 const struct sockaddr *addr,
346 socklen_t addrlen) 342 socklen_t addrlen)
347{ 343{
348 struct GNUNET_NAT_Test *h = cls; 344 struct GNUNET_NAT_Test *h = cls;
349 struct ClientActivity *ca; 345 struct ClientActivity *ca;
@@ -353,39 +349,39 @@ addr_cb (void *cls,
353 349
354 if (GNUNET_YES != add_remove) 350 if (GNUNET_YES != add_remove)
355 return; 351 return;
356 if (addrlen != sizeof (struct sockaddr_in)) 352 if (addrlen != sizeof(struct sockaddr_in))
357 { 353 {
358 LOG (GNUNET_ERROR_TYPE_DEBUG, 354 LOG(GNUNET_ERROR_TYPE_DEBUG,
359 "NAT test ignores IPv6 address `%s' returned from NAT library\n", 355 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
360 GNUNET_a2s (addr, addrlen)); 356 GNUNET_a2s(addr, addrlen));
361 return; /* ignore IPv6 here */ 357 return; /* ignore IPv6 here */
362 } 358 }
363 LOG (GNUNET_ERROR_TYPE_INFO, 359 LOG(GNUNET_ERROR_TYPE_INFO,
364 "Asking gnunet-nat-server to connect to `%s'\n", 360 "Asking gnunet-nat-server to connect to `%s'\n",
365 GNUNET_a2s (addr, addrlen)); 361 GNUNET_a2s(addr, addrlen));
366 362
367 ca = GNUNET_new (struct ClientActivity); 363 ca = GNUNET_new(struct ClientActivity);
368 ca->h = h; 364 ca->h = h;
369 ca->mq = GNUNET_CLIENT_connect (h->cfg, 365 ca->mq = GNUNET_CLIENT_connect(h->cfg,
370 "gnunet-nat-server", 366 "gnunet-nat-server",
371 NULL, 367 NULL,
372 &mq_error_handler, 368 &mq_error_handler,
373 ca); 369 ca);
374 if (NULL == ca->mq) 370 if (NULL == ca->mq)
375 { 371 {
376 GNUNET_free (ca); 372 GNUNET_free(ca);
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 373 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
378 _ ("Failed to connect to `gnunet-nat-server'\n")); 374 _("Failed to connect to `gnunet-nat-server'\n"));
379 return; 375 return;
380 } 376 }
381 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca); 377 GNUNET_CONTAINER_DLL_insert(h->ca_head, h->ca_tail, ca);
382 sa = (const struct sockaddr_in *) addr; 378 sa = (const struct sockaddr_in *)addr;
383 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAT_TEST); 379 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_NAT_TEST);
384 msg->dst_ipv4 = sa->sin_addr.s_addr; 380 msg->dst_ipv4 = sa->sin_addr.s_addr;
385 msg->dport = sa->sin_port; 381 msg->dport = sa->sin_port;
386 msg->data = h->data; 382 msg->data = h->data;
387 msg->is_tcp = htonl ((uint32_t) h->is_tcp); 383 msg->is_tcp = htonl((uint32_t)h->is_tcp);
388 GNUNET_MQ_send (ca->mq, env); 384 GNUNET_MQ_send(ca->mq, env);
389} 385}
390 386
391 387
@@ -398,15 +394,15 @@ addr_cb (void *cls,
398 * @param cls handle to the timed out NAT test 394 * @param cls handle to the timed out NAT test
399 */ 395 */
400static void 396static void
401do_timeout (void *cls) 397do_timeout(void *cls)
402{ 398{
403 struct GNUNET_NAT_Test *nh = cls; 399 struct GNUNET_NAT_Test *nh = cls;
404 400
405 nh->ttask = NULL; 401 nh->ttask = NULL;
406 nh->report (nh->report_cls, 402 nh->report(nh->report_cls,
407 (GNUNET_NAT_ERROR_SUCCESS == nh->status) 403 (GNUNET_NAT_ERROR_SUCCESS == nh->status)
408 ? GNUNET_NAT_ERROR_TIMEOUT 404 ? GNUNET_NAT_ERROR_TIMEOUT
409 : nh->status); 405 : nh->status);
410} 406}
411 407
412 408
@@ -426,27 +422,27 @@ do_timeout (void *cls)
426 * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback 422 * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback
427 */ 423 */
428struct GNUNET_NAT_Test * 424struct GNUNET_NAT_Test *
429GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 425GNUNET_NAT_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
430 int is_tcp, 426 int is_tcp,
431 uint16_t bnd_port, 427 uint16_t bnd_port,
432 uint16_t adv_port, 428 uint16_t adv_port,
433 struct GNUNET_TIME_Relative timeout, 429 struct GNUNET_TIME_Relative timeout,
434 GNUNET_NAT_TestCallback report, 430 GNUNET_NAT_TestCallback report,
435 void *report_cls) 431 void *report_cls)
436{ 432{
437 struct GNUNET_NAT_Test *nh; 433 struct GNUNET_NAT_Test *nh;
438 struct sockaddr_in sa; 434 struct sockaddr_in sa;
439 const struct sockaddr *addrs[] = {(const struct sockaddr *) &sa}; 435 const struct sockaddr *addrs[] = { (const struct sockaddr *)&sa };
440 const socklen_t addrlens[] = {sizeof (sa)}; 436 const socklen_t addrlens[] = { sizeof(sa) };
441 437
442 memset (&sa, 0, sizeof (sa)); 438 memset(&sa, 0, sizeof(sa));
443 sa.sin_family = AF_INET; 439 sa.sin_family = AF_INET;
444 sa.sin_port = htons (bnd_port); 440 sa.sin_port = htons(bnd_port);
445#if HAVE_SOCKADDR_IN_SIN_LEN 441#if HAVE_SOCKADDR_IN_SIN_LEN
446 sa.sin_len = sizeof (sa); 442 sa.sin_len = sizeof(sa);
447#endif 443#endif
448 444
449 nh = GNUNET_new (struct GNUNET_NAT_Test); 445 nh = GNUNET_new(struct GNUNET_NAT_Test);
450 nh->cfg = cfg; 446 nh->cfg = cfg;
451 nh->is_tcp = is_tcp; 447 nh->is_tcp = is_tcp;
452 nh->data = bnd_port; 448 nh->data = bnd_port;
@@ -455,93 +451,93 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
455 nh->report_cls = report_cls; 451 nh->report_cls = report_cls;
456 nh->status = GNUNET_NAT_ERROR_SUCCESS; 452 nh->status = GNUNET_NAT_ERROR_SUCCESS;
457 if (0 == bnd_port) 453 if (0 == bnd_port)
458 {
459 nh->nat = GNUNET_NAT_register (cfg,
460 is_tcp,
461 0,
462 0,
463 NULL,
464 NULL,
465 &addr_cb,
466 &reversal_cb,
467 nh,
468 NULL);
469 }
470 else
471 {
472 nh->lsock =
473 GNUNET_NETWORK_socket_create (AF_INET,
474 (is_tcp == GNUNET_YES) ? SOCK_STREAM
475 : SOCK_DGRAM,
476 0);
477 if ((nh->lsock == NULL) ||
478 (GNUNET_OK != GNUNET_NETWORK_socket_bind (nh->lsock,
479 (const struct sockaddr *) &sa,
480 sizeof (sa))))
481 {
482 GNUNET_log (
483 GNUNET_ERROR_TYPE_ERROR,
484 _ ("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
485 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
486 strerror (errno));
487 if (NULL != nh->lsock)
488 {
489 GNUNET_NETWORK_socket_close (nh->lsock);
490 nh->lsock = NULL;
491 }
492 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
493 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
494 return nh;
495 }
496 if (GNUNET_YES == is_tcp)
497 {
498 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5));
499 nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
500 nh->lsock,
501 &do_accept,
502 nh);
503 }
504 else
505 { 454 {
506 nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 455 nh->nat = GNUNET_NAT_register(cfg,
507 nh->lsock, 456 is_tcp,
508 &do_udp_read, 457 0,
509 nh); 458 0,
459 NULL,
460 NULL,
461 &addr_cb,
462 &reversal_cb,
463 nh,
464 NULL);
510 } 465 }
511 LOG (GNUNET_ERROR_TYPE_INFO, 466 else
512 "NAT test listens on port %u (%s)\n",
513 bnd_port,
514 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
515 nh->nat = GNUNET_NAT_register (cfg,
516 is_tcp,
517 adv_port,
518 1,
519 addrs,
520 addrlens,
521 &addr_cb,
522 NULL,
523 nh,
524 NULL);
525 if (NULL == nh->nat)
526 { 467 {
527 LOG (GNUNET_ERROR_TYPE_INFO, 468 nh->lsock =
528 _ ("NAT test failed to start NAT library\n")); 469 GNUNET_NETWORK_socket_create(AF_INET,
529 if (NULL != nh->ltask) 470 (is_tcp == GNUNET_YES) ? SOCK_STREAM
530 { 471 : SOCK_DGRAM,
531 GNUNET_SCHEDULER_cancel (nh->ltask); 472 0);
532 nh->ltask = NULL; 473 if ((nh->lsock == NULL) ||
533 } 474 (GNUNET_OK != GNUNET_NETWORK_socket_bind(nh->lsock,
534 if (NULL != nh->lsock) 475 (const struct sockaddr *)&sa,
535 { 476 sizeof(sa))))
536 GNUNET_NETWORK_socket_close (nh->lsock); 477 {
537 nh->lsock = NULL; 478 GNUNET_log(
538 } 479 GNUNET_ERROR_TYPE_ERROR,
539 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED; 480 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
540 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh); 481 GNUNET_a2s((const struct sockaddr *)&sa, sizeof(sa)),
541 return nh; 482 strerror(errno));
483 if (NULL != nh->lsock)
484 {
485 GNUNET_NETWORK_socket_close(nh->lsock);
486 nh->lsock = NULL;
487 }
488 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
489 nh->ttask = GNUNET_SCHEDULER_add_now(&do_timeout, nh);
490 return nh;
491 }
492 if (GNUNET_YES == is_tcp)
493 {
494 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_listen(nh->lsock, 5));
495 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
496 nh->lsock,
497 &do_accept,
498 nh);
499 }
500 else
501 {
502 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
503 nh->lsock,
504 &do_udp_read,
505 nh);
506 }
507 LOG(GNUNET_ERROR_TYPE_INFO,
508 "NAT test listens on port %u (%s)\n",
509 bnd_port,
510 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
511 nh->nat = GNUNET_NAT_register(cfg,
512 is_tcp,
513 adv_port,
514 1,
515 addrs,
516 addrlens,
517 &addr_cb,
518 NULL,
519 nh,
520 NULL);
521 if (NULL == nh->nat)
522 {
523 LOG(GNUNET_ERROR_TYPE_INFO,
524 _("NAT test failed to start NAT library\n"));
525 if (NULL != nh->ltask)
526 {
527 GNUNET_SCHEDULER_cancel(nh->ltask);
528 nh->ltask = NULL;
529 }
530 if (NULL != nh->lsock)
531 {
532 GNUNET_NETWORK_socket_close(nh->lsock);
533 nh->lsock = NULL;
534 }
535 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
536 nh->ttask = GNUNET_SCHEDULER_add_now(&do_timeout, nh);
537 return nh;
538 }
542 } 539 }
543 } 540 nh->ttask = GNUNET_SCHEDULER_add_delayed(timeout, &do_timeout, nh);
544 nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
545 return nh; 541 return nh;
546} 542}
547 543
@@ -552,46 +548,46 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
552 * @param tst test to stop. 548 * @param tst test to stop.
553 */ 549 */
554void 550void
555GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst) 551GNUNET_NAT_test_stop(struct GNUNET_NAT_Test *tst)
556{ 552{
557 struct NatActivity *pos; 553 struct NatActivity *pos;
558 struct ClientActivity *cpos; 554 struct ClientActivity *cpos;
559 555
560 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n"); 556 LOG(GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n");
561 while (NULL != (cpos = tst->ca_head)) 557 while (NULL != (cpos = tst->ca_head))
562 { 558 {
563 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos); 559 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, cpos);
564 GNUNET_MQ_destroy (cpos->mq); 560 GNUNET_MQ_destroy(cpos->mq);
565 GNUNET_free (cpos); 561 GNUNET_free(cpos);
566 } 562 }
567 while (NULL != (pos = tst->na_head)) 563 while (NULL != (pos = tst->na_head))
568 { 564 {
569 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos); 565 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, pos);
570 GNUNET_SCHEDULER_cancel (pos->rtask); 566 GNUNET_SCHEDULER_cancel(pos->rtask);
571 GNUNET_NETWORK_socket_close (pos->sock); 567 GNUNET_NETWORK_socket_close(pos->sock);
572 GNUNET_free (pos); 568 GNUNET_free(pos);
573 } 569 }
574 if (NULL != tst->ttask) 570 if (NULL != tst->ttask)
575 { 571 {
576 GNUNET_SCHEDULER_cancel (tst->ttask); 572 GNUNET_SCHEDULER_cancel(tst->ttask);
577 tst->ttask = NULL; 573 tst->ttask = NULL;
578 } 574 }
579 if (NULL != tst->ltask) 575 if (NULL != tst->ltask)
580 { 576 {
581 GNUNET_SCHEDULER_cancel (tst->ltask); 577 GNUNET_SCHEDULER_cancel(tst->ltask);
582 tst->ltask = NULL; 578 tst->ltask = NULL;
583 } 579 }
584 if (NULL != tst->lsock) 580 if (NULL != tst->lsock)
585 { 581 {
586 GNUNET_NETWORK_socket_close (tst->lsock); 582 GNUNET_NETWORK_socket_close(tst->lsock);
587 tst->lsock = NULL; 583 tst->lsock = NULL;
588 } 584 }
589 if (NULL != tst->nat) 585 if (NULL != tst->nat)
590 { 586 {
591 GNUNET_NAT_unregister (tst->nat); 587 GNUNET_NAT_unregister(tst->nat);
592 tst->nat = NULL; 588 tst->nat = NULL;
593 } 589 }
594 GNUNET_free (tst); 590 GNUNET_free(tst);
595} 591}
596 592
597/* end of nat_test.c */ 593/* end of nat_test.c */
diff --git a/src/nat-auto/gnunet-nat-server.c b/src/nat-auto/gnunet-nat-server.c
index c58768982..db44d7629 100644
--- a/src/nat-auto/gnunet-nat-server.c
+++ b/src/nat-auto/gnunet-nat-server.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file src/nat/gnunet-nat-server.c 22 * @file src/nat/gnunet-nat-server.c
@@ -33,8 +33,7 @@
33/** 33/**
34 * Information we track per client. 34 * Information we track per client.
35 */ 35 */
36struct ClientData 36struct ClientData {
37{
38 /** 37 /**
39 * Timeout task. 38 * Timeout task.
40 */ 39 */
@@ -61,9 +60,9 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
61 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO) 60 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO)
62 */ 61 */
63static void 62static void
64try_anat (uint32_t dst_ipv4, 63try_anat(uint32_t dst_ipv4,
65 uint16_t dport, 64 uint16_t dport,
66 int is_tcp) 65 int is_tcp)
67{ 66{
68 struct GNUNET_NAT_Handle *h; 67 struct GNUNET_NAT_Handle *h;
69 struct sockaddr_in lsa; 68 struct sockaddr_in lsa;
@@ -71,45 +70,44 @@ try_anat (uint32_t dst_ipv4,
71 const struct sockaddr *sa; 70 const struct sockaddr *sa;
72 socklen_t sa_len; 71 socklen_t sa_len;
73 72
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 73 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
75 "Asking for connection reversal with %x and code %u\n", 74 "Asking for connection reversal with %x and code %u\n",
76 (unsigned int) dst_ipv4, 75 (unsigned int)dst_ipv4,
77 (unsigned int) dport); 76 (unsigned int)dport);
78 memset (&lsa, 0, sizeof (lsa)); 77 memset(&lsa, 0, sizeof(lsa));
79 lsa.sin_family = AF_INET; 78 lsa.sin_family = AF_INET;
80#if HAVE_SOCKADDR_IN_SIN_LEN 79#if HAVE_SOCKADDR_IN_SIN_LEN
81 lsa.sin_len = sizeof (sa); 80 lsa.sin_len = sizeof(sa);
82#endif 81#endif
83 lsa.sin_addr.s_addr = 0; 82 lsa.sin_addr.s_addr = 0;
84 lsa.sin_port = htons (dport); 83 lsa.sin_port = htons(dport);
85 memset (&rsa, 0, sizeof (rsa)); 84 memset(&rsa, 0, sizeof(rsa));
86 rsa.sin_family = AF_INET; 85 rsa.sin_family = AF_INET;
87#if HAVE_SOCKADDR_IN_SIN_LEN 86#if HAVE_SOCKADDR_IN_SIN_LEN
88 rsa.sin_len = sizeof (sa); 87 rsa.sin_len = sizeof(sa);
89#endif 88#endif
90 rsa.sin_addr.s_addr = dst_ipv4; 89 rsa.sin_addr.s_addr = dst_ipv4;
91 rsa.sin_port = htons (dport); 90 rsa.sin_port = htons(dport);
92 sa_len = sizeof (lsa); 91 sa_len = sizeof(lsa);
93 sa = (const struct sockaddr *) &lsa; 92 sa = (const struct sockaddr *)&lsa;
94 h = GNUNET_NAT_register (cfg, 93 h = GNUNET_NAT_register(cfg,
95 "none", 94 "none",
96 is_tcp ? IPPROTO_TCP : IPPROTO_UDP, 95 is_tcp ? IPPROTO_TCP : IPPROTO_UDP,
97 1, 96 1,
98 &sa, 97 &sa,
99 &sa_len, 98 &sa_len,
100 NULL, NULL, NULL); 99 NULL, NULL, NULL);
101 GNUNET_NAT_request_reversal (h, 100 GNUNET_NAT_request_reversal(h,
102 &lsa, 101 &lsa,
103 &rsa); 102 &rsa);
104 GNUNET_NAT_unregister (h); 103 GNUNET_NAT_unregister(h);
105} 104}
106 105
107 106
108/** 107/**
109 * Closure for #tcp_send. 108 * Closure for #tcp_send.
110 */ 109 */
111struct TcpContext 110struct TcpContext {
112{
113 /** 111 /**
114 * TCP socket. 112 * TCP socket.
115 */ 113 */
@@ -129,24 +127,24 @@ struct TcpContext
129 * @param cls the `struct TcpContext` 127 * @param cls the `struct TcpContext`
130 */ 128 */
131static void 129static void
132tcp_send (void *cls) 130tcp_send(void *cls)
133{ 131{
134 struct TcpContext *ctx = cls; 132 struct TcpContext *ctx = cls;
135 const struct GNUNET_SCHEDULER_TaskContext *tc; 133 const struct GNUNET_SCHEDULER_TaskContext *tc;
136 134
137 tc = GNUNET_SCHEDULER_get_task_context (); 135 tc = GNUNET_SCHEDULER_get_task_context();
138 if ((NULL != tc->write_ready) && 136 if ((NULL != tc->write_ready) &&
139 (GNUNET_NETWORK_fdset_isset (tc->write_ready, ctx->s))) 137 (GNUNET_NETWORK_fdset_isset(tc->write_ready, ctx->s)))
140 {
141 if (-1 ==
142 GNUNET_NETWORK_socket_send (ctx->s, &ctx->data, sizeof (ctx->data)))
143 { 138 {
144 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send"); 139 if (-1 ==
140 GNUNET_NETWORK_socket_send(ctx->s, &ctx->data, sizeof(ctx->data)))
141 {
142 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "send");
143 }
144 GNUNET_NETWORK_socket_shutdown(ctx->s, SHUT_RDWR);
145 } 145 }
146 GNUNET_NETWORK_socket_shutdown (ctx->s, SHUT_RDWR); 146 GNUNET_NETWORK_socket_close(ctx->s);
147 } 147 GNUNET_free(ctx);
148 GNUNET_NETWORK_socket_close (ctx->s);
149 GNUNET_free (ctx);
150} 148}
151 149
152 150
@@ -159,52 +157,52 @@ tcp_send (void *cls)
159 * @param data data to send 157 * @param data data to send
160 */ 158 */
161static void 159static void
162try_send_tcp (uint32_t dst_ipv4, 160try_send_tcp(uint32_t dst_ipv4,
163 uint16_t dport, 161 uint16_t dport,
164 uint16_t data) 162 uint16_t data)
165{ 163{
166 struct GNUNET_NETWORK_Handle *s; 164 struct GNUNET_NETWORK_Handle *s;
167 struct sockaddr_in sa; 165 struct sockaddr_in sa;
168 struct TcpContext *ctx; 166 struct TcpContext *ctx;
169 167
170 s = GNUNET_NETWORK_socket_create (AF_INET, 168 s = GNUNET_NETWORK_socket_create(AF_INET,
171 SOCK_STREAM, 169 SOCK_STREAM,
172 0); 170 0);
173 if (NULL == s) 171 if (NULL == s)
174 { 172 {
175 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 173 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING,
176 "socket"); 174 "socket");
177 return; 175 return;
178 } 176 }
179 memset (&sa, 0, sizeof (sa)); 177 memset(&sa, 0, sizeof(sa));
180 sa.sin_family = AF_INET; 178 sa.sin_family = AF_INET;
181#if HAVE_SOCKADDR_IN_SIN_LEN 179#if HAVE_SOCKADDR_IN_SIN_LEN
182 sa.sin_len = sizeof (sa); 180 sa.sin_len = sizeof(sa);
183#endif 181#endif
184 sa.sin_addr.s_addr = dst_ipv4; 182 sa.sin_addr.s_addr = dst_ipv4;
185 sa.sin_port = htons (dport); 183 sa.sin_port = htons(dport);
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
187 "Sending TCP message to `%s'\n", 185 "Sending TCP message to `%s'\n",
188 GNUNET_a2s ((struct sockaddr *) &sa, 186 GNUNET_a2s((struct sockaddr *)&sa,
189 sizeof (sa))); 187 sizeof(sa)));
190 if ( (GNUNET_OK != 188 if ((GNUNET_OK !=
191 GNUNET_NETWORK_socket_connect (s, 189 GNUNET_NETWORK_socket_connect(s,
192 (const struct sockaddr *) &sa, 190 (const struct sockaddr *)&sa,
193 sizeof (sa))) && 191 sizeof(sa))) &&
194 (errno != EINPROGRESS) ) 192 (errno != EINPROGRESS))
195 { 193 {
196 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 194 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING,
197 "connect"); 195 "connect");
198 GNUNET_NETWORK_socket_close (s); 196 GNUNET_NETWORK_socket_close(s);
199 return; 197 return;
200 } 198 }
201 ctx = GNUNET_new (struct TcpContext); 199 ctx = GNUNET_new(struct TcpContext);
202 ctx->s = s; 200 ctx->s = s;
203 ctx->data = data; 201 ctx->data = data;
204 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS, 202 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_SECONDS,
205 s, 203 s,
206 &tcp_send, 204 &tcp_send,
207 ctx); 205 ctx);
208} 206}
209 207
210 208
@@ -217,42 +215,42 @@ try_send_tcp (uint32_t dst_ipv4,
217 * @param data data to send 215 * @param data data to send
218 */ 216 */
219static void 217static void
220try_send_udp (uint32_t dst_ipv4, 218try_send_udp(uint32_t dst_ipv4,
221 uint16_t dport, 219 uint16_t dport,
222 uint16_t data) 220 uint16_t data)
223{ 221{
224 struct GNUNET_NETWORK_Handle *s; 222 struct GNUNET_NETWORK_Handle *s;
225 struct sockaddr_in sa; 223 struct sockaddr_in sa;
226 224
227 s = GNUNET_NETWORK_socket_create (AF_INET, 225 s = GNUNET_NETWORK_socket_create(AF_INET,
228 SOCK_DGRAM, 226 SOCK_DGRAM,
229 0); 227 0);
230 if (NULL == s) 228 if (NULL == s)
231 { 229 {
232 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 230 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING,
233 "socket"); 231 "socket");
234 return; 232 return;
235 } 233 }
236 memset (&sa, 0, sizeof (sa)); 234 memset(&sa, 0, sizeof(sa));
237 sa.sin_family = AF_INET; 235 sa.sin_family = AF_INET;
238#if HAVE_SOCKADDR_IN_SIN_LEN 236#if HAVE_SOCKADDR_IN_SIN_LEN
239 sa.sin_len = sizeof (sa); 237 sa.sin_len = sizeof(sa);
240#endif 238#endif
241 sa.sin_addr.s_addr = dst_ipv4; 239 sa.sin_addr.s_addr = dst_ipv4;
242 sa.sin_port = htons (dport); 240 sa.sin_port = htons(dport);
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
244 "Sending UDP packet to `%s'\n", 242 "Sending UDP packet to `%s'\n",
245 GNUNET_a2s ((struct sockaddr *) &sa, 243 GNUNET_a2s((struct sockaddr *)&sa,
246 sizeof (sa))); 244 sizeof(sa)));
247 if (-1 == 245 if (-1 ==
248 GNUNET_NETWORK_socket_sendto (s, 246 GNUNET_NETWORK_socket_sendto(s,
249 &data, 247 &data,
250 sizeof (data), 248 sizeof(data),
251 (const struct sockaddr *) &sa, 249 (const struct sockaddr *)&sa,
252 sizeof (sa))) 250 sizeof(sa)))
253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 251 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING,
254 "sendto"); 252 "sendto");
255 GNUNET_NETWORK_socket_close (s); 253 GNUNET_NETWORK_socket_close(s);
256} 254}
257 255
258 256
@@ -264,28 +262,28 @@ try_send_udp (uint32_t dst_ipv4,
264 * @param msg message with details about what to test 262 * @param msg message with details about what to test
265 */ 263 */
266static void 264static void
267handle_test (void *cls, 265handle_test(void *cls,
268 const struct GNUNET_NAT_AUTO_TestMessage *tm) 266 const struct GNUNET_NAT_AUTO_TestMessage *tm)
269{ 267{
270 struct ClientData *cd = cls; 268 struct ClientData *cd = cls;
271 uint16_t dport; 269 uint16_t dport;
272 270
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
274 "Received test request\n"); 272 "Received test request\n");
275 dport = ntohs (tm->dport); 273 dport = ntohs(tm->dport);
276 if (0 == dport) 274 if (0 == dport)
277 try_anat (tm->dst_ipv4, 275 try_anat(tm->dst_ipv4,
278 ntohs (tm->data), 276 ntohs(tm->data),
279 (int) ntohl (tm->is_tcp)); 277 (int)ntohl(tm->is_tcp));
280 else if (GNUNET_YES == ntohl (tm->is_tcp)) 278 else if (GNUNET_YES == ntohl(tm->is_tcp))
281 try_send_tcp (tm->dst_ipv4, 279 try_send_tcp(tm->dst_ipv4,
282 dport, 280 dport,
283 tm->data); 281 tm->data);
284 else 282 else
285 try_send_udp (tm->dst_ipv4, 283 try_send_udp(tm->dst_ipv4,
286 dport, 284 dport,
287 tm->data); 285 tm->data);
288 GNUNET_SERVICE_client_drop (cd->client); 286 GNUNET_SERVICE_client_drop(cd->client);
289} 287}
290 288
291 289
@@ -297,9 +295,9 @@ handle_test (void *cls,
297 * @param srv service handle 295 * @param srv service handle
298 */ 296 */
299static void 297static void
300run (void *cls, 298run(void *cls,
301 const struct GNUNET_CONFIGURATION_Handle *c, 299 const struct GNUNET_CONFIGURATION_Handle *c,
302 struct GNUNET_SERVICE_Handle *srv) 300 struct GNUNET_SERVICE_Handle *srv)
303{ 301{
304 cfg = c; 302 cfg = c;
305} 303}
@@ -311,12 +309,12 @@ run (void *cls,
311 * @param cls our `struct ClientData` of a client to drop 309 * @param cls our `struct ClientData` of a client to drop
312 */ 310 */
313static void 311static void
314force_timeout (void *cls) 312force_timeout(void *cls)
315{ 313{
316 struct ClientData *cd = cls; 314 struct ClientData *cd = cls;
317 315
318 cd->tt = NULL; 316 cd->tt = NULL;
319 GNUNET_SERVICE_client_drop (cd->client); 317 GNUNET_SERVICE_client_drop(cd->client);
320} 318}
321 319
322 320
@@ -330,17 +328,17 @@ force_timeout (void *cls)
330 * @return our `struct ClientData` 328 * @return our `struct ClientData`
331 */ 329 */
332static void * 330static void *
333client_connect_cb (void *cls, 331client_connect_cb(void *cls,
334 struct GNUNET_SERVICE_Client *c, 332 struct GNUNET_SERVICE_Client *c,
335 struct GNUNET_MQ_Handle *mq) 333 struct GNUNET_MQ_Handle *mq)
336{ 334{
337 struct ClientData *cd; 335 struct ClientData *cd;
338 336
339 cd = GNUNET_new (struct ClientData); 337 cd = GNUNET_new(struct ClientData);
340 cd->client = c; 338 cd->client = c;
341 cd->tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 339 cd->tt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
342 &force_timeout, 340 &force_timeout,
343 cd); 341 cd);
344 return cd; 342 return cd;
345} 343}
346 344
@@ -353,15 +351,15 @@ client_connect_cb (void *cls,
353 * @param internal_cls our `struct ClientData` 351 * @param internal_cls our `struct ClientData`
354 */ 352 */
355static void 353static void
356client_disconnect_cb (void *cls, 354client_disconnect_cb(void *cls,
357 struct GNUNET_SERVICE_Client *c, 355 struct GNUNET_SERVICE_Client *c,
358 void *internal_cls) 356 void *internal_cls)
359{ 357{
360 struct ClientData *cd = internal_cls; 358 struct ClientData *cd = internal_cls;
361 359
362 if (NULL != cd->tt) 360 if (NULL != cd->tt)
363 GNUNET_SCHEDULER_cancel (cd->tt); 361 GNUNET_SCHEDULER_cancel(cd->tt);
364 GNUNET_free (cd); 362 GNUNET_free(cd);
365} 363}
366 364
367 365
@@ -369,17 +367,17 @@ client_disconnect_cb (void *cls,
369 * Define "main" method using service macro. 367 * Define "main" method using service macro.
370 */ 368 */
371GNUNET_SERVICE_MAIN 369GNUNET_SERVICE_MAIN
372("nat-server", 370 ("nat-server",
373 GNUNET_SERVICE_OPTION_NONE, 371 GNUNET_SERVICE_OPTION_NONE,
374 &run, 372 &run,
375 &client_connect_cb, 373 &client_connect_cb,
376 &client_disconnect_cb, 374 &client_disconnect_cb,
377 NULL, 375 NULL,
378 GNUNET_MQ_hd_fixed_size (test, 376 GNUNET_MQ_hd_fixed_size(test,
379 GNUNET_MESSAGE_TYPE_NAT_TEST, 377 GNUNET_MESSAGE_TYPE_NAT_TEST,
380 struct GNUNET_NAT_AUTO_TestMessage, 378 struct GNUNET_NAT_AUTO_TestMessage,
381 NULL), 379 NULL),
382 GNUNET_MQ_handler_end ()); 380 GNUNET_MQ_handler_end());
383 381
384 382
385#if defined(LINUX) && defined(__GLIBC__) 383#if defined(LINUX) && defined(__GLIBC__)
@@ -389,11 +387,11 @@ GNUNET_SERVICE_MAIN
389 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 387 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
390 */ 388 */
391void __attribute__ ((constructor)) 389void __attribute__ ((constructor))
392GNUNET_ARM_memory_init () 390GNUNET_ARM_memory_init()
393{ 391{
394 mallopt (M_TRIM_THRESHOLD, 4 * 1024); 392 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
395 mallopt (M_TOP_PAD, 1 * 1024); 393 mallopt(M_TOP_PAD, 1 * 1024);
396 malloc_trim (0); 394 malloc_trim(0);
397} 395}
398#endif 396#endif
399 397
diff --git a/src/nat-auto/gnunet-service-nat-auto.c b/src/nat-auto/gnunet-service-nat-auto.c
index 0b2995441..199a759ea 100644
--- a/src/nat-auto/gnunet-service-nat-auto.c
+++ b/src/nat-auto/gnunet-service-nat-auto.c
@@ -1,19 +1,19 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2016, 2017 GNUnet e.V. 3 Copyright (C) 2016, 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
@@ -45,15 +45,13 @@
45/** 45/**
46 * How long do we wait until we forcefully terminate autoconfiguration? 46 * How long do we wait until we forcefully terminate autoconfiguration?
47 */ 47 */
48#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) 48#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
49 49
50 50
51/** 51/**
52 * Internal data structure we track for each of our clients. 52 * Internal data structure we track for each of our clients.
53 */ 53 */
54struct ClientHandle 54struct ClientHandle {
55{
56
57 /** 55 /**
58 * Kept in a DLL. 56 * Kept in a DLL.
59 */ 57 */
@@ -79,8 +77,7 @@ struct ClientHandle
79/** 77/**
80 * Context for autoconfiguration operations. 78 * Context for autoconfiguration operations.
81 */ 79 */
82struct AutoconfigContext 80struct AutoconfigContext {
83{
84 /** 81 /**
85 * Kept in a DLL. 82 * Kept in a DLL.
86 */ 83 */
@@ -170,8 +167,8 @@ static struct GNUNET_STATISTICS_Handle *stats;
170 * @return #GNUNET_OK if message is well-formed 167 * @return #GNUNET_OK if message is well-formed
171 */ 168 */
172static int 169static int
173check_autoconfig_request (void *cls, 170check_autoconfig_request(void *cls,
174 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) 171 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
175{ 172{
176 return GNUNET_OK; /* checked later */ 173 return GNUNET_OK; /* checked later */
177} 174}
@@ -183,13 +180,13 @@ check_autoconfig_request (void *cls,
183 * @param ac autoconfiguration to terminate activities for 180 * @param ac autoconfiguration to terminate activities for
184 */ 181 */
185static void 182static void
186terminate_ac_activities (struct AutoconfigContext *ac) 183terminate_ac_activities(struct AutoconfigContext *ac)
187{ 184{
188 if (NULL != ac->timeout_task) 185 if (NULL != ac->timeout_task)
189 { 186 {
190 GNUNET_SCHEDULER_cancel (ac->timeout_task); 187 GNUNET_SCHEDULER_cancel(ac->timeout_task);
191 ac->timeout_task = NULL; 188 ac->timeout_task = NULL;
192 } 189 }
193} 190}
194 191
195 192
@@ -200,7 +197,7 @@ terminate_ac_activities (struct AutoconfigContext *ac)
200 * @param cls the `struct AutoconfigContext` to conclude 197 * @param cls the `struct AutoconfigContext` to conclude
201 */ 198 */
202static void 199static void
203conclude_autoconfig_request (void *cls) 200conclude_autoconfig_request(void *cls)
204{ 201{
205 struct AutoconfigContext *ac = cls; 202 struct AutoconfigContext *ac = cls;
206 struct ClientHandle *ch = ac->ch; 203 struct ClientHandle *ch = ac->ch;
@@ -211,34 +208,34 @@ conclude_autoconfig_request (void *cls)
211 struct GNUNET_CONFIGURATION_Handle *diff; 208 struct GNUNET_CONFIGURATION_Handle *diff;
212 209
213 ac->timeout_task = NULL; 210 ac->timeout_task = NULL;
214 terminate_ac_activities (ac); 211 terminate_ac_activities(ac);
215 212
216 /* Send back response */ 213 /* Send back response */
217 diff = GNUNET_CONFIGURATION_get_diff (ac->orig, 214 diff = GNUNET_CONFIGURATION_get_diff(ac->orig,
218 ac->c); 215 ac->c);
219 buf = GNUNET_CONFIGURATION_serialize (diff, 216 buf = GNUNET_CONFIGURATION_serialize(diff,
220 &c_size); 217 &c_size);
221 GNUNET_CONFIGURATION_destroy (diff); 218 GNUNET_CONFIGURATION_destroy(diff);
222 env = GNUNET_MQ_msg_extra (arm, 219 env = GNUNET_MQ_msg_extra(arm,
223 c_size, 220 c_size,
224 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT); 221 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
225 arm->status_code = htonl ((uint32_t) ac->status_code); 222 arm->status_code = htonl((uint32_t)ac->status_code);
226 arm->type = htonl ((uint32_t) ac->type); 223 arm->type = htonl((uint32_t)ac->type);
227 GNUNET_memcpy (&arm[1], 224 GNUNET_memcpy(&arm[1],
228 buf, 225 buf,
229 c_size); 226 c_size);
230 GNUNET_free (buf); 227 GNUNET_free(buf);
231 GNUNET_MQ_send (ch->mq, 228 GNUNET_MQ_send(ch->mq,
232 env); 229 env);
233 230
234 /* clean up */ 231 /* clean up */
235 GNUNET_CONFIGURATION_destroy (ac->orig); 232 GNUNET_CONFIGURATION_destroy(ac->orig);
236 GNUNET_CONFIGURATION_destroy (ac->c); 233 GNUNET_CONFIGURATION_destroy(ac->c);
237 GNUNET_CONTAINER_DLL_remove (ac_head, 234 GNUNET_CONTAINER_DLL_remove(ac_head,
238 ac_tail, 235 ac_tail,
239 ac); 236 ac);
240 GNUNET_free (ac); 237 GNUNET_free(ac);
241 GNUNET_SERVICE_client_continue (ch->client); 238 GNUNET_SERVICE_client_continue(ch->client);
242} 239}
243 240
244 241
@@ -249,12 +246,12 @@ conclude_autoconfig_request (void *cls)
249 * @param ac autoconfiguation context to check 246 * @param ac autoconfiguation context to check
250 */ 247 */
251static void 248static void
252check_autoconfig_finished (struct AutoconfigContext *ac) 249check_autoconfig_finished(struct AutoconfigContext *ac)
253{ 250{
254 GNUNET_SCHEDULER_cancel (ac->timeout_task); 251 GNUNET_SCHEDULER_cancel(ac->timeout_task);
255 ac->timeout_task 252 ac->timeout_task
256 = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request, 253 = GNUNET_SCHEDULER_add_now(&conclude_autoconfig_request,
257 ac); 254 ac);
258} 255}
259 256
260 257
@@ -264,26 +261,28 @@ check_autoconfig_finished (struct AutoconfigContext *ac)
264 * @param ac autoconfiguration to update 261 * @param ac autoconfiguration to update
265 */ 262 */
266static void 263static void
267update_enable_upnpc_option (struct AutoconfigContext *ac) 264update_enable_upnpc_option(struct AutoconfigContext *ac)
268{ 265{
269 switch (ac->enable_upnpc) 266 switch (ac->enable_upnpc)
270 { 267 {
271 case GNUNET_YES: 268 case GNUNET_YES:
272 GNUNET_CONFIGURATION_set_value_string (ac->c, 269 GNUNET_CONFIGURATION_set_value_string(ac->c,
273 "NAT", 270 "NAT",
274 "ENABLE_UPNP", 271 "ENABLE_UPNP",
275 "YES"); 272 "YES");
276 break; 273 break;
277 case GNUNET_NO: 274
278 GNUNET_CONFIGURATION_set_value_string (ac->c, 275 case GNUNET_NO:
279 "NAT", 276 GNUNET_CONFIGURATION_set_value_string(ac->c,
280 "ENABLE_UPNP", 277 "NAT",
281 "NO"); 278 "ENABLE_UPNP",
282 break; 279 "NO");
283 case GNUNET_SYSERR: 280 break;
284 /* We are unsure, do not change option */ 281
285 break; 282 case GNUNET_SYSERR:
286 } 283 /* We are unsure, do not change option */
284 break;
285 }
287} 286}
288 287
289 288
@@ -295,62 +294,62 @@ update_enable_upnpc_option (struct AutoconfigContext *ac)
295 * @param message the message received 294 * @param message the message received
296 */ 295 */
297static void 296static void
298handle_autoconfig_request (void *cls, 297handle_autoconfig_request(void *cls,
299 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) 298 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
300{ 299{
301 struct ClientHandle *ch = cls; 300 struct ClientHandle *ch = cls;
302 size_t left = ntohs (message->header.size) - sizeof (*message); 301 size_t left = ntohs(message->header.size) - sizeof(*message);
303 struct AutoconfigContext *ac; 302 struct AutoconfigContext *ac;
304 303
305 ac = GNUNET_new (struct AutoconfigContext); 304 ac = GNUNET_new(struct AutoconfigContext);
306 ac->status_code = GNUNET_NAT_ERROR_SUCCESS; 305 ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
307 ac->ch = ch; 306 ac->ch = ch;
308 ac->c = GNUNET_CONFIGURATION_create (); 307 ac->c = GNUNET_CONFIGURATION_create();
309 if (GNUNET_OK != 308 if (GNUNET_OK !=
310 GNUNET_CONFIGURATION_deserialize (ac->c, 309 GNUNET_CONFIGURATION_deserialize(ac->c,
311 (const char *) &message[1], 310 (const char *)&message[1],
312 left, 311 left,
313 NULL)) 312 NULL))
314 { 313 {
315 GNUNET_break (0); 314 GNUNET_break(0);
316 GNUNET_SERVICE_client_drop (ch->client); 315 GNUNET_SERVICE_client_drop(ch->client);
317 GNUNET_CONFIGURATION_destroy (ac->c); 316 GNUNET_CONFIGURATION_destroy(ac->c);
318 GNUNET_free (ac); 317 GNUNET_free(ac);
319 return; 318 return;
320 } 319 }
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
322 "Received REQUEST_AUTO_CONFIG message from client\n"); 321 "Received REQUEST_AUTO_CONFIG message from client\n");
323 322
324 GNUNET_CONTAINER_DLL_insert (ac_head, 323 GNUNET_CONTAINER_DLL_insert(ac_head,
325 ac_tail, 324 ac_tail,
326 ac); 325 ac);
327 ac->orig 326 ac->orig
328 = GNUNET_CONFIGURATION_dup (ac->c); 327 = GNUNET_CONFIGURATION_dup(ac->c);
329 ac->timeout_task 328 ac->timeout_task
330 = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT, 329 = GNUNET_SCHEDULER_add_delayed(AUTOCONFIG_TIMEOUT,
331 &conclude_autoconfig_request, 330 &conclude_autoconfig_request,
332 ac); 331 ac);
333 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */ 332 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
334 333
335 /* Probe for upnpc */ 334 /* Probe for upnpc */
336 if (GNUNET_SYSERR == 335 if (GNUNET_SYSERR ==
337 GNUNET_OS_check_helper_binary ("upnpc", 336 GNUNET_OS_check_helper_binary("upnpc",
338 GNUNET_NO, 337 GNUNET_NO,
339 NULL)) 338 NULL))
340 { 339 {
341 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 340 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
342 _("UPnP client `upnpc` command not found, disabling UPnP\n")); 341 _("UPnP client `upnpc` command not found, disabling UPnP\n"));
343 ac->enable_upnpc = GNUNET_NO; 342 ac->enable_upnpc = GNUNET_NO;
344 } 343 }
345 else 344 else
346 { 345 {
347 /* We might at some point be behind NAT, try upnpc */ 346 /* We might at some point be behind NAT, try upnpc */
348 ac->enable_upnpc = GNUNET_YES; 347 ac->enable_upnpc = GNUNET_YES;
349 } 348 }
350 update_enable_upnpc_option (ac); 349 update_enable_upnpc_option(ac);
351 350
352 /* Finally, check if we are already done */ 351 /* Finally, check if we are already done */
353 check_autoconfig_finished (ac); 352 check_autoconfig_finished(ac);
354} 353}
355 354
356 355
@@ -360,24 +359,24 @@ handle_autoconfig_request (void *cls,
360 * @param cls unused 359 * @param cls unused
361 */ 360 */
362static void 361static void
363shutdown_task (void *cls) 362shutdown_task(void *cls)
364{ 363{
365 struct AutoconfigContext *ac; 364 struct AutoconfigContext *ac;
366 365
367 while (NULL != (ac = ac_head)) 366 while (NULL != (ac = ac_head))
368 { 367 {
369 GNUNET_CONTAINER_DLL_remove (ac_head, 368 GNUNET_CONTAINER_DLL_remove(ac_head,
370 ac_tail, 369 ac_tail,
371 ac); 370 ac);
372 terminate_ac_activities (ac); 371 terminate_ac_activities(ac);
373 GNUNET_free (ac); 372 GNUNET_free(ac);
374 } 373 }
375 if (NULL != stats) 374 if (NULL != stats)
376 { 375 {
377 GNUNET_STATISTICS_destroy (stats, 376 GNUNET_STATISTICS_destroy(stats,
378 GNUNET_NO); 377 GNUNET_NO);
379 stats = NULL; 378 stats = NULL;
380 } 379 }
381} 380}
382 381
383 382
@@ -389,15 +388,15 @@ shutdown_task (void *cls)
389 * @param service the initialized service 388 * @param service the initialized service
390 */ 389 */
391static void 390static void
392run (void *cls, 391run(void *cls,
393 const struct GNUNET_CONFIGURATION_Handle *c, 392 const struct GNUNET_CONFIGURATION_Handle *c,
394 struct GNUNET_SERVICE_Handle *service) 393 struct GNUNET_SERVICE_Handle *service)
395{ 394{
396 cfg = c; 395 cfg = c;
397 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 396 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
398 NULL); 397 NULL);
399 stats = GNUNET_STATISTICS_create ("nat-auto", 398 stats = GNUNET_STATISTICS_create("nat-auto",
400 cfg); 399 cfg);
401} 400}
402 401
403 402
@@ -410,18 +409,18 @@ run (void *cls,
410 * @return a `struct ClientHandle` 409 * @return a `struct ClientHandle`
411 */ 410 */
412static void * 411static void *
413client_connect_cb (void *cls, 412client_connect_cb(void *cls,
414 struct GNUNET_SERVICE_Client *c, 413 struct GNUNET_SERVICE_Client *c,
415 struct GNUNET_MQ_Handle *mq) 414 struct GNUNET_MQ_Handle *mq)
416{ 415{
417 struct ClientHandle *ch; 416 struct ClientHandle *ch;
418 417
419 ch = GNUNET_new (struct ClientHandle); 418 ch = GNUNET_new(struct ClientHandle);
420 ch->mq = mq; 419 ch->mq = mq;
421 ch->client = c; 420 ch->client = c;
422 GNUNET_CONTAINER_DLL_insert (ch_head, 421 GNUNET_CONTAINER_DLL_insert(ch_head,
423 ch_tail, 422 ch_tail,
424 ch); 423 ch);
425 return ch; 424 return ch;
426} 425}
427 426
@@ -434,16 +433,16 @@ client_connect_cb (void *cls,
434 * @param internal_cls a `struct ClientHandle *` 433 * @param internal_cls a `struct ClientHandle *`
435 */ 434 */
436static void 435static void
437client_disconnect_cb (void *cls, 436client_disconnect_cb(void *cls,
438 struct GNUNET_SERVICE_Client *c, 437 struct GNUNET_SERVICE_Client *c,
439 void *internal_cls) 438 void *internal_cls)
440{ 439{
441 struct ClientHandle *ch = internal_cls; 440 struct ClientHandle *ch = internal_cls;
442 441
443 GNUNET_CONTAINER_DLL_remove (ch_head, 442 GNUNET_CONTAINER_DLL_remove(ch_head,
444 ch_tail, 443 ch_tail,
445 ch); 444 ch);
446 GNUNET_free (ch); 445 GNUNET_free(ch);
447} 446}
448 447
449 448
@@ -451,17 +450,17 @@ client_disconnect_cb (void *cls,
451 * Define "main" method using service macro. 450 * Define "main" method using service macro.
452 */ 451 */
453GNUNET_SERVICE_MAIN 452GNUNET_SERVICE_MAIN
454("nat-auto", 453 ("nat-auto",
455 GNUNET_SERVICE_OPTION_NONE, 454 GNUNET_SERVICE_OPTION_NONE,
456 &run, 455 &run,
457 &client_connect_cb, 456 &client_connect_cb,
458 &client_disconnect_cb, 457 &client_disconnect_cb,
459 NULL, 458 NULL,
460 GNUNET_MQ_hd_var_size (autoconfig_request, 459 GNUNET_MQ_hd_var_size(autoconfig_request,
461 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG, 460 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
462 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage, 461 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage,
463 NULL), 462 NULL),
464 GNUNET_MQ_handler_end ()); 463 GNUNET_MQ_handler_end());
465 464
466 465
467#if defined(LINUX) && defined(__GLIBC__) 466#if defined(LINUX) && defined(__GLIBC__)
@@ -471,11 +470,11 @@ GNUNET_SERVICE_MAIN
471 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 470 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
472 */ 471 */
473void __attribute__ ((constructor)) 472void __attribute__ ((constructor))
474GNUNET_ARM_memory_init () 473GNUNET_ARM_memory_init()
475{ 474{
476 mallopt (M_TRIM_THRESHOLD, 4 * 1024); 475 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
477 mallopt (M_TOP_PAD, 1 * 1024); 476 mallopt(M_TOP_PAD, 1 * 1024);
478 malloc_trim (0); 477 malloc_trim(0);
479} 478}
480#endif 479#endif
481 480
diff --git a/src/nat-auto/gnunet-service-nat-auto_legacy.c b/src/nat-auto/gnunet-service-nat-auto_legacy.c
index 00bdeb693..30f1f855d 100644
--- a/src/nat-auto/gnunet-service-nat-auto_legacy.c
+++ b/src/nat-auto/gnunet-service-nat-auto_legacy.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file nat/nat_auto.c 22 * @file nat/nat_auto.c
@@ -30,21 +30,20 @@
30#include "gnunet_nat_lib.h" 30#include "gnunet_nat_lib.h"
31#include "nat.h" 31#include "nat.h"
32 32
33#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__)
34 34
35 35
36/** 36/**
37 * How long do we wait for the NAT test to report success? 37 * How long do we wait for the NAT test to report success?
38 */ 38 */
39#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) 39#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
40 40
41#define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) 41#define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
42 42
43/** 43/**
44 * Phases of the auto configuration. 44 * Phases of the auto configuration.
45 */ 45 */
46enum AutoPhase 46enum AutoPhase {
47{
48 /** 47 /**
49 * Initial start value. 48 * Initial start value.
50 */ 49 */
@@ -58,7 +57,7 @@ enum AutoPhase
58 /** 57 /**
59 * Test our external IP. 58 * Test our external IP.
60 */ 59 */
61 AUTO_STUN, 60 AUTO_STUN,
62 61
63 /** 62 /**
64 * Test our internal IP. 63 * Test our internal IP.
@@ -89,16 +88,13 @@ enum AutoPhase
89 * Last phase, we're done. 88 * Last phase, we're done.
90 */ 89 */
91 AUTO_DONE 90 AUTO_DONE
92
93}; 91};
94 92
95 93
96/** 94/**
97 * Handle to auto-configuration in progress. 95 * Handle to auto-configuration in progress.
98 */ 96 */
99struct GNUNET_NAT_AutoHandle 97struct GNUNET_NAT_AutoHandle {
100{
101
102 /** 98 /**
103 * Handle to the active NAT test. 99 * Handle to the active NAT test.
104 */ 100 */
@@ -209,7 +205,7 @@ static unsigned int stun_port = 3478;
209 * @param ah auto test handle 205 * @param ah auto test handle
210 */ 206 */
211static void 207static void
212next_phase (struct GNUNET_NAT_AutoHandle *ah); 208next_phase(struct GNUNET_NAT_AutoHandle *ah);
213 209
214 210
215static void 211static void
@@ -217,12 +213,12 @@ process_stun_reply(struct sockaddr_in *answer,
217 struct GNUNET_NAT_AutoHandle *ah) 213 struct GNUNET_NAT_AutoHandle *ah)
218{ 214{
219 ah->stun_ip = inet_ntoa(answer->sin_addr); 215 ah->stun_ip = inet_ntoa(answer->sin_addr);
220 ah->stun_port = ntohs (answer->sin_port); 216 ah->stun_port = ntohs(answer->sin_port);
221 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 217 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
222 "External IP is: %s , with port %u\n", 218 "External IP is: %s , with port %u\n",
223 ah->stun_ip, 219 ah->stun_ip,
224 ah->stun_port); 220 ah->stun_port);
225 next_phase (ah); 221 next_phase(ah);
226} 222}
227 223
228 224
@@ -230,22 +226,22 @@ process_stun_reply(struct sockaddr_in *answer,
230 * Function that terminates the test. 226 * Function that terminates the test.
231 */ 227 */
232static void 228static void
233stop_stun () 229stop_stun()
234{ 230{
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 231 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
236 "Stopping STUN and quitting...\n"); 232 "Stopping STUN and quitting...\n");
237 /* Clean task */ 233 /* Clean task */
238 if (NULL != ltask4) 234 if (NULL != ltask4)
239 { 235 {
240 GNUNET_SCHEDULER_cancel (ltask4); 236 GNUNET_SCHEDULER_cancel(ltask4);
241 ltask4 = NULL; 237 ltask4 = NULL;
242 } 238 }
243 /* Clean socket */ 239 /* Clean socket */
244 if (NULL != lsock4) 240 if (NULL != lsock4)
245 { 241 {
246 GNUNET_NETWORK_socket_close (lsock4); 242 GNUNET_NETWORK_socket_close(lsock4);
247 lsock4 = NULL; 243 lsock4 = NULL;
248 } 244 }
249} 245}
250 246
251 247
@@ -256,7 +252,7 @@ stop_stun ()
256 * @param cls 252 * @param cls
257 */ 253 */
258static void 254static void
259do_udp_read (void *cls) 255do_udp_read(void *cls)
260{ 256{
261 struct GNUNET_NAT_AutoHandle *ah = cls; 257 struct GNUNET_NAT_AutoHandle *ah = cls;
262 unsigned char reply_buf[1024]; 258 unsigned char reply_buf[1024];
@@ -264,51 +260,51 @@ do_udp_read (void *cls)
264 struct sockaddr_in answer; 260 struct sockaddr_in answer;
265 const struct GNUNET_SCHEDULER_TaskContext *tc; 261 const struct GNUNET_SCHEDULER_TaskContext *tc;
266 262
267 tc = GNUNET_SCHEDULER_get_task_context (); 263 tc = GNUNET_SCHEDULER_get_task_context();
268 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && 264 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
269 (GNUNET_NETWORK_fdset_isset (tc->read_ready, 265 (GNUNET_NETWORK_fdset_isset(tc->read_ready,
270 lsock4))) 266 lsock4)))
271 {
272 rlen = GNUNET_NETWORK_socket_recv (lsock4,
273 reply_buf,
274 sizeof (reply_buf));
275
276 //Lets handle the packet
277 memset (&answer, 0, sizeof(struct sockaddr_in));
278 if (ah->phase == AUTO_NAT_PUNCHED)
279 { 267 {
280 //Destroy the connection 268 rlen = GNUNET_NETWORK_socket_recv(lsock4,
281 GNUNET_NETWORK_socket_close (lsock4); 269 reply_buf,
282 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 270 sizeof(reply_buf));
283 "The external server was able to connect back"); 271
284 ah->connected_back = GNUNET_YES; 272 //Lets handle the packet
285 next_phase (ah); 273 memset(&answer, 0, sizeof(struct sockaddr_in));
286 } 274 if (ah->phase == AUTO_NAT_PUNCHED)
287 else 275 {
288 { 276 //Destroy the connection
289 if (GNUNET_OK == 277 GNUNET_NETWORK_socket_close(lsock4);
290 GNUNET_NAT_stun_handle_packet (reply_buf, rlen, &answer)) 278 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
291 { 279 "The external server was able to connect back");
292 //Process the answer 280 ah->connected_back = GNUNET_YES;
293 process_stun_reply (&answer, ah); 281 next_phase(ah);
294 } 282 }
295 else 283 else
296 { 284 {
297 next_phase (ah); 285 if (GNUNET_OK ==
298 } 286 GNUNET_NAT_stun_handle_packet(reply_buf, rlen, &answer))
287 {
288 //Process the answer
289 process_stun_reply(&answer, ah);
290 }
291 else
292 {
293 next_phase(ah);
294 }
295 }
299 } 296 }
300 }
301 else 297 else
302 {
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304 "TIMEOUT while waiting for an answer\n");
305 if (ah->phase == AUTO_NAT_PUNCHED)
306 { 298 {
307 stop_stun(); 299 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
308 } 300 "TIMEOUT while waiting for an answer\n");
301 if (ah->phase == AUTO_NAT_PUNCHED)
302 {
303 stop_stun();
304 }
309 305
310 next_phase (ah); 306 next_phase(ah);
311 } 307 }
312} 308}
313 309
314 310
@@ -318,45 +314,45 @@ do_udp_read (void *cls)
318 * @return NULL on error 314 * @return NULL on error
319 */ 315 */
320static struct GNUNET_NETWORK_Handle * 316static struct GNUNET_NETWORK_Handle *
321bind_v4 () 317bind_v4()
322{ 318{
323 struct GNUNET_NETWORK_Handle *ls; 319 struct GNUNET_NETWORK_Handle *ls;
324 struct sockaddr_in sa4; 320 struct sockaddr_in sa4;
325 int eno; 321 int eno;
326 322
327 memset (&sa4, 0, sizeof (sa4)); 323 memset(&sa4, 0, sizeof(sa4));
328 sa4.sin_family = AF_INET; 324 sa4.sin_family = AF_INET;
329 sa4.sin_port = htons (port); 325 sa4.sin_port = htons(port);
330#if HAVE_SOCKADDR_IN_SIN_LEN 326#if HAVE_SOCKADDR_IN_SIN_LEN
331 sa4.sin_len = sizeof (sa4); 327 sa4.sin_len = sizeof(sa4);
332#endif 328#endif
333 ls = GNUNET_NETWORK_socket_create (AF_INET, 329 ls = GNUNET_NETWORK_socket_create(AF_INET,
334 SOCK_DGRAM, 330 SOCK_DGRAM,
335 0); 331 0);
336 if (NULL == ls) 332 if (NULL == ls)
337 return NULL; 333 return NULL;
338 if (GNUNET_OK != 334 if (GNUNET_OK !=
339 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4, 335 GNUNET_NETWORK_socket_bind(ls, (const struct sockaddr *)&sa4,
340 sizeof (sa4))) 336 sizeof(sa4)))
341 { 337 {
342 eno = errno; 338 eno = errno;
343 GNUNET_NETWORK_socket_close (ls); 339 GNUNET_NETWORK_socket_close(ls);
344 errno = eno; 340 errno = eno;
345 return NULL; 341 return NULL;
346 } 342 }
347 return ls; 343 return ls;
348} 344}
349 345
350 346
351static void 347static void
352request_callback (void *cls, 348request_callback(void *cls,
353 enum GNUNET_NAT_StatusCode result) 349 enum GNUNET_NAT_StatusCode result)
354{ 350{
355 // struct GNUNET_NAT_AutoHandle *ah = cls; 351 // struct GNUNET_NAT_AutoHandle *ah = cls;
356 352
357 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 353 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
358 "Request callback: stop and quit\n"); 354 "Request callback: stop and quit\n");
359 stop_stun (); 355 stop_stun();
360 356
361 // next_phase (ah); FIXME this always will be NULL, as called in test_stun() 357 // next_phase (ah); FIXME this always will be NULL, as called in test_stun()
362} 358}
@@ -371,22 +367,22 @@ request_callback (void *cls,
371 * @param emsg NULL on success, otherwise an error message 367 * @param emsg NULL on success, otherwise an error message
372 */ 368 */
373static void 369static void
374result_callback (void *cls, 370result_callback(void *cls,
375 enum GNUNET_NAT_StatusCode ret) 371 enum GNUNET_NAT_StatusCode ret)
376{ 372{
377 struct GNUNET_NAT_AutoHandle *ah = cls; 373 struct GNUNET_NAT_AutoHandle *ah = cls;
378 374
379 if (GNUNET_NAT_ERROR_SUCCESS == ret) 375 if (GNUNET_NAT_ERROR_SUCCESS == ret)
380 GNUNET_NAT_test_stop (ah->tst); 376 GNUNET_NAT_test_stop(ah->tst);
381 ah->tst = NULL; 377 ah->tst = NULL;
382 ah->ret = ret; 378 ah->ret = ret;
383 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 379 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
384 GNUNET_NAT_ERROR_SUCCESS == ret 380 GNUNET_NAT_ERROR_SUCCESS == ret
385 ? _("NAT traversal with ICMP Server succeeded.\n") 381 ? _("NAT traversal with ICMP Server succeeded.\n")
386 : _("NAT traversal with ICMP Server failed.\n")); 382 : _("NAT traversal with ICMP Server failed.\n"));
387 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER", 383 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "ENABLE_ICMP_SERVER",
388 GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES"); 384 GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES");
389 next_phase (ah); 385 next_phase(ah);
390} 386}
391 387
392 388
@@ -396,16 +392,16 @@ result_callback (void *cls,
396 * @param cls the `struct GNUNET_NAT_AutoHandle` 392 * @param cls the `struct GNUNET_NAT_AutoHandle`
397 */ 393 */
398static void 394static void
399reversal_test (void *cls) 395reversal_test(void *cls)
400{ 396{
401 struct GNUNET_NAT_AutoHandle *ah = cls; 397 struct GNUNET_NAT_AutoHandle *ah = cls;
402 398
403 ah->task = NULL; 399 ah->task = NULL;
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 400 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
405 _("Testing connection reversal with ICMP server.\n")); 401 _("Testing connection reversal with ICMP server.\n"));
406 GNUNET_RESOLVER_connect (ah->cfg); 402 GNUNET_RESOLVER_connect(ah->cfg);
407 ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0, TIMEOUT, 403 ah->tst = GNUNET_NAT_test_start(ah->cfg, GNUNET_YES, 0, 0, TIMEOUT,
408 &result_callback, ah); 404 &result_callback, ah);
409} 405}
410 406
411 407
@@ -418,9 +414,9 @@ reversal_test (void *cls)
418 * @param emsg NULL on success, otherwise an error message 414 * @param emsg NULL on success, otherwise an error message
419 */ 415 */
420static void 416static void
421set_external_ipv4 (void *cls, 417set_external_ipv4(void *cls,
422 const struct in_addr *addr, 418 const struct in_addr *addr,
423 enum GNUNET_NAT_StatusCode ret) 419 enum GNUNET_NAT_StatusCode ret)
424{ 420{
425 struct GNUNET_NAT_AutoHandle *ah = cls; 421 struct GNUNET_NAT_AutoHandle *ah = cls;
426 char buf[INET_ADDRSTRLEN]; 422 char buf[INET_ADDRSTRLEN];
@@ -428,35 +424,35 @@ set_external_ipv4 (void *cls,
428 ah->eh = NULL; 424 ah->eh = NULL;
429 ah->ret = ret; 425 ah->ret = ret;
430 if (GNUNET_NAT_ERROR_SUCCESS != ret) 426 if (GNUNET_NAT_ERROR_SUCCESS != ret)
431 { 427 {
432 next_phase (ah); 428 next_phase(ah);
433 return; 429 return;
434 } 430 }
435 /* enable 'behind nat' */ 431 /* enable 'behind nat' */
436 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 432 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
437 _("Detected external IP `%s'\n"), 433 _("Detected external IP `%s'\n"),
438 inet_ntop (AF_INET, 434 inet_ntop(AF_INET,
439 addr, 435 addr,
440 buf, 436 buf,
441 sizeof (buf))); 437 sizeof(buf)));
442 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES"); 438 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "BEHIND_NAT", "YES");
443 439
444 /* set external IP address */ 440 /* set external IP address */
445 if (NULL == inet_ntop (AF_INET, addr, buf, sizeof (buf))) 441 if (NULL == inet_ntop(AF_INET, addr, buf, sizeof(buf)))
446 { 442 {
447 GNUNET_break (0); 443 GNUNET_break(0);
448 /* actually, this should never happen, as the caller already executed just 444 /* actually, this should never happen, as the caller already executed just
449 * this check, but for consistency (eg: future changes in the caller) 445 * this check, but for consistency (eg: future changes in the caller)
450 * we still need to report this error... 446 * we still need to report this error...
451 */ 447 */
452 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; 448 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;
453 next_phase (ah); 449 next_phase(ah);
454 return; 450 return;
455 } 451 }
456 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS", 452 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "EXTERNAL_ADDRESS",
457 buf); 453 buf);
458 ah->upnp_set_external_address = GNUNET_YES; 454 ah->upnp_set_external_address = GNUNET_YES;
459 next_phase (ah); 455 next_phase(ah);
460} 456}
461 457
462 458
@@ -466,15 +462,15 @@ set_external_ipv4 (void *cls,
466 * @param ah auto setup context 462 * @param ah auto setup context
467 */ 463 */
468static void 464static void
469test_external_ip (struct GNUNET_NAT_AutoHandle *ah) 465test_external_ip(struct GNUNET_NAT_AutoHandle *ah)
470{ 466{
471 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret) 467 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
472 next_phase (ah); 468 next_phase(ah);
473 469
474 // FIXME: CPS? 470 // FIXME: CPS?
475 /* try to detect external IP */ 471 /* try to detect external IP */
476 ah->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT, 472 ah->eh = GNUNET_NAT_mini_get_external_ipv4(TIMEOUT,
477 &set_external_ipv4, ah); 473 &set_external_ipv4, ah);
478} 474}
479 475
480 476
@@ -484,54 +480,53 @@ test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
484 * @param ah auto setup context 480 * @param ah auto setup context
485 */ 481 */
486static void 482static void
487test_stun (struct GNUNET_NAT_AutoHandle *ah) 483test_stun(struct GNUNET_NAT_AutoHandle *ah)
488{ 484{
489 485 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running STUN test\n");
490 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running STUN test\n");
491 486
492 /* Get port from the configuration */ 487 /* Get port from the configuration */
493 if (GNUNET_OK != 488 if (GNUNET_OK !=
494 GNUNET_CONFIGURATION_get_value_number (ah->cfg, 489 GNUNET_CONFIGURATION_get_value_number(ah->cfg,
495 "transport-udp", 490 "transport-udp",
496 "PORT", 491 "PORT",
497 &port)) 492 &port))
498 { 493 {
499 port = 2086; 494 port = 2086;
500 } 495 }
501 496
502 //Lets create the socket 497 //Lets create the socket
503 lsock4 = bind_v4 (); 498 lsock4 = bind_v4();
504 if (NULL == lsock4) 499 if (NULL == lsock4)
505 { 500 {
506 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); 501 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "bind");
507 next_phase(ah); 502 next_phase(ah);
508 return; 503 return;
509 } 504 }
510 else 505 else
511 { 506 {
512 //Lets call our function now when it accepts 507 //Lets call our function now when it accepts
513 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT, 508 ltask4 = GNUNET_SCHEDULER_add_read_net(NAT_SERVER_TIMEOUT,
514 lsock4, 509 lsock4,
515 &do_udp_read, 510 &do_udp_read,
516 ah); 511 ah);
517 } 512 }
518 513
519 514
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 515 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
521 "STUN service listens on port %u\n", 516 "STUN service listens on port %u\n",
522 (unsigned int) port); 517 (unsigned int)port);
523 if (GNUNET_NO == 518 if (GNUNET_NO ==
524 GNUNET_NAT_stun_make_request (stun_server, 519 GNUNET_NAT_stun_make_request(stun_server,
525 stun_port, 520 stun_port,
526 lsock4, 521 lsock4,
527 &request_callback, 522 &request_callback,
528 NULL)) 523 NULL))
529 { 524 {
530 /*An error happened*/ 525 /*An error happened*/
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n"); 526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n");
532 stop_stun (); 527 stop_stun();
533 next_phase (ah); 528 next_phase(ah);
534 } 529 }
535} 530}
536 531
537 532
@@ -549,57 +544,57 @@ test_stun (struct GNUNET_NAT_AutoHandle *ah)
549 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort 544 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
550 */ 545 */
551static int 546static int
552process_if (void *cls, 547process_if(void *cls,
553 const char *name, 548 const char *name,
554 int isDefault, 549 int isDefault,
555 const struct sockaddr *addr, 550 const struct sockaddr *addr,
556 const struct sockaddr *broadcast_addr, 551 const struct sockaddr *broadcast_addr,
557 const struct sockaddr *netmask, 552 const struct sockaddr *netmask,
558 socklen_t addrlen) 553 socklen_t addrlen)
559{ 554{
560 struct GNUNET_NAT_AutoHandle *ah = cls; 555 struct GNUNET_NAT_AutoHandle *ah = cls;
561 const struct sockaddr_in *in; 556 const struct sockaddr_in *in;
562 char buf[INET_ADDRSTRLEN]; 557 char buf[INET_ADDRSTRLEN];
563 558
564 559
565 if ( (sizeof (struct sockaddr_in6) == addrlen) && 560 if ((sizeof(struct sockaddr_in6) == addrlen) &&
566 (0 != GNUNET_memcmp (&in6addr_loopback, &((const struct sockaddr_in6 *) addr)->sin6_addr)) && 561 (0 != GNUNET_memcmp(&in6addr_loopback, &((const struct sockaddr_in6 *)addr)->sin6_addr)) &&
567 (! IN6_IS_ADDR_LINKLOCAL(&((const struct sockaddr_in6 *) addr)->sin6_addr)) ) 562 (!IN6_IS_ADDR_LINKLOCAL(&((const struct sockaddr_in6 *)addr)->sin6_addr)))
568 { 563 {
569 ah->have_v6 = GNUNET_YES; 564 ah->have_v6 = GNUNET_YES;
570 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 565 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
571 _("This system has a global IPv6 address, setting IPv6 to supported.\n")); 566 _("This system has a global IPv6 address, setting IPv6 to supported.\n"));
572 567
568 return GNUNET_OK;
569 }
570 if (addrlen != sizeof(struct sockaddr_in))
573 return GNUNET_OK; 571 return GNUNET_OK;
574 } 572 in = (const struct sockaddr_in *)addr;
575 if (addrlen != sizeof (struct sockaddr_in))
576 return GNUNET_OK;
577 in = (const struct sockaddr_in *) addr;
578 573
579 574
580 /* set internal IP address */ 575 /* set internal IP address */
581 if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof (buf))) 576 if (NULL == inet_ntop(AF_INET, &in->sin_addr, buf, sizeof(buf)))
582 { 577 {
583 GNUNET_break (0); 578 GNUNET_break(0);
584 return GNUNET_OK; 579 return GNUNET_OK;
585 } 580 }
586 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS", 581 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "INTERNAL_ADDRESS",
587 buf); 582 buf);
588 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 583 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
589 _("Detected internal network address `%s'.\n"), 584 _("Detected internal network address `%s'.\n"),
590 buf); 585 buf);
591 586
592 587
593 ah->ret = GNUNET_NAT_ERROR_SUCCESS; 588 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
594 589
595 /* Check if our internal IP is the same as the External detect by STUN*/ 590 /* Check if our internal IP is the same as the External detect by STUN*/
596 if(ah->stun_ip && (strcmp(buf, ah->stun_ip) == 0) ) 591 if (ah->stun_ip && (strcmp(buf, ah->stun_ip) == 0))
597 { 592 {
598 ah->internal_ip_is_public = GNUNET_YES; 593 ah->internal_ip_is_public = GNUNET_YES;
599 GNUNET_log (GNUNET_ERROR_TYPE_INFO,"A internal IP is the sameas the external"); 594 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "A internal IP is the sameas the external");
600 /* No need to continue*/ 595 /* No need to continue*/
601 return GNUNET_SYSERR; 596 return GNUNET_SYSERR;
602 } 597 }
603 598
604 /* no need to continue iteration if we found the default */ 599 /* no need to continue iteration if we found the default */
605 if (!isDefault) 600 if (!isDefault)
@@ -615,15 +610,15 @@ process_if (void *cls,
615 * @param ah auto setup context 610 * @param ah auto setup context
616 */ 611 */
617static void 612static void
618test_local_ip (struct GNUNET_NAT_AutoHandle *ah) 613test_local_ip(struct GNUNET_NAT_AutoHandle *ah)
619{ 614{
620 ah->have_v6 = GNUNET_NO; 615 ah->have_v6 = GNUNET_NO;
621 ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP 616 ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP
622 GNUNET_OS_network_interfaces_list (&process_if, ah); 617 GNUNET_OS_network_interfaces_list(&process_if, ah);
623 618
624 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "DISABLEV6", 619 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "DISABLEV6",
625 (GNUNET_YES == ah->have_v6) ? "NO" : "YES"); 620 (GNUNET_YES == ah->have_v6) ? "NO" : "YES");
626 next_phase (ah); 621 next_phase(ah);
627} 622}
628 623
629 624
@@ -635,15 +630,15 @@ test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
635 * @param error error code 630 * @param error error code
636 */ 631 */
637static void 632static void
638mq_error_handler (void *cls, 633mq_error_handler(void *cls,
639 enum GNUNET_MQ_Error error) 634 enum GNUNET_MQ_Error error)
640{ 635{
641 struct GNUNET_NAT_AutoHandle *ah = cls; 636 struct GNUNET_NAT_AutoHandle *ah = cls;
642 637
643 GNUNET_MQ_destroy (ah->mq); 638 GNUNET_MQ_destroy(ah->mq);
644 ah->mq = NULL; 639 ah->mq = NULL;
645 /* wait a bit first? */ 640 /* wait a bit first? */
646 next_phase (ah); 641 next_phase(ah);
647} 642}
648 643
649 644
@@ -653,50 +648,50 @@ mq_error_handler (void *cls,
653 * @param ah auto setup context 648 * @param ah auto setup context
654 */ 649 */
655static void 650static void
656test_nat_punched (struct GNUNET_NAT_AutoHandle *ah) 651test_nat_punched(struct GNUNET_NAT_AutoHandle *ah)
657{ 652{
658 struct GNUNET_NAT_TestMessage *msg; 653 struct GNUNET_NAT_TestMessage *msg;
659 struct GNUNET_MQ_Envelope *env; 654 struct GNUNET_MQ_Envelope *env;
660 655
661 if (! ah->stun_ip) 656 if (!ah->stun_ip)
662 { 657 {
663 LOG (GNUNET_ERROR_TYPE_INFO, 658 LOG(GNUNET_ERROR_TYPE_INFO,
664 "We don't have a STUN IP"); 659 "We don't have a STUN IP");
665 next_phase (ah); 660 next_phase(ah);
666 return; 661 return;
667 } 662 }
668 663
669 LOG (GNUNET_ERROR_TYPE_INFO, 664 LOG(GNUNET_ERROR_TYPE_INFO,
670 "Asking gnunet-nat-server to connect to `%s'\n", 665 "Asking gnunet-nat-server to connect to `%s'\n",
671 ah->stun_ip); 666 ah->stun_ip);
672 ah->mq = GNUNET_CLIENT_connect (ah->cfg, 667 ah->mq = GNUNET_CLIENT_connect(ah->cfg,
673 "gnunet-nat-server", 668 "gnunet-nat-server",
674 NULL, 669 NULL,
675 &mq_error_handler, 670 &mq_error_handler,
676 ah); 671 ah);
677 if (NULL == ah->mq) 672 if (NULL == ah->mq)
678 { 673 {
679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 674 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
680 _("Failed to connect to `gnunet-nat-server'\n")); 675 _("Failed to connect to `gnunet-nat-server'\n"));
681 next_phase (ah); 676 next_phase(ah);
682 return; 677 return;
683 } 678 }
684 env = GNUNET_MQ_msg (msg, 679 env = GNUNET_MQ_msg(msg,
685 GNUNET_MESSAGE_TYPE_NAT_TEST); 680 GNUNET_MESSAGE_TYPE_NAT_TEST);
686 msg->dst_ipv4 = inet_addr (ah->stun_ip); 681 msg->dst_ipv4 = inet_addr(ah->stun_ip);
687 msg->dport = htons (ah->stun_port); 682 msg->dport = htons(ah->stun_port);
688 msg->data = port; 683 msg->data = port;
689 msg->is_tcp = htonl ((uint32_t) GNUNET_NO); 684 msg->is_tcp = htonl((uint32_t)GNUNET_NO);
690 GNUNET_MQ_send (ah->mq, 685 GNUNET_MQ_send(ah->mq,
691 env); 686 env);
692 if (NULL != ltask4) 687 if (NULL != ltask4)
693 { 688 {
694 GNUNET_SCHEDULER_cancel (ltask4); 689 GNUNET_SCHEDULER_cancel(ltask4);
695 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT, 690 ltask4 = GNUNET_SCHEDULER_add_read_net(NAT_SERVER_TIMEOUT,
696 lsock4, 691 lsock4,
697 &do_udp_read, 692 &do_udp_read,
698 ah); 693 ah);
699 } 694 }
700} 695}
701 696
702 697
@@ -706,26 +701,24 @@ test_nat_punched (struct GNUNET_NAT_AutoHandle *ah)
706 * @param ah auto setup context 701 * @param ah auto setup context
707 */ 702 */
708static void 703static void
709test_upnpc (struct GNUNET_NAT_AutoHandle *ah) 704test_upnpc(struct GNUNET_NAT_AutoHandle *ah)
710{ 705{
711
712 int have_upnpc; 706 int have_upnpc;
713 707
714 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret) 708 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
715 next_phase (ah); 709 next_phase(ah);
716 710
717 // test if upnpc is available 711 // test if upnpc is available
718 have_upnpc = (GNUNET_SYSERR != 712 have_upnpc = (GNUNET_SYSERR !=
719 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)); 713 GNUNET_OS_check_helper_binary("upnpc", GNUNET_NO, NULL));
720 //FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP 714 //FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP
721 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 715 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
722 (have_upnpc) 716 (have_upnpc)
723 ? _("upnpc found, enabling its use\n") 717 ? _("upnpc found, enabling its use\n")
724 : _("upnpc not found\n")); 718 : _("upnpc not found\n"));
725 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_UPNP", 719 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "ENABLE_UPNP",
726 (GNUNET_YES == have_upnpc) ? "YES" : "NO"); 720 (GNUNET_YES == have_upnpc) ? "YES" : "NO");
727 next_phase (ah); 721 next_phase(ah);
728
729} 722}
730 723
731 724
@@ -735,66 +728,66 @@ test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
735 * @param ah auto setup context 728 * @param ah auto setup context
736 */ 729 */
737static void 730static void
738test_icmp_server (struct GNUNET_NAT_AutoHandle *ah) 731test_icmp_server(struct GNUNET_NAT_AutoHandle *ah)
739{ 732{
740
741 int ext_ip; 733 int ext_ip;
742 int nated; 734 int nated;
743 int binary; 735 int binary;
744 char *tmp; 736 char *tmp;
745 char *helper; 737 char *helper;
738
746 ext_ip = GNUNET_NO; 739 ext_ip = GNUNET_NO;
747 nated = GNUNET_NO; 740 nated = GNUNET_NO;
748 binary = GNUNET_NO; 741 binary = GNUNET_NO;
749 742
750 tmp = NULL; 743 tmp = NULL;
751 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server"); 744 helper = GNUNET_OS_get_libexec_binary_path("gnunet-helper-nat-server");
752 if ( (GNUNET_OK == 745 if ((GNUNET_OK ==
753 GNUNET_CONFIGURATION_get_value_string (ah->cfg, 746 GNUNET_CONFIGURATION_get_value_string(ah->cfg,
754 "nat", 747 "nat",
755 "EXTERNAL_ADDRESS", 748 "EXTERNAL_ADDRESS",
756 &tmp)) && 749 &tmp)) &&
757 (0 < strlen (tmp)) ) 750 (0 < strlen(tmp)))
758 { 751 {
759 ext_ip = GNUNET_OK; 752 ext_ip = GNUNET_OK;
760 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 753 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
761 _("test_icmp_server not possible, as we have no public IPv4 address\n")); 754 _("test_icmp_server not possible, as we have no public IPv4 address\n"));
762 } 755 }
763 else 756 else
764 goto err; 757 goto err;
765 758
766 if (GNUNET_YES == 759 if (GNUNET_YES ==
767 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, 760 GNUNET_CONFIGURATION_get_value_yesno(ah->cfg,
768 "nat", 761 "nat",
769 "BEHIND_NAT")) 762 "BEHIND_NAT"))
770 { 763 {
771 nated = GNUNET_YES; 764 nated = GNUNET_YES;
772 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 765 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
773 _("test_icmp_server not possible, as we are not behind NAT\n")); 766 _("test_icmp_server not possible, as we are not behind NAT\n"));
774 } 767 }
775 else 768 else
776 goto err; 769 goto err;
777 770
778 if (GNUNET_YES == 771 if (GNUNET_YES ==
779 GNUNET_OS_check_helper_binary (helper, 772 GNUNET_OS_check_helper_binary(helper,
780 GNUNET_YES, 773 GNUNET_YES,
781 "-d 127.0.0.1" )) 774 "-d 127.0.0.1"))
782 { 775 {
783 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing 776 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing
784 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 777 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
785 _("No working gnunet-helper-nat-server found\n")); 778 _("No working gnunet-helper-nat-server found\n"));
786 } 779 }
787err: 780err:
788 GNUNET_free_non_null (tmp); 781 GNUNET_free_non_null(tmp);
789 GNUNET_free (helper); 782 GNUNET_free(helper);
790 783
791 if ( (GNUNET_OK == ext_ip) && 784 if ((GNUNET_OK == ext_ip) &&
792 (GNUNET_YES == nated) && 785 (GNUNET_YES == nated) &&
793 (GNUNET_OK == binary) ) 786 (GNUNET_OK == binary))
794 ah->task = GNUNET_SCHEDULER_add_now (&reversal_test, 787 ah->task = GNUNET_SCHEDULER_add_now(&reversal_test,
795 ah); 788 ah);
796 else 789 else
797 next_phase (ah); 790 next_phase(ah);
798} 791}
799 792
800 793
@@ -804,51 +797,51 @@ err:
804 * @param ah auto setup context 797 * @param ah auto setup context
805 */ 798 */
806static void 799static void
807test_icmp_client (struct GNUNET_NAT_AutoHandle *ah) 800test_icmp_client(struct GNUNET_NAT_AutoHandle *ah)
808{ 801{
809 char *tmp; 802 char *tmp;
810 char *helper; 803 char *helper;
811 804
812 tmp = NULL; 805 tmp = NULL;
813 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client"); 806 helper = GNUNET_OS_get_libexec_binary_path("gnunet-helper-nat-client");
814 if ( (GNUNET_OK == 807 if ((GNUNET_OK ==
815 GNUNET_CONFIGURATION_get_value_string (ah->cfg, 808 GNUNET_CONFIGURATION_get_value_string(ah->cfg,
816 "nat", 809 "nat",
817 "INTERNAL_ADDRESS", 810 "INTERNAL_ADDRESS",
818 &tmp)) && 811 &tmp)) &&
819 (0 < strlen (tmp)) ) 812 (0 < strlen(tmp)))
820 { 813 {
821 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 814 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
822 _("test_icmp_client not possible, as we have no internal IPv4 address\n")); 815 _("test_icmp_client not possible, as we have no internal IPv4 address\n"));
823 } 816 }
824 else 817 else
825 goto err; 818 goto err;
826 819
827 if (GNUNET_YES != 820 if (GNUNET_YES !=
828 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, 821 GNUNET_CONFIGURATION_get_value_yesno(ah->cfg,
829 "nat", 822 "nat",
830 "BEHIND_NAT")) 823 "BEHIND_NAT"))
831 { 824 {
832 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 825 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
833 _("test_icmp_server not possible, as we are not behind NAT\n")); 826 _("test_icmp_server not possible, as we are not behind NAT\n"));
834 } 827 }
835 else 828 else
836 goto err; 829 goto err;
837 830
838 if (GNUNET_YES == 831 if (GNUNET_YES ==
839 GNUNET_OS_check_helper_binary (helper, 832 GNUNET_OS_check_helper_binary(helper,
840 GNUNET_YES, 833 GNUNET_YES,
841 "-d 127.0.0.1 127.0.0.2 42")) 834 "-d 127.0.0.1 127.0.0.2 42"))
842 { 835 {
843 // none of these parameters are actually used in privilege testing mode 836 // none of these parameters are actually used in privilege testing mode
844 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 837 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
845 _("No working gnunet-helper-nat-server found\n")); 838 _("No working gnunet-helper-nat-server found\n"));
846 } 839 }
847err: 840err:
848 GNUNET_free_non_null (tmp); 841 GNUNET_free_non_null(tmp);
849 GNUNET_free (helper); 842 GNUNET_free(helper);
850 843
851 next_phase (ah); 844 next_phase(ah);
852} 845}
853 846
854 847
@@ -856,156 +849,161 @@ err:
856 * Run the next phase of the auto test. 849 * Run the next phase of the auto test.
857 */ 850 */
858static void 851static void
859next_phase (struct GNUNET_NAT_AutoHandle *ah) 852next_phase(struct GNUNET_NAT_AutoHandle *ah)
860{ 853{
861 struct GNUNET_CONFIGURATION_Handle *diff; 854 struct GNUNET_CONFIGURATION_Handle *diff;
862 855
863 ah->phase++; 856 ah->phase++;
864 switch (ah->phase) 857 switch (ah->phase)
865 {
866 case AUTO_INIT:
867 GNUNET_assert (0);
868 break;
869 case AUTO_EXTERNAL_IP:
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871 "Will run AUTO_EXTERNAL_IP\n");
872 test_external_ip (ah);
873 break;
874 case AUTO_STUN:
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Will run AUTO_STUN\n");
877 test_stun (ah);
878 break;
879 case AUTO_LOCAL_IP:
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Will run AUTO_LOCAL_IP\n");
882 test_local_ip (ah);
883 break;
884 case AUTO_NAT_PUNCHED:
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Will run AUTO_NAT_PUNCHED\n");
887 test_nat_punched (ah);
888 break;
889 case AUTO_UPNPC:
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Will run AUTO_UPNPC\n");
892 test_upnpc (ah);
893 break;
894 case AUTO_ICMP_SERVER:
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Will run AUTO_ICMP_SERVER\n");
897 test_icmp_server (ah);
898 break;
899 case AUTO_ICMP_CLIENT:
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Will run AUTO_ICMP_CLIENT\n");
902 test_icmp_client (ah);
903 break;
904 case AUTO_DONE:
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Done with tests\n");
907 if (!ah->internal_ip_is_public)
908 { 858 {
909 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 859 case AUTO_INIT:
910 "nat", 860 GNUNET_assert(0);
911 "BEHIND_NAT", 861 break;
912 "YES"); 862
913 863 case AUTO_EXTERNAL_IP:
914 if (ah->connected_back) 864 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
915 { 865 "Will run AUTO_EXTERNAL_IP\n");
916 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 866 test_external_ip(ah);
917 "nat", 867 break;
918 "PUNCHED_NAT", 868
919 "YES"); 869 case AUTO_STUN:
920 } 870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
921 else 871 "Will run AUTO_STUN\n");
922 { 872 test_stun(ah);
923 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 873 break;
924 "nat", 874
925 "PUNCHED_NAT", 875 case AUTO_LOCAL_IP:
926 "NO"); 876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
927 } 877 "Will run AUTO_LOCAL_IP\n");
928 878 test_local_ip(ah);
929 if (ah->stun_ip) 879 break;
930 { 880
931 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 881 case AUTO_NAT_PUNCHED:
932 "nat", 882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
933 "EXTERNAL_ADDRESS", 883 "Will run AUTO_NAT_PUNCHED\n");
934 ah->stun_ip); 884 test_nat_punched(ah);
935 if (ah->connected_back) 885 break;
886
887 case AUTO_UPNPC:
888 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
889 "Will run AUTO_UPNPC\n");
890 test_upnpc(ah);
891 break;
892
893 case AUTO_ICMP_SERVER:
894 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
895 "Will run AUTO_ICMP_SERVER\n");
896 test_icmp_server(ah);
897 break;
898
899 case AUTO_ICMP_CLIENT:
900 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
901 "Will run AUTO_ICMP_CLIENT\n");
902 test_icmp_client(ah);
903 break;
904
905 case AUTO_DONE:
906 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
907 "Done with tests\n");
908 if (!ah->internal_ip_is_public)
936 { 909 {
937 ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT; 910 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
938 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 911 "nat",
939 "nat", 912 "BEHIND_NAT",
940 "USE_STUN", 913 "YES");
941 "YES"); 914
915 if (ah->connected_back)
916 {
917 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
918 "nat",
919 "PUNCHED_NAT",
920 "YES");
921 }
922 else
923 {
924 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
925 "nat",
926 "PUNCHED_NAT",
927 "NO");
928 }
929
930 if (ah->stun_ip)
931 {
932 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
933 "nat",
934 "EXTERNAL_ADDRESS",
935 ah->stun_ip);
936 if (ah->connected_back)
937 {
938 ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT;
939 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
940 "nat",
941 "USE_STUN",
942 "YES");
943 }
944 else
945 {
946 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
947 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
948 "nat",
949 "USE_STUN",
950 "NO");
951 }
952 }
953 if (0 != ah->stun_port)
954 {
955 GNUNET_CONFIGURATION_set_value_number(ah->cfg,
956 "transport-udp",
957 "ADVERTISED_PORT",
958 ah->stun_port);
959 }
942 } 960 }
943 else
944 {
945 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
946 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
947 "nat",
948 "USE_STUN",
949 "NO");
950 }
951
952 }
953 if (0 != ah->stun_port)
954 {
955 GNUNET_CONFIGURATION_set_value_number (ah->cfg,
956 "transport-udp",
957 "ADVERTISED_PORT",
958 ah->stun_port);
959 }
960
961 }
962 else
963 {
964 //The internal IP is the same as public, but we didn't got a incoming connection
965 if (ah->connected_back)
966 {
967 ah->type = GNUNET_NAT_TYPE_NO_NAT;
968 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
969 "nat",
970 "BEHIND_NAT",
971 "NO");
972 }
973 else 961 else
974 {
975 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
976 "nat",
977 "BEHIND_NAT",
978 "YES");
979 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
980 if (ah->stun_ip)
981 { 962 {
982 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 963 //The internal IP is the same as public, but we didn't got a incoming connection
983 "nat", 964 if (ah->connected_back)
984 "EXTERNAL_ADDRESS", 965 {
985 ah->stun_ip); 966 ah->type = GNUNET_NAT_TYPE_NO_NAT;
967 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
968 "nat",
969 "BEHIND_NAT",
970 "NO");
971 }
972 else
973 {
974 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
975 "nat",
976 "BEHIND_NAT",
977 "YES");
978 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
979 if (ah->stun_ip)
980 {
981 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
982 "nat",
983 "EXTERNAL_ADDRESS",
984 ah->stun_ip);
985 }
986 if (0 != ah->stun_port)
987 {
988 GNUNET_CONFIGURATION_set_value_number(ah->cfg,
989 "transport-udp",
990 "ADVERTISED_PORT",
991 ah->stun_port);
992 }
993 }
986 } 994 }
987 if (0 != ah->stun_port)
988 {
989 GNUNET_CONFIGURATION_set_value_number (ah->cfg,
990 "transport-udp",
991 "ADVERTISED_PORT",
992 ah->stun_port);
993 995
994 } 996 diff = GNUNET_CONFIGURATION_get_diff(ah->initial_cfg,
995 } 997 ah->cfg);
996 }
997
998 diff = GNUNET_CONFIGURATION_get_diff (ah->initial_cfg,
999 ah->cfg);
1000 998
1001 999
1002 ah->fin_cb (ah->fin_cb_cls, 1000 ah->fin_cb(ah->fin_cb_cls,
1003 diff, 1001 diff,
1004 ah->ret, 1002 ah->ret,
1005 ah->type); 1003 ah->type);
1006 GNUNET_CONFIGURATION_destroy (diff); 1004 GNUNET_CONFIGURATION_destroy(diff);
1007 GNUNET_NAT_autoconfig_cancel (ah); 1005 GNUNET_NAT_autoconfig_cancel(ah);
1008 } 1006 }
1009} 1007}
1010 1008
1011 1009
@@ -1019,26 +1017,26 @@ next_phase (struct GNUNET_NAT_AutoHandle *ah)
1019 * @return handle to cancel operation 1017 * @return handle to cancel operation
1020 */ 1018 */
1021struct GNUNET_NAT_AutoHandle * 1019struct GNUNET_NAT_AutoHandle *
1022GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 1020GNUNET_NAT_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
1023 GNUNET_NAT_AutoResultCallback cb, 1021 GNUNET_NAT_AutoResultCallback cb,
1024 void *cb_cls) 1022 void *cb_cls)
1025{ 1023{
1026 struct GNUNET_NAT_AutoHandle *ah; 1024 struct GNUNET_NAT_AutoHandle *ah;
1027 1025
1028 ah = GNUNET_new (struct GNUNET_NAT_AutoHandle); 1026 ah = GNUNET_new(struct GNUNET_NAT_AutoHandle);
1029 ah->fin_cb = cb; 1027 ah->fin_cb = cb;
1030 ah->fin_cb_cls = cb_cls; 1028 ah->fin_cb_cls = cb_cls;
1031 ah->ret = GNUNET_NAT_ERROR_SUCCESS; 1029 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
1032 ah->cfg = GNUNET_CONFIGURATION_dup (cfg); 1030 ah->cfg = GNUNET_CONFIGURATION_dup(cfg);
1033 ah->initial_cfg = GNUNET_CONFIGURATION_dup (cfg); 1031 ah->initial_cfg = GNUNET_CONFIGURATION_dup(cfg);
1034 1032
1035 /* never use loopback addresses if user wanted autoconfiguration */ 1033 /* never use loopback addresses if user wanted autoconfiguration */
1036 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 1034 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
1037 "nat", 1035 "nat",
1038 "USE_LOCALADDR", 1036 "USE_LOCALADDR",
1039 "NO"); 1037 "NO");
1040 1038
1041 next_phase (ah); 1039 next_phase(ah);
1042 return ah; 1040 return ah;
1043} 1041}
1044 1042
@@ -1049,31 +1047,31 @@ GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1049 * @param ah handle for operation to abort 1047 * @param ah handle for operation to abort
1050 */ 1048 */
1051void 1049void
1052GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah) 1050GNUNET_NAT_autoconfig_cancel(struct GNUNET_NAT_AutoHandle *ah)
1053{ 1051{
1054 if (NULL != ah->tst) 1052 if (NULL != ah->tst)
1055 { 1053 {
1056 GNUNET_NAT_test_stop (ah->tst); 1054 GNUNET_NAT_test_stop(ah->tst);
1057 ah->tst = NULL; 1055 ah->tst = NULL;
1058 } 1056 }
1059 if (NULL != ah->eh) 1057 if (NULL != ah->eh)
1060 { 1058 {
1061 GNUNET_NAT_mini_get_external_ipv4_cancel (ah->eh); 1059 GNUNET_NAT_mini_get_external_ipv4_cancel(ah->eh);
1062 ah->eh = NULL; 1060 ah->eh = NULL;
1063 } 1061 }
1064 if (NULL != ah->mq) 1062 if (NULL != ah->mq)
1065 { 1063 {
1066 GNUNET_MQ_destroy (ah->mq); 1064 GNUNET_MQ_destroy(ah->mq);
1067 ah->mq = NULL; 1065 ah->mq = NULL;
1068 } 1066 }
1069 if (NULL != ah->task) 1067 if (NULL != ah->task)
1070 { 1068 {
1071 GNUNET_SCHEDULER_cancel (ah->task); 1069 GNUNET_SCHEDULER_cancel(ah->task);
1072 ah->task = NULL; 1070 ah->task = NULL;
1073 } 1071 }
1074 GNUNET_CONFIGURATION_destroy (ah->cfg); 1072 GNUNET_CONFIGURATION_destroy(ah->cfg);
1075 GNUNET_CONFIGURATION_destroy (ah->initial_cfg); 1073 GNUNET_CONFIGURATION_destroy(ah->initial_cfg);
1076 GNUNET_free (ah); 1074 GNUNET_free(ah);
1077} 1075}
1078 1076
1079 1077
diff --git a/src/nat-auto/nat-auto.h b/src/nat-auto/nat-auto.h
index 76d877dd3..6ef9b7e1e 100644
--- a/src/nat-auto/nat-auto.h
+++ b/src/nat-auto/nat-auto.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file src/nat-auto/nat-auto.h 22 * @file src/nat-auto/nat-auto.h
@@ -36,8 +36,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
36 * Request to test NAT traversal, sent to the gnunet-nat-server 36 * Request to test NAT traversal, sent to the gnunet-nat-server
37 * (not the service!). 37 * (not the service!).
38 */ 38 */
39struct GNUNET_NAT_AUTO_TestMessage 39struct GNUNET_NAT_AUTO_TestMessage {
40{
41 /** 40 /**
42 * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST 41 * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST
43 */ 42 */
@@ -62,35 +61,31 @@ struct GNUNET_NAT_AUTO_TestMessage
62 * #GNUNET_YES for TCP, #GNUNET_NO for UDP. 61 * #GNUNET_YES for TCP, #GNUNET_NO for UDP.
63 */ 62 */
64 int32_t is_tcp; 63 int32_t is_tcp;
65
66}; 64};
67 65
68 66
69/** 67/**
70 * Client requesting automatic configuration. 68 * Client requesting automatic configuration.
71 */ 69 */
72struct GNUNET_NAT_AUTO_AutoconfigRequestMessage 70struct GNUNET_NAT_AUTO_AutoconfigRequestMessage {
73{
74 /** 71 /**
75 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG 72 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG
76 */ 73 */
77 struct GNUNET_MessageHeader header; 74 struct GNUNET_MessageHeader header;
78 75
79 /* Followed by configuration (diff, serialized, compressed) */ 76 /* Followed by configuration (diff, serialized, compressed) */
80
81}; 77};
82 78
83 79
84/** 80/**
85 * Service responding with proposed configuration. 81 * Service responding with proposed configuration.
86 */ 82 */
87struct GNUNET_NAT_AUTO_AutoconfigResultMessage 83struct GNUNET_NAT_AUTO_AutoconfigResultMessage {
88{
89 /** 84 /**
90 * Header with type #GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT 85 * Header with type #GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT
91 */ 86 */
92 struct GNUNET_MessageHeader header; 87 struct GNUNET_MessageHeader header;
93 88
94 /** 89 /**
95 * An `enum GNUNET_NAT_StatusCode` in NBO. 90 * An `enum GNUNET_NAT_StatusCode` in NBO.
96 */ 91 */
diff --git a/src/nat-auto/nat_auto_api.c b/src/nat-auto/nat_auto_api.c
index d7c081e49..ae7bb6514 100644
--- a/src/nat-auto/nat_auto_api.c
+++ b/src/nat-auto/nat_auto_api.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @author Christian Grothoff 22 * @author Christian Grothoff
@@ -35,9 +35,7 @@
35/** 35/**
36 * Handle to auto-configuration in progress. 36 * Handle to auto-configuration in progress.
37 */ 37 */
38struct GNUNET_NAT_AUTO_AutoHandle 38struct GNUNET_NAT_AUTO_AutoHandle {
39{
40
41 /** 39 /**
42 * Configuration we use. 40 * Configuration we use.
43 */ 41 */
@@ -57,7 +55,6 @@ struct GNUNET_NAT_AUTO_AutoHandle
57 * Closure for @e arc. 55 * Closure for @e arc.
58 */ 56 */
59 void *arc_cls; 57 void *arc_cls;
60
61}; 58};
62 59
63 60
@@ -68,51 +65,70 @@ struct GNUNET_NAT_AUTO_AutoHandle
68 * @return point to a static string containing the error code 65 * @return point to a static string containing the error code
69 */ 66 */
70const char * 67const char *
71GNUNET_NAT_AUTO_status2string (enum GNUNET_NAT_StatusCode err) 68GNUNET_NAT_AUTO_status2string(enum GNUNET_NAT_StatusCode err)
72{ 69{
73 switch (err) 70 switch (err)
74 { 71 {
75 case GNUNET_NAT_ERROR_SUCCESS: 72 case GNUNET_NAT_ERROR_SUCCESS:
76 return _ ("Operation Successful"); 73 return _("Operation Successful");
77 case GNUNET_NAT_ERROR_IPC_FAILURE: 74
78 return _ ("IPC failure"); 75 case GNUNET_NAT_ERROR_IPC_FAILURE:
79 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR: 76 return _("IPC failure");
80 return _ ("Failure in network subsystem, check permissions."); 77
81 case GNUNET_NAT_ERROR_TIMEOUT: 78 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR:
82 return _ ("Encountered timeout while performing operation"); 79 return _("Failure in network subsystem, check permissions.");
83 case GNUNET_NAT_ERROR_NOT_ONLINE: 80
84 return _ ("detected that we are offline"); 81 case GNUNET_NAT_ERROR_TIMEOUT:
85 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND: 82 return _("Encountered timeout while performing operation");
86 return _ ("`upnpc` command not found"); 83
87 case GNUNET_NAT_ERROR_UPNPC_FAILED: 84 case GNUNET_NAT_ERROR_NOT_ONLINE:
88 return _ ("Failed to run `upnpc` command"); 85 return _("detected that we are offline");
89 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT: 86
90 return _ ("`upnpc' command took too long, process killed"); 87 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
91 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED: 88 return _("`upnpc` command not found");
92 return _ ("`upnpc' command failed to establish port mapping"); 89
93 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND: 90 case GNUNET_NAT_ERROR_UPNPC_FAILED:
94 return _ ("`external-ip' command not found"); 91 return _("Failed to run `upnpc` command");
95 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED: 92
96 return _ ("Failed to run `external-ip` command"); 93 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
97 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID: 94 return _("`upnpc' command took too long, process killed");
98 return _ ("`external-ip' command output invalid"); 95
99 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID: 96 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
100 return _ ("no valid address was returned by `external-ip'"); 97 return _("`upnpc' command failed to establish port mapping");
101 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO: 98
102 return _ ("Could not determine interface with internal/local network address"); 99 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
103 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND: 100 return _("`external-ip' command not found");
104 return _ ("No functioning gnunet-helper-nat-server installation found"); 101
105 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED: 102 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
106 return _ ("NAT test could not be initialized"); 103 return _("Failed to run `external-ip` command");
107 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT: 104
108 return _ ("NAT test timeout reached"); 105 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
109 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED: 106 return _("`external-ip' command output invalid");
110 return _ ("could not register NAT"); 107
111 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND: 108 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
112 return _ ("No working gnunet-helper-nat-client installation found"); 109 return _("no valid address was returned by `external-ip'");
113 default: 110
114 return "unknown status code"; 111 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO:
115 } 112 return _("Could not determine interface with internal/local network address");
113
114 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND:
115 return _("No functioning gnunet-helper-nat-server installation found");
116
117 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED:
118 return _("NAT test could not be initialized");
119
120 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT:
121 return _("NAT test timeout reached");
122
123 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED:
124 return _("could not register NAT");
125
126 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND:
127 return _("No working gnunet-helper-nat-client installation found");
128
129 default:
130 return "unknown status code";
131 }
116} 132}
117 133
118 134
@@ -124,8 +140,8 @@ GNUNET_NAT_AUTO_status2string (enum GNUNET_NAT_StatusCode err)
124 * @return #GNUNET_OK if @a res is well-formed (always for now) 140 * @return #GNUNET_OK if @a res is well-formed (always for now)
125 */ 141 */
126static int 142static int
127check_auto_result (void *cls, 143check_auto_result(void *cls,
128 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res) 144 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
129{ 145{
130 return GNUNET_OK; 146 return GNUNET_OK;
131} 147}
@@ -138,40 +154,40 @@ check_auto_result (void *cls,
138 * @param res the result 154 * @param res the result
139 */ 155 */
140static void 156static void
141handle_auto_result (void *cls, 157handle_auto_result(void *cls,
142 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res) 158 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
143{ 159{
144 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls; 160 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
145 size_t left; 161 size_t left;
146 struct GNUNET_CONFIGURATION_Handle *cfg; 162 struct GNUNET_CONFIGURATION_Handle *cfg;
147 enum GNUNET_NAT_Type type 163 enum GNUNET_NAT_Type type
148 = (enum GNUNET_NAT_Type) ntohl (res->type); 164 = (enum GNUNET_NAT_Type)ntohl(res->type);
149 enum GNUNET_NAT_StatusCode status 165 enum GNUNET_NAT_StatusCode status
150 = (enum GNUNET_NAT_StatusCode) ntohl (res->status_code); 166 = (enum GNUNET_NAT_StatusCode)ntohl(res->status_code);
151 167
152 left = ntohs (res->header.size) - sizeof (*res); 168 left = ntohs(res->header.size) - sizeof(*res);
153 cfg = GNUNET_CONFIGURATION_create (); 169 cfg = GNUNET_CONFIGURATION_create();
154 if (GNUNET_OK != 170 if (GNUNET_OK !=
155 GNUNET_CONFIGURATION_deserialize (cfg, 171 GNUNET_CONFIGURATION_deserialize(cfg,
156 (const char *) &res[1], 172 (const char *)&res[1],
157 left, 173 left,
158 NULL)) 174 NULL))
159 { 175 {
160 GNUNET_break (0); 176 GNUNET_break(0);
161 ah->arc (ah->arc_cls, 177 ah->arc(ah->arc_cls,
162 NULL, 178 NULL,
163 GNUNET_NAT_ERROR_IPC_FAILURE, 179 GNUNET_NAT_ERROR_IPC_FAILURE,
164 type); 180 type);
165 } 181 }
166 else 182 else
167 { 183 {
168 ah->arc (ah->arc_cls, 184 ah->arc(ah->arc_cls,
169 cfg, 185 cfg,
170 status, 186 status,
171 type); 187 type);
172 } 188 }
173 GNUNET_CONFIGURATION_destroy (cfg); 189 GNUNET_CONFIGURATION_destroy(cfg);
174 GNUNET_NAT_AUTO_autoconfig_cancel (ah); 190 GNUNET_NAT_AUTO_autoconfig_cancel(ah);
175} 191}
176 192
177 193
@@ -182,16 +198,16 @@ handle_auto_result (void *cls,
182 * @param error details about the error 198 * @param error details about the error
183 */ 199 */
184static void 200static void
185ah_error_handler (void *cls, 201ah_error_handler(void *cls,
186 enum GNUNET_MQ_Error error) 202 enum GNUNET_MQ_Error error)
187{ 203{
188 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls; 204 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
189 205
190 ah->arc (ah->arc_cls, 206 ah->arc(ah->arc_cls,
191 NULL, 207 NULL,
192 GNUNET_NAT_ERROR_IPC_FAILURE, 208 GNUNET_NAT_ERROR_IPC_FAILURE,
193 GNUNET_NAT_TYPE_UNKNOWN); 209 GNUNET_NAT_TYPE_UNKNOWN);
194 GNUNET_NAT_AUTO_autoconfig_cancel (ah); 210 GNUNET_NAT_AUTO_autoconfig_cancel(ah);
195} 211}
196 212
197 213
@@ -205,55 +221,55 @@ ah_error_handler (void *cls,
205 * @return handle to cancel operation 221 * @return handle to cancel operation
206 */ 222 */
207struct GNUNET_NAT_AUTO_AutoHandle * 223struct GNUNET_NAT_AUTO_AutoHandle *
208GNUNET_NAT_AUTO_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 224GNUNET_NAT_AUTO_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
209 GNUNET_NAT_AUTO_AutoResultCallback cb, 225 GNUNET_NAT_AUTO_AutoResultCallback cb,
210 void *cb_cls) 226 void *cb_cls)
211{ 227{
212 struct GNUNET_NAT_AUTO_AutoHandle *ah = GNUNET_new (struct GNUNET_NAT_AUTO_AutoHandle); 228 struct GNUNET_NAT_AUTO_AutoHandle *ah = GNUNET_new(struct GNUNET_NAT_AUTO_AutoHandle);
213 struct GNUNET_MQ_MessageHandler handlers[] = { 229 struct GNUNET_MQ_MessageHandler handlers[] = {
214 GNUNET_MQ_hd_var_size (auto_result, 230 GNUNET_MQ_hd_var_size(auto_result,
215 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT, 231 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT,
216 struct GNUNET_NAT_AUTO_AutoconfigResultMessage, 232 struct GNUNET_NAT_AUTO_AutoconfigResultMessage,
217 ah), 233 ah),
218 GNUNET_MQ_handler_end () 234 GNUNET_MQ_handler_end()
219 }; 235 };
220 struct GNUNET_MQ_Envelope *env; 236 struct GNUNET_MQ_Envelope *env;
221 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *req; 237 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *req;
222 char *buf; 238 char *buf;
223 size_t size; 239 size_t size;
224 240
225 buf = GNUNET_CONFIGURATION_serialize (cfg, 241 buf = GNUNET_CONFIGURATION_serialize(cfg,
226 &size); 242 &size);
227 if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof (*req)) 243 if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof(*req))
228 { 244 {
229 GNUNET_break (0); 245 GNUNET_break(0);
230 GNUNET_free (buf); 246 GNUNET_free(buf);
231 GNUNET_free (ah); 247 GNUNET_free(ah);
232 return NULL; 248 return NULL;
233 } 249 }
234 ah->arc = cb; 250 ah->arc = cb;
235 ah->arc_cls = cb_cls; 251 ah->arc_cls = cb_cls;
236 ah->mq = GNUNET_CLIENT_connect (cfg, 252 ah->mq = GNUNET_CLIENT_connect(cfg,
237 "nat", 253 "nat",
238 handlers, 254 handlers,
239 &ah_error_handler, 255 &ah_error_handler,
240 ah); 256 ah);
241 if (NULL == ah->mq) 257 if (NULL == ah->mq)
242 { 258 {
243 GNUNET_break (0); 259 GNUNET_break(0);
244 GNUNET_free (buf); 260 GNUNET_free(buf);
245 GNUNET_free (ah); 261 GNUNET_free(ah);
246 return NULL; 262 return NULL;
247 } 263 }
248 env = GNUNET_MQ_msg_extra (req, 264 env = GNUNET_MQ_msg_extra(req,
249 size, 265 size,
250 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG); 266 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG);
251 GNUNET_memcpy (&req[1], 267 GNUNET_memcpy(&req[1],
252 buf, 268 buf,
253 size); 269 size);
254 GNUNET_free (buf); 270 GNUNET_free(buf);
255 GNUNET_MQ_send (ah->mq, 271 GNUNET_MQ_send(ah->mq,
256 env); 272 env);
257 return ah; 273 return ah;
258} 274}
259 275
@@ -264,10 +280,10 @@ GNUNET_NAT_AUTO_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
264 * @param ah handle for operation to abort 280 * @param ah handle for operation to abort
265 */ 281 */
266void 282void
267GNUNET_NAT_AUTO_autoconfig_cancel (struct GNUNET_NAT_AUTO_AutoHandle *ah) 283GNUNET_NAT_AUTO_autoconfig_cancel(struct GNUNET_NAT_AUTO_AutoHandle *ah)
268{ 284{
269 GNUNET_MQ_destroy (ah->mq); 285 GNUNET_MQ_destroy(ah->mq);
270 GNUNET_free (ah); 286 GNUNET_free(ah);
271} 287}
272 288
273/* end of nat_api_auto.c */ 289/* end of nat_api_auto.c */
diff --git a/src/nat-auto/nat_auto_api_test.c b/src/nat-auto/nat_auto_api_test.c
index 56a2b8d72..daea3063c 100644
--- a/src/nat-auto/nat_auto_api_test.c
+++ b/src/nat-auto/nat_auto_api_test.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file nat/nat_auto_api_test.c 21 * @file nat/nat_auto_api_test.c
22 * @brief functions to test if the NAT configuration is successful at achieving NAT traversal (with the help of a gnunet-nat-server) 22 * @brief functions to test if the NAT configuration is successful at achieving NAT traversal (with the help of a gnunet-nat-server)
@@ -28,16 +28,15 @@
28#include "gnunet_nat_auto_service.h" 28#include "gnunet_nat_auto_service.h"
29#include "nat-auto.h" 29#include "nat-auto.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from (kind, "nat-auto", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from(kind, "nat-auto", __VA_ARGS__)
32 32
33#define NAT_SERVER_TIMEOUT \ 33#define NAT_SERVER_TIMEOUT \
34 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) 34 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
35 35
36/** 36/**
37 * Entry we keep for each incoming connection. 37 * Entry we keep for each incoming connection.
38 */ 38 */
39struct NatActivity 39struct NatActivity {
40{
41 /** 40 /**
42 * This is a doubly-linked list. 41 * This is a doubly-linked list.
43 */ 42 */
@@ -68,8 +67,7 @@ struct NatActivity
68/** 67/**
69 * Entry we keep for each connection to the gnunet-nat-service. 68 * Entry we keep for each connection to the gnunet-nat-service.
70 */ 69 */
71struct ClientActivity 70struct ClientActivity {
72{
73 /** 71 /**
74 * This is a doubly-linked list. 72 * This is a doubly-linked list.
75 */ 73 */
@@ -95,9 +93,7 @@ struct ClientActivity
95/** 93/**
96 * Handle to a NAT test. 94 * Handle to a NAT test.
97 */ 95 */
98struct GNUNET_NAT_AUTO_Test 96struct GNUNET_NAT_AUTO_Test {
99{
100
101 /** 97 /**
102 * Configuration used 98 * Configuration used
103 */ 99 */
@@ -184,22 +180,22 @@ struct GNUNET_NAT_AUTO_Test
184 * @param addrlen actual lenght of the @a addr 180 * @param addrlen actual lenght of the @a addr
185 */ 181 */
186static void 182static void
187reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen) 183reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen)
188{ 184{
189 struct GNUNET_NAT_AUTO_Test *h = cls; 185 struct GNUNET_NAT_AUTO_Test *h = cls;
190 const struct sockaddr_in *sa; 186 const struct sockaddr_in *sa;
191 187
192 if (sizeof (struct sockaddr_in) != addrlen) 188 if (sizeof(struct sockaddr_in) != addrlen)
193 return; 189 return;
194 sa = (const struct sockaddr_in *) addr; 190 sa = (const struct sockaddr_in *)addr;
195 if (h->data != sa->sin_port) 191 if (h->data != sa->sin_port)
196 { 192 {
197 LOG (GNUNET_ERROR_TYPE_DEBUG, 193 LOG(GNUNET_ERROR_TYPE_DEBUG,
198 "Received connection reversal request for wrong port\n"); 194 "Received connection reversal request for wrong port\n");
199 return; /* wrong port */ 195 return; /* wrong port */
200 } 196 }
201 /* report success */ 197 /* report success */
202 h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS); 198 h->report(h->report_cls, GNUNET_NAT_ERROR_SUCCESS);
203} 199}
204 200
205 201
@@ -210,31 +206,31 @@ reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
210 * @param cls the `struct GNUNET_NAT_AUTO_Test` 206 * @param cls the `struct GNUNET_NAT_AUTO_Test`
211 */ 207 */
212static void 208static void
213do_udp_read (void *cls) 209do_udp_read(void *cls)
214{ 210{
215 struct GNUNET_NAT_AUTO_Test *tst = cls; 211 struct GNUNET_NAT_AUTO_Test *tst = cls;
216 uint16_t data; 212 uint16_t data;
217 const struct GNUNET_SCHEDULER_TaskContext *tc; 213 const struct GNUNET_SCHEDULER_TaskContext *tc;
218 214
219 tc = GNUNET_SCHEDULER_get_task_context (); 215 tc = GNUNET_SCHEDULER_get_task_context();
220 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 216 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
221 tst->lsock, 217 tst->lsock,
222 &do_udp_read, 218 &do_udp_read,
223 tst); 219 tst);
224 if ((NULL != tc->write_ready) && 220 if ((NULL != tc->write_ready) &&
225 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) && 221 (GNUNET_NETWORK_fdset_isset(tc->read_ready, tst->lsock)) &&
226 (sizeof (data) == 222 (sizeof(data) ==
227 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data)))) 223 GNUNET_NETWORK_socket_recv(tst->lsock, &data, sizeof(data))))
228 { 224 {
229 if (data == tst->data) 225 if (data == tst->data)
230 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 226 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
231 else 227 else
232 LOG (GNUNET_ERROR_TYPE_DEBUG, 228 LOG(GNUNET_ERROR_TYPE_DEBUG,
233 "Received data mismatches expected value\n"); 229 "Received data mismatches expected value\n");
234 } 230 }
235 else 231 else
236 LOG (GNUNET_ERROR_TYPE_DEBUG, 232 LOG(GNUNET_ERROR_TYPE_DEBUG,
237 "Failed to receive data from inbound connection\n"); 233 "Failed to receive data from inbound connection\n");
238} 234}
239 235
240 236
@@ -245,33 +241,33 @@ do_udp_read (void *cls)
245 * @param cls the `struct NatActivity` 241 * @param cls the `struct NatActivity`
246 */ 242 */
247static void 243static void
248do_read (void *cls) 244do_read(void *cls)
249{ 245{
250 struct NatActivity *na = cls; 246 struct NatActivity *na = cls;
251 struct GNUNET_NAT_AUTO_Test *tst; 247 struct GNUNET_NAT_AUTO_Test *tst;
252 uint16_t data; 248 uint16_t data;
253 const struct GNUNET_SCHEDULER_TaskContext *tc; 249 const struct GNUNET_SCHEDULER_TaskContext *tc;
254 250
255 tc = GNUNET_SCHEDULER_get_task_context (); 251 tc = GNUNET_SCHEDULER_get_task_context();
256 na->rtask = NULL; 252 na->rtask = NULL;
257 tst = na->h; 253 tst = na->h;
258 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na); 254 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, na);
259 if ((NULL != tc->write_ready) && 255 if ((NULL != tc->write_ready) &&
260 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) && 256 (GNUNET_NETWORK_fdset_isset(tc->read_ready, na->sock)) &&
261 (sizeof (data) == 257 (sizeof(data) ==
262 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data)))) 258 GNUNET_NETWORK_socket_recv(na->sock, &data, sizeof(data))))
263 { 259 {
264 if (data == tst->data) 260 if (data == tst->data)
265 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 261 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
266 else 262 else
267 LOG (GNUNET_ERROR_TYPE_DEBUG, 263 LOG(GNUNET_ERROR_TYPE_DEBUG,
268 "Received data does not match expected value\n"); 264 "Received data does not match expected value\n");
269 } 265 }
270 else 266 else
271 LOG (GNUNET_ERROR_TYPE_DEBUG, 267 LOG(GNUNET_ERROR_TYPE_DEBUG,
272 "Failed to receive data from inbound connection\n"); 268 "Failed to receive data from inbound connection\n");
273 GNUNET_NETWORK_socket_close (na->sock); 269 GNUNET_NETWORK_socket_close(na->sock);
274 GNUNET_free (na); 270 GNUNET_free(na);
275} 271}
276 272
277 273
@@ -282,32 +278,32 @@ do_read (void *cls)
282 * @param cls the `struct GNUNET_NAT_AUTO_Test` 278 * @param cls the `struct GNUNET_NAT_AUTO_Test`
283 */ 279 */
284static void 280static void
285do_accept (void *cls) 281do_accept(void *cls)
286{ 282{
287 struct GNUNET_NAT_AUTO_Test *tst = cls; 283 struct GNUNET_NAT_AUTO_Test *tst = cls;
288 struct GNUNET_NETWORK_Handle *s; 284 struct GNUNET_NETWORK_Handle *s;
289 struct NatActivity *wl; 285 struct NatActivity *wl;
290 286
291 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 287 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
292 tst->lsock, 288 tst->lsock,
293 &do_accept, 289 &do_accept,
294 tst); 290 tst);
295 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL); 291 s = GNUNET_NETWORK_socket_accept(tst->lsock, NULL, NULL);
296 if (NULL == s) 292 if (NULL == s)
297 { 293 {
298 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept"); 294 GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO, "accept");
299 return; /* odd error */ 295 return; /* odd error */
300 } 296 }
301 LOG (GNUNET_ERROR_TYPE_DEBUG, 297 LOG(GNUNET_ERROR_TYPE_DEBUG,
302 "Got an inbound connection, waiting for data\n"); 298 "Got an inbound connection, waiting for data\n");
303 wl = GNUNET_new (struct NatActivity); 299 wl = GNUNET_new(struct NatActivity);
304 wl->sock = s; 300 wl->sock = s;
305 wl->h = tst; 301 wl->h = tst;
306 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 302 wl->rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
307 wl->sock, 303 wl->sock,
308 &do_read, 304 &do_read,
309 wl); 305 wl);
310 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl); 306 GNUNET_CONTAINER_DLL_insert(tst->na_head, tst->na_tail, wl);
311} 307}
312 308
313 309
@@ -319,14 +315,14 @@ do_accept (void *cls)
319 * @param error error code 315 * @param error error code
320 */ 316 */
321static void 317static void
322mq_error_handler (void *cls, enum GNUNET_MQ_Error error) 318mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
323{ 319{
324 struct ClientActivity *ca = cls; 320 struct ClientActivity *ca = cls;
325 struct GNUNET_NAT_AUTO_Test *tst = ca->h; 321 struct GNUNET_NAT_AUTO_Test *tst = ca->h;
326 322
327 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, ca); 323 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, ca);
328 GNUNET_MQ_destroy (ca->mq); 324 GNUNET_MQ_destroy(ca->mq);
329 GNUNET_free (ca); 325 GNUNET_free(ca);
330} 326}
331 327
332 328
@@ -343,12 +339,12 @@ mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
343 * @param addrlen actual length of the @a addr 339 * @param addrlen actual length of the @a addr
344 */ 340 */
345static void 341static void
346addr_cb (void *cls, 342addr_cb(void *cls,
347 void **app_ctx, 343 void **app_ctx,
348 int add_remove, 344 int add_remove,
349 enum GNUNET_NAT_AddressClass ac, 345 enum GNUNET_NAT_AddressClass ac,
350 const struct sockaddr *addr, 346 const struct sockaddr *addr,
351 socklen_t addrlen) 347 socklen_t addrlen)
352{ 348{
353 struct GNUNET_NAT_AUTO_Test *h = cls; 349 struct GNUNET_NAT_AUTO_Test *h = cls;
354 struct ClientActivity *ca; 350 struct ClientActivity *ca;
@@ -356,42 +352,42 @@ addr_cb (void *cls,
356 struct GNUNET_NAT_AUTO_TestMessage *msg; 352 struct GNUNET_NAT_AUTO_TestMessage *msg;
357 const struct sockaddr_in *sa; 353 const struct sockaddr_in *sa;
358 354
359 (void) app_ctx; 355 (void)app_ctx;
360 if (GNUNET_YES != add_remove) 356 if (GNUNET_YES != add_remove)
361 return; 357 return;
362 if (addrlen != sizeof (struct sockaddr_in)) 358 if (addrlen != sizeof(struct sockaddr_in))
363 { 359 {
364 LOG (GNUNET_ERROR_TYPE_DEBUG, 360 LOG(GNUNET_ERROR_TYPE_DEBUG,
365 "NAT test ignores IPv6 address `%s' returned from NAT library\n", 361 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
366 GNUNET_a2s (addr, addrlen)); 362 GNUNET_a2s(addr, addrlen));
367 return; /* ignore IPv6 here */ 363 return; /* ignore IPv6 here */
368 } 364 }
369 LOG (GNUNET_ERROR_TYPE_INFO, 365 LOG(GNUNET_ERROR_TYPE_INFO,
370 "Asking gnunet-nat-server to connect to `%s'\n", 366 "Asking gnunet-nat-server to connect to `%s'\n",
371 GNUNET_a2s (addr, addrlen)); 367 GNUNET_a2s(addr, addrlen));
372 368
373 ca = GNUNET_new (struct ClientActivity); 369 ca = GNUNET_new(struct ClientActivity);
374 ca->h = h; 370 ca->h = h;
375 ca->mq = GNUNET_CLIENT_connect (h->cfg, 371 ca->mq = GNUNET_CLIENT_connect(h->cfg,
376 "gnunet-nat-server", 372 "gnunet-nat-server",
377 NULL, 373 NULL,
378 &mq_error_handler, 374 &mq_error_handler,
379 ca); 375 ca);
380 if (NULL == ca->mq) 376 if (NULL == ca->mq)
381 { 377 {
382 GNUNET_free (ca); 378 GNUNET_free(ca);
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 379 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
384 _ ("Failed to connect to `gnunet-nat-server'\n")); 380 _("Failed to connect to `gnunet-nat-server'\n"));
385 return; 381 return;
386 } 382 }
387 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca); 383 GNUNET_CONTAINER_DLL_insert(h->ca_head, h->ca_tail, ca);
388 sa = (const struct sockaddr_in *) addr; 384 sa = (const struct sockaddr_in *)addr;
389 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAT_TEST); 385 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_NAT_TEST);
390 msg->dst_ipv4 = sa->sin_addr.s_addr; 386 msg->dst_ipv4 = sa->sin_addr.s_addr;
391 msg->dport = sa->sin_port; 387 msg->dport = sa->sin_port;
392 msg->data = h->data; 388 msg->data = h->data;
393 msg->is_tcp = htonl ((uint32_t) (h->proto == IPPROTO_TCP)); 389 msg->is_tcp = htonl((uint32_t)(h->proto == IPPROTO_TCP));
394 GNUNET_MQ_send (ca->mq, env); 390 GNUNET_MQ_send(ca->mq, env);
395} 391}
396 392
397 393
@@ -403,12 +399,12 @@ addr_cb (void *cls,
403 * @param cls handle to the timed out NAT test 399 * @param cls handle to the timed out NAT test
404 */ 400 */
405static void 401static void
406do_fail (void *cls) 402do_fail(void *cls)
407{ 403{
408 struct GNUNET_NAT_AUTO_Test *nh = cls; 404 struct GNUNET_NAT_AUTO_Test *nh = cls;
409 405
410 nh->ttask = NULL; 406 nh->ttask = NULL;
411 nh->report (nh->report_cls, nh->status); 407 nh->report(nh->report_cls, nh->status);
412} 408}
413 409
414 410
@@ -424,128 +420,128 @@ do_fail (void *cls)
424 * @return handle to cancel NAT test 420 * @return handle to cancel NAT test
425 */ 421 */
426struct GNUNET_NAT_AUTO_Test * 422struct GNUNET_NAT_AUTO_Test *
427GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 423GNUNET_NAT_AUTO_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
428 uint8_t proto, 424 uint8_t proto,
429 const char *section_name, 425 const char *section_name,
430 GNUNET_NAT_TestCallback report, 426 GNUNET_NAT_TestCallback report,
431 void *report_cls) 427 void *report_cls)
432{ 428{
433 struct GNUNET_NAT_AUTO_Test *nh; 429 struct GNUNET_NAT_AUTO_Test *nh;
434 unsigned long long bnd_port; 430 unsigned long long bnd_port;
435 struct sockaddr_in sa; 431 struct sockaddr_in sa;
436 const struct sockaddr *addrs[] = {(const struct sockaddr *) &sa}; 432 const struct sockaddr *addrs[] = { (const struct sockaddr *)&sa };
437 const socklen_t addrlens[] = {sizeof (sa)}; 433 const socklen_t addrlens[] = { sizeof(sa) };
438 434
439 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, 435 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg,
440 section_name, 436 section_name,
441 "PORT", 437 "PORT",
442 &bnd_port)) || 438 &bnd_port)) ||
443 (bnd_port > 65535)) 439 (bnd_port > 65535))
444 { 440 {
445 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 441 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
446 _ ("Failed to find valid PORT in section `%s'\n"), 442 _("Failed to find valid PORT in section `%s'\n"),
447 section_name); 443 section_name);
448 return NULL; 444 return NULL;
449 } 445 }
450 446
451 memset (&sa, 0, sizeof (sa)); 447 memset(&sa, 0, sizeof(sa));
452 sa.sin_family = AF_INET; 448 sa.sin_family = AF_INET;
453 sa.sin_port = htons ((uint16_t) bnd_port); 449 sa.sin_port = htons((uint16_t)bnd_port);
454#if HAVE_SOCKADDR_IN_SIN_LEN 450#if HAVE_SOCKADDR_IN_SIN_LEN
455 sa.sin_len = sizeof (sa); 451 sa.sin_len = sizeof(sa);
456#endif 452#endif
457 453
458 nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test); 454 nh = GNUNET_new(struct GNUNET_NAT_AUTO_Test);
459 nh->cfg = cfg; 455 nh->cfg = cfg;
460 nh->proto = proto; 456 nh->proto = proto;
461 nh->section_name = GNUNET_strdup (section_name); 457 nh->section_name = GNUNET_strdup(section_name);
462 nh->report = report; 458 nh->report = report;
463 nh->report_cls = report_cls; 459 nh->report_cls = report_cls;
464 nh->status = GNUNET_NAT_ERROR_SUCCESS; 460 nh->status = GNUNET_NAT_ERROR_SUCCESS;
465 if (0 == bnd_port) 461 if (0 == bnd_port)
466 {
467 nh->nat = GNUNET_NAT_register (cfg,
468 section_name,
469 proto,
470 0,
471 NULL,
472 NULL,
473 &addr_cb,
474 &reversal_cb,
475 nh);
476 }
477 else
478 {
479 nh->lsock =
480 GNUNET_NETWORK_socket_create (AF_INET,
481 (IPPROTO_UDP == proto) ? SOCK_DGRAM
482 : SOCK_STREAM,
483 proto);
484 if ((NULL == nh->lsock) ||
485 (GNUNET_OK != GNUNET_NETWORK_socket_bind (nh->lsock,
486 (const struct sockaddr *) &sa,
487 sizeof (sa))))
488 {
489 LOG (GNUNET_ERROR_TYPE_ERROR,
490 _ ("Failed to create socket bound to `%s' for NAT test: %s\n"),
491 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
492 strerror (errno));
493 if (NULL != nh->lsock)
494 {
495 GNUNET_NETWORK_socket_close (nh->lsock);
496 nh->lsock = NULL;
497 }
498 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
499 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail, nh);
500 return nh;
501 }
502 if (IPPROTO_TCP == proto)
503 { 462 {
504 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5)); 463 nh->nat = GNUNET_NAT_register(cfg,
505 nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 464 section_name,
506 nh->lsock, 465 proto,
507 &do_accept, 466 0,
508 nh); 467 NULL,
468 NULL,
469 &addr_cb,
470 &reversal_cb,
471 nh);
509 } 472 }
510 else 473 else
511 {
512 nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
513 nh->lsock,
514 &do_udp_read,
515 nh);
516 }
517 LOG (GNUNET_ERROR_TYPE_INFO,
518 "NAT test listens on port %llu (%s)\n",
519 bnd_port,
520 (IPPROTO_TCP == proto) ? "tcp" : "udp");
521 nh->nat = GNUNET_NAT_register (cfg,
522 section_name,
523 proto,
524 1,
525 addrs,
526 addrlens,
527 &addr_cb,
528 NULL,
529 nh);
530 if (NULL == nh->nat)
531 { 474 {
532 LOG (GNUNET_ERROR_TYPE_INFO, 475 nh->lsock =
533 _ ("NAT test failed to start NAT library\n")); 476 GNUNET_NETWORK_socket_create(AF_INET,
534 if (NULL != nh->ltask) 477 (IPPROTO_UDP == proto) ? SOCK_DGRAM
535 { 478 : SOCK_STREAM,
536 GNUNET_SCHEDULER_cancel (nh->ltask); 479 proto);
537 nh->ltask = NULL; 480 if ((NULL == nh->lsock) ||
538 } 481 (GNUNET_OK != GNUNET_NETWORK_socket_bind(nh->lsock,
539 if (NULL != nh->lsock) 482 (const struct sockaddr *)&sa,
540 { 483 sizeof(sa))))
541 GNUNET_NETWORK_socket_close (nh->lsock); 484 {
542 nh->lsock = NULL; 485 LOG(GNUNET_ERROR_TYPE_ERROR,
543 } 486 _("Failed to create socket bound to `%s' for NAT test: %s\n"),
544 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED; 487 GNUNET_a2s((const struct sockaddr *)&sa, sizeof(sa)),
545 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail, nh); 488 strerror(errno));
546 return nh; 489 if (NULL != nh->lsock)
490 {
491 GNUNET_NETWORK_socket_close(nh->lsock);
492 nh->lsock = NULL;
493 }
494 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
495 nh->ttask = GNUNET_SCHEDULER_add_now(&do_fail, nh);
496 return nh;
497 }
498 if (IPPROTO_TCP == proto)
499 {
500 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_listen(nh->lsock, 5));
501 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
502 nh->lsock,
503 &do_accept,
504 nh);
505 }
506 else
507 {
508 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
509 nh->lsock,
510 &do_udp_read,
511 nh);
512 }
513 LOG(GNUNET_ERROR_TYPE_INFO,
514 "NAT test listens on port %llu (%s)\n",
515 bnd_port,
516 (IPPROTO_TCP == proto) ? "tcp" : "udp");
517 nh->nat = GNUNET_NAT_register(cfg,
518 section_name,
519 proto,
520 1,
521 addrs,
522 addrlens,
523 &addr_cb,
524 NULL,
525 nh);
526 if (NULL == nh->nat)
527 {
528 LOG(GNUNET_ERROR_TYPE_INFO,
529 _("NAT test failed to start NAT library\n"));
530 if (NULL != nh->ltask)
531 {
532 GNUNET_SCHEDULER_cancel(nh->ltask);
533 nh->ltask = NULL;
534 }
535 if (NULL != nh->lsock)
536 {
537 GNUNET_NETWORK_socket_close(nh->lsock);
538 nh->lsock = NULL;
539 }
540 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
541 nh->ttask = GNUNET_SCHEDULER_add_now(&do_fail, nh);
542 return nh;
543 }
547 } 544 }
548 }
549 return nh; 545 return nh;
550} 546}
551 547
@@ -556,47 +552,47 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
556 * @param tst test to stop. 552 * @param tst test to stop.
557 */ 553 */
558void 554void
559GNUNET_NAT_AUTO_test_stop (struct GNUNET_NAT_AUTO_Test *tst) 555GNUNET_NAT_AUTO_test_stop(struct GNUNET_NAT_AUTO_Test *tst)
560{ 556{
561 struct NatActivity *pos; 557 struct NatActivity *pos;
562 struct ClientActivity *cpos; 558 struct ClientActivity *cpos;
563 559
564 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n"); 560 LOG(GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n");
565 while (NULL != (cpos = tst->ca_head)) 561 while (NULL != (cpos = tst->ca_head))
566 { 562 {
567 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos); 563 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, cpos);
568 GNUNET_MQ_destroy (cpos->mq); 564 GNUNET_MQ_destroy(cpos->mq);
569 GNUNET_free (cpos); 565 GNUNET_free(cpos);
570 } 566 }
571 while (NULL != (pos = tst->na_head)) 567 while (NULL != (pos = tst->na_head))
572 { 568 {
573 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos); 569 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, pos);
574 GNUNET_SCHEDULER_cancel (pos->rtask); 570 GNUNET_SCHEDULER_cancel(pos->rtask);
575 GNUNET_NETWORK_socket_close (pos->sock); 571 GNUNET_NETWORK_socket_close(pos->sock);
576 GNUNET_free (pos); 572 GNUNET_free(pos);
577 } 573 }
578 if (NULL != tst->ttask) 574 if (NULL != tst->ttask)
579 { 575 {
580 GNUNET_SCHEDULER_cancel (tst->ttask); 576 GNUNET_SCHEDULER_cancel(tst->ttask);
581 tst->ttask = NULL; 577 tst->ttask = NULL;
582 } 578 }
583 if (NULL != tst->ltask) 579 if (NULL != tst->ltask)
584 { 580 {
585 GNUNET_SCHEDULER_cancel (tst->ltask); 581 GNUNET_SCHEDULER_cancel(tst->ltask);
586 tst->ltask = NULL; 582 tst->ltask = NULL;
587 } 583 }
588 if (NULL != tst->lsock) 584 if (NULL != tst->lsock)
589 { 585 {
590 GNUNET_NETWORK_socket_close (tst->lsock); 586 GNUNET_NETWORK_socket_close(tst->lsock);
591 tst->lsock = NULL; 587 tst->lsock = NULL;
592 } 588 }
593 if (NULL != tst->nat) 589 if (NULL != tst->nat)
594 { 590 {
595 GNUNET_NAT_unregister (tst->nat); 591 GNUNET_NAT_unregister(tst->nat);
596 tst->nat = NULL; 592 tst->nat = NULL;
597 } 593 }
598 GNUNET_free (tst->section_name); 594 GNUNET_free(tst->section_name);
599 GNUNET_free (tst); 595 GNUNET_free(tst);
600} 596}
601 597
602/* end of nat_auto_api_test.c */ 598/* end of nat_auto_api_test.c */