aboutsummaryrefslogtreecommitdiff
path: root/src/nat
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/nat
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/nat')
-rw-r--r--src/nat/gnunet-helper-nat-client.c314
-rw-r--r--src/nat/gnunet-helper-nat-server.c560
-rw-r--r--src/nat/gnunet-nat.c424
-rw-r--r--src/nat/gnunet-service-nat.c2104
-rw-r--r--src/nat/gnunet-service-nat_externalip.c168
-rw-r--r--src/nat/gnunet-service-nat_externalip.h8
-rw-r--r--src/nat/gnunet-service-nat_helper.c359
-rw-r--r--src/nat/gnunet-service-nat_helper.h18
-rw-r--r--src/nat/gnunet-service-nat_mini.c584
-rw-r--r--src/nat/gnunet-service-nat_mini.h17
-rw-r--r--src/nat/gnunet-service-nat_stun.c171
-rw-r--r--src/nat/gnunet-service-nat_stun.h6
-rw-r--r--src/nat/nat.h21
-rw-r--r--src/nat/nat_api.c524
-rw-r--r--src/nat/nat_api_stun.c156
-rw-r--r--src/nat/nat_stun.h74
-rw-r--r--src/nat/test_nat.c119
-rw-r--r--src/nat/test_nat_mini.c78
-rw-r--r--src/nat/test_nat_test.c105
-rw-r--r--src/nat/test_stun.c266
20 files changed, 3069 insertions, 3007 deletions
diff --git a/src/nat/gnunet-helper-nat-client.c b/src/nat/gnunet-helper-nat-client.c
index d5d774964..5d12092e7 100644
--- a/src/nat/gnunet-helper-nat-client.c
+++ b/src/nat/gnunet-helper-nat-client.c
@@ -75,7 +75,9 @@
75 * @param src source of the copy, may be NULL if @a n is zero 75 * @param src source of the copy, may be NULL if @a n is zero
76 * @param n number of bytes to copy 76 * @param n number of bytes to copy
77 */ 77 */
78#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) 78#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void) memcpy (dst, src, \
79 n); \
80 } } while (0)
79 81
80/** 82/**
81 * Must match IP given in the server. 83 * Must match IP given in the server.
@@ -92,7 +94,8 @@
92/** 94/**
93 * IPv4 header. 95 * IPv4 header.
94 */ 96 */
95struct ip_header { 97struct ip_header
98{
96 /** 99 /**
97 * Version (4 bits) + Internet header length (4 bits) 100 * Version (4 bits) + Internet header length (4 bits)
98 */ 101 */
@@ -147,7 +150,8 @@ struct ip_header {
147/** 150/**
148 * Format of ICMP packet. 151 * Format of ICMP packet.
149 */ 152 */
150struct icmp_ttl_exceeded_header { 153struct icmp_ttl_exceeded_header
154{
151 uint8_t type; 155 uint8_t type;
152 156
153 uint8_t code; 157 uint8_t code;
@@ -159,7 +163,8 @@ struct icmp_ttl_exceeded_header {
159 /* followed by original payload */ 163 /* followed by original payload */
160}; 164};
161 165
162struct icmp_echo_header { 166struct icmp_echo_header
167{
163 uint8_t type; 168 uint8_t type;
164 169
165 uint8_t code; 170 uint8_t code;
@@ -172,7 +177,8 @@ struct icmp_echo_header {
172/** 177/**
173 * Beginning of UDP packet. 178 * Beginning of UDP packet.
174 */ 179 */
175struct udp_header { 180struct udp_header
181{
176 uint16_t src_port; 182 uint16_t src_port;
177 183
178 uint16_t dst_port; 184 uint16_t dst_port;
@@ -206,7 +212,7 @@ static uint16_t port;
206 * @return the CRC 16. 212 * @return the CRC 16.
207 */ 213 */
208static uint16_t 214static uint16_t
209calc_checksum(const uint16_t * data, unsigned int bytes) 215calc_checksum (const uint16_t *data, unsigned int bytes)
210{ 216{
211 uint32_t sum; 217 uint32_t sum;
212 unsigned int i; 218 unsigned int i;
@@ -215,7 +221,7 @@ calc_checksum(const uint16_t * data, unsigned int bytes)
215 for (i = 0; i < bytes / 2; i++) 221 for (i = 0; i < bytes / 2; i++)
216 sum += data[i]; 222 sum += data[i];
217 sum = (sum & 0xffff) + (sum >> 16); 223 sum = (sum & 0xffff) + (sum >> 16);
218 sum = htons(0xffff - sum); 224 sum = htons (0xffff - sum);
219 return sum; 225 return sum;
220} 226}
221 227
@@ -227,11 +233,11 @@ calc_checksum(const uint16_t * data, unsigned int bytes)
227 * @param other target address 233 * @param other target address
228 */ 234 */
229static void 235static void
230send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) 236send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
231{ 237{
232 char packet[sizeof(struct ip_header) * 2 + 238 char packet[sizeof(struct ip_header) * 2
233 sizeof(struct icmp_ttl_exceeded_header) + 239 + sizeof(struct icmp_ttl_exceeded_header)
234 sizeof(struct udp_header)]; 240 + sizeof(struct udp_header)];
235 struct ip_header ip_pkt; 241 struct ip_header ip_pkt;
236 struct icmp_ttl_exceeded_header icmp_pkt; 242 struct icmp_ttl_exceeded_header icmp_pkt;
237 struct udp_header udp_pkt; 243 struct udp_header udp_pkt;
@@ -246,9 +252,9 @@ send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other)
246#ifdef FREEBSD 252#ifdef FREEBSD
247 ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */ 253 ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */
248#else 254#else
249 ip_pkt.pkt_len = htons(sizeof(packet)); 255 ip_pkt.pkt_len = htons (sizeof(packet));
250#endif 256#endif
251 ip_pkt.id = htons(PACKET_ID); 257 ip_pkt.id = htons (PACKET_ID);
252 ip_pkt.flags_frag_offset = 0; 258 ip_pkt.flags_frag_offset = 0;
253 ip_pkt.ttl = 128; 259 ip_pkt.ttl = 128;
254 ip_pkt.proto = IPPROTO_ICMP; 260 ip_pkt.proto = IPPROTO_ICMP;
@@ -256,27 +262,27 @@ send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other)
256 ip_pkt.src_ip = my_ip->s_addr; 262 ip_pkt.src_ip = my_ip->s_addr;
257 ip_pkt.dst_ip = other->s_addr; 263 ip_pkt.dst_ip = other->s_addr;
258 ip_pkt.checksum = 264 ip_pkt.checksum =
259 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); 265 htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header)));
260 GNUNET_memcpy(&packet[off], 266 GNUNET_memcpy (&packet[off],
261 &ip_pkt, 267 &ip_pkt,
262 sizeof(struct ip_header)); 268 sizeof(struct ip_header));
263 off += sizeof(struct ip_header); 269 off += sizeof(struct ip_header);
264 270
265 icmp_pkt.type = ICMP_TIME_EXCEEDED; 271 icmp_pkt.type = ICMP_TIME_EXCEEDED;
266 icmp_pkt.code = 0; 272 icmp_pkt.code = 0;
267 icmp_pkt.checksum = 0; 273 icmp_pkt.checksum = 0;
268 icmp_pkt.unused = 0; 274 icmp_pkt.unused = 0;
269 GNUNET_memcpy(&packet[off], 275 GNUNET_memcpy (&packet[off],
270 &icmp_pkt, 276 &icmp_pkt,
271 sizeof(struct icmp_ttl_exceeded_header)); 277 sizeof(struct icmp_ttl_exceeded_header));
272 off += sizeof(struct icmp_ttl_exceeded_header); 278 off += sizeof(struct icmp_ttl_exceeded_header);
273 279
274 /* ip header of the presumably 'lost' udp packet */ 280 /* ip header of the presumably 'lost' udp packet */
275 ip_pkt.vers_ihl = 0x45; 281 ip_pkt.vers_ihl = 0x45;
276 ip_pkt.tos = 0; 282 ip_pkt.tos = 0;
277 ip_pkt.pkt_len = 283 ip_pkt.pkt_len =
278 htons(sizeof(struct ip_header) + sizeof(struct udp_header)); 284 htons (sizeof(struct ip_header) + sizeof(struct udp_header));
279 ip_pkt.id = htons(0); 285 ip_pkt.id = htons (0);
280 ip_pkt.flags_frag_offset = 0; 286 ip_pkt.flags_frag_offset = 0;
281 ip_pkt.ttl = 128; 287 ip_pkt.ttl = 128;
282 ip_pkt.proto = IPPROTO_UDP; 288 ip_pkt.proto = IPPROTO_UDP;
@@ -284,49 +290,50 @@ send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other)
284 ip_pkt.src_ip = other->s_addr; 290 ip_pkt.src_ip = other->s_addr;
285 ip_pkt.dst_ip = dummy.s_addr; 291 ip_pkt.dst_ip = dummy.s_addr;
286 ip_pkt.checksum = 292 ip_pkt.checksum =
287 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); 293 htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header)));
288 GNUNET_memcpy(&packet[off], 294 GNUNET_memcpy (&packet[off],
289 &ip_pkt, 295 &ip_pkt,
290 sizeof(struct ip_header)); 296 sizeof(struct ip_header));
291 off += sizeof(struct ip_header); 297 off += sizeof(struct ip_header);
292 298
293 /* build UDP header */ 299 /* build UDP header */
294 udp_pkt.src_port = htons(NAT_TRAV_PORT); 300 udp_pkt.src_port = htons (NAT_TRAV_PORT);
295 udp_pkt.dst_port = htons(NAT_TRAV_PORT); 301 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
296 udp_pkt.length = htons(port); 302 udp_pkt.length = htons (port);
297 udp_pkt.crc = 0; 303 udp_pkt.crc = 0;
298 GNUNET_memcpy(&packet[off], 304 GNUNET_memcpy (&packet[off],
299 &udp_pkt, 305 &udp_pkt,
300 sizeof(struct udp_header)); 306 sizeof(struct udp_header));
301 off += sizeof(struct udp_header); 307 off += sizeof(struct udp_header);
302 308
303 /* set ICMP checksum */ 309 /* set ICMP checksum */
304 icmp_pkt.checksum = 310 icmp_pkt.checksum =
305 htons(calc_checksum 311 htons (calc_checksum
306 ((uint16_t *)&packet[sizeof(struct ip_header)], 312 ((uint16_t *) &packet[sizeof(struct ip_header)],
307 sizeof(struct icmp_ttl_exceeded_header) + 313 sizeof(struct icmp_ttl_exceeded_header)
308 sizeof(struct ip_header) + sizeof(struct udp_header))); 314 + sizeof(struct ip_header) + sizeof(struct udp_header)));
309 GNUNET_memcpy(&packet[sizeof(struct ip_header)], 315 GNUNET_memcpy (&packet[sizeof(struct ip_header)],
310 &icmp_pkt, 316 &icmp_pkt,
311 sizeof(struct icmp_ttl_exceeded_header)); 317 sizeof(struct icmp_ttl_exceeded_header));
312 318
313 memset(&dst, 0, sizeof(dst)); 319 memset (&dst, 0, sizeof(dst));
314 dst.sin_family = AF_INET; 320 dst.sin_family = AF_INET;
315#if HAVE_SOCKADDR_IN_SIN_LEN 321#if HAVE_SOCKADDR_IN_SIN_LEN
316 dst.sin_len = sizeof(struct sockaddr_in); 322 dst.sin_len = sizeof(struct sockaddr_in);
317#endif 323#endif
318 dst.sin_addr = *other; 324 dst.sin_addr = *other;
319 err = 325 err =
320 sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, 326 sendto (rawsock, packet, sizeof(packet), 0, (struct sockaddr *) &dst,
321 sizeof(dst)); 327 sizeof(dst));
322 if (err < 0) 328 if (err < 0)
323 { 329 {
324 fprintf(stderr, "sendto failed: %s\n", strerror(errno)); 330 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
325 } 331 }
326 else if (sizeof(packet) != (size_t)err) 332 else if (sizeof(packet) != (size_t) err)
327 { 333 {
328 fprintf(stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long)off); 334 fprintf (stderr, "Error: partial send of ICMP message with size %lu\n",
329 } 335 (unsigned long) off);
336 }
330} 337}
331 338
332 339
@@ -337,15 +344,15 @@ send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other)
337 * @param other target address 344 * @param other target address
338 */ 345 */
339static void 346static void
340send_icmp(const struct in_addr *my_ip, const struct in_addr *other) 347send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
341{ 348{
342 struct ip_header ip_pkt; 349 struct ip_header ip_pkt;
343 struct icmp_ttl_exceeded_header icmp_ttl; 350 struct icmp_ttl_exceeded_header icmp_ttl;
344 struct icmp_echo_header icmp_echo; 351 struct icmp_echo_header icmp_echo;
345 struct sockaddr_in dst; 352 struct sockaddr_in dst;
346 char packet[sizeof(struct ip_header) * 2 + 353 char packet[sizeof(struct ip_header) * 2
347 sizeof(struct icmp_ttl_exceeded_header) + 354 + sizeof(struct icmp_ttl_exceeded_header)
348 sizeof(struct icmp_echo_header)]; 355 + sizeof(struct icmp_echo_header)];
349 size_t off; 356 size_t off;
350 int err; 357 int err;
351 358
@@ -356,9 +363,9 @@ send_icmp(const struct in_addr *my_ip, const struct in_addr *other)
356#ifdef FREEBSD 363#ifdef FREEBSD
357 ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */ 364 ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */
358#else 365#else
359 ip_pkt.pkt_len = htons(sizeof(packet)); 366 ip_pkt.pkt_len = htons (sizeof(packet));
360#endif 367#endif
361 ip_pkt.id = htons(PACKET_ID); 368 ip_pkt.id = htons (PACKET_ID);
362 ip_pkt.flags_frag_offset = 0; 369 ip_pkt.flags_frag_offset = 0;
363 ip_pkt.ttl = IPDEFTTL; 370 ip_pkt.ttl = IPDEFTTL;
364 ip_pkt.proto = IPPROTO_ICMP; 371 ip_pkt.proto = IPPROTO_ICMP;
@@ -366,10 +373,10 @@ send_icmp(const struct in_addr *my_ip, const struct in_addr *other)
366 ip_pkt.src_ip = my_ip->s_addr; 373 ip_pkt.src_ip = my_ip->s_addr;
367 ip_pkt.dst_ip = other->s_addr; 374 ip_pkt.dst_ip = other->s_addr;
368 ip_pkt.checksum = 375 ip_pkt.checksum =
369 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); 376 htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header)));
370 GNUNET_memcpy(&packet[off], 377 GNUNET_memcpy (&packet[off],
371 &ip_pkt, 378 &ip_pkt,
372 sizeof(struct ip_header)); 379 sizeof(struct ip_header));
373 off = sizeof(ip_pkt); 380 off = sizeof(ip_pkt);
374 381
375 /* icmp reply: time exceeded */ 382 /* icmp reply: time exceeded */
@@ -377,17 +384,17 @@ send_icmp(const struct in_addr *my_ip, const struct in_addr *other)
377 icmp_ttl.code = 0; 384 icmp_ttl.code = 0;
378 icmp_ttl.checksum = 0; 385 icmp_ttl.checksum = 0;
379 icmp_ttl.unused = 0; 386 icmp_ttl.unused = 0;
380 GNUNET_memcpy(&packet[off], 387 GNUNET_memcpy (&packet[off],
381 &icmp_ttl, 388 &icmp_ttl,
382 sizeof(struct icmp_ttl_exceeded_header)); 389 sizeof(struct icmp_ttl_exceeded_header));
383 off += sizeof(struct icmp_ttl_exceeded_header); 390 off += sizeof(struct icmp_ttl_exceeded_header);
384 391
385 /* ip header of the presumably 'lost' udp packet */ 392 /* ip header of the presumably 'lost' udp packet */
386 ip_pkt.vers_ihl = 0x45; 393 ip_pkt.vers_ihl = 0x45;
387 ip_pkt.tos = 0; 394 ip_pkt.tos = 0;
388 ip_pkt.pkt_len = 395 ip_pkt.pkt_len =
389 htons(sizeof(struct ip_header) + sizeof(struct icmp_echo_header)); 396 htons (sizeof(struct ip_header) + sizeof(struct icmp_echo_header));
390 ip_pkt.id = htons(PACKET_ID); 397 ip_pkt.id = htons (PACKET_ID);
391 ip_pkt.flags_frag_offset = 0; 398 ip_pkt.flags_frag_offset = 0;
392 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ 399 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
393 ip_pkt.proto = IPPROTO_ICMP; 400 ip_pkt.proto = IPPROTO_ICMP;
@@ -395,57 +402,57 @@ send_icmp(const struct in_addr *my_ip, const struct in_addr *other)
395 ip_pkt.dst_ip = dummy.s_addr; 402 ip_pkt.dst_ip = dummy.s_addr;
396 ip_pkt.checksum = 0; 403 ip_pkt.checksum = 0;
397 ip_pkt.checksum = 404 ip_pkt.checksum =
398 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); 405 htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header)));
399 GNUNET_memcpy(&packet[off], 406 GNUNET_memcpy (&packet[off],
400 &ip_pkt, 407 &ip_pkt,
401 sizeof(struct ip_header)); 408 sizeof(struct ip_header));
402 off += sizeof(struct ip_header); 409 off += sizeof(struct ip_header);
403 410
404 icmp_echo.type = ICMP_ECHO; 411 icmp_echo.type = ICMP_ECHO;
405 icmp_echo.code = 0; 412 icmp_echo.code = 0;
406 icmp_echo.reserved = htonl(port); 413 icmp_echo.reserved = htonl (port);
407 icmp_echo.checksum = 0; 414 icmp_echo.checksum = 0;
408 icmp_echo.checksum = 415 icmp_echo.checksum =
409 htons(calc_checksum 416 htons (calc_checksum
410 ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header))); 417 ((uint16_t *) &icmp_echo, sizeof(struct icmp_echo_header)));
411 GNUNET_memcpy(&packet[off], 418 GNUNET_memcpy (&packet[off],
412 &icmp_echo, 419 &icmp_echo,
413 sizeof(struct icmp_echo_header)); 420 sizeof(struct icmp_echo_header));
414 421
415 /* no go back to calculate ICMP packet checksum */ 422 /* no go back to calculate ICMP packet checksum */
416 off = sizeof(struct ip_header); 423 off = sizeof(struct ip_header);
417 icmp_ttl.checksum = 424 icmp_ttl.checksum =
418 htons(calc_checksum 425 htons (calc_checksum
419 ((uint16_t *)&packet[off], 426 ((uint16_t *) &packet[off],
420 sizeof(struct icmp_ttl_exceeded_header) + 427 sizeof(struct icmp_ttl_exceeded_header)
421 sizeof(struct ip_header) + sizeof(struct icmp_echo_header))); 428 + sizeof(struct ip_header) + sizeof(struct icmp_echo_header)));
422 GNUNET_memcpy(&packet[off], 429 GNUNET_memcpy (&packet[off],
423 &icmp_ttl, 430 &icmp_ttl,
424 sizeof(struct icmp_ttl_exceeded_header)); 431 sizeof(struct icmp_ttl_exceeded_header));
425 432
426 /* prepare for transmission */ 433 /* prepare for transmission */
427 memset(&dst, 0, sizeof(dst)); 434 memset (&dst, 0, sizeof(dst));
428 dst.sin_family = AF_INET; 435 dst.sin_family = AF_INET;
429#if HAVE_SOCKADDR_IN_SIN_LEN 436#if HAVE_SOCKADDR_IN_SIN_LEN
430 dst.sin_len = sizeof(struct sockaddr_in); 437 dst.sin_len = sizeof(struct sockaddr_in);
431#endif 438#endif
432 dst.sin_addr = *other; 439 dst.sin_addr = *other;
433 err = 440 err =
434 sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, 441 sendto (rawsock, packet, sizeof(packet), 0, (struct sockaddr *) &dst,
435 sizeof(dst)); 442 sizeof(dst));
436 if (err < 0) 443 if (err < 0)
437 { 444 {
438 fprintf(stderr, "sendto failed: %s\n", strerror(errno)); 445 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
439 } 446 }
440 else if (sizeof(packet) != (size_t)err) 447 else if (sizeof(packet) != (size_t) err)
441 { 448 {
442 fprintf(stderr, "Error: partial send of ICMP message\n"); 449 fprintf (stderr, "Error: partial send of ICMP message\n");
443 } 450 }
444} 451}
445 452
446 453
447int 454int
448main(int argc, char *const *argv) 455main (int argc, char *const *argv)
449{ 456{
450 const int one = 1; 457 const int one = 1;
451 struct in_addr external; 458 struct in_addr external;
@@ -456,80 +463,81 @@ main(int argc, char *const *argv)
456 int global_ret; 463 int global_ret;
457 464
458 /* Create an ICMP raw socket for writing (only operation that requires root) */ 465 /* Create an ICMP raw socket for writing (only operation that requires root) */
459 rawsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 466 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
460 raw_eno = errno; /* for later error checking */ 467 raw_eno = errno; /* for later error checking */
461 468
462 /* now drop root privileges */ 469 /* now drop root privileges */
463 uid = getuid(); 470 uid = getuid ();
464#ifdef HAVE_SETRESUID 471#ifdef HAVE_SETRESUID
465 if (0 != setresuid(uid, uid, uid)) 472 if (0 != setresuid (uid, uid, uid))
466 { 473 {
467 fprintf(stderr, "Failed to setresuid: %s\n", strerror(errno)); 474 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
468 global_ret = 1; 475 global_ret = 1;
469 goto cleanup; 476 goto cleanup;
470 } 477 }
471#else 478#else
472 if (0 != (setuid(uid) | seteuid(uid))) 479 if (0 != (setuid (uid) | seteuid (uid)))
473 { 480 {
474 fprintf(stderr, "Failed to setuid: %s\n", strerror(errno)); 481 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
475 global_ret = 2; 482 global_ret = 2;
476 goto cleanup; 483 goto cleanup;
477 } 484 }
478#endif 485#endif
479 if (-1 == rawsock) 486 if (-1 == rawsock)
480 { 487 {
481 fprintf(stderr, "Error opening RAW socket: %s\n", strerror(raw_eno)); 488 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno));
482 global_ret = 3; 489 global_ret = 3;
483 goto cleanup; 490 goto cleanup;
484 } 491 }
485 if (0 != 492 if (0 !=
486 setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char *)&one, sizeof(one))) 493 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one,
487 { 494 sizeof(one)))
488 fprintf(stderr, "setsockopt failed: %s\n", strerror(errno)); 495 {
489 global_ret = 4; 496 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
490 goto cleanup; 497 global_ret = 4;
491 } 498 goto cleanup;
499 }
492 if (0 != 500 if (0 !=
493 setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one))) 501 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof(one)))
494 { 502 {
495 fprintf(stderr, "setsockopt failed: %s\n", strerror(errno)); 503 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
496 global_ret = 5; 504 global_ret = 5;
497 goto cleanup; 505 goto cleanup;
498 } 506 }
499 507
500 if (4 != argc) 508 if (4 != argc)
501 { 509 {
502 fprintf(stderr, 510 fprintf (stderr,
503 "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); 511 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
504 global_ret = 6; 512 global_ret = 6;
505 goto cleanup; 513 goto cleanup;
506 } 514 }
507 if ((1 != inet_pton(AF_INET, argv[1], &external)) || 515 if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
508 (1 != inet_pton(AF_INET, argv[2], &target))) 516 (1 != inet_pton (AF_INET, argv[2], &target)))
509 { 517 {
510 fprintf(stderr, "Error parsing IPv4 address: %s\n", strerror(errno)); 518 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
511 global_ret = 7; 519 global_ret = 7;
512 goto cleanup; 520 goto cleanup;
513 } 521 }
514 if ((1 != sscanf(argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) 522 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
515 { 523 {
516 fprintf(stderr, "Error parsing port value `%s'\n", argv[3]); 524 fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
517 global_ret = 8; 525 global_ret = 8;
518 goto cleanup; 526 goto cleanup;
519 } 527 }
520 port = (uint16_t)p; 528 port = (uint16_t) p;
521 if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) 529 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
522 { 530 {
523 fprintf(stderr, "Internal error converting dummy IP to binary.\n"); 531 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
524 global_ret = 9; 532 global_ret = 9;
525 goto cleanup; 533 goto cleanup;
526 } 534 }
527 send_icmp(&external, &target); 535 send_icmp (&external, &target);
528 send_icmp_udp(&external, &target); 536 send_icmp_udp (&external, &target);
529 global_ret = 0; 537 global_ret = 0;
530cleanup: 538cleanup:
531 if (-1 != rawsock) 539 if (-1 != rawsock)
532 (void)close(rawsock); 540 (void) close (rawsock);
533 return global_ret; 541 return global_ret;
534} 542}
535 543
diff --git a/src/nat/gnunet-helper-nat-server.c b/src/nat/gnunet-helper-nat-server.c
index f087590c8..38d7d22c8 100644
--- a/src/nat/gnunet-helper-nat-server.c
+++ b/src/nat/gnunet-helper-nat-server.c
@@ -79,7 +79,9 @@
79 * @param src source of the copy, may be NULL if @a n is zero 79 * @param src source of the copy, may be NULL if @a n is zero
80 * @param n number of bytes to copy 80 * @param n number of bytes to copy
81 */ 81 */
82#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) 82#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void) memcpy (dst, src, \
83 n); \
84 } } while (0)
83 85
84/** 86/**
85 * Should we print some debug output? 87 * Should we print some debug output?
@@ -109,7 +111,8 @@
109/** 111/**
110 * IPv4 header. 112 * IPv4 header.
111 */ 113 */
112struct ip_header { 114struct ip_header
115{
113 /** 116 /**
114 * Version (4 bits) + Internet header length (4 bits) 117 * Version (4 bits) + Internet header length (4 bits)
115 */ 118 */
@@ -164,7 +167,8 @@ struct ip_header {
164/** 167/**
165 * Format of ICMP packet. 168 * Format of ICMP packet.
166 */ 169 */
167struct icmp_ttl_exceeded_header { 170struct icmp_ttl_exceeded_header
171{
168 uint8_t type; 172 uint8_t type;
169 173
170 uint8_t code; 174 uint8_t code;
@@ -176,7 +180,8 @@ struct icmp_ttl_exceeded_header {
176 /* followed by original payload */ 180 /* followed by original payload */
177}; 181};
178 182
179struct icmp_echo_header { 183struct icmp_echo_header
184{
180 uint8_t type; 185 uint8_t type;
181 186
182 uint8_t code; 187 uint8_t code;
@@ -190,7 +195,8 @@ struct icmp_echo_header {
190/** 195/**
191 * Beginning of UDP packet. 196 * Beginning of UDP packet.
192 */ 197 */
193struct udp_header { 198struct udp_header
199{
194 uint16_t src_port; 200 uint16_t src_port;
195 201
196 uint16_t dst_port; 202 uint16_t dst_port;
@@ -229,7 +235,7 @@ static struct in_addr dummy;
229 * @return the CRC 16. 235 * @return the CRC 16.
230 */ 236 */
231static uint16_t 237static uint16_t
232calc_checksum(const uint16_t * data, unsigned int bytes) 238calc_checksum (const uint16_t *data, unsigned int bytes)
233{ 239{
234 uint32_t sum; 240 uint32_t sum;
235 unsigned int i; 241 unsigned int i;
@@ -238,7 +244,7 @@ calc_checksum(const uint16_t * data, unsigned int bytes)
238 for (i = 0; i < bytes / 2; i++) 244 for (i = 0; i < bytes / 2; i++)
239 sum += data[i]; 245 sum += data[i];
240 sum = (sum & 0xffff) + (sum >> 16); 246 sum = (sum & 0xffff) + (sum >> 16);
241 sum = htons(0xffff - sum); 247 sum = htons (0xffff - sum);
242 return sum; 248 return sum;
243} 249}
244 250
@@ -249,7 +255,7 @@ calc_checksum(const uint16_t * data, unsigned int bytes)
249 * @param my_ip source address (our ip address) 255 * @param my_ip source address (our ip address)
250 */ 256 */
251static void 257static void
252send_icmp_echo(const struct in_addr *my_ip) 258send_icmp_echo (const struct in_addr *my_ip)
253{ 259{
254 char packet[sizeof(struct ip_header) + sizeof(struct icmp_echo_header)]; 260 char packet[sizeof(struct ip_header) + sizeof(struct icmp_echo_header)];
255 struct icmp_echo_header icmp_echo; 261 struct icmp_echo_header icmp_echo;
@@ -261,8 +267,8 @@ send_icmp_echo(const struct in_addr *my_ip)
261 off = 0; 267 off = 0;
262 ip_pkt.vers_ihl = 0x45; 268 ip_pkt.vers_ihl = 0x45;
263 ip_pkt.tos = 0; 269 ip_pkt.tos = 0;
264 ip_pkt.pkt_len = htons(sizeof(packet)); 270 ip_pkt.pkt_len = htons (sizeof(packet));
265 ip_pkt.id = htons(PACKET_ID); 271 ip_pkt.id = htons (PACKET_ID);
266 ip_pkt.flags_frag_offset = 0; 272 ip_pkt.flags_frag_offset = 0;
267 ip_pkt.ttl = IPDEFTTL; 273 ip_pkt.ttl = IPDEFTTL;
268 ip_pkt.proto = IPPROTO_ICMP; 274 ip_pkt.proto = IPPROTO_ICMP;
@@ -270,11 +276,11 @@ send_icmp_echo(const struct in_addr *my_ip)
270 ip_pkt.src_ip = my_ip->s_addr; 276 ip_pkt.src_ip = my_ip->s_addr;
271 ip_pkt.dst_ip = dummy.s_addr; 277 ip_pkt.dst_ip = dummy.s_addr;
272 ip_pkt.checksum = 278 ip_pkt.checksum =
273 htons(calc_checksum((uint16_t *)&ip_pkt, 279 htons (calc_checksum ((uint16_t *) &ip_pkt,
274 sizeof(struct ip_header))); 280 sizeof(struct ip_header)));
275 GNUNET_memcpy(&packet[off], 281 GNUNET_memcpy (&packet[off],
276 &ip_pkt, 282 &ip_pkt,
277 sizeof(struct ip_header)); 283 sizeof(struct ip_header));
278 off += sizeof(struct ip_header); 284 off += sizeof(struct ip_header);
279 285
280 icmp_echo.type = ICMP_ECHO; 286 icmp_echo.type = ICMP_ECHO;
@@ -282,39 +288,39 @@ send_icmp_echo(const struct in_addr *my_ip)
282 icmp_echo.checksum = 0; 288 icmp_echo.checksum = 0;
283 icmp_echo.reserved = 0; 289 icmp_echo.reserved = 0;
284 icmp_echo.checksum = 290 icmp_echo.checksum =
285 htons(calc_checksum 291 htons (calc_checksum
286 ((uint16_t *)&icmp_echo, 292 ((uint16_t *) &icmp_echo,
287 sizeof(struct icmp_echo_header))); 293 sizeof(struct icmp_echo_header)));
288 GNUNET_memcpy(&packet[off], 294 GNUNET_memcpy (&packet[off],
289 &icmp_echo, 295 &icmp_echo,
290 sizeof(struct icmp_echo_header)); 296 sizeof(struct icmp_echo_header));
291 off += sizeof(struct icmp_echo_header); 297 off += sizeof(struct icmp_echo_header);
292 298
293 memset(&dst, 0, sizeof(dst)); 299 memset (&dst, 0, sizeof(dst));
294 dst.sin_family = AF_INET; 300 dst.sin_family = AF_INET;
295#if HAVE_SOCKADDR_IN_SIN_LEN 301#if HAVE_SOCKADDR_IN_SIN_LEN
296 dst.sin_len = sizeof(struct sockaddr_in); 302 dst.sin_len = sizeof(struct sockaddr_in);
297#endif 303#endif
298 dst.sin_addr = dummy; 304 dst.sin_addr = dummy;
299 err = sendto(rawsock, 305 err = sendto (rawsock,
300 packet, 306 packet,
301 off, 307 off,
302 0, 308 0,
303 (struct sockaddr *)&dst, 309 (struct sockaddr *) &dst,
304 sizeof(dst)); 310 sizeof(dst));
305 if (err < 0) 311 if (err < 0)
306 { 312 {
307#if VERBOSE 313#if VERBOSE
308 fprintf(stderr, 314 fprintf (stderr,
309 "sendto failed: %s\n", 315 "sendto failed: %s\n",
310 strerror(errno)); 316 strerror (errno));
311#endif 317#endif
312 } 318 }
313 else if (sizeof(packet) != err) 319 else if (sizeof(packet) != err)
314 { 320 {
315 fprintf(stderr, 321 fprintf (stderr,
316 "Error: partial send of ICMP message\n"); 322 "Error: partial send of ICMP message\n");
317 } 323 }
318} 324}
319 325
320 326
@@ -322,37 +328,37 @@ send_icmp_echo(const struct in_addr *my_ip)
322 * Send a UDP message to the dummy IP. 328 * Send a UDP message to the dummy IP.
323 */ 329 */
324static void 330static void
325send_udp() 331send_udp ()
326{ 332{
327 struct sockaddr_in dst; 333 struct sockaddr_in dst;
328 ssize_t err; 334 ssize_t err;
329 335
330 memset(&dst, 0, sizeof(dst)); 336 memset (&dst, 0, sizeof(dst));
331 dst.sin_family = AF_INET; 337 dst.sin_family = AF_INET;
332#if HAVE_SOCKADDR_IN_SIN_LEN 338#if HAVE_SOCKADDR_IN_SIN_LEN
333 dst.sin_len = sizeof(struct sockaddr_in); 339 dst.sin_len = sizeof(struct sockaddr_in);
334#endif 340#endif
335 dst.sin_addr = dummy; 341 dst.sin_addr = dummy;
336 dst.sin_port = htons(NAT_TRAV_PORT); 342 dst.sin_port = htons (NAT_TRAV_PORT);
337 err = sendto(udpsock, 343 err = sendto (udpsock,
338 NULL, 344 NULL,
339 0, 345 0,
340 0, 346 0,
341 (struct sockaddr *)&dst, 347 (struct sockaddr *) &dst,
342 sizeof(dst)); 348 sizeof(dst));
343 if (err < 0) 349 if (err < 0)
344 { 350 {
345#if VERBOSE 351#if VERBOSE
346 fprintf(stderr, 352 fprintf (stderr,
347 "sendto failed: %s\n", 353 "sendto failed: %s\n",
348 strerror(errno)); 354 strerror (errno));
349#endif 355#endif
350 } 356 }
351 else if (0 != err) 357 else if (0 != err)
352 { 358 {
353 fprintf(stderr, 359 fprintf (stderr,
354 "Error: partial send of ICMP message\n"); 360 "Error: partial send of ICMP message\n");
355 } 361 }
356} 362}
357 363
358 364
@@ -360,7 +366,7 @@ send_udp()
360 * We've received an ICMP response. Process it. 366 * We've received an ICMP response. Process it.
361 */ 367 */
362static void 368static void
363process_icmp_response() 369process_icmp_response ()
364{ 370{
365 char buf[65536]; 371 char buf[65536];
366 ssize_t have; 372 ssize_t have;
@@ -372,96 +378,96 @@ process_icmp_response()
372 size_t off; 378 size_t off;
373 uint16_t port; 379 uint16_t port;
374 380
375 have = read(icmpsock, buf, sizeof(buf)); 381 have = read (icmpsock, buf, sizeof(buf));
376 if (-1 == have) 382 if (-1 == have)
377 { 383 {
378 fprintf(stderr, 384 fprintf (stderr,
379 "Error reading raw socket: %s\n", 385 "Error reading raw socket: %s\n",
380 strerror(errno)); 386 strerror (errno));
381 return; 387 return;
382 } 388 }
383#if VERBOSE 389#if VERBOSE
384 fprintf(stderr, 390 fprintf (stderr,
385 "Received message of %u bytes\n", 391 "Received message of %u bytes\n",
386 (unsigned int)have); 392 (unsigned int) have);
387#endif 393#endif
388 if (have < 394 if (have <
389 (ssize_t)(sizeof(struct ip_header) + 395 (ssize_t) (sizeof(struct ip_header)
390 sizeof(struct icmp_ttl_exceeded_header) + 396 + sizeof(struct icmp_ttl_exceeded_header)
391 sizeof(struct ip_header))) 397 + sizeof(struct ip_header)))
392 { 398 {
393 /* malformed */ 399 /* malformed */
394 return; 400 return;
395 } 401 }
396 off = 0; 402 off = 0;
397 GNUNET_memcpy(&ip_pkt, 403 GNUNET_memcpy (&ip_pkt,
398 &buf[off], 404 &buf[off],
399 sizeof(struct ip_header)); 405 sizeof(struct ip_header));
400 off += sizeof(struct ip_header); 406 off += sizeof(struct ip_header);
401 GNUNET_memcpy(&icmp_ttl, 407 GNUNET_memcpy (&icmp_ttl,
402 &buf[off], 408 &buf[off],
403 sizeof(struct icmp_ttl_exceeded_header)); 409 sizeof(struct icmp_ttl_exceeded_header));
404 off += sizeof(struct icmp_ttl_exceeded_header); 410 off += sizeof(struct icmp_ttl_exceeded_header);
405 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code)) 411 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
406 { 412 {
407 /* different type than what we want */ 413 /* different type than what we want */
408 return; 414 return;
409 } 415 }
410 /* grab source IP of 1st IP header */ 416 /* grab source IP of 1st IP header */
411 source_ip.s_addr = ip_pkt.src_ip; 417 source_ip.s_addr = ip_pkt.src_ip;
412 418
413 /* skip 2nd IP header */ 419 /* skip 2nd IP header */
414 GNUNET_memcpy(&ip_pkt, 420 GNUNET_memcpy (&ip_pkt,
415 &buf[off], 421 &buf[off],
416 sizeof(struct ip_header)); 422 sizeof(struct ip_header));
417 off += sizeof(struct ip_header); 423 off += sizeof(struct ip_header);
418 424
419 switch (ip_pkt.proto) 425 switch (ip_pkt.proto)
426 {
427 case IPPROTO_ICMP:
428 if (have !=
429 (sizeof(struct ip_header) * 2
430 + sizeof(struct icmp_ttl_exceeded_header)
431 + sizeof(struct icmp_echo_header)))
420 { 432 {
421 case IPPROTO_ICMP: 433 /* malformed */
422 if (have != 434 return;
423 (sizeof(struct ip_header) * 2 + 435 }
424 sizeof(struct icmp_ttl_exceeded_header) + 436 /* grab ICMP ECHO content */
425 sizeof(struct icmp_echo_header))) 437 GNUNET_memcpy (&icmp_echo,
426 { 438 &buf[off],
427 /* malformed */ 439 sizeof(struct icmp_echo_header));
428 return; 440 port = (uint16_t) ntohl (icmp_echo.reserved);
429 } 441 break;
430 /* grab ICMP ECHO content */ 442
431 GNUNET_memcpy(&icmp_echo, 443 case IPPROTO_UDP:
432 &buf[off], 444 if (have !=
433 sizeof(struct icmp_echo_header)); 445 (sizeof(struct ip_header) * 2
434 port = (uint16_t)ntohl(icmp_echo.reserved); 446 + sizeof(struct icmp_ttl_exceeded_header) + sizeof(struct udp_header)))
435 break; 447 {
436 448 /* malformed */
437 case IPPROTO_UDP:
438 if (have !=
439 (sizeof(struct ip_header) * 2 +
440 sizeof(struct icmp_ttl_exceeded_header) + sizeof(struct udp_header)))
441 {
442 /* malformed */
443 return;
444 }
445 /* grab UDP content */
446 GNUNET_memcpy(&udp_pkt,
447 &buf[off],
448 sizeof(struct udp_header));
449 port = ntohs(udp_pkt.length);
450 break;
451
452 default:
453 /* different type than what we want */
454 return; 449 return;
455 } 450 }
451 /* grab UDP content */
452 GNUNET_memcpy (&udp_pkt,
453 &buf[off],
454 sizeof(struct udp_header));
455 port = ntohs (udp_pkt.length);
456 break;
457
458 default:
459 /* different type than what we want */
460 return;
461 }
456 462
457 if (port == 0) 463 if (port == 0)
458 fprintf(stdout, "%s\n", 464 fprintf (stdout, "%s\n",
459 inet_ntop(AF_INET, &source_ip, buf, sizeof(buf))); 465 inet_ntop (AF_INET, &source_ip, buf, sizeof(buf)));
460 else 466 else
461 fprintf(stdout, "%s:%u\n", 467 fprintf (stdout, "%s:%u\n",
462 inet_ntop(AF_INET, &source_ip, buf, sizeof(buf)), 468 inet_ntop (AF_INET, &source_ip, buf, sizeof(buf)),
463 (unsigned int)port); 469 (unsigned int) port);
464 fflush(stdout); 470 fflush (stdout);
465} 471}
466 472
467 473
@@ -471,34 +477,34 @@ process_icmp_response()
471 * @return -1 on error, 0 on success 477 * @return -1 on error, 0 on success
472 */ 478 */
473static int 479static int
474setup_raw_socket() 480setup_raw_socket ()
475{ 481{
476 const int one = 1; 482 const int one = 1;
477 483
478 if (-1 == 484 if (-1 ==
479 setsockopt(rawsock, 485 setsockopt (rawsock,
480 SOL_SOCKET, 486 SOL_SOCKET,
481 SO_BROADCAST, 487 SO_BROADCAST,
482 (char *)&one, 488 (char *) &one,
483 sizeof(one))) 489 sizeof(one)))
484 { 490 {
485 fprintf(stderr, 491 fprintf (stderr,
486 "setsockopt failed: %s\n", 492 "setsockopt failed: %s\n",
487 strerror(errno)); 493 strerror (errno));
488 return -1; 494 return -1;
489 } 495 }
490 if (-1 == 496 if (-1 ==
491 setsockopt(rawsock, 497 setsockopt (rawsock,
492 IPPROTO_IP, 498 IPPROTO_IP,
493 IP_HDRINCL, 499 IP_HDRINCL,
494 (char *)&one, 500 (char *) &one,
495 sizeof(one))) 501 sizeof(one)))
496 { 502 {
497 fprintf(stderr, 503 fprintf (stderr,
498 "setsockopt failed: %s\n", 504 "setsockopt failed: %s\n",
499 strerror(errno)); 505 strerror (errno));
500 return -1; 506 return -1;
501 } 507 }
502 return 0; 508 return 0;
503} 509}
504 510
@@ -510,45 +516,45 @@ setup_raw_socket()
510 * @return -1 on error 516 * @return -1 on error
511 */ 517 */
512static int 518static int
513make_udp_socket(const struct in_addr *my_ip) 519make_udp_socket (const struct in_addr *my_ip)
514{ 520{
515 int ret; 521 int ret;
516 struct sockaddr_in addr; 522 struct sockaddr_in addr;
517 523
518 ret = socket(AF_INET, SOCK_DGRAM, 0); 524 ret = socket (AF_INET, SOCK_DGRAM, 0);
519 if (-1 == ret) 525 if (-1 == ret)
520 { 526 {
521 fprintf(stderr, 527 fprintf (stderr,
522 "Error opening UDP socket: %s\n", 528 "Error opening UDP socket: %s\n",
523 strerror(errno)); 529 strerror (errno));
524 return -1; 530 return -1;
525 } 531 }
526 memset(&addr, 0, sizeof(addr)); 532 memset (&addr, 0, sizeof(addr));
527 addr.sin_family = AF_INET; 533 addr.sin_family = AF_INET;
528#if HAVE_SOCKADDR_IN_SIN_LEN 534#if HAVE_SOCKADDR_IN_SIN_LEN
529 addr.sin_len = sizeof(struct sockaddr_in); 535 addr.sin_len = sizeof(struct sockaddr_in);
530#endif 536#endif
531 addr.sin_addr = *my_ip; 537 addr.sin_addr = *my_ip;
532 addr.sin_port = htons(NAT_TRAV_PORT); 538 addr.sin_port = htons (NAT_TRAV_PORT);
533 539
534 if (0 != bind(ret, 540 if (0 != bind (ret,
535 (struct sockaddr *)&addr, 541 (struct sockaddr *) &addr,
536 sizeof(addr))) 542 sizeof(addr)))
537 { 543 {
538 fprintf(stderr, 544 fprintf (stderr,
539 "Error binding UDP socket to port %u: %s\n", 545 "Error binding UDP socket to port %u: %s\n",
540 NAT_TRAV_PORT, 546 NAT_TRAV_PORT,
541 strerror(errno)); 547 strerror (errno));
542 (void)close(ret); 548 (void) close (ret);
543 return -1; 549 return -1;
544 } 550 }
545 return ret; 551 return ret;
546} 552}
547 553
548 554
549int 555int
550main(int argc, 556main (int argc,
551 char *const *argv) 557 char *const *argv)
552{ 558{
553 struct in_addr external; 559 struct in_addr external;
554 fd_set rs; 560 fd_set rs;
@@ -560,147 +566,147 @@ main(int argc,
560 int global_ret; 566 int global_ret;
561 567
562 /* Create an ICMP raw socket for reading (we'll check errors later) */ 568 /* Create an ICMP raw socket for reading (we'll check errors later) */
563 icmpsock = socket(AF_INET, 569 icmpsock = socket (AF_INET,
564 SOCK_RAW, 570 SOCK_RAW,
565 IPPROTO_ICMP); 571 IPPROTO_ICMP);
566 icmp_eno = errno; 572 icmp_eno = errno;
567 573
568 /* Create an (ICMP) raw socket for writing (we'll check errors later) */ 574 /* Create an (ICMP) raw socket for writing (we'll check errors later) */
569 rawsock = socket(AF_INET, 575 rawsock = socket (AF_INET,
570 SOCK_RAW, 576 SOCK_RAW,
571 IPPROTO_RAW); 577 IPPROTO_RAW);
572 raw_eno = errno; 578 raw_eno = errno;
573 udpsock = -1; 579 udpsock = -1;
574 580
575 /* drop root rights */ 581 /* drop root rights */
576 uid = getuid(); 582 uid = getuid ();
577#ifdef HAVE_SETRESUID 583#ifdef HAVE_SETRESUID
578 if (0 != setresuid(uid, uid, uid)) 584 if (0 != setresuid (uid, uid, uid))
579 { 585 {
580 fprintf(stderr, 586 fprintf (stderr,
581 "Failed to setresuid: %s\n", 587 "Failed to setresuid: %s\n",
582 strerror(errno)); 588 strerror (errno));
583 global_ret = 1; 589 global_ret = 1;
584 goto error_exit; 590 goto error_exit;
585 } 591 }
586#else 592#else
587 if (0 != (setuid(uid) | seteuid(uid))) 593 if (0 != (setuid (uid) | seteuid (uid)))
588 { 594 {
589 fprintf(stderr, 595 fprintf (stderr,
590 "Failed to setuid: %s\n", 596 "Failed to setuid: %s\n",
591 strerror(errno)); 597 strerror (errno));
592 global_ret = 2; 598 global_ret = 2;
593 goto error_exit; 599 goto error_exit;
594 } 600 }
595#endif 601#endif
596 602
597 /* Now that we run without root rights, we can do error checking... */ 603 /* Now that we run without root rights, we can do error checking... */
598 if (2 != argc) 604 if (2 != argc)
599 { 605 {
600 fprintf(stderr, 606 fprintf (stderr,
601 "This program must be started with our (internal NAT) IP as the only argument.\n"); 607 "This program must be started with our (internal NAT) IP as the only argument.\n");
602 global_ret = 3; 608 global_ret = 3;
603 goto error_exit; 609 goto error_exit;
604 } 610 }
605 if (1 != inet_pton(AF_INET, argv[1], &external)) 611 if (1 != inet_pton (AF_INET, argv[1], &external))
606 { 612 {
607 fprintf(stderr, 613 fprintf (stderr,
608 "Error parsing IPv4 address: %s\n", 614 "Error parsing IPv4 address: %s\n",
609 strerror(errno)); 615 strerror (errno));
610 global_ret = 4; 616 global_ret = 4;
611 goto error_exit; 617 goto error_exit;
612 } 618 }
613 if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) 619 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
614 { 620 {
615 fprintf(stderr, 621 fprintf (stderr,
616 "Internal error converting dummy IP to binary.\n"); 622 "Internal error converting dummy IP to binary.\n");
617 global_ret = 5; 623 global_ret = 5;
618 goto error_exit; 624 goto error_exit;
619 } 625 }
620 626
621 /* error checking icmpsock */ 627 /* error checking icmpsock */
622 if (-1 == icmpsock) 628 if (-1 == icmpsock)
623 { 629 {
624 fprintf(stderr, 630 fprintf (stderr,
625 "Error opening RAW socket: %s\n", 631 "Error opening RAW socket: %s\n",
626 strerror(icmp_eno)); 632 strerror (icmp_eno));
627 global_ret = 6; 633 global_ret = 6;
628 goto error_exit; 634 goto error_exit;
629 } 635 }
630 if (icmpsock >= FD_SETSIZE) 636 if (icmpsock >= FD_SETSIZE)
631 { 637 {
632 /* this could happen if we were started with a large number of already-open 638 /* this could happen if we were started with a large number of already-open
633 file descriptors... */ 639 file descriptors... */
634 fprintf(stderr, 640 fprintf (stderr,
635 "Socket number too large (%d > %u)\n", 641 "Socket number too large (%d > %u)\n",
636 icmpsock, 642 icmpsock,
637 (unsigned int)FD_SETSIZE); 643 (unsigned int) FD_SETSIZE);
638 global_ret = 7; 644 global_ret = 7;
639 goto error_exit; 645 goto error_exit;
640 } 646 }
641 647
642 /* error checking rawsock */ 648 /* error checking rawsock */
643 if (-1 == rawsock) 649 if (-1 == rawsock)
644 { 650 {
645 fprintf(stderr, 651 fprintf (stderr,
646 "Error opening RAW socket: %s\n", 652 "Error opening RAW socket: %s\n",
647 strerror(raw_eno)); 653 strerror (raw_eno));
648 global_ret = 8; 654 global_ret = 8;
649 goto error_exit; 655 goto error_exit;
650 } 656 }
651 /* no need to check 'rawsock' against FD_SETSIZE as it is never used 657 /* no need to check 'rawsock' against FD_SETSIZE as it is never used
652 with 'select' */ 658 with 'select' */
653 659
654 if (0 != setup_raw_socket()) 660 if (0 != setup_raw_socket ())
655 { 661 {
656 global_ret = 9; 662 global_ret = 9;
657 goto error_exit; 663 goto error_exit;
658 } 664 }
659 665
660 if (-1 == (udpsock = make_udp_socket(&external))) 666 if (-1 == (udpsock = make_udp_socket (&external)))
661 { 667 {
662 global_ret = 10; 668 global_ret = 10;
663 goto error_exit; 669 goto error_exit;
664 } 670 }
665 671
666 alt = 0; 672 alt = 0;
667 while (1) 673 while (1)
674 {
675 FD_ZERO (&rs);
676 FD_SET (icmpsock, &rs);
677 tv.tv_sec = 0;
678 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
679 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
680 {
681 if (errno == EINTR)
682 continue;
683 fprintf (stderr,
684 "select failed: %s\n",
685 strerror (errno));
686 break;
687 }
688 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */
689 break;
690 if (FD_ISSET (icmpsock, &rs))
668 { 691 {
669 FD_ZERO(&rs); 692 process_icmp_response ();
670 FD_SET(icmpsock, &rs); 693 continue;
671 tv.tv_sec = 0;
672 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
673 if (-1 == select(icmpsock + 1, &rs, NULL, NULL, &tv))
674 {
675 if (errno == EINTR)
676 continue;
677 fprintf(stderr,
678 "select failed: %s\n",
679 strerror(errno));
680 break;
681 }
682 if (1 == getppid()) /* Check the parent process id, if 1 the parent has died, so we should die too */
683 break;
684 if (FD_ISSET(icmpsock, &rs))
685 {
686 process_icmp_response();
687 continue;
688 }
689 if (0 == (++alt % 2))
690 send_icmp_echo(&external);
691 else
692 send_udp();
693 } 694 }
695 if (0 == (++alt % 2))
696 send_icmp_echo (&external);
697 else
698 send_udp ();
699 }
694 700
695 /* select failed (internal error or OS out of resources) */ 701 /* select failed (internal error or OS out of resources) */
696 global_ret = 11; 702 global_ret = 11;
697error_exit: 703error_exit:
698 if (-1 != icmpsock) 704 if (-1 != icmpsock)
699 (void)close(icmpsock); 705 (void) close (icmpsock);
700 if (-1 != rawsock) 706 if (-1 != rawsock)
701 (void)close(rawsock); 707 (void) close (rawsock);
702 if (-1 != udpsock) 708 if (-1 != udpsock)
703 (void)close(udpsock); 709 (void) close (udpsock);
704 return global_ret; 710 return global_ret;
705} 711}
706 712
diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c
index d82898f56..857ce5093 100644
--- a/src/nat/gnunet-nat.c
+++ b/src/nat/gnunet-nat.c
@@ -95,13 +95,13 @@ static struct GNUNET_SCHEDULER_Task *rtask;
95 * terminate. 95 * terminate.
96 */ 96 */
97static void 97static void
98test_finished() 98test_finished ()
99{ 99{
100 if (NULL != nh) 100 if (NULL != nh)
101 return; 101 return;
102 if (NULL != rtask) 102 if (NULL != rtask)
103 return; 103 return;
104 GNUNET_SCHEDULER_shutdown(); 104 GNUNET_SCHEDULER_shutdown ();
105} 105}
106 106
107 107
@@ -119,21 +119,21 @@ test_finished()
119 * @param addrlen actual length of the @a addr 119 * @param addrlen actual length of the @a addr
120 */ 120 */
121static void 121static void
122address_cb(void *cls, 122address_cb (void *cls,
123 void **app_ctx, 123 void **app_ctx,
124 int add_remove, 124 int add_remove,
125 enum GNUNET_NAT_AddressClass ac, 125 enum GNUNET_NAT_AddressClass ac,
126 const struct sockaddr *addr, 126 const struct sockaddr *addr,
127 socklen_t addrlen) 127 socklen_t addrlen)
128{ 128{
129 (void)cls; 129 (void) cls;
130 (void)app_ctx; 130 (void) app_ctx;
131 131
132 fprintf(stdout, 132 fprintf (stdout,
133 "%s %s (%d)\n", 133 "%s %s (%d)\n",
134 add_remove ? "+" : "-", 134 add_remove ? "+" : "-",
135 GNUNET_a2s(addr, addrlen), 135 GNUNET_a2s (addr, addrlen),
136 (int)ac); 136 (int) ac);
137} 137}
138 138
139 139
@@ -147,13 +147,13 @@ address_cb(void *cls,
147 * @param remote_addrlen actual length of the @a remote_addr 147 * @param remote_addrlen actual length of the @a remote_addr
148 */ 148 */
149static void 149static void
150reversal_cb(void *cls, 150reversal_cb (void *cls,
151 const struct sockaddr *remote_addr, 151 const struct sockaddr *remote_addr,
152 socklen_t remote_addrlen) 152 socklen_t remote_addrlen)
153{ 153{
154 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 154 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
155 "Connection reversal requested by %s\n", 155 "Connection reversal requested by %s\n",
156 GNUNET_a2s(remote_addr, remote_addrlen)); 156 GNUNET_a2s (remote_addr, remote_addrlen));
157} 157}
158 158
159 159
@@ -163,23 +163,23 @@ reversal_cb(void *cls,
163 * @param cls NULL 163 * @param cls NULL
164 */ 164 */
165static void 165static void
166do_shutdown(void *cls) 166do_shutdown (void *cls)
167{ 167{
168 if (NULL != nh) 168 if (NULL != nh)
169 { 169 {
170 GNUNET_NAT_unregister(nh); 170 GNUNET_NAT_unregister (nh);
171 nh = NULL; 171 nh = NULL;
172 } 172 }
173 if (NULL != ls) 173 if (NULL != ls)
174 { 174 {
175 GNUNET_NETWORK_socket_close(ls); 175 GNUNET_NETWORK_socket_close (ls);
176 ls = NULL; 176 ls = NULL;
177 } 177 }
178 if (NULL != rtask) 178 if (NULL != rtask)
179 { 179 {
180 GNUNET_SCHEDULER_cancel(rtask); 180 GNUNET_SCHEDULER_cancel (rtask);
181 rtask = NULL; 181 rtask = NULL;
182 } 182 }
183} 183}
184 184
185 185
@@ -187,45 +187,45 @@ do_shutdown(void *cls)
187 * Task to receive incoming packets for STUN processing. 187 * Task to receive incoming packets for STUN processing.
188 */ 188 */
189static void 189static void
190stun_read_task(void *cls) 190stun_read_task (void *cls)
191{ 191{
192 ssize_t size; 192 ssize_t size;
193 193
194 rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 194 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
195 ls, 195 ls,
196 &stun_read_task, 196 &stun_read_task,
197 NULL); 197 NULL);
198 size = GNUNET_NETWORK_socket_recvfrom_amount(ls); 198 size = GNUNET_NETWORK_socket_recvfrom_amount (ls);
199 if (size > 0) 199 if (size > 0)
200 { 200 {
201 GNUNET_break(0); 201 GNUNET_break (0);
202 GNUNET_SCHEDULER_shutdown(); 202 GNUNET_SCHEDULER_shutdown ();
203 global_ret = 1; 203 global_ret = 1;
204 return; 204 return;
205 } 205 }
206 { 206 {
207 char buf[size + 1]; 207 char buf[size + 1];
208 struct sockaddr_storage sa; 208 struct sockaddr_storage sa;
209 socklen_t salen = sizeof(sa); 209 socklen_t salen = sizeof(sa);
210 ssize_t ret; 210 ssize_t ret;
211 211
212 ret = GNUNET_NETWORK_socket_recvfrom(ls, 212 ret = GNUNET_NETWORK_socket_recvfrom (ls,
213 buf, 213 buf,
214 size + 1, 214 size + 1,
215 (struct sockaddr *)&sa, 215 (struct sockaddr *) &sa,
216 &salen); 216 &salen);
217 if (ret != size) 217 if (ret != size)
218 { 218 {
219 GNUNET_break(0); 219 GNUNET_break (0);
220 GNUNET_SCHEDULER_shutdown(); 220 GNUNET_SCHEDULER_shutdown ();
221 global_ret = 1; 221 global_ret = 1;
222 return; 222 return;
223 } 223 }
224 (void)GNUNET_NAT_stun_handle_packet(nh, 224 (void) GNUNET_NAT_stun_handle_packet (nh,
225 (const struct sockaddr *)&sa, 225 (const struct sockaddr *) &sa,
226 salen, 226 salen,
227 buf, 227 buf,
228 ret); 228 ret);
229 } 229 }
230} 230}
231 231
@@ -239,10 +239,10 @@ stun_read_task(void *cls)
239 * @param c configuration 239 * @param c configuration
240 */ 240 */
241static void 241static void
242run(void *cls, 242run (void *cls,
243 char *const *args, 243 char *const *args,
244 const char *cfgfile, 244 const char *cfgfile,
245 const struct GNUNET_CONFIGURATION_Handle *c) 245 const struct GNUNET_CONFIGURATION_Handle *c)
246{ 246{
247 uint8_t af; 247 uint8_t af;
248 struct sockaddr *local_sa; 248 struct sockaddr *local_sa;
@@ -251,156 +251,156 @@ run(void *cls,
251 size_t remote_len; 251 size_t remote_len;
252 252
253 if (use_tcp && use_udp) 253 if (use_tcp && use_udp)
254 { 254 {
255 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n"); 255 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n");
256 global_ret = 1; 256 global_ret = 1;
257 return; 257 return;
258 } 258 }
259 proto = 0; 259 proto = 0;
260 if (use_tcp) 260 if (use_tcp)
261 proto = IPPROTO_TCP; 261 proto = IPPROTO_TCP;
262 if (use_udp) 262 if (use_udp)
263 proto = IPPROTO_UDP; 263 proto = IPPROTO_UDP;
264 264
265 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); 265 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
266 266
267 if (0 == proto) 267 if (0 == proto)
268 { 268 {
269 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Must specify either TCP or UDP\n"); 269 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Must specify either TCP or UDP\n");
270 global_ret = 1; 270 global_ret = 1;
271 return; 271 return;
272 } 272 }
273 local_len = 0; 273 local_len = 0;
274 local_sa = NULL; 274 local_sa = NULL;
275 remote_len = 0; 275 remote_len = 0;
276 remote_sa = NULL; 276 remote_sa = NULL;
277 if (NULL != local_addr) 277 if (NULL != local_addr)
278 {
279 local_len =
280 (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, &af, &local_sa);
281 if (0 == local_len)
278 { 282 {
279 local_len = 283 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
280 (socklen_t)GNUNET_STRINGS_parse_socket_addr(local_addr, &af, &local_sa); 284 "Invalid socket address `%s'\n",
281 if (0 == local_len) 285 local_addr);
282 { 286 goto fail_and_shutdown;
283 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
284 "Invalid socket address `%s'\n",
285 local_addr);
286 goto fail_and_shutdown;
287 }
288 } 287 }
288 }
289 289
290 if (NULL != remote_addr) 290 if (NULL != remote_addr)
291 {
292 remote_len =
293 GNUNET_STRINGS_parse_socket_addr (remote_addr, &af, &remote_sa);
294 if (0 == remote_len)
291 { 295 {
292 remote_len = 296 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
293 GNUNET_STRINGS_parse_socket_addr(remote_addr, &af, &remote_sa); 297 "Invalid socket address `%s'\n",
294 if (0 == remote_len) 298 remote_addr);
295 { 299 goto fail_and_shutdown;
296 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
297 "Invalid socket address `%s'\n",
298 remote_addr);
299 goto fail_and_shutdown;
300 }
301 } 300 }
301 }
302 302
303 if (NULL != local_addr) 303 if (NULL != local_addr)
304 {
305 if (NULL == section_name)
306 section_name = GNUNET_strdup ("undefined");
307 nh = GNUNET_NAT_register (c,
308 section_name,
309 proto,
310 1,
311 (const struct sockaddr **) &local_sa,
312 &local_len,
313 &address_cb,
314 (listen_reversal) ? &reversal_cb : NULL,
315 NULL);
316 }
317 else if (listen_reversal)
318 {
319 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
320 "Use of `-W` only effective in combination with `-i`\n");
321 goto fail_and_shutdown;
322 }
323
324 if (NULL != remote_addr)
325 {
326 int ret;
327
328 if ((NULL == nh) || (sizeof(struct sockaddr_in) != local_len))
304 { 329 {
305 if (NULL == section_name) 330 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
306 section_name = GNUNET_strdup("undefined"); 331 "Require IPv4 local address to initiate connection reversal\n");
307 nh = GNUNET_NAT_register(c, 332 goto fail_and_shutdown;
308 section_name,
309 proto,
310 1,
311 (const struct sockaddr **)&local_sa,
312 &local_len,
313 &address_cb,
314 (listen_reversal) ? &reversal_cb : NULL,
315 NULL);
316 } 333 }
317 else if (listen_reversal) 334 if (sizeof(struct sockaddr_in) != remote_len)
318 { 335 {
319 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 336 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
320 "Use of `-W` only effective in combination with `-i`\n"); 337 "Require IPv4 reversal target address\n");
321 goto fail_and_shutdown; 338 goto fail_and_shutdown;
322 } 339 }
323 340 GNUNET_assert (AF_INET == local_sa->sa_family);
324 if (NULL != remote_addr) 341 GNUNET_assert (AF_INET == remote_sa->sa_family);
342 ret = GNUNET_NAT_request_reversal (nh,
343 (const struct sockaddr_in *) local_sa,
344 (const struct sockaddr_in *) remote_sa);
345 switch (ret)
325 { 346 {
326 int ret; 347 case GNUNET_SYSERR:
327 348 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
328 if ((NULL == nh) || (sizeof(struct sockaddr_in) != local_len)) 349 "Connection reversal internal error\n");
329 { 350 break;
330 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 351
331 "Require IPv4 local address to initiate connection reversal\n"); 352 case GNUNET_NO:
332 goto fail_and_shutdown; 353 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
333 } 354 "Connection reversal unavailable\n");
334 if (sizeof(struct sockaddr_in) != remote_len) 355 break;
335 { 356
336 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 357 case GNUNET_OK:
337 "Require IPv4 reversal target address\n"); 358 /* operation in progress */
338 goto fail_and_shutdown; 359 break;
339 }
340 GNUNET_assert(AF_INET == local_sa->sa_family);
341 GNUNET_assert(AF_INET == remote_sa->sa_family);
342 ret = GNUNET_NAT_request_reversal(nh,
343 (const struct sockaddr_in *)local_sa,
344 (const struct sockaddr_in *)remote_sa);
345 switch (ret)
346 {
347 case GNUNET_SYSERR:
348 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
349 "Connection reversal internal error\n");
350 break;
351
352 case GNUNET_NO:
353 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
354 "Connection reversal unavailable\n");
355 break;
356
357 case GNUNET_OK:
358 /* operation in progress */
359 break;
360 }
361 } 360 }
361 }
362 362
363 if (do_stun) 363 if (do_stun)
364 {
365 if (NULL == local_addr)
366 {
367 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
368 "Require local address to support STUN requests\n");
369 goto fail_and_shutdown;
370 }
371 if (IPPROTO_UDP != proto)
372 {
373 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "STUN only supported over UDP\n");
374 goto fail_and_shutdown;
375 }
376 ls = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, IPPROTO_UDP);
377 if (NULL == ls)
378 {
379 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Failed to create socket\n");
380 goto fail_and_shutdown;
381 }
382 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls, local_sa, local_len))
364 { 383 {
365 if (NULL == local_addr) 384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
366 { 385 "Failed to bind to %s: %s\n",
367 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 386 GNUNET_a2s (local_sa, local_len),
368 "Require local address to support STUN requests\n"); 387 strerror (errno));
369 goto fail_and_shutdown; 388 goto fail_and_shutdown;
370 }
371 if (IPPROTO_UDP != proto)
372 {
373 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "STUN only supported over UDP\n");
374 goto fail_and_shutdown;
375 }
376 ls = GNUNET_NETWORK_socket_create(af, SOCK_DGRAM, IPPROTO_UDP);
377 if (NULL == ls)
378 {
379 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Failed to create socket\n");
380 goto fail_and_shutdown;
381 }
382 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(ls, local_sa, local_len))
383 {
384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
385 "Failed to bind to %s: %s\n",
386 GNUNET_a2s(local_sa, local_len),
387 strerror(errno));
388 goto fail_and_shutdown;
389 }
390 rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
391 ls,
392 &stun_read_task,
393 NULL);
394 } 389 }
395 GNUNET_free_non_null(remote_sa); 390 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
396 GNUNET_free_non_null(local_sa); 391 ls,
397 test_finished(); 392 &stun_read_task,
393 NULL);
394 }
395 GNUNET_free_non_null (remote_sa);
396 GNUNET_free_non_null (local_sa);
397 test_finished ();
398 return; 398 return;
399fail_and_shutdown: 399fail_and_shutdown:
400 global_ret = 1; 400 global_ret = 1;
401 GNUNET_SCHEDULER_shutdown(); 401 GNUNET_SCHEDULER_shutdown ();
402 GNUNET_free_non_null(remote_sa); 402 GNUNET_free_non_null (remote_sa);
403 GNUNET_free_non_null(local_sa); 403 GNUNET_free_non_null (local_sa);
404} 404}
405 405
406 406
@@ -412,63 +412,63 @@ fail_and_shutdown:
412 * @return 0 on success, -1 on error 412 * @return 0 on success, -1 on error
413 */ 413 */
414int 414int
415main(int argc, char *const argv[]) 415main (int argc, char *const argv[])
416{ 416{
417 struct GNUNET_GETOPT_CommandLineOption options[] = { 417 struct GNUNET_GETOPT_CommandLineOption options[] = {
418 GNUNET_GETOPT_option_string( 418 GNUNET_GETOPT_option_string (
419 'i', 419 'i',
420 "in", 420 "in",
421 "ADDRESS", 421 "ADDRESS",
422 gettext_noop("which IP and port are we locally using to bind/listen to"), 422 gettext_noop ("which IP and port are we locally using to bind/listen to"),
423 &local_addr), 423 &local_addr),
424 424
425 GNUNET_GETOPT_option_string( 425 GNUNET_GETOPT_option_string (
426 'r', 426 'r',
427 "remote", 427 "remote",
428 "ADDRESS", 428 "ADDRESS",
429 gettext_noop( 429 gettext_noop (
430 "which remote IP and port should be asked for connection reversal"), 430 "which remote IP and port should be asked for connection reversal"),
431 &remote_addr), 431 &remote_addr),
432 432
433 GNUNET_GETOPT_option_string( 433 GNUNET_GETOPT_option_string (
434 'S', 434 'S',
435 "section", 435 "section",
436 NULL, 436 NULL,
437 gettext_noop( 437 gettext_noop (
438 "name of configuration section to find additional options, such as manual host punching data"), 438 "name of configuration section to find additional options, such as manual host punching data"),
439 &section_name), 439 &section_name),
440 440
441 GNUNET_GETOPT_option_flag('s', 441 GNUNET_GETOPT_option_flag ('s',
442 "stun", 442 "stun",
443 gettext_noop("enable STUN processing"), 443 gettext_noop ("enable STUN processing"),
444 &do_stun), 444 &do_stun),
445 445
446 GNUNET_GETOPT_option_flag('t', "tcp", gettext_noop("use TCP"), &use_tcp), 446 GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp),
447 447
448 GNUNET_GETOPT_option_flag('u', "udp", gettext_noop("use UDP"), &use_udp), 448 GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp),
449 449
450 GNUNET_GETOPT_option_flag('W', 450 GNUNET_GETOPT_option_flag ('W',
451 "watch", 451 "watch",
452 gettext_noop( 452 gettext_noop (
453 "watch for connection reversal requests"), 453 "watch for connection reversal requests"),
454 &listen_reversal), 454 &listen_reversal),
455 GNUNET_GETOPT_OPTION_END 455 GNUNET_GETOPT_OPTION_END
456 }; 456 };
457 457
458 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 458 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
459 return 2; 459 return 2;
460 if (GNUNET_OK != 460 if (GNUNET_OK !=
461 GNUNET_PROGRAM_run(argc, 461 GNUNET_PROGRAM_run (argc,
462 argv, 462 argv,
463 "gnunet-nat [options]", 463 "gnunet-nat [options]",
464 _("GNUnet NAT traversal autoconfigure daemon"), 464 _ ("GNUnet NAT traversal autoconfigure daemon"),
465 options, 465 options,
466 &run, 466 &run,
467 NULL)) 467 NULL))
468 { 468 {
469 global_ret = 1; 469 global_ret = 1;
470 } 470 }
471 GNUNET_free((void *)argv); 471 GNUNET_free ((void *) argv);
472 return global_ret; 472 return global_ret;
473} 473}
474 474
diff --git a/src/nat/gnunet-service-nat.c b/src/nat/gnunet-service-nat.c
index 6fd09a7d9..694949dde 100644
--- a/src/nat/gnunet-service-nat.c
+++ b/src/nat/gnunet-service-nat.c
@@ -55,23 +55,26 @@
55 * How often should we ask the OS about a list of active 55 * How often should we ask the OS about a list of active
56 * network interfaces? 56 * network interfaces?
57 */ 57 */
58#define SCAN_FREQ GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) 58#define SCAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
59 59
60/** 60/**
61 * How long do we wait until we forcefully terminate autoconfiguration? 61 * How long do we wait until we forcefully terminate autoconfiguration?
62 */ 62 */
63#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) 63#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply ( \
64 GNUNET_TIME_UNIT_SECONDS, 5)
64 65
65/** 66/**
66 * How often do we scan for changes in how our external (dyndns) hostname resolves? 67 * How often do we scan for changes in how our external (dyndns) hostname resolves?
67 */ 68 */
68#define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 7) 69#define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply ( \
70 GNUNET_TIME_UNIT_MINUTES, 7)
69 71
70 72
71/** 73/**
72 * Information we track per client address. 74 * Information we track per client address.
73 */ 75 */
74struct ClientAddress { 76struct ClientAddress
77{
75 /** 78 /**
76 * Network address used by the client. 79 * Network address used by the client.
77 */ 80 */
@@ -89,7 +92,8 @@ struct ClientAddress {
89/** 92/**
90 * List of local addresses this system has. 93 * List of local addresses this system has.
91 */ 94 */
92struct LocalAddressList { 95struct LocalAddressList
96{
93 /** 97 /**
94 * This is a linked list. 98 * This is a linked list.
95 */ 99 */
@@ -134,7 +138,8 @@ struct LocalAddressList {
134/** 138/**
135 * Internal data structure we track for each of our clients. 139 * Internal data structure we track for each of our clients.
136 */ 140 */
137struct ClientHandle { 141struct ClientHandle
142{
138 /** 143 /**
139 * Kept in a DLL. 144 * Kept in a DLL.
140 */ 145 */
@@ -232,7 +237,8 @@ struct ClientHandle {
232/** 237/**
233 * External IP address as given to us via some STUN server. 238 * External IP address as given to us via some STUN server.
234 */ 239 */
235struct StunExternalIP { 240struct StunExternalIP
241{
236 /** 242 /**
237 * Kept in a DLL. 243 * Kept in a DLL.
238 */ 244 */
@@ -336,22 +342,22 @@ int enable_upnp;
336 * @param lal entry to free 342 * @param lal entry to free
337 */ 343 */
338static void 344static void
339free_lal(struct LocalAddressList *lal) 345free_lal (struct LocalAddressList *lal)
340{ 346{
341 GNUNET_CONTAINER_DLL_remove(lal_head, 347 GNUNET_CONTAINER_DLL_remove (lal_head,
342 lal_tail, 348 lal_tail,
343 lal); 349 lal);
344 if (NULL != lal->hc) 350 if (NULL != lal->hc)
345 { 351 {
346 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 352 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
347 "Lost NATed local address %s, stopping NAT server\n", 353 "Lost NATed local address %s, stopping NAT server\n",
348 GNUNET_a2s((const struct sockaddr *)&lal->addr, 354 GNUNET_a2s ((const struct sockaddr *) &lal->addr,
349 sizeof(struct sockaddr_in))); 355 sizeof(struct sockaddr_in)));
350 356
351 GN_stop_gnunet_nat_server_(lal->hc); 357 GN_stop_gnunet_nat_server_ (lal->hc);
352 lal->hc = NULL; 358 lal->hc = NULL;
353 } 359 }
354 GNUNET_free(lal); 360 GNUNET_free (lal);
355} 361}
356 362
357 363
@@ -359,12 +365,12 @@ free_lal(struct LocalAddressList *lal)
359 * Free the DLL starting at #lal_head. 365 * Free the DLL starting at #lal_head.
360 */ 366 */
361static void 367static void
362destroy_lal() 368destroy_lal ()
363{ 369{
364 struct LocalAddressList *lal; 370 struct LocalAddressList *lal;
365 371
366 while (NULL != (lal = lal_head)) 372 while (NULL != (lal = lal_head))
367 free_lal(lal); 373 free_lal (lal);
368} 374}
369 375
370 376
@@ -377,55 +383,55 @@ destroy_lal()
377 * @return #GNUNET_OK if message is well-formed 383 * @return #GNUNET_OK if message is well-formed
378 */ 384 */
379static int 385static int
380check_register(void *cls, 386check_register (void *cls,
381 const struct GNUNET_NAT_RegisterMessage *message) 387 const struct GNUNET_NAT_RegisterMessage *message)
382{ 388{
383 uint16_t num_addrs = ntohs(message->num_addrs); 389 uint16_t num_addrs = ntohs (message->num_addrs);
384 const char *off = (const char *)&message[1]; 390 const char *off = (const char *) &message[1];
385 size_t left = ntohs(message->header.size) - sizeof(*message); 391 size_t left = ntohs (message->header.size) - sizeof(*message);
386 392
387 for (unsigned int i = 0; i < num_addrs; i++) 393 for (unsigned int i = 0; i < num_addrs; i++)
394 {
395 size_t alen;
396 const struct sockaddr *sa = (const struct sockaddr *) off;
397
398 if (sizeof(sa_family_t) > left)
399 {
400 GNUNET_break (0);
401 return GNUNET_SYSERR;
402 }
403 switch (sa->sa_family)
388 { 404 {
389 size_t alen; 405 case AF_INET:
390 const struct sockaddr *sa = (const struct sockaddr *)off; 406 alen = sizeof(struct sockaddr_in);
391 407 break;
392 if (sizeof(sa_family_t) > left) 408
393 { 409 case AF_INET6:
394 GNUNET_break(0); 410 alen = sizeof(struct sockaddr_in6);
395 return GNUNET_SYSERR; 411 break;
396 }
397 switch (sa->sa_family)
398 {
399 case AF_INET:
400 alen = sizeof(struct sockaddr_in);
401 break;
402
403 case AF_INET6:
404 alen = sizeof(struct sockaddr_in6);
405 break;
406 412
407#if AF_UNIX 413#if AF_UNIX
408 case AF_UNIX: 414 case AF_UNIX:
409 alen = sizeof(struct sockaddr_un); 415 alen = sizeof(struct sockaddr_un);
410 break; 416 break;
411#endif 417#endif
412 default: 418 default:
413 GNUNET_break(0); 419 GNUNET_break (0);
414 return GNUNET_SYSERR; 420 return GNUNET_SYSERR;
415 }
416 if (alen > left)
417 {
418 GNUNET_break(0);
419 return GNUNET_SYSERR;
420 }
421 off += alen;
422 left -= alen;
423 } 421 }
424 if (left != ntohs(message->str_len)) 422 if (alen > left)
425 { 423 {
426 GNUNET_break(0); 424 GNUNET_break (0);
427 return GNUNET_SYSERR; 425 return GNUNET_SYSERR;
428 } 426 }
427 off += alen;
428 left -= alen;
429 }
430 if (left != ntohs (message->str_len))
431 {
432 GNUNET_break (0);
433 return GNUNET_SYSERR;
434 }
429 return GNUNET_OK; 435 return GNUNET_OK;
430} 436}
431 437
@@ -439,9 +445,9 @@ check_register(void *cls,
439 * @return #GNUNET_YES if @a ip is in @a network 445 * @return #GNUNET_YES if @a ip is in @a network
440 */ 446 */
441static int 447static int
442match_ipv4(const char *network, 448match_ipv4 (const char *network,
443 const struct in_addr *ip, 449 const struct in_addr *ip,
444 uint8_t bits) 450 uint8_t bits)
445{ 451{
446 struct in_addr net; 452 struct in_addr net;
447 453
@@ -449,10 +455,10 @@ match_ipv4(const char *network,
449 return GNUNET_YES; 455 return GNUNET_YES;
450 if (0 == bits) 456 if (0 == bits)
451 return GNUNET_YES; 457 return GNUNET_YES;
452 GNUNET_assert(1 == inet_pton(AF_INET, 458 GNUNET_assert (1 == inet_pton (AF_INET,
453 network, 459 network,
454 &net)); 460 &net));
455 return !((ip->s_addr ^ net.s_addr) & htonl(0xFFFFFFFFu << (32 - bits))); 461 return ! ((ip->s_addr ^ net.s_addr) & htonl (0xFFFFFFFFu << (32 - bits)));
456} 462}
457 463
458 464
@@ -465,9 +471,9 @@ match_ipv4(const char *network,
465 * @return #GNUNET_YES if @a ip is in @a network 471 * @return #GNUNET_YES if @a ip is in @a network
466 */ 472 */
467static int 473static int
468match_ipv6(const char *network, 474match_ipv6 (const char *network,
469 const struct in6_addr *ip, 475 const struct in6_addr *ip,
470 uint8_t bits) 476 uint8_t bits)
471{ 477{
472 struct in6_addr net; 478 struct in6_addr net;
473 struct in6_addr mask; 479 struct in6_addr mask;
@@ -475,27 +481,27 @@ match_ipv6(const char *network,
475 481
476 if (0 == bits) 482 if (0 == bits)
477 return GNUNET_YES; 483 return GNUNET_YES;
478 GNUNET_assert(1 == inet_pton(AF_INET6, 484 GNUNET_assert (1 == inet_pton (AF_INET6,
479 network, 485 network,
480 &net)); 486 &net));
481 memset(&mask, 0, sizeof(mask)); 487 memset (&mask, 0, sizeof(mask));
482 if (0 == GNUNET_memcmp(&mask, 488 if (0 == GNUNET_memcmp (&mask,
483 ip)) 489 ip))
484 return GNUNET_YES; 490 return GNUNET_YES;
485 off = 0; 491 off = 0;
486 while (bits > 8) 492 while (bits > 8)
487 { 493 {
488 mask.s6_addr[off++] = 0xFF; 494 mask.s6_addr[off++] = 0xFF;
489 bits -= 8; 495 bits -= 8;
490 } 496 }
491 while (bits > 0) 497 while (bits > 0)
492 { 498 {
493 mask.s6_addr[off] = (mask.s6_addr[off] >> 1) + 0x80; 499 mask.s6_addr[off] = (mask.s6_addr[off] >> 1) + 0x80;
494 bits--; 500 bits--;
495 } 501 }
496 for (unsigned j = 0; j < sizeof(struct in6_addr) / sizeof(uint32_t); j++) 502 for (unsigned j = 0; j < sizeof(struct in6_addr) / sizeof(uint32_t); j++)
497 if (((((uint32_t *)ip)[j] & ((uint32_t *)&mask)[j])) != 503 if (((((uint32_t *) ip)[j] & ((uint32_t *) &mask)[j])) !=
498 (((uint32_t *)&net)[j] & ((int *)&mask)[j])) 504 (((uint32_t *) &net)[j] & ((int *) &mask)[j]))
499 return GNUNET_NO; 505 return GNUNET_NO;
500 return GNUNET_YES; 506 return GNUNET_YES;
501} 507}
@@ -509,14 +515,14 @@ match_ipv6(const char *network,
509 * @return #GNUNET_YES if @a ip is in a NAT range 515 * @return #GNUNET_YES if @a ip is in a NAT range
510 */ 516 */
511static int 517static int
512is_nat_v4(const struct in_addr *ip) 518is_nat_v4 (const struct in_addr *ip)
513{ 519{
514 return 520 return
515 match_ipv4("10.0.0.0", ip, 8) || /* RFC 1918 */ 521 match_ipv4 ("10.0.0.0", ip, 8) || /* RFC 1918 */
516 match_ipv4("100.64.0.0", ip, 10) || /* CG-NAT, RFC 6598 */ 522 match_ipv4 ("100.64.0.0", ip, 10) || /* CG-NAT, RFC 6598 */
517 match_ipv4("192.168.0.0", ip, 12) || /* RFC 1918 */ 523 match_ipv4 ("192.168.0.0", ip, 12) || /* RFC 1918 */
518 match_ipv4("169.254.0.0", ip, 16) || /* AUTO, RFC 3927 */ 524 match_ipv4 ("169.254.0.0", ip, 16) || /* AUTO, RFC 3927 */
519 match_ipv4("172.16.0.0", ip, 16); /* RFC 1918 */ 525 match_ipv4 ("172.16.0.0", ip, 16); /* RFC 1918 */
520} 526}
521 527
522 528
@@ -528,19 +534,20 @@ is_nat_v4(const struct in_addr *ip)
528 * @return #GNUNET_YES if @a ip is in a NAT range 534 * @return #GNUNET_YES if @a ip is in a NAT range
529 */ 535 */
530static int 536static int
531is_nat_v6(const struct in6_addr *ip) 537is_nat_v6 (const struct in6_addr *ip)
532{ 538{
533 return 539 return
534 match_ipv6("fc00::", ip, 7) || /* RFC 4193 */ 540 match_ipv6 ("fc00::", ip, 7) || /* RFC 4193 */
535 match_ipv6("fec0::", ip, 10) || /* RFC 3879 */ 541 match_ipv6 ("fec0::", ip, 10) || /* RFC 3879 */
536 match_ipv6("fe80::", ip, 10); /* RFC 4291, link-local */ 542 match_ipv6 ("fe80::", ip, 10); /* RFC 4291, link-local */
537} 543}
538 544
539 545
540/** 546/**
541 * Closure for #ifc_proc. 547 * Closure for #ifc_proc.
542 */ 548 */
543struct IfcProcContext { 549struct IfcProcContext
550{
544 /** 551 /**
545 * Head of DLL of local addresses. 552 * Head of DLL of local addresses.
546 */ 553 */
@@ -567,13 +574,13 @@ struct IfcProcContext {
567 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort 574 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
568 */ 575 */
569static int 576static int
570ifc_proc(void *cls, 577ifc_proc (void *cls,
571 const char *name, 578 const char *name,
572 int isDefault, 579 int isDefault,
573 const struct sockaddr *addr, 580 const struct sockaddr *addr,
574 const struct sockaddr *broadcast_addr, 581 const struct sockaddr *broadcast_addr,
575 const struct sockaddr *netmask, 582 const struct sockaddr *netmask,
576 socklen_t addrlen) 583 socklen_t addrlen)
577{ 584{
578 struct IfcProcContext *ifc_ctx = cls; 585 struct IfcProcContext *ifc_ctx = cls;
579 struct LocalAddressList *lal; 586 struct LocalAddressList *lal;
@@ -583,53 +590,53 @@ ifc_proc(void *cls,
583 enum GNUNET_NAT_AddressClass ac; 590 enum GNUNET_NAT_AddressClass ac;
584 591
585 switch (addr->sa_family) 592 switch (addr->sa_family)
593 {
594 case AF_INET:
595 alen = sizeof(struct sockaddr_in);
596 ip4 = &((const struct sockaddr_in *) addr)->sin_addr;
597 if (match_ipv4 ("127.0.0.0", ip4, 8))
598 ac = GNUNET_NAT_AC_LOOPBACK;
599 else if (is_nat_v4 (ip4))
600 ac = GNUNET_NAT_AC_LAN;
601 else
602 ac = GNUNET_NAT_AC_GLOBAL;
603 break;
604
605 case AF_INET6:
606 alen = sizeof(struct sockaddr_in6);
607 ip6 = &((const struct sockaddr_in6 *) addr)->sin6_addr;
608 if (match_ipv6 ("::1", ip6, 128))
609 ac = GNUNET_NAT_AC_LOOPBACK;
610 else if (is_nat_v6 (ip6))
611 ac = GNUNET_NAT_AC_LAN;
612 else
613 ac = GNUNET_NAT_AC_GLOBAL;
614 if ((ip6->s6_addr[11] == 0xFF) &&
615 (ip6->s6_addr[12] == 0xFE))
586 { 616 {
587 case AF_INET: 617 /* contains a MAC, be extra careful! */
588 alen = sizeof(struct sockaddr_in); 618 ac |= GNUNET_NAT_AC_PRIVATE;
589 ip4 = &((const struct sockaddr_in *)addr)->sin_addr; 619 }
590 if (match_ipv4("127.0.0.0", ip4, 8)) 620 break;
591 ac = GNUNET_NAT_AC_LOOPBACK;
592 else if (is_nat_v4(ip4))
593 ac = GNUNET_NAT_AC_LAN;
594 else
595 ac = GNUNET_NAT_AC_GLOBAL;
596 break;
597
598 case AF_INET6:
599 alen = sizeof(struct sockaddr_in6);
600 ip6 = &((const struct sockaddr_in6 *)addr)->sin6_addr;
601 if (match_ipv6("::1", ip6, 128))
602 ac = GNUNET_NAT_AC_LOOPBACK;
603 else if (is_nat_v6(ip6))
604 ac = GNUNET_NAT_AC_LAN;
605 else
606 ac = GNUNET_NAT_AC_GLOBAL;
607 if ((ip6->s6_addr[11] == 0xFF) &&
608 (ip6->s6_addr[12] == 0xFE))
609 {
610 /* contains a MAC, be extra careful! */
611 ac |= GNUNET_NAT_AC_PRIVATE;
612 }
613 break;
614 621
615#if AF_UNIX 622#if AF_UNIX
616 case AF_UNIX: 623 case AF_UNIX:
617 GNUNET_break(0); 624 GNUNET_break (0);
618 return GNUNET_OK; 625 return GNUNET_OK;
619#endif 626#endif
620 default: 627 default:
621 GNUNET_break(0); 628 GNUNET_break (0);
622 return GNUNET_OK; 629 return GNUNET_OK;
623 } 630 }
624 lal = GNUNET_malloc(sizeof(*lal)); 631 lal = GNUNET_malloc (sizeof(*lal));
625 lal->af = addr->sa_family; 632 lal->af = addr->sa_family;
626 lal->ac = ac; 633 lal->ac = ac;
627 GNUNET_memcpy(&lal->addr, 634 GNUNET_memcpy (&lal->addr,
628 addr, 635 addr,
629 alen); 636 alen);
630 GNUNET_CONTAINER_DLL_insert(ifc_ctx->lal_head, 637 GNUNET_CONTAINER_DLL_insert (ifc_ctx->lal_head,
631 ifc_ctx->lal_tail, 638 ifc_ctx->lal_tail,
632 lal); 639 lal);
633 return GNUNET_OK; 640 return GNUNET_OK;
634} 641}
635 642
@@ -645,30 +652,30 @@ ifc_proc(void *cls,
645 * @param addr_len number of bytes in @a addr 652 * @param addr_len number of bytes in @a addr
646 */ 653 */
647static void 654static void
648notify_client(enum GNUNET_NAT_AddressClass ac, 655notify_client (enum GNUNET_NAT_AddressClass ac,
649 struct ClientHandle *ch, 656 struct ClientHandle *ch,
650 int add, 657 int add,
651 const void *addr, 658 const void *addr,
652 size_t addr_len) 659 size_t addr_len)
653{ 660{
654 struct GNUNET_MQ_Envelope *env; 661 struct GNUNET_MQ_Envelope *env;
655 struct GNUNET_NAT_AddressChangeNotificationMessage *msg; 662 struct GNUNET_NAT_AddressChangeNotificationMessage *msg;
656 663
657 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Notifying client about %s of IP %s\n", 665 "Notifying client about %s of IP %s\n",
659 add ? "addition" : "removal", 666 add ? "addition" : "removal",
660 GNUNET_a2s(addr, 667 GNUNET_a2s (addr,
661 addr_len)); 668 addr_len));
662 env = GNUNET_MQ_msg_extra(msg, 669 env = GNUNET_MQ_msg_extra (msg,
663 addr_len, 670 addr_len,
664 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE); 671 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE);
665 msg->add_remove = htonl(add); 672 msg->add_remove = htonl (add);
666 msg->addr_class = htonl(ac); 673 msg->addr_class = htonl (ac);
667 GNUNET_memcpy(&msg[1], 674 GNUNET_memcpy (&msg[1],
668 addr, 675 addr,
669 addr_len); 676 addr_len);
670 GNUNET_MQ_send(ch->mq, 677 GNUNET_MQ_send (ch->mq,
671 env); 678 env);
672} 679}
673 680
674 681
@@ -681,123 +688,123 @@ notify_client(enum GNUNET_NAT_AddressClass ac,
681 * @param add #GNUNET_YES to add, #GNUNET_NO to remove 688 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
682 */ 689 */
683static void 690static void
684check_notify_client(struct LocalAddressList *delta, 691check_notify_client (struct LocalAddressList *delta,
685 struct ClientHandle *ch, 692 struct ClientHandle *ch,
686 int add) 693 int add)
687{ 694{
688 size_t alen; 695 size_t alen;
689 struct sockaddr_in v4; 696 struct sockaddr_in v4;
690 struct sockaddr_in6 v6; 697 struct sockaddr_in6 v6;
691 698
692 if (0 == (ch->flags & GNUNET_NAT_RF_ADDRESSES)) 699 if (0 == (ch->flags & GNUNET_NAT_RF_ADDRESSES))
693 { 700 {
694 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Not notifying client as it does not care about addresses\n"); 702 "Not notifying client as it does not care about addresses\n");
696 return; 703 return;
697 } 704 }
698 switch (delta->af) 705 switch (delta->af)
706 {
707 case AF_INET:
708 alen = sizeof(struct sockaddr_in);
709 GNUNET_memcpy (&v4,
710 &delta->addr,
711 alen);
712
713 /* Check for client notifications */
714 for (unsigned int i = 0; i < ch->num_caddrs; i++)
699 { 715 {
700 case AF_INET: 716 const struct sockaddr_in *c4;
701 alen = sizeof(struct sockaddr_in); 717
702 GNUNET_memcpy(&v4, 718 if (AF_INET != ch->caddrs[i].ss.ss_family)
703 &delta->addr, 719 continue; /* IPv4 not relevant */
704 alen); 720 c4 = (const struct sockaddr_in *) &ch->caddrs[i].ss;
705 721 if (match_ipv4 ("127.0.0.1", &c4->sin_addr, 8) &&
706 /* Check for client notifications */ 722 (0 != c4->sin_addr.s_addr) &&
707 for (unsigned int i = 0; i < ch->num_caddrs; i++) 723 (! match_ipv4 ("127.0.0.1", &v4.sin_addr, 8)))
708 { 724 continue; /* bound to loopback, but this is not loopback */
709 const struct sockaddr_in *c4; 725 if ((! match_ipv4 ("127.0.0.1", &c4->sin_addr, 8)) &&
710 726 match_ipv4 ("127.0.0.1", &v4.sin_addr, 8))
711 if (AF_INET != ch->caddrs[i].ss.ss_family) 727 continue; /* bound to non-loopback, but this is loopback */
712 continue; /* IPv4 not relevant */ 728 if ((0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) &&
713 c4 = (const struct sockaddr_in *)&ch->caddrs[i].ss; 729 (0 != c4->sin_addr.s_addr) &&
714 if (match_ipv4("127.0.0.1", &c4->sin_addr, 8) && 730 (! is_nat_v4 (&v4.sin_addr)))
715 (0 != c4->sin_addr.s_addr) && 731 continue; /* based on external-IP, but this IP is not
716 (!match_ipv4("127.0.0.1", &v4.sin_addr, 8)))
717 continue; /* bound to loopback, but this is not loopback */
718 if ((!match_ipv4("127.0.0.1", &c4->sin_addr, 8)) &&
719 match_ipv4("127.0.0.1", &v4.sin_addr, 8))
720 continue; /* bound to non-loopback, but this is loopback */
721 if ((0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) &&
722 (0 != c4->sin_addr.s_addr) &&
723 (!is_nat_v4(&v4.sin_addr)))
724 continue; /* based on external-IP, but this IP is not
725 from private address range. */ 732 from private address range. */
726 if ((0 != GNUNET_memcmp(&v4.sin_addr, 733 if ((0 != GNUNET_memcmp (&v4.sin_addr,
727 &c4->sin_addr)) && 734 &c4->sin_addr)) &&
728 (0 != c4->sin_addr.s_addr) && 735 (0 != c4->sin_addr.s_addr) &&
729 (!is_nat_v4(&c4->sin_addr))) 736 (! is_nat_v4 (&c4->sin_addr)))
730 continue; /* this IP is not from private address range, 737 continue; /* this IP is not from private address range,
731 and IP does not match. */ 738 and IP does not match. */
732 739
733 /* OK, IP seems relevant, notify client */ 740 /* OK, IP seems relevant, notify client */
734 if (0 == htons(v4.sin_port)) 741 if (0 == htons (v4.sin_port))
735 v4.sin_port = c4->sin_port; 742 v4.sin_port = c4->sin_port;
736 notify_client(delta->ac, 743 notify_client (delta->ac,
737 ch, 744 ch,
738 add, 745 add,
739 &v4, 746 &v4,
740 alen); 747 alen);
741 } 748 }
742 break; 749 break;
743 750
744 case AF_INET6: 751 case AF_INET6:
745 alen = sizeof(struct sockaddr_in6); 752 alen = sizeof(struct sockaddr_in6);
746 GNUNET_memcpy(&v6, 753 GNUNET_memcpy (&v6,
747 &delta->addr, 754 &delta->addr,
748 alen); 755 alen);
749 for (unsigned int i = 0; i < ch->num_caddrs; i++) 756 for (unsigned int i = 0; i < ch->num_caddrs; i++)
750 { 757 {
751 const struct sockaddr_in6 *c6; 758 const struct sockaddr_in6 *c6;
752 759
753 if (AF_INET6 != ch->caddrs[i].ss.ss_family) 760 if (AF_INET6 != ch->caddrs[i].ss.ss_family)
754 continue; /* IPv4 not relevant */ 761 continue; /* IPv4 not relevant */
755 c6 = (const struct sockaddr_in6 *)&ch->caddrs[i].ss; 762 c6 = (const struct sockaddr_in6 *) &ch->caddrs[i].ss;
756 if (match_ipv6("::1", &c6->sin6_addr, 128) && 763 if (match_ipv6 ("::1", &c6->sin6_addr, 128) &&
757 (0 != GNUNET_memcmp(&c6->sin6_addr, 764 (0 != GNUNET_memcmp (&c6->sin6_addr,
758 &in6addr_any)) && 765 &in6addr_any)) &&
759 (!match_ipv6("::1", &v6.sin6_addr, 128))) 766 (! match_ipv6 ("::1", &v6.sin6_addr, 128)))
760 continue; /* bound to loopback, but this is not loopback */ 767 continue; /* bound to loopback, but this is not loopback */
761 if ((!match_ipv6("::1", &c6->sin6_addr, 128)) && 768 if ((! match_ipv6 ("::1", &c6->sin6_addr, 128)) &&
762 match_ipv6("::1", &v6.sin6_addr, 128)) 769 match_ipv6 ("::1", &v6.sin6_addr, 128))
763 continue; /* bound to non-loopback, but this is loopback */ 770 continue; /* bound to non-loopback, but this is loopback */
764 if ((0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) && 771 if ((0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) &&
765 (0 != GNUNET_memcmp(&c6->sin6_addr, 772 (0 != GNUNET_memcmp (&c6->sin6_addr,
766 &in6addr_any)) && 773 &in6addr_any)) &&
767 (!is_nat_v6(&v6.sin6_addr))) 774 (! is_nat_v6 (&v6.sin6_addr)))
768 continue; /* based on external-IP, but this IP is not 775 continue; /* based on external-IP, but this IP is not
769 from private address range. */ 776 from private address range. */
770 if ((0 != GNUNET_memcmp(&v6.sin6_addr, 777 if ((0 != GNUNET_memcmp (&v6.sin6_addr,
771 &c6->sin6_addr)) && 778 &c6->sin6_addr)) &&
772 (0 != GNUNET_memcmp(&c6->sin6_addr, 779 (0 != GNUNET_memcmp (&c6->sin6_addr,
773 &in6addr_any)) && 780 &in6addr_any)) &&
774 (!is_nat_v6(&c6->sin6_addr))) 781 (! is_nat_v6 (&c6->sin6_addr)))
775 continue; /* this IP is not from private address range, 782 continue; /* this IP is not from private address range,
776 and IP does not match. */ 783 and IP does not match. */
777 if ((match_ipv6("fe80::", &c6->sin6_addr, 10)) && 784 if ((match_ipv6 ("fe80::", &c6->sin6_addr, 10)) &&
778 (0 != GNUNET_memcmp(&c6->sin6_addr, 785 (0 != GNUNET_memcmp (&c6->sin6_addr,
779 &in6addr_any)) && 786 &in6addr_any)) &&
780 (0 != GNUNET_memcmp(&v6.sin6_addr, 787 (0 != GNUNET_memcmp (&v6.sin6_addr,
781 &c6->sin6_addr)) && 788 &c6->sin6_addr)) &&
782 (0 == (delta->ac & GNUNET_NAT_AC_EXTERN))) 789 (0 == (delta->ac & GNUNET_NAT_AC_EXTERN)))
783 continue; /* client bound to link-local, and the other address 790 continue; /* client bound to link-local, and the other address
784 does not match and is not an external IP */ 791 does not match and is not an external IP */
785 792
786 /* OK, IP seems relevant, notify client */ 793 /* OK, IP seems relevant, notify client */
787 if (0 == htons(v6.sin6_port)) 794 if (0 == htons (v6.sin6_port))
788 v6.sin6_port = c6->sin6_port; 795 v6.sin6_port = c6->sin6_port;
789 notify_client(delta->ac, 796 notify_client (delta->ac,
790 ch, 797 ch,
791 add, 798 add,
792 &v6, 799 &v6,
793 alen); 800 alen);
794 }
795 break;
796
797 default:
798 GNUNET_break(0);
799 return;
800 } 801 }
802 break;
803
804 default:
805 GNUNET_break (0);
806 return;
807 }
801} 808}
802 809
803 810
@@ -809,15 +816,15 @@ check_notify_client(struct LocalAddressList *delta,
809 * @param add #GNUNET_YES to add, #GNUNET_NO to remove 816 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
810 */ 817 */
811static void 818static void
812notify_clients(struct LocalAddressList *delta, 819notify_clients (struct LocalAddressList *delta,
813 int add) 820 int add)
814{ 821{
815 for (struct ClientHandle *ch = ch_head; 822 for (struct ClientHandle *ch = ch_head;
816 NULL != ch; 823 NULL != ch;
817 ch = ch->next) 824 ch = ch->next)
818 check_notify_client(delta, 825 check_notify_client (delta,
819 ch, 826 ch,
820 add); 827 add);
821} 828}
822 829
823 830
@@ -830,9 +837,9 @@ notify_clients(struct LocalAddressList *delta,
830 * @param add #GNUNET_YES to add, #GNUNET_NO to remove 837 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
831 */ 838 */
832static void 839static void
833notify_client_external_ipv4_change(void *cls, 840notify_client_external_ipv4_change (void *cls,
834 const struct in_addr *v4, 841 const struct in_addr *v4,
835 int add) 842 int add)
836{ 843{
837 struct ClientHandle *ch = cls; 844 struct ClientHandle *ch = cls;
838 struct sockaddr_in sa; 845 struct sockaddr_in sa;
@@ -840,64 +847,64 @@ notify_client_external_ipv4_change(void *cls,
840 847
841 /* (0) check if this impacts 'hole_external' */ 848 /* (0) check if this impacts 'hole_external' */
842 if ((NULL != ch->hole_external) && 849 if ((NULL != ch->hole_external) &&
843 (0 == strcasecmp(ch->hole_external, 850 (0 == strcasecmp (ch->hole_external,
844 "AUTO"))) 851 "AUTO")))
845 { 852 {
846 struct LocalAddressList lal; 853 struct LocalAddressList lal;
847 struct sockaddr_in *s4; 854 struct sockaddr_in *s4;
848 855
849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "Detected eternal IP, can now back-fill AUTO:%u in hole punching specification of `%s'\n", 857 "Detected eternal IP, can now back-fill AUTO:%u in hole punching specification of `%s'\n",
851 (unsigned int)ch->ext_dns_port, 858 (unsigned int) ch->ext_dns_port,
852 ch->section_name); 859 ch->section_name);
853 memset(&lal, 0, sizeof(lal)); 860 memset (&lal, 0, sizeof(lal));
854 s4 = (struct sockaddr_in *)&lal.addr; 861 s4 = (struct sockaddr_in *) &lal.addr;
855 s4->sin_family = AF_INET; 862 s4->sin_family = AF_INET;
856 s4->sin_port = htons(ch->ext_dns_port); 863 s4->sin_port = htons (ch->ext_dns_port);
857 s4->sin_addr = *v4; 864 s4->sin_addr = *v4;
858 lal.af = AF_INET; 865 lal.af = AF_INET;
859 lal.ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; 866 lal.ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
860 check_notify_client(&lal, 867 check_notify_client (&lal,
861 ch, 868 ch,
862 add); 869 add);
863 } 870 }
864 871
865 /* (1) check if client cares. */ 872 /* (1) check if client cares. */
866 if (!ch->natted_address) 873 if (! ch->natted_address)
867 return; 874 return;
868 have_v4 = GNUNET_NO; 875 have_v4 = GNUNET_NO;
869 for (unsigned int i = 0; i < ch->num_caddrs; i++) 876 for (unsigned int i = 0; i < ch->num_caddrs; i++)
870 { 877 {
871 const struct sockaddr_storage *ss = &ch->caddrs[i].ss; 878 const struct sockaddr_storage *ss = &ch->caddrs[i].ss;
872 879
873 if (AF_INET != ss->ss_family) 880 if (AF_INET != ss->ss_family)
874 continue; 881 continue;
875 have_v4 = GNUNET_YES; 882 have_v4 = GNUNET_YES;
876 break; 883 break;
877 } 884 }
878 if (GNUNET_NO == have_v4) 885 if (GNUNET_NO == have_v4)
879 return; /* IPv6-only */ 886 return; /* IPv6-only */
880 887
881 /* (2) build address info */ 888 /* (2) build address info */
882 memset(&sa, 889 memset (&sa,
883 0, 890 0,
884 sizeof(sa)); 891 sizeof(sa));
885 sa.sin_family = AF_INET; 892 sa.sin_family = AF_INET;
886 sa.sin_addr = *v4; 893 sa.sin_addr = *v4;
887 sa.sin_port = htons(0); 894 sa.sin_port = htons (0);
888 895
889 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Detected eternal IP %s, notifying client of external IP (without port)\n", 897 "Detected eternal IP %s, notifying client of external IP (without port)\n",
891 GNUNET_a2s((const struct sockaddr *)&sa, 898 GNUNET_a2s ((const struct sockaddr *) &sa,
892 sizeof(sa))); 899 sizeof(sa)));
893 /* (3) notify client of change */ 900 /* (3) notify client of change */
894 notify_client(is_nat_v4(v4) 901 notify_client (is_nat_v4 (v4)
895 ? GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_LAN 902 ? GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_LAN
896 : GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_GLOBAL, 903 : GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_GLOBAL,
897 ch, 904 ch,
898 add, 905 add,
899 &sa, 906 &sa,
900 sizeof(sa)); 907 sizeof(sa));
901} 908}
902 909
903 910
@@ -909,54 +916,54 @@ notify_client_external_ipv4_change(void *cls,
909 * @param ra IP address of the peer who wants us to connect to it 916 * @param ra IP address of the peer who wants us to connect to it
910 */ 917 */
911static void 918static void
912reversal_callback(void *cls, 919reversal_callback (void *cls,
913 const struct sockaddr_in *ra) 920 const struct sockaddr_in *ra)
914{ 921{
915 struct LocalAddressList *lal = cls; 922 struct LocalAddressList *lal = cls;
916 const struct sockaddr_in *l4; 923 const struct sockaddr_in *l4;
917 924
918 GNUNET_assert(AF_INET == lal->af); 925 GNUNET_assert (AF_INET == lal->af);
919 l4 = (const struct sockaddr_in *)&lal->addr; 926 l4 = (const struct sockaddr_in *) &lal->addr;
920 for (struct ClientHandle *ch = ch_head; 927 for (struct ClientHandle *ch = ch_head;
921 NULL != ch; 928 NULL != ch;
922 ch = ch->next) 929 ch = ch->next)
930 {
931 struct GNUNET_NAT_ConnectionReversalRequestedMessage *crrm;
932 struct GNUNET_MQ_Envelope *env;
933 int match;
934
935 /* Check if client is in applicable range for ICMP NAT traversal
936 for this local address */
937 if (! ch->natted_address)
938 continue;
939 match = GNUNET_NO;
940 for (unsigned int i = 0; i < ch->num_caddrs; i++)
923 { 941 {
924 struct GNUNET_NAT_ConnectionReversalRequestedMessage *crrm; 942 struct ClientAddress *ca = &ch->caddrs[i];
925 struct GNUNET_MQ_Envelope *env; 943 const struct sockaddr_in *c4;
926 int match;
927 944
928 /* Check if client is in applicable range for ICMP NAT traversal 945 if (AF_INET != ca->ss.ss_family)
929 for this local address */
930 if (!ch->natted_address)
931 continue; 946 continue;
932 match = GNUNET_NO; 947 c4 = (const struct sockaddr_in *) &ca->ss;
933 for (unsigned int i = 0; i < ch->num_caddrs; i++) 948 if ((0 != c4->sin_addr.s_addr) &&
934 { 949 (l4->sin_addr.s_addr != c4->sin_addr.s_addr))
935 struct ClientAddress *ca = &ch->caddrs[i];
936 const struct sockaddr_in *c4;
937
938 if (AF_INET != ca->ss.ss_family)
939 continue;
940 c4 = (const struct sockaddr_in *)&ca->ss;
941 if ((0 != c4->sin_addr.s_addr) &&
942 (l4->sin_addr.s_addr != c4->sin_addr.s_addr))
943 continue;
944 match = GNUNET_YES;
945 break;
946 }
947 if (!match)
948 continue; 950 continue;
949 951 match = GNUNET_YES;
950 /* Notify applicable client about connection reversal request */ 952 break;
951 env = GNUNET_MQ_msg_extra(crrm,
952 sizeof(struct sockaddr_in),
953 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED);
954 GNUNET_memcpy(&crrm[1],
955 ra,
956 sizeof(struct sockaddr_in));
957 GNUNET_MQ_send(ch->mq,
958 env);
959 } 953 }
954 if (! match)
955 continue;
956
957 /* Notify applicable client about connection reversal request */
958 env = GNUNET_MQ_msg_extra (crrm,
959 sizeof(struct sockaddr_in),
960 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED);
961 GNUNET_memcpy (&crrm[1],
962 ra,
963 sizeof(struct sockaddr_in));
964 GNUNET_MQ_send (ch->mq,
965 env);
966 }
960} 967}
961 968
962 969
@@ -966,104 +973,104 @@ reversal_callback(void *cls,
966 * @param cls NULL 973 * @param cls NULL
967 */ 974 */
968static void 975static void
969run_scan(void *cls) 976run_scan (void *cls)
970{ 977{
971 struct IfcProcContext ifc_ctx; 978 struct IfcProcContext ifc_ctx;
972 int found; 979 int found;
973 int have_nat; 980 int have_nat;
974 struct LocalAddressList *lnext; 981 struct LocalAddressList *lnext;
975 982
976 scan_task = GNUNET_SCHEDULER_add_delayed(SCAN_FREQ, 983 scan_task = GNUNET_SCHEDULER_add_delayed (SCAN_FREQ,
977 &run_scan, 984 &run_scan,
978 NULL); 985 NULL);
979 memset(&ifc_ctx, 986 memset (&ifc_ctx,
980 0, 987 0,
981 sizeof(ifc_ctx)); 988 sizeof(ifc_ctx));
982 GNUNET_OS_network_interfaces_list(&ifc_proc, 989 GNUNET_OS_network_interfaces_list (&ifc_proc,
983 &ifc_ctx); 990 &ifc_ctx);
984 /* remove addresses that disappeared */ 991 /* remove addresses that disappeared */
985 for (struct LocalAddressList *lal = lal_head; 992 for (struct LocalAddressList *lal = lal_head;
986 NULL != lal; 993 NULL != lal;
987 lal = lnext) 994 lal = lnext)
995 {
996 lnext = lal->next;
997 found = GNUNET_NO;
998 for (struct LocalAddressList *pos = ifc_ctx.lal_head;
999 NULL != pos;
1000 pos = pos->next)
1001 {
1002 if ((pos->af == lal->af) &&
1003 (0 == memcmp (&lal->addr,
1004 &pos->addr,
1005 (AF_INET == lal->af)
1006 ? sizeof(struct sockaddr_in)
1007 : sizeof(struct sockaddr_in6))))
1008 {
1009 found = GNUNET_YES;
1010 }
1011 }
1012 if (GNUNET_NO == found)
988 { 1013 {
989 lnext = lal->next; 1014 notify_clients (lal,
990 found = GNUNET_NO; 1015 GNUNET_NO);
991 for (struct LocalAddressList *pos = ifc_ctx.lal_head; 1016 free_lal (lal);
992 NULL != pos;
993 pos = pos->next)
994 {
995 if ((pos->af == lal->af) &&
996 (0 == memcmp(&lal->addr,
997 &pos->addr,
998 (AF_INET == lal->af)
999 ? sizeof(struct sockaddr_in)
1000 : sizeof(struct sockaddr_in6))))
1001 {
1002 found = GNUNET_YES;
1003 }
1004 }
1005 if (GNUNET_NO == found)
1006 {
1007 notify_clients(lal,
1008 GNUNET_NO);
1009 free_lal(lal);
1010 }
1011 } 1017 }
1018 }
1012 1019
1013 /* add addresses that appeared */ 1020 /* add addresses that appeared */
1014 have_nat = GNUNET_NO; 1021 have_nat = GNUNET_NO;
1015 for (struct LocalAddressList *pos = ifc_ctx.lal_head; 1022 for (struct LocalAddressList *pos = ifc_ctx.lal_head;
1016 NULL != pos; 1023 NULL != pos;
1017 pos = ifc_ctx.lal_head) 1024 pos = ifc_ctx.lal_head)
1025 {
1026 found = GNUNET_NO;
1027 if (GNUNET_NAT_AC_LAN == (GNUNET_NAT_AC_LAN & pos->ac))
1028 have_nat = GNUNET_YES;
1029 for (struct LocalAddressList *lal = lal_head;
1030 NULL != lal;
1031 lal = lal->next)
1018 { 1032 {
1019 found = GNUNET_NO; 1033 if ((pos->af == lal->af) &&
1020 if (GNUNET_NAT_AC_LAN == (GNUNET_NAT_AC_LAN & pos->ac)) 1034 (0 == memcmp (&lal->addr,
1021 have_nat = GNUNET_YES; 1035 &pos->addr,
1022 for (struct LocalAddressList *lal = lal_head; 1036 (AF_INET == lal->af)
1023 NULL != lal; 1037 ? sizeof(struct sockaddr_in)
1024 lal = lal->next) 1038 : sizeof(struct sockaddr_in6))))
1025 { 1039 found = GNUNET_YES;
1026 if ((pos->af == lal->af) && 1040 }
1027 (0 == memcmp(&lal->addr, 1041 GNUNET_CONTAINER_DLL_remove (ifc_ctx.lal_head,
1028 &pos->addr, 1042 ifc_ctx.lal_tail,
1029 (AF_INET == lal->af) 1043 pos);
1030 ? sizeof(struct sockaddr_in) 1044 if (GNUNET_YES == found)
1031 : sizeof(struct sockaddr_in6)))) 1045 {
1032 found = GNUNET_YES; 1046 GNUNET_free (pos);
1033 }
1034 GNUNET_CONTAINER_DLL_remove(ifc_ctx.lal_head,
1035 ifc_ctx.lal_tail,
1036 pos);
1037 if (GNUNET_YES == found)
1038 {
1039 GNUNET_free(pos);
1040 }
1041 else
1042 {
1043 notify_clients(pos,
1044 GNUNET_YES);
1045 GNUNET_CONTAINER_DLL_insert(lal_head,
1046 lal_tail,
1047 pos);
1048 if ((AF_INET == pos->af) &&
1049 (NULL == pos->hc) &&
1050 (0 != (GNUNET_NAT_AC_LAN & pos->ac)))
1051 {
1052 const struct sockaddr_in *s4
1053 = (const struct sockaddr_in *)&pos->addr;
1054
1055 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1056 "Found NATed local address %s, starting NAT server\n",
1057 GNUNET_a2s((const struct sockaddr *)&pos->addr,
1058 sizeof(*s4)));
1059 pos->hc = GN_start_gnunet_nat_server_(&s4->sin_addr,
1060 &reversal_callback,
1061 pos,
1062 cfg);
1063 }
1064 }
1065 } 1047 }
1066 GN_nat_status_changed(have_nat); 1048 else
1049 {
1050 notify_clients (pos,
1051 GNUNET_YES);
1052 GNUNET_CONTAINER_DLL_insert (lal_head,
1053 lal_tail,
1054 pos);
1055 if ((AF_INET == pos->af) &&
1056 (NULL == pos->hc) &&
1057 (0 != (GNUNET_NAT_AC_LAN & pos->ac)))
1058 {
1059 const struct sockaddr_in *s4
1060 = (const struct sockaddr_in *) &pos->addr;
1061
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1063 "Found NATed local address %s, starting NAT server\n",
1064 GNUNET_a2s ((const struct sockaddr *) &pos->addr,
1065 sizeof(*s4)));
1066 pos->hc = GN_start_gnunet_nat_server_ (&s4->sin_addr,
1067 &reversal_callback,
1068 pos,
1069 cfg);
1070 }
1071 }
1072 }
1073 GN_nat_status_changed (have_nat);
1067} 1074}
1068 1075
1069 1076
@@ -1079,91 +1086,91 @@ run_scan(void *cls)
1079 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code 1086 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
1080 */ 1087 */
1081static void 1088static void
1082upnp_addr_change_cb(void *cls, 1089upnp_addr_change_cb (void *cls,
1083 int add_remove, 1090 int add_remove,
1084 const struct sockaddr *addr, 1091 const struct sockaddr *addr,
1085 socklen_t addrlen, 1092 socklen_t addrlen,
1086 enum GNUNET_NAT_StatusCode result) 1093 enum GNUNET_NAT_StatusCode result)
1087{ 1094{
1088 struct ClientHandle *ch = cls; 1095 struct ClientHandle *ch = cls;
1089 enum GNUNET_NAT_AddressClass ac; 1096 enum GNUNET_NAT_AddressClass ac;
1090 1097
1091 switch (result) 1098 switch (result)
1092 { 1099 {
1093 case GNUNET_NAT_ERROR_SUCCESS: 1100 case GNUNET_NAT_ERROR_SUCCESS:
1094 GNUNET_assert(NULL != addr); 1101 GNUNET_assert (NULL != addr);
1095 break; 1102 break;
1096 1103
1097 case GNUNET_NAT_ERROR_UPNPC_FAILED: 1104 case GNUNET_NAT_ERROR_UPNPC_FAILED:
1098 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT: 1105 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
1099 case GNUNET_NAT_ERROR_IPC_FAILURE: 1106 case GNUNET_NAT_ERROR_IPC_FAILURE:
1100 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1101 "Running upnpc failed: %d\n", 1108 "Running upnpc failed: %d\n",
1102 result); 1109 result);
1103 return; 1110 return;
1104 1111
1105 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND: 1112 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
1106 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 1113 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1107 "external-ip binary not found\n"); 1114 "external-ip binary not found\n");
1108 return; 1115 return;
1109 1116
1110 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND: 1117 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
1111 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 1118 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1112 "upnpc binary not found\n"); 1119 "upnpc binary not found\n");
1113 return; 1120 return;
1114 1121
1115 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED: 1122 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
1116 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1123 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1117 "external-ip binary could not be run\n"); 1124 "external-ip binary could not be run\n");
1118 return; 1125 return;
1119 1126
1120 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED: 1127 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
1121 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1128 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1122 "upnpc failed to create port mapping\n"); 1129 "upnpc failed to create port mapping\n");
1123 return; 1130 return;
1124 1131
1125 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID: 1132 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
1126 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "Invalid output from upnpc\n"); 1134 "Invalid output from upnpc\n");
1128 return; 1135 return;
1129 1136
1130 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID: 1137 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
1131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Invalid address returned by upnpc\n"); 1139 "Invalid address returned by upnpc\n");
1133 return; 1140 return;
1134 1141
1135 default: 1142 default:
1136 GNUNET_break(0); /* should not be possible */ 1143 GNUNET_break (0); /* should not be possible */
1137 return; 1144 return;
1138 } 1145 }
1139 switch (addr->sa_family) 1146 switch (addr->sa_family)
1140 { 1147 {
1141 case AF_INET: 1148 case AF_INET:
1142 ac = is_nat_v4(&((const struct sockaddr_in *)addr)->sin_addr) 1149 ac = is_nat_v4 (&((const struct sockaddr_in *) addr)->sin_addr)
1143 ? GNUNET_NAT_AC_LAN 1150 ? GNUNET_NAT_AC_LAN
1144 : GNUNET_NAT_AC_EXTERN; 1151 : GNUNET_NAT_AC_EXTERN;
1145 break; 1152 break;
1146 1153
1147 case AF_INET6: 1154 case AF_INET6:
1148 ac = is_nat_v6(&((const struct sockaddr_in6 *)addr)->sin6_addr) 1155 ac = is_nat_v6 (&((const struct sockaddr_in6 *) addr)->sin6_addr)
1149 ? GNUNET_NAT_AC_LAN 1156 ? GNUNET_NAT_AC_LAN
1150 : GNUNET_NAT_AC_EXTERN; 1157 : GNUNET_NAT_AC_EXTERN;
1151 break; 1158 break;
1152 1159
1153 default: 1160 default:
1154 GNUNET_break(0); 1161 GNUNET_break (0);
1155 return; 1162 return;
1156 } 1163 }
1157 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "upnp external address %s: %s\n", 1165 "upnp external address %s: %s\n",
1159 add_remove ? "added" : "removed", 1166 add_remove ? "added" : "removed",
1160 GNUNET_a2s(addr, 1167 GNUNET_a2s (addr,
1161 addrlen)); 1168 addrlen));
1162 notify_client(ac, 1169 notify_client (ac,
1163 ch, 1170 ch,
1164 add_remove, 1171 add_remove,
1165 addr, 1172 addr,
1166 addrlen); 1173 addrlen);
1167} 1174}
1168 1175
1169 1176
@@ -1176,7 +1183,7 @@ upnp_addr_change_cb(void *cls,
1176 * @param ch client handle to act upon 1183 * @param ch client handle to act upon
1177 */ 1184 */
1178static void 1185static void
1179dyndns_lookup(void *cls); 1186dyndns_lookup (void *cls);
1180 1187
1181 1188
1182/** 1189/**
@@ -1189,9 +1196,9 @@ dyndns_lookup(void *cls);
1189 * @param addrlen number of bytes in @a addr 1196 * @param addrlen number of bytes in @a addr
1190 */ 1197 */
1191static void 1198static void
1192process_external_ip(void *cls, 1199process_external_ip (void *cls,
1193 const struct sockaddr *addr, 1200 const struct sockaddr *addr,
1194 socklen_t addrlen) 1201 socklen_t addrlen)
1195{ 1202{
1196 struct ClientHandle *ch = cls; 1203 struct ClientHandle *ch = cls;
1197 struct LocalAddressList *lal; 1204 struct LocalAddressList *lal;
@@ -1200,86 +1207,86 @@ process_external_ip(void *cls,
1200 struct sockaddr_in6 *v6; 1207 struct sockaddr_in6 *v6;
1201 1208
1202 if (NULL == addr) 1209 if (NULL == addr)
1210 {
1211 struct LocalAddressList *laln;
1212
1213 ch->ext_dns = NULL;
1214 ch->ext_dns_task
1215 = GNUNET_SCHEDULER_add_delayed (dyndns_frequency,
1216 &dyndns_lookup,
1217 ch);
1218 /* Current iteration is over, remove 'old' IPs now */
1219 for (lal = ch->ext_addr_head; NULL != lal; lal = laln)
1203 { 1220 {
1204 struct LocalAddressList *laln; 1221 laln = lal->next;
1205 1222 if (GNUNET_YES == lal->old)
1206 ch->ext_dns = NULL; 1223 {
1207 ch->ext_dns_task 1224 GNUNET_CONTAINER_DLL_remove (ch->ext_addr_head,
1208 = GNUNET_SCHEDULER_add_delayed(dyndns_frequency, 1225 ch->ext_addr_tail,
1209 &dyndns_lookup, 1226 lal);
1210 ch); 1227 check_notify_client (lal,
1211 /* Current iteration is over, remove 'old' IPs now */ 1228 ch,
1212 for (lal = ch->ext_addr_head; NULL != lal; lal = laln) 1229 GNUNET_NO);
1213 { 1230 GNUNET_free (lal);
1214 laln = lal->next; 1231 }
1215 if (GNUNET_YES == lal->old)
1216 {
1217 GNUNET_CONTAINER_DLL_remove(ch->ext_addr_head,
1218 ch->ext_addr_tail,
1219 lal);
1220 check_notify_client(lal,
1221 ch,
1222 GNUNET_NO);
1223 GNUNET_free(lal);
1224 }
1225 }
1226 return;
1227 } 1232 }
1228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1233 return;
1229 "Got IP `%s' for external address `%s'\n", 1234 }
1230 GNUNET_a2s(addr, 1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 addrlen), 1236 "Got IP `%s' for external address `%s'\n",
1232 ch->hole_external); 1237 GNUNET_a2s (addr,
1238 addrlen),
1239 ch->hole_external);
1233 1240
1234 /* build sockaddr storage with port number */ 1241 /* build sockaddr storage with port number */
1235 memset(&ss, 1242 memset (&ss,
1236 0, 1243 0,
1237 sizeof(ss)); 1244 sizeof(ss));
1238 GNUNET_memcpy(&ss, 1245 GNUNET_memcpy (&ss,
1239 addr, 1246 addr,
1240 addrlen); 1247 addrlen);
1241 switch (addr->sa_family) 1248 switch (addr->sa_family)
1242 { 1249 {
1243 case AF_INET: 1250 case AF_INET:
1244 v4 = (struct sockaddr_in *)&ss; 1251 v4 = (struct sockaddr_in *) &ss;
1245 v4->sin_port = htons(ch->ext_dns_port); 1252 v4->sin_port = htons (ch->ext_dns_port);
1246 break; 1253 break;
1247 1254
1248 case AF_INET6: 1255 case AF_INET6:
1249 v6 = (struct sockaddr_in6 *)&ss; 1256 v6 = (struct sockaddr_in6 *) &ss;
1250 v6->sin6_port = htons(ch->ext_dns_port); 1257 v6->sin6_port = htons (ch->ext_dns_port);
1251 break; 1258 break;
1252 1259
1253 default: 1260 default:
1254 GNUNET_break(0); 1261 GNUNET_break (0);
1255 return; 1262 return;
1256 } 1263 }
1257 /* See if 'ss' matches any of our known addresses */ 1264 /* See if 'ss' matches any of our known addresses */
1258 for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next) 1265 for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next)
1266 {
1267 if (GNUNET_NO == lal->old)
1268 continue; /* already processed, skip */
1269 if ((addr->sa_family == lal->addr.ss_family) &&
1270 (0 == memcmp (&ss,
1271 &lal->addr,
1272 addrlen)))
1259 { 1273 {
1260 if (GNUNET_NO == lal->old) 1274 /* Address unchanged, remember so we do not remove */
1261 continue; /* already processed, skip */ 1275 lal->old = GNUNET_NO;
1262 if ((addr->sa_family == lal->addr.ss_family) && 1276 return; /* done here */
1263 (0 == memcmp(&ss,
1264 &lal->addr,
1265 addrlen)))
1266 {
1267 /* Address unchanged, remember so we do not remove */
1268 lal->old = GNUNET_NO;
1269 return; /* done here */
1270 }
1271 } 1277 }
1278 }
1272 /* notify client, and remember IP for later removal! */ 1279 /* notify client, and remember IP for later removal! */
1273 lal = GNUNET_new(struct LocalAddressList); 1280 lal = GNUNET_new (struct LocalAddressList);
1274 lal->addr = ss; 1281 lal->addr = ss;
1275 lal->af = ss.ss_family; 1282 lal->af = ss.ss_family;
1276 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; 1283 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1277 GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head, 1284 GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head,
1278 ch->ext_addr_tail, 1285 ch->ext_addr_tail,
1279 lal); 1286 lal);
1280 check_notify_client(lal, 1287 check_notify_client (lal,
1281 ch, 1288 ch,
1282 GNUNET_YES); 1289 GNUNET_YES);
1283} 1290}
1284 1291
1285 1292
@@ -1292,24 +1299,24 @@ process_external_ip(void *cls,
1292 * @param ch client handle to act upon 1299 * @param ch client handle to act upon
1293 */ 1300 */
1294static void 1301static void
1295dyndns_lookup(void *cls) 1302dyndns_lookup (void *cls)
1296{ 1303{
1297 struct ClientHandle *ch = cls; 1304 struct ClientHandle *ch = cls;
1298 struct LocalAddressList *lal; 1305 struct LocalAddressList *lal;
1299 1306
1300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "Performing DNS lookup for punched hole given for `%s' as `%s:%u'\n", 1308 "Performing DNS lookup for punched hole given for `%s' as `%s:%u'\n",
1302 ch->section_name, 1309 ch->section_name,
1303 ch->hole_external, 1310 ch->hole_external,
1304 (unsigned int)ch->ext_dns_port); 1311 (unsigned int) ch->ext_dns_port);
1305 for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next) 1312 for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next)
1306 lal->old = GNUNET_YES; 1313 lal->old = GNUNET_YES;
1307 ch->ext_dns_task = NULL; 1314 ch->ext_dns_task = NULL;
1308 ch->ext_dns = GNUNET_RESOLVER_ip_get(ch->hole_external, 1315 ch->ext_dns = GNUNET_RESOLVER_ip_get (ch->hole_external,
1309 AF_UNSPEC, 1316 AF_UNSPEC,
1310 GNUNET_TIME_UNIT_MINUTES, 1317 GNUNET_TIME_UNIT_MINUTES,
1311 &process_external_ip, 1318 &process_external_ip,
1312 ch); 1319 ch);
1313} 1320}
1314 1321
1315 1322
@@ -1325,105 +1332,107 @@ dyndns_lookup(void *cls)
1325 * @param ch client handle to act upon 1332 * @param ch client handle to act upon
1326 */ 1333 */
1327static void 1334static void
1328lookup_hole_external(struct ClientHandle *ch) 1335lookup_hole_external (struct ClientHandle *ch)
1329{ 1336{
1330 char *port; 1337 char *port;
1331 unsigned int pnum; 1338 unsigned int pnum;
1332 struct sockaddr_in *s4; 1339 struct sockaddr_in *s4;
1333 struct LocalAddressList *lal; 1340 struct LocalAddressList *lal;
1334 1341
1335 port = strrchr(ch->hole_external, ':'); 1342 port = strrchr (ch->hole_external, ':');
1336 if (NULL == port) 1343 if (NULL == port)
1337 { 1344 {
1338 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1339 _("Malformed punched hole specification `%s' (lacks port)\n"), 1346 _ ("Malformed punched hole specification `%s' (lacks port)\n"),
1340 ch->hole_external); 1347 ch->hole_external);
1341 return; 1348 return;
1342 } 1349 }
1343 if ((1 != sscanf(port + 1, 1350 if ((1 != sscanf (port + 1,
1344 "%u", 1351 "%u",
1345 &pnum)) || 1352 &pnum)) ||
1346 (pnum > 65535)) 1353 (pnum > 65535))
1347 { 1354 {
1348 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1349 _("Invalid port number in punched hole specification `%s' (lacks port)\n"), 1356 _ (
1350 port + 1); 1357 "Invalid port number in punched hole specification `%s' (lacks port)\n"),
1351 return; 1358 port + 1);
1352 } 1359 return;
1353 ch->ext_dns_port = (uint16_t)pnum; 1360 }
1361 ch->ext_dns_port = (uint16_t) pnum;
1354 *port = '\0'; 1362 *port = '\0';
1355 1363
1356 lal = GNUNET_new(struct LocalAddressList); 1364 lal = GNUNET_new (struct LocalAddressList);
1357 if ('[' == *ch->hole_external) 1365 if ('[' == *ch->hole_external)
1358 { 1366 {
1359 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&lal->addr; 1367 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &lal->addr;
1360
1361 s6->sin6_family = AF_INET6;
1362 if (']' != (ch->hole_external[strlen(ch->hole_external) - 1]))
1363 {
1364 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1365 _("Malformed punched hole specification `%s' (lacks `]')\n"),
1366 ch->hole_external);
1367 GNUNET_free(lal);
1368 return;
1369 }
1370 ch->hole_external[strlen(ch->hole_external) - 1] = '\0';
1371 if (1 != inet_pton(AF_INET6,
1372 ch->hole_external + 1,
1373 &s6->sin6_addr))
1374 {
1375 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1376 _("Malformed punched hole specification `%s' (IPv6 address invalid)"),
1377 ch->hole_external + 1);
1378 GNUNET_free(lal);
1379 return;
1380 }
1381 s6->sin6_port = htons(ch->ext_dns_port);
1382 lal->af = AF_INET6;
1383 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1384 GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head,
1385 ch->ext_addr_tail,
1386 lal);
1387 check_notify_client(lal,
1388 ch,
1389 GNUNET_YES);
1390 return;
1391 }
1392 1368
1393 s4 = (struct sockaddr_in *)&lal->addr; 1369 s6->sin6_family = AF_INET6;
1394 s4->sin_family = AF_INET; 1370 if (']' != (ch->hole_external[strlen (ch->hole_external) - 1]))
1395 if (1 == inet_pton(AF_INET,
1396 ch->hole_external,
1397 &s4->sin_addr))
1398 { 1371 {
1399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1372 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1400 "IPv4 punched hole given for `%s' via `%s:%u'\n", 1373 _ ("Malformed punched hole specification `%s' (lacks `]')\n"),
1401 ch->section_name, 1374 ch->hole_external);
1402 ch->hole_external, 1375 GNUNET_free (lal);
1403 (unsigned int)ch->ext_dns_port);
1404 s4->sin_port = htons(ch->ext_dns_port);
1405 lal->af = AF_INET;
1406 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1407 GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head,
1408 ch->ext_addr_tail,
1409 lal);
1410 check_notify_client(lal,
1411 ch,
1412 GNUNET_YES);
1413 return; 1376 return;
1414 } 1377 }
1415 if (0 == strcasecmp(ch->hole_external, 1378 ch->hole_external[strlen (ch->hole_external) - 1] = '\0';
1416 "AUTO")) 1379 if (1 != inet_pton (AF_INET6,
1380 ch->hole_external + 1,
1381 &s6->sin6_addr))
1417 { 1382 {
1418 /* handled in #notify_client_external_ipv4_change() */ 1383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1419 GNUNET_free(lal); 1384 _ (
1385 "Malformed punched hole specification `%s' (IPv6 address invalid)"),
1386 ch->hole_external + 1);
1387 GNUNET_free (lal);
1420 return; 1388 return;
1421 } 1389 }
1390 s6->sin6_port = htons (ch->ext_dns_port);
1391 lal->af = AF_INET6;
1392 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1393 GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head,
1394 ch->ext_addr_tail,
1395 lal);
1396 check_notify_client (lal,
1397 ch,
1398 GNUNET_YES);
1399 return;
1400 }
1401
1402 s4 = (struct sockaddr_in *) &lal->addr;
1403 s4->sin_family = AF_INET;
1404 if (1 == inet_pton (AF_INET,
1405 ch->hole_external,
1406 &s4->sin_addr))
1407 {
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1409 "IPv4 punched hole given for `%s' via `%s:%u'\n",
1410 ch->section_name,
1411 ch->hole_external,
1412 (unsigned int) ch->ext_dns_port);
1413 s4->sin_port = htons (ch->ext_dns_port);
1414 lal->af = AF_INET;
1415 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1416 GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head,
1417 ch->ext_addr_tail,
1418 lal);
1419 check_notify_client (lal,
1420 ch,
1421 GNUNET_YES);
1422 return;
1423 }
1424 if (0 == strcasecmp (ch->hole_external,
1425 "AUTO"))
1426 {
1427 /* handled in #notify_client_external_ipv4_change() */
1428 GNUNET_free (lal);
1429 return;
1430 }
1422 /* got a DNS name, trigger lookup! */ 1431 /* got a DNS name, trigger lookup! */
1423 GNUNET_free(lal); 1432 GNUNET_free (lal);
1424 ch->ext_dns_task 1433 ch->ext_dns_task
1425 = GNUNET_SCHEDULER_add_now(&dyndns_lookup, 1434 = GNUNET_SCHEDULER_add_now (&dyndns_lookup,
1426 ch); 1435 ch);
1427} 1436}
1428 1437
1429 1438
@@ -1435,8 +1444,8 @@ lookup_hole_external(struct ClientHandle *ch)
1435 * @param message the message received 1444 * @param message the message received
1436 */ 1445 */
1437static void 1446static void
1438handle_register(void *cls, 1447handle_register (void *cls,
1439 const struct GNUNET_NAT_RegisterMessage *message) 1448 const struct GNUNET_NAT_RegisterMessage *message)
1440{ 1449{
1441 struct ClientHandle *ch = cls; 1450 struct ClientHandle *ch = cls;
1442 const char *off; 1451 const char *off;
@@ -1444,125 +1453,125 @@ handle_register(void *cls,
1444 1453
1445 if ((0 != ch->proto) || 1454 if ((0 != ch->proto) ||
1446 (NULL != ch->caddrs)) 1455 (NULL != ch->caddrs))
1447 { 1456 {
1448 /* double registration not allowed */ 1457 /* double registration not allowed */
1449 GNUNET_break(0); 1458 GNUNET_break (0);
1450 GNUNET_SERVICE_client_drop(ch->client); 1459 GNUNET_SERVICE_client_drop (ch->client);
1451 return; 1460 return;
1452 } 1461 }
1453 ch->flags = message->flags; 1462 ch->flags = message->flags;
1454 ch->proto = message->proto; 1463 ch->proto = message->proto;
1455 ch->num_caddrs = ntohs(message->num_addrs); 1464 ch->num_caddrs = ntohs (message->num_addrs);
1456 ch->caddrs = GNUNET_new_array(ch->num_caddrs, 1465 ch->caddrs = GNUNET_new_array (ch->num_caddrs,
1457 struct ClientAddress); 1466 struct ClientAddress);
1458 left = ntohs(message->header.size) - sizeof(*message); 1467 left = ntohs (message->header.size) - sizeof(*message);
1459 off = (const char *)&message[1]; 1468 off = (const char *) &message[1];
1460 for (unsigned int i = 0; i < ch->num_caddrs; i++) 1469 for (unsigned int i = 0; i < ch->num_caddrs; i++)
1470 {
1471 const struct sockaddr *sa = (const struct sockaddr *) off;
1472 size_t alen;
1473 uint16_t port;
1474 int is_nat;
1475
1476 if (sizeof(sa_family_t) > left)
1461 { 1477 {
1462 const struct sockaddr *sa = (const struct sockaddr *)off; 1478 GNUNET_break (0);
1463 size_t alen; 1479 GNUNET_SERVICE_client_drop (ch->client);
1464 uint16_t port; 1480 return;
1465 int is_nat; 1481 }
1466 1482 is_nat = GNUNET_NO;
1467 if (sizeof(sa_family_t) > left) 1483 switch (sa->sa_family)
1468 { 1484 {
1469 GNUNET_break(0); 1485 case AF_INET:
1470 GNUNET_SERVICE_client_drop(ch->client); 1486 {
1471 return; 1487 struct sockaddr_in s4;
1472 } 1488
1473 is_nat = GNUNET_NO; 1489 GNUNET_memcpy (&s4,
1474 switch (sa->sa_family) 1490 off,
1475 { 1491 sizeof(struct sockaddr_in));
1476 case AF_INET: 1492 alen = sizeof(struct sockaddr_in);
1477 { 1493 if (is_nat_v4 (&s4.sin_addr))
1478 struct sockaddr_in s4; 1494 is_nat = GNUNET_YES;
1479 1495 port = ntohs (s4.sin_port);
1480 GNUNET_memcpy(&s4, 1496 }
1481 off, 1497 break;
1482 sizeof(struct sockaddr_in)); 1498
1483 alen = sizeof(struct sockaddr_in); 1499 case AF_INET6:
1484 if (is_nat_v4(&s4.sin_addr)) 1500 {
1485 is_nat = GNUNET_YES; 1501 struct sockaddr_in6 s6;
1486 port = ntohs(s4.sin_port); 1502
1487 } 1503 GNUNET_memcpy (&s6,
1488 break; 1504 off,
1489 1505 sizeof(struct sockaddr_in6));
1490 case AF_INET6: 1506 alen = sizeof(struct sockaddr_in6);
1491 { 1507 if (is_nat_v6 (&s6.sin6_addr))
1492 struct sockaddr_in6 s6; 1508 is_nat = GNUNET_YES;
1493 1509 port = ntohs (s6.sin6_port);
1494 GNUNET_memcpy(&s6, 1510 }
1495 off, 1511 break;
1496 sizeof(struct sockaddr_in6));
1497 alen = sizeof(struct sockaddr_in6);
1498 if (is_nat_v6(&s6.sin6_addr))
1499 is_nat = GNUNET_YES;
1500 port = ntohs(s6.sin6_port);
1501 }
1502 break;
1503 1512
1504#if AF_UNIX 1513#if AF_UNIX
1505 case AF_UNIX: 1514 case AF_UNIX:
1506 alen = sizeof(struct sockaddr_un); 1515 alen = sizeof(struct sockaddr_un);
1507 port = 0; 1516 port = 0;
1508 break; 1517 break;
1509#endif 1518#endif
1510 default: 1519 default:
1511 GNUNET_break(0); 1520 GNUNET_break (0);
1512 GNUNET_SERVICE_client_drop(ch->client); 1521 GNUNET_SERVICE_client_drop (ch->client);
1513 return; 1522 return;
1514 }
1515 /* store address */
1516 GNUNET_assert(alen <= left);
1517 GNUNET_assert(alen <= sizeof(struct sockaddr_storage));
1518 GNUNET_memcpy(&ch->caddrs[i].ss,
1519 off,
1520 alen);
1521
1522 /* If applicable, try UPNPC NAT punching */
1523 if ((is_nat) &&
1524 (enable_upnp) &&
1525 ((IPPROTO_TCP == ch->proto) ||
1526 (IPPROTO_UDP == ch->proto)))
1527 {
1528 ch->natted_address = GNUNET_YES;
1529 ch->caddrs[i].mh
1530 = GNUNET_NAT_mini_map_start(port,
1531 IPPROTO_TCP == ch->proto,
1532 &upnp_addr_change_cb,
1533 ch);
1534 }
1535
1536 off += alen;
1537 } 1523 }
1524 /* store address */
1525 GNUNET_assert (alen <= left);
1526 GNUNET_assert (alen <= sizeof(struct sockaddr_storage));
1527 GNUNET_memcpy (&ch->caddrs[i].ss,
1528 off,
1529 alen);
1530
1531 /* If applicable, try UPNPC NAT punching */
1532 if ((is_nat) &&
1533 (enable_upnp) &&
1534 ((IPPROTO_TCP == ch->proto) ||
1535 (IPPROTO_UDP == ch->proto)))
1536 {
1537 ch->natted_address = GNUNET_YES;
1538 ch->caddrs[i].mh
1539 = GNUNET_NAT_mini_map_start (port,
1540 IPPROTO_TCP == ch->proto,
1541 &upnp_addr_change_cb,
1542 ch);
1543 }
1544
1545 off += alen;
1546 }
1538 1547
1539 ch->section_name 1548 ch->section_name
1540 = GNUNET_strndup(off, 1549 = GNUNET_strndup (off,
1541 ntohs(message->str_len)); 1550 ntohs (message->str_len));
1542 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Received REGISTER message from client for subsystem `%s'\n", 1552 "Received REGISTER message from client for subsystem `%s'\n",
1544 ch->section_name); 1553 ch->section_name);
1545 if (GNUNET_OK == 1554 if (GNUNET_OK ==
1546 GNUNET_CONFIGURATION_get_value_string(cfg, 1555 GNUNET_CONFIGURATION_get_value_string (cfg,
1547 ch->section_name, 1556 ch->section_name,
1548 "HOLE_EXTERNAL", 1557 "HOLE_EXTERNAL",
1549 &ch->hole_external)) 1558 &ch->hole_external))
1550 lookup_hole_external(ch); 1559 lookup_hole_external (ch);
1551 1560
1552 /* Actually send IP address list to client */ 1561 /* Actually send IP address list to client */
1553 for (struct LocalAddressList *lal = lal_head; 1562 for (struct LocalAddressList *lal = lal_head;
1554 NULL != lal; 1563 NULL != lal;
1555 lal = lal->next) 1564 lal = lal->next)
1556 { 1565 {
1557 check_notify_client(lal, 1566 check_notify_client (lal,
1558 ch, 1567 ch,
1559 GNUNET_YES); 1568 GNUNET_YES);
1560 } 1569 }
1561 /* Also consider IPv4 determined by `external-ip` */ 1570 /* Also consider IPv4 determined by `external-ip` */
1562 ch->external_monitor 1571 ch->external_monitor
1563 = GN_external_ipv4_monitor_start(&notify_client_external_ipv4_change, 1572 = GN_external_ipv4_monitor_start (&notify_client_external_ipv4_change,
1564 ch); 1573 ch);
1565 GNUNET_SERVICE_client_continue(ch->client); 1574 GNUNET_SERVICE_client_continue (ch->client);
1566} 1575}
1567 1576
1568 1577
@@ -1575,22 +1584,22 @@ handle_register(void *cls,
1575 * @return #GNUNET_OK if message is well-formed 1584 * @return #GNUNET_OK if message is well-formed
1576 */ 1585 */
1577static int 1586static int
1578check_stun(void *cls, 1587check_stun (void *cls,
1579 const struct GNUNET_NAT_HandleStunMessage *message) 1588 const struct GNUNET_NAT_HandleStunMessage *message)
1580{ 1589{
1581 size_t sa_len = ntohs(message->sender_addr_size); 1590 size_t sa_len = ntohs (message->sender_addr_size);
1582 size_t expect = sa_len + ntohs(message->payload_size); 1591 size_t expect = sa_len + ntohs (message->payload_size);
1583 1592
1584 if (ntohs(message->header.size) - sizeof(*message) != expect) 1593 if (ntohs (message->header.size) - sizeof(*message) != expect)
1585 { 1594 {
1586 GNUNET_break(0); 1595 GNUNET_break (0);
1587 return GNUNET_SYSERR; 1596 return GNUNET_SYSERR;
1588 } 1597 }
1589 if (sa_len < sizeof(sa_family_t)) 1598 if (sa_len < sizeof(sa_family_t))
1590 { 1599 {
1591 GNUNET_break(0); 1600 GNUNET_break (0);
1592 return GNUNET_SYSERR; 1601 return GNUNET_SYSERR;
1593 } 1602 }
1594 return GNUNET_OK; 1603 return GNUNET_OK;
1595} 1604}
1596 1605
@@ -1603,33 +1612,33 @@ check_stun(void *cls,
1603 * @param add #GNUNET_YES to add, #GNUNET_NO to remove 1612 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
1604 */ 1613 */
1605static void 1614static void
1606notify_clients_stun_change(const struct sockaddr_in *ip, 1615notify_clients_stun_change (const struct sockaddr_in *ip,
1607 int add) 1616 int add)
1608{ 1617{
1609 for (struct ClientHandle *ch = ch_head; 1618 for (struct ClientHandle *ch = ch_head;
1610 NULL != ch; 1619 NULL != ch;
1611 ch = ch->next) 1620 ch = ch->next)
1612 { 1621 {
1613 struct sockaddr_in v4; 1622 struct sockaddr_in v4;
1614 struct GNUNET_NAT_AddressChangeNotificationMessage *msg; 1623 struct GNUNET_NAT_AddressChangeNotificationMessage *msg;
1615 struct GNUNET_MQ_Envelope *env; 1624 struct GNUNET_MQ_Envelope *env;
1616 1625
1617 if (!ch->natted_address) 1626 if (! ch->natted_address)
1618 continue; 1627 continue;
1619 v4 = *ip; 1628 v4 = *ip;
1620 v4.sin_port = htons(0); 1629 v4.sin_port = htons (0);
1621 env = GNUNET_MQ_msg_extra(msg, 1630 env = GNUNET_MQ_msg_extra (msg,
1622 sizeof(v4), 1631 sizeof(v4),
1623 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE); 1632 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE);
1624 msg->add_remove = htonl((int32_t)add); 1633 msg->add_remove = htonl ((int32_t) add);
1625 msg->addr_class = htonl(GNUNET_NAT_AC_EXTERN | 1634 msg->addr_class = htonl (GNUNET_NAT_AC_EXTERN
1626 GNUNET_NAT_AC_GLOBAL); 1635 | GNUNET_NAT_AC_GLOBAL);
1627 GNUNET_memcpy(&msg[1], 1636 GNUNET_memcpy (&msg[1],
1628 &v4, 1637 &v4,
1629 sizeof(v4)); 1638 sizeof(v4));
1630 GNUNET_MQ_send(ch->mq, 1639 GNUNET_MQ_send (ch->mq,
1631 env); 1640 env);
1632 } 1641 }
1633} 1642}
1634 1643
1635 1644
@@ -1641,17 +1650,17 @@ notify_clients_stun_change(const struct sockaddr_in *ip,
1641 * @param cls the `struct StunExternalIP` to drop 1650 * @param cls the `struct StunExternalIP` to drop
1642 */ 1651 */
1643static void 1652static void
1644stun_ip_timeout(void *cls) 1653stun_ip_timeout (void *cls)
1645{ 1654{
1646 struct StunExternalIP *se = cls; 1655 struct StunExternalIP *se = cls;
1647 1656
1648 se->timeout_task = NULL; 1657 se->timeout_task = NULL;
1649 notify_clients_stun_change(&se->external_addr, 1658 notify_clients_stun_change (&se->external_addr,
1650 GNUNET_NO); 1659 GNUNET_NO);
1651 GNUNET_CONTAINER_DLL_remove(se_head, 1660 GNUNET_CONTAINER_DLL_remove (se_head,
1652 se_tail, 1661 se_tail,
1653 se); 1662 se);
1654 GNUNET_free(se); 1663 GNUNET_free (se);
1655} 1664}
1656 1665
1657 1666
@@ -1663,107 +1672,107 @@ stun_ip_timeout(void *cls)
1663 * @param message the message received 1672 * @param message the message received
1664 */ 1673 */
1665static void 1674static void
1666handle_stun(void *cls, 1675handle_stun (void *cls,
1667 const struct GNUNET_NAT_HandleStunMessage *message) 1676 const struct GNUNET_NAT_HandleStunMessage *message)
1668{ 1677{
1669 struct ClientHandle *ch = cls; 1678 struct ClientHandle *ch = cls;
1670 const char *buf = (const char *)&message[1]; 1679 const char *buf = (const char *) &message[1];
1671 const struct sockaddr *sa; 1680 const struct sockaddr *sa;
1672 const void *payload; 1681 const void *payload;
1673 size_t sa_len; 1682 size_t sa_len;
1674 size_t payload_size; 1683 size_t payload_size;
1675 struct sockaddr_in external_addr; 1684 struct sockaddr_in external_addr;
1676 1685
1677 sa_len = ntohs(message->sender_addr_size); 1686 sa_len = ntohs (message->sender_addr_size);
1678 payload_size = ntohs(message->payload_size); 1687 payload_size = ntohs (message->payload_size);
1679 sa = (const struct sockaddr *)&buf[0]; 1688 sa = (const struct sockaddr *) &buf[0];
1680 payload = (const struct sockaddr *)&buf[sa_len]; 1689 payload = (const struct sockaddr *) &buf[sa_len];
1681 switch (sa->sa_family) 1690 switch (sa->sa_family)
1691 {
1692 case AF_INET:
1693 if (sa_len != sizeof(struct sockaddr_in))
1682 { 1694 {
1683 case AF_INET: 1695 GNUNET_break (0);
1684 if (sa_len != sizeof(struct sockaddr_in)) 1696 GNUNET_SERVICE_client_drop (ch->client);
1685 { 1697 return;
1686 GNUNET_break(0); 1698 }
1687 GNUNET_SERVICE_client_drop(ch->client); 1699 break;
1688 return;
1689 }
1690 break;
1691 1700
1692 case AF_INET6: 1701 case AF_INET6:
1693 if (sa_len != sizeof(struct sockaddr_in6)) 1702 if (sa_len != sizeof(struct sockaddr_in6))
1694 { 1703 {
1695 GNUNET_break(0); 1704 GNUNET_break (0);
1696 GNUNET_SERVICE_client_drop(ch->client); 1705 GNUNET_SERVICE_client_drop (ch->client);
1697 return; 1706 return;
1698 }
1699 break;
1700 } 1707 }
1701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1708 break;
1702 "Received HANDLE_STUN message from client\n"); 1709 }
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "Received HANDLE_STUN message from client\n");
1703 if (GNUNET_OK == 1712 if (GNUNET_OK ==
1704 GNUNET_NAT_stun_handle_packet_(payload, 1713 GNUNET_NAT_stun_handle_packet_ (payload,
1705 payload_size, 1714 payload_size,
1706 &external_addr)) 1715 &external_addr))
1716 {
1717 /* We now know that a server at "sa" claims that
1718 we are visible at IP "external_addr".
1719
1720 We should (for some fixed period of time) tell
1721 all of our clients that listen to a NAT'ed address
1722 that they might want to consider the given 'external_ip'
1723 as their public IP address (this includes TCP and UDP
1724 clients, even if only UDP sends STUN requests).
1725
1726 If we do not get a renewal, the "external_addr" should be
1727 removed again. The timeout frequency should be configurable
1728 (with a sane default), so that the UDP plugin can tell how
1729 often to re-request STUN.
1730 */
1731 struct StunExternalIP *se;
1732
1733 /* Check if we had a prior response from this STUN server */
1734 for (se = se_head; NULL != se; se = se->next)
1707 { 1735 {
1708 /* We now know that a server at "sa" claims that 1736 if ((se->stun_server_addr_len != sa_len) ||
1709 we are visible at IP "external_addr". 1737 (0 != memcmp (sa,
1710 1738 &se->stun_server_addr,
1711 We should (for some fixed period of time) tell 1739 sa_len)))
1712 all of our clients that listen to a NAT'ed address 1740 continue; /* different STUN server */
1713 that they might want to consider the given 'external_ip' 1741 if (0 != GNUNET_memcmp (&external_addr,
1714 as their public IP address (this includes TCP and UDP 1742 &se->external_addr))
1715 clients, even if only UDP sends STUN requests). 1743 {
1716 1744 /* external IP changed, update! */
1717 If we do not get a renewal, the "external_addr" should be 1745 notify_clients_stun_change (&se->external_addr,
1718 removed again. The timeout frequency should be configurable 1746 GNUNET_NO);
1719 (with a sane default), so that the UDP plugin can tell how 1747 se->external_addr = external_addr;
1720 often to re-request STUN. 1748 notify_clients_stun_change (&se->external_addr,
1721 */ 1749 GNUNET_YES);
1722 struct StunExternalIP *se; 1750 }
1723 1751 /* update timeout */
1724 /* Check if we had a prior response from this STUN server */ 1752 GNUNET_SCHEDULER_cancel (se->timeout_task);
1725 for (se = se_head; NULL != se; se = se->next) 1753 se->timeout_task
1726 { 1754 = GNUNET_SCHEDULER_add_delayed (stun_stale_timeout,
1727 if ((se->stun_server_addr_len != sa_len) || 1755 &stun_ip_timeout,
1728 (0 != memcmp(sa, 1756 se);
1729 &se->stun_server_addr, 1757 return;
1730 sa_len)))
1731 continue; /* different STUN server */
1732 if (0 != GNUNET_memcmp(&external_addr,
1733 &se->external_addr))
1734 {
1735 /* external IP changed, update! */
1736 notify_clients_stun_change(&se->external_addr,
1737 GNUNET_NO);
1738 se->external_addr = external_addr;
1739 notify_clients_stun_change(&se->external_addr,
1740 GNUNET_YES);
1741 }
1742 /* update timeout */
1743 GNUNET_SCHEDULER_cancel(se->timeout_task);
1744 se->timeout_task
1745 = GNUNET_SCHEDULER_add_delayed(stun_stale_timeout,
1746 &stun_ip_timeout,
1747 se);
1748 return;
1749 }
1750 /* STUN server is completely new, create fresh entry */
1751 se = GNUNET_new(struct StunExternalIP);
1752 se->external_addr = external_addr;
1753 GNUNET_memcpy(&se->stun_server_addr,
1754 sa,
1755 sa_len);
1756 se->stun_server_addr_len = sa_len;
1757 se->timeout_task = GNUNET_SCHEDULER_add_delayed(stun_stale_timeout,
1758 &stun_ip_timeout,
1759 se);
1760 GNUNET_CONTAINER_DLL_insert(se_head,
1761 se_tail,
1762 se);
1763 notify_clients_stun_change(&se->external_addr,
1764 GNUNET_NO);
1765 } 1758 }
1766 GNUNET_SERVICE_client_continue(ch->client); 1759 /* STUN server is completely new, create fresh entry */
1760 se = GNUNET_new (struct StunExternalIP);
1761 se->external_addr = external_addr;
1762 GNUNET_memcpy (&se->stun_server_addr,
1763 sa,
1764 sa_len);
1765 se->stun_server_addr_len = sa_len;
1766 se->timeout_task = GNUNET_SCHEDULER_add_delayed (stun_stale_timeout,
1767 &stun_ip_timeout,
1768 se);
1769 GNUNET_CONTAINER_DLL_insert (se_head,
1770 se_tail,
1771 se);
1772 notify_clients_stun_change (&se->external_addr,
1773 GNUNET_NO);
1774 }
1775 GNUNET_SERVICE_client_continue (ch->client);
1767} 1776}
1768 1777
1769 1778
@@ -1777,18 +1786,20 @@ handle_stun(void *cls,
1777 * @return #GNUNET_OK if message is well-formed 1786 * @return #GNUNET_OK if message is well-formed
1778 */ 1787 */
1779static int 1788static int
1780check_request_connection_reversal(void *cls, 1789check_request_connection_reversal (void *cls,
1781 const struct GNUNET_NAT_RequestConnectionReversalMessage *message) 1790 const struct
1791 GNUNET_NAT_RequestConnectionReversalMessage *
1792 message)
1782{ 1793{
1783 size_t expect; 1794 size_t expect;
1784 1795
1785 expect = ntohs(message->local_addr_size) 1796 expect = ntohs (message->local_addr_size)
1786 + ntohs(message->remote_addr_size); 1797 + ntohs (message->remote_addr_size);
1787 if (ntohs(message->header.size) - sizeof(*message) != expect) 1798 if (ntohs (message->header.size) - sizeof(*message) != expect)
1788 { 1799 {
1789 GNUNET_break(0); 1800 GNUNET_break (0);
1790 return GNUNET_SYSERR; 1801 return GNUNET_SYSERR;
1791 } 1802 }
1792 return GNUNET_OK; 1803 return GNUNET_OK;
1793} 1804}
1794 1805
@@ -1801,48 +1812,50 @@ check_request_connection_reversal(void *cls,
1801 * @param message the message received 1812 * @param message the message received
1802 */ 1813 */
1803static void 1814static void
1804handle_request_connection_reversal(void *cls, 1815handle_request_connection_reversal (void *cls,
1805 const struct GNUNET_NAT_RequestConnectionReversalMessage *message) 1816 const struct
1817 GNUNET_NAT_RequestConnectionReversalMessage
1818 *message)
1806{ 1819{
1807 struct ClientHandle *ch = cls; 1820 struct ClientHandle *ch = cls;
1808 const char *buf = (const char *)&message[1]; 1821 const char *buf = (const char *) &message[1];
1809 size_t local_sa_len = ntohs(message->local_addr_size); 1822 size_t local_sa_len = ntohs (message->local_addr_size);
1810 size_t remote_sa_len = ntohs(message->remote_addr_size); 1823 size_t remote_sa_len = ntohs (message->remote_addr_size);
1811 struct sockaddr_in l4; 1824 struct sockaddr_in l4;
1812 struct sockaddr_in r4; 1825 struct sockaddr_in r4;
1813 int ret; 1826 int ret;
1814 1827
1815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1816 "Received REQUEST CONNECTION REVERSAL message from client\n"); 1829 "Received REQUEST CONNECTION REVERSAL message from client\n");
1817 if (local_sa_len != sizeof(struct sockaddr_in)) 1830 if (local_sa_len != sizeof(struct sockaddr_in))
1818 { 1831 {
1819 GNUNET_break_op(0); 1832 GNUNET_break_op (0);
1820 GNUNET_SERVICE_client_drop(ch->client); 1833 GNUNET_SERVICE_client_drop (ch->client);
1821 return; 1834 return;
1822 } 1835 }
1823 if (remote_sa_len != sizeof(struct sockaddr_in)) 1836 if (remote_sa_len != sizeof(struct sockaddr_in))
1824 { 1837 {
1825 GNUNET_break_op(0); 1838 GNUNET_break_op (0);
1826 GNUNET_SERVICE_client_drop(ch->client); 1839 GNUNET_SERVICE_client_drop (ch->client);
1827 return; 1840 return;
1828 } 1841 }
1829 GNUNET_memcpy(&l4, 1842 GNUNET_memcpy (&l4,
1830 buf, 1843 buf,
1831 sizeof(struct sockaddr_in)); 1844 sizeof(struct sockaddr_in));
1832 GNUNET_break_op(AF_INET == l4.sin_family); 1845 GNUNET_break_op (AF_INET == l4.sin_family);
1833 buf += sizeof(struct sockaddr_in); 1846 buf += sizeof(struct sockaddr_in);
1834 GNUNET_memcpy(&r4, 1847 GNUNET_memcpy (&r4,
1835 buf, 1848 buf,
1836 sizeof(struct sockaddr_in)); 1849 sizeof(struct sockaddr_in));
1837 GNUNET_break_op(AF_INET == r4.sin_family); 1850 GNUNET_break_op (AF_INET == r4.sin_family);
1838 ret = GN_request_connection_reversal(&l4.sin_addr, 1851 ret = GN_request_connection_reversal (&l4.sin_addr,
1839 ntohs(l4.sin_port), 1852 ntohs (l4.sin_port),
1840 &r4.sin_addr, 1853 &r4.sin_addr,
1841 cfg); 1854 cfg);
1842 if (GNUNET_OK != ret) 1855 if (GNUNET_OK != ret)
1843 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1856 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1844 _("Connection reversal request failed\n")); 1857 _ ("Connection reversal request failed\n"));
1845 GNUNET_SERVICE_client_continue(ch->client); 1858 GNUNET_SERVICE_client_continue (ch->client);
1846} 1859}
1847 1860
1848 1861
@@ -1852,31 +1865,31 @@ handle_request_connection_reversal(void *cls,
1852 * @param cls unused 1865 * @param cls unused
1853 */ 1866 */
1854static void 1867static void
1855shutdown_task(void *cls) 1868shutdown_task (void *cls)
1856{ 1869{
1857 struct StunExternalIP *se; 1870 struct StunExternalIP *se;
1858 1871
1859 while (NULL != (se = se_head)) 1872 while (NULL != (se = se_head))
1860 { 1873 {
1861 GNUNET_CONTAINER_DLL_remove(se_head, 1874 GNUNET_CONTAINER_DLL_remove (se_head,
1862 se_tail, 1875 se_tail,
1863 se); 1876 se);
1864 GNUNET_SCHEDULER_cancel(se->timeout_task); 1877 GNUNET_SCHEDULER_cancel (se->timeout_task);
1865 GNUNET_free(se); 1878 GNUNET_free (se);
1866 } 1879 }
1867 GN_nat_status_changed(GNUNET_NO); 1880 GN_nat_status_changed (GNUNET_NO);
1868 if (NULL != scan_task) 1881 if (NULL != scan_task)
1869 { 1882 {
1870 GNUNET_SCHEDULER_cancel(scan_task); 1883 GNUNET_SCHEDULER_cancel (scan_task);
1871 scan_task = NULL; 1884 scan_task = NULL;
1872 } 1885 }
1873 if (NULL != stats) 1886 if (NULL != stats)
1874 { 1887 {
1875 GNUNET_STATISTICS_destroy(stats, 1888 GNUNET_STATISTICS_destroy (stats,
1876 GNUNET_NO); 1889 GNUNET_NO);
1877 stats = NULL; 1890 stats = NULL;
1878 } 1891 }
1879 destroy_lal(); 1892 destroy_lal ();
1880} 1893}
1881 1894
1882 1895
@@ -1888,49 +1901,50 @@ shutdown_task(void *cls)
1888 * @param service the initialized service 1901 * @param service the initialized service
1889 */ 1902 */
1890static void 1903static void
1891run(void *cls, 1904run (void *cls,
1892 const struct GNUNET_CONFIGURATION_Handle *c, 1905 const struct GNUNET_CONFIGURATION_Handle *c,
1893 struct GNUNET_SERVICE_Handle *service) 1906 struct GNUNET_SERVICE_Handle *service)
1894{ 1907{
1895 cfg = c; 1908 cfg = c;
1896 if (GNUNET_OK != 1909 if (GNUNET_OK !=
1897 GNUNET_CONFIGURATION_get_value_time(cfg, 1910 GNUNET_CONFIGURATION_get_value_time (cfg,
1898 "NAT", 1911 "NAT",
1899 "STUN_STALE", 1912 "STUN_STALE",
1900 &stun_stale_timeout)) 1913 &stun_stale_timeout))
1901 stun_stale_timeout = GNUNET_TIME_UNIT_HOURS; 1914 stun_stale_timeout = GNUNET_TIME_UNIT_HOURS;
1902 1915
1903 /* Check for UPnP */ 1916 /* Check for UPnP */
1904 enable_upnp 1917 enable_upnp
1905 = GNUNET_CONFIGURATION_get_value_yesno(cfg, 1918 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1906 "NAT", 1919 "NAT",
1907 "ENABLE_UPNP"); 1920 "ENABLE_UPNP");
1908 if (GNUNET_YES == enable_upnp) 1921 if (GNUNET_YES == enable_upnp)
1922 {
1923 /* check if it works */
1924 if (GNUNET_SYSERR ==
1925 GNUNET_OS_check_helper_binary ("upnpc",
1926 GNUNET_NO,
1927 NULL))
1909 { 1928 {
1910 /* check if it works */ 1929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1911 if (GNUNET_SYSERR == 1930 _ (
1912 GNUNET_OS_check_helper_binary("upnpc", 1931 "UPnP enabled in configuration, but UPnP client `upnpc` command not found, disabling UPnP\n"));
1913 GNUNET_NO, 1932 enable_upnp = GNUNET_SYSERR;
1914 NULL))
1915 {
1916 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1917 _("UPnP enabled in configuration, but UPnP client `upnpc` command not found, disabling UPnP\n"));
1918 enable_upnp = GNUNET_SYSERR;
1919 }
1920 } 1933 }
1934 }
1921 if (GNUNET_OK != 1935 if (GNUNET_OK !=
1922 GNUNET_CONFIGURATION_get_value_time(cfg, 1936 GNUNET_CONFIGURATION_get_value_time (cfg,
1923 "nat", 1937 "nat",
1924 "DYNDNS_FREQUENCY", 1938 "DYNDNS_FREQUENCY",
1925 &dyndns_frequency)) 1939 &dyndns_frequency))
1926 dyndns_frequency = DYNDNS_FREQUENCY; 1940 dyndns_frequency = DYNDNS_FREQUENCY;
1927 1941
1928 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, 1942 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1929 NULL); 1943 NULL);
1930 stats = GNUNET_STATISTICS_create("nat", 1944 stats = GNUNET_STATISTICS_create ("nat",
1931 cfg); 1945 cfg);
1932 scan_task = GNUNET_SCHEDULER_add_now(&run_scan, 1946 scan_task = GNUNET_SCHEDULER_add_now (&run_scan,
1933 NULL); 1947 NULL);
1934} 1948}
1935 1949
1936 1950
@@ -1943,18 +1957,18 @@ run(void *cls,
1943 * @return a `struct ClientHandle` 1957 * @return a `struct ClientHandle`
1944 */ 1958 */
1945static void * 1959static void *
1946client_connect_cb(void *cls, 1960client_connect_cb (void *cls,
1947 struct GNUNET_SERVICE_Client *c, 1961 struct GNUNET_SERVICE_Client *c,
1948 struct GNUNET_MQ_Handle *mq) 1962 struct GNUNET_MQ_Handle *mq)
1949{ 1963{
1950 struct ClientHandle *ch; 1964 struct ClientHandle *ch;
1951 1965
1952 ch = GNUNET_new(struct ClientHandle); 1966 ch = GNUNET_new (struct ClientHandle);
1953 ch->mq = mq; 1967 ch->mq = mq;
1954 ch->client = c; 1968 ch->client = c;
1955 GNUNET_CONTAINER_DLL_insert(ch_head, 1969 GNUNET_CONTAINER_DLL_insert (ch_head,
1956 ch_tail, 1970 ch_tail,
1957 ch); 1971 ch);
1958 return ch; 1972 return ch;
1959} 1973}
1960 1974
@@ -1967,50 +1981,50 @@ client_connect_cb(void *cls,
1967 * @param internal_cls a `struct ClientHandle *` 1981 * @param internal_cls a `struct ClientHandle *`
1968 */ 1982 */
1969static void 1983static void
1970client_disconnect_cb(void *cls, 1984client_disconnect_cb (void *cls,
1971 struct GNUNET_SERVICE_Client *c, 1985 struct GNUNET_SERVICE_Client *c,
1972 void *internal_cls) 1986 void *internal_cls)
1973{ 1987{
1974 struct ClientHandle *ch = internal_cls; 1988 struct ClientHandle *ch = internal_cls;
1975 struct LocalAddressList *lal; 1989 struct LocalAddressList *lal;
1976 1990
1977 GNUNET_CONTAINER_DLL_remove(ch_head, 1991 GNUNET_CONTAINER_DLL_remove (ch_head,
1978 ch_tail, 1992 ch_tail,
1979 ch); 1993 ch);
1980 for (unsigned int i = 0; i < ch->num_caddrs; i++) 1994 for (unsigned int i = 0; i < ch->num_caddrs; i++)
1995 {
1996 if (NULL != ch->caddrs[i].mh)
1981 { 1997 {
1982 if (NULL != ch->caddrs[i].mh) 1998 GNUNET_NAT_mini_map_stop (ch->caddrs[i].mh);
1983 { 1999 ch->caddrs[i].mh = NULL;
1984 GNUNET_NAT_mini_map_stop(ch->caddrs[i].mh);
1985 ch->caddrs[i].mh = NULL;
1986 }
1987 } 2000 }
1988 GNUNET_free_non_null(ch->caddrs); 2001 }
2002 GNUNET_free_non_null (ch->caddrs);
1989 while (NULL != (lal = ch->ext_addr_head)) 2003 while (NULL != (lal = ch->ext_addr_head))
1990 { 2004 {
1991 GNUNET_CONTAINER_DLL_remove(ch->ext_addr_head, 2005 GNUNET_CONTAINER_DLL_remove (ch->ext_addr_head,
1992 ch->ext_addr_tail, 2006 ch->ext_addr_tail,
1993 lal); 2007 lal);
1994 GNUNET_free(lal); 2008 GNUNET_free (lal);
1995 } 2009 }
1996 if (NULL != ch->ext_dns_task) 2010 if (NULL != ch->ext_dns_task)
1997 { 2011 {
1998 GNUNET_SCHEDULER_cancel(ch->ext_dns_task); 2012 GNUNET_SCHEDULER_cancel (ch->ext_dns_task);
1999 ch->ext_dns_task = NULL; 2013 ch->ext_dns_task = NULL;
2000 } 2014 }
2001 if (NULL != ch->external_monitor) 2015 if (NULL != ch->external_monitor)
2002 { 2016 {
2003 GN_external_ipv4_monitor_stop(ch->external_monitor); 2017 GN_external_ipv4_monitor_stop (ch->external_monitor);
2004 ch->external_monitor = NULL; 2018 ch->external_monitor = NULL;
2005 } 2019 }
2006 if (NULL != ch->ext_dns) 2020 if (NULL != ch->ext_dns)
2007 { 2021 {
2008 GNUNET_RESOLVER_request_cancel(ch->ext_dns); 2022 GNUNET_RESOLVER_request_cancel (ch->ext_dns);
2009 ch->ext_dns = NULL; 2023 ch->ext_dns = NULL;
2010 } 2024 }
2011 GNUNET_free_non_null(ch->hole_external); 2025 GNUNET_free_non_null (ch->hole_external);
2012 GNUNET_free_non_null(ch->section_name); 2026 GNUNET_free_non_null (ch->section_name);
2013 GNUNET_free(ch); 2027 GNUNET_free (ch);
2014} 2028}
2015 2029
2016 2030
@@ -2024,19 +2038,19 @@ GNUNET_SERVICE_MAIN
2024 &client_connect_cb, 2038 &client_connect_cb,
2025 &client_disconnect_cb, 2039 &client_disconnect_cb,
2026 NULL, 2040 NULL,
2027 GNUNET_MQ_hd_var_size(register, 2041 GNUNET_MQ_hd_var_size (register,
2028 GNUNET_MESSAGE_TYPE_NAT_REGISTER, 2042 GNUNET_MESSAGE_TYPE_NAT_REGISTER,
2029 struct GNUNET_NAT_RegisterMessage, 2043 struct GNUNET_NAT_RegisterMessage,
2030 NULL), 2044 NULL),
2031 GNUNET_MQ_hd_var_size(stun, 2045 GNUNET_MQ_hd_var_size (stun,
2032 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN, 2046 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN,
2033 struct GNUNET_NAT_HandleStunMessage, 2047 struct GNUNET_NAT_HandleStunMessage,
2034 NULL), 2048 NULL),
2035 GNUNET_MQ_hd_var_size(request_connection_reversal, 2049 GNUNET_MQ_hd_var_size (request_connection_reversal,
2036 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL, 2050 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL,
2037 struct GNUNET_NAT_RequestConnectionReversalMessage, 2051 struct GNUNET_NAT_RequestConnectionReversalMessage,
2038 NULL), 2052 NULL),
2039 GNUNET_MQ_handler_end()); 2053 GNUNET_MQ_handler_end ());
2040 2054
2041 2055
2042#if defined(LINUX) && defined(__GLIBC__) 2056#if defined(LINUX) && defined(__GLIBC__)
@@ -2046,11 +2060,11 @@ GNUNET_SERVICE_MAIN
2046 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 2060 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2047 */ 2061 */
2048void __attribute__ ((constructor)) 2062void __attribute__ ((constructor))
2049GNUNET_ARM_memory_init() 2063GNUNET_ARM_memory_init ()
2050{ 2064{
2051 mallopt(M_TRIM_THRESHOLD, 4 * 1024); 2065 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
2052 mallopt(M_TOP_PAD, 1 * 1024); 2066 mallopt (M_TOP_PAD, 1 * 1024);
2053 malloc_trim(0); 2067 malloc_trim (0);
2054} 2068}
2055#endif 2069#endif
2056 2070
diff --git a/src/nat/gnunet-service-nat_externalip.c b/src/nat/gnunet-service-nat_externalip.c
index c97f7f58b..0a251bdb2 100644
--- a/src/nat/gnunet-service-nat_externalip.c
+++ b/src/nat/gnunet-service-nat_externalip.c
@@ -54,25 +54,29 @@
54 * How long do we wait until we re-try running `external-ip` if the 54 * How long do we wait until we re-try running `external-ip` if the
55 * command failed to terminate nicely? 55 * command failed to terminate nicely?
56 */ 56 */
57#define EXTERN_IP_RETRY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15) 57#define EXTERN_IP_RETRY_TIMEOUT GNUNET_TIME_relative_multiply ( \
58 GNUNET_TIME_UNIT_MINUTES, 15)
58 59
59/** 60/**
60 * How long do we wait until we re-try running `external-ip` if the 61 * How long do we wait until we re-try running `external-ip` if the
61 * command failed (but terminated)? 62 * command failed (but terminated)?
62 */ 63 */
63#define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30) 64#define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply ( \
65 GNUNET_TIME_UNIT_MINUTES, 30)
64 66
65/** 67/**
66 * How long do we wait until we re-try running `external-ip` if the 68 * How long do we wait until we re-try running `external-ip` if the
67 * command succeeded? 69 * command succeeded?
68 */ 70 */
69#define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5) 71#define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply ( \
72 GNUNET_TIME_UNIT_MINUTES, 5)
70 73
71 74
72/** 75/**
73 * Handle to monitor for external IP changes. 76 * Handle to monitor for external IP changes.
74 */ 77 */
75struct GN_ExternalIPMonitor { 78struct GN_ExternalIPMonitor
79{
76 /** 80 /**
77 * Kept in DLL. 81 * Kept in DLL.
78 */ 82 */
@@ -131,15 +135,15 @@ static struct in_addr mini_external_ipv4;
131 * @param v4 the external address that changed 135 * @param v4 the external address that changed
132 */ 136 */
133static void 137static void
134notify_monitors_external_ipv4_change(int add, 138notify_monitors_external_ipv4_change (int add,
135 const struct in_addr *v4) 139 const struct in_addr *v4)
136{ 140{
137 for (struct GN_ExternalIPMonitor *mon = mon_head; 141 for (struct GN_ExternalIPMonitor *mon = mon_head;
138 NULL != mon; 142 NULL != mon;
139 mon = mon->next) 143 mon = mon->next)
140 mon->cb(mon->cb_cls, 144 mon->cb (mon->cb_cls,
141 v4, 145 v4,
142 add); 146 add);
143} 147}
144 148
145 149
@@ -150,7 +154,7 @@ notify_monitors_external_ipv4_change(int add,
150 * @param cls NULL 154 * @param cls NULL
151 */ 155 */
152static void 156static void
153run_external_ip(void *cls); 157run_external_ip (void *cls);
154 158
155 159
156/** 160/**
@@ -163,47 +167,47 @@ run_external_ip(void *cls);
163 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code 167 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
164 */ 168 */
165static void 169static void
166handle_external_ip(void *cls, 170handle_external_ip (void *cls,
167 const struct in_addr *addr, 171 const struct in_addr *addr,
168 enum GNUNET_NAT_StatusCode result) 172 enum GNUNET_NAT_StatusCode result)
169{ 173{
170 char buf[INET_ADDRSTRLEN]; 174 char buf[INET_ADDRSTRLEN];
171 175
172 probe_external_ip_op = NULL; 176 probe_external_ip_op = NULL;
173 GNUNET_SCHEDULER_cancel(probe_external_ip_task); 177 GNUNET_SCHEDULER_cancel (probe_external_ip_task);
174 probe_external_ip_task 178 probe_external_ip_task
175 = GNUNET_SCHEDULER_add_delayed((NULL == addr) 179 = GNUNET_SCHEDULER_add_delayed ((NULL == addr)
176 ? EXTERN_IP_RETRY_FAILURE 180 ? EXTERN_IP_RETRY_FAILURE
177 : EXTERN_IP_RETRY_SUCCESS, 181 : EXTERN_IP_RETRY_SUCCESS,
178 &run_external_ip, 182 &run_external_ip,
179 NULL); 183 NULL);
180 switch (result) 184 switch (result)
181 { 185 {
182 case GNUNET_NAT_ERROR_SUCCESS: 186 case GNUNET_NAT_ERROR_SUCCESS:
183 GNUNET_assert(NULL != addr); 187 GNUNET_assert (NULL != addr);
184 if (addr->s_addr == mini_external_ipv4.s_addr) 188 if (addr->s_addr == mini_external_ipv4.s_addr)
185 return; /* not change */ 189 return; /* not change */
186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Our external IP is now %s\n", 191 "Our external IP is now %s\n",
188 inet_ntop(AF_INET, 192 inet_ntop (AF_INET,
189 addr, 193 addr,
190 buf, 194 buf,
191 sizeof(buf))); 195 sizeof(buf)));
192 if (0 != mini_external_ipv4.s_addr) 196 if (0 != mini_external_ipv4.s_addr)
193 notify_monitors_external_ipv4_change(GNUNET_NO, 197 notify_monitors_external_ipv4_change (GNUNET_NO,
194 &mini_external_ipv4); 198 &mini_external_ipv4);
195 mini_external_ipv4 = *addr; 199 mini_external_ipv4 = *addr;
196 notify_monitors_external_ipv4_change(GNUNET_YES, 200 notify_monitors_external_ipv4_change (GNUNET_YES,
197 &mini_external_ipv4); 201 &mini_external_ipv4);
198 break; 202 break;
199 203
200 default: 204 default:
201 if (0 != mini_external_ipv4.s_addr) 205 if (0 != mini_external_ipv4.s_addr)
202 notify_monitors_external_ipv4_change(GNUNET_NO, 206 notify_monitors_external_ipv4_change (GNUNET_NO,
203 &mini_external_ipv4); 207 &mini_external_ipv4);
204 mini_external_ipv4.s_addr = 0; 208 mini_external_ipv4.s_addr = 0;
205 break; 209 break;
206 } 210 }
207} 211}
208 212
209 213
@@ -214,20 +218,20 @@ handle_external_ip(void *cls,
214 * @param cls NULL 218 * @param cls NULL
215 */ 219 */
216static void 220static void
217run_external_ip(void *cls) 221run_external_ip (void *cls)
218{ 222{
219 probe_external_ip_task 223 probe_external_ip_task
220 = GNUNET_SCHEDULER_add_delayed(EXTERN_IP_RETRY_TIMEOUT, 224 = GNUNET_SCHEDULER_add_delayed (EXTERN_IP_RETRY_TIMEOUT,
221 &run_external_ip, 225 &run_external_ip,
222 NULL); 226 NULL);
223 if (NULL != probe_external_ip_op) 227 if (NULL != probe_external_ip_op)
224 { 228 {
225 GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op); 229 GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op);
226 probe_external_ip_op = NULL; 230 probe_external_ip_op = NULL;
227 } 231 }
228 probe_external_ip_op 232 probe_external_ip_op
229 = GNUNET_NAT_mini_get_external_ipv4_(&handle_external_ip, 233 = GNUNET_NAT_mini_get_external_ipv4_ (&handle_external_ip,
230 NULL); 234 NULL);
231} 235}
232 236
233 237
@@ -238,32 +242,32 @@ run_external_ip(void *cls)
238 * @param have_nat #GNUNET_YES if we believe we are behind NAT 242 * @param have_nat #GNUNET_YES if we believe we are behind NAT
239 */ 243 */
240void 244void
241GN_nat_status_changed(int have_nat) 245GN_nat_status_changed (int have_nat)
242{ 246{
243 if (GNUNET_YES != enable_upnp) 247 if (GNUNET_YES != enable_upnp)
244 return; 248 return;
245 if ((GNUNET_YES == have_nat) && 249 if ((GNUNET_YES == have_nat) &&
246 (NULL == probe_external_ip_task) && 250 (NULL == probe_external_ip_task) &&
247 (NULL == probe_external_ip_op)) 251 (NULL == probe_external_ip_op))
252 {
253 probe_external_ip_task
254 = GNUNET_SCHEDULER_add_now (&run_external_ip,
255 NULL);
256 return;
257 }
258 if (GNUNET_NO == have_nat)
259 {
260 if (NULL != probe_external_ip_task)
248 { 261 {
249 probe_external_ip_task 262 GNUNET_SCHEDULER_cancel (probe_external_ip_task);
250 = GNUNET_SCHEDULER_add_now(&run_external_ip, 263 probe_external_ip_task = NULL;
251 NULL);
252 return;
253 } 264 }
254 if (GNUNET_NO == have_nat) 265 if (NULL != probe_external_ip_op)
255 { 266 {
256 if (NULL != probe_external_ip_task) 267 GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op);
257 { 268 probe_external_ip_op = NULL;
258 GNUNET_SCHEDULER_cancel(probe_external_ip_task);
259 probe_external_ip_task = NULL;
260 }
261 if (NULL != probe_external_ip_op)
262 {
263 GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op);
264 probe_external_ip_op = NULL;
265 }
266 } 269 }
270 }
267} 271}
268 272
269 273
@@ -275,21 +279,21 @@ GN_nat_status_changed(int have_nat)
275 * @return handle to cancel 279 * @return handle to cancel
276 */ 280 */
277struct GN_ExternalIPMonitor * 281struct GN_ExternalIPMonitor *
278GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb, 282GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb,
279 void *cb_cls) 283 void *cb_cls)
280{ 284{
281 struct GN_ExternalIPMonitor *mon; 285 struct GN_ExternalIPMonitor *mon;
282 286
283 mon = GNUNET_new(struct GN_ExternalIPMonitor); 287 mon = GNUNET_new (struct GN_ExternalIPMonitor);
284 mon->cb = cb; 288 mon->cb = cb;
285 mon->cb_cls = cb_cls; 289 mon->cb_cls = cb_cls;
286 GNUNET_CONTAINER_DLL_insert(mon_head, 290 GNUNET_CONTAINER_DLL_insert (mon_head,
287 mon_tail, 291 mon_tail,
288 mon); 292 mon);
289 if (0 != mini_external_ipv4.s_addr) 293 if (0 != mini_external_ipv4.s_addr)
290 cb(cb_cls, 294 cb (cb_cls,
291 &mini_external_ipv4, 295 &mini_external_ipv4,
292 GNUNET_YES); 296 GNUNET_YES);
293 return mon; 297 return mon;
294} 298}
295 299
@@ -300,12 +304,12 @@ GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb,
300 * @param mon monitor to call 304 * @param mon monitor to call
301 */ 305 */
302void 306void
303GN_external_ipv4_monitor_stop(struct GN_ExternalIPMonitor *mon) 307GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon)
304{ 308{
305 GNUNET_CONTAINER_DLL_remove(mon_head, 309 GNUNET_CONTAINER_DLL_remove (mon_head,
306 mon_tail, 310 mon_tail,
307 mon); 311 mon);
308 GNUNET_free(mon); 312 GNUNET_free (mon);
309} 313}
310 314
311/* end of gnunet-service-nat_externalip.c */ 315/* end of gnunet-service-nat_externalip.c */
diff --git a/src/nat/gnunet-service-nat_externalip.h b/src/nat/gnunet-service-nat_externalip.h
index 41ab49e2f..6b3467fab 100644
--- a/src/nat/gnunet-service-nat_externalip.h
+++ b/src/nat/gnunet-service-nat_externalip.h
@@ -46,7 +46,7 @@
46 * @param have_nat #GNUNET_YES if we believe we are behind NAT 46 * @param have_nat #GNUNET_YES if we believe we are behind NAT
47 */ 47 */
48void 48void
49GN_nat_status_changed(int have_nat); 49GN_nat_status_changed (int have_nat);
50 50
51 51
52/** 52/**
@@ -76,8 +76,8 @@ struct GN_ExternalIPMonitor;
76 * @return handle to cancel 76 * @return handle to cancel
77 */ 77 */
78struct GN_ExternalIPMonitor * 78struct GN_ExternalIPMonitor *
79GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb, 79GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb,
80 void *cb_cls); 80 void *cb_cls);
81 81
82 82
83/** 83/**
@@ -86,7 +86,7 @@ GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb,
86 * @param mon monitor to call 86 * @param mon monitor to call
87 */ 87 */
88void 88void
89GN_external_ipv4_monitor_stop(struct GN_ExternalIPMonitor *mon); 89GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon);
90 90
91 91
92#endif 92#endif
diff --git a/src/nat/gnunet-service-nat_helper.c b/src/nat/gnunet-service-nat_helper.c
index 42612751d..58d7b4c61 100644
--- a/src/nat/gnunet-service-nat_helper.c
+++ b/src/nat/gnunet-service-nat_helper.c
@@ -32,7 +32,8 @@
32/** 32/**
33 * Information we keep per NAT helper process. 33 * Information we keep per NAT helper process.
34 */ 34 */
35struct HelperContext { 35struct HelperContext
36{
36 /** 37 /**
37 * IP address we pass to the NAT helper. 38 * IP address we pass to the NAT helper.
38 */ 39 */
@@ -87,7 +88,7 @@ struct HelperContext {
87 * @param cls a `struct HelperContext` 88 * @param cls a `struct HelperContext`
88 */ 89 */
89static void 90static void
90restart_nat_server(void *cls); 91restart_nat_server (void *cls);
91 92
92 93
93/** 94/**
@@ -96,13 +97,13 @@ restart_nat_server(void *cls);
96 * @param h context of the helper 97 * @param h context of the helper
97 */ 98 */
98static void 99static void
99try_again(struct HelperContext *h) 100try_again (struct HelperContext *h)
100{ 101{
101 GNUNET_assert(NULL == h->server_read_task); 102 GNUNET_assert (NULL == h->server_read_task);
102 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF(h->server_retry_delay); 103 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
103 h->server_read_task = GNUNET_SCHEDULER_add_delayed(h->server_retry_delay, 104 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
104 &restart_nat_server, 105 &restart_nat_server,
105 h); 106 h);
106} 107}
107 108
108 109
@@ -114,7 +115,7 @@ try_again(struct HelperContext *h)
114 * @param cls the `struct HelperContext` 115 * @param cls the `struct HelperContext`
115 */ 116 */
116static void 117static void
117nat_server_read(void *cls) 118nat_server_read (void *cls)
118{ 119{
119 struct HelperContext *h = cls; 120 struct HelperContext *h = cls;
120 char mybuf[40]; 121 char mybuf[40];
@@ -124,73 +125,73 @@ nat_server_read(void *cls)
124 struct sockaddr_in sin_addr; 125 struct sockaddr_in sin_addr;
125 126
126 h->server_read_task = NULL; 127 h->server_read_task = NULL;
127 memset(mybuf, 0, sizeof(mybuf)); 128 memset (mybuf, 0, sizeof(mybuf));
128 bytes = 129 bytes =
129 GNUNET_DISK_file_read(h->server_stdout_handle, mybuf, sizeof(mybuf)); 130 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof(mybuf));
130 if (bytes < 1) 131 if (bytes < 1)
131 { 132 {
132 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133 "Finished reading from server stdout with code: %d\n", 134 "Finished reading from server stdout with code: %d\n",
134 (int)bytes); 135 (int) bytes);
135 if (0 != GNUNET_OS_process_kill(h->server_proc, GNUNET_TERM_SIG)) 136 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
136 GNUNET_log_from_strerror(GNUNET_ERROR_TYPE_WARNING, "nat", "kill"); 137 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
137 GNUNET_OS_process_wait(h->server_proc); 138 GNUNET_OS_process_wait (h->server_proc);
138 GNUNET_OS_process_destroy(h->server_proc); 139 GNUNET_OS_process_destroy (h->server_proc);
139 h->server_proc = NULL; 140 h->server_proc = NULL;
140 GNUNET_DISK_pipe_close(h->server_stdout); 141 GNUNET_DISK_pipe_close (h->server_stdout);
141 h->server_stdout = NULL; 142 h->server_stdout = NULL;
142 h->server_stdout_handle = NULL; 143 h->server_stdout_handle = NULL;
143 try_again(h); 144 try_again (h);
144 return; 145 return;
145 } 146 }
146 147
147 port_start = NULL; 148 port_start = NULL;
148 for (size_t i = 0; i < sizeof(mybuf); i++) 149 for (size_t i = 0; i < sizeof(mybuf); i++)
150 {
151 if (mybuf[i] == '\n')
149 { 152 {
150 if (mybuf[i] == '\n') 153 mybuf[i] = '\0';
151 { 154 break;
152 mybuf[i] = '\0';
153 break;
154 }
155 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
156 {
157 mybuf[i] = '\0';
158 port_start = &mybuf[i + 1];
159 }
160 } 155 }
156 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
157 {
158 mybuf[i] = '\0';
159 port_start = &mybuf[i + 1];
160 }
161 }
161 162
162 /* construct socket address of sender */ 163 /* construct socket address of sender */
163 memset(&sin_addr, 0, sizeof(sin_addr)); 164 memset (&sin_addr, 0, sizeof(sin_addr));
164 sin_addr.sin_family = AF_INET; 165 sin_addr.sin_family = AF_INET;
165#if HAVE_SOCKADDR_IN_SIN_LEN 166#if HAVE_SOCKADDR_IN_SIN_LEN
166 sin_addr.sin_len = sizeof(sin_addr); 167 sin_addr.sin_len = sizeof(sin_addr);
167#endif 168#endif
168 if ((NULL == port_start) || (1 != sscanf(port_start, "%d", &port)) || 169 if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) ||
169 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr))) 170 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
170 { 171 {
171 /* should we restart gnunet-helper-nat-server? */ 172 /* should we restart gnunet-helper-nat-server? */
172 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 173 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
173 _( 174 _ (
174 "gnunet-helper-nat-server generated malformed address `%s'\n"), 175 "gnunet-helper-nat-server generated malformed address `%s'\n"),
175 mybuf); 176 mybuf);
176 h->server_read_task = 177 h->server_read_task =
177 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 178 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
178 h->server_stdout_handle, 179 h->server_stdout_handle,
179 &nat_server_read, 180 &nat_server_read,
180 h); 181 h);
181 return; 182 return;
182 } 183 }
183 sin_addr.sin_port = htons((uint16_t)port); 184 sin_addr.sin_port = htons ((uint16_t) port);
184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "gnunet-helper-nat-server read: %s:%d\n", 186 "gnunet-helper-nat-server read: %s:%d\n",
186 mybuf, 187 mybuf,
187 port); 188 port);
188 h->cb(h->cb_cls, &sin_addr); 189 h->cb (h->cb_cls, &sin_addr);
189 h->server_read_task = 190 h->server_read_task =
190 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 191 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
191 h->server_stdout_handle, 192 h->server_stdout_handle,
192 &nat_server_read, 193 &nat_server_read,
193 h); 194 h);
194} 195}
195 196
196 197
@@ -201,67 +202,67 @@ nat_server_read(void *cls)
201 * @param cls a `struct HelperContext` 202 * @param cls a `struct HelperContext`
202 */ 203 */
203static void 204static void
204restart_nat_server(void *cls) 205restart_nat_server (void *cls)
205{ 206{
206 struct HelperContext *h = cls; 207 struct HelperContext *h = cls;
207 char *binary; 208 char *binary;
208 char ia[INET_ADDRSTRLEN]; 209 char ia[INET_ADDRSTRLEN];
209 210
210 h->server_read_task = NULL; 211 h->server_read_task = NULL;
211 GNUNET_assert(NULL != 212 GNUNET_assert (NULL !=
212 inet_ntop(AF_INET, &h->internal_address, ia, sizeof(ia))); 213 inet_ntop (AF_INET, &h->internal_address, ia, sizeof(ia)));
213 /* Start the server process */ 214 /* Start the server process */
214 binary = GNUNET_OS_get_suid_binary_path(h->cfg, "gnunet-helper-nat-server"); 215 binary = GNUNET_OS_get_suid_binary_path (h->cfg, "gnunet-helper-nat-server");
215 if (GNUNET_YES != GNUNET_OS_check_helper_binary(binary, GNUNET_YES, ia)) 216 if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, ia))
216 { 217 {
217 /* move instantly to max delay, as this is unlikely to be fixed */ 218 /* move instantly to max delay, as this is unlikely to be fixed */
218 h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD; 219 h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
219 GNUNET_free(binary); 220 GNUNET_free (binary);
220 try_again(h); 221 try_again (h);
221 return; 222 return;
222 } 223 }
223 h->server_stdout = 224 h->server_stdout =
224 GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 225 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
225 if (NULL == h->server_stdout) 226 if (NULL == h->server_stdout)
226 { 227 {
227 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "pipe"); 228 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
228 GNUNET_free(binary); 229 GNUNET_free (binary);
229 try_again(h); 230 try_again (h);
230 return; 231 return;
231 } 232 }
232 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Starting `%s' at `%s'\n", 234 "Starting `%s' at `%s'\n",
234 "gnunet-helper-nat-server", 235 "gnunet-helper-nat-server",
235 ia); 236 ia);
236 h->server_proc = GNUNET_OS_start_process(GNUNET_NO, 237 h->server_proc = GNUNET_OS_start_process (GNUNET_NO,
237 0, 238 0,
238 NULL, 239 NULL,
239 h->server_stdout, 240 h->server_stdout,
240 NULL, 241 NULL,
241 binary, 242 binary,
242 "gnunet-helper-nat-server", 243 "gnunet-helper-nat-server",
243 ia, 244 ia,
244 NULL); 245 NULL);
245 GNUNET_free(binary); 246 GNUNET_free (binary);
246 if (NULL == h->server_proc) 247 if (NULL == h->server_proc)
247 { 248 {
248 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
249 _("Failed to start %s\n"), 250 _ ("Failed to start %s\n"),
250 "gnunet-helper-nat-server"); 251 "gnunet-helper-nat-server");
251 GNUNET_DISK_pipe_close(h->server_stdout); 252 GNUNET_DISK_pipe_close (h->server_stdout);
252 h->server_stdout = NULL; 253 h->server_stdout = NULL;
253 try_again(h); 254 try_again (h);
254 return; 255 return;
255 } 256 }
256 /* Close the write end of the read pipe */ 257 /* Close the write end of the read pipe */
257 GNUNET_DISK_pipe_close_end(h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); 258 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
258 h->server_stdout_handle = 259 h->server_stdout_handle =
259 GNUNET_DISK_pipe_handle(h->server_stdout, GNUNET_DISK_PIPE_END_READ); 260 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
260 h->server_read_task = 261 h->server_read_task =
261 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 262 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
262 h->server_stdout_handle, 263 h->server_stdout_handle,
263 &nat_server_read, 264 &nat_server_read,
264 h); 265 h);
265} 266}
266 267
267 268
@@ -276,24 +277,24 @@ restart_nat_server(void *cls)
276 * @return NULL on error 277 * @return NULL on error
277 */ 278 */
278struct HelperContext * 279struct HelperContext *
279GN_start_gnunet_nat_server_(const struct in_addr *internal_address, 280GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
280 GN_ReversalCallback cb, 281 GN_ReversalCallback cb,
281 void *cb_cls, 282 void *cb_cls,
282 const struct GNUNET_CONFIGURATION_Handle *cfg) 283 const struct GNUNET_CONFIGURATION_Handle *cfg)
283{ 284{
284 struct HelperContext *h; 285 struct HelperContext *h;
285 286
286 h = GNUNET_new(struct HelperContext); 287 h = GNUNET_new (struct HelperContext);
287 h->cb = cb; 288 h->cb = cb;
288 h->cb_cls = cb_cls; 289 h->cb_cls = cb_cls;
289 h->internal_address = *internal_address; 290 h->internal_address = *internal_address;
290 h->cfg = cfg; 291 h->cfg = cfg;
291 restart_nat_server(h); 292 restart_nat_server (h);
292 if (NULL == h->server_stdout) 293 if (NULL == h->server_stdout)
293 { 294 {
294 GN_stop_gnunet_nat_server_(h); 295 GN_stop_gnunet_nat_server_ (h);
295 return NULL; 296 return NULL;
296 } 297 }
297 return h; 298 return h;
298} 299}
299 300
@@ -305,31 +306,31 @@ GN_start_gnunet_nat_server_(const struct in_addr *internal_address,
305 * @param h helper context to stop 306 * @param h helper context to stop
306 */ 307 */
307void 308void
308GN_stop_gnunet_nat_server_(struct HelperContext *h) 309GN_stop_gnunet_nat_server_ (struct HelperContext *h)
309{ 310{
310 if (NULL != h->server_read_task) 311 if (NULL != h->server_read_task)
311 { 312 {
312 GNUNET_SCHEDULER_cancel(h->server_read_task); 313 GNUNET_SCHEDULER_cancel (h->server_read_task);
313 h->server_read_task = NULL; 314 h->server_read_task = NULL;
314 } 315 }
315 if (NULL != h->server_proc) 316 if (NULL != h->server_proc)
316 { 317 {
317 if (0 != GNUNET_OS_process_kill(h->server_proc, GNUNET_TERM_SIG)) 318 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
318 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 319 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
319 GNUNET_OS_process_wait(h->server_proc); 320 GNUNET_OS_process_wait (h->server_proc);
320 GNUNET_OS_process_destroy(h->server_proc); 321 GNUNET_OS_process_destroy (h->server_proc);
321 h->server_proc = NULL; 322 h->server_proc = NULL;
322 GNUNET_DISK_pipe_close(h->server_stdout); 323 GNUNET_DISK_pipe_close (h->server_stdout);
323 h->server_stdout = NULL; 324 h->server_stdout = NULL;
324 h->server_stdout_handle = NULL; 325 h->server_stdout_handle = NULL;
325 } 326 }
326 if (NULL != h->server_stdout) 327 if (NULL != h->server_stdout)
327 { 328 {
328 GNUNET_DISK_pipe_close(h->server_stdout); 329 GNUNET_DISK_pipe_close (h->server_stdout);
329 h->server_stdout = NULL; 330 h->server_stdout = NULL;
330 h->server_stdout_handle = NULL; 331 h->server_stdout_handle = NULL;
331 } 332 }
332 GNUNET_free(h); 333 GNUNET_free (h);
333} 334}
334 335
335 336
@@ -346,10 +347,10 @@ GN_stop_gnunet_nat_server_(struct HelperContext *h)
346 * #GNUNET_OK otherwise 347 * #GNUNET_OK otherwise
347 */ 348 */
348int 349int
349GN_request_connection_reversal(const struct in_addr *internal_address, 350GN_request_connection_reversal (const struct in_addr *internal_address,
350 uint16_t internal_port, 351 uint16_t internal_port,
351 const struct in_addr *remote_v4, 352 const struct in_addr *remote_v4,
352 const struct GNUNET_CONFIGURATION_Handle *cfg) 353 const struct GNUNET_CONFIGURATION_Handle *cfg)
353{ 354{
354 char intv4[INET_ADDRSTRLEN]; 355 char intv4[INET_ADDRSTRLEN];
355 char remv4[INET_ADDRSTRLEN]; 356 char remv4[INET_ADDRSTRLEN];
@@ -357,44 +358,44 @@ GN_request_connection_reversal(const struct in_addr *internal_address,
357 struct GNUNET_OS_Process *proc; 358 struct GNUNET_OS_Process *proc;
358 char *binary; 359 char *binary;
359 360
360 if (NULL == inet_ntop(AF_INET, internal_address, intv4, INET_ADDRSTRLEN)) 361 if (NULL == inet_ntop (AF_INET, internal_address, intv4, INET_ADDRSTRLEN))
361 { 362 {
362 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); 363 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
363 return GNUNET_SYSERR; 364 return GNUNET_SYSERR;
364 } 365 }
365 if (NULL == inet_ntop(AF_INET, remote_v4, remv4, INET_ADDRSTRLEN)) 366 if (NULL == inet_ntop (AF_INET, remote_v4, remv4, INET_ADDRSTRLEN))
366 { 367 {
367 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); 368 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
368 return GNUNET_SYSERR; 369 return GNUNET_SYSERR;
369 } 370 }
370 GNUNET_snprintf(port_as_string, 371 GNUNET_snprintf (port_as_string,
371 sizeof(port_as_string), 372 sizeof(port_as_string),
372 "%d", 373 "%d",
373 internal_port); 374 internal_port);
374 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Running gnunet-helper-nat-client %s %s %u\n", 376 "Running gnunet-helper-nat-client %s %s %u\n",
376 intv4, 377 intv4,
377 remv4, 378 remv4,
378 internal_port); 379 internal_port);
379 binary = GNUNET_OS_get_suid_binary_path(cfg, "gnunet-helper-nat-client"); 380 binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-nat-client");
380 proc = GNUNET_OS_start_process(GNUNET_NO, 381 proc = GNUNET_OS_start_process (GNUNET_NO,
381 0, 382 0,
382 NULL, 383 NULL,
383 NULL, 384 NULL,
384 NULL, 385 NULL,
385 binary, 386 binary,
386 "gnunet-helper-nat-client", 387 "gnunet-helper-nat-client",
387 intv4, 388 intv4,
388 remv4, 389 remv4,
389 port_as_string, 390 port_as_string,
390 NULL); 391 NULL);
391 GNUNET_free(binary); 392 GNUNET_free (binary);
392 if (NULL == proc) 393 if (NULL == proc)
393 return GNUNET_SYSERR; 394 return GNUNET_SYSERR;
394 /* we know that the gnunet-helper-nat-client will terminate virtually 395 /* we know that the gnunet-helper-nat-client will terminate virtually
395 * instantly */ 396 * instantly */
396 GNUNET_OS_process_wait(proc); 397 GNUNET_OS_process_wait (proc);
397 GNUNET_OS_process_destroy(proc); 398 GNUNET_OS_process_destroy (proc);
398 return GNUNET_OK; 399 return GNUNET_OK;
399} 400}
400 401
diff --git a/src/nat/gnunet-service-nat_helper.h b/src/nat/gnunet-service-nat_helper.h
index e655149bc..dc6b9ae61 100644
--- a/src/nat/gnunet-service-nat_helper.h
+++ b/src/nat/gnunet-service-nat_helper.h
@@ -57,10 +57,10 @@ typedef void
57 * @return NULL on error 57 * @return NULL on error
58 */ 58 */
59struct HelperContext * 59struct HelperContext *
60GN_start_gnunet_nat_server_(const struct in_addr *internal_address, 60GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
61 GN_ReversalCallback cb, 61 GN_ReversalCallback cb,
62 void *cb_cls, 62 void *cb_cls,
63 const struct GNUNET_CONFIGURATION_Handle *cfg); 63 const struct GNUNET_CONFIGURATION_Handle *cfg);
64 64
65 65
66/** 66/**
@@ -70,7 +70,7 @@ GN_start_gnunet_nat_server_(const struct in_addr *internal_address,
70 * @param h helper context to stop 70 * @param h helper context to stop
71 */ 71 */
72void 72void
73GN_stop_gnunet_nat_server_(struct HelperContext *h); 73GN_stop_gnunet_nat_server_ (struct HelperContext *h);
74 74
75 75
76/** 76/**
@@ -86,10 +86,10 @@ GN_stop_gnunet_nat_server_(struct HelperContext *h);
86 * #GNUNET_OK otherwise 86 * #GNUNET_OK otherwise
87 */ 87 */
88int 88int
89GN_request_connection_reversal(const struct in_addr *internal_address, 89GN_request_connection_reversal (const struct in_addr *internal_address,
90 uint16_t internal_port, 90 uint16_t internal_port,
91 const struct in_addr *remote_v4, 91 const struct in_addr *remote_v4,
92 const struct GNUNET_CONFIGURATION_Handle *cfg); 92 const struct GNUNET_CONFIGURATION_Handle *cfg);
93 93
94 94
95/* end of gnunet-service-nat_helper.h */ 95/* end of gnunet-service-nat_helper.h */
diff --git a/src/nat/gnunet-service-nat_mini.c b/src/nat/gnunet-service-nat_mini.c
index 94534e81c..1382010bd 100644
--- a/src/nat/gnunet-service-nat_mini.c
+++ b/src/nat/gnunet-service-nat_mini.c
@@ -29,24 +29,24 @@
29#include "gnunet-service-nat_mini.h" 29#include "gnunet-service-nat_mini.h"
30#include "nat.h" 30#include "nat.h"
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
33 33
34/** 34/**
35 * How long do we give upnpc to create a mapping? 35 * How long do we give upnpc to create a mapping?
36 */ 36 */
37#define MAP_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) 37#define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
38 38
39/** 39/**
40 * How long do we give upnpc to remove a mapping? 40 * How long do we give upnpc to remove a mapping?
41 */ 41 */
42#define UNMAP_TIMEOUT \ 42#define UNMAP_TIMEOUT \
43 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1) 43 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 44
45/** 45/**
46 * How often do we check for changes in the mapping? 46 * How often do we check for changes in the mapping?
47 */ 47 */
48#define MAP_REFRESH_FREQ \ 48#define MAP_REFRESH_FREQ \
49 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5) 49 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
50 50
51 51
52/* ************************* external-ip calling ************************ */ 52/* ************************* external-ip calling ************************ */
@@ -54,7 +54,8 @@
54/** 54/**
55 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation. 55 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation.
56 */ 56 */
57struct GNUNET_NAT_ExternalHandle { 57struct GNUNET_NAT_ExternalHandle
58{
58 /** 59 /**
59 * Function to call with the result. 60 * Function to call with the result.
60 */ 61 */
@@ -109,43 +110,43 @@ struct GNUNET_NAT_ExternalHandle {
109 * @param cls the `struct GNUNET_NAT_ExternalHandle` 110 * @param cls the `struct GNUNET_NAT_ExternalHandle`
110 */ 111 */
111static void 112static void
112read_external_ipv4(void *cls) 113read_external_ipv4 (void *cls)
113{ 114{
114 struct GNUNET_NAT_ExternalHandle *eh = cls; 115 struct GNUNET_NAT_ExternalHandle *eh = cls;
115 ssize_t ret; 116 ssize_t ret;
116 struct in_addr addr; 117 struct in_addr addr;
117 118
118 eh->task = NULL; 119 eh->task = NULL;
119 ret = GNUNET_DISK_file_read(eh->r, 120 ret = GNUNET_DISK_file_read (eh->r,
120 &eh->buf[eh->off], 121 &eh->buf[eh->off],
121 sizeof(eh->buf) - eh->off); 122 sizeof(eh->buf) - eh->off);
122 if (ret > 0) 123 if (ret > 0)
123 { 124 {
124 /* try to read more */ 125 /* try to read more */
125 eh->off += ret; 126 eh->off += ret;
126 eh->task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 127 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
127 eh->r, 128 eh->r,
128 &read_external_ipv4, 129 &read_external_ipv4,
129 eh); 130 eh);
130 return; 131 return;
131 } 132 }
132 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID; 133 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID;
133 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n')) 134 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n'))
135 {
136 eh->buf[eh->off - 1] = '\0';
137 if (1 == inet_pton (AF_INET, eh->buf, &addr))
134 { 138 {
135 eh->buf[eh->off - 1] = '\0'; 139 if (0 == addr.s_addr)
136 if (1 == inet_pton(AF_INET, eh->buf, &addr)) 140 eh->ret =
137 { 141 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */
138 if (0 == addr.s_addr) 142 else
139 eh->ret = 143 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
140 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */
141 else
142 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
143 }
144 } 144 }
145 eh->cb(eh->cb_cls, 145 }
146 (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL, 146 eh->cb (eh->cb_cls,
147 eh->ret); 147 (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL,
148 GNUNET_NAT_mini_get_external_ipv4_cancel_(eh); 148 eh->ret);
149 GNUNET_NAT_mini_get_external_ipv4_cancel_ (eh);
149} 150}
150 151
151 152
@@ -155,13 +156,13 @@ read_external_ipv4(void *cls)
155 * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed) 156 * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed)
156 */ 157 */
157static void 158static void
158signal_external_ip_error(void *cls) 159signal_external_ip_error (void *cls)
159{ 160{
160 struct GNUNET_NAT_ExternalHandle *eh = cls; 161 struct GNUNET_NAT_ExternalHandle *eh = cls;
161 162
162 eh->task = NULL; 163 eh->task = NULL;
163 eh->cb(eh->cb_cls, NULL, eh->ret); 164 eh->cb (eh->cb_cls, NULL, eh->ret);
164 GNUNET_free(eh); 165 GNUNET_free (eh);
165} 166}
166 167
167 168
@@ -173,52 +174,52 @@ signal_external_ip_error(void *cls)
173 * @return handle for cancellation (can only be used until @a cb is called), never NULL 174 * @return handle for cancellation (can only be used until @a cb is called), never NULL
174 */ 175 */
175struct GNUNET_NAT_ExternalHandle * 176struct GNUNET_NAT_ExternalHandle *
176GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb, void *cb_cls) 177GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls)
177{ 178{
178 struct GNUNET_NAT_ExternalHandle *eh; 179 struct GNUNET_NAT_ExternalHandle *eh;
179 180
180 eh = GNUNET_new(struct GNUNET_NAT_ExternalHandle); 181 eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle);
181 eh->cb = cb; 182 eh->cb = cb;
182 eh->cb_cls = cb_cls; 183 eh->cb_cls = cb_cls;
183 eh->ret = GNUNET_NAT_ERROR_SUCCESS; 184 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
184 if (GNUNET_SYSERR == 185 if (GNUNET_SYSERR ==
185 GNUNET_OS_check_helper_binary("external-ip", GNUNET_NO, NULL)) 186 GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL))
186 { 187 {
187 LOG(GNUNET_ERROR_TYPE_INFO, _("`external-ip' command not found\n")); 188 LOG (GNUNET_ERROR_TYPE_INFO, _ ("`external-ip' command not found\n"));
188 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; 189 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND;
189 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh); 190 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
190 return eh; 191 return eh;
191 } 192 }
192 LOG(GNUNET_ERROR_TYPE_DEBUG, 193 LOG (GNUNET_ERROR_TYPE_DEBUG,
193 "Running `external-ip' to determine our external IP\n"); 194 "Running `external-ip' to determine our external IP\n");
194 eh->opipe = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 195 eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
195 if (NULL == eh->opipe) 196 if (NULL == eh->opipe)
196 { 197 {
197 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; 198 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
198 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh); 199 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
199 return eh; 200 return eh;
200 } 201 }
201 eh->eip = GNUNET_OS_start_process(GNUNET_NO, 202 eh->eip = GNUNET_OS_start_process (GNUNET_NO,
202 0, 203 0,
203 NULL, 204 NULL,
204 eh->opipe, 205 eh->opipe,
205 NULL, 206 NULL,
206 "external-ip", 207 "external-ip",
207 "external-ip", 208 "external-ip",
208 NULL); 209 NULL);
209 if (NULL == eh->eip) 210 if (NULL == eh->eip)
210 { 211 {
211 GNUNET_DISK_pipe_close(eh->opipe); 212 GNUNET_DISK_pipe_close (eh->opipe);
212 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; 213 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED;
213 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh); 214 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
214 return eh; 215 return eh;
215 } 216 }
216 GNUNET_DISK_pipe_close_end(eh->opipe, GNUNET_DISK_PIPE_END_WRITE); 217 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
217 eh->r = GNUNET_DISK_pipe_handle(eh->opipe, GNUNET_DISK_PIPE_END_READ); 218 eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ);
218 eh->task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 219 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
219 eh->r, 220 eh->r,
220 &read_external_ipv4, 221 &read_external_ipv4,
221 eh); 222 eh);
222 return eh; 223 return eh;
223} 224}
224 225
@@ -229,25 +230,25 @@ GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb, void *cb_cls)
229 * @param eh operation to cancel 230 * @param eh operation to cancel
230 */ 231 */
231void 232void
232GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh) 233GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh)
233{ 234{
234 if (NULL != eh->eip) 235 if (NULL != eh->eip)
235 { 236 {
236 (void)GNUNET_OS_process_kill(eh->eip, SIGKILL); 237 (void) GNUNET_OS_process_kill (eh->eip, SIGKILL);
237 GNUNET_break(GNUNET_OK == GNUNET_OS_process_wait(eh->eip)); 238 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (eh->eip));
238 GNUNET_OS_process_destroy(eh->eip); 239 GNUNET_OS_process_destroy (eh->eip);
239 } 240 }
240 if (NULL != eh->opipe) 241 if (NULL != eh->opipe)
241 { 242 {
242 GNUNET_DISK_pipe_close(eh->opipe); 243 GNUNET_DISK_pipe_close (eh->opipe);
243 eh->opipe = NULL; 244 eh->opipe = NULL;
244 } 245 }
245 if (NULL != eh->task) 246 if (NULL != eh->task)
246 { 247 {
247 GNUNET_SCHEDULER_cancel(eh->task); 248 GNUNET_SCHEDULER_cancel (eh->task);
248 eh->task = NULL; 249 eh->task = NULL;
249 } 250 }
250 GNUNET_free(eh); 251 GNUNET_free (eh);
251} 252}
252 253
253 254
@@ -257,7 +258,8 @@ GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh)
257/** 258/**
258 * Handle to a mapping created with upnpc. 259 * Handle to a mapping created with upnpc.
259 */ 260 */
260struct GNUNET_NAT_MiniHandle { 261struct GNUNET_NAT_MiniHandle
262{
261 /** 263 /**
262 * Function to call on mapping changes. 264 * Function to call on mapping changes.
263 */ 265 */
@@ -322,7 +324,7 @@ struct GNUNET_NAT_MiniHandle {
322 * @param cls the `struct GNUNET_NAT_MiniHandle` 324 * @param cls the `struct GNUNET_NAT_MiniHandle`
323 */ 325 */
324static void 326static void
325do_refresh(void *cls); 327do_refresh (void *cls);
326 328
327 329
328/** 330/**
@@ -332,7 +334,7 @@ do_refresh(void *cls);
332 * @param line line of output, NULL at the end 334 * @param line line of output, NULL at the end
333 */ 335 */
334static void 336static void
335process_map_output(void *cls, const char *line); 337process_map_output (void *cls, const char *line);
336 338
337 339
338/** 340/**
@@ -341,29 +343,29 @@ process_map_output(void *cls, const char *line);
341 * @param mini our handle 343 * @param mini our handle
342 */ 344 */
343static void 345static void
344run_upnpc_r(struct GNUNET_NAT_MiniHandle *mini) 346run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
345{ 347{
346 char pstr[6]; 348 char pstr[6];
347 349
348 GNUNET_snprintf(pstr, sizeof(pstr), "%u", (unsigned int)mini->port); 350 GNUNET_snprintf (pstr, sizeof(pstr), "%u", (unsigned int) mini->port);
349 mini->map_cmd = GNUNET_OS_command_run(&process_map_output, 351 mini->map_cmd = GNUNET_OS_command_run (&process_map_output,
350 mini, 352 mini,
351 MAP_TIMEOUT, 353 MAP_TIMEOUT,
352 "upnpc", 354 "upnpc",
353 "upnpc", 355 "upnpc",
354 "-r", 356 "-r",
355 pstr, 357 pstr,
356 mini->is_tcp ? "tcp" : "udp", 358 mini->is_tcp ? "tcp" : "udp",
357 NULL); 359 NULL);
358 if (NULL == mini->map_cmd) 360 if (NULL == mini->map_cmd)
359 { 361 {
360 mini->ac(mini->ac_cls, 362 mini->ac (mini->ac_cls,
361 GNUNET_SYSERR, 363 GNUNET_SYSERR,
362 NULL, 364 NULL,
363 0, 365 0,
364 GNUNET_NAT_ERROR_UPNPC_FAILED); 366 GNUNET_NAT_ERROR_UPNPC_FAILED);
365 return; 367 return;
366 } 368 }
367} 369}
368 370
369 371
@@ -375,7 +377,7 @@ run_upnpc_r(struct GNUNET_NAT_MiniHandle *mini)
375 * @param line line of output, NULL at the end 377 * @param line line of output, NULL at the end
376 */ 378 */
377static void 379static void
378process_refresh_output(void *cls, const char *line) 380process_refresh_output (void *cls, const char *line)
379{ 381{
380 struct GNUNET_NAT_MiniHandle *mini = cls; 382 struct GNUNET_NAT_MiniHandle *mini = cls;
381 char pstr[9]; 383 char pstr[9];
@@ -384,53 +386,53 @@ process_refresh_output(void *cls, const char *line)
384 struct in_addr exip; 386 struct in_addr exip;
385 387
386 if (NULL == line) 388 if (NULL == line)
389 {
390 GNUNET_OS_command_stop (mini->refresh_cmd);
391 mini->refresh_cmd = NULL;
392 if (GNUNET_NO == mini->found)
387 { 393 {
388 GNUNET_OS_command_stop(mini->refresh_cmd); 394 /* mapping disappeared, try to re-create */
389 mini->refresh_cmd = NULL; 395 if (GNUNET_YES == mini->did_map)
390 if (GNUNET_NO == mini->found) 396 {
391 { 397 mini->ac (mini->ac_cls,
392 /* mapping disappeared, try to re-create */ 398 GNUNET_NO,
393 if (GNUNET_YES == mini->did_map) 399 (const struct sockaddr *) &mini->current_addr,
394 { 400 sizeof(mini->current_addr),
395 mini->ac(mini->ac_cls, 401 GNUNET_NAT_ERROR_SUCCESS);
396 GNUNET_NO, 402 mini->did_map = GNUNET_NO;
397 (const struct sockaddr *)&mini->current_addr, 403 }
398 sizeof(mini->current_addr), 404 run_upnpc_r (mini);
399 GNUNET_NAT_ERROR_SUCCESS);
400 mini->did_map = GNUNET_NO;
401 }
402 run_upnpc_r(mini);
403 }
404 return;
405 } 405 }
406 if (!mini->did_map) 406 return;
407 }
408 if (! mini->did_map)
407 return; /* never mapped, won't find our mapping anyway */ 409 return; /* never mapped, won't find our mapping anyway */
408 410
409 /* we're looking for output of the form: 411 /* we're looking for output of the form:
410 * "ExternalIPAddress = 12.134.41.124" */ 412 * "ExternalIPAddress = 12.134.41.124" */
411 413
412 s = strstr(line, "ExternalIPAddress = "); 414 s = strstr (line, "ExternalIPAddress = ");
413 if (NULL != s) 415 if (NULL != s)
414 { 416 {
415 s += strlen("ExternalIPAddress = "); 417 s += strlen ("ExternalIPAddress = ");
416 if (1 != inet_pton(AF_INET, s, &exip)) 418 if (1 != inet_pton (AF_INET, s, &exip))
417 return; /* skip */ 419 return; /* skip */
418 if (exip.s_addr == mini->current_addr.sin_addr.s_addr) 420 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
419 return; /* no change */ 421 return; /* no change */
420 /* update mapping */ 422 /* update mapping */
421 mini->ac(mini->ac_cls, 423 mini->ac (mini->ac_cls,
422 GNUNET_NO, 424 GNUNET_NO,
423 (const struct sockaddr *)&mini->current_addr, 425 (const struct sockaddr *) &mini->current_addr,
424 sizeof(mini->current_addr), 426 sizeof(mini->current_addr),
425 GNUNET_NAT_ERROR_SUCCESS); 427 GNUNET_NAT_ERROR_SUCCESS);
426 mini->current_addr.sin_addr = exip; 428 mini->current_addr.sin_addr = exip;
427 mini->ac(mini->ac_cls, 429 mini->ac (mini->ac_cls,
428 GNUNET_YES, 430 GNUNET_YES,
429 (const struct sockaddr *)&mini->current_addr, 431 (const struct sockaddr *) &mini->current_addr,
430 sizeof(mini->current_addr), 432 sizeof(mini->current_addr),
431 GNUNET_NAT_ERROR_SUCCESS); 433 GNUNET_NAT_ERROR_SUCCESS);
432 return; 434 return;
433 } 435 }
434 /* 436 /*
435 * we're looking for output of the form: 437 * we're looking for output of the form:
436 * 438 *
@@ -442,32 +444,32 @@ process_refresh_output(void *cls, const char *line)
442 * "%s TCP PORT->STRING:OURPORT *" or 444 * "%s TCP PORT->STRING:OURPORT *" or
443 * "%s UDP PORT->STRING:OURPORT *" 445 * "%s UDP PORT->STRING:OURPORT *"
444 */ 446 */
445 GNUNET_snprintf(pstr, sizeof(pstr), ":%u ", mini->port); 447 GNUNET_snprintf (pstr, sizeof(pstr), ":%u ", mini->port);
446 if (NULL == (s = strstr(line, "->"))) 448 if (NULL == (s = strstr (line, "->")))
447 return; /* skip */ 449 return; /* skip */
448 if (NULL == strstr(s, pstr)) 450 if (NULL == strstr (s, pstr))
449 return; /* skip */ 451 return; /* skip */
450 if (1 != sscanf(line, 452 if (1 != sscanf (line,
451 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" 453 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s"
452 : "%*u UDP %u->%*s:%*u %*s", 454 : "%*u UDP %u->%*s:%*u %*s",
453 &nport)) 455 &nport))
454 return; /* skip */ 456 return; /* skip */
455 mini->found = GNUNET_YES; 457 mini->found = GNUNET_YES;
456 if (nport == ntohs(mini->current_addr.sin_port)) 458 if (nport == ntohs (mini->current_addr.sin_port))
457 return; /* no change */ 459 return; /* no change */
458 460
459 /* external port changed, update mapping */ 461 /* external port changed, update mapping */
460 mini->ac(mini->ac_cls, 462 mini->ac (mini->ac_cls,
461 GNUNET_NO, 463 GNUNET_NO,
462 (const struct sockaddr *)&mini->current_addr, 464 (const struct sockaddr *) &mini->current_addr,
463 sizeof(mini->current_addr), 465 sizeof(mini->current_addr),
464 GNUNET_NAT_ERROR_SUCCESS); 466 GNUNET_NAT_ERROR_SUCCESS);
465 mini->current_addr.sin_port = htons((uint16_t)nport); 467 mini->current_addr.sin_port = htons ((uint16_t) nport);
466 mini->ac(mini->ac_cls, 468 mini->ac (mini->ac_cls,
467 GNUNET_YES, 469 GNUNET_YES,
468 (const struct sockaddr *)&mini->current_addr, 470 (const struct sockaddr *) &mini->current_addr,
469 sizeof(mini->current_addr), 471 sizeof(mini->current_addr),
470 GNUNET_NAT_ERROR_SUCCESS); 472 GNUNET_NAT_ERROR_SUCCESS);
471} 473}
472 474
473 475
@@ -477,44 +479,44 @@ process_refresh_output(void *cls, const char *line)
477 * @param cls the 'struct GNUNET_NAT_MiniHandle' 479 * @param cls the 'struct GNUNET_NAT_MiniHandle'
478 */ 480 */
479static void 481static void
480do_refresh(void *cls) 482do_refresh (void *cls)
481{ 483{
482 struct GNUNET_NAT_MiniHandle *mini = cls; 484 struct GNUNET_NAT_MiniHandle *mini = cls;
483 int ac; 485 int ac;
484 486
485 mini->refresh_task = 487 mini->refresh_task =
486 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, mini); 488 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
487 LOG(GNUNET_ERROR_TYPE_DEBUG, 489 LOG (GNUNET_ERROR_TYPE_DEBUG,
488 "Running `upnpc' to check if our mapping still exists\n"); 490 "Running `upnpc' to check if our mapping still exists\n");
489 mini->found = GNUNET_NO; 491 mini->found = GNUNET_NO;
490 ac = GNUNET_NO; 492 ac = GNUNET_NO;
491 if (NULL != mini->map_cmd) 493 if (NULL != mini->map_cmd)
492 { 494 {
493 /* took way too long, abort it! */ 495 /* took way too long, abort it! */
494 GNUNET_OS_command_stop(mini->map_cmd); 496 GNUNET_OS_command_stop (mini->map_cmd);
495 mini->map_cmd = NULL; 497 mini->map_cmd = NULL;
496 ac = GNUNET_YES; 498 ac = GNUNET_YES;
497 } 499 }
498 if (NULL != mini->refresh_cmd) 500 if (NULL != mini->refresh_cmd)
499 { 501 {
500 /* took way too long, abort it! */ 502 /* took way too long, abort it! */
501 GNUNET_OS_command_stop(mini->refresh_cmd); 503 GNUNET_OS_command_stop (mini->refresh_cmd);
502 mini->refresh_cmd = NULL; 504 mini->refresh_cmd = NULL;
503 ac = GNUNET_YES; 505 ac = GNUNET_YES;
504 } 506 }
505 mini->refresh_cmd = GNUNET_OS_command_run(&process_refresh_output, 507 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
506 mini, 508 mini,
507 MAP_TIMEOUT, 509 MAP_TIMEOUT,
508 "upnpc", 510 "upnpc",
509 "upnpc", 511 "upnpc",
510 "-l", 512 "-l",
511 NULL); 513 NULL);
512 if (GNUNET_YES == ac) 514 if (GNUNET_YES == ac)
513 mini->ac(mini->ac_cls, 515 mini->ac (mini->ac_cls,
514 GNUNET_SYSERR, 516 GNUNET_SYSERR,
515 NULL, 517 NULL,
516 0, 518 0,
517 GNUNET_NAT_ERROR_UPNPC_TIMEOUT); 519 GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
518} 520}
519 521
520 522
@@ -525,7 +527,7 @@ do_refresh(void *cls)
525 * @param line line of output, NULL at the end 527 * @param line line of output, NULL at the end
526 */ 528 */
527static void 529static void
528process_map_output(void *cls, const char *line) 530process_map_output (void *cls, const char *line)
529{ 531{
530 struct GNUNET_NAT_MiniHandle *mini = cls; 532 struct GNUNET_NAT_MiniHandle *mini = cls;
531 const char *ipaddr; 533 const char *ipaddr;
@@ -534,51 +536,51 @@ process_map_output(void *cls, const char *line)
534 unsigned int port; 536 unsigned int port;
535 537
536 if (NULL == line) 538 if (NULL == line)
537 { 539 {
538 GNUNET_OS_command_stop(mini->map_cmd); 540 GNUNET_OS_command_stop (mini->map_cmd);
539 mini->map_cmd = NULL; 541 mini->map_cmd = NULL;
540 if (GNUNET_YES != mini->did_map) 542 if (GNUNET_YES != mini->did_map)
541 mini->ac(mini->ac_cls, 543 mini->ac (mini->ac_cls,
542 GNUNET_SYSERR, 544 GNUNET_SYSERR,
543 NULL, 545 NULL,
544 0, 546 0,
545 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED); 547 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
546 if (NULL == mini->refresh_task) 548 if (NULL == mini->refresh_task)
547 mini->refresh_task = 549 mini->refresh_task =
548 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, mini); 550 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
549 return; 551 return;
550 } 552 }
551 /* 553 /*
552 * The upnpc output we're after looks like this: 554 * The upnpc output we're after looks like this:
553 * 555 *
554 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000" 556 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
555 */ 557 */
556 if ((NULL == (ipaddr = strstr(line, " "))) || 558 if ((NULL == (ipaddr = strstr (line, " "))) ||
557 (NULL == (pstr = strstr(ipaddr, ":"))) || 559 (NULL == (pstr = strstr (ipaddr, ":"))) ||
558 (1 != sscanf(pstr + 1, "%u", &port))) 560 (1 != sscanf (pstr + 1, "%u", &port)))
559 { 561 {
560 return; /* skip line */ 562 return; /* skip line */
561 } 563 }
562 ipa = GNUNET_strdup(ipaddr + 1); 564 ipa = GNUNET_strdup (ipaddr + 1);
563 strstr(ipa, ":")[0] = '\0'; 565 strstr (ipa, ":")[0] = '\0';
564 if (1 != inet_pton(AF_INET, ipa, &mini->current_addr.sin_addr)) 566 if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr))
565 { 567 {
566 GNUNET_free(ipa); 568 GNUNET_free (ipa);
567 return; /* skip line */ 569 return; /* skip line */
568 } 570 }
569 GNUNET_free(ipa); 571 GNUNET_free (ipa);
570 572
571 mini->current_addr.sin_port = htons(port); 573 mini->current_addr.sin_port = htons (port);
572 mini->current_addr.sin_family = AF_INET; 574 mini->current_addr.sin_family = AF_INET;
573#if HAVE_SOCKADDR_IN_SIN_LEN 575#if HAVE_SOCKADDR_IN_SIN_LEN
574 mini->current_addr.sin_len = sizeof(struct sockaddr_in); 576 mini->current_addr.sin_len = sizeof(struct sockaddr_in);
575#endif 577#endif
576 mini->did_map = GNUNET_YES; 578 mini->did_map = GNUNET_YES;
577 mini->ac(mini->ac_cls, 579 mini->ac (mini->ac_cls,
578 GNUNET_YES, 580 GNUNET_YES,
579 (const struct sockaddr *)&mini->current_addr, 581 (const struct sockaddr *) &mini->current_addr,
580 sizeof(mini->current_addr), 582 sizeof(mini->current_addr),
581 GNUNET_NAT_ERROR_SUCCESS); 583 GNUNET_NAT_ERROR_SUCCESS);
582} 584}
583 585
584 586
@@ -596,28 +598,28 @@ process_map_output(void *cls, const char *line)
596 * @return NULL on error (no 'upnpc' installed) 598 * @return NULL on error (no 'upnpc' installed)
597 */ 599 */
598struct GNUNET_NAT_MiniHandle * 600struct GNUNET_NAT_MiniHandle *
599GNUNET_NAT_mini_map_start(uint16_t port, 601GNUNET_NAT_mini_map_start (uint16_t port,
600 int is_tcp, 602 int is_tcp,
601 GNUNET_NAT_MiniAddressCallback ac, 603 GNUNET_NAT_MiniAddressCallback ac,
602 void *ac_cls) 604 void *ac_cls)
603{ 605{
604 struct GNUNET_NAT_MiniHandle *ret; 606 struct GNUNET_NAT_MiniHandle *ret;
605 607
606 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary("upnpc", GNUNET_NO, NULL)) 608 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL))
607 { 609 {
608 LOG(GNUNET_ERROR_TYPE_INFO, _("`upnpc' command not found\n")); 610 LOG (GNUNET_ERROR_TYPE_INFO, _ ("`upnpc' command not found\n"));
609 ac(ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND); 611 ac (ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
610 return NULL; 612 return NULL;
611 } 613 }
612 LOG(GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n"); 614 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n");
613 ret = GNUNET_new(struct GNUNET_NAT_MiniHandle); 615 ret = GNUNET_new (struct GNUNET_NAT_MiniHandle);
614 ret->ac = ac; 616 ret->ac = ac;
615 ret->ac_cls = ac_cls; 617 ret->ac_cls = ac_cls;
616 ret->is_tcp = is_tcp; 618 ret->is_tcp = is_tcp;
617 ret->port = port; 619 ret->port = port;
618 ret->refresh_task = 620 ret->refresh_task =
619 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, ret); 621 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret);
620 run_upnpc_r(ret); 622 run_upnpc_r (ret);
621 return ret; 623 return ret;
622} 624}
623 625
@@ -629,18 +631,18 @@ GNUNET_NAT_mini_map_start(uint16_t port,
629 * @param line line of output, NULL at the end 631 * @param line line of output, NULL at the end
630 */ 632 */
631static void 633static void
632process_unmap_output(void *cls, const char *line) 634process_unmap_output (void *cls, const char *line)
633{ 635{
634 struct GNUNET_NAT_MiniHandle *mini = cls; 636 struct GNUNET_NAT_MiniHandle *mini = cls;
635 637
636 if (NULL == line) 638 if (NULL == line)
637 { 639 {
638 LOG(GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n"); 640 LOG (GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n");
639 GNUNET_OS_command_stop(mini->unmap_cmd); 641 GNUNET_OS_command_stop (mini->unmap_cmd);
640 mini->unmap_cmd = NULL; 642 mini->unmap_cmd = NULL;
641 GNUNET_free(mini); 643 GNUNET_free (mini);
642 return; 644 return;
643 } 645 }
644 /* we don't really care about the output... */ 646 /* we don't really care about the output... */
645} 647}
646 648
@@ -654,54 +656,54 @@ process_unmap_output(void *cls, const char *line)
654 * @param mini the handle 656 * @param mini the handle
655 */ 657 */
656void 658void
657GNUNET_NAT_mini_map_stop(struct GNUNET_NAT_MiniHandle *mini) 659GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
658{ 660{
659 char pstr[6]; 661 char pstr[6];
660 662
661 if (NULL != mini->refresh_task) 663 if (NULL != mini->refresh_task)
662 { 664 {
663 GNUNET_SCHEDULER_cancel(mini->refresh_task); 665 GNUNET_SCHEDULER_cancel (mini->refresh_task);
664 mini->refresh_task = NULL; 666 mini->refresh_task = NULL;
665 } 667 }
666 if (NULL != mini->refresh_cmd) 668 if (NULL != mini->refresh_cmd)
667 { 669 {
668 GNUNET_OS_command_stop(mini->refresh_cmd); 670 GNUNET_OS_command_stop (mini->refresh_cmd);
669 mini->refresh_cmd = NULL; 671 mini->refresh_cmd = NULL;
670 } 672 }
671 if (NULL != mini->map_cmd) 673 if (NULL != mini->map_cmd)
672 { 674 {
673 GNUNET_OS_command_stop(mini->map_cmd); 675 GNUNET_OS_command_stop (mini->map_cmd);
674 mini->map_cmd = NULL; 676 mini->map_cmd = NULL;
675 } 677 }
676 if (GNUNET_NO == mini->did_map) 678 if (GNUNET_NO == mini->did_map)
677 { 679 {
678 GNUNET_free(mini); 680 GNUNET_free (mini);
679 return; 681 return;
680 } 682 }
681 mini->ac(mini->ac_cls, 683 mini->ac (mini->ac_cls,
682 GNUNET_NO, 684 GNUNET_NO,
683 (const struct sockaddr *)&mini->current_addr, 685 (const struct sockaddr *) &mini->current_addr,
684 sizeof(mini->current_addr), 686 sizeof(mini->current_addr),
685 GNUNET_NAT_ERROR_SUCCESS); 687 GNUNET_NAT_ERROR_SUCCESS);
686 /* Note: oddly enough, deletion uses the external port whereas 688 /* Note: oddly enough, deletion uses the external port whereas
687 * addition uses the internal port; this rarely matters since they 689 * addition uses the internal port; this rarely matters since they
688 * often are the same, but it might... */ 690 * often are the same, but it might... */
689 GNUNET_snprintf(pstr, 691 GNUNET_snprintf (pstr,
690 sizeof(pstr), 692 sizeof(pstr),
691 "%u", 693 "%u",
692 (unsigned int)ntohs(mini->current_addr.sin_port)); 694 (unsigned int) ntohs (mini->current_addr.sin_port));
693 LOG(GNUNET_ERROR_TYPE_DEBUG, 695 LOG (GNUNET_ERROR_TYPE_DEBUG,
694 "Unmapping port %u with UPnP\n", 696 "Unmapping port %u with UPnP\n",
695 ntohs(mini->current_addr.sin_port)); 697 ntohs (mini->current_addr.sin_port));
696 mini->unmap_cmd = GNUNET_OS_command_run(&process_unmap_output, 698 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
697 mini, 699 mini,
698 UNMAP_TIMEOUT, 700 UNMAP_TIMEOUT,
699 "upnpc", 701 "upnpc",
700 "upnpc", 702 "upnpc",
701 "-d", 703 "-d",
702 pstr, 704 pstr,
703 mini->is_tcp ? "tcp" : "udp", 705 mini->is_tcp ? "tcp" : "udp",
704 NULL); 706 NULL);
705} 707}
706 708
707 709
diff --git a/src/nat/gnunet-service-nat_mini.h b/src/nat/gnunet-service-nat_mini.h
index 031b8e935..dffc9758a 100644
--- a/src/nat/gnunet-service-nat_mini.h
+++ b/src/nat/gnunet-service-nat_mini.h
@@ -54,8 +54,8 @@ struct GNUNET_NAT_ExternalHandle;
54 * @return handle for cancellation (can only be used until @a cb is called), NULL on error 54 * @return handle for cancellation (can only be used until @a cb is called), NULL on error
55 */ 55 */
56struct GNUNET_NAT_ExternalHandle * 56struct GNUNET_NAT_ExternalHandle *
57GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb, 57GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb,
58 void *cb_cls); 58 void *cb_cls);
59 59
60 60
61/** 61/**
@@ -64,7 +64,8 @@ GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb,
64 * @param eh operation to cancel 64 * @param eh operation to cancel
65 */ 65 */
66void 66void
67GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh); 67GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct
68 GNUNET_NAT_ExternalHandle *eh);
68 69
69 70
70/** 71/**
@@ -106,10 +107,10 @@ typedef void
106 * @return NULL on error 107 * @return NULL on error
107 */ 108 */
108struct GNUNET_NAT_MiniHandle * 109struct GNUNET_NAT_MiniHandle *
109GNUNET_NAT_mini_map_start(uint16_t port, 110GNUNET_NAT_mini_map_start (uint16_t port,
110 int is_tcp, 111 int is_tcp,
111 GNUNET_NAT_MiniAddressCallback ac, 112 GNUNET_NAT_MiniAddressCallback ac,
112 void *ac_cls); 113 void *ac_cls);
113 114
114 115
115/** 116/**
@@ -121,7 +122,7 @@ GNUNET_NAT_mini_map_start(uint16_t port,
121 * @param mini the handle 122 * @param mini the handle
122 */ 123 */
123void 124void
124GNUNET_NAT_mini_map_stop(struct GNUNET_NAT_MiniHandle *mini); 125GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
125 126
126 127
127#endif 128#endif
diff --git a/src/nat/gnunet-service-nat_stun.c b/src/nat/gnunet-service-nat_stun.c
index d8b791d0a..8a5311254 100644
--- a/src/nat/gnunet-service-nat_stun.c
+++ b/src/nat/gnunet-service-nat_stun.c
@@ -40,14 +40,15 @@
40#include "gnunet_util_lib.h" 40#include "gnunet_util_lib.h"
41#include "nat_stun.h" 41#include "nat_stun.h"
42 42
43#define LOG(kind, ...) GNUNET_log_from(kind, "stun", __VA_ARGS__) 43#define LOG(kind, ...) GNUNET_log_from (kind, "stun", __VA_ARGS__)
44 44
45 45
46/** 46/**
47 * Context for #stun_get_mapped(). 47 * Context for #stun_get_mapped().
48 * Used to store state across processing attributes. 48 * Used to store state across processing attributes.
49 */ 49 */
50struct StunState { 50struct StunState
51{
51 uint16_t attr; 52 uint16_t attr;
52}; 53};
53 54
@@ -64,44 +65,44 @@ struct StunState {
64 * @return #GNUNET_OK if @a arg was initialized 65 * @return #GNUNET_OK if @a arg was initialized
65 */ 66 */
66static int 67static int
67stun_get_mapped(struct StunState *st, 68stun_get_mapped (struct StunState *st,
68 const struct stun_attr *attr, 69 const struct stun_attr *attr,
69 uint32_t magic, 70 uint32_t magic,
70 struct sockaddr_in *arg) 71 struct sockaddr_in *arg)
71{ 72{
72 const struct stun_addr *returned_addr; 73 const struct stun_addr *returned_addr;
73 struct sockaddr_in *sa = (struct sockaddr_in *)arg; 74 struct sockaddr_in *sa = (struct sockaddr_in *) arg;
74 uint16_t type = ntohs(attr->attr); 75 uint16_t type = ntohs (attr->attr);
75 76
76 switch (type) 77 switch (type)
77 { 78 {
78 case STUN_MAPPED_ADDRESS: 79 case STUN_MAPPED_ADDRESS:
79 if ((st->attr == STUN_XOR_MAPPED_ADDRESS) || 80 if ((st->attr == STUN_XOR_MAPPED_ADDRESS) ||
80 (st->attr == STUN_MS_XOR_MAPPED_ADDRESS)) 81 (st->attr == STUN_MS_XOR_MAPPED_ADDRESS))
81 return GNUNET_NO; 82 return GNUNET_NO;
82 magic = 0; 83 magic = 0;
83 break; 84 break;
84 85
85 case STUN_MS_XOR_MAPPED_ADDRESS: 86 case STUN_MS_XOR_MAPPED_ADDRESS:
86 if (st->attr == STUN_XOR_MAPPED_ADDRESS) 87 if (st->attr == STUN_XOR_MAPPED_ADDRESS)
87 return GNUNET_NO; 88 return GNUNET_NO;
88 break; 89 break;
89 90
90 case STUN_XOR_MAPPED_ADDRESS: 91 case STUN_XOR_MAPPED_ADDRESS:
91 break; 92 break;
92 93
93 default: 94 default:
94 return GNUNET_NO; 95 return GNUNET_NO;
95 } 96 }
96 97
97 if (ntohs(attr->len) < sizeof(struct stun_addr)) 98 if (ntohs (attr->len) < sizeof(struct stun_addr))
98 return GNUNET_NO; 99 return GNUNET_NO;
99 returned_addr = (const struct stun_addr *)(attr + 1); 100 returned_addr = (const struct stun_addr *) (attr + 1);
100 if (AF_INET != returned_addr->family) 101 if (AF_INET != returned_addr->family)
101 return GNUNET_NO; 102 return GNUNET_NO;
102 st->attr = type; 103 st->attr = type;
103 sa->sin_family = AF_INET; 104 sa->sin_family = AF_INET;
104 sa->sin_port = returned_addr->port ^ htons(ntohl(magic) >> 16); 105 sa->sin_port = returned_addr->port ^ htons (ntohl (magic) >> 16);
105 sa->sin_addr.s_addr = returned_addr->addr ^ magic; 106 sa->sin_addr.s_addr = returned_addr->addr ^ magic;
106 return GNUNET_OK; 107 return GNUNET_OK;
107} 108}
@@ -121,9 +122,9 @@ stun_get_mapped(struct StunState *st,
121 * #GNUNET_NO if the packet is invalid (not a stun packet) 122 * #GNUNET_NO if the packet is invalid (not a stun packet)
122 */ 123 */
123int 124int
124GNUNET_NAT_stun_handle_packet_(const void *data, 125GNUNET_NAT_stun_handle_packet_ (const void *data,
125 size_t len, 126 size_t len,
126 struct sockaddr_in *arg) 127 struct sockaddr_in *arg)
127{ 128{
128 const struct stun_header *hdr; 129 const struct stun_header *hdr;
129 const struct stun_attr *attr; 130 const struct stun_attr *attr;
@@ -137,76 +138,76 @@ GNUNET_NAT_stun_handle_packet_(const void *data,
137 * while 'data' is advanced accordingly. 138 * while 'data' is advanced accordingly.
138 */ 139 */
139 if (len < sizeof(struct stun_header)) 140 if (len < sizeof(struct stun_header))
140 { 141 {
141 LOG(GNUNET_ERROR_TYPE_DEBUG, 142 LOG (GNUNET_ERROR_TYPE_DEBUG,
142 "Packet too short to be a STUN packet\n"); 143 "Packet too short to be a STUN packet\n");
143 return GNUNET_NO; 144 return GNUNET_NO;
144 } 145 }
145 hdr = data; 146 hdr = data;
146 /* Skip header as it is already in hdr */ 147 /* Skip header as it is already in hdr */
147 len -= sizeof(struct stun_header); 148 len -= sizeof(struct stun_header);
148 data += sizeof(struct stun_header); 149 data += sizeof(struct stun_header);
149 150
150 /* len as advertised in the message */ 151 /* len as advertised in the message */
151 advertised_message_size = ntohs(hdr->msglen); 152 advertised_message_size = ntohs (hdr->msglen);
152 message_magic_cookie = ntohl(hdr->magic); 153 message_magic_cookie = ntohl (hdr->magic);
153 /* Compare if the cookie match */ 154 /* Compare if the cookie match */
154 if (STUN_MAGIC_COOKIE != message_magic_cookie) 155 if (STUN_MAGIC_COOKIE != message_magic_cookie)
155 { 156 {
156 LOG(GNUNET_ERROR_TYPE_DEBUG, 157 LOG (GNUNET_ERROR_TYPE_DEBUG,
157 "Invalid magic cookie for STUN packet\n"); 158 "Invalid magic cookie for STUN packet\n");
158 return GNUNET_NO; 159 return GNUNET_NO;
159 } 160 }
160 161
161 LOG(GNUNET_ERROR_TYPE_INFO, 162 LOG (GNUNET_ERROR_TYPE_INFO,
162 "STUN Packet, msg %s (%04x), length: %d\n", 163 "STUN Packet, msg %s (%04x), length: %d\n",
163 stun_msg2str(ntohs(hdr->msgtype)), 164 stun_msg2str (ntohs (hdr->msgtype)),
164 ntohs(hdr->msgtype), 165 ntohs (hdr->msgtype),
165 advertised_message_size); 166 advertised_message_size);
166 if (advertised_message_size > len) 167 if (advertised_message_size > len)
167 { 168 {
168 LOG(GNUNET_ERROR_TYPE_INFO, 169 LOG (GNUNET_ERROR_TYPE_INFO,
169 "Scrambled STUN packet length (got %d, expecting %d)\n", 170 "Scrambled STUN packet length (got %d, expecting %d)\n",
170 advertised_message_size, 171 advertised_message_size,
171 (int)len); 172 (int) len);
172 return GNUNET_NO; 173 return GNUNET_NO;
173 } 174 }
174 len = advertised_message_size; 175 len = advertised_message_size;
175 memset(&st, 0, sizeof(st)); 176 memset (&st, 0, sizeof(st));
176 177
177 while (len > 0) 178 while (len > 0)
179 {
180 if (len < sizeof(struct stun_attr))
178 { 181 {
179 if (len < sizeof(struct stun_attr)) 182 LOG (GNUNET_ERROR_TYPE_INFO,
180 { 183 "Attribute too short (got %d, expecting %d)\n",
181 LOG(GNUNET_ERROR_TYPE_INFO, 184 (int) len,
182 "Attribute too short (got %d, expecting %d)\n", 185 (int) sizeof(struct stun_attr));
183 (int)len, 186 break;
184 (int)sizeof(struct stun_attr)); 187 }
185 break; 188 attr = (const struct stun_attr *) data;
186 } 189
187 attr = (const struct stun_attr *)data; 190 /* compute total attribute length */
188 191 advertised_message_size = ntohs (attr->len) + sizeof(struct stun_attr);
189 /* compute total attribute length */ 192
190 advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr); 193 /* Check if we still have space in our buffer */
191 194 if (advertised_message_size > len)
192 /* Check if we still have space in our buffer */ 195 {
193 if (advertised_message_size > len) 196 LOG (GNUNET_ERROR_TYPE_INFO,
194 { 197 "Inconsistent attribute (length %d exceeds remaining msg len %d)\n",
195 LOG(GNUNET_ERROR_TYPE_INFO, 198 advertised_message_size,
196 "Inconsistent attribute (length %d exceeds remaining msg len %d)\n", 199 (int) len);
197 advertised_message_size, 200 break;
198 (int)len);
199 break;
200 }
201 if (GNUNET_OK ==
202 stun_get_mapped(&st,
203 attr,
204 hdr->magic,
205 arg))
206 ret = GNUNET_OK;
207 data += advertised_message_size;
208 len -= advertised_message_size;
209 } 201 }
202 if (GNUNET_OK ==
203 stun_get_mapped (&st,
204 attr,
205 hdr->magic,
206 arg))
207 ret = GNUNET_OK;
208 data += advertised_message_size;
209 len -= advertised_message_size;
210 }
210 return ret; 211 return ret;
211} 212}
212 213
diff --git a/src/nat/gnunet-service-nat_stun.h b/src/nat/gnunet-service-nat_stun.h
index af6520fbf..912fc3b46 100644
--- a/src/nat/gnunet-service-nat_stun.h
+++ b/src/nat/gnunet-service-nat_stun.h
@@ -54,8 +54,8 @@
54 * #GNUNET_NO if the packet is invalid (not a stun packet) 54 * #GNUNET_NO if the packet is invalid (not a stun packet)
55 */ 55 */
56int 56int
57GNUNET_NAT_stun_handle_packet_(const void *data, 57GNUNET_NAT_stun_handle_packet_ (const void *data,
58 size_t len, 58 size_t len,
59 struct sockaddr_in *arg); 59 struct sockaddr_in *arg);
60 60
61#endif 61#endif
diff --git a/src/nat/nat.h b/src/nat/nat.h
index 62377435a..aac8db64c 100644
--- a/src/nat/nat.h
+++ b/src/nat/nat.h
@@ -36,7 +36,8 @@ 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_TestMessage { 39struct GNUNET_NAT_TestMessage
40{
40 /** 41 /**
41 * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST 42 * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST
42 */ 43 */
@@ -68,7 +69,8 @@ struct GNUNET_NAT_TestMessage {
68 * Flags specifying the events this client would be 69 * Flags specifying the events this client would be
69 * interested in being told about. 70 * interested in being told about.
70 */ 71 */
71enum GNUNET_NAT_RegisterFlags { 72enum GNUNET_NAT_RegisterFlags
73{
72 /** 74 /**
73 * This client does not want any notifications. 75 * This client does not want any notifications.
74 */ 76 */
@@ -90,7 +92,8 @@ enum GNUNET_NAT_RegisterFlags {
90/** 92/**
91 * Message sent by a client to register with its addresses. 93 * Message sent by a client to register with its addresses.
92 */ 94 */
93struct GNUNET_NAT_RegisterMessage { 95struct GNUNET_NAT_RegisterMessage
96{
94 /** 97 /**
95 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REGISTER 98 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REGISTER
96 */ 99 */
@@ -129,7 +132,8 @@ struct GNUNET_NAT_RegisterMessage {
129/** 132/**
130 * Client telling the service to (possibly) handle a STUN message. 133 * Client telling the service to (possibly) handle a STUN message.
131 */ 134 */
132struct GNUNET_NAT_HandleStunMessage { 135struct GNUNET_NAT_HandleStunMessage
136{
133 /** 137 /**
134 * Header with type #GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN 138 * Header with type #GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN
135 */ 139 */
@@ -154,7 +158,8 @@ struct GNUNET_NAT_HandleStunMessage {
154/** 158/**
155 * Client asking the service to initiate connection reversal. 159 * Client asking the service to initiate connection reversal.
156 */ 160 */
157struct GNUNET_NAT_RequestConnectionReversalMessage { 161struct GNUNET_NAT_RequestConnectionReversalMessage
162{
158 /** 163 /**
159 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL 164 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL
160 */ 165 */
@@ -179,7 +184,8 @@ struct GNUNET_NAT_RequestConnectionReversalMessage {
179/** 184/**
180 * Service telling a client that connection reversal was requested. 185 * Service telling a client that connection reversal was requested.
181 */ 186 */
182struct GNUNET_NAT_ConnectionReversalRequestedMessage { 187struct GNUNET_NAT_ConnectionReversalRequestedMessage
188{
183 /** 189 /**
184 * Header with type #GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED 190 * Header with type #GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED
185 */ 191 */
@@ -193,7 +199,8 @@ struct GNUNET_NAT_ConnectionReversalRequestedMessage {
193 * Service notifying the client about changes in the set of 199 * Service notifying the client about changes in the set of
194 * addresses it has. 200 * addresses it has.
195 */ 201 */
196struct GNUNET_NAT_AddressChangeNotificationMessage { 202struct GNUNET_NAT_AddressChangeNotificationMessage
203{
197 /** 204 /**
198 * Header with type #GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE 205 * Header with type #GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE
199 */ 206 */
diff --git a/src/nat/nat_api.c b/src/nat/nat_api.c
index 877589d85..c1125dc75 100644
--- a/src/nat/nat_api.c
+++ b/src/nat/nat_api.c
@@ -35,7 +35,8 @@
35/** 35/**
36 * Entry in DLL of addresses of this peer. 36 * Entry in DLL of addresses of this peer.
37 */ 37 */
38struct AddrEntry { 38struct AddrEntry
39{
39 /** 40 /**
40 * DLL. 41 * DLL.
41 */ 42 */
@@ -67,7 +68,8 @@ struct AddrEntry {
67/** 68/**
68 * Handle for active NAT registrations. 69 * Handle for active NAT registrations.
69 */ 70 */
70struct GNUNET_NAT_Handle { 71struct GNUNET_NAT_Handle
72{
71 /** 73 /**
72 * Configuration we use. 74 * Configuration we use.
73 */ 75 */
@@ -126,7 +128,7 @@ struct GNUNET_NAT_Handle {
126 * @param cls our `struct GNUNET_NAT_Handle *` 128 * @param cls our `struct GNUNET_NAT_Handle *`
127 */ 129 */
128static void 130static void
129do_connect(void *cls); 131do_connect (void *cls);
130 132
131 133
132/** 134/**
@@ -135,29 +137,29 @@ do_connect(void *cls);
135 * @param nh handle to reconnect 137 * @param nh handle to reconnect
136 */ 138 */
137static void 139static void
138reconnect(struct GNUNET_NAT_Handle *nh) 140reconnect (struct GNUNET_NAT_Handle *nh)
139{ 141{
140 struct AddrEntry *ae; 142 struct AddrEntry *ae;
141 143
142 if (NULL != nh->mq) 144 if (NULL != nh->mq)
143 { 145 {
144 GNUNET_MQ_destroy(nh->mq); 146 GNUNET_MQ_destroy (nh->mq);
145 nh->mq = NULL; 147 nh->mq = NULL;
146 } 148 }
147 while (NULL != (ae = nh->ae_head)) 149 while (NULL != (ae = nh->ae_head))
148 { 150 {
149 GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae); 151 GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae);
150 nh->address_callback(nh->callback_cls, 152 nh->address_callback (nh->callback_cls,
151 &ae->app_ctx, 153 &ae->app_ctx,
152 GNUNET_NO, 154 GNUNET_NO,
153 ae->ac, 155 ae->ac,
154 (const struct sockaddr *)&ae[1], 156 (const struct sockaddr *) &ae[1],
155 ae->addrlen); 157 ae->addrlen);
156 GNUNET_free(ae); 158 GNUNET_free (ae);
157 } 159 }
158 nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF(nh->reconnect_delay); 160 nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF (nh->reconnect_delay);
159 nh->reconnect_task = 161 nh->reconnect_task =
160 GNUNET_SCHEDULER_add_delayed(nh->reconnect_delay, &do_connect, nh); 162 GNUNET_SCHEDULER_add_delayed (nh->reconnect_delay, &do_connect, nh);
161} 163}
162 164
163 165
@@ -169,15 +171,15 @@ reconnect(struct GNUNET_NAT_Handle *nh)
169 * @return #GNUNET_OK if @a crm is well-formed 171 * @return #GNUNET_OK if @a crm is well-formed
170 */ 172 */
171static int 173static int
172check_connection_reversal_request( 174check_connection_reversal_request (
173 void *cls, 175 void *cls,
174 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) 176 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
175{ 177{
176 if (ntohs(crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in)) 178 if (ntohs (crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in))
177 { 179 {
178 GNUNET_break(0); 180 GNUNET_break (0);
179 return GNUNET_SYSERR; 181 return GNUNET_SYSERR;
180 } 182 }
181 return GNUNET_OK; 183 return GNUNET_OK;
182} 184}
183 185
@@ -189,15 +191,15 @@ check_connection_reversal_request(
189 * @param crm the message 191 * @param crm the message
190 */ 192 */
191static void 193static void
192handle_connection_reversal_request( 194handle_connection_reversal_request (
193 void *cls, 195 void *cls,
194 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) 196 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
195{ 197{
196 struct GNUNET_NAT_Handle *nh = cls; 198 struct GNUNET_NAT_Handle *nh = cls;
197 199
198 nh->reversal_callback(nh->callback_cls, 200 nh->reversal_callback (nh->callback_cls,
199 (const struct sockaddr *)&crm[1], 201 (const struct sockaddr *) &crm[1],
200 sizeof(struct sockaddr_in)); 202 sizeof(struct sockaddr_in));
201} 203}
202 204
203 205
@@ -209,38 +211,38 @@ handle_connection_reversal_request(
209 * @return #GNUNET_OK if @a crm is well-formed 211 * @return #GNUNET_OK if @a crm is well-formed
210 */ 212 */
211static int 213static int
212check_address_change_notification( 214check_address_change_notification (
213 void *cls, 215 void *cls,
214 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) 216 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
215{ 217{
216 size_t alen = ntohs(acn->header.size) - sizeof(*acn); 218 size_t alen = ntohs (acn->header.size) - sizeof(*acn);
217 219
218 switch (alen) 220 switch (alen)
219 { 221 {
220 case sizeof(struct sockaddr_in): { 222 case sizeof(struct sockaddr_in): {
221 const struct sockaddr_in *s4 = (const struct sockaddr_in *)&acn[1]; 223 const struct sockaddr_in *s4 = (const struct sockaddr_in *) &acn[1];
222 if (AF_INET != s4->sin_family) 224 if (AF_INET != s4->sin_family)
223 { 225 {
224 GNUNET_break(0); 226 GNUNET_break (0);
225 return GNUNET_SYSERR; 227 return GNUNET_SYSERR;
226 } 228 }
227 } 229 }
228 break; 230 break;
229 231
230 case sizeof(struct sockaddr_in6): { 232 case sizeof(struct sockaddr_in6): {
231 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)&acn[1]; 233 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) &acn[1];
232 if (AF_INET6 != s6->sin6_family) 234 if (AF_INET6 != s6->sin6_family)
233 { 235 {
234 GNUNET_break(0); 236 GNUNET_break (0);
235 return GNUNET_SYSERR; 237 return GNUNET_SYSERR;
236 } 238 }
237 } 239 }
238 break; 240 break;
239 241
240 default: 242 default:
241 GNUNET_break(0); 243 GNUNET_break (0);
242 return GNUNET_SYSERR; 244 return GNUNET_SYSERR;
243 } 245 }
244 return GNUNET_OK; 246 return GNUNET_OK;
245} 247}
246 248
@@ -252,53 +254,53 @@ check_address_change_notification(
252 * @param acn the message 254 * @param acn the message
253 */ 255 */
254static void 256static void
255handle_address_change_notification( 257handle_address_change_notification (
256 void *cls, 258 void *cls,
257 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) 259 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
258{ 260{
259 struct GNUNET_NAT_Handle *nh = cls; 261 struct GNUNET_NAT_Handle *nh = cls;
260 size_t alen = ntohs(acn->header.size) - sizeof(*acn); 262 size_t alen = ntohs (acn->header.size) - sizeof(*acn);
261 const struct sockaddr *sa = (const struct sockaddr *)&acn[1]; 263 const struct sockaddr *sa = (const struct sockaddr *) &acn[1];
262 enum GNUNET_NAT_AddressClass ac; 264 enum GNUNET_NAT_AddressClass ac;
263 struct AddrEntry *ae; 265 struct AddrEntry *ae;
264 266
265 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Received address change notification\n"); 268 "Received address change notification\n");
267 ac = (enum GNUNET_NAT_AddressClass)ntohl(acn->addr_class); 269 ac = (enum GNUNET_NAT_AddressClass) ntohl (acn->addr_class);
268 if (GNUNET_YES == ntohl(acn->add_remove)) 270 if (GNUNET_YES == ntohl (acn->add_remove))
269 { 271 {
270 ae = GNUNET_malloc(sizeof(*ae) + alen); 272 ae = GNUNET_malloc (sizeof(*ae) + alen);
271 ae->ac = ac; 273 ae->ac = ac;
272 ae->addrlen = alen; 274 ae->addrlen = alen;
273 GNUNET_memcpy(&ae[1], sa, alen); 275 GNUNET_memcpy (&ae[1], sa, alen);
274 GNUNET_CONTAINER_DLL_insert(nh->ae_head, nh->ae_tail, ae); 276 GNUNET_CONTAINER_DLL_insert (nh->ae_head, nh->ae_tail, ae);
275 nh->address_callback(nh->callback_cls, 277 nh->address_callback (nh->callback_cls,
276 &ae->app_ctx, 278 &ae->app_ctx,
277 ntohl(acn->add_remove), 279 ntohl (acn->add_remove),
278 ac, 280 ac,
279 sa, 281 sa,
280 alen); 282 alen);
281 } 283 }
282 else 284 else
285 {
286 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
287 if ((ae->addrlen == alen) && (0 == memcmp (&ae[1], sa, alen)))
288 break;
289 if (NULL == ae)
283 { 290 {
284 for (ae = nh->ae_head; NULL != ae; ae = ae->next) 291 GNUNET_break (0);
285 if ((ae->addrlen == alen) && (0 == memcmp(&ae[1], sa, alen))) 292 reconnect (nh);
286 break; 293 return;
287 if (NULL == ae)
288 {
289 GNUNET_break(0);
290 reconnect(nh);
291 return;
292 }
293 GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae);
294 nh->address_callback(nh->callback_cls,
295 &ae->app_ctx,
296 ntohl(acn->add_remove),
297 ac,
298 sa,
299 alen);
300 GNUNET_free(ae);
301 } 294 }
295 GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae);
296 nh->address_callback (nh->callback_cls,
297 &ae->app_ctx,
298 ntohl (acn->add_remove),
299 ac,
300 sa,
301 alen);
302 GNUNET_free (ae);
303 }
302} 304}
303 305
304 306
@@ -309,11 +311,11 @@ handle_address_change_notification(
309 * @param error details about the error 311 * @param error details about the error
310 */ 312 */
311static void 313static void
312mq_error_handler(void *cls, enum GNUNET_MQ_Error error) 314mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
313{ 315{
314 struct GNUNET_NAT_Handle *nh = cls; 316 struct GNUNET_NAT_Handle *nh = cls;
315 317
316 reconnect(nh); 318 reconnect (nh);
317} 319}
318 320
319 321
@@ -323,32 +325,32 @@ mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
323 * @param cls our `struct GNUNET_NAT_Handle *` 325 * @param cls our `struct GNUNET_NAT_Handle *`
324 */ 326 */
325static void 327static void
326do_connect(void *cls) 328do_connect (void *cls)
327{ 329{
328 struct GNUNET_NAT_Handle *nh = cls; 330 struct GNUNET_NAT_Handle *nh = cls;
329 struct GNUNET_MQ_MessageHandler handlers[] = 331 struct GNUNET_MQ_MessageHandler handlers[] =
330 { GNUNET_MQ_hd_var_size(connection_reversal_request, 332 { GNUNET_MQ_hd_var_size (connection_reversal_request,
331 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED, 333 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED,
332 struct 334 struct
333 GNUNET_NAT_ConnectionReversalRequestedMessage, 335 GNUNET_NAT_ConnectionReversalRequestedMessage,
334 nh), 336 nh),
335 GNUNET_MQ_hd_var_size(address_change_notification, 337 GNUNET_MQ_hd_var_size (address_change_notification,
336 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE, 338 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE,
337 struct GNUNET_NAT_AddressChangeNotificationMessage, 339 struct GNUNET_NAT_AddressChangeNotificationMessage,
338 nh), 340 nh),
339 GNUNET_MQ_handler_end() }; 341 GNUNET_MQ_handler_end () };
340 struct GNUNET_MQ_Envelope *env; 342 struct GNUNET_MQ_Envelope *env;
341 343
342 nh->reconnect_task = NULL; 344 nh->reconnect_task = NULL;
343 nh->mq = 345 nh->mq =
344 GNUNET_CLIENT_connect(nh->cfg, "nat", handlers, &mq_error_handler, nh); 346 GNUNET_CLIENT_connect (nh->cfg, "nat", handlers, &mq_error_handler, nh);
345 if (NULL == nh->mq) 347 if (NULL == nh->mq)
346 { 348 {
347 reconnect(nh); 349 reconnect (nh);
348 return; 350 return;
349 } 351 }
350 env = GNUNET_MQ_msg_copy(nh->reg); 352 env = GNUNET_MQ_msg_copy (nh->reg);
351 GNUNET_MQ_send(nh->mq, env); 353 GNUNET_MQ_send (nh->mq, env);
352} 354}
353 355
354 356
@@ -373,15 +375,15 @@ do_connect(void *cls)
373 * @return NULL on error, otherwise handle that can be used to unregister 375 * @return NULL on error, otherwise handle that can be used to unregister
374 */ 376 */
375struct GNUNET_NAT_Handle * 377struct GNUNET_NAT_Handle *
376GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, 378GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
377 const char *config_section, 379 const char *config_section,
378 uint8_t proto, 380 uint8_t proto,
379 unsigned int num_addrs, 381 unsigned int num_addrs,
380 const struct sockaddr **addrs, 382 const struct sockaddr **addrs,
381 const socklen_t *addrlens, 383 const socklen_t *addrlens,
382 GNUNET_NAT_AddressCallback address_callback, 384 GNUNET_NAT_AddressCallback address_callback,
383 GNUNET_NAT_ReversalCallback reversal_callback, 385 GNUNET_NAT_ReversalCallback reversal_callback,
384 void *callback_cls) 386 void *callback_cls)
385{ 387{
386 struct GNUNET_NAT_Handle *nh; 388 struct GNUNET_NAT_Handle *nh;
387 struct GNUNET_NAT_RegisterMessage *rm; 389 struct GNUNET_NAT_RegisterMessage *rm;
@@ -392,75 +394,75 @@ GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg,
392 len = 0; 394 len = 0;
393 for (unsigned int i = 0; i < num_addrs; i++) 395 for (unsigned int i = 0; i < num_addrs; i++)
394 len += addrlens[i]; 396 len += addrlens[i];
395 str_len = strlen(config_section) + 1; 397 str_len = strlen (config_section) + 1;
396 len += str_len; 398 len += str_len;
397 if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) || 399 if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) ||
398 (num_addrs > UINT16_MAX)) 400 (num_addrs > UINT16_MAX))
399 { 401 {
400 GNUNET_break(0); 402 GNUNET_break (0);
401 return NULL; 403 return NULL;
402 } 404 }
403 rm = GNUNET_malloc(sizeof(*rm) + len); 405 rm = GNUNET_malloc (sizeof(*rm) + len);
404 rm->header.size = htons(sizeof(*rm) + len); 406 rm->header.size = htons (sizeof(*rm) + len);
405 rm->header.type = htons(GNUNET_MESSAGE_TYPE_NAT_REGISTER); 407 rm->header.type = htons (GNUNET_MESSAGE_TYPE_NAT_REGISTER);
406 rm->flags = GNUNET_NAT_RF_NONE; 408 rm->flags = GNUNET_NAT_RF_NONE;
407 if (NULL != address_callback) 409 if (NULL != address_callback)
408 rm->flags |= GNUNET_NAT_RF_ADDRESSES; 410 rm->flags |= GNUNET_NAT_RF_ADDRESSES;
409 if (NULL != reversal_callback) 411 if (NULL != reversal_callback)
410 rm->flags |= GNUNET_NAT_RF_REVERSAL; 412 rm->flags |= GNUNET_NAT_RF_REVERSAL;
411 rm->proto = proto; 413 rm->proto = proto;
412 rm->str_len = htons(str_len); 414 rm->str_len = htons (str_len);
413 rm->num_addrs = htons((uint16_t)num_addrs); 415 rm->num_addrs = htons ((uint16_t) num_addrs);
414 off = (char *)&rm[1]; 416 off = (char *) &rm[1];
415 for (unsigned int i = 0; i < num_addrs; i++) 417 for (unsigned int i = 0; i < num_addrs; i++)
418 {
419 switch (addrs[i]->sa_family)
416 { 420 {
417 switch (addrs[i]->sa_family) 421 case AF_INET:
418 { 422 if (sizeof(struct sockaddr_in) != addrlens[i])
419 case AF_INET: 423 {
420 if (sizeof(struct sockaddr_in) != addrlens[i]) 424 GNUNET_break (0);
421 { 425 GNUNET_free (rm);
422 GNUNET_break(0); 426 return NULL;
423 GNUNET_free(rm); 427 }
424 return NULL; 428 break;
425 } 429
426 break; 430 case AF_INET6:
427 431 if (sizeof(struct sockaddr_in6) != addrlens[i])
428 case AF_INET6: 432 {
429 if (sizeof(struct sockaddr_in6) != addrlens[i]) 433 GNUNET_break (0);
430 { 434 GNUNET_free (rm);
431 GNUNET_break(0); 435 return NULL;
432 GNUNET_free(rm); 436 }
433 return NULL; 437 break;
434 }
435 break;
436 438
437#if AF_UNIX 439#if AF_UNIX
438 case AF_UNIX: 440 case AF_UNIX:
439 if (sizeof(struct sockaddr_un) != addrlens[i]) 441 if (sizeof(struct sockaddr_un) != addrlens[i])
440 { 442 {
441 GNUNET_break(0); 443 GNUNET_break (0);
442 GNUNET_free(rm); 444 GNUNET_free (rm);
443 return NULL; 445 return NULL;
444 } 446 }
445 break; 447 break;
446#endif 448#endif
447 default: 449 default:
448 GNUNET_break(0); 450 GNUNET_break (0);
449 GNUNET_free(rm); 451 GNUNET_free (rm);
450 return NULL; 452 return NULL;
451 }
452 GNUNET_memcpy(off, addrs[i], addrlens[i]);
453 off += addrlens[i];
454 } 453 }
455 GNUNET_memcpy(off, config_section, str_len); 454 GNUNET_memcpy (off, addrs[i], addrlens[i]);
455 off += addrlens[i];
456 }
457 GNUNET_memcpy (off, config_section, str_len);
456 458
457 nh = GNUNET_new(struct GNUNET_NAT_Handle); 459 nh = GNUNET_new (struct GNUNET_NAT_Handle);
458 nh->reg = &rm->header; 460 nh->reg = &rm->header;
459 nh->cfg = cfg; 461 nh->cfg = cfg;
460 nh->address_callback = address_callback; 462 nh->address_callback = address_callback;
461 nh->reversal_callback = reversal_callback; 463 nh->reversal_callback = reversal_callback;
462 nh->callback_cls = callback_cls; 464 nh->callback_cls = callback_cls;
463 do_connect(nh); 465 do_connect (nh);
464 return nh; 466 return nh;
465} 467}
466 468
@@ -474,7 +476,7 @@ GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg,
474 * #GNUNET_NO if the packet is invalid (not a stun packet) 476 * #GNUNET_NO if the packet is invalid (not a stun packet)
475 */ 477 */
476static int 478static int
477test_stun_packet(const void *data, size_t len) 479test_stun_packet (const void *data, size_t len)
478{ 480{
479 const struct stun_header *hdr; 481 const struct stun_header *hdr;
480 const struct stun_attr *attr; 482 const struct stun_attr *attr;
@@ -486,70 +488,70 @@ test_stun_packet(const void *data, size_t len)
486 * while 'data' is advanced accordingly. 488 * while 'data' is advanced accordingly.
487 */ 489 */
488 if (len < sizeof(struct stun_header)) 490 if (len < sizeof(struct stun_header))
489 { 491 {
490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "STUN packet too short (only %d, wanting at least %d)\n", 493 "STUN packet too short (only %d, wanting at least %d)\n",
492 (int)len, 494 (int) len,
493 (int)sizeof(struct stun_header)); 495 (int) sizeof(struct stun_header));
494 return GNUNET_NO; 496 return GNUNET_NO;
495 } 497 }
496 hdr = (const struct stun_header *)data; 498 hdr = (const struct stun_header *) data;
497 /* Skip header as it is already in hdr */ 499 /* Skip header as it is already in hdr */
498 len -= sizeof(struct stun_header); 500 len -= sizeof(struct stun_header);
499 data += sizeof(struct stun_header); 501 data += sizeof(struct stun_header);
500 502
501 /* len as advertised in the message */ 503 /* len as advertised in the message */
502 advertised_message_size = ntohs(hdr->msglen); 504 advertised_message_size = ntohs (hdr->msglen);
503 505
504 message_magic_cookie = ntohl(hdr->magic); 506 message_magic_cookie = ntohl (hdr->magic);
505 /* Compare if the cookie match */ 507 /* Compare if the cookie match */
506 if (STUN_MAGIC_COOKIE != message_magic_cookie) 508 if (STUN_MAGIC_COOKIE != message_magic_cookie)
507 { 509 {
508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n"); 510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n");
509 return GNUNET_NO; 511 return GNUNET_NO;
510 } 512 }
511 513
512 if (advertised_message_size > len) 514 if (advertised_message_size > len)
515 {
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "Scrambled STUN packet length (got %d, expecting %d)\n",
518 advertised_message_size,
519 (int) len);
520 return GNUNET_NO;
521 }
522 len = advertised_message_size;
523 while (len > 0)
524 {
525 if (len < sizeof(struct stun_attr))
513 { 526 {
514 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Scrambled STUN packet length (got %d, expecting %d)\n", 528 "Attribute too short in STUN packet (got %d, expecting %d)\n",
516 advertised_message_size, 529 (int) len,
517 (int)len); 530 (int) sizeof(struct stun_attr));
518 return GNUNET_NO; 531 return GNUNET_NO;
519 } 532 }
520 len = advertised_message_size; 533 attr = (const struct stun_attr *) data;
521 while (len > 0) 534
535 /* compute total attribute length */
536 advertised_message_size = ntohs (attr->len) + sizeof(struct stun_attr);
537
538 /* Check if we still have space in our buffer */
539 if (advertised_message_size > len)
522 { 540 {
523 if (len < sizeof(struct stun_attr)) 541 GNUNET_log (
524 { 542 GNUNET_ERROR_TYPE_DEBUG,
525 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 543 "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n",
526 "Attribute too short in STUN packet (got %d, expecting %d)\n", 544 advertised_message_size,
527 (int)len, 545 (int) len);
528 (int)sizeof(struct stun_attr)); 546 return GNUNET_NO;
529 return GNUNET_NO;
530 }
531 attr = (const struct stun_attr *)data;
532
533 /* compute total attribute length */
534 advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr);
535
536 /* Check if we still have space in our buffer */
537 if (advertised_message_size > len)
538 {
539 GNUNET_log(
540 GNUNET_ERROR_TYPE_DEBUG,
541 "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n",
542 advertised_message_size,
543 (int)len);
544 return GNUNET_NO;
545 }
546 data += advertised_message_size;
547 len -= advertised_message_size;
548 } 547 }
549 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 548 data += advertised_message_size;
550 "STUN Packet, msg %04x, length: %d\n", 549 len -= advertised_message_size;
551 ntohs(hdr->msgtype), 550 }
552 advertised_message_size); 551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "STUN Packet, msg %04x, length: %d\n",
553 ntohs (hdr->msgtype),
554 advertised_message_size);
553 return GNUNET_OK; 555 return GNUNET_OK;
554} 556}
555 557
@@ -578,30 +580,30 @@ test_stun_packet(const void *data, size_t len)
578 * #GNUNET_SYSERR on internal error handling the packet 580 * #GNUNET_SYSERR on internal error handling the packet
579 */ 581 */
580int 582int
581GNUNET_NAT_stun_handle_packet(struct GNUNET_NAT_Handle *nh, 583GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
582 const struct sockaddr *sender_addr, 584 const struct sockaddr *sender_addr,
583 size_t sender_addr_len, 585 size_t sender_addr_len,
584 const void *data, 586 const void *data,
585 size_t data_size) 587 size_t data_size)
586{ 588{
587 struct GNUNET_MQ_Envelope *env; 589 struct GNUNET_MQ_Envelope *env;
588 struct GNUNET_NAT_HandleStunMessage *hsn; 590 struct GNUNET_NAT_HandleStunMessage *hsn;
589 char *buf; 591 char *buf;
590 592
591 if (GNUNET_YES != test_stun_packet(data, data_size)) 593 if (GNUNET_YES != test_stun_packet (data, data_size))
592 return GNUNET_NO; 594 return GNUNET_NO;
593 if (NULL == nh->mq) 595 if (NULL == nh->mq)
594 return GNUNET_SYSERR; 596 return GNUNET_SYSERR;
595 env = GNUNET_MQ_msg_extra(hsn, 597 env = GNUNET_MQ_msg_extra (hsn,
596 data_size + sender_addr_len, 598 data_size + sender_addr_len,
597 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN); 599 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN);
598 hsn->sender_addr_size = htons((uint16_t)sender_addr_len); 600 hsn->sender_addr_size = htons ((uint16_t) sender_addr_len);
599 hsn->payload_size = htons((uint16_t)data_size); 601 hsn->payload_size = htons ((uint16_t) data_size);
600 buf = (char *)&hsn[1]; 602 buf = (char *) &hsn[1];
601 GNUNET_memcpy(buf, sender_addr, sender_addr_len); 603 GNUNET_memcpy (buf, sender_addr, sender_addr_len);
602 buf += sender_addr_len; 604 buf += sender_addr_len;
603 GNUNET_memcpy(buf, data, data_size); 605 GNUNET_memcpy (buf, data, data_size);
604 GNUNET_MQ_send(nh->mq, env); 606 GNUNET_MQ_send (nh->mq, env);
605 return GNUNET_OK; 607 return GNUNET_OK;
606} 608}
607 609
@@ -620,20 +622,20 @@ GNUNET_NAT_stun_handle_packet(struct GNUNET_NAT_Handle *nh,
620 * #GNUNET_SYSERR if the address is malformed 622 * #GNUNET_SYSERR if the address is malformed
621 */ 623 */
622int 624int
623GNUNET_NAT_test_address(struct GNUNET_NAT_Handle *nh, 625GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
624 const void *addr, 626 const void *addr,
625 socklen_t addrlen) 627 socklen_t addrlen)
626{ 628{
627 struct AddrEntry *ae; 629 struct AddrEntry *ae;
628 630
629 if ((addrlen != sizeof(struct sockaddr_in)) && 631 if ((addrlen != sizeof(struct sockaddr_in)) &&
630 (addrlen != sizeof(struct sockaddr_in6))) 632 (addrlen != sizeof(struct sockaddr_in6)))
631 { 633 {
632 GNUNET_break(0); 634 GNUNET_break (0);
633 return GNUNET_SYSERR; 635 return GNUNET_SYSERR;
634 } 636 }
635 for (ae = nh->ae_head; NULL != ae; ae = ae->next) 637 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
636 if ((addrlen == ae->addrlen) && (0 == memcmp(addr, &ae[1], addrlen))) 638 if ((addrlen == ae->addrlen) && (0 == memcmp (addr, &ae[1], addrlen)))
637 return GNUNET_YES; 639 return GNUNET_YES;
638 return GNUNET_NO; 640 return GNUNET_NO;
639} 641}
@@ -652,9 +654,9 @@ GNUNET_NAT_test_address(struct GNUNET_NAT_Handle *nh,
652 * #GNUNET_OK otherwise (presumably in progress) 654 * #GNUNET_OK otherwise (presumably in progress)
653 */ 655 */
654int 656int
655GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, 657GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
656 const struct sockaddr_in *local_sa, 658 const struct sockaddr_in *local_sa,
657 const struct sockaddr_in *remote_sa) 659 const struct sockaddr_in *remote_sa)
658{ 660{
659 struct GNUNET_MQ_Envelope *env; 661 struct GNUNET_MQ_Envelope *env;
660 struct GNUNET_NAT_RequestConnectionReversalMessage *req; 662 struct GNUNET_NAT_RequestConnectionReversalMessage *req;
@@ -662,19 +664,19 @@ GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh,
662 664
663 if (NULL == nh->mq) 665 if (NULL == nh->mq)
664 return GNUNET_SYSERR; 666 return GNUNET_SYSERR;
665 GNUNET_break(AF_INET == local_sa->sin_family); 667 GNUNET_break (AF_INET == local_sa->sin_family);
666 GNUNET_break(AF_INET == remote_sa->sin_family); 668 GNUNET_break (AF_INET == remote_sa->sin_family);
667 env = 669 env =
668 GNUNET_MQ_msg_extra(req, 670 GNUNET_MQ_msg_extra (req,
669 2 * sizeof(struct sockaddr_in), 671 2 * sizeof(struct sockaddr_in),
670 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL); 672 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL);
671 req->local_addr_size = htons(sizeof(struct sockaddr_in)); 673 req->local_addr_size = htons (sizeof(struct sockaddr_in));
672 req->remote_addr_size = htons(sizeof(struct sockaddr_in)); 674 req->remote_addr_size = htons (sizeof(struct sockaddr_in));
673 buf = (char *)&req[1]; 675 buf = (char *) &req[1];
674 GNUNET_memcpy(buf, local_sa, sizeof(struct sockaddr_in)); 676 GNUNET_memcpy (buf, local_sa, sizeof(struct sockaddr_in));
675 buf += sizeof(struct sockaddr_in); 677 buf += sizeof(struct sockaddr_in);
676 GNUNET_memcpy(buf, remote_sa, sizeof(struct sockaddr_in)); 678 GNUNET_memcpy (buf, remote_sa, sizeof(struct sockaddr_in));
677 GNUNET_MQ_send(nh->mq, env); 679 GNUNET_MQ_send (nh->mq, env);
678 return GNUNET_OK; 680 return GNUNET_OK;
679} 681}
680 682
@@ -687,20 +689,20 @@ GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh,
687 * @param nh the handle to stop 689 * @param nh the handle to stop
688 */ 690 */
689void 691void
690GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh) 692GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh)
691{ 693{
692 if (NULL != nh->mq) 694 if (NULL != nh->mq)
693 { 695 {
694 GNUNET_MQ_destroy(nh->mq); 696 GNUNET_MQ_destroy (nh->mq);
695 nh->mq = NULL; 697 nh->mq = NULL;
696 } 698 }
697 if (NULL != nh->reconnect_task) 699 if (NULL != nh->reconnect_task)
698 { 700 {
699 GNUNET_SCHEDULER_cancel(nh->reconnect_task); 701 GNUNET_SCHEDULER_cancel (nh->reconnect_task);
700 nh->reconnect_task = NULL; 702 nh->reconnect_task = NULL;
701 } 703 }
702 GNUNET_free(nh->reg); 704 GNUNET_free (nh->reg);
703 GNUNET_free(nh); 705 GNUNET_free (nh);
704} 706}
705 707
706 708
diff --git a/src/nat/nat_api_stun.c b/src/nat/nat_api_stun.c
index 0f8694867..461dae1f5 100644
--- a/src/nat/nat_api_stun.c
+++ b/src/nat/nat_api_stun.c
@@ -43,9 +43,9 @@
43 43
44#include "nat_stun.h" 44#include "nat_stun.h"
45 45
46#define LOG(kind, ...) GNUNET_log_from(kind, "stun", __VA_ARGS__) 46#define LOG(kind, ...) GNUNET_log_from (kind, "stun", __VA_ARGS__)
47 47
48#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) 48#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
49 49
50 50
51/** 51/**
@@ -53,7 +53,8 @@
53 * the request prior to the timeout or successful execution. Also 53 * the request prior to the timeout or successful execution. Also
54 * used to track our internal state for the request. 54 * used to track our internal state for the request.
55 */ 55 */
56struct GNUNET_NAT_STUN_Handle { 56struct GNUNET_NAT_STUN_Handle
57{
57 /** 58 /**
58 * Handle to a pending DNS lookup request. 59 * Handle to a pending DNS lookup request.
59 */ 60 */
@@ -99,11 +100,12 @@ struct GNUNET_NAT_STUN_Handle {
99 * @return message in a STUN compatible format 100 * @return message in a STUN compatible format
100 */ 101 */
101static int 102static int
102encode_message(enum StunClasses msg_class, 103encode_message (enum StunClasses msg_class,
103 enum StunMethods method) 104 enum StunMethods method)
104{ 105{
105 return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) | 106 return ((msg_class & 1) << 4) | ((msg_class & 2) << 7)
106 (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2); 107 | (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800)
108 << 2);
107} 109}
108 110
109 111
@@ -113,12 +115,12 @@ encode_message(enum StunClasses msg_class,
113 * @param req, stun header to be filled 115 * @param req, stun header to be filled
114 */ 116 */
115static void 117static void
116generate_request_id(struct stun_header *req) 118generate_request_id (struct stun_header *req)
117{ 119{
118 req->magic = htonl(STUN_MAGIC_COOKIE); 120 req->magic = htonl (STUN_MAGIC_COOKIE);
119 for (unsigned int x = 0; x < 3; x++) 121 for (unsigned int x = 0; x < 3; x++)
120 req->id.id[x] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 122 req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
121 UINT32_MAX); 123 UINT32_MAX);
122} 124}
123 125
124 126
@@ -130,67 +132,67 @@ generate_request_id(struct stun_header *req)
130 * @param addrlen length of @a addr 132 * @param addrlen length of @a addr
131 */ 133 */
132static void 134static void
133stun_dns_callback(void *cls, 135stun_dns_callback (void *cls,
134 const struct sockaddr *addr, 136 const struct sockaddr *addr,
135 socklen_t addrlen) 137 socklen_t addrlen)
136{ 138{
137 struct GNUNET_NAT_STUN_Handle *rh = cls; 139 struct GNUNET_NAT_STUN_Handle *rh = cls;
138 struct stun_header req; 140 struct stun_header req;
139 struct sockaddr_in server; 141 struct sockaddr_in server;
140 142
141 if (NULL == addr) 143 if (NULL == addr)
144 {
145 rh->dns_active = NULL;
146 if (GNUNET_NO == rh->dns_success)
147 {
148 LOG (GNUNET_ERROR_TYPE_INFO,
149 "Error resolving host %s\n",
150 rh->stun_server);
151 rh->cb (rh->cb_cls,
152 GNUNET_NAT_ERROR_NOT_ONLINE);
153 }
154 else if (GNUNET_SYSERR == rh->dns_success)
142 { 155 {
143 rh->dns_active = NULL; 156 rh->cb (rh->cb_cls,
144 if (GNUNET_NO == rh->dns_success) 157 GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR);
145 {
146 LOG(GNUNET_ERROR_TYPE_INFO,
147 "Error resolving host %s\n",
148 rh->stun_server);
149 rh->cb(rh->cb_cls,
150 GNUNET_NAT_ERROR_NOT_ONLINE);
151 }
152 else if (GNUNET_SYSERR == rh->dns_success)
153 {
154 rh->cb(rh->cb_cls,
155 GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR);
156 }
157 else
158 {
159 rh->cb(rh->cb_cls,
160 GNUNET_NAT_ERROR_SUCCESS);
161 }
162 GNUNET_NAT_stun_make_request_cancel(rh);
163 return;
164 } 158 }
159 else
160 {
161 rh->cb (rh->cb_cls,
162 GNUNET_NAT_ERROR_SUCCESS);
163 }
164 GNUNET_NAT_stun_make_request_cancel (rh);
165 return;
166 }
165 167
166 rh->dns_success = GNUNET_YES; 168 rh->dns_success = GNUNET_YES;
167 memset(&server, 0, sizeof(server)); 169 memset (&server, 0, sizeof(server));
168 server.sin_family = AF_INET; 170 server.sin_family = AF_INET;
169 server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr; 171 server.sin_addr = ((struct sockaddr_in *) addr)->sin_addr;
170 server.sin_port = htons(rh->stun_port); 172 server.sin_port = htons (rh->stun_port);
171#if HAVE_SOCKADDR_IN_SIN_LEN 173#if HAVE_SOCKADDR_IN_SIN_LEN
172 server.sin_len = (u_char)sizeof(struct sockaddr_in); 174 server.sin_len = (u_char) sizeof(struct sockaddr_in);
173#endif 175#endif
174 176
175 /* Craft the simplest possible STUN packet. A request binding */ 177 /* Craft the simplest possible STUN packet. A request binding */
176 generate_request_id(&req); 178 generate_request_id (&req);
177 req.msglen = htons(0); 179 req.msglen = htons (0);
178 req.msgtype = htons(encode_message(STUN_REQUEST, 180 req.msgtype = htons (encode_message (STUN_REQUEST,
179 STUN_BINDING)); 181 STUN_BINDING));
180 182
181 /* Send the packet */ 183 /* Send the packet */
182 if (-1 == 184 if (-1 ==
183 GNUNET_NETWORK_socket_sendto(rh->sock, 185 GNUNET_NETWORK_socket_sendto (rh->sock,
184 &req, 186 &req,
185 sizeof(req), 187 sizeof(req),
186 (const struct sockaddr *)&server, 188 (const struct sockaddr *) &server,
187 sizeof(server))) 189 sizeof(server)))
188 { 190 {
189 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, 191 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
190 "sendto"); 192 "sendto");
191 rh->dns_success = GNUNET_SYSERR; 193 rh->dns_success = GNUNET_SYSERR;
192 return; 194 return;
193 } 195 }
194} 196}
195 197
196 198
@@ -206,31 +208,31 @@ stun_dns_callback(void *cls,
206 * @return NULL on error 208 * @return NULL on error
207 */ 209 */
208struct GNUNET_NAT_STUN_Handle * 210struct GNUNET_NAT_STUN_Handle *
209GNUNET_NAT_stun_make_request(const char *server, 211GNUNET_NAT_stun_make_request (const char *server,
210 uint16_t port, 212 uint16_t port,
211 struct GNUNET_NETWORK_Handle *sock, 213 struct GNUNET_NETWORK_Handle *sock,
212 GNUNET_NAT_TestCallback cb, 214 GNUNET_NAT_TestCallback cb,
213 void *cb_cls) 215 void *cb_cls)
214{ 216{
215 struct GNUNET_NAT_STUN_Handle *rh; 217 struct GNUNET_NAT_STUN_Handle *rh;
216 218
217 rh = GNUNET_new(struct GNUNET_NAT_STUN_Handle); 219 rh = GNUNET_new (struct GNUNET_NAT_STUN_Handle);
218 rh->sock = sock; 220 rh->sock = sock;
219 rh->cb = cb; 221 rh->cb = cb;
220 rh->cb_cls = cb_cls; 222 rh->cb_cls = cb_cls;
221 rh->stun_server = GNUNET_strdup(server); 223 rh->stun_server = GNUNET_strdup (server);
222 rh->stun_port = port; 224 rh->stun_port = port;
223 rh->dns_success = GNUNET_NO; 225 rh->dns_success = GNUNET_NO;
224 rh->dns_active = GNUNET_RESOLVER_ip_get(rh->stun_server, 226 rh->dns_active = GNUNET_RESOLVER_ip_get (rh->stun_server,
225 AF_INET, 227 AF_INET,
226 TIMEOUT, 228 TIMEOUT,
227 &stun_dns_callback, 229 &stun_dns_callback,
228 rh); 230 rh);
229 if (NULL == rh->dns_active) 231 if (NULL == rh->dns_active)
230 { 232 {
231 GNUNET_NAT_stun_make_request_cancel(rh); 233 GNUNET_NAT_stun_make_request_cancel (rh);
232 return NULL; 234 return NULL;
233 } 235 }
234 return rh; 236 return rh;
235} 237}
236 238
@@ -242,15 +244,15 @@ GNUNET_NAT_stun_make_request(const char *server,
242 * @param rh request to cancel 244 * @param rh request to cancel
243 */ 245 */
244void 246void
245GNUNET_NAT_stun_make_request_cancel(struct GNUNET_NAT_STUN_Handle *rh) 247GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh)
246{ 248{
247 if (NULL != rh->dns_active) 249 if (NULL != rh->dns_active)
248 { 250 {
249 GNUNET_RESOLVER_request_cancel(rh->dns_active); 251 GNUNET_RESOLVER_request_cancel (rh->dns_active);
250 rh->dns_active = NULL; 252 rh->dns_active = NULL;
251 } 253 }
252 GNUNET_free(rh->stun_server); 254 GNUNET_free (rh->stun_server);
253 GNUNET_free(rh); 255 GNUNET_free (rh);
254} 256}
255 257
256 258
diff --git a/src/nat/nat_stun.h b/src/nat/nat_stun.h
index 85b177271..86b4bd0cb 100644
--- a/src/nat/nat_stun.h
+++ b/src/nat/nat_stun.h
@@ -33,12 +33,14 @@
33 33
34#define STUN_MAGIC_COOKIE 0x2112A442 34#define STUN_MAGIC_COOKIE 0x2112A442
35 35
36typedef struct { 36typedef struct
37{
37 uint32_t id[3]; 38 uint32_t id[3];
38} GNUNET_PACKED stun_trans_id; 39} GNUNET_PACKED stun_trans_id;
39 40
40 41
41struct stun_header { 42struct stun_header
43{
42 uint16_t msgtype; 44 uint16_t msgtype;
43 uint16_t msglen; 45 uint16_t msglen;
44 uint32_t magic; 46 uint32_t magic;
@@ -46,7 +48,8 @@ struct stun_header {
46} GNUNET_PACKED; 48} GNUNET_PACKED;
47 49
48 50
49struct stun_attr { 51struct stun_attr
52{
50 uint16_t attr; 53 uint16_t attr;
51 uint16_t len; 54 uint16_t len;
52} GNUNET_PACKED; 55} GNUNET_PACKED;
@@ -55,7 +58,8 @@ struct stun_attr {
55/** 58/**
56 * The format normally used for addresses carried by STUN messages. 59 * The format normally used for addresses carried by STUN messages.
57 */ 60 */
58struct stun_addr { 61struct stun_addr
62{
59 uint8_t unused; 63 uint8_t unused;
60 64
61 /** 65 /**
@@ -78,7 +82,8 @@ struct stun_addr {
78/** 82/**
79 * STUN message classes 83 * STUN message classes
80 */ 84 */
81enum StunClasses { 85enum StunClasses
86{
82 INVALID_CLASS = 0, 87 INVALID_CLASS = 0,
83 STUN_REQUEST = 0x0000, 88 STUN_REQUEST = 0x0000,
84 STUN_INDICATION = 0x0001, 89 STUN_INDICATION = 0x0001,
@@ -86,7 +91,8 @@ enum StunClasses {
86 STUN_ERROR_RESPONSE = 0x0003 91 STUN_ERROR_RESPONSE = 0x0003
87}; 92};
88 93
89enum StunMethods { 94enum StunMethods
95{
90 INVALID_METHOD = 0, 96 INVALID_METHOD = 0,
91 STUN_BINDING = 0x0001, 97 STUN_BINDING = 0x0001,
92 STUN_SHARED_SECRET = 0x0002, 98 STUN_SHARED_SECRET = 0x0002,
@@ -103,7 +109,8 @@ enum StunMethods {
103 * Basic attribute types in stun messages. 109 * Basic attribute types in stun messages.
104 * Messages can also contain custom attributes (codes above 0x7fff) 110 * Messages can also contain custom attributes (codes above 0x7fff)
105 */ 111 */
106enum StunAttributes { 112enum StunAttributes
113{
107 STUN_MAPPED_ADDRESS = 0x0001, 114 STUN_MAPPED_ADDRESS = 0x0001,
108 STUN_RESPONSE_ADDRESS = 0x0002, 115 STUN_RESPONSE_ADDRESS = 0x0002,
109 STUN_CHANGE_ADDRESS = 0x0003, 116 STUN_CHANGE_ADDRESS = 0x0003,
@@ -133,10 +140,10 @@ enum StunAttributes {
133 * @return the converted StunClass 140 * @return the converted StunClass
134 */ 141 */
135static enum StunClasses 142static enum StunClasses
136decode_class(int msg) 143decode_class (int msg)
137{ 144{
138 /* Sorry for the magic, but this maps the class according to rfc5245 */ 145 /* Sorry for the magic, but this maps the class according to rfc5245 */
139 return (enum StunClasses)((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7); 146 return (enum StunClasses) ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7);
140} 147}
141 148
142 149
@@ -147,9 +154,11 @@ decode_class(int msg)
147 * @return the converted StunMethod 154 * @return the converted StunMethod
148 */ 155 */
149static enum StunMethods 156static enum StunMethods
150decode_method(int msg) 157decode_method (int msg)
151{ 158{
152 return (enum StunMethods)(msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2); 159 return (enum StunMethods) (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg
160 & 0x3e00)
161 >> 2);
153} 162}
154 163
155 164
@@ -161,9 +170,10 @@ decode_method(int msg)
161 */ 170 */
162GNUNET_UNUSED 171GNUNET_UNUSED
163static const char * 172static const char *
164stun_msg2str(int msg) 173stun_msg2str (int msg)
165{ 174{
166 static const struct { 175 static const struct
176 {
167 enum StunClasses value; 177 enum StunClasses value;
168 const char *name; 178 const char *name;
169 } classes[] = { 179 } classes[] = {
@@ -173,7 +183,8 @@ stun_msg2str(int msg)
173 { STUN_ERROR_RESPONSE, "Error Response" }, 183 { STUN_ERROR_RESPONSE, "Error Response" },
174 { INVALID_CLASS, NULL } 184 { INVALID_CLASS, NULL }
175 }; 185 };
176 static const struct { 186 static const struct
187 {
177 enum StunMethods value; 188 enum StunMethods value;
178 const char *name; 189 const char *name;
179 } methods[] = { 190 } methods[] = {
@@ -186,25 +197,25 @@ stun_msg2str(int msg)
186 enum StunClasses cvalue; 197 enum StunClasses cvalue;
187 enum StunMethods mvalue; 198 enum StunMethods mvalue;
188 199
189 cvalue = decode_class(msg); 200 cvalue = decode_class (msg);
190 for (unsigned int i = 0; classes[i].name; i++) 201 for (unsigned int i = 0; classes[i].name; i++)
191 if (classes[i].value == cvalue) 202 if (classes[i].value == cvalue)
192 { 203 {
193 msg_class = classes[i].name; 204 msg_class = classes[i].name;
194 break; 205 break;
195 } 206 }
196 mvalue = decode_method(msg); 207 mvalue = decode_method (msg);
197 for (unsigned int i = 0; methods[i].name; i++) 208 for (unsigned int i = 0; methods[i].name; i++)
198 if (methods[i].value == mvalue) 209 if (methods[i].value == mvalue)
199 { 210 {
200 method = methods[i].name; 211 method = methods[i].name;
201 break; 212 break;
202 } 213 }
203 GNUNET_snprintf(result, 214 GNUNET_snprintf (result,
204 sizeof(result), 215 sizeof(result),
205 "%s %s", 216 "%s %s",
206 method ? : "Unknown Method", 217 method ? : "Unknown Method",
207 msg_class ? : "Unknown Class Message"); 218 msg_class ? : "Unknown Class Message");
208 return result; 219 return result;
209} 220}
210 221
@@ -217,9 +228,10 @@ stun_msg2str(int msg)
217 */ 228 */
218GNUNET_UNUSED 229GNUNET_UNUSED
219static const char * 230static const char *
220stun_attr2str(enum StunAttributes msg) 231stun_attr2str (enum StunAttributes msg)
221{ 232{
222 static const struct { 233 static const struct
234 {
223 enum StunAttributes value; 235 enum StunAttributes value;
224 const char *name; 236 const char *name;
225 } attrs[] = { 237 } attrs[] = {
diff --git a/src/nat/test_nat.c b/src/nat/test_nat.c
index 5758f63b9..f04cf5669 100644
--- a/src/nat/test_nat.c
+++ b/src/nat/test_nat.c
@@ -43,7 +43,7 @@
43/** 43/**
44 * Time to wait before stopping NAT, in seconds 44 * Time to wait before stopping NAT, in seconds
45 */ 45 */
46#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) 46#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
47 47
48 48
49/** 49/**
@@ -51,15 +51,15 @@
51 * believes to be valid for the transport. 51 * believes to be valid for the transport.
52 */ 52 */
53static void 53static void
54addr_callback(void *cls, int add_remove, const struct sockaddr *addr, 54addr_callback (void *cls, int add_remove, const struct sockaddr *addr,
55 socklen_t addrlen) 55 socklen_t addrlen)
56{ 56{
57 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 57 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
58 "Address changed: %s `%s' (%u bytes)\n", 58 "Address changed: %s `%s' (%u bytes)\n",
59 add_remove == GNUNET_YES ? "added" : "removed", 59 add_remove == GNUNET_YES ? "added" : "removed",
60 GNUNET_a2s(addr, 60 GNUNET_a2s (addr,
61 addrlen), 61 addrlen),
62 (unsigned int)addrlen); 62 (unsigned int) addrlen);
63} 63}
64 64
65 65
@@ -67,17 +67,18 @@ addr_callback(void *cls, int add_remove, const struct sockaddr *addr,
67 * Function that terminates the test. 67 * Function that terminates the test.
68 */ 68 */
69static void 69static void
70stop(void *cls) 70stop (void *cls)
71{ 71{
72 struct GNUNET_NAT_Handle *nat = cls; 72 struct GNUNET_NAT_Handle *nat = cls;
73 73
74 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 74 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
75 "Stopping NAT and quitting...\n"); 75 "Stopping NAT and quitting...\n");
76 GNUNET_NAT_unregister(nat); 76 GNUNET_NAT_unregister (nat);
77} 77}
78 78
79 79
80struct addr_cls { 80struct addr_cls
81{
81 struct sockaddr *addr; 82 struct sockaddr *addr;
82 socklen_t addrlen; 83 socklen_t addrlen;
83}; 84};
@@ -95,21 +96,21 @@ struct addr_cls {
95 * @return #GNUNET_OK to continue iterating 96 * @return #GNUNET_OK to continue iterating
96 */ 97 */
97static int 98static int
98process_if(void *cls, 99process_if (void *cls,
99 const char *name, 100 const char *name,
100 int isDefault, 101 int isDefault,
101 const struct sockaddr *addr, 102 const struct sockaddr *addr,
102 const struct sockaddr *broadcast_addr, 103 const struct sockaddr *broadcast_addr,
103 const struct sockaddr *netmask, 104 const struct sockaddr *netmask,
104 socklen_t addrlen) 105 socklen_t addrlen)
105{ 106{
106 struct addr_cls *data = cls; 107 struct addr_cls *data = cls;
107 108
108 if (addr == NULL) 109 if (addr == NULL)
109 return GNUNET_OK; 110 return GNUNET_OK;
110 GNUNET_free_non_null(data->addr); 111 GNUNET_free_non_null (data->addr);
111 data->addr = GNUNET_malloc(addrlen); 112 data->addr = GNUNET_malloc (addrlen);
112 GNUNET_memcpy(data->addr, addr, addrlen); 113 GNUNET_memcpy (data->addr, addr, addrlen);
113 data->addrlen = addrlen; 114 data->addrlen = addrlen;
114 if (isDefault) 115 if (isDefault)
115 return GNUNET_SYSERR; 116 return GNUNET_SYSERR;
@@ -121,44 +122,44 @@ process_if(void *cls,
121 * Main function run with scheduler. 122 * Main function run with scheduler.
122 */ 123 */
123static void 124static void
124run(void *cls, 125run (void *cls,
125 char *const *args, 126 char *const *args,
126 const char *cfgfile, 127 const char *cfgfile,
127 const struct GNUNET_CONFIGURATION_Handle *cfg) 128 const struct GNUNET_CONFIGURATION_Handle *cfg)
128{ 129{
129 struct GNUNET_NAT_Handle *nat; 130 struct GNUNET_NAT_Handle *nat;
130 struct addr_cls data; 131 struct addr_cls data;
131 struct sockaddr *addr; 132 struct sockaddr *addr;
132 133
133 data.addr = NULL; 134 data.addr = NULL;
134 GNUNET_OS_network_interfaces_list(process_if, &data); 135 GNUNET_OS_network_interfaces_list (process_if, &data);
135 if (NULL == data.addr) 136 if (NULL == data.addr)
136 { 137 {
137 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
138 "Could not find a valid interface address!\n"); 139 "Could not find a valid interface address!\n");
139 exit(77); /* marks test as skipped */ 140 exit (77); /* marks test as skipped */
140 } 141 }
141 addr = data.addr; 142 addr = data.addr;
142 GNUNET_assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6); 143 GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
143 if (addr->sa_family == AF_INET) 144 if (addr->sa_family == AF_INET)
144 ((struct sockaddr_in *)addr)->sin_port = htons(2086); 145 ((struct sockaddr_in *) addr)->sin_port = htons (2086);
145 else 146 else
146 ((struct sockaddr_in6 *)addr)->sin6_port = htons(2086); 147 ((struct sockaddr_in6 *) addr)->sin6_port = htons (2086);
147 148
148 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 149 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
149 "Requesting NAT redirection from address %s...\n", 150 "Requesting NAT redirection from address %s...\n",
150 GNUNET_a2s(addr, data.addrlen)); 151 GNUNET_a2s (addr, data.addrlen));
151 152
152 nat = GNUNET_NAT_register(cfg, GNUNET_YES /* tcp */, 153 nat = GNUNET_NAT_register (cfg, GNUNET_YES /* tcp */,
153 2086, 1, (const struct sockaddr **)&addr, 154 2086, 1, (const struct sockaddr **) &addr,
154 &data.addrlen, &addr_callback, NULL, NULL, NULL); 155 &data.addrlen, &addr_callback, NULL, NULL, NULL);
155 GNUNET_free(addr); 156 GNUNET_free (addr);
156 GNUNET_SCHEDULER_add_delayed(TIMEOUT, &stop, nat); 157 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat);
157} 158}
158 159
159 160
160int 161int
161main(int argc, char *const argv[]) 162main (int argc, char *const argv[])
162{ 163{
163 struct GNUNET_GETOPT_CommandLineOption options[] = { 164 struct GNUNET_GETOPT_CommandLineOption options[] = {
164 GNUNET_GETOPT_OPTION_END 165 GNUNET_GETOPT_OPTION_END
@@ -171,19 +172,19 @@ main(int argc, char *const argv[])
171 NULL 172 NULL
172 }; 173 };
173 174
174 GNUNET_log_setup("test-nat", 175 GNUNET_log_setup ("test-nat",
175 "WARNING", 176 "WARNING",
176 NULL); 177 NULL);
177 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 178 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
178 "Testing NAT library, timeout set to %s\n", 179 "Testing NAT library, timeout set to %s\n",
179 GNUNET_STRINGS_relative_time_to_string(TIMEOUT, 180 GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
180 GNUNET_YES)); 181 GNUNET_YES));
181 GNUNET_PROGRAM_run(3, 182 GNUNET_PROGRAM_run (3,
182 argv_prog, 183 argv_prog,
183 "test-nat", 184 "test-nat",
184 "nohelp", 185 "nohelp",
185 options, 186 options,
186 &run, NULL); 187 &run, NULL);
187 return 0; 188 return 0;
188} 189}
189 190
diff --git a/src/nat/test_nat_mini.c b/src/nat/test_nat_mini.c
index 09231d8ec..49c801571 100644
--- a/src/nat/test_nat_mini.c
+++ b/src/nat/test_nat_mini.c
@@ -35,31 +35,31 @@
35#include "gnunet_nat_lib.h" 35#include "gnunet_nat_lib.h"
36 36
37/* Time to wait before stopping NAT, in seconds */ 37/* Time to wait before stopping NAT, in seconds */
38#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) 38#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
39 39
40/** 40/**
41 * Function called on each address that the NAT service 41 * Function called on each address that the NAT service
42 * believes to be valid for the transport. 42 * believes to be valid for the transport.
43 */ 43 */
44static void 44static void
45addr_callback(void *cls, int add_remove, 45addr_callback (void *cls, int add_remove,
46 const struct sockaddr *addr, 46 const struct sockaddr *addr,
47 socklen_t addrlen, 47 socklen_t addrlen,
48 enum GNUNET_NAT_StatusCode ret) 48 enum GNUNET_NAT_StatusCode ret)
49{ 49{
50 if (GNUNET_NAT_ERROR_SUCCESS == ret) 50 if (GNUNET_NAT_ERROR_SUCCESS == ret)
51 { 51 {
52 fprintf(stderr, 52 fprintf (stderr,
53 "Address changed: %s `%s' (%u bytes)\n", 53 "Address changed: %s `%s' (%u bytes)\n",
54 add_remove == GNUNET_YES 54 add_remove == GNUNET_YES
55 ? "added" : "removed", 55 ? "added" : "removed",
56 GNUNET_a2s(addr, 56 GNUNET_a2s (addr,
57 addrlen), 57 addrlen),
58 (unsigned int)addrlen); 58 (unsigned int) addrlen);
59 } 59 }
60 else 60 else
61 ; 61 ;
62 //TODO: proper error handling! 62 // TODO: proper error handling!
63} 63}
64 64
65 65
@@ -67,12 +67,12 @@ addr_callback(void *cls, int add_remove,
67 * Function that terminates the test. 67 * Function that terminates the test.
68 */ 68 */
69static void 69static void
70stop(void *cls) 70stop (void *cls)
71{ 71{
72 struct GNUNET_NAT_MiniHandle *mini = cls; 72 struct GNUNET_NAT_MiniHandle *mini = cls;
73 73
74 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); 74 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
75 GNUNET_NAT_mini_map_stop(mini); 75 GNUNET_NAT_mini_map_stop (mini);
76} 76}
77 77
78#define PORT 10000 78#define PORT 10000
@@ -81,27 +81,27 @@ stop(void *cls)
81 * Main function run with scheduler. 81 * Main function run with scheduler.
82 */ 82 */
83static void 83static void
84run(void *cls, char *const *args, const char *cfgfile, 84run (void *cls, char *const *args, const char *cfgfile,
85 const struct GNUNET_CONFIGURATION_Handle *cfg) 85 const struct GNUNET_CONFIGURATION_Handle *cfg)
86{ 86{
87 struct GNUNET_NAT_MiniHandle *mini; 87 struct GNUNET_NAT_MiniHandle *mini;
88 88
89 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 89 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
90 "Requesting NAT redirection for port %u...\n", 90 "Requesting NAT redirection for port %u...\n",
91 PORT); 91 PORT);
92 mini = GNUNET_NAT_mini_map_start(PORT, GNUNET_YES /* tcp */, 92 mini = GNUNET_NAT_mini_map_start (PORT, GNUNET_YES /* tcp */,
93 &addr_callback, NULL); 93 &addr_callback, NULL);
94 if (NULL == mini) 94 if (NULL == mini)
95 { 95 {
96 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n"); 96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n");
97 return; 97 return;
98 } 98 }
99 GNUNET_SCHEDULER_add_delayed(TIMEOUT, &stop, mini); 99 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, mini);
100} 100}
101 101
102 102
103int 103int
104main(int argc, char *const argv[]) 104main (int argc, char *const argv[])
105{ 105{
106 struct GNUNET_GETOPT_CommandLineOption options[] = { 106 struct GNUNET_GETOPT_CommandLineOption options[] = {
107 GNUNET_GETOPT_OPTION_END 107 GNUNET_GETOPT_OPTION_END
@@ -116,16 +116,16 @@ main(int argc, char *const argv[])
116 NULL 116 NULL
117 }; 117 };
118 118
119 GNUNET_log_setup("test-nat-mini", 119 GNUNET_log_setup ("test-nat-mini",
120 "WARNING", 120 "WARNING",
121 NULL); 121 NULL);
122 122
123 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
124 "UPnP test for NAT library, timeout set to %s\n", 124 "UPnP test for NAT library, timeout set to %s\n",
125 GNUNET_STRINGS_relative_time_to_string(TIMEOUT, 125 GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
126 GNUNET_YES)); 126 GNUNET_YES));
127 GNUNET_PROGRAM_run(5, argv_prog, "test-nat-mini", "nohelp", options, &run, 127 GNUNET_PROGRAM_run (5, argv_prog, "test-nat-mini", "nohelp", options, &run,
128 NULL); 128 NULL);
129 return 0; 129 return 0;
130} 130}
131 131
diff --git a/src/nat/test_nat_test.c b/src/nat/test_nat_test.c
index e217c1d29..1eabab50c 100644
--- a/src/nat/test_nat_test.c
+++ b/src/nat/test_nat_test.c
@@ -29,43 +29,43 @@
29/** 29/**
30 * Time to wait before stopping NAT test, in seconds 30 * Time to wait before stopping NAT test, in seconds
31 */ 31 */
32#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) 32#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
33 33
34 34
35static int ret = 1; 35static int ret = 1;
36 36
37static struct GNUNET_NAT_Test *tst; 37static struct GNUNET_NAT_Test *tst;
38 38
39static struct GNUNET_SCHEDULER_Task * tsk; 39static struct GNUNET_SCHEDULER_Task *tsk;
40 40
41 41
42static void 42static void
43report_result(void *cls, 43report_result (void *cls,
44 enum GNUNET_NAT_StatusCode aret) 44 enum GNUNET_NAT_StatusCode aret)
45{ 45{
46 if (GNUNET_NAT_ERROR_TIMEOUT == aret) 46 if (GNUNET_NAT_ERROR_TIMEOUT == aret)
47 fprintf(stderr, 47 fprintf (stderr,
48 "NAT test timed out\n"); 48 "NAT test timed out\n");
49 else if (GNUNET_NAT_ERROR_SUCCESS != aret) 49 else if (GNUNET_NAT_ERROR_SUCCESS != aret)
50 fprintf(stderr, 50 fprintf (stderr,
51 "NAT test reported error %d\n", aret); 51 "NAT test reported error %d\n", aret);
52 else 52 else
53 ret = 0; 53 ret = 0;
54 GNUNET_NAT_test_stop(tst); 54 GNUNET_NAT_test_stop (tst);
55 tst = NULL; 55 tst = NULL;
56 GNUNET_SCHEDULER_cancel(tsk); 56 GNUNET_SCHEDULER_cancel (tsk);
57 tsk = NULL; 57 tsk = NULL;
58} 58}
59 59
60 60
61static void 61static void
62failed_timeout(void *cls) 62failed_timeout (void *cls)
63{ 63{
64 tsk = NULL; 64 tsk = NULL;
65 fprintf(stderr, 65 fprintf (stderr,
66 "NAT test failed to terminate on timeout\n"); 66 "NAT test failed to terminate on timeout\n");
67 ret = 2; 67 ret = 2;
68 GNUNET_NAT_test_stop(tst); 68 GNUNET_NAT_test_stop (tst);
69 tst = NULL; 69 tst = NULL;
70} 70}
71 71
@@ -74,22 +74,22 @@ failed_timeout(void *cls)
74 * Main function run with scheduler. 74 * Main function run with scheduler.
75 */ 75 */
76static void 76static void
77run(void *cls, char *const *args, const char *cfgfile, 77run (void *cls, char *const *args, const char *cfgfile,
78 const struct GNUNET_CONFIGURATION_Handle *cfg) 78 const struct GNUNET_CONFIGURATION_Handle *cfg)
79{ 79{
80 tst = 80 tst =
81 GNUNET_NAT_test_start(cfg, GNUNET_YES, 1285, 1285, TIMEOUT, 81 GNUNET_NAT_test_start (cfg, GNUNET_YES, 1285, 1285, TIMEOUT,
82 &report_result, 82 &report_result,
83 NULL); 83 NULL);
84 tsk = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(TIMEOUT, 84 tsk = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (TIMEOUT,
85 2), 85 2),
86 &failed_timeout, 86 &failed_timeout,
87 NULL); 87 NULL);
88} 88}
89 89
90 90
91int 91int
92main(int argc, char *const argv[]) 92main (int argc, char *const argv[])
93{ 93{
94 struct GNUNET_GETOPT_CommandLineOption options[] = { 94 struct GNUNET_GETOPT_CommandLineOption options[] = {
95 GNUNET_GETOPT_OPTION_END 95 GNUNET_GETOPT_OPTION_END
@@ -103,37 +103,38 @@ main(int argc, char *const argv[])
103 NULL 103 NULL
104 }; 104 };
105 105
106 GNUNET_log_setup("test-nat-test", 106 GNUNET_log_setup ("test-nat-test",
107 "WARNING", 107 "WARNING",
108 NULL); 108 NULL);
109 109
110 nat_res = GNUNET_OS_check_helper_binary("gnunet-nat-server", GNUNET_NO, NULL); 110 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server", GNUNET_NO,
111 NULL);
111 if (GNUNET_SYSERR == nat_res) 112 if (GNUNET_SYSERR == nat_res)
112 { 113 {
113 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
114 "Cannot run NAT test: `%s' file not found\n", 115 "Cannot run NAT test: `%s' file not found\n",
115 "gnunet-nat-server"); 116 "gnunet-nat-server");
116 return 0; 117 return 0;
117 } 118 }
118 119
119 gns = GNUNET_OS_start_process(GNUNET_YES, 120 gns = GNUNET_OS_start_process (GNUNET_YES,
120 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 121 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
121 NULL, NULL, NULL, 122 NULL, NULL, NULL,
122 "gnunet-nat-server", 123 "gnunet-nat-server",
123 "gnunet-nat-server", 124 "gnunet-nat-server",
124 "-c", "test_nat_test_data.conf", 125 "-c", "test_nat_test_data.conf",
125 "12345", NULL); 126 "12345", NULL);
126 GNUNET_assert(NULL != gns); 127 GNUNET_assert (NULL != gns);
127 GNUNET_PROGRAM_run(3, argv_prog, 128 GNUNET_PROGRAM_run (3, argv_prog,
128 "test-nat-test", "nohelp", 129 "test-nat-test", "nohelp",
129 options, &run, 130 options, &run,
130 NULL); 131 NULL);
131 GNUNET_break(0 == GNUNET_OS_process_kill(gns, GNUNET_TERM_SIG)); 132 GNUNET_break (0 == GNUNET_OS_process_kill (gns, GNUNET_TERM_SIG));
132 GNUNET_break(GNUNET_OK == GNUNET_OS_process_wait(gns)); 133 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns));
133 GNUNET_OS_process_destroy(gns); 134 GNUNET_OS_process_destroy (gns);
134 if (0 != ret) 135 if (0 != ret)
135 fprintf(stderr, 136 fprintf (stderr,
136 "NAT test failed to report success\n"); 137 "NAT test failed to report success\n");
137 return ret; 138 return ret;
138} 139}
139 140
diff --git a/src/nat/test_stun.c b/src/nat/test_stun.c
index ee755905e..41e962ea0 100644
--- a/src/nat/test_stun.c
+++ b/src/nat/test_stun.c
@@ -36,12 +36,12 @@
36 36
37 37
38 38
39#define LOG(kind, ...) GNUNET_log_from(kind, "test-stun", __VA_ARGS__) 39#define LOG(kind, ...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
40 40
41/** 41/**
42 * Time to wait before stopping NAT, in seconds 42 * Time to wait before stopping NAT, in seconds
43 */ 43 */
44#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) 44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
45 45
46 46
47/** 47/**
@@ -72,12 +72,12 @@ static struct GNUNET_NAT_STUN_Handle *rh;
72 72
73 73
74static void 74static void
75print_answer(struct sockaddr_in* answer) 75print_answer (struct sockaddr_in*answer)
76{ 76{
77 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 77 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
78 "External IP is: %s , with port %d\n", 78 "External IP is: %s , with port %d\n",
79 inet_ntoa(answer->sin_addr), 79 inet_ntoa (answer->sin_addr),
80 ntohs(answer->sin_port)); 80 ntohs (answer->sin_port));
81} 81}
82 82
83 83
@@ -85,25 +85,25 @@ print_answer(struct sockaddr_in* answer)
85 * Function that terminates the test. 85 * Function that terminates the test.
86 */ 86 */
87static void 87static void
88stop() 88stop ()
89{ 89{
90 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 90 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
91 "Stopping NAT and quitting...\n"); 91 "Stopping NAT and quitting...\n");
92 if (NULL != ltask4) 92 if (NULL != ltask4)
93 { 93 {
94 GNUNET_SCHEDULER_cancel(ltask4); 94 GNUNET_SCHEDULER_cancel (ltask4);
95 ltask4 = NULL; 95 ltask4 = NULL;
96 } 96 }
97 if (NULL != lsock4) 97 if (NULL != lsock4)
98 { 98 {
99 GNUNET_NETWORK_socket_close(lsock4); 99 GNUNET_NETWORK_socket_close (lsock4);
100 lsock4 = NULL; 100 lsock4 = NULL;
101 } 101 }
102 if (NULL != rh) 102 if (NULL != rh)
103 { 103 {
104 GNUNET_NAT_stun_make_request_cancel(rh); 104 GNUNET_NAT_stun_make_request_cancel (rh);
105 rh = NULL; 105 rh = NULL;
106 } 106 }
107} 107}
108 108
109 109
@@ -114,45 +114,45 @@ stop()
114 * @param cls 114 * @param cls
115 */ 115 */
116static void 116static void
117do_udp_read(void *cls) 117do_udp_read (void *cls)
118{ 118{
119 //struct GNUNET_NAT_Test *tst = cls; 119 // struct GNUNET_NAT_Test *tst = cls;
120 unsigned char reply_buf[1024]; 120 unsigned char reply_buf[1024];
121 ssize_t rlen; 121 ssize_t rlen;
122 struct sockaddr_in answer; 122 struct sockaddr_in answer;
123 const struct GNUNET_SCHEDULER_TaskContext *tc; 123 const struct GNUNET_SCHEDULER_TaskContext *tc;
124 124
125 ltask4 = NULL; 125 ltask4 = NULL;
126 tc = GNUNET_SCHEDULER_get_task_context(); 126 tc = GNUNET_SCHEDULER_get_task_context ();
127 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) || 127 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) ||
128 (!GNUNET_NETWORK_fdset_isset(tc->read_ready, 128 (! GNUNET_NETWORK_fdset_isset (tc->read_ready,
129 lsock4))) 129 lsock4)))
130 { 130 {
131 fprintf(stderr, 131 fprintf (stderr,
132 "Timeout waiting for STUN response\n"); 132 "Timeout waiting for STUN response\n");
133 stop(); 133 stop ();
134 } 134 }
135 rlen = GNUNET_NETWORK_socket_recv(lsock4, 135 rlen = GNUNET_NETWORK_socket_recv (lsock4,
136 reply_buf, 136 reply_buf,
137 sizeof(reply_buf)); 137 sizeof(reply_buf));
138 memset(&answer, 138 memset (&answer,
139 0, 139 0,
140 sizeof(struct sockaddr_in)); 140 sizeof(struct sockaddr_in));
141 if (GNUNET_OK != 141 if (GNUNET_OK !=
142 GNUNET_NAT_stun_handle_packet(reply_buf, 142 GNUNET_NAT_stun_handle_packet (reply_buf,
143 rlen, 143 rlen,
144 &answer)) 144 &answer))
145 { 145 {
146 fprintf(stderr, 146 fprintf (stderr,
147 "Unexpected UDP packet, trying to read more\n"); 147 "Unexpected UDP packet, trying to read more\n");
148 ltask4 = GNUNET_SCHEDULER_add_read_net(TIMEOUT, 148 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
149 lsock4, 149 lsock4,
150 &do_udp_read, NULL); 150 &do_udp_read, NULL);
151 return; 151 return;
152 } 152 }
153 ret = 0; 153 ret = 0;
154 print_answer(&answer); 154 print_answer (&answer);
155 stop(); 155 stop ();
156} 156}
157 157
158 158
@@ -162,33 +162,33 @@ do_udp_read(void *cls)
162 * @return NULL on error 162 * @return NULL on error
163 */ 163 */
164static struct GNUNET_NETWORK_Handle * 164static struct GNUNET_NETWORK_Handle *
165bind_v4() 165bind_v4 ()
166{ 166{
167 struct GNUNET_NETWORK_Handle *ls; 167 struct GNUNET_NETWORK_Handle *ls;
168 struct sockaddr_in sa4; 168 struct sockaddr_in sa4;
169 int eno; 169 int eno;
170 170
171 memset(&sa4, 0, sizeof(sa4)); 171 memset (&sa4, 0, sizeof(sa4));
172 sa4.sin_family = AF_INET; 172 sa4.sin_family = AF_INET;
173 sa4.sin_port = htons(port); 173 sa4.sin_port = htons (port);
174#if HAVE_SOCKADDR_IN_SIN_LEN 174#if HAVE_SOCKADDR_IN_SIN_LEN
175 sa4.sin_len = sizeof(sa4); 175 sa4.sin_len = sizeof(sa4);
176#endif 176#endif
177 ls = GNUNET_NETWORK_socket_create(AF_INET, 177 ls = GNUNET_NETWORK_socket_create (AF_INET,
178 SOCK_DGRAM, 178 SOCK_DGRAM,
179 0); 179 0);
180 if (NULL == ls) 180 if (NULL == ls)
181 return NULL; 181 return NULL;
182 if (GNUNET_OK != 182 if (GNUNET_OK !=
183 GNUNET_NETWORK_socket_bind(ls, 183 GNUNET_NETWORK_socket_bind (ls,
184 (const struct sockaddr *)&sa4, 184 (const struct sockaddr *) &sa4,
185 sizeof(sa4))) 185 sizeof(sa4)))
186 { 186 {
187 eno = errno; 187 eno = errno;
188 GNUNET_NETWORK_socket_close(ls); 188 GNUNET_NETWORK_socket_close (ls);
189 errno = eno; 189 errno = eno;
190 return NULL; 190 return NULL;
191 } 191 }
192 return ls; 192 return ls;
193} 193}
194 194
@@ -200,30 +200,30 @@ bind_v4()
200 * @param error status code from STUN 200 * @param error status code from STUN
201 */ 201 */
202static void 202static void
203request_callback(void *cls, 203request_callback (void *cls,
204 enum GNUNET_NAT_StatusCode error) 204 enum GNUNET_NAT_StatusCode error)
205{ 205{
206 rh = NULL; 206 rh = NULL;
207 if (GNUNET_NAT_ERROR_SUCCESS == error) 207 if (GNUNET_NAT_ERROR_SUCCESS == error)
208 { 208 {
209 /* all good, start to receive */ 209 /* all good, start to receive */
210 ltask4 = GNUNET_SCHEDULER_add_read_net(TIMEOUT, 210 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
211 lsock4, 211 lsock4,
212 &do_udp_read, 212 &do_udp_read,
213 NULL); 213 NULL);
214 return; 214 return;
215 } 215 }
216 if (error == GNUNET_NAT_ERROR_NOT_ONLINE) 216 if (error == GNUNET_NAT_ERROR_NOT_ONLINE)
217 { 217 {
218 ret = 77; /* report 'skip' */ 218 ret = 77; /* report 'skip' */
219 fprintf(stderr, 219 fprintf (stderr,
220 "System is offline, cannot test STUN request.\n"); 220 "System is offline, cannot test STUN request.\n");
221 } 221 }
222 else 222 else
223 { 223 {
224 ret = error; 224 ret = error;
225 } 225 }
226 stop(); 226 stop ();
227} 227}
228 228
229 229
@@ -231,34 +231,34 @@ request_callback(void *cls,
231 * Main function run with scheduler. 231 * Main function run with scheduler.
232 */ 232 */
233static void 233static void
234run(void *cls, 234run (void *cls,
235 char *const *args, 235 char *const *args,
236 const char *cfgfile, 236 const char *cfgfile,
237 const struct GNUNET_CONFIGURATION_Handle *cfg) 237 const struct GNUNET_CONFIGURATION_Handle *cfg)
238{ 238{
239 //Lets create the socket 239 // Lets create the socket
240 lsock4 = bind_v4(); 240 lsock4 = bind_v4 ();
241 if (NULL == lsock4) 241 if (NULL == lsock4)
242 { 242 {
243 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, 243 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
244 "bind"); 244 "bind");
245 GNUNET_SCHEDULER_shutdown(); 245 GNUNET_SCHEDULER_shutdown ();
246 return; 246 return;
247 } 247 }
248 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249 "Service listens on port %u\n", 249 "Service listens on port %u\n",
250 (unsigned int)port); 250 (unsigned int) port);
251 rh = GNUNET_NAT_stun_make_request(stun_server, 251 rh = GNUNET_NAT_stun_make_request (stun_server,
252 stun_port, 252 stun_port,
253 lsock4, 253 lsock4,
254 &request_callback, NULL); 254 &request_callback, NULL);
255 GNUNET_SCHEDULER_add_delayed(TIMEOUT, 255 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
256 &stop, NULL); 256 &stop, NULL);
257} 257}
258 258
259 259
260int 260int
261main(int argc, char *const argv[]) 261main (int argc, char *const argv[])
262{ 262{
263 struct GNUNET_GETOPT_CommandLineOption options[] = { 263 struct GNUNET_GETOPT_CommandLineOption options[] = {
264 GNUNET_GETOPT_OPTION_END 264 GNUNET_GETOPT_OPTION_END
@@ -272,40 +272,40 @@ main(int argc, char *const argv[])
272 char *fn; 272 char *fn;
273 struct GNUNET_OS_Process *proc; 273 struct GNUNET_OS_Process *proc;
274 274
275 GNUNET_log_setup("test-stun", 275 GNUNET_log_setup ("test-stun",
276 "WARNING", 276 "WARNING",
277 NULL); 277 NULL);
278 278
279 /* Lets start resolver */ 279 /* Lets start resolver */
280 fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver"); 280 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
281 proc = GNUNET_OS_start_process(GNUNET_YES, 281 proc = GNUNET_OS_start_process (GNUNET_YES,
282 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 282 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
283 NULL, NULL, NULL, 283 NULL, NULL, NULL,
284 fn, 284 fn,
285 "gnunet-service-resolver", 285 "gnunet-service-resolver",
286 "-c", "test_stun.conf", NULL); 286 "-c", "test_stun.conf", NULL);
287 287
288 if (NULL == proc) 288 if (NULL == proc)
289 { 289 {
290 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 290 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
291 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n"); 291 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n");
292 exit(1); 292 exit (1);
293 } 293 }
294 294
295 GNUNET_PROGRAM_run(3, argv_prog, 295 GNUNET_PROGRAM_run (3, argv_prog,
296 "test-stun", "nohelp", 296 "test-stun", "nohelp",
297 options, 297 options,
298 &run, NULL); 298 &run, NULL);
299 299
300 /* Now kill the resolver */ 300 /* Now kill the resolver */
301 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) 301 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
302 { 302 {
303 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 303 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
304 } 304 }
305 GNUNET_OS_process_wait(proc); 305 GNUNET_OS_process_wait (proc);
306 GNUNET_OS_process_destroy(proc); 306 GNUNET_OS_process_destroy (proc);
307 proc = NULL; 307 proc = NULL;
308 GNUNET_free(fn); 308 GNUNET_free (fn);
309 309
310 return ret; 310 return ret;
311} 311}