diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-02-04 09:44:21 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-02-04 09:44:21 +0000 |
commit | e53e1c7834c4df3fd43971e9eb2113ce73fd9a52 (patch) | |
tree | ed3c740a7460a20723fbdfca12f2b65091396ff7 /src | |
parent | ebcb0910344d8a148cd2fac4015b548471cc62ee (diff) | |
download | gnunet-e53e1c7834c4df3fd43971e9eb2113ce73fd9a52.tar.gz gnunet-e53e1c7834c4df3fd43971e9eb2113ce73fd9a52.zip |
fixes
Diffstat (limited to 'src')
-rw-r--r-- | src/transport/gnunet-nat-server.c | 57 |
1 files changed, 53 insertions, 4 deletions
diff --git a/src/transport/gnunet-nat-server.c b/src/transport/gnunet-nat-server.c index 88674fd2c..11e4e905a 100644 --- a/src/transport/gnunet-nat-server.c +++ b/src/transport/gnunet-nat-server.c | |||
@@ -118,6 +118,8 @@ static uint16_t udpports[NUM_UDP_PORTS]; | |||
118 | 118 | ||
119 | static int icmpsock; | 119 | static int icmpsock; |
120 | 120 | ||
121 | static int rawsock; | ||
122 | |||
121 | static struct in_addr dummy; | 123 | static struct in_addr dummy; |
122 | 124 | ||
123 | 125 | ||
@@ -284,7 +286,7 @@ send_icmp (const struct in_addr *my_ip, | |||
284 | memset (&dst, 0, sizeof (dst)); | 286 | memset (&dst, 0, sizeof (dst)); |
285 | dst.sin_family = AF_INET; | 287 | dst.sin_family = AF_INET; |
286 | dst.sin_addr = *other; | 288 | dst.sin_addr = *other; |
287 | err = sendto(icmpsock, | 289 | err = sendto(rawsock, |
288 | packet, | 290 | packet, |
289 | off, 0, | 291 | off, 0, |
290 | (struct sockaddr*)&dst, | 292 | (struct sockaddr*)&dst, |
@@ -354,12 +356,22 @@ process_icmp_response (const struct in_addr *my_ip, | |||
354 | off += sizeof (struct udp_packet); | 356 | off += sizeof (struct udp_packet); |
355 | 357 | ||
356 | /* If not ICMP and not TTL exceeded */ | 358 | /* If not ICMP and not TTL exceeded */ |
359 | if ( (ip_pkt.proto == IPPROTO_ICMP) && | ||
360 | (icmp_pkt.type == ICMP_DEST_UNREACH) && | ||
361 | (icmp_pkt.code == ICMP_HOST_UNREACH) ) | ||
362 | { | ||
363 | /* this is what is normal due to our UDP traffic */ | ||
364 | return; | ||
365 | } | ||
357 | if ( (ip_pkt.proto != IPPROTO_ICMP) || | 366 | if ( (ip_pkt.proto != IPPROTO_ICMP) || |
358 | (icmp_pkt.type != ICMP_TIME_EXCEEDED) || | 367 | (icmp_pkt.type != ICMP_TIME_EXCEEDED) || |
359 | (icmp_pkt.code != ICMP_NET_UNREACH) ) | 368 | (icmp_pkt.code != ICMP_NET_UNREACH) ) |
360 | { | 369 | { |
361 | fprintf (stderr, | 370 | fprintf (stderr, |
362 | "Received unexpected ICMP message contents, ignoring\n"); | 371 | "Received unexpected ICMP message contents (%u, %u, %u), ignoring\n", |
372 | ip_pkt.proto, | ||
373 | icmp_pkt.type, | ||
374 | icmp_pkt.code); | ||
363 | return; | 375 | return; |
364 | } | 376 | } |
365 | memcpy(&sip, &ip_pkt.src_ip, sizeof (sip)); | 377 | memcpy(&sip, &ip_pkt.src_ip, sizeof (sip)); |
@@ -392,10 +404,36 @@ process_icmp_response (const struct in_addr *my_ip, | |||
392 | static int | 404 | static int |
393 | make_icmp_socket () | 405 | make_icmp_socket () |
394 | { | 406 | { |
407 | int ret; | ||
408 | |||
409 | ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); | ||
410 | if (-1 == ret) | ||
411 | { | ||
412 | fprintf (stderr, | ||
413 | "Error opening RAW socket: %s\n", | ||
414 | strerror (errno)); | ||
415 | return -1; | ||
416 | } | ||
417 | if (ret >= FD_SETSIZE) | ||
418 | { | ||
419 | fprintf (stderr, | ||
420 | "Socket number too large (%d > %u)\n", | ||
421 | ret, | ||
422 | (unsigned int) FD_SETSIZE); | ||
423 | close (ret); | ||
424 | return -1; | ||
425 | } | ||
426 | return ret; | ||
427 | } | ||
428 | |||
429 | |||
430 | static int | ||
431 | make_raw_socket () | ||
432 | { | ||
395 | const int one = 1; | 433 | const int one = 1; |
396 | int ret; | 434 | int ret; |
397 | 435 | ||
398 | ret = socket (AF_INET, SOCK_RAW, 0); | 436 | ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); |
399 | if (-1 == ret) | 437 | if (-1 == ret) |
400 | { | 438 | { |
401 | fprintf (stderr, | 439 | fprintf (stderr, |
@@ -454,8 +492,14 @@ main (int argc, char *const *argv) | |||
454 | dst.sin_family = AF_INET; | 492 | dst.sin_family = AF_INET; |
455 | dst.sin_port = htons (NAT_TRAV_PORT); | 493 | dst.sin_port = htons (NAT_TRAV_PORT); |
456 | dst.sin_addr = dummy; | 494 | dst.sin_addr = dummy; |
495 | |||
457 | if (-1 == (icmpsock = make_icmp_socket())) | 496 | if (-1 == (icmpsock = make_icmp_socket())) |
458 | return 1; | 497 | return 1; |
498 | if (-1 == (rawsock = make_raw_socket())) | ||
499 | { | ||
500 | close (icmpsock); | ||
501 | return 1; | ||
502 | } | ||
459 | for (i=0;i<NUM_UDP_PORTS;i++) | 503 | for (i=0;i<NUM_UDP_PORTS;i++) |
460 | udpsocks[i] = make_udp_socket (&udpports[i]); | 504 | udpsocks[i] = make_udp_socket (&udpports[i]); |
461 | pos = 0; | 505 | pos = 0; |
@@ -464,11 +508,16 @@ main (int argc, char *const *argv) | |||
464 | FD_ZERO (&rs); | 508 | FD_ZERO (&rs); |
465 | FD_SET (icmpsock, &rs); | 509 | FD_SET (icmpsock, &rs); |
466 | tv.tv_sec = 0; | 510 | tv.tv_sec = 0; |
467 | tv.tv_usec = UDP_SEND_FREQUENCY_MS * 1000 * 1000; | 511 | tv.tv_usec = UDP_SEND_FREQUENCY_MS * 1000; |
468 | select (icmpsock + 1, &rs, NULL, NULL, &tv); | 512 | select (icmpsock + 1, &rs, NULL, NULL, &tv); |
469 | /* FIXME: do I need my external IP here? */ | 513 | /* FIXME: do I need my external IP here? */ |
470 | if (FD_ISSET (icmpsock, &rs)) | 514 | if (FD_ISSET (icmpsock, &rs)) |
471 | process_icmp_response (&external, icmpsock); | 515 | process_icmp_response (&external, icmpsock); |
516 | fprintf (stderr, | ||
517 | "Sending UDP message to %s:%u\n", | ||
518 | argv[2], | ||
519 | NAT_TRAV_PORT); | ||
520 | |||
472 | if (-1 == sendto (udpsocks[pos], | 521 | if (-1 == sendto (udpsocks[pos], |
473 | NULL, 0, 0, | 522 | NULL, 0, 0, |
474 | (struct sockaddr*) &dst, sizeof (dst))) | 523 | (struct sockaddr*) &dst, sizeof (dst))) |