aboutsummaryrefslogtreecommitdiff
path: root/src/nat/gnunet-helper-nat-server.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-01-02 20:13:38 +0100
committerChristian Grothoff <christian@grothoff.org>2017-01-02 20:13:38 +0100
commit60c6dfd1fe22a01a3f7723e7e2bb18d52895c527 (patch)
treef6ecf4f27ad1e91e4d49b061be0f4b05bda199f3 /src/nat/gnunet-helper-nat-server.c
parentf28533eb5fbd2b8e00bb351eb9fd1da322c70505 (diff)
downloadgnunet-60c6dfd1fe22a01a3f7723e7e2bb18d52895c527.tar.gz
gnunet-60c6dfd1fe22a01a3f7723e7e2bb18d52895c527.zip
fixes for starting and stopping gnunet-helper-nat-server
Diffstat (limited to 'src/nat/gnunet-helper-nat-server.c')
-rw-r--r--src/nat/gnunet-helper-nat-server.c115
1 files changed, 86 insertions, 29 deletions
diff --git a/src/nat/gnunet-helper-nat-server.c b/src/nat/gnunet-helper-nat-server.c
index 8c3df749d..ef8e97482 100644
--- a/src/nat/gnunet-helper-nat-server.c
+++ b/src/nat/gnunet-helper-nat-server.c
@@ -264,8 +264,11 @@ send_icmp_echo (const struct in_addr *my_ip)
264 ip_pkt.src_ip = my_ip->s_addr; 264 ip_pkt.src_ip = my_ip->s_addr;
265 ip_pkt.dst_ip = dummy.s_addr; 265 ip_pkt.dst_ip = dummy.s_addr;
266 ip_pkt.checksum = 266 ip_pkt.checksum =
267 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 267 htons (calc_checksum ((uint16_t *) & ip_pkt,
268 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 268 sizeof (struct ip_header)));
269 memcpy (&packet[off],
270 &ip_pkt,
271 sizeof (struct ip_header));
269 off += sizeof (struct ip_header); 272 off += sizeof (struct ip_header);
270 273
271 icmp_echo.type = ICMP_ECHO; 274 icmp_echo.type = ICMP_ECHO;
@@ -273,9 +276,12 @@ send_icmp_echo (const struct in_addr *my_ip)
273 icmp_echo.checksum = 0; 276 icmp_echo.checksum = 0;
274 icmp_echo.reserved = 0; 277 icmp_echo.reserved = 0;
275 icmp_echo.checksum = 278 icmp_echo.checksum =
276 htons (calc_checksum 279 htons (calc_checksum
277 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header))); 280 ((uint16_t *) & icmp_echo,
278 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); 281 sizeof (struct icmp_echo_header)));
282 memcpy (&packet[off],
283 &icmp_echo,
284 sizeof (struct icmp_echo_header));
279 off += sizeof (struct icmp_echo_header); 285 off += sizeof (struct icmp_echo_header);
280 286
281 memset (&dst, 0, sizeof (dst)); 287 memset (&dst, 0, sizeof (dst));
@@ -284,17 +290,24 @@ send_icmp_echo (const struct in_addr *my_ip)
284 dst.sin_len = sizeof (struct sockaddr_in); 290 dst.sin_len = sizeof (struct sockaddr_in);
285#endif 291#endif
286 dst.sin_addr = dummy; 292 dst.sin_addr = dummy;
287 err = 293 err = sendto (rawsock,
288 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst)); 294 packet,
295 off,
296 0,
297 (struct sockaddr *) &dst,
298 sizeof (dst));
289 if (err < 0) 299 if (err < 0)
290 { 300 {
291#if VERBOSE 301#if VERBOSE
292 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 302 fprintf (stderr,
303 "sendto failed: %s\n",
304 strerror (errno));
293#endif 305#endif
294 } 306 }
295 else if (sizeof (packet) != err) 307 else if (sizeof (packet) != err)
296 { 308 {
297 fprintf (stderr, "Error: partial send of ICMP message\n"); 309 fprintf (stderr,
310 "Error: partial send of ICMP message\n");
298 } 311 }
299} 312}
300 313
@@ -315,16 +328,24 @@ send_udp ()
315#endif 328#endif
316 dst.sin_addr = dummy; 329 dst.sin_addr = dummy;
317 dst.sin_port = htons (NAT_TRAV_PORT); 330 dst.sin_port = htons (NAT_TRAV_PORT);
318 err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst)); 331 err = sendto (udpsock,
332 NULL,
333 0,
334 0,
335 (struct sockaddr *) &dst,
336 sizeof (dst));
319 if (err < 0) 337 if (err < 0)
320 { 338 {
321#if VERBOSE 339#if VERBOSE
322 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 340 fprintf (stderr,
341 "sendto failed: %s\n",
342 strerror (errno));
323#endif 343#endif
324 } 344 }
325 else if (0 != err) 345 else if (0 != err)
326 { 346 {
327 fprintf (stderr, "Error: partial send of ICMP message\n"); 347 fprintf (stderr,
348 "Error: partial send of ICMP message\n");
328 } 349 }
329} 350}
330 351
@@ -348,11 +369,15 @@ process_icmp_response ()
348 have = read (icmpsock, buf, sizeof (buf)); 369 have = read (icmpsock, buf, sizeof (buf));
349 if (-1 == have) 370 if (-1 == have)
350 { 371 {
351 fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno)); 372 fprintf (stderr,
373 "Error reading raw socket: %s\n",
374 strerror (errno));
352 return; 375 return;
353 } 376 }
354#if VERBOSE 377#if VERBOSE
355 fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have); 378 fprintf (stderr,
379 "Received message of %u bytes\n",
380 (unsigned int) have);
356#endif 381#endif
357 if (have < 382 if (have <
358 (ssize_t) (sizeof (struct ip_header) + 383 (ssize_t) (sizeof (struct ip_header) +
@@ -433,15 +458,27 @@ setup_raw_socket ()
433 const int one = 1; 458 const int one = 1;
434 459
435 if (-1 == 460 if (-1 ==
436 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one))) 461 setsockopt (rawsock,
462 SOL_SOCKET,
463 SO_BROADCAST,
464 (char *) &one,
465 sizeof (one)))
437 { 466 {
438 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); 467 fprintf (stderr,
468 "setsockopt failed: %s\n",
469 strerror (errno));
439 return -1; 470 return -1;
440 } 471 }
441 if (-1 == 472 if (-1 ==
442 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one))) 473 setsockopt (rawsock,
474 IPPROTO_IP,
475 IP_HDRINCL,
476 (char *) &one,
477 sizeof (one)))
443 { 478 {
444 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); 479 fprintf (stderr,
480 "setsockopt failed: %s\n",
481 strerror (errno));
445 return -1; 482 return -1;
446 } 483 }
447 return 0; 484 return 0;
@@ -492,7 +529,8 @@ make_udp_socket (const struct in_addr *my_ip)
492 529
493 530
494int 531int
495main (int argc, char *const *argv) 532main (int argc,
533 char *const *argv)
496{ 534{
497 struct in_addr external; 535 struct in_addr external;
498 fd_set rs; 536 fd_set rs;
@@ -504,11 +542,15 @@ main (int argc, char *const *argv)
504 int global_ret; 542 int global_ret;
505 543
506 /* Create an ICMP raw socket for reading (we'll check errors later) */ 544 /* Create an ICMP raw socket for reading (we'll check errors later) */
507 icmpsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); 545 icmpsock = socket (AF_INET,
546 SOCK_RAW,
547 IPPROTO_ICMP);
508 icmp_eno = errno; 548 icmp_eno = errno;
509 549
510 /* Create an (ICMP) raw socket for writing (we'll check errors later) */ 550 /* Create an (ICMP) raw socket for writing (we'll check errors later) */
511 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); 551 rawsock = socket (AF_INET,
552 SOCK_RAW,
553 IPPROTO_RAW);
512 raw_eno = errno; 554 raw_eno = errno;
513 udpsock = -1; 555 udpsock = -1;
514 556
@@ -517,14 +559,18 @@ main (int argc, char *const *argv)
517#ifdef HAVE_SETRESUID 559#ifdef HAVE_SETRESUID
518 if (0 != setresuid (uid, uid, uid)) 560 if (0 != setresuid (uid, uid, uid))
519 { 561 {
520 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); 562 fprintf (stderr,
563 "Failed to setresuid: %s\n",
564 strerror (errno));
521 global_ret = 1; 565 global_ret = 1;
522 goto error_exit; 566 goto error_exit;
523 } 567 }
524#else 568#else
525 if (0 != (setuid (uid) | seteuid (uid))) 569 if (0 != (setuid (uid) | seteuid (uid)))
526 { 570 {
527 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno)); 571 fprintf (stderr,
572 "Failed to setuid: %s\n",
573 strerror (errno));
528 global_ret = 2; 574 global_ret = 2;
529 goto error_exit; 575 goto error_exit;
530 } 576 }
@@ -540,13 +586,16 @@ main (int argc, char *const *argv)
540 } 586 }
541 if (1 != inet_pton (AF_INET, argv[1], &external)) 587 if (1 != inet_pton (AF_INET, argv[1], &external))
542 { 588 {
543 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno)); 589 fprintf (stderr,
590 "Error parsing IPv4 address: %s\n",
591 strerror (errno));
544 global_ret = 4; 592 global_ret = 4;
545 goto error_exit; 593 goto error_exit;
546 } 594 }
547 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) 595 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
548 { 596 {
549 fprintf (stderr, "Internal error converting dummy IP to binary.\n"); 597 fprintf (stderr,
598 "Internal error converting dummy IP to binary.\n");
550 global_ret = 5; 599 global_ret = 5;
551 goto error_exit; 600 goto error_exit;
552 } 601 }
@@ -554,7 +603,9 @@ main (int argc, char *const *argv)
554 /* error checking icmpsock */ 603 /* error checking icmpsock */
555 if (-1 == icmpsock) 604 if (-1 == icmpsock)
556 { 605 {
557 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (icmp_eno)); 606 fprintf (stderr,
607 "Error opening RAW socket: %s\n",
608 strerror (icmp_eno));
558 global_ret = 6; 609 global_ret = 6;
559 goto error_exit; 610 goto error_exit;
560 } 611 }
@@ -562,7 +613,9 @@ main (int argc, char *const *argv)
562 { 613 {
563 /* this could happen if we were started with a large number of already-open 614 /* this could happen if we were started with a large number of already-open
564 file descriptors... */ 615 file descriptors... */
565 fprintf (stderr, "Socket number too large (%d > %u)\n", icmpsock, 616 fprintf (stderr,
617 "Socket number too large (%d > %u)\n",
618 icmpsock,
566 (unsigned int) FD_SETSIZE); 619 (unsigned int) FD_SETSIZE);
567 global_ret = 7; 620 global_ret = 7;
568 goto error_exit; 621 goto error_exit;
@@ -571,7 +624,9 @@ main (int argc, char *const *argv)
571 /* error checking rawsock */ 624 /* error checking rawsock */
572 if (-1 == rawsock) 625 if (-1 == rawsock)
573 { 626 {
574 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno)); 627 fprintf (stderr,
628 "Error opening RAW socket: %s\n",
629 strerror (raw_eno));
575 global_ret = 8; 630 global_ret = 8;
576 goto error_exit; 631 goto error_exit;
577 } 632 }
@@ -601,7 +656,9 @@ main (int argc, char *const *argv)
601 { 656 {
602 if (errno == EINTR) 657 if (errno == EINTR)
603 continue; 658 continue;
604 fprintf (stderr, "select failed: %s\n", strerror (errno)); 659 fprintf (stderr,
660 "select failed: %s\n",
661 strerror (errno));
605 break; 662 break;
606 } 663 }
607 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */ 664 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */