aboutsummaryrefslogtreecommitdiff
path: root/src/nat/gnunet-helper-nat-server.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/nat/gnunet-helper-nat-server.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/nat/gnunet-helper-nat-server.c')
-rw-r--r--src/nat/gnunet-helper-nat-server.c560
1 files changed, 283 insertions, 277 deletions
diff --git a/src/nat/gnunet-helper-nat-server.c b/src/nat/gnunet-helper-nat-server.c
index f087590c8..38d7d22c8 100644
--- a/src/nat/gnunet-helper-nat-server.c
+++ b/src/nat/gnunet-helper-nat-server.c
@@ -79,7 +79,9 @@
79 * @param src source of the copy, may be NULL if @a n is zero 79 * @param src source of the copy, may be NULL if @a n is zero
80 * @param n number of bytes to copy 80 * @param n number of bytes to copy
81 */ 81 */
82#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) 82#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void) memcpy (dst, src, \
83 n); \
84 } } while (0)
83 85
84/** 86/**
85 * Should we print some debug output? 87 * Should we print some debug output?
@@ -109,7 +111,8 @@
109/** 111/**
110 * IPv4 header. 112 * IPv4 header.
111 */ 113 */
112struct ip_header { 114struct ip_header
115{
113 /** 116 /**
114 * Version (4 bits) + Internet header length (4 bits) 117 * Version (4 bits) + Internet header length (4 bits)
115 */ 118 */
@@ -164,7 +167,8 @@ struct ip_header {
164/** 167/**
165 * Format of ICMP packet. 168 * Format of ICMP packet.
166 */ 169 */
167struct icmp_ttl_exceeded_header { 170struct icmp_ttl_exceeded_header
171{
168 uint8_t type; 172 uint8_t type;
169 173
170 uint8_t code; 174 uint8_t code;
@@ -176,7 +180,8 @@ struct icmp_ttl_exceeded_header {
176 /* followed by original payload */ 180 /* followed by original payload */
177}; 181};
178 182
179struct icmp_echo_header { 183struct icmp_echo_header
184{
180 uint8_t type; 185 uint8_t type;
181 186
182 uint8_t code; 187 uint8_t code;
@@ -190,7 +195,8 @@ struct icmp_echo_header {
190/** 195/**
191 * Beginning of UDP packet. 196 * Beginning of UDP packet.
192 */ 197 */
193struct udp_header { 198struct udp_header
199{
194 uint16_t src_port; 200 uint16_t src_port;
195 201
196 uint16_t dst_port; 202 uint16_t dst_port;
@@ -229,7 +235,7 @@ static struct in_addr dummy;
229 * @return the CRC 16. 235 * @return the CRC 16.
230 */ 236 */
231static uint16_t 237static uint16_t
232calc_checksum(const uint16_t * data, unsigned int bytes) 238calc_checksum (const uint16_t *data, unsigned int bytes)
233{ 239{
234 uint32_t sum; 240 uint32_t sum;
235 unsigned int i; 241 unsigned int i;
@@ -238,7 +244,7 @@ calc_checksum(const uint16_t * data, unsigned int bytes)
238 for (i = 0; i < bytes / 2; i++) 244 for (i = 0; i < bytes / 2; i++)
239 sum += data[i]; 245 sum += data[i];
240 sum = (sum & 0xffff) + (sum >> 16); 246 sum = (sum & 0xffff) + (sum >> 16);
241 sum = htons(0xffff - sum); 247 sum = htons (0xffff - sum);
242 return sum; 248 return sum;
243} 249}
244 250
@@ -249,7 +255,7 @@ calc_checksum(const uint16_t * data, unsigned int bytes)
249 * @param my_ip source address (our ip address) 255 * @param my_ip source address (our ip address)
250 */ 256 */
251static void 257static void
252send_icmp_echo(const struct in_addr *my_ip) 258send_icmp_echo (const struct in_addr *my_ip)
253{ 259{
254 char packet[sizeof(struct ip_header) + sizeof(struct icmp_echo_header)]; 260 char packet[sizeof(struct ip_header) + sizeof(struct icmp_echo_header)];
255 struct icmp_echo_header icmp_echo; 261 struct icmp_echo_header icmp_echo;
@@ -261,8 +267,8 @@ send_icmp_echo(const struct in_addr *my_ip)
261 off = 0; 267 off = 0;
262 ip_pkt.vers_ihl = 0x45; 268 ip_pkt.vers_ihl = 0x45;
263 ip_pkt.tos = 0; 269 ip_pkt.tos = 0;
264 ip_pkt.pkt_len = htons(sizeof(packet)); 270 ip_pkt.pkt_len = htons (sizeof(packet));
265 ip_pkt.id = htons(PACKET_ID); 271 ip_pkt.id = htons (PACKET_ID);
266 ip_pkt.flags_frag_offset = 0; 272 ip_pkt.flags_frag_offset = 0;
267 ip_pkt.ttl = IPDEFTTL; 273 ip_pkt.ttl = IPDEFTTL;
268 ip_pkt.proto = IPPROTO_ICMP; 274 ip_pkt.proto = IPPROTO_ICMP;
@@ -270,11 +276,11 @@ send_icmp_echo(const struct in_addr *my_ip)
270 ip_pkt.src_ip = my_ip->s_addr; 276 ip_pkt.src_ip = my_ip->s_addr;
271 ip_pkt.dst_ip = dummy.s_addr; 277 ip_pkt.dst_ip = dummy.s_addr;
272 ip_pkt.checksum = 278 ip_pkt.checksum =
273 htons(calc_checksum((uint16_t *)&ip_pkt, 279 htons (calc_checksum ((uint16_t *) &ip_pkt,
274 sizeof(struct ip_header))); 280 sizeof(struct ip_header)));
275 GNUNET_memcpy(&packet[off], 281 GNUNET_memcpy (&packet[off],
276 &ip_pkt, 282 &ip_pkt,
277 sizeof(struct ip_header)); 283 sizeof(struct ip_header));
278 off += sizeof(struct ip_header); 284 off += sizeof(struct ip_header);
279 285
280 icmp_echo.type = ICMP_ECHO; 286 icmp_echo.type = ICMP_ECHO;
@@ -282,39 +288,39 @@ send_icmp_echo(const struct in_addr *my_ip)
282 icmp_echo.checksum = 0; 288 icmp_echo.checksum = 0;
283 icmp_echo.reserved = 0; 289 icmp_echo.reserved = 0;
284 icmp_echo.checksum = 290 icmp_echo.checksum =
285 htons(calc_checksum 291 htons (calc_checksum
286 ((uint16_t *)&icmp_echo, 292 ((uint16_t *) &icmp_echo,
287 sizeof(struct icmp_echo_header))); 293 sizeof(struct icmp_echo_header)));
288 GNUNET_memcpy(&packet[off], 294 GNUNET_memcpy (&packet[off],
289 &icmp_echo, 295 &icmp_echo,
290 sizeof(struct icmp_echo_header)); 296 sizeof(struct icmp_echo_header));
291 off += sizeof(struct icmp_echo_header); 297 off += sizeof(struct icmp_echo_header);
292 298
293 memset(&dst, 0, sizeof(dst)); 299 memset (&dst, 0, sizeof(dst));
294 dst.sin_family = AF_INET; 300 dst.sin_family = AF_INET;
295#if HAVE_SOCKADDR_IN_SIN_LEN 301#if HAVE_SOCKADDR_IN_SIN_LEN
296 dst.sin_len = sizeof(struct sockaddr_in); 302 dst.sin_len = sizeof(struct sockaddr_in);
297#endif 303#endif
298 dst.sin_addr = dummy; 304 dst.sin_addr = dummy;
299 err = sendto(rawsock, 305 err = sendto (rawsock,
300 packet, 306 packet,
301 off, 307 off,
302 0, 308 0,
303 (struct sockaddr *)&dst, 309 (struct sockaddr *) &dst,
304 sizeof(dst)); 310 sizeof(dst));
305 if (err < 0) 311 if (err < 0)
306 { 312 {
307#if VERBOSE 313#if VERBOSE
308 fprintf(stderr, 314 fprintf (stderr,
309 "sendto failed: %s\n", 315 "sendto failed: %s\n",
310 strerror(errno)); 316 strerror (errno));
311#endif 317#endif
312 } 318 }
313 else if (sizeof(packet) != err) 319 else if (sizeof(packet) != err)
314 { 320 {
315 fprintf(stderr, 321 fprintf (stderr,
316 "Error: partial send of ICMP message\n"); 322 "Error: partial send of ICMP message\n");
317 } 323 }
318} 324}
319 325
320 326
@@ -322,37 +328,37 @@ send_icmp_echo(const struct in_addr *my_ip)
322 * Send a UDP message to the dummy IP. 328 * Send a UDP message to the dummy IP.
323 */ 329 */
324static void 330static void
325send_udp() 331send_udp ()
326{ 332{
327 struct sockaddr_in dst; 333 struct sockaddr_in dst;
328 ssize_t err; 334 ssize_t err;
329 335
330 memset(&dst, 0, sizeof(dst)); 336 memset (&dst, 0, sizeof(dst));
331 dst.sin_family = AF_INET; 337 dst.sin_family = AF_INET;
332#if HAVE_SOCKADDR_IN_SIN_LEN 338#if HAVE_SOCKADDR_IN_SIN_LEN
333 dst.sin_len = sizeof(struct sockaddr_in); 339 dst.sin_len = sizeof(struct sockaddr_in);
334#endif 340#endif
335 dst.sin_addr = dummy; 341 dst.sin_addr = dummy;
336 dst.sin_port = htons(NAT_TRAV_PORT); 342 dst.sin_port = htons (NAT_TRAV_PORT);
337 err = sendto(udpsock, 343 err = sendto (udpsock,
338 NULL, 344 NULL,
339 0, 345 0,
340 0, 346 0,
341 (struct sockaddr *)&dst, 347 (struct sockaddr *) &dst,
342 sizeof(dst)); 348 sizeof(dst));
343 if (err < 0) 349 if (err < 0)
344 { 350 {
345#if VERBOSE 351#if VERBOSE
346 fprintf(stderr, 352 fprintf (stderr,
347 "sendto failed: %s\n", 353 "sendto failed: %s\n",
348 strerror(errno)); 354 strerror (errno));
349#endif 355#endif
350 } 356 }
351 else if (0 != err) 357 else if (0 != err)
352 { 358 {
353 fprintf(stderr, 359 fprintf (stderr,
354 "Error: partial send of ICMP message\n"); 360 "Error: partial send of ICMP message\n");
355 } 361 }
356} 362}
357 363
358 364
@@ -360,7 +366,7 @@ send_udp()
360 * We've received an ICMP response. Process it. 366 * We've received an ICMP response. Process it.
361 */ 367 */
362static void 368static void
363process_icmp_response() 369process_icmp_response ()
364{ 370{
365 char buf[65536]; 371 char buf[65536];
366 ssize_t have; 372 ssize_t have;
@@ -372,96 +378,96 @@ process_icmp_response()
372 size_t off; 378 size_t off;
373 uint16_t port; 379 uint16_t port;
374 380
375 have = read(icmpsock, buf, sizeof(buf)); 381 have = read (icmpsock, buf, sizeof(buf));
376 if (-1 == have) 382 if (-1 == have)
377 { 383 {
378 fprintf(stderr, 384 fprintf (stderr,
379 "Error reading raw socket: %s\n", 385 "Error reading raw socket: %s\n",
380 strerror(errno)); 386 strerror (errno));
381 return; 387 return;
382 } 388 }
383#if VERBOSE 389#if VERBOSE
384 fprintf(stderr, 390 fprintf (stderr,
385 "Received message of %u bytes\n", 391 "Received message of %u bytes\n",
386 (unsigned int)have); 392 (unsigned int) have);
387#endif 393#endif
388 if (have < 394 if (have <
389 (ssize_t)(sizeof(struct ip_header) + 395 (ssize_t) (sizeof(struct ip_header)
390 sizeof(struct icmp_ttl_exceeded_header) + 396 + sizeof(struct icmp_ttl_exceeded_header)
391 sizeof(struct ip_header))) 397 + sizeof(struct ip_header)))
392 { 398 {
393 /* malformed */ 399 /* malformed */
394 return; 400 return;
395 } 401 }
396 off = 0; 402 off = 0;
397 GNUNET_memcpy(&ip_pkt, 403 GNUNET_memcpy (&ip_pkt,
398 &buf[off], 404 &buf[off],
399 sizeof(struct ip_header)); 405 sizeof(struct ip_header));
400 off += sizeof(struct ip_header); 406 off += sizeof(struct ip_header);
401 GNUNET_memcpy(&icmp_ttl, 407 GNUNET_memcpy (&icmp_ttl,
402 &buf[off], 408 &buf[off],
403 sizeof(struct icmp_ttl_exceeded_header)); 409 sizeof(struct icmp_ttl_exceeded_header));
404 off += sizeof(struct icmp_ttl_exceeded_header); 410 off += sizeof(struct icmp_ttl_exceeded_header);
405 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code)) 411 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
406 { 412 {
407 /* different type than what we want */ 413 /* different type than what we want */
408 return; 414 return;
409 } 415 }
410 /* grab source IP of 1st IP header */ 416 /* grab source IP of 1st IP header */
411 source_ip.s_addr = ip_pkt.src_ip; 417 source_ip.s_addr = ip_pkt.src_ip;
412 418
413 /* skip 2nd IP header */ 419 /* skip 2nd IP header */
414 GNUNET_memcpy(&ip_pkt, 420 GNUNET_memcpy (&ip_pkt,
415 &buf[off], 421 &buf[off],
416 sizeof(struct ip_header)); 422 sizeof(struct ip_header));
417 off += sizeof(struct ip_header); 423 off += sizeof(struct ip_header);
418 424
419 switch (ip_pkt.proto) 425 switch (ip_pkt.proto)
426 {
427 case IPPROTO_ICMP:
428 if (have !=
429 (sizeof(struct ip_header) * 2
430 + sizeof(struct icmp_ttl_exceeded_header)
431 + sizeof(struct icmp_echo_header)))
420 { 432 {
421 case IPPROTO_ICMP: 433 /* malformed */
422 if (have != 434 return;
423 (sizeof(struct ip_header) * 2 + 435 }
424 sizeof(struct icmp_ttl_exceeded_header) + 436 /* grab ICMP ECHO content */
425 sizeof(struct icmp_echo_header))) 437 GNUNET_memcpy (&icmp_echo,
426 { 438 &buf[off],
427 /* malformed */ 439 sizeof(struct icmp_echo_header));
428 return; 440 port = (uint16_t) ntohl (icmp_echo.reserved);
429 } 441 break;
430 /* grab ICMP ECHO content */ 442
431 GNUNET_memcpy(&icmp_echo, 443 case IPPROTO_UDP:
432 &buf[off], 444 if (have !=
433 sizeof(struct icmp_echo_header)); 445 (sizeof(struct ip_header) * 2
434 port = (uint16_t)ntohl(icmp_echo.reserved); 446 + sizeof(struct icmp_ttl_exceeded_header) + sizeof(struct udp_header)))
435 break; 447 {
436 448 /* malformed */
437 case IPPROTO_UDP:
438 if (have !=
439 (sizeof(struct ip_header) * 2 +
440 sizeof(struct icmp_ttl_exceeded_header) + sizeof(struct udp_header)))
441 {
442 /* malformed */
443 return;
444 }
445 /* grab UDP content */
446 GNUNET_memcpy(&udp_pkt,
447 &buf[off],
448 sizeof(struct udp_header));
449 port = ntohs(udp_pkt.length);
450 break;
451
452 default:
453 /* different type than what we want */
454 return; 449 return;
455 } 450 }
451 /* grab UDP content */
452 GNUNET_memcpy (&udp_pkt,
453 &buf[off],
454 sizeof(struct udp_header));
455 port = ntohs (udp_pkt.length);
456 break;
457
458 default:
459 /* different type than what we want */
460 return;
461 }
456 462
457 if (port == 0) 463 if (port == 0)
458 fprintf(stdout, "%s\n", 464 fprintf (stdout, "%s\n",
459 inet_ntop(AF_INET, &source_ip, buf, sizeof(buf))); 465 inet_ntop (AF_INET, &source_ip, buf, sizeof(buf)));
460 else 466 else
461 fprintf(stdout, "%s:%u\n", 467 fprintf (stdout, "%s:%u\n",
462 inet_ntop(AF_INET, &source_ip, buf, sizeof(buf)), 468 inet_ntop (AF_INET, &source_ip, buf, sizeof(buf)),
463 (unsigned int)port); 469 (unsigned int) port);
464 fflush(stdout); 470 fflush (stdout);
465} 471}
466 472
467 473
@@ -471,34 +477,34 @@ process_icmp_response()
471 * @return -1 on error, 0 on success 477 * @return -1 on error, 0 on success
472 */ 478 */
473static int 479static int
474setup_raw_socket() 480setup_raw_socket ()
475{ 481{
476 const int one = 1; 482 const int one = 1;
477 483
478 if (-1 == 484 if (-1 ==
479 setsockopt(rawsock, 485 setsockopt (rawsock,
480 SOL_SOCKET, 486 SOL_SOCKET,
481 SO_BROADCAST, 487 SO_BROADCAST,
482 (char *)&one, 488 (char *) &one,
483 sizeof(one))) 489 sizeof(one)))
484 { 490 {
485 fprintf(stderr, 491 fprintf (stderr,
486 "setsockopt failed: %s\n", 492 "setsockopt failed: %s\n",
487 strerror(errno)); 493 strerror (errno));
488 return -1; 494 return -1;
489 } 495 }
490 if (-1 == 496 if (-1 ==
491 setsockopt(rawsock, 497 setsockopt (rawsock,
492 IPPROTO_IP, 498 IPPROTO_IP,
493 IP_HDRINCL, 499 IP_HDRINCL,
494 (char *)&one, 500 (char *) &one,
495 sizeof(one))) 501 sizeof(one)))
496 { 502 {
497 fprintf(stderr, 503 fprintf (stderr,
498 "setsockopt failed: %s\n", 504 "setsockopt failed: %s\n",
499 strerror(errno)); 505 strerror (errno));
500 return -1; 506 return -1;
501 } 507 }
502 return 0; 508 return 0;
503} 509}
504 510
@@ -510,45 +516,45 @@ setup_raw_socket()
510 * @return -1 on error 516 * @return -1 on error
511 */ 517 */
512static int 518static int
513make_udp_socket(const struct in_addr *my_ip) 519make_udp_socket (const struct in_addr *my_ip)
514{ 520{
515 int ret; 521 int ret;
516 struct sockaddr_in addr; 522 struct sockaddr_in addr;
517 523
518 ret = socket(AF_INET, SOCK_DGRAM, 0); 524 ret = socket (AF_INET, SOCK_DGRAM, 0);
519 if (-1 == ret) 525 if (-1 == ret)
520 { 526 {
521 fprintf(stderr, 527 fprintf (stderr,
522 "Error opening UDP socket: %s\n", 528 "Error opening UDP socket: %s\n",
523 strerror(errno)); 529 strerror (errno));
524 return -1; 530 return -1;
525 } 531 }
526 memset(&addr, 0, sizeof(addr)); 532 memset (&addr, 0, sizeof(addr));
527 addr.sin_family = AF_INET; 533 addr.sin_family = AF_INET;
528#if HAVE_SOCKADDR_IN_SIN_LEN 534#if HAVE_SOCKADDR_IN_SIN_LEN
529 addr.sin_len = sizeof(struct sockaddr_in); 535 addr.sin_len = sizeof(struct sockaddr_in);
530#endif 536#endif
531 addr.sin_addr = *my_ip; 537 addr.sin_addr = *my_ip;
532 addr.sin_port = htons(NAT_TRAV_PORT); 538 addr.sin_port = htons (NAT_TRAV_PORT);
533 539
534 if (0 != bind(ret, 540 if (0 != bind (ret,
535 (struct sockaddr *)&addr, 541 (struct sockaddr *) &addr,
536 sizeof(addr))) 542 sizeof(addr)))
537 { 543 {
538 fprintf(stderr, 544 fprintf (stderr,
539 "Error binding UDP socket to port %u: %s\n", 545 "Error binding UDP socket to port %u: %s\n",
540 NAT_TRAV_PORT, 546 NAT_TRAV_PORT,
541 strerror(errno)); 547 strerror (errno));
542 (void)close(ret); 548 (void) close (ret);
543 return -1; 549 return -1;
544 } 550 }
545 return ret; 551 return ret;
546} 552}
547 553
548 554
549int 555int
550main(int argc, 556main (int argc,
551 char *const *argv) 557 char *const *argv)
552{ 558{
553 struct in_addr external; 559 struct in_addr external;
554 fd_set rs; 560 fd_set rs;
@@ -560,147 +566,147 @@ main(int argc,
560 int global_ret; 566 int global_ret;
561 567
562 /* Create an ICMP raw socket for reading (we'll check errors later) */ 568 /* Create an ICMP raw socket for reading (we'll check errors later) */
563 icmpsock = socket(AF_INET, 569 icmpsock = socket (AF_INET,
564 SOCK_RAW, 570 SOCK_RAW,
565 IPPROTO_ICMP); 571 IPPROTO_ICMP);
566 icmp_eno = errno; 572 icmp_eno = errno;
567 573
568 /* Create an (ICMP) raw socket for writing (we'll check errors later) */ 574 /* Create an (ICMP) raw socket for writing (we'll check errors later) */
569 rawsock = socket(AF_INET, 575 rawsock = socket (AF_INET,
570 SOCK_RAW, 576 SOCK_RAW,
571 IPPROTO_RAW); 577 IPPROTO_RAW);
572 raw_eno = errno; 578 raw_eno = errno;
573 udpsock = -1; 579 udpsock = -1;
574 580
575 /* drop root rights */ 581 /* drop root rights */
576 uid = getuid(); 582 uid = getuid ();
577#ifdef HAVE_SETRESUID 583#ifdef HAVE_SETRESUID
578 if (0 != setresuid(uid, uid, uid)) 584 if (0 != setresuid (uid, uid, uid))
579 { 585 {
580 fprintf(stderr, 586 fprintf (stderr,
581 "Failed to setresuid: %s\n", 587 "Failed to setresuid: %s\n",
582 strerror(errno)); 588 strerror (errno));
583 global_ret = 1; 589 global_ret = 1;
584 goto error_exit; 590 goto error_exit;
585 } 591 }
586#else 592#else
587 if (0 != (setuid(uid) | seteuid(uid))) 593 if (0 != (setuid (uid) | seteuid (uid)))
588 { 594 {
589 fprintf(stderr, 595 fprintf (stderr,
590 "Failed to setuid: %s\n", 596 "Failed to setuid: %s\n",
591 strerror(errno)); 597 strerror (errno));
592 global_ret = 2; 598 global_ret = 2;
593 goto error_exit; 599 goto error_exit;
594 } 600 }
595#endif 601#endif
596 602
597 /* Now that we run without root rights, we can do error checking... */ 603 /* Now that we run without root rights, we can do error checking... */
598 if (2 != argc) 604 if (2 != argc)
599 { 605 {
600 fprintf(stderr, 606 fprintf (stderr,
601 "This program must be started with our (internal NAT) IP as the only argument.\n"); 607 "This program must be started with our (internal NAT) IP as the only argument.\n");
602 global_ret = 3; 608 global_ret = 3;
603 goto error_exit; 609 goto error_exit;
604 } 610 }
605 if (1 != inet_pton(AF_INET, argv[1], &external)) 611 if (1 != inet_pton (AF_INET, argv[1], &external))
606 { 612 {
607 fprintf(stderr, 613 fprintf (stderr,
608 "Error parsing IPv4 address: %s\n", 614 "Error parsing IPv4 address: %s\n",
609 strerror(errno)); 615 strerror (errno));
610 global_ret = 4; 616 global_ret = 4;
611 goto error_exit; 617 goto error_exit;
612 } 618 }
613 if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) 619 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
614 { 620 {
615 fprintf(stderr, 621 fprintf (stderr,
616 "Internal error converting dummy IP to binary.\n"); 622 "Internal error converting dummy IP to binary.\n");
617 global_ret = 5; 623 global_ret = 5;
618 goto error_exit; 624 goto error_exit;
619 } 625 }
620 626
621 /* error checking icmpsock */ 627 /* error checking icmpsock */
622 if (-1 == icmpsock) 628 if (-1 == icmpsock)
623 { 629 {
624 fprintf(stderr, 630 fprintf (stderr,
625 "Error opening RAW socket: %s\n", 631 "Error opening RAW socket: %s\n",
626 strerror(icmp_eno)); 632 strerror (icmp_eno));
627 global_ret = 6; 633 global_ret = 6;
628 goto error_exit; 634 goto error_exit;
629 } 635 }
630 if (icmpsock >= FD_SETSIZE) 636 if (icmpsock >= FD_SETSIZE)
631 { 637 {
632 /* this could happen if we were started with a large number of already-open 638 /* this could happen if we were started with a large number of already-open
633 file descriptors... */ 639 file descriptors... */
634 fprintf(stderr, 640 fprintf (stderr,
635 "Socket number too large (%d > %u)\n", 641 "Socket number too large (%d > %u)\n",
636 icmpsock, 642 icmpsock,
637 (unsigned int)FD_SETSIZE); 643 (unsigned int) FD_SETSIZE);
638 global_ret = 7; 644 global_ret = 7;
639 goto error_exit; 645 goto error_exit;
640 } 646 }
641 647
642 /* error checking rawsock */ 648 /* error checking rawsock */
643 if (-1 == rawsock) 649 if (-1 == rawsock)
644 { 650 {
645 fprintf(stderr, 651 fprintf (stderr,
646 "Error opening RAW socket: %s\n", 652 "Error opening RAW socket: %s\n",
647 strerror(raw_eno)); 653 strerror (raw_eno));
648 global_ret = 8; 654 global_ret = 8;
649 goto error_exit; 655 goto error_exit;
650 } 656 }
651 /* no need to check 'rawsock' against FD_SETSIZE as it is never used 657 /* no need to check 'rawsock' against FD_SETSIZE as it is never used
652 with 'select' */ 658 with 'select' */
653 659
654 if (0 != setup_raw_socket()) 660 if (0 != setup_raw_socket ())
655 { 661 {
656 global_ret = 9; 662 global_ret = 9;
657 goto error_exit; 663 goto error_exit;
658 } 664 }
659 665
660 if (-1 == (udpsock = make_udp_socket(&external))) 666 if (-1 == (udpsock = make_udp_socket (&external)))
661 { 667 {
662 global_ret = 10; 668 global_ret = 10;
663 goto error_exit; 669 goto error_exit;
664 } 670 }
665 671
666 alt = 0; 672 alt = 0;
667 while (1) 673 while (1)
674 {
675 FD_ZERO (&rs);
676 FD_SET (icmpsock, &rs);
677 tv.tv_sec = 0;
678 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
679 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
680 {
681 if (errno == EINTR)
682 continue;
683 fprintf (stderr,
684 "select failed: %s\n",
685 strerror (errno));
686 break;
687 }
688 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */
689 break;
690 if (FD_ISSET (icmpsock, &rs))
668 { 691 {
669 FD_ZERO(&rs); 692 process_icmp_response ();
670 FD_SET(icmpsock, &rs); 693 continue;
671 tv.tv_sec = 0;
672 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
673 if (-1 == select(icmpsock + 1, &rs, NULL, NULL, &tv))
674 {
675 if (errno == EINTR)
676 continue;
677 fprintf(stderr,
678 "select failed: %s\n",
679 strerror(errno));
680 break;
681 }
682 if (1 == getppid()) /* Check the parent process id, if 1 the parent has died, so we should die too */
683 break;
684 if (FD_ISSET(icmpsock, &rs))
685 {
686 process_icmp_response();
687 continue;
688 }
689 if (0 == (++alt % 2))
690 send_icmp_echo(&external);
691 else
692 send_udp();
693 } 694 }
695 if (0 == (++alt % 2))
696 send_icmp_echo (&external);
697 else
698 send_udp ();
699 }
694 700
695 /* select failed (internal error or OS out of resources) */ 701 /* select failed (internal error or OS out of resources) */
696 global_ret = 11; 702 global_ret = 11;
697error_exit: 703error_exit:
698 if (-1 != icmpsock) 704 if (-1 != icmpsock)
699 (void)close(icmpsock); 705 (void) close (icmpsock);
700 if (-1 != rawsock) 706 if (-1 != rawsock)
701 (void)close(rawsock); 707 (void) close (rawsock);
702 if (-1 != udpsock) 708 if (-1 != udpsock)
703 (void)close(udpsock); 709 (void) close (udpsock);
704 return global_ret; 710 return global_ret;
705} 711}
706 712