aboutsummaryrefslogtreecommitdiff
path: root/src/nat
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat')
-rw-r--r--src/nat/gnunet-helper-nat-client-windows.c61
-rw-r--r--src/nat/gnunet-helper-nat-client.c55
-rw-r--r--src/nat/gnunet-helper-nat-server-windows.c53
-rw-r--r--src/nat/gnunet-helper-nat-server.c55
-rw-r--r--src/nat/gnunet-nat-server.c45
-rw-r--r--src/nat/nat.c282
-rw-r--r--src/nat/nat_mini.c90
-rw-r--r--src/nat/nat_test.c95
-rw-r--r--src/nat/test_nat.c26
-rw-r--r--src/nat/test_nat_mini.c17
-rw-r--r--src/nat/test_nat_test.c24
11 files changed, 365 insertions, 438 deletions
diff --git a/src/nat/gnunet-helper-nat-client-windows.c b/src/nat/gnunet-helper-nat-client-windows.c
index 0fa62ff7c..864c911b0 100644
--- a/src/nat/gnunet-helper-nat-client-windows.c
+++ b/src/nat/gnunet-helper-nat-client-windows.c
@@ -258,8 +258,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
258 ip_pkt.checksum = 0; 258 ip_pkt.checksum = 0;
259 ip_pkt.src_ip = my_ip->s_addr; 259 ip_pkt.src_ip = my_ip->s_addr;
260 ip_pkt.dst_ip = other->s_addr; 260 ip_pkt.dst_ip = other->s_addr;
261 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 261 ip_pkt.checksum =
262 sizeof (struct ip_header))); 262 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
263 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 263 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
264 off += sizeof (struct ip_header); 264 off += sizeof (struct ip_header);
265 265
@@ -273,8 +273,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
273 /* ip header of the presumably 'lost' udp packet */ 273 /* ip header of the presumably 'lost' udp packet */
274 ip_pkt.vers_ihl = 0x45; 274 ip_pkt.vers_ihl = 0x45;
275 ip_pkt.tos = 0; 275 ip_pkt.tos = 0;
276 ip_pkt.pkt_len = htons (sizeof (struct ip_header) + 276 ip_pkt.pkt_len =
277 sizeof (struct udp_header)); 277 htons (sizeof (struct ip_header) + sizeof (struct udp_header));
278 ip_pkt.id = htons (0); 278 ip_pkt.id = htons (0);
279 ip_pkt.flags_frag_offset = 0; 279 ip_pkt.flags_frag_offset = 0;
280 ip_pkt.ttl = 128; 280 ip_pkt.ttl = 128;
@@ -282,8 +282,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
282 ip_pkt.checksum = 0; 282 ip_pkt.checksum = 0;
283 ip_pkt.src_ip = other->s_addr; 283 ip_pkt.src_ip = other->s_addr;
284 ip_pkt.dst_ip = dummy.s_addr; 284 ip_pkt.dst_ip = dummy.s_addr;
285 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 285 ip_pkt.checksum =
286 sizeof (struct ip_header))); 286 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
287 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 287 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
288 off += sizeof (struct ip_header); 288 off += sizeof (struct ip_header);
289 289
@@ -296,20 +296,20 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
296 off += sizeof (struct udp_header); 296 off += sizeof (struct udp_header);
297 297
298 /* no go back to calculate ICMP packet checksum */ 298 /* no go back to calculate ICMP packet checksum */
299 icmp_pkt.checksum = htons (calc_checksum ((uint16_t *) & packet[off], 299 icmp_pkt.checksum =
300 sizeof (struct 300 htons (calc_checksum
301 icmp_ttl_exceeded_header) + 301 ((uint16_t *) & packet[off],
302 sizeof (struct ip_header) + 302 sizeof (struct icmp_ttl_exceeded_header) +
303 sizeof (struct udp_header))); 303 sizeof (struct ip_header) + sizeof (struct udp_header)));
304 memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt, 304 memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt,
305 sizeof (struct icmp_ttl_exceeded_header)); 305 sizeof (struct icmp_ttl_exceeded_header));
306 306
307 memset (&dst, 0, sizeof (dst)); 307 memset (&dst, 0, sizeof (dst));
308 dst.sin_family = AF_INET; 308 dst.sin_family = AF_INET;
309 dst.sin_addr = *other; 309 dst.sin_addr = *other;
310 err = sendto (rawsock, 310 err =
311 packet, 311 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
312 sizeof (packet), 0, (struct sockaddr *) &dst, sizeof (dst)); 312 sizeof (dst));
313 if (err < 0) 313 if (err < 0)
314 { 314 {
315 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 315 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
@@ -352,8 +352,8 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
352 ip_pkt.checksum = 0; 352 ip_pkt.checksum = 0;
353 ip_pkt.src_ip = my_ip->s_addr; 353 ip_pkt.src_ip = my_ip->s_addr;
354 ip_pkt.dst_ip = other->s_addr; 354 ip_pkt.dst_ip = other->s_addr;
355 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 355 ip_pkt.checksum =
356 sizeof (struct ip_header))); 356 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
357 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 357 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
358 off += sizeof (ip_pkt); 358 off += sizeof (ip_pkt);
359 359
@@ -377,8 +377,8 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
377 ip_pkt.src_ip = other->s_addr; 377 ip_pkt.src_ip = other->s_addr;
378 ip_pkt.dst_ip = dummy.s_addr; 378 ip_pkt.dst_ip = dummy.s_addr;
379 ip_pkt.checksum = 0; 379 ip_pkt.checksum = 0;
380 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 380 ip_pkt.checksum =
381 sizeof (struct ip_header))); 381 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
382 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 382 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
383 off += sizeof (struct ip_header); 383 off += sizeof (struct ip_header);
384 384
@@ -386,26 +386,27 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
386 icmp_echo.code = 0; 386 icmp_echo.code = 0;
387 icmp_echo.reserved = htonl (port); 387 icmp_echo.reserved = htonl (port);
388 icmp_echo.checksum = 0; 388 icmp_echo.checksum = 0;
389 icmp_echo.checksum = htons (calc_checksum ((uint16_t *) & icmp_echo, 389 icmp_echo.checksum =
390 sizeof (struct icmp_echo_header))); 390 htons (calc_checksum
391 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
391 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); 392 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
392 393
393 /* no go back to calculate ICMP packet checksum */ 394 /* no go back to calculate ICMP packet checksum */
394 off = sizeof (struct ip_header); 395 off = sizeof (struct ip_header);
395 icmp_ttl.checksum = htons (calc_checksum ((uint16_t *) & packet[off], 396 icmp_ttl.checksum =
396 sizeof (struct 397 htons (calc_checksum
397 icmp_ttl_exceeded_header) + 398 ((uint16_t *) & packet[off],
398 sizeof (struct ip_header) + 399 sizeof (struct icmp_ttl_exceeded_header) +
399 sizeof (struct icmp_echo_header))); 400 sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
400 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); 401 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
401 402
402 memset (&dst, 0, sizeof (dst)); 403 memset (&dst, 0, sizeof (dst));
403 dst.sin_family = AF_INET; 404 dst.sin_family = AF_INET;
404 dst.sin_addr = *other; 405 dst.sin_addr = *other;
405 406
406 err = sendto (rawsock, 407 err =
407 packet, 408 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
408 sizeof (packet), 0, (struct sockaddr *) &dst, sizeof (dst)); 409 sizeof (dst));
409 410
410 if (err < 0) 411 if (err < 0)
411 { 412 {
@@ -439,8 +440,8 @@ make_raw_socket ()
439 if (0 != 440 if (0 !=
440 setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen)) 441 setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen))
441 { 442 {
442 fprintf (stderr, 443 fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n",
443 "Error setting SO_BROADCAST to ON: %s\n", strerror (errno)); 444 strerror (errno));
444 closesocket (rawsock); 445 closesocket (rawsock);
445 return INVALID_SOCKET; 446 return INVALID_SOCKET;
446 } 447 }
diff --git a/src/nat/gnunet-helper-nat-client.c b/src/nat/gnunet-helper-nat-client.c
index 76f405bf1..fbebdbc0f 100644
--- a/src/nat/gnunet-helper-nat-client.c
+++ b/src/nat/gnunet-helper-nat-client.c
@@ -235,8 +235,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
235 ip_pkt.checksum = 0; 235 ip_pkt.checksum = 0;
236 ip_pkt.src_ip = my_ip->s_addr; 236 ip_pkt.src_ip = my_ip->s_addr;
237 ip_pkt.dst_ip = other->s_addr; 237 ip_pkt.dst_ip = other->s_addr;
238 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 238 ip_pkt.checksum =
239 sizeof (struct ip_header))); 239 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
240 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 240 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
241 off += sizeof (struct ip_header); 241 off += sizeof (struct ip_header);
242 242
@@ -250,8 +250,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
250 /* ip header of the presumably 'lost' udp packet */ 250 /* ip header of the presumably 'lost' udp packet */
251 ip_pkt.vers_ihl = 0x45; 251 ip_pkt.vers_ihl = 0x45;
252 ip_pkt.tos = 0; 252 ip_pkt.tos = 0;
253 ip_pkt.pkt_len = htons (sizeof (struct ip_header) + 253 ip_pkt.pkt_len =
254 sizeof (struct udp_header)); 254 htons (sizeof (struct ip_header) + sizeof (struct udp_header));
255 ip_pkt.id = htons (0); 255 ip_pkt.id = htons (0);
256 ip_pkt.flags_frag_offset = 0; 256 ip_pkt.flags_frag_offset = 0;
257 ip_pkt.ttl = 128; 257 ip_pkt.ttl = 128;
@@ -259,8 +259,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
259 ip_pkt.checksum = 0; 259 ip_pkt.checksum = 0;
260 ip_pkt.src_ip = other->s_addr; 260 ip_pkt.src_ip = other->s_addr;
261 ip_pkt.dst_ip = dummy.s_addr; 261 ip_pkt.dst_ip = dummy.s_addr;
262 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 262 ip_pkt.checksum =
263 sizeof (struct ip_header))); 263 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
264 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 264 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
265 off += sizeof (struct ip_header); 265 off += sizeof (struct ip_header);
266 266
@@ -287,9 +287,9 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
287 dst.sin_len = sizeof (struct sockaddr_in); 287 dst.sin_len = sizeof (struct sockaddr_in);
288#endif 288#endif
289 dst.sin_addr = *other; 289 dst.sin_addr = *other;
290 err = sendto (rawsock, 290 err =
291 packet, 291 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
292 sizeof (packet), 0, (struct sockaddr *) &dst, sizeof (dst)); 292 sizeof (dst));
293 if (err < 0) 293 if (err < 0)
294 { 294 {
295 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 295 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
@@ -332,8 +332,8 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
332 ip_pkt.checksum = 0; 332 ip_pkt.checksum = 0;
333 ip_pkt.src_ip = my_ip->s_addr; 333 ip_pkt.src_ip = my_ip->s_addr;
334 ip_pkt.dst_ip = other->s_addr; 334 ip_pkt.dst_ip = other->s_addr;
335 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 335 ip_pkt.checksum =
336 sizeof (struct ip_header))); 336 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
337 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 337 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
338 off = sizeof (ip_pkt); 338 off = sizeof (ip_pkt);
339 339
@@ -357,8 +357,8 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
357 ip_pkt.src_ip = other->s_addr; 357 ip_pkt.src_ip = other->s_addr;
358 ip_pkt.dst_ip = dummy.s_addr; 358 ip_pkt.dst_ip = dummy.s_addr;
359 ip_pkt.checksum = 0; 359 ip_pkt.checksum = 0;
360 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 360 ip_pkt.checksum =
361 sizeof (struct ip_header))); 361 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
362 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 362 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
363 off += sizeof (struct ip_header); 363 off += sizeof (struct ip_header);
364 364
@@ -366,17 +366,18 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
366 icmp_echo.code = 0; 366 icmp_echo.code = 0;
367 icmp_echo.reserved = htonl (port); 367 icmp_echo.reserved = htonl (port);
368 icmp_echo.checksum = 0; 368 icmp_echo.checksum = 0;
369 icmp_echo.checksum = htons (calc_checksum ((uint16_t *) & icmp_echo, 369 icmp_echo.checksum =
370 sizeof (struct icmp_echo_header))); 370 htons (calc_checksum
371 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
371 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); 372 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
372 373
373 /* no go back to calculate ICMP packet checksum */ 374 /* no go back to calculate ICMP packet checksum */
374 off = sizeof (struct ip_header); 375 off = sizeof (struct ip_header);
375 icmp_ttl.checksum = htons (calc_checksum ((uint16_t *) & packet[off], 376 icmp_ttl.checksum =
376 sizeof (struct 377 htons (calc_checksum
377 icmp_ttl_exceeded_header) + 378 ((uint16_t *) & packet[off],
378 sizeof (struct ip_header) + 379 sizeof (struct icmp_ttl_exceeded_header) +
379 sizeof (struct icmp_echo_header))); 380 sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
380 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); 381 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
381 382
382 /* prepare for transmission */ 383 /* prepare for transmission */
@@ -386,9 +387,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
386 dst.sin_len = sizeof (struct sockaddr_in); 387 dst.sin_len = sizeof (struct sockaddr_in);
387#endif 388#endif
388 dst.sin_addr = *other; 389 dst.sin_addr = *other;
389 err = sendto (rawsock, 390 err =
390 packet, 391 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
391 sizeof (packet), 0, (struct sockaddr *) &dst, sizeof (dst)); 392 sizeof (dst));
392 if (err < 0) 393 if (err < 0)
393 { 394 {
394 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 395 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
@@ -417,15 +418,15 @@ make_raw_socket ()
417 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); 418 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
418 return -1; 419 return -1;
419 } 420 }
420 if (0 != setsockopt (ret, SOL_SOCKET, SO_BROADCAST, 421 if (0 !=
421 (char *) &one, sizeof (one))) 422 setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
422 { 423 {
423 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); 424 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
424 close (ret); 425 close (ret);
425 return -1; 426 return -1;
426 } 427 }
427 if (0 != setsockopt (ret, IPPROTO_IP, IP_HDRINCL, 428 if (0 !=
428 (char *) &one, sizeof (one))) 429 setsockopt (ret, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
429 { 430 {
430 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); 431 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
431 close (ret); 432 close (ret);
diff --git a/src/nat/gnunet-helper-nat-server-windows.c b/src/nat/gnunet-helper-nat-server-windows.c
index 727a7be67..8386a15b1 100644
--- a/src/nat/gnunet-helper-nat-server-windows.c
+++ b/src/nat/gnunet-helper-nat-server-windows.c
@@ -272,8 +272,8 @@ send_icmp_echo (const struct in_addr *my_ip)
272 ip_pkt.checksum = 0; 272 ip_pkt.checksum = 0;
273 ip_pkt.src_ip = my_ip->s_addr; 273 ip_pkt.src_ip = my_ip->s_addr;
274 ip_pkt.dst_ip = dummy.s_addr; 274 ip_pkt.dst_ip = dummy.s_addr;
275 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 275 ip_pkt.checksum =
276 sizeof (struct ip_header))); 276 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
277 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 277 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
278 off += sizeof (struct ip_header); 278 off += sizeof (struct ip_header);
279 279
@@ -281,16 +281,17 @@ send_icmp_echo (const struct in_addr *my_ip)
281 icmp_echo.code = 0; 281 icmp_echo.code = 0;
282 icmp_echo.reserved = 0; 282 icmp_echo.reserved = 0;
283 icmp_echo.checksum = 0; 283 icmp_echo.checksum = 0;
284 icmp_echo.checksum = htons (calc_checksum ((uint16_t *) & icmp_echo, 284 icmp_echo.checksum =
285 sizeof (struct icmp_echo_header))); 285 htons (calc_checksum
286 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
286 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); 287 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
287 off += sizeof (struct icmp_echo_header); 288 off += sizeof (struct icmp_echo_header);
288 289
289 memset (&dst, 0, sizeof (dst)); 290 memset (&dst, 0, sizeof (dst));
290 dst.sin_family = AF_INET; 291 dst.sin_family = AF_INET;
291 dst.sin_addr = dummy; 292 dst.sin_addr = dummy;
292 err = sendto (rawsock, 293 err =
293 packet, off, 0, (struct sockaddr *) &dst, sizeof (dst)); 294 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
294 if (err < 0) 295 if (err < 0)
295 { 296 {
296#if VERBOSE 297#if VERBOSE
@@ -383,9 +384,10 @@ process_icmp_response ()
383 switch (ip_pkt.proto) 384 switch (ip_pkt.proto)
384 { 385 {
385 case IPPROTO_ICMP: 386 case IPPROTO_ICMP:
386 if (have != (sizeof (struct ip_header) * 2 + 387 if (have !=
387 sizeof (struct icmp_ttl_exceeded_header) + 388 (sizeof (struct ip_header) * 2 +
388 sizeof (struct icmp_echo_header))) 389 sizeof (struct icmp_ttl_exceeded_header) +
390 sizeof (struct icmp_echo_header)))
389 { 391 {
390 /* malformed */ 392 /* malformed */
391 return; 393 return;
@@ -395,9 +397,9 @@ process_icmp_response ()
395 port = (uint16_t) ntohl (icmp_echo.reserved); 397 port = (uint16_t) ntohl (icmp_echo.reserved);
396 break; 398 break;
397 case IPPROTO_UDP: 399 case IPPROTO_UDP:
398 if (have != (sizeof (struct ip_header) * 2 + 400 if (have !=
399 sizeof (struct icmp_ttl_exceeded_header) + 401 (sizeof (struct ip_header) * 2 +
400 sizeof (struct udp_header))) 402 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
401 { 403 {
402 /* malformed */ 404 /* malformed */
403 return; 405 return;
@@ -412,8 +414,8 @@ process_icmp_response ()
412 } 414 }
413 415
414 ssize = sizeof (buf); 416 ssize = sizeof (buf);
415 WSAAddressToString ((LPSOCKADDR) & source_ip, 417 WSAAddressToString ((LPSOCKADDR) & source_ip, sizeof (source_ip), NULL, buf,
416 sizeof (source_ip), NULL, buf, &ssize); 418 &ssize);
417 if (port == 0) 419 if (port == 0)
418 fprintf (stdout, "%s\n", buf); 420 fprintf (stdout, "%s\n", buf);
419 else 421 else
@@ -460,16 +462,17 @@ make_raw_socket ()
460 return INVALID_SOCKET; 462 return INVALID_SOCKET;
461 } 463 }
462 464
463 if (0 != setsockopt (rawsock, 465 if (0 !=
464 SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen)) 466 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal,
467 bOptLen))
465 { 468 {
466 fprintf (stderr, 469 fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n",
467 "Error setting SO_BROADCAST to ON: %s\n", strerror (errno)); 470 strerror (errno));
468 closesocket (rawsock); 471 closesocket (rawsock);
469 return INVALID_SOCKET; 472 return INVALID_SOCKET;
470 } 473 }
471 if (0 != setsockopt (rawsock, 474 if (0 !=
472 IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen)) 475 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen))
473 { 476 {
474 fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno)); 477 fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno));
475 closesocket (rawsock); 478 closesocket (rawsock);
@@ -503,9 +506,8 @@ make_udp_socket (const struct in_addr *my_ip)
503 addr.sin_port = htons (NAT_TRAV_PORT); 506 addr.sin_port = htons (NAT_TRAV_PORT);
504 if (0 != bind (ret, (struct sockaddr *) &addr, sizeof (addr))) 507 if (0 != bind (ret, (struct sockaddr *) &addr, sizeof (addr)))
505 { 508 {
506 fprintf (stderr, 509 fprintf (stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT,
507 "Error binding UDP socket to port %u: %s\n", 510 strerror (errno));
508 NAT_TRAV_PORT, strerror (errno));
509 /* likely problematic, but not certain, try to continue */ 511 /* likely problematic, but not certain, try to continue */
510 } 512 }
511 return ret; 513 return ret;
@@ -530,9 +532,8 @@ main (int argc, char *const *argv)
530 } 532 }
531 if (1 != inet_pton (AF_INET, argv[1], &external)) 533 if (1 != inet_pton (AF_INET, argv[1], &external))
532 { 534 {
533 fprintf (stderr, 535 fprintf (stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1],
534 "Error parsing IPv4 address: %s, error %s\n", 536 strerror (errno));
535 argv[1], strerror (errno));
536 return 1; 537 return 1;
537 } 538 }
538 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) 539 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
diff --git a/src/nat/gnunet-helper-nat-server.c b/src/nat/gnunet-helper-nat-server.c
index 636ae6003..684fa9706 100644
--- a/src/nat/gnunet-helper-nat-server.c
+++ b/src/nat/gnunet-helper-nat-server.c
@@ -252,8 +252,8 @@ send_icmp_echo (const struct in_addr *my_ip)
252 ip_pkt.checksum = 0; 252 ip_pkt.checksum = 0;
253 ip_pkt.src_ip = my_ip->s_addr; 253 ip_pkt.src_ip = my_ip->s_addr;
254 ip_pkt.dst_ip = dummy.s_addr; 254 ip_pkt.dst_ip = dummy.s_addr;
255 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt, 255 ip_pkt.checksum =
256 sizeof (struct ip_header))); 256 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
257 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 257 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
258 off += sizeof (struct ip_header); 258 off += sizeof (struct ip_header);
259 259
@@ -261,8 +261,9 @@ send_icmp_echo (const struct in_addr *my_ip)
261 icmp_echo.code = 0; 261 icmp_echo.code = 0;
262 icmp_echo.checksum = 0; 262 icmp_echo.checksum = 0;
263 icmp_echo.reserved = 0; 263 icmp_echo.reserved = 0;
264 icmp_echo.checksum = htons (calc_checksum ((uint16_t *) & icmp_echo, 264 icmp_echo.checksum =
265 sizeof (struct icmp_echo_header))); 265 htons (calc_checksum
266 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
266 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); 267 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
267 off += sizeof (struct icmp_echo_header); 268 off += sizeof (struct icmp_echo_header);
268 269
@@ -272,8 +273,8 @@ send_icmp_echo (const struct in_addr *my_ip)
272 dst.sin_len = sizeof (struct sockaddr_in); 273 dst.sin_len = sizeof (struct sockaddr_in);
273#endif 274#endif
274 dst.sin_addr = dummy; 275 dst.sin_addr = dummy;
275 err = sendto (rawsock, 276 err =
276 packet, off, 0, (struct sockaddr *) &dst, sizeof (dst)); 277 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
277 if (err < 0) 278 if (err < 0)
278 { 279 {
279#if VERBOSE 280#if VERBOSE
@@ -368,9 +369,10 @@ process_icmp_response ()
368 switch (ip_pkt.proto) 369 switch (ip_pkt.proto)
369 { 370 {
370 case IPPROTO_ICMP: 371 case IPPROTO_ICMP:
371 if (have != (sizeof (struct ip_header) * 2 + 372 if (have !=
372 sizeof (struct icmp_ttl_exceeded_header) + 373 (sizeof (struct ip_header) * 2 +
373 sizeof (struct icmp_echo_header))) 374 sizeof (struct icmp_ttl_exceeded_header) +
375 sizeof (struct icmp_echo_header)))
374 { 376 {
375 /* malformed */ 377 /* malformed */
376 return; 378 return;
@@ -380,9 +382,9 @@ process_icmp_response ()
380 port = (uint16_t) ntohl (icmp_echo.reserved); 382 port = (uint16_t) ntohl (icmp_echo.reserved);
381 break; 383 break;
382 case IPPROTO_UDP: 384 case IPPROTO_UDP:
383 if (have != (sizeof (struct ip_header) * 2 + 385 if (have !=
384 sizeof (struct icmp_ttl_exceeded_header) + 386 (sizeof (struct ip_header) * 2 +
385 sizeof (struct udp_header))) 387 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
386 { 388 {
387 /* malformed */ 389 /* malformed */
388 return; 390 return;
@@ -397,13 +399,12 @@ process_icmp_response ()
397 } 399 }
398 400
399 if (port == 0) 401 if (port == 0)
400 fprintf (stdout, 402 fprintf (stdout, "%s\n",
401 "%s\n", inet_ntop (AF_INET, &source_ip, buf, sizeof (buf))); 403 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)));
402 else 404 else
403 fprintf (stdout, 405 fprintf (stdout, "%s:%u\n",
404 "%s:%u\n", 406 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)),
405 inet_ntop (AF_INET, 407 (unsigned int) port);
406 &source_ip, buf, sizeof (buf)), (unsigned int) port);
407 fflush (stdout); 408 fflush (stdout);
408} 409}
409 410
@@ -426,9 +427,8 @@ make_icmp_socket ()
426 } 427 }
427 if (ret >= FD_SETSIZE) 428 if (ret >= FD_SETSIZE)
428 { 429 {
429 fprintf (stderr, 430 fprintf (stderr, "Socket number too large (%d > %u)\n", ret,
430 "Socket number too large (%d > %u)\n", 431 (unsigned int) FD_SETSIZE);
431 ret, (unsigned int) FD_SETSIZE);
432 close (ret); 432 close (ret);
433 return -1; 433 return -1;
434 } 434 }
@@ -453,15 +453,15 @@ make_raw_socket ()
453 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); 453 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
454 return -1; 454 return -1;
455 } 455 }
456 if (-1 == setsockopt (ret, 456 if (-1 ==
457 SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one))) 457 setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
458 { 458 {
459 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); 459 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
460 close (ret); 460 close (ret);
461 return -1; 461 return -1;
462 } 462 }
463 if (-1 == setsockopt (ret, 463 if (-1 ==
464 IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one))) 464 setsockopt (ret, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
465 { 465 {
466 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); 466 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
467 close (ret); 467 close (ret);
@@ -499,9 +499,8 @@ make_udp_socket (const struct in_addr *my_ip)
499 499
500 if (0 != bind (ret, &addr, sizeof (addr))) 500 if (0 != bind (ret, &addr, sizeof (addr)))
501 { 501 {
502 fprintf (stderr, 502 fprintf (stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT,
503 "Error binding UDP socket to port %u: %s\n", 503 strerror (errno));
504 NAT_TRAV_PORT, strerror (errno));
505 /* likely problematic, but not certain, try to continue */ 504 /* likely problematic, but not certain, try to continue */
506 } 505 }
507 return ret; 506 return ret;
diff --git a/src/nat/gnunet-nat-server.c b/src/nat/gnunet-nat-server.c
index 3b4ec2293..988e9fbc9 100644
--- a/src/nat/gnunet-nat-server.c
+++ b/src/nat/gnunet-nat-server.c
@@ -148,13 +148,11 @@ try_send_tcp (uint32_t dst_ipv4, uint16_t dport, uint16_t data)
148 sa.sin_addr.s_addr = dst_ipv4; 148 sa.sin_addr.s_addr = dst_ipv4;
149 sa.sin_port = htons (dport); 149 sa.sin_port = htons (dport);
150#if DEBUG_NAT 150#if DEBUG_NAT
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending TCP message to `%s'\n",
152 "Sending TCP message to `%s'\n",
153 GNUNET_a2s ((struct sockaddr *) &sa, sizeof (sa))); 152 GNUNET_a2s ((struct sockaddr *) &sa, sizeof (sa)));
154#endif 153#endif
155 if ((GNUNET_OK != 154 if ((GNUNET_OK !=
156 GNUNET_NETWORK_socket_connect (s, 155 GNUNET_NETWORK_socket_connect (s, (const struct sockaddr *) &sa,
157 (const struct sockaddr *) &sa,
158 sizeof (sa))) && (errno != EINPROGRESS)) 156 sizeof (sa))) && (errno != EINPROGRESS))
159 { 157 {
160 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect"); 158 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
@@ -196,14 +194,12 @@ try_send_udp (uint32_t dst_ipv4, uint16_t dport, uint16_t data)
196 sa.sin_addr.s_addr = dst_ipv4; 194 sa.sin_addr.s_addr = dst_ipv4;
197 sa.sin_port = htons (dport); 195 sa.sin_port = htons (dport);
198#if DEBUG_NAT 196#if DEBUG_NAT
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending UDP packet to `%s'\n",
200 "Sending UDP packet to `%s'\n",
201 GNUNET_a2s ((struct sockaddr *) &sa, sizeof (sa))); 198 GNUNET_a2s ((struct sockaddr *) &sa, sizeof (sa)));
202#endif 199#endif
203 if (-1 == GNUNET_NETWORK_socket_sendto (s, 200 if (-1 ==
204 &data, sizeof (data), 201 GNUNET_NETWORK_socket_sendto (s, &data, sizeof (data),
205 (const struct sockaddr *) &sa, 202 (const struct sockaddr *) &sa, sizeof (sa)))
206 sizeof (sa)))
207 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto"); 203 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
208 GNUNET_NETWORK_socket_close (s); 204 GNUNET_NETWORK_socket_close (s);
209} 205}
@@ -218,8 +214,7 @@ try_send_udp (uint32_t dst_ipv4, uint16_t dport, uint16_t data)
218 * @param msg message with details about what to test 214 * @param msg message with details about what to test
219 */ 215 */
220static void 216static void
221test (void *cls, 217test (void *cls, struct GNUNET_SERVER_Client *client,
222 struct GNUNET_SERVER_Client *client,
223 const struct GNUNET_MessageHeader *msg) 218 const struct GNUNET_MessageHeader *msg)
224{ 219{
225 const struct GNUNET_NAT_TestMessage *tm; 220 const struct GNUNET_NAT_TestMessage *tm;
@@ -263,9 +258,8 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
263 * @param c configuration 258 * @param c configuration
264 */ 259 */
265static void 260static void
266run (void *cls, 261run (void *cls, char *const *args, const char *cfgfile,
267 char *const *args, 262 const struct GNUNET_CONFIGURATION_Handle *c)
268 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
269{ 263{
270 static const struct GNUNET_SERVER_MessageHandler handlers[] = { 264 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
271 {&test, NULL, GNUNET_MESSAGE_TYPE_NAT_TEST, 265 {&test, NULL, GNUNET_MESSAGE_TYPE_NAT_TEST,
@@ -288,8 +282,8 @@ run (void *cls,
288 }; 282 };
289 283
290 cfg = c; 284 cfg = c;
291 if ((args[0] == NULL) || 285 if ((args[0] == NULL) || (1 != SSCANF (args[0], "%u", &port)) || (0 == port)
292 (1 != SSCANF (args[0], "%u", &port)) || (0 == port) || (65536 <= port)) 286 || (65536 <= port))
293 { 287 {
294 fprintf (stderr, 288 fprintf (stderr,
295 _ 289 _
@@ -307,12 +301,12 @@ run (void *cls,
307 in4.sin_len = sizeof (in4); 301 in4.sin_len = sizeof (in4);
308 in6.sin6_len = sizeof (in6); 302 in6.sin6_len = sizeof (in6);
309#endif 303#endif
310 server = GNUNET_SERVER_create (NULL, NULL, 304 server =
311 (struct sockaddr * const *) sa, 305 GNUNET_SERVER_create (NULL, NULL, (struct sockaddr * const *) sa, slen,
312 slen, GNUNET_TIME_UNIT_SECONDS, GNUNET_YES); 306 GNUNET_TIME_UNIT_SECONDS, GNUNET_YES);
313 GNUNET_SERVER_add_handlers (server, handlers); 307 GNUNET_SERVER_add_handlers (server, handlers);
314 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 308 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
315 &shutdown_task, NULL); 309 NULL);
316} 310}
317 311
318 312
@@ -331,10 +325,9 @@ main (int argc, char *const argv[])
331 }; 325 };
332 326
333 if (GNUNET_OK != 327 if (GNUNET_OK !=
334 GNUNET_PROGRAM_run (argc, argv, 328 GNUNET_PROGRAM_run (argc, argv, "gnunet-nat-server [options] PORT",
335 "gnunet-nat-server [options] PORT", 329 _("GNUnet NAT traversal test helper daemon"), options,
336 _("GNUnet NAT traversal test helper daemon"), 330 &run, NULL))
337 options, &run, NULL))
338 return 1; 331 return 1;
339 return 0; 332 return 0;
340} 333}
diff --git a/src/nat/nat.c b/src/nat/nat.c
index ed72be9f8..6069d40ad 100644
--- a/src/nat/nat.c
+++ b/src/nat/nat.c
@@ -386,8 +386,7 @@ remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
386 continue; 386 continue;
387 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos); 387 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
388 if (NULL != h->address_callback) 388 if (NULL != h->address_callback)
389 h->address_callback (h->callback_cls, 389 h->address_callback (h->callback_cls, GNUNET_NO,
390 GNUNET_NO,
391 (const struct sockaddr *) &pos[1], pos->addrlen); 390 (const struct sockaddr *) &pos[1], pos->addrlen);
392 GNUNET_free (pos); 391 GNUNET_free (pos);
393 } 392 }
@@ -416,10 +415,10 @@ add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
416 lal->source = src; 415 lal->source = src;
417 GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal); 416 GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal);
418#if DEBUG_NAT 417#if DEBUG_NAT
419 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 418 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
420 "nat", 419 "Adding address `%s' from source %d\n", GNUNET_a2s (arg,
421 "Adding address `%s' from source %d\n", 420 arg_size),
422 GNUNET_a2s (arg, arg_size), src); 421 src);
423#endif 422#endif
424 if (NULL != h->address_callback) 423 if (NULL != h->address_callback)
425 h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size); 424 h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size);
@@ -438,8 +437,7 @@ add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
438 * @param arg_size number of bytes in arg 437 * @param arg_size number of bytes in arg
439 */ 438 */
440static void 439static void
441add_to_address_list (struct GNUNET_NAT_Handle *h, 440add_to_address_list (struct GNUNET_NAT_Handle *h, enum LocalAddressSource src,
442 enum LocalAddressSource src,
443 const struct sockaddr *arg, socklen_t arg_size) 441 const struct sockaddr *arg, socklen_t arg_size)
444{ 442{
445 struct sockaddr_in s4; 443 struct sockaddr_in s4;
@@ -452,17 +450,13 @@ add_to_address_list (struct GNUNET_NAT_Handle *h,
452 in4 = (const struct sockaddr_in *) arg; 450 in4 = (const struct sockaddr_in *) arg;
453 s4 = *in4; 451 s4 = *in4;
454 s4.sin_port = htons (h->adv_port); 452 s4.sin_port = htons (h->adv_port);
455 add_to_address_list_as_is (h, 453 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
456 src,
457 (const struct sockaddr *) &s4,
458 sizeof (struct sockaddr_in)); 454 sizeof (struct sockaddr_in));
459 if (GNUNET_YES == h->enable_nat_server) 455 if (GNUNET_YES == h->enable_nat_server)
460 { 456 {
461 /* also add with PORT = 0 to indicate NAT server is enabled */ 457 /* also add with PORT = 0 to indicate NAT server is enabled */
462 s4.sin_port = htons (0); 458 s4.sin_port = htons (0);
463 add_to_address_list_as_is (h, 459 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
464 src,
465 (const struct sockaddr *) &s4,
466 sizeof (struct sockaddr_in)); 460 sizeof (struct sockaddr_in));
467 } 461 }
468 } 462 }
@@ -473,9 +467,7 @@ add_to_address_list (struct GNUNET_NAT_Handle *h,
473 in6 = (const struct sockaddr_in6 *) arg; 467 in6 = (const struct sockaddr_in6 *) arg;
474 s6 = *in6; 468 s6 = *in6;
475 s6.sin6_port = htons (h->adv_port); 469 s6.sin6_port = htons (h->adv_port);
476 add_to_address_list_as_is (h, 470 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s6,
477 src,
478 (const struct sockaddr *) &s6,
479 sizeof (struct sockaddr_in6)); 471 sizeof (struct sockaddr_in6));
480 } 472 }
481 } 473 }
@@ -497,8 +489,8 @@ add_to_address_list (struct GNUNET_NAT_Handle *h,
497 */ 489 */
498static void 490static void
499add_ip_to_address_list (struct GNUNET_NAT_Handle *h, 491add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
500 enum LocalAddressSource src, 492 enum LocalAddressSource src, const void *addr,
501 const void *addr, socklen_t addrlen) 493 socklen_t addrlen)
502{ 494{
503 struct sockaddr_in s4; 495 struct sockaddr_in s4;
504 const struct in_addr *in4; 496 const struct in_addr *in4;
@@ -515,17 +507,13 @@ add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
515 s4.sin_len = (u_char) sizeof (struct sockaddr_in); 507 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
516#endif 508#endif
517 s4.sin_addr = *in4; 509 s4.sin_addr = *in4;
518 add_to_address_list (h, 510 add_to_address_list (h, src, (const struct sockaddr *) &s4,
519 src,
520 (const struct sockaddr *) &s4,
521 sizeof (struct sockaddr_in)); 511 sizeof (struct sockaddr_in));
522 if (GNUNET_YES == h->enable_nat_server) 512 if (GNUNET_YES == h->enable_nat_server)
523 { 513 {
524 /* also add with PORT = 0 to indicate NAT server is enabled */ 514 /* also add with PORT = 0 to indicate NAT server is enabled */
525 s4.sin_port = htons (0); 515 s4.sin_port = htons (0);
526 add_to_address_list (h, 516 add_to_address_list (h, src, (const struct sockaddr *) &s4,
527 src,
528 (const struct sockaddr *) &s4,
529 sizeof (struct sockaddr_in)); 517 sizeof (struct sockaddr_in));
530 518
531 } 519 }
@@ -542,9 +530,7 @@ add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
542 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6); 530 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
543#endif 531#endif
544 s6.sin6_addr = *in6; 532 s6.sin6_addr = *in6;
545 add_to_address_list (h, 533 add_to_address_list (h, src, (const struct sockaddr *) &s6,
546 src,
547 (const struct sockaddr *) &s6,
548 sizeof (struct sockaddr_in6)); 534 sizeof (struct sockaddr_in6));
549 } 535 }
550 } 536 }
@@ -562,8 +548,8 @@ add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
562 * @param cls the NAT handle 548 * @param cls the NAT handle
563 * @param tc scheduler context 549 * @param tc scheduler context
564 */ 550 */
565static void 551static void resolve_dns (void *cls,
566resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 552 const struct GNUNET_SCHEDULER_TaskContext *tc);
567 553
568 554
569/** 555/**
@@ -585,8 +571,8 @@ process_external_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
585 h->ext_dns = NULL; 571 h->ext_dns = NULL;
586 if (1 == inet_pton (AF_INET, h->external_address, &dummy)) 572 if (1 == inet_pton (AF_INET, h->external_address, &dummy))
587 return; /* repated lookup pointless: was numeric! */ 573 return; /* repated lookup pointless: was numeric! */
588 h->dns_task = GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency, 574 h->dns_task =
589 &resolve_dns, h); 575 GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency, &resolve_dns, h);
590 return; 576 return;
591 } 577 }
592 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen); 578 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
@@ -599,8 +585,8 @@ process_external_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
599 * @param cls the NAT handle 585 * @param cls the NAT handle
600 * @param tc scheduler context 586 * @param tc scheduler context
601 */ 587 */
602static void 588static void resolve_hostname (void *cls,
603resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 589 const struct GNUNET_SCHEDULER_TaskContext *tc);
604 590
605 591
606/** 592/**
@@ -620,8 +606,9 @@ process_hostname_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
620 if (addr == NULL) 606 if (addr == NULL)
621 { 607 {
622 h->hostname_dns = NULL; 608 h->hostname_dns = NULL;
623 h->hostname_task = GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency, 609 h->hostname_task =
624 &resolve_hostname, h); 610 GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
611 &resolve_hostname, h);
625 return; 612 return;
626 } 613 }
627 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen); 614 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
@@ -640,9 +627,7 @@ process_hostname_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
640 * @return GNUNET_OK to continue iterating 627 * @return GNUNET_OK to continue iterating
641 */ 628 */
642static int 629static int
643process_interfaces (void *cls, 630process_interfaces (void *cls, const char *name, int isDefault,
644 const char *name,
645 int isDefault,
646 const struct sockaddr *addr, socklen_t addrlen) 631 const struct sockaddr *addr, socklen_t addrlen)
647{ 632{
648 struct GNUNET_NAT_Handle *h = cls; 633 struct GNUNET_NAT_Handle *h = cls;
@@ -657,9 +642,8 @@ process_interfaces (void *cls,
657 s4 = (struct sockaddr_in *) addr; 642 s4 = (struct sockaddr_in *) addr;
658 ip = &s4->sin_addr; 643 ip = &s4->sin_addr;
659 if (GNUNET_YES == h->use_localaddresses) 644 if (GNUNET_YES == h->use_localaddresses)
660 add_ip_to_address_list (h, 645 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s4->sin_addr,
661 LAL_INTERFACE_ADDRESS, 646 sizeof (struct in_addr));
662 &s4->sin_addr, sizeof (struct in_addr));
663 break; 647 break;
664 case AF_INET6: 648 case AF_INET6:
665 s6 = (struct sockaddr_in6 *) addr; 649 s6 = (struct sockaddr_in6 *) addr;
@@ -670,24 +654,22 @@ process_interfaces (void *cls,
670 } 654 }
671 ip = &s6->sin6_addr; 655 ip = &s6->sin6_addr;
672 if (GNUNET_YES == h->use_localaddresses) 656 if (GNUNET_YES == h->use_localaddresses)
673 add_ip_to_address_list (h, 657 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s6->sin6_addr,
674 LAL_INTERFACE_ADDRESS, 658 sizeof (struct in6_addr));
675 &s6->sin6_addr, sizeof (struct in6_addr));
676 break; 659 break;
677 default: 660 default:
678 GNUNET_break (0); 661 GNUNET_break (0);
679 return GNUNET_OK; 662 return GNUNET_OK;
680 } 663 }
681 if ((h->internal_address == NULL) && 664 if ((h->internal_address == NULL) && (h->server_proc == NULL) &&
682 (h->server_proc == NULL) &&
683 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) && 665 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
684 (GNUNET_YES == isDefault) && 666 (GNUNET_YES == isDefault) && ((addr->sa_family == AF_INET) ||
685 ((addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6))) 667 (addr->sa_family == AF_INET6)))
686 { 668 {
687 /* no internal address configured, but we found a "default" 669 /* no internal address configured, but we found a "default"
688 * interface, try using that as our 'internal' address */ 670 * interface, try using that as our 'internal' address */
689 h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family, 671 h->internal_address =
690 ip, buf, sizeof (buf))); 672 GNUNET_strdup (inet_ntop (addr->sa_family, ip, buf, sizeof (buf)));
691 start_gnunet_nat_server (h); 673 start_gnunet_nat_server (h);
692 } 674 }
693 return GNUNET_OK; 675 return GNUNET_OK;
@@ -736,13 +718,12 @@ nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
736 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 718 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
737 return; 719 return;
738 memset (mybuf, 0, sizeof (mybuf)); 720 memset (mybuf, 0, sizeof (mybuf));
739 bytes = GNUNET_DISK_file_read (h->server_stdout_handle, 721 bytes =
740 mybuf, sizeof (mybuf)); 722 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf));
741 if (bytes < 1) 723 if (bytes < 1)
742 { 724 {
743#if DEBUG_NAT 725#if DEBUG_NAT
744 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 726 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
745 "nat",
746 "Finished reading from server stdout with code: %d\n", 727 "Finished reading from server stdout with code: %d\n",
747 bytes); 728 bytes);
748#endif 729#endif
@@ -787,13 +768,11 @@ nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
787#if HAVE_SOCKADDR_IN_SIN_LEN 768#if HAVE_SOCKADDR_IN_SIN_LEN
788 sin_addr.sin_len = sizeof (sin_addr); 769 sin_addr.sin_len = sizeof (sin_addr);
789#endif 770#endif
790 if ((NULL == port_start) || 771 if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) ||
791 (1 != sscanf (port_start, "%d", &port)) ||
792 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr))) 772 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
793 { 773 {
794 /* should we restart gnunet-helper-nat-server? */ 774 /* should we restart gnunet-helper-nat-server? */
795 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 775 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
796 "nat",
797 _ 776 _
798 ("gnunet-helper-nat-server generated malformed address `%s'\n"), 777 ("gnunet-helper-nat-server generated malformed address `%s'\n"),
799 mybuf); 778 mybuf);
@@ -805,16 +784,15 @@ nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
805 } 784 }
806 sin_addr.sin_port = htons ((uint16_t) port); 785 sin_addr.sin_port = htons ((uint16_t) port);
807#if DEBUG_NAT 786#if DEBUG_NAT
808 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 787 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
809 "nat",
810 "gnunet-helper-nat-server read: %s:%d\n", mybuf, port); 788 "gnunet-helper-nat-server read: %s:%d\n", mybuf, port);
811#endif 789#endif
812 h->reversal_callback (h->callback_cls, 790 h->reversal_callback (h->callback_cls, (const struct sockaddr *) &sin_addr,
813 (const struct sockaddr *) &sin_addr, sizeof (sin_addr)); 791 sizeof (sin_addr));
814 h->server_read_task = 792 h->server_read_task =
815 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 793 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
816 h->server_stdout_handle, 794 h->server_stdout_handle, &nat_server_read,
817 &nat_server_read, h); 795 h);
818} 796}
819 797
820 798
@@ -827,28 +805,25 @@ nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
827static void 805static void
828start_gnunet_nat_server (struct GNUNET_NAT_Handle *h) 806start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
829{ 807{
830 if ((h->behind_nat == GNUNET_YES) && 808 if ((h->behind_nat == GNUNET_YES) && (h->enable_nat_server == GNUNET_YES) &&
831 (h->enable_nat_server == GNUNET_YES) &&
832 (h->internal_address != NULL) && 809 (h->internal_address != NULL) &&
833 (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, 810 (NULL !=
834 GNUNET_NO, GNUNET_YES)))) 811 (h->server_stdout =
812 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES))))
835 { 813 {
836#if DEBUG_NAT 814#if DEBUG_NAT
837 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 815 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat" "Starting %s at `%s'\n",
838 "nat"
839 "Starting %s at `%s'\n",
840 "gnunet-helper-nat-server", h->internal_address); 816 "gnunet-helper-nat-server", h->internal_address);
841#endif 817#endif
842 /* Start the server process */ 818 /* Start the server process */
843 h->server_proc = GNUNET_OS_start_process (NULL, 819 h->server_proc =
844 h->server_stdout, 820 GNUNET_OS_start_process (NULL, h->server_stdout,
845 "gnunet-helper-nat-server", 821 "gnunet-helper-nat-server",
846 "gnunet-helper-nat-server", 822 "gnunet-helper-nat-server",
847 h->internal_address, NULL); 823 h->internal_address, NULL);
848 if (h->server_proc == NULL) 824 if (h->server_proc == NULL)
849 { 825 {
850 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 826 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
851 "nat",
852 _("Failed to start %s\n"), "gnunet-helper-nat-server"); 827 _("Failed to start %s\n"), "gnunet-helper-nat-server");
853 GNUNET_DISK_pipe_close (h->server_stdout); 828 GNUNET_DISK_pipe_close (h->server_stdout);
854 h->server_stdout = NULL; 829 h->server_stdout = NULL;
@@ -857,13 +832,12 @@ start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
857 { 832 {
858 /* Close the write end of the read pipe */ 833 /* Close the write end of the read pipe */
859 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); 834 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
860 h->server_stdout_handle 835 h->server_stdout_handle =
861 = GNUNET_DISK_pipe_handle (h->server_stdout, 836 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
862 GNUNET_DISK_PIPE_END_READ); 837 h->server_read_task =
863 h->server_read_task 838 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
864 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 839 h->server_stdout_handle,
865 h->server_stdout_handle, 840 &nat_server_read, h);
866 &nat_server_read, h);
867 } 841 }
868 } 842 }
869} 843}
@@ -883,8 +857,8 @@ list_interfaces (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
883 h->ifc_task = GNUNET_SCHEDULER_NO_TASK; 857 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
884 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS); 858 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
885 GNUNET_OS_network_interfaces_list (&process_interfaces, h); 859 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
886 h->ifc_task = GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency, 860 h->ifc_task =
887 &list_interfaces, h); 861 GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency, &list_interfaces, h);
888} 862}
889 863
890 864
@@ -901,9 +875,9 @@ resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
901 875
902 h->hostname_task = GNUNET_SCHEDULER_NO_TASK; 876 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
903 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS); 877 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
904 h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, 878 h->hostname_dns =
905 HOSTNAME_RESOLVE_TIMEOUT, 879 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT,
906 &process_hostname_ip, h); 880 &process_hostname_ip, h);
907} 881}
908 882
909 883
@@ -921,10 +895,10 @@ resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
921 895
922 h->dns_task = GNUNET_SCHEDULER_NO_TASK; 896 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
923 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP); 897 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
924 h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address, 898 h->ext_dns =
925 AF_INET, 899 GNUNET_RESOLVER_ip_get (h->external_address, AF_INET,
926 GNUNET_TIME_UNIT_MINUTES, 900 GNUNET_TIME_UNIT_MINUTES, &process_external_ip,
927 &process_external_ip, h); 901 h);
928} 902}
929 903
930 904
@@ -938,8 +912,8 @@ resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
938 * @param addrlen actual lenght of the address 912 * @param addrlen actual lenght of the address
939 */ 913 */
940static void 914static void
941upnp_add (void *cls, 915upnp_add (void *cls, int add_remove, const struct sockaddr *addr,
942 int add_remove, const struct sockaddr *addr, socklen_t addrlen) 916 socklen_t addrlen)
943{ 917{
944 struct GNUNET_NAT_Handle *h = cls; 918 struct GNUNET_NAT_Handle *h = cls;
945 struct LocalAddressList *pos; 919 struct LocalAddressList *pos;
@@ -955,13 +929,12 @@ upnp_add (void *cls,
955 while (NULL != (pos = next)) 929 while (NULL != (pos = next))
956 { 930 {
957 next = pos->next; 931 next = pos->next;
958 if ((pos->source != LAL_UPNP) || 932 if ((pos->source != LAL_UPNP) || (pos->addrlen != addrlen) ||
959 (pos->addrlen != addrlen) || (0 != memcmp (&pos[1], addr, addrlen))) 933 (0 != memcmp (&pos[1], addr, addrlen)))
960 continue; 934 continue;
961 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos); 935 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
962 if (NULL != h->address_callback) 936 if (NULL != h->address_callback)
963 h->address_callback (h->callback_cls, 937 h->address_callback (h->callback_cls, GNUNET_NO,
964 GNUNET_NO,
965 (const struct sockaddr *) &pos[1], pos->addrlen); 938 (const struct sockaddr *) &pos[1], pos->addrlen);
966 GNUNET_free (pos); 939 GNUNET_free (pos);
967 return; /* only remove once */ 940 return; /* only remove once */
@@ -1036,8 +1009,9 @@ add_from_bind (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1036 GNUNET_break (0); 1009 GNUNET_break (0);
1037 break; 1010 break;
1038 } 1011 }
1039 if (0 != memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr, 1012 if (0 !=
1040 &any, sizeof (struct in6_addr))) 1013 memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr, &any,
1014 sizeof (struct in6_addr)))
1041 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, 1015 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1042 sizeof (struct sockaddr_in6)); 1016 sizeof (struct sockaddr_in6));
1043 break; 1017 break;
@@ -1068,12 +1042,9 @@ add_from_bind (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1068 * @return NULL on error, otherwise handle that can be used to unregister 1042 * @return NULL on error, otherwise handle that can be used to unregister
1069 */ 1043 */
1070struct GNUNET_NAT_Handle * 1044struct GNUNET_NAT_Handle *
1071GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, 1045GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, int is_tcp,
1072 int is_tcp, 1046 uint16_t adv_port, unsigned int num_addrs,
1073 uint16_t adv_port, 1047 const struct sockaddr **addrs, const socklen_t * addrlens,
1074 unsigned int num_addrs,
1075 const struct sockaddr **addrs,
1076 const socklen_t * addrlens,
1077 GNUNET_NAT_AddressCallback address_callback, 1048 GNUNET_NAT_AddressCallback address_callback,
1078 GNUNET_NAT_ReversalCallback reversal_callback, 1049 GNUNET_NAT_ReversalCallback reversal_callback,
1079 void *callback_cls) 1050 void *callback_cls)
@@ -1083,8 +1054,7 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1083 unsigned int i; 1054 unsigned int i;
1084 1055
1085#if DEBUG_NAT 1056#if DEBUG_NAT
1086 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1057 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
1087 "nat",
1088 "Registered with NAT service at port %u with %u IP bound local addresses\n", 1058 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1089 (unsigned int) adv_port, num_addrs); 1059 (unsigned int) adv_port, num_addrs);
1090#endif 1060#endif
@@ -1114,16 +1084,14 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1114 if (GNUNET_OK == 1084 if (GNUNET_OK ==
1115 GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS")) 1085 GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS"))
1116 { 1086 {
1117 (void) GNUNET_CONFIGURATION_get_value_string (cfg, 1087 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1118 "nat",
1119 "INTERNAL_ADDRESS", 1088 "INTERNAL_ADDRESS",
1120 &h->internal_address); 1089 &h->internal_address);
1121 } 1090 }
1122 if ((h->internal_address != NULL) && 1091 if ((h->internal_address != NULL) &&
1123 (inet_pton (AF_INET, h->internal_address, &in_addr) != 1)) 1092 (inet_pton (AF_INET, h->internal_address, &in_addr) != 1))
1124 { 1093 {
1125 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 1094 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1126 "nat",
1127 _("Malformed %s `%s' given in configuration!\n"), 1095 _("Malformed %s `%s' given in configuration!\n"),
1128 "INTERNAL_ADDRESS", h->internal_address); 1096 "INTERNAL_ADDRESS", h->internal_address);
1129 GNUNET_free (h->internal_address); 1097 GNUNET_free (h->internal_address);
@@ -1133,57 +1101,45 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1133 if (GNUNET_OK == 1101 if (GNUNET_OK ==
1134 GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS")) 1102 GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS"))
1135 { 1103 {
1136 (void) GNUNET_CONFIGURATION_get_value_string (cfg, 1104 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1137 "nat",
1138 "EXTERNAL_ADDRESS", 1105 "EXTERNAL_ADDRESS",
1139 &h->external_address); 1106 &h->external_address);
1140 } 1107 }
1141 if ((h->external_address != NULL) && 1108 if ((h->external_address != NULL) &&
1142 (inet_pton (AF_INET, h->external_address, &in_addr) != 1)) 1109 (inet_pton (AF_INET, h->external_address, &in_addr) != 1))
1143 { 1110 {
1144 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 1111 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1145 "nat",
1146 _("Malformed %s `%s' given in configuration!\n"), 1112 _("Malformed %s `%s' given in configuration!\n"),
1147 "EXTERNAL_ADDRESS", h->external_address); 1113 "EXTERNAL_ADDRESS", h->external_address);
1148 GNUNET_free (h->external_address); 1114 GNUNET_free (h->external_address);
1149 h->external_address = NULL; 1115 h->external_address = NULL;
1150 } 1116 }
1151 h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1117 h->behind_nat =
1152 "nat", "BEHIND_NAT"); 1118 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT");
1153 h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1119 h->nat_punched =
1154 "nat", "PUNCHED_NAT"); 1120 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "PUNCHED_NAT");
1155 h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1121 h->enable_nat_client =
1156 "nat", 1122 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_NAT_CLIENT");
1157 "ENABLE_NAT_CLIENT"); 1123 h->enable_nat_server =
1158 h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1124 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_NAT_SERVER");
1159 "nat", 1125 h->enable_upnp =
1160 "ENABLE_NAT_SERVER"); 1126 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_UPNP");
1161 h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1127 h->use_localaddresses =
1162 "nat", "ENABLE_UPNP"); 1128 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_LOCALADDR");
1163 h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1129 h->use_hostname =
1164 "nat", 1130 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_HOSTNAME");
1165 "USE_LOCALADDR"); 1131 h->disable_ipv6 =
1166 h->use_hostname = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1132 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "DISABLEV6");
1167 "nat",
1168 "USE_HOSTNAME");
1169 h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1170 "nat", "DISABLEV6");
1171 if (GNUNET_OK != 1133 if (GNUNET_OK !=
1172 GNUNET_CONFIGURATION_get_value_time (cfg, 1134 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "DYNDNS_FREQUENCY",
1173 "nat",
1174 "DYNDNS_FREQUENCY",
1175 &h->dyndns_frequency)) 1135 &h->dyndns_frequency))
1176 h->dyndns_frequency = DYNDNS_FREQUENCY; 1136 h->dyndns_frequency = DYNDNS_FREQUENCY;
1177 if (GNUNET_OK != 1137 if (GNUNET_OK !=
1178 GNUNET_CONFIGURATION_get_value_time (cfg, 1138 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "IFC_SCAN_FREQUENCY",
1179 "nat",
1180 "IFC_SCAN_FREQUENCY",
1181 &h->ifc_scan_frequency)) 1139 &h->ifc_scan_frequency))
1182 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY; 1140 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1183 if (GNUNET_OK != 1141 if (GNUNET_OK !=
1184 GNUNET_CONFIGURATION_get_value_time (cfg, 1142 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "HOSTNAME_DNS_FREQUENCY",
1185 "nat",
1186 "HOSTNAME_DNS_FREQUENCY",
1187 &h->hostname_dns_frequency)) 1143 &h->hostname_dns_frequency))
1188 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY; 1144 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1189 1145
@@ -1191,8 +1147,8 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1191 h->enable_nat_server = GNUNET_NO; 1147 h->enable_nat_server = GNUNET_NO;
1192 1148
1193 /* Check if NAT was hole-punched */ 1149 /* Check if NAT was hole-punched */
1194 if ((NULL != h->address_callback) && 1150 if ((NULL != h->address_callback) && (h->external_address != NULL) &&
1195 (h->external_address != NULL) && (h->nat_punched == GNUNET_YES)) 1151 (h->nat_punched == GNUNET_YES))
1196 { 1152 {
1197 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h); 1153 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1198 h->enable_nat_server = GNUNET_NO; 1154 h->enable_nat_server = GNUNET_NO;
@@ -1200,8 +1156,7 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1200 } 1156 }
1201 1157
1202 /* Test for SUID binaries */ 1158 /* Test for SUID binaries */
1203 if ((h->behind_nat == GNUNET_YES) && 1159 if ((h->behind_nat == GNUNET_YES) && (GNUNET_YES == h->enable_nat_server) &&
1204 (GNUNET_YES == h->enable_nat_server) &&
1205 (GNUNET_YES != 1160 (GNUNET_YES !=
1206 GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server"))) 1161 GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server")))
1207 { 1162 {
@@ -1313,8 +1268,7 @@ GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1313 { 1268 {
1314 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal); 1269 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal);
1315 if (NULL != h->address_callback) 1270 if (NULL != h->address_callback)
1316 h->address_callback (h->callback_cls, 1271 h->address_callback (h->callback_cls, GNUNET_NO,
1317 GNUNET_NO,
1318 (const struct sockaddr *) &lal[1], lal->addrlen); 1272 (const struct sockaddr *) &lal[1], lal->addrlen);
1319 GNUNET_free (lal); 1273 GNUNET_free (lal);
1320 } 1274 }
@@ -1349,8 +1303,7 @@ GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1349 1303
1350 if (h->internal_address == NULL) 1304 if (h->internal_address == NULL)
1351 { 1305 {
1352 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 1306 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1353 "nat",
1354 _ 1307 _
1355 ("Internal IP address not known, cannot use ICMP NAT traversal method\n")); 1308 ("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1356 return; 1309 return;
@@ -1363,17 +1316,14 @@ GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1363 } 1316 }
1364 GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port); 1317 GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port);
1365#if DEBUG_NAT 1318#if DEBUG_NAT
1366 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1319 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
1367 "nat",
1368 _("Running gnunet-helper-nat-client %s %s %u\n"), 1320 _("Running gnunet-helper-nat-client %s %s %u\n"),
1369 h->internal_address, inet4, (unsigned int) h->adv_port); 1321 h->internal_address, inet4, (unsigned int) h->adv_port);
1370#endif 1322#endif
1371 proc = GNUNET_OS_start_process (NULL, 1323 proc =
1372 NULL, 1324 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-nat-client",
1373 "gnunet-helper-nat-client", 1325 "gnunet-helper-nat-client", h->internal_address,
1374 "gnunet-helper-nat-client", 1326 inet4, port_as_string, NULL);
1375 h->internal_address,
1376 inet4, port_as_string, NULL);
1377 if (NULL == proc) 1327 if (NULL == proc)
1378 return; 1328 return;
1379 /* we know that the gnunet-helper-nat-client will terminate virtually 1329 /* we know that the gnunet-helper-nat-client will terminate virtually
@@ -1394,8 +1344,8 @@ GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1394 * GNUNET_SYSERR if the address is malformed 1344 * GNUNET_SYSERR if the address is malformed
1395 */ 1345 */
1396int 1346int
1397GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, 1347GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, const void *addr,
1398 const void *addr, socklen_t addrlen) 1348 socklen_t addrlen)
1399{ 1349{
1400 struct LocalAddressList *pos; 1350 struct LocalAddressList *pos;
1401 const struct sockaddr_in *in4; 1351 const struct sockaddr_in *in4;
diff --git a/src/nat/nat_mini.c b/src/nat/nat_mini.c
index c40231067..e88c9ca55 100644
--- a/src/nat/nat_mini.c
+++ b/src/nat/nat_mini.c
@@ -118,8 +118,9 @@ read_external_ipv4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
118 118
119 eh->task = GNUNET_SCHEDULER_NO_TASK; 119 eh->task = GNUNET_SCHEDULER_NO_TASK;
120 if (GNUNET_YES == GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r)) 120 if (GNUNET_YES == GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r))
121 ret = GNUNET_DISK_file_read (eh->r, 121 ret =
122 &eh->buf[eh->off], sizeof (eh->buf) - eh->off); 122 GNUNET_DISK_file_read (eh->r, &eh->buf[eh->off],
123 sizeof (eh->buf) - eh->off);
123 else 124 else
124 ret = -1; /* error reading, timeout, etc. */ 125 ret = -1; /* error reading, timeout, etc. */
125 if (ret > 0) 126 if (ret > 0)
@@ -172,9 +173,9 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
172 GNUNET_free (eh); 173 GNUNET_free (eh);
173 return NULL; 174 return NULL;
174 } 175 }
175 eh->eip = GNUNET_OS_start_process (NULL, 176 eh->eip =
176 eh->opipe, 177 GNUNET_OS_start_process (NULL, eh->opipe, "external-ip", "external-ip",
177 "external-ip", "external-ip", NULL); 178 NULL);
178 if (NULL == eh->eip) 179 if (NULL == eh->eip)
179 { 180 {
180 GNUNET_DISK_pipe_close (eh->opipe); 181 GNUNET_DISK_pipe_close (eh->opipe);
@@ -184,8 +185,8 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
184 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); 185 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
185 eh->timeout = GNUNET_TIME_relative_to_absolute (timeout); 186 eh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
186 eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); 187 eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ);
187 eh->task = GNUNET_SCHEDULER_add_read_file (timeout, 188 eh->task =
188 eh->r, &read_external_ipv4, eh); 189 GNUNET_SCHEDULER_add_read_file (timeout, eh->r, &read_external_ipv4, eh);
189 return eh; 190 return eh;
190} 191}
191 192
@@ -278,8 +279,8 @@ struct GNUNET_NAT_MiniHandle
278 * @param cls the 'struct GNUNET_NAT_MiniHandle' 279 * @param cls the 'struct GNUNET_NAT_MiniHandle'
279 * @param tc scheduler context 280 * @param tc scheduler context
280 */ 281 */
281static void 282static void do_refresh (void *cls,
282do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 283 const struct GNUNET_SCHEDULER_TaskContext *tc);
283 284
284 285
285/** 286/**
@@ -322,19 +323,15 @@ process_refresh_output (void *cls, const char *line)
322 mini->did_map = GNUNET_NO; 323 mini->did_map = GNUNET_NO;
323 } 324 }
324 GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port); 325 GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port);
325 mini->map_cmd = GNUNET_OS_command_run (&process_map_output, 326 mini->map_cmd =
326 mini, 327 GNUNET_OS_command_run (&process_map_output, mini, MAP_TIMEOUT,
327 MAP_TIMEOUT, 328 "upnpc", "upnpc", "-r", pstr,
328 "upnpc", 329 mini->is_tcp ? "tcp" : "udp", NULL);
329 "upnpc",
330 "-r", pstr,
331 mini->is_tcp ? "tcp" : "udp",
332 NULL);
333 if (NULL != mini->map_cmd) 330 if (NULL != mini->map_cmd)
334 return; 331 return;
335 } 332 }
336 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, 333 mini->refresh_task =
337 &do_refresh, mini); 334 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
338 return; 335 return;
339 } 336 }
340 if (!mini->did_map) 337 if (!mini->did_map)
@@ -377,10 +374,11 @@ process_refresh_output (void *cls, const char *line)
377 return; /* skip */ 374 return; /* skip */
378 if (NULL == strstr (s, pstr)) 375 if (NULL == strstr (s, pstr))
379 return; /* skip */ 376 return; /* skip */
380 if (1 != sscanf (line, 377 if (1 !=
381 (mini->is_tcp) 378 sscanf (line,
382 ? "%*u TCP %u->%*s:%*u %*s" 379 (mini->
383 : "%*u UDP %u->%*s:%*u %*s", &nport)) 380 is_tcp) ? "%*u TCP %u->%*s:%*u %*s" :
381 "%*u UDP %u->%*s:%*u %*s", &nport))
384 return; /* skip */ 382 return; /* skip */
385 mini->found = GNUNET_YES; 383 mini->found = GNUNET_YES;
386 if (nport == ntohs (mini->current_addr.sin_port)) 384 if (nport == ntohs (mini->current_addr.sin_port))
@@ -410,10 +408,9 @@ do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
410 408
411 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK; 409 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
412 mini->found = GNUNET_NO; 410 mini->found = GNUNET_NO;
413 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output, 411 mini->refresh_cmd =
414 mini, 412 GNUNET_OS_command_run (&process_refresh_output, mini, MAP_TIMEOUT,
415 MAP_TIMEOUT, 413 "upnpc", "upnpc", "-l", NULL);
416 "upnpc", "upnpc", "-l", NULL);
417} 414}
418 415
419 416
@@ -436,8 +433,8 @@ process_map_output (void *cls, const char *line)
436 { 433 {
437 GNUNET_OS_command_stop (mini->map_cmd); 434 GNUNET_OS_command_stop (mini->map_cmd);
438 mini->map_cmd = NULL; 435 mini->map_cmd = NULL;
439 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, 436 mini->refresh_task =
440 &do_refresh, mini); 437 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
441 return; 438 return;
442 } 439 }
443 /* 440 /*
@@ -486,8 +483,7 @@ process_map_output (void *cls, const char *line)
486 * @return NULL on error (no 'upnpc' installed) 483 * @return NULL on error (no 'upnpc' installed)
487 */ 484 */
488struct GNUNET_NAT_MiniHandle * 485struct GNUNET_NAT_MiniHandle *
489GNUNET_NAT_mini_map_start (uint16_t port, 486GNUNET_NAT_mini_map_start (uint16_t port, int is_tcp,
490 int is_tcp,
491 GNUNET_NAT_AddressCallback ac, void *ac_cls) 487 GNUNET_NAT_AddressCallback ac, void *ac_cls)
492{ 488{
493 struct GNUNET_NAT_MiniHandle *ret; 489 struct GNUNET_NAT_MiniHandle *ret;
@@ -501,17 +497,13 @@ GNUNET_NAT_mini_map_start (uint16_t port,
501 ret->is_tcp = is_tcp; 497 ret->is_tcp = is_tcp;
502 ret->port = port; 498 ret->port = port;
503 GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) port); 499 GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) port);
504 ret->map_cmd = GNUNET_OS_command_run (&process_map_output, 500 ret->map_cmd =
505 ret, 501 GNUNET_OS_command_run (&process_map_output, ret, MAP_TIMEOUT, "upnpc",
506 MAP_TIMEOUT, 502 "upnpc", "-r", pstr, is_tcp ? "tcp" : "udp", NULL);
507 "upnpc",
508 "upnpc",
509 "-r", pstr,
510 is_tcp ? "tcp" : "udp", NULL);
511 if (NULL != ret->map_cmd) 503 if (NULL != ret->map_cmd)
512 return ret; 504 return ret;
513 ret->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, 505 ret->refresh_task =
514 &do_refresh, ret); 506 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret);
515 507
516 return ret; 508 return ret;
517} 509}
@@ -581,21 +573,17 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
581 /* Note: oddly enough, deletion uses the external port whereas 573 /* Note: oddly enough, deletion uses the external port whereas
582 * addition uses the internal port; this rarely matters since they 574 * addition uses the internal port; this rarely matters since they
583 * often are the same, but it might... */ 575 * often are the same, but it might... */
584 GNUNET_snprintf (pstr, sizeof (pstr), 576 GNUNET_snprintf (pstr, sizeof (pstr), "%u",
585 "%u", (unsigned int) ntohs (mini->current_addr.sin_port)); 577 (unsigned int) ntohs (mini->current_addr.sin_port));
586#if DEBUG_NAT 578#if DEBUG_NAT
587 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 579 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
588 "nat",
589 "Unmapping port %u with UPnP\n", 580 "Unmapping port %u with UPnP\n",
590 ntohs (mini->current_addr.sin_port)); 581 ntohs (mini->current_addr.sin_port));
591#endif 582#endif
592 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output, 583 mini->unmap_cmd =
593 mini, 584 GNUNET_OS_command_run (&process_unmap_output, mini, UNMAP_TIMEOUT,
594 UNMAP_TIMEOUT, 585 "upnpc", "upnpc", "-d", pstr,
595 "upnpc", 586 mini->is_tcp ? "tcp" : "udp", NULL);
596 "upnpc",
597 "-d", pstr,
598 mini->is_tcp ? "tcp" : "udp", NULL);
599} 587}
600 588
601 589
diff --git a/src/nat/nat_test.c b/src/nat/nat_test.c
index 30db00803..74bb758dd 100644
--- a/src/nat/nat_test.c
+++ b/src/nat/nat_test.c
@@ -178,8 +178,7 @@ reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
178 if (h->data != sa->sin_port) 178 if (h->data != sa->sin_port)
179 { 179 {
180#if DEBUG_NAT 180#if DEBUG_NAT
181 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 181 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
182 "nat",
183 "Received connection reversal request for wrong port\n"); 182 "Received connection reversal request for wrong port\n");
184#endif 183#endif
185 return; /* wrong port */ 184 return; /* wrong port */
@@ -202,11 +201,11 @@ do_udp_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 struct GNUNET_NAT_Test *tst = cls; 201 struct GNUNET_NAT_Test *tst = cls;
203 uint16_t data; 202 uint16_t data;
204 203
205 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 204 tst->ltask =
206 tst->lsock, &do_udp_read, tst); 205 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
206 &do_udp_read, tst);
207 if ((NULL != tc->write_ready) && 207 if ((NULL != tc->write_ready) &&
208 (GNUNET_NETWORK_fdset_isset (tc->read_ready, 208 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
209 tst->lsock)) &&
210 (sizeof (data) == 209 (sizeof (data) ==
211 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data)))) 210 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data))))
212 { 211 {
@@ -214,14 +213,14 @@ do_udp_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
214 tst->report (tst->report_cls, GNUNET_OK); 213 tst->report (tst->report_cls, GNUNET_OK);
215#if DEBUG_NAT 214#if DEBUG_NAT
216 else 215 else
217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 216 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
218 "nat", "Received data mismatches expected value\n"); 217 "Received data mismatches expected value\n");
219#endif 218#endif
220 } 219 }
221#if DEBUG_NAT 220#if DEBUG_NAT
222 else 221 else
223 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 222 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
224 "nat", "Failed to receive data from inbound connection\n"); 223 "Failed to receive data from inbound connection\n");
225#endif 224#endif
226} 225}
227 226
@@ -244,8 +243,7 @@ do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 tst = na->h; 243 tst = na->h;
245 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na); 244 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
246 if ((NULL != tc->write_ready) && 245 if ((NULL != tc->write_ready) &&
247 (GNUNET_NETWORK_fdset_isset (tc->read_ready, 246 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
248 na->sock)) &&
249 (sizeof (data) == 247 (sizeof (data) ==
250 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data)))) 248 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data))))
251 { 249 {
@@ -259,8 +257,8 @@ do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 } 257 }
260#if DEBUG_NAT 258#if DEBUG_NAT
261 else 259 else
262 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 260 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
263 "nat", "Failed to receive data from inbound connection\n"); 261 "Failed to receive data from inbound connection\n");
264#endif 262#endif
265 GNUNET_NETWORK_socket_close (na->sock); 263 GNUNET_NETWORK_socket_close (na->sock);
266 GNUNET_free (na); 264 GNUNET_free (na);
@@ -284,8 +282,9 @@ do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
284 tst->ltask = GNUNET_SCHEDULER_NO_TASK; 282 tst->ltask = GNUNET_SCHEDULER_NO_TASK;
285 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 283 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
286 return; 284 return;
287 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 285 tst->ltask =
288 tst->lsock, &do_accept, tst); 286 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
287 &do_accept, tst);
289 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL); 288 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
290 if (NULL == s) 289 if (NULL == s)
291 { 290 {
@@ -293,14 +292,15 @@ do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
293 return; /* odd error */ 292 return; /* odd error */
294 } 293 }
295#if DEBUG_NAT 294#if DEBUG_NAT
296 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 295 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
297 "nat", "Got an inbound connection, waiting for data\n"); 296 "Got an inbound connection, waiting for data\n");
298#endif 297#endif
299 wl = GNUNET_malloc (sizeof (struct NatActivity)); 298 wl = GNUNET_malloc (sizeof (struct NatActivity));
300 wl->sock = s; 299 wl->sock = s;
301 wl->h = tst; 300 wl->h = tst;
302 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 301 wl->rtask =
303 wl->sock, &do_read, wl); 302 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, wl->sock,
303 &do_read, wl);
304 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl); 304 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
305} 305}
306 306
@@ -315,8 +315,8 @@ do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 * @param addrlen actual lenght of the address 315 * @param addrlen actual lenght of the address
316 */ 316 */
317static void 317static void
318addr_cb (void *cls, 318addr_cb (void *cls, int add_remove, const struct sockaddr *addr,
319 int add_remove, const struct sockaddr *addr, socklen_t addrlen) 319 socklen_t addrlen)
320{ 320{
321 struct GNUNET_NAT_Test *h = cls; 321 struct GNUNET_NAT_Test *h = cls;
322 struct ClientActivity *ca; 322 struct ClientActivity *ca;
@@ -329,8 +329,7 @@ addr_cb (void *cls,
329 if (addrlen != sizeof (struct sockaddr_in)) 329 if (addrlen != sizeof (struct sockaddr_in))
330 return; /* ignore IPv6 here */ 330 return; /* ignore IPv6 here */
331#if DEBUG_NAT 331#if DEBUG_NAT
332 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 332 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
333 "nat",
334 "Asking gnunet-nat-server to connect to `%s'\n", 333 "Asking gnunet-nat-server to connect to `%s'\n",
335 GNUNET_a2s (addr, addrlen)); 334 GNUNET_a2s (addr, addrlen));
336#endif 335#endif
@@ -353,11 +352,10 @@ addr_cb (void *cls,
353 ca->client = client; 352 ca->client = client;
354 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca); 353 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
355 GNUNET_break (GNUNET_OK == 354 GNUNET_break (GNUNET_OK ==
356 GNUNET_CLIENT_transmit_and_get_response (client, 355 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
357 &msg.header,
358 GNUNET_TIME_UNIT_SECONDS, 356 GNUNET_TIME_UNIT_SECONDS,
359 GNUNET_YES, 357 GNUNET_YES, NULL,
360 NULL, NULL)); 358 NULL));
361} 359}
362 360
363 361
@@ -375,9 +373,7 @@ addr_cb (void *cls,
375 */ 373 */
376struct GNUNET_NAT_Test * 374struct GNUNET_NAT_Test *
377GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 375GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
378 int is_tcp, 376 int is_tcp, uint16_t bnd_port, uint16_t adv_port,
379 uint16_t bnd_port,
380 uint16_t adv_port,
381 GNUNET_NAT_TestCallback report, void *report_cls) 377 GNUNET_NAT_TestCallback report, void *report_cls)
382{ 378{
383 struct GNUNET_NAT_Test *ret; 379 struct GNUNET_NAT_Test *ret;
@@ -401,19 +397,21 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
401 ret->report_cls = report_cls; 397 ret->report_cls = report_cls;
402 if (bnd_port == 0) 398 if (bnd_port == 0)
403 { 399 {
404 ret->nat = GNUNET_NAT_register (cfg, is_tcp, 400 ret->nat =
405 0, 401 GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
406 0, NULL, NULL, &addr_cb, &reversal_cb, ret); 402 &reversal_cb, ret);
407 } 403 }
408 else 404 else
409 { 405 {
410 ret->lsock = GNUNET_NETWORK_socket_create (AF_INET, 406 ret->lsock =
411 (is_tcp == GNUNET_YES) 407 GNUNET_NETWORK_socket_create (AF_INET,
412 ? SOCK_STREAM : SOCK_DGRAM, 0); 408 (is_tcp ==
409 GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
410 0);
413 if ((ret->lsock == NULL) || 411 if ((ret->lsock == NULL) ||
414 (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret->lsock, 412 (GNUNET_OK !=
415 (const struct sockaddr *) &sa, 413 GNUNET_NETWORK_socket_bind (ret->lsock, (const struct sockaddr *) &sa,
416 sizeof (sa)))) 414 sizeof (sa))))
417 { 415 {
418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
419 _ 417 _
@@ -428,18 +426,19 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
428 if (GNUNET_YES == is_tcp) 426 if (GNUNET_YES == is_tcp)
429 { 427 {
430 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (ret->lsock, 5)); 428 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (ret->lsock, 5));
431 ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 429 ret->ltask =
432 ret->lsock, &do_accept, ret); 430 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
431 ret->lsock, &do_accept, ret);
433 } 432 }
434 else 433 else
435 { 434 {
436 ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 435 ret->ltask =
437 ret->lsock, 436 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
438 &do_udp_read, ret); 437 ret->lsock, &do_udp_read, ret);
439 } 438 }
440 ret->nat = GNUNET_NAT_register (cfg, is_tcp, 439 ret->nat =
441 adv_port, 440 GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, addrs, addrlens,
442 1, addrs, addrlens, &addr_cb, NULL, ret); 441 &addr_cb, NULL, ret);
443 } 442 }
444 return ret; 443 return ret;
445} 444}
diff --git a/src/nat/test_nat.c b/src/nat/test_nat.c
index 12ff30cac..6a533798e 100644
--- a/src/nat/test_nat.c
+++ b/src/nat/test_nat.c
@@ -55,13 +55,13 @@
55 * believes to be valid for the transport. 55 * believes to be valid for the transport.
56 */ 56 */
57static void 57static void
58addr_callback (void *cls, int add_remove, 58addr_callback (void *cls, int add_remove, const struct sockaddr *addr,
59 const struct sockaddr *addr, socklen_t addrlen) 59 socklen_t addrlen)
60{ 60{
61 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 61 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Address changed: %s `%s' (%u bytes)\n",
62 "Address changed: %s `%s' (%u bytes)\n", 62 add_remove == GNUNET_YES ? "added" : "removed", GNUNET_a2s (addr,
63 add_remove == GNUNET_YES ? "added" : "removed", 63 addrlen),
64 GNUNET_a2s (addr, addrlen), (unsigned int) addrlen); 64 (unsigned int) addrlen);
65} 65}
66 66
67 67
@@ -97,9 +97,8 @@ struct addr_cls
97 * @return GNUNET_OK to continue iterating 97 * @return GNUNET_OK to continue iterating
98 */ 98 */
99static int 99static int
100process_if (void *cls, 100process_if (void *cls, const char *name, int isDefault,
101 const char *name, 101 const struct sockaddr *addr, socklen_t addrlen)
102 int isDefault, const struct sockaddr *addr, socklen_t addrlen)
103{ 102{
104 struct addr_cls *data = cls; 103 struct addr_cls *data = cls;
105 104
@@ -119,9 +118,8 @@ process_if (void *cls,
119 * Main function run with scheduler. 118 * Main function run with scheduler.
120 */ 119 */
121static void 120static void
122run (void *cls, 121run (void *cls, char *const *args, const char *cfgfile,
123 char *const *args, 122 const struct GNUNET_CONFIGURATION_Handle *cfg)
124 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
125{ 123{
126 struct GNUNET_NAT_Handle *nat; 124 struct GNUNET_NAT_Handle *nat;
127 struct addr_cls data; 125 struct addr_cls data;
@@ -147,9 +145,7 @@ run (void *cls,
147 GNUNET_a2s (addr, data.addrlen)); 145 GNUNET_a2s (addr, data.addrlen));
148 146
149 nat = GNUNET_NAT_register (cfg, GNUNET_YES /* tcp */ , 147 nat = GNUNET_NAT_register (cfg, GNUNET_YES /* tcp */ ,
150 2086, 148 2086, 1, (const struct sockaddr **) &addr,
151 1,
152 (const struct sockaddr **) &addr,
153 &data.addrlen, &addr_callback, NULL, NULL); 149 &data.addrlen, &addr_callback, NULL, NULL);
154 GNUNET_free (addr); 150 GNUNET_free (addr);
155 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat); 151 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat);
diff --git a/src/nat/test_nat_mini.c b/src/nat/test_nat_mini.c
index 8f689d6bc..4aac3d17f 100644
--- a/src/nat/test_nat_mini.c
+++ b/src/nat/test_nat_mini.c
@@ -46,13 +46,13 @@
46 * believes to be valid for the transport. 46 * believes to be valid for the transport.
47 */ 47 */
48static void 48static void
49addr_callback (void *cls, int add_remove, 49addr_callback (void *cls, int add_remove, const struct sockaddr *addr,
50 const struct sockaddr *addr, socklen_t addrlen) 50 socklen_t addrlen)
51{ 51{
52 fprintf (stderr, 52 fprintf (stderr, "Address changed: %s `%s' (%u bytes)\n",
53 "Address changed: %s `%s' (%u bytes)\n", 53 add_remove == GNUNET_YES ? "added" : "removed", GNUNET_a2s (addr,
54 add_remove == GNUNET_YES ? "added" : "removed", 54 addrlen),
55 GNUNET_a2s (addr, addrlen), (unsigned int) addrlen); 55 (unsigned int) addrlen);
56} 56}
57 57
58 58
@@ -74,9 +74,8 @@ stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 * Main function run with scheduler. 74 * Main function run with scheduler.
75 */ 75 */
76static void 76static void
77run (void *cls, 77run (void *cls, char *const *args, const char *cfgfile,
78 char *const *args, 78 const struct GNUNET_CONFIGURATION_Handle *cfg)
79 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
80{ 79{
81 struct GNUNET_NAT_MiniHandle *mini; 80 struct GNUNET_NAT_MiniHandle *mini;
82 81
diff --git a/src/nat/test_nat_test.c b/src/nat/test_nat_test.c
index 520d82d92..b1e6235f9 100644
--- a/src/nat/test_nat_test.c
+++ b/src/nat/test_nat_test.c
@@ -65,12 +65,12 @@ report_success (void *cls, int success)
65 * Main function run with scheduler. 65 * Main function run with scheduler.
66 */ 66 */
67static void 67static void
68run (void *cls, 68run (void *cls, char *const *args, const char *cfgfile,
69 char *const *args, 69 const struct GNUNET_CONFIGURATION_Handle *cfg)
70 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
71{ 70{
72 tst = GNUNET_NAT_test_start (cfg, GNUNET_YES, 71 tst =
73 1285, 1285, &report_success, NULL); 72 GNUNET_NAT_test_start (cfg, GNUNET_YES, 1285, 1285, &report_success,
73 NULL);
74 if (NULL == tst) 74 if (NULL == tst)
75 return; 75 return;
76 end = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL); 76 end = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
@@ -105,16 +105,16 @@ main (int argc, char *const argv[])
105 "WARNING", 105 "WARNING",
106#endif 106#endif
107 NULL); 107 NULL);
108 gns = GNUNET_OS_start_process (NULL, NULL, 108 gns =
109 "gnunet-nat-server", "gnunet-nat-server", 109 GNUNET_OS_start_process (NULL, NULL, "gnunet-nat-server",
110 "gnunet-nat-server",
110#if VERBOSE 111#if VERBOSE
111 "-L", "DEBUG", 112 "-L", "DEBUG",
112#endif 113#endif
113 "-c", "test_nat_test_data.conf", 114 "-c", "test_nat_test_data.conf", "12345", NULL);
114 "12345", NULL);
115 GNUNET_assert (NULL != gns); 115 GNUNET_assert (NULL != gns);
116 GNUNET_PROGRAM_run (5, argv_prog, 116 GNUNET_PROGRAM_run (5, argv_prog, "test-nat-test", "nohelp", options, &run,
117 "test-nat-test", "nohelp", options, &run, NULL); 117 NULL);
118 GNUNET_break (0 == GNUNET_OS_process_kill (gns, SIGTERM)); 118 GNUNET_break (0 == GNUNET_OS_process_kill (gns, SIGTERM));
119 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns)); 119 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns));
120 GNUNET_OS_process_close (gns); 120 GNUNET_OS_process_close (gns);