1 /**
2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2009 Florian octo Forster <octo at verplant.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; only version 2 of the License is
8 * applicable.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #if STDC_HEADERS
25 # include <stdlib.h>
26 # include <stdio.h>
27 # include <string.h>
28 # include <inttypes.h>
29 # include <errno.h>
30 # include <assert.h>
31 #else
32 # error "You don't have the standard C99 header files installed"
33 #endif /* STDC_HEADERS */
35 #ifdef HAVE_STDINT_H
36 # include <stdint.h>
37 #endif
39 #if HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
43 #if HAVE_FCNTL_H
44 # include <fcntl.h>
45 #endif
46 #if HAVE_SYS_TYPES_H
47 # include <sys/types.h>
48 #endif
49 #if HAVE_SYS_STAT_H
50 # include <sys/stat.h>
51 #endif
53 #if TIME_WITH_SYS_TIME
54 # include <sys/time.h>
55 # include <time.h>
56 #else
57 # if HAVE_SYS_TIME_H
58 # include <sys/time.h>
59 # else
60 # include <time.h>
61 # endif
62 #endif
64 #if HAVE_SYS_SOCKET_H
65 # include <sys/socket.h>
66 #endif
68 #if HAVE_NETDB_H
69 # include <netdb.h>
70 #endif
72 #if HAVE_NETINET_IN_SYSTM_H
73 # include <netinet/in_systm.h>
74 #endif
75 #if HAVE_NETINET_IN_H
76 # include <netinet/in.h>
77 #endif
78 #if HAVE_NETINET_IP_H
79 # include <netinet/ip.h>
80 #endif
81 #if HAVE_NETINET_IP_ICMP_H
82 # include <netinet/ip_icmp.h>
83 #endif
84 #ifdef HAVE_NETINET_IP_VAR_H
85 # include <netinet/ip_var.h>
86 #endif
87 #if HAVE_NETINET_IP6_H
88 # include <netinet/ip6.h>
89 #endif
90 #if HAVE_NETINET_ICMP6_H
91 # include <netinet/icmp6.h>
92 #endif
94 #include "oping.h"
96 #if WITH_DEBUG
97 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
98 #else
99 # define dprintf(...) /**/
100 #endif
102 #define PING_ERRMSG_LEN 256
104 struct pinghost
105 {
106 /* username: name passed in by the user */
107 char *username;
108 /* hostname: name returned by the reverse lookup */
109 char *hostname;
110 struct sockaddr_storage *addr;
111 socklen_t addrlen;
112 int addrfamily;
113 int fd;
114 int ident;
115 int sequence;
116 struct timeval *timer;
117 double latency;
118 uint32_t dropped;
119 int recv_ttl;
120 char *data;
122 void *context;
124 struct pinghost *next;
125 };
127 struct pingobj
128 {
129 double timeout;
130 int ttl;
131 int addrfamily;
132 char *data;
134 struct sockaddr *srcaddr;
135 socklen_t srcaddrlen;
137 char errmsg[PING_ERRMSG_LEN];
139 pinghost_t *head;
140 };
142 /*
143 * private (static) functions
144 */
145 /* Even though Posix requires "strerror_r" to return an "int",
146 * some systems (e.g. the GNU libc) return a "char *" _and_
147 * ignore the second argument ... -tokkee */
148 char *sstrerror (int errnum, char *buf, size_t buflen)
149 {
150 buf[0] = 0;
152 #if !HAVE_STRERROR_R
153 {
154 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
155 }
156 /* #endif !HAVE_STRERROR_R */
158 #elif STRERROR_R_CHAR_P
159 {
160 char *temp;
161 temp = strerror_r (errnum, buf, buflen);
162 if (buf[0] == 0)
163 {
164 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
165 strncpy (buf, temp, buflen);
166 else
167 strncpy (buf, "strerror_r did not return "
168 "an error message", buflen);
169 }
170 }
171 /* #endif STRERROR_R_CHAR_P */
173 #else
174 if (strerror_r (errnum, buf, buflen) != 0)
175 {
176 snprintf (buf, buflen, "Error %i (%#x); "
177 "Additionally, strerror_r failed.",
178 errnum, errnum);
179 }
180 #endif /* STRERROR_R_CHAR_P */
182 buf[buflen - 1] = 0;
184 return (buf);
185 } /* char *sstrerror */
187 static void ping_set_error (pingobj_t *obj, const char *function,
188 const char *message)
189 {
190 snprintf (obj->errmsg, sizeof (obj->errmsg),
191 "%s: %s", function, message);
192 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
193 }
195 static void ping_set_errno (pingobj_t *obj, int error_number)
196 {
197 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
198 }
200 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
201 struct timeval *res)
202 {
203 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
204 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
206 while (res->tv_usec > 1000000)
207 {
208 res->tv_usec -= 1000000;
209 res->tv_sec++;
210 }
212 return (0);
213 }
215 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
216 struct timeval *res)
217 {
218 if ((tv1->tv_sec < tv2->tv_sec)
219 || ((tv1->tv_sec == tv2->tv_sec)
220 && (tv1->tv_usec < tv2->tv_usec)))
221 return (-1);
223 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
224 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
226 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
228 while (res->tv_usec < 0)
229 {
230 res->tv_usec += 1000000;
231 res->tv_sec--;
232 }
234 return (0);
235 }
237 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
238 {
239 uint32_t sum = 0;
240 uint16_t ret = 0;
242 uint16_t *ptr;
244 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
245 sum += *ptr;
247 if (len == 1)
248 {
249 *(char *) &ret = *(char *) ptr;
250 sum += ret;
251 }
253 /* Do this twice to get all possible carries.. */
254 sum = (sum >> 16) + (sum & 0xFFFF);
255 sum = (sum >> 16) + (sum & 0xFFFF);
257 ret = ~sum;
259 return (ret);
260 }
262 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
263 {
264 struct ip *ip_hdr;
265 struct icmp *icmp_hdr;
267 size_t ip_hdr_len;
269 uint16_t recv_checksum;
270 uint16_t calc_checksum;
272 uint16_t ident;
273 uint16_t seq;
275 pinghost_t *ptr;
277 if (buffer_len < sizeof (struct ip))
278 return (NULL);
280 ip_hdr = (struct ip *) buffer;
281 ip_hdr_len = ip_hdr->ip_hl << 2;
283 if (buffer_len < ip_hdr_len)
284 return (NULL);
286 buffer += ip_hdr_len;
287 buffer_len -= ip_hdr_len;
289 if (buffer_len < sizeof (struct icmp))
290 return (NULL);
292 icmp_hdr = (struct icmp *) buffer;
293 buffer += sizeof (struct icmp);
294 buffer_len -= sizeof (struct icmp);
296 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
297 {
298 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
299 return (NULL);
300 }
302 recv_checksum = icmp_hdr->icmp_cksum;
303 icmp_hdr->icmp_cksum = 0;
304 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
305 sizeof (struct icmp) + buffer_len);
307 if (recv_checksum != calc_checksum)
308 {
309 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
310 recv_checksum, calc_checksum);
311 return (NULL);
312 }
314 ident = ntohs (icmp_hdr->icmp_id);
315 seq = ntohs (icmp_hdr->icmp_seq);
317 for (ptr = ph; ptr != NULL; ptr = ptr->next)
318 {
319 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
320 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
322 if (ptr->addrfamily != AF_INET)
323 continue;
325 if (!timerisset (ptr->timer))
326 continue;
328 if (ptr->ident != ident)
329 continue;
331 if (((ptr->sequence - 1) & 0xFFFF) != seq)
332 continue;
334 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
335 ptr->hostname, ident, seq);
337 break;
338 }
340 if (ptr == NULL)
341 {
342 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
343 ident, seq);
344 }
346 ptr->recv_ttl = ip_hdr->ip_ttl;
348 return (ptr);
349 }
351 #ifndef ICMP6_ECHO_REQUEST
352 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
353 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
354 # else
355 # define ICMP6_ECHO_REQUEST 128
356 # endif
357 #endif
359 #ifndef ICMP6_ECHO_REPLY
360 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
361 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
362 # else
363 # define ICMP6_ECHO_REPLY 129
364 # endif
365 #endif
367 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
368 {
369 struct icmp6_hdr *icmp_hdr;
371 uint16_t ident;
372 uint16_t seq;
374 pinghost_t *ptr;
376 if (buffer_len < sizeof (struct icmp6_hdr))
377 return (NULL);
379 icmp_hdr = (struct icmp6_hdr *) buffer;
380 buffer += sizeof (struct icmp);
381 buffer_len -= sizeof (struct icmp);
383 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
384 {
385 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
386 return (NULL);
387 }
389 if (icmp_hdr->icmp6_code != 0)
390 {
391 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
392 return (NULL);
393 }
395 ident = ntohs (icmp_hdr->icmp6_id);
396 seq = ntohs (icmp_hdr->icmp6_seq);
398 for (ptr = ph; ptr != NULL; ptr = ptr->next)
399 {
400 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
401 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
403 if (ptr->addrfamily != AF_INET6)
404 continue;
406 if (!timerisset (ptr->timer))
407 continue;
409 if (ptr->ident != ident)
410 continue;
412 if (((ptr->sequence - 1) & 0xFFFF) != seq)
413 continue;
415 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
416 ptr->hostname, ident, seq);
418 break;
419 }
421 if (ptr == NULL)
422 {
423 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
424 ident, seq);
425 }
427 return (ptr);
428 }
430 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
431 {
432 struct timeval diff;
433 pinghost_t *host = NULL;
434 int family;
435 int recv_ttl;
437 /*
438 * Set up the receive buffer..
439 */
440 struct msghdr msghdr;
441 struct cmsghdr *cmsg;
442 char payload_buffer[4096];
443 ssize_t payload_buffer_len;
444 char control_buffer[4096];
445 struct iovec payload_iovec;
447 memset (&payload_iovec, 0, sizeof (payload_iovec));
448 payload_iovec.iov_base = payload_buffer;
449 payload_iovec.iov_len = sizeof (payload_buffer);
451 memset (&msghdr, 0, sizeof (msghdr));
452 /* unspecified source address */
453 msghdr.msg_name = NULL;
454 msghdr.msg_namelen = 0;
455 /* output buffer vector, see readv(2) */
456 msghdr.msg_iov = &payload_iovec;
457 msghdr.msg_iovlen = 1;
458 /* output buffer for control messages */
459 msghdr.msg_control = control_buffer;
460 msghdr.msg_controllen = sizeof (control_buffer);
461 /* flags; this is an output only field.. */
462 msghdr.msg_flags = 0;
463 #ifdef MSG_XPG4_2
464 msghdr.msg_flags |= MSG_XPG4_2;
465 #endif
467 payload_buffer_len = recvmsg (fd, &msghdr, /* flags = */ 0);
468 if (payload_buffer_len < 0)
469 {
470 #if WITH_DEBUG
471 char errbuf[PING_ERRMSG_LEN];
472 dprintf ("recvfrom: %s\n",
473 sstrerror (errno, errbuf, sizeof (errbuf)));
474 #endif
475 return (-1);
476 }
477 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, fd);
479 /* Iterate over all auxiliary data in msghdr */
480 family = ph->addrfamily;
481 recv_ttl = -1;
482 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
483 cmsg != NULL;
484 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
485 {
486 if (cmsg->cmsg_level == IPPROTO_IP) /* {{{ */
487 {
488 family = AF_INET;
489 if (cmsg->cmsg_type == IP_TTL)
490 {
491 memcpy (&recv_ttl, CMSG_DATA (cmsg),
492 sizeof (recv_ttl));
493 dprintf ("TTLv4 = %i;\n", recv_ttl);
494 }
495 else
496 {
497 dprintf ("Not handling option %i.\n",
498 cmsg->cmsg_type);
499 }
500 } /* }}} */
501 else if (cmsg->cmsg_level == IPPROTO_IPV6) /* {{{ */
502 {
503 family = AF_INET6;
504 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
505 {
506 memcpy (&recv_ttl, CMSG_DATA (cmsg),
507 sizeof (recv_ttl));
508 dprintf ("TTLv6 = %i;\n", recv_ttl);
509 }
510 else
511 {
512 dprintf ("Not handling option %i.\n",
513 cmsg->cmsg_type);
514 }
515 } /* }}} */
516 else
517 {
518 dprintf ("Don't know how to handle "
519 "unknown protocol %i.\n",
520 cmsg->cmsg_level);
521 }
522 } /* }}} for (cmsg) */
524 if (family == AF_INET)
525 {
526 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
527 if (host == NULL)
528 return (-1);
529 }
530 else if (family == AF_INET6)
531 {
532 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
533 if (host == NULL)
534 return (-1);
535 }
536 else
537 {
538 dprintf ("ping_receive_one: Unknown address family %i.\n",
539 family);
540 return (-1);
541 }
543 dprintf ("rcvd: %12i.%06i\n",
544 (int) now->tv_sec,
545 (int) now->tv_usec);
546 dprintf ("sent: %12i.%06i\n",
547 (int) host->timer->tv_sec,
548 (int) host->timer->tv_usec);
550 if (ping_timeval_sub (now, host->timer, &diff) < 0)
551 {
552 timerclear (host->timer);
553 return (-1);
554 }
556 dprintf ("diff: %12i.%06i\n",
557 (int) diff.tv_sec,
558 (int) diff.tv_usec);
560 if (recv_ttl >= 0)
561 host->recv_ttl = recv_ttl;
563 host->latency = ((double) diff.tv_usec) / 1000.0;
564 host->latency += ((double) diff.tv_sec) * 1000.0;
566 timerclear (host->timer);
568 return (0);
569 }
571 static int ping_receive_all (pingobj_t *obj)
572 {
573 fd_set readfds;
574 int num_readfds;
575 int max_readfds;
577 pinghost_t *ph;
578 pinghost_t *ptr;
580 struct timeval endtime;
581 struct timeval nowtime;
582 struct timeval timeout;
583 int status;
585 int ret;
587 ph = obj->head;
588 ret = 0;
590 for (ptr = ph; ptr != NULL; ptr = ptr->next)
591 ptr->latency = -1.0;
593 if (gettimeofday (&nowtime, NULL) == -1)
594 {
595 ping_set_errno (obj, errno);
596 return (-1);
597 }
599 /* Set up timeout */
600 timeout.tv_sec = (time_t) obj->timeout;
601 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
603 dprintf ("Set timeout to %i.%06i seconds\n",
604 (int) timeout.tv_sec,
605 (int) timeout.tv_usec);
607 ping_timeval_add (&nowtime, &timeout, &endtime);
609 while (1)
610 {
611 FD_ZERO (&readfds);
612 num_readfds = 0;
613 max_readfds = -1;
615 for (ptr = ph; ptr != NULL; ptr = ptr->next)
616 {
617 if (!timerisset (ptr->timer))
618 continue;
620 FD_SET (ptr->fd, &readfds);
621 num_readfds++;
623 if (max_readfds < ptr->fd)
624 max_readfds = ptr->fd;
625 }
627 if (num_readfds == 0)
628 break;
630 if (gettimeofday (&nowtime, NULL) == -1)
631 {
632 ping_set_errno (obj, errno);
633 return (-1);
634 }
636 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
637 break;
639 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
640 (int) timeout.tv_sec,
641 (int) timeout.tv_usec);
643 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
645 if (gettimeofday (&nowtime, NULL) == -1)
646 {
647 ping_set_errno (obj, errno);
648 return (-1);
649 }
651 if ((status == -1) && (errno == EINTR))
652 {
653 dprintf ("select was interrupted by signal..\n");
654 continue;
655 }
656 else if (status < 0)
657 {
658 #if WITH_DEBUG
659 char errbuf[PING_ERRMSG_LEN];
660 dprintf ("select: %s\n",
661 sstrerror (errno, errbuf, sizeof (errbuf)));
662 #endif
663 break;
664 }
665 else if (status == 0)
666 {
667 dprintf ("select timed out\n");
668 for (ptr = ph; ptr != NULL; ptr = ptr->next)
669 if (ptr->latency < 0.0)
670 ptr->dropped++;
671 break;
672 }
674 for (ptr = ph; ptr != NULL; ptr = ptr->next)
675 {
676 if (FD_ISSET (ptr->fd, &readfds))
677 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
678 ret++;
679 }
680 } /* while (1) */
682 return (ret);
683 }
685 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
686 * Sending functions: *
687 * *
688 * ping_send_all *
689 * +-> ping_send_one_ipv4 *
690 * `-> ping_send_one_ipv6 *
691 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
692 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
693 const void *buf, size_t buflen)
694 {
695 ssize_t ret;
697 if (gettimeofday (ph->timer, NULL) == -1)
698 {
699 timerclear (ph->timer);
700 return (-1);
701 }
703 ret = sendto (ph->fd, buf, buflen, 0,
704 (struct sockaddr *) ph->addr, ph->addrlen);
706 if (ret < 0)
707 {
708 #if defined(EHOSTUNREACH)
709 if (errno == EHOSTUNREACH)
710 return (0);
711 #endif
712 #if defined(ENETUNREACH)
713 if (errno == ENETUNREACH)
714 return (0);
715 #endif
716 ping_set_errno (obj, errno);
717 }
719 return (ret);
720 }
722 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
723 {
724 struct icmp *icmp4;
725 int status;
727 char buf[4096];
728 int buflen;
730 char *data;
731 int datalen;
733 dprintf ("ph->hostname = %s\n", ph->hostname);
735 memset (buf, '\0', sizeof (buf));
736 icmp4 = (struct icmp *) buf;
737 data = (char *) (icmp4 + 1);
739 icmp4->icmp_type = ICMP_ECHO;
740 icmp4->icmp_code = 0;
741 icmp4->icmp_cksum = 0;
742 icmp4->icmp_id = htons (ph->ident);
743 icmp4->icmp_seq = htons (ph->sequence);
745 buflen = 4096 - sizeof (struct icmp);
746 strncpy (data, ph->data, buflen);
747 datalen = strlen (data);
749 buflen = datalen + sizeof (struct icmp);
751 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
753 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
755 status = ping_sendto (obj, ph, buf, buflen);
756 if (status < 0)
757 {
758 perror ("ping_sendto");
759 return (-1);
760 }
762 dprintf ("sendto: status = %i\n", status);
764 return (0);
765 }
767 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
768 {
769 struct icmp6_hdr *icmp6;
770 int status;
772 char buf[4096];
773 int buflen;
775 char *data;
776 int datalen;
778 dprintf ("ph->hostname = %s\n", ph->hostname);
780 memset (buf, '\0', sizeof (buf));
781 icmp6 = (struct icmp6_hdr *) buf;
782 data = (char *) (icmp6 + 1);
784 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
785 icmp6->icmp6_code = 0;
786 /* The checksum will be calculated by the TCP/IP stack. */
787 /* FIXME */
788 icmp6->icmp6_cksum = 0;
789 icmp6->icmp6_id = htons (ph->ident);
790 icmp6->icmp6_seq = htons (ph->sequence);
792 buflen = 4096 - sizeof (struct icmp6_hdr);
793 strncpy (data, ph->data, buflen);
794 datalen = strlen (data);
796 buflen = datalen + sizeof (struct icmp6_hdr);
798 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
800 status = ping_sendto (obj, ph, buf, buflen);
801 if (status < 0)
802 {
803 perror ("ping_sendto");
804 return (-1);
805 }
807 dprintf ("sendto: status = %i\n", status);
809 return (0);
810 }
812 static int ping_send_all (pingobj_t *obj)
813 {
814 pinghost_t *ph;
815 pinghost_t *ptr;
817 int ret;
819 ret = 0;
820 ph = obj->head;
822 for (ptr = ph; ptr != NULL; ptr = ptr->next)
823 {
824 /* start timer.. The GNU `ping6' starts the timer before
825 * sending the packet, so I will do that too */
826 if (gettimeofday (ptr->timer, NULL) == -1)
827 {
828 #if WITH_DEBUG
829 char errbuf[PING_ERRMSG_LEN];
830 dprintf ("gettimeofday: %s\n",
831 sstrerror (errno, errbuf, sizeof (errbuf)));
832 #endif
833 timerclear (ptr->timer);
834 ret--;
835 continue;
836 }
837 else
838 {
839 dprintf ("timer set for hostname = %s\n", ptr->hostname);
840 }
842 if (ptr->addrfamily == AF_INET6)
843 {
844 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
845 if (ping_send_one_ipv6 (obj, ptr) != 0)
846 {
847 timerclear (ptr->timer);
848 ret--;
849 continue;
850 }
851 }
852 else if (ptr->addrfamily == AF_INET)
853 {
854 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
855 if (ping_send_one_ipv4 (obj, ptr) != 0)
856 {
857 timerclear (ptr->timer);
858 ret--;
859 continue;
860 }
861 }
862 else /* this should not happen */
863 {
864 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
865 timerclear (ptr->timer);
866 ret--;
867 continue;
868 }
870 ptr->sequence++;
871 }
873 return (ret);
874 }
876 /*
877 * Set the TTL of a socket protocol independently.
878 */
879 static int ping_set_ttl (pinghost_t *ph, int ttl)
880 {
881 int ret = -2;
883 if (ph->addrfamily == AF_INET)
884 {
885 dprintf ("Setting TTLv4 to %i\n", ttl);
886 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
887 &ttl, sizeof (ttl));
888 }
889 else if (ph->addrfamily == AF_INET6)
890 {
891 dprintf ("Setting TTLv6 to %i\n", ttl);
892 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
893 &ttl, sizeof (ttl));
894 }
896 return (ret);
897 }
899 static int ping_get_ident (void)
900 {
901 int fd;
902 static int did_seed = 0;
904 int retval;
906 if (did_seed == 0)
907 {
908 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
909 {
910 unsigned int seed;
912 if (read (fd, &seed, sizeof (seed)) != -1)
913 {
914 did_seed = 1;
915 dprintf ("Random seed: %#x\n", seed);
916 srandom (seed);
917 }
919 close (fd);
920 }
921 #if WITH_DEBUG
922 else
923 {
924 char errbuf[PING_ERRMSG_LEN];
925 dprintf ("open (/dev/urandom): %s\n",
926 sstrerror (errno, errbuf, sizeof (errbuf)));
927 }
928 #endif
929 }
931 retval = (int) random ();
933 dprintf ("Random number: %#x\n", retval);
935 return (retval);
936 }
938 static pinghost_t *ping_alloc (void)
939 {
940 pinghost_t *ph;
941 size_t ph_size;
943 ph_size = sizeof (pinghost_t)
944 + sizeof (struct sockaddr_storage)
945 + sizeof (struct timeval);
947 ph = (pinghost_t *) malloc (ph_size);
948 if (ph == NULL)
949 return (NULL);
951 memset (ph, '\0', ph_size);
953 ph->timer = (struct timeval *) (ph + 1);
954 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
956 ph->addrlen = sizeof (struct sockaddr_storage);
957 ph->fd = -1;
958 ph->latency = -1.0;
959 ph->dropped = 0;
960 ph->ident = ping_get_ident () & 0xFFFF;
962 return (ph);
963 }
965 static void ping_free (pinghost_t *ph)
966 {
967 if (ph->fd >= 0)
968 close (ph->fd);
970 if (ph->username != NULL)
971 free (ph->username);
973 if (ph->hostname != NULL)
974 free (ph->hostname);
976 if (ph->data != NULL)
977 free (ph->data);
979 free (ph);
980 }
982 /*
983 * public methods
984 */
985 const char *ping_get_error (pingobj_t *obj)
986 {
987 return (obj->errmsg);
988 }
990 pingobj_t *ping_construct (void)
991 {
992 pingobj_t *obj;
994 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
995 return (NULL);
996 memset (obj, '\0', sizeof (pingobj_t));
998 obj->timeout = PING_DEF_TIMEOUT;
999 obj->ttl = PING_DEF_TTL;
1000 obj->addrfamily = PING_DEF_AF;
1001 obj->data = strdup (PING_DEF_DATA);
1003 return (obj);
1004 }
1006 void ping_destroy (pingobj_t *obj)
1007 {
1008 pinghost_t *current;
1009 pinghost_t *next;
1011 current = obj->head;
1012 next = NULL;
1014 while (current != NULL)
1015 {
1016 next = current->next;
1017 ping_free (current);
1018 current = next;
1019 }
1021 if (obj->data != NULL)
1022 free (obj->data);
1024 if (obj->srcaddr != NULL)
1025 free (obj->srcaddr);
1027 free (obj);
1029 return;
1030 }
1032 int ping_setopt (pingobj_t *obj, int option, void *value)
1033 {
1034 int ret = 0;
1036 switch (option)
1037 {
1038 case PING_OPT_TIMEOUT:
1039 obj->timeout = *((double *) value);
1040 if (obj->timeout < 0.0)
1041 {
1042 obj->timeout = PING_DEF_TIMEOUT;
1043 ret = -1;
1044 }
1045 break;
1047 case PING_OPT_TTL:
1048 obj->ttl = *((int *) value);
1049 if ((obj->ttl < 1) || (obj->ttl > 255))
1050 {
1051 obj->ttl = PING_DEF_TTL;
1052 ret = -1;
1053 }
1054 else
1055 {
1056 pinghost_t *ph;
1058 for (ph = obj->head; ph != NULL; ph = ph->next)
1059 ping_set_ttl (ph, obj->ttl);
1060 }
1061 break;
1063 case PING_OPT_AF:
1064 obj->addrfamily = *((int *) value);
1065 if ((obj->addrfamily != AF_UNSPEC)
1066 && (obj->addrfamily != AF_INET)
1067 && (obj->addrfamily != AF_INET6))
1068 {
1069 obj->addrfamily = PING_DEF_AF;
1070 ret = -1;
1071 }
1072 if (obj->srcaddr != NULL)
1073 {
1074 free (obj->srcaddr);
1075 obj->srcaddr = NULL;
1076 }
1077 break;
1079 case PING_OPT_DATA:
1080 if (obj->data != NULL)
1081 {
1082 free (obj->data);
1083 obj->data = NULL;
1084 }
1085 obj->data = strdup ((const char *) value);
1086 break;
1088 case PING_OPT_SOURCE:
1089 {
1090 char *hostname = (char *) value;
1091 struct addrinfo ai_hints;
1092 struct addrinfo *ai_list;
1093 int status;
1094 #if WITH_DEBUG
1095 if (obj->addrfamily != AF_UNSPEC)
1096 {
1097 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1098 }
1099 #endif
1100 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1101 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1102 #if defined(AI_ADDRCONFIG)
1103 ai_hints.ai_flags = AI_ADDRCONFIG;
1104 #endif
1105 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1106 if (status != 0)
1107 {
1108 #if defined(EAI_SYSTEM)
1109 char errbuf[PING_ERRMSG_LEN];
1110 #endif
1111 ping_set_error (obj, "getaddrinfo",
1112 #if defined(EAI_SYSTEM)
1113 (status == EAI_SYSTEM)
1114 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1115 #endif
1116 gai_strerror (status));
1117 ret = -1;
1118 break;
1119 }
1120 #if WITH_DEBUG
1121 if (ai_list->ai_next != NULL)
1122 {
1123 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1124 }
1125 #endif
1126 if (obj->srcaddr == NULL)
1127 {
1128 obj->srcaddrlen = 0;
1129 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1130 if (obj->srcaddr == NULL)
1131 {
1132 ping_set_errno (obj, errno);
1133 ret = -1;
1134 freeaddrinfo (ai_list);
1135 break;
1136 }
1137 }
1138 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1139 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1140 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1141 ai_list->ai_addrlen);
1142 obj->srcaddrlen = ai_list->ai_addrlen;
1143 obj->addrfamily = ai_list->ai_family;
1145 freeaddrinfo (ai_list);
1146 } /* case PING_OPT_SOURCE */
1147 break;
1149 default:
1150 ret = -2;
1151 } /* switch (option) */
1153 return (ret);
1154 } /* int ping_setopt */
1157 int ping_send (pingobj_t *obj)
1158 {
1159 int ret;
1161 if (ping_send_all (obj) < 0)
1162 return (-1);
1164 if ((ret = ping_receive_all (obj)) < 0)
1165 return (-2);
1167 return (ret);
1168 }
1170 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1171 {
1172 while (ph != NULL)
1173 {
1174 if (strcasecmp (ph->username, host) == 0)
1175 break;
1177 ph = ph->next;
1178 }
1180 return (ph);
1181 }
1183 int ping_host_add (pingobj_t *obj, const char *host)
1184 {
1185 pinghost_t *ph;
1187 struct addrinfo ai_hints;
1188 struct addrinfo *ai_list, *ai_ptr;
1189 int ai_return;
1191 dprintf ("host = %s\n", host);
1193 if (ping_host_search (obj->head, host) != NULL)
1194 return (0);
1196 memset (&ai_hints, '\0', sizeof (ai_hints));
1197 ai_hints.ai_flags = 0;
1198 #ifdef AI_ADDRCONFIG
1199 ai_hints.ai_flags |= AI_ADDRCONFIG;
1200 #endif
1201 #ifdef AI_CANONNAME
1202 ai_hints.ai_flags |= AI_CANONNAME;
1203 #endif
1204 ai_hints.ai_family = obj->addrfamily;
1205 ai_hints.ai_socktype = SOCK_RAW;
1207 if ((ph = ping_alloc ()) == NULL)
1208 {
1209 dprintf ("Out of memory!\n");
1210 return (-1);
1211 }
1213 if ((ph->username = strdup (host)) == NULL)
1214 {
1215 dprintf ("Out of memory!\n");
1216 ping_set_errno (obj, errno);
1217 ping_free (ph);
1218 return (-1);
1219 }
1221 if ((ph->hostname = strdup (host)) == NULL)
1222 {
1223 dprintf ("Out of memory!\n");
1224 ping_set_errno (obj, errno);
1225 ping_free (ph);
1226 return (-1);
1227 }
1229 /* obj->data is not garuanteed to be != NULL */
1230 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1231 {
1232 dprintf ("Out of memory!\n");
1233 ping_set_errno (obj, errno);
1234 ping_free (ph);
1235 return (-1);
1236 }
1238 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1239 {
1240 #if defined(EAI_SYSTEM)
1241 char errbuf[PING_ERRMSG_LEN];
1242 #endif
1243 dprintf ("getaddrinfo failed\n");
1244 ping_set_error (obj, "getaddrinfo",
1245 #if defined(EAI_SYSTEM)
1246 (ai_return == EAI_SYSTEM)
1247 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1248 #endif
1249 gai_strerror (ai_return));
1250 ping_free (ph);
1251 return (-1);
1252 }
1254 if (ai_list == NULL)
1255 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1257 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1258 {
1259 ph->fd = -1;
1261 if (ai_ptr->ai_family == AF_INET)
1262 {
1263 ai_ptr->ai_socktype = SOCK_RAW;
1264 ai_ptr->ai_protocol = IPPROTO_ICMP;
1265 }
1266 else if (ai_ptr->ai_family == AF_INET6)
1267 {
1268 ai_ptr->ai_socktype = SOCK_RAW;
1269 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1270 }
1271 else
1272 {
1273 char errmsg[PING_ERRMSG_LEN];
1275 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1276 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1278 dprintf (errmsg);
1279 ping_set_error (obj, "getaddrinfo", errmsg);
1280 continue;
1281 }
1283 /* TODO: Move this to a static function `ping_open_socket' and
1284 * call it whenever the socket dies. */
1285 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1286 if (ph->fd == -1)
1287 {
1288 #if WITH_DEBUG
1289 char errbuf[PING_ERRMSG_LEN];
1290 dprintf ("socket: %s\n",
1291 sstrerror (errno, errbuf, sizeof (errbuf)));
1292 #endif
1293 ping_set_errno (obj, errno);
1294 continue;
1295 }
1297 if (obj->srcaddr != NULL)
1298 {
1299 assert (obj->srcaddrlen > 0);
1300 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1302 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1303 {
1304 #if WITH_DEBUG
1305 char errbuf[PING_ERRMSG_LEN];
1306 dprintf ("bind: %s\n",
1307 sstrerror (errno, errbuf, sizeof (errbuf)));
1308 #endif
1309 ping_set_errno (obj, errno);
1310 close (ph->fd);
1311 ph->fd = -1;
1312 continue;
1313 }
1314 }
1316 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1317 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1318 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1319 ph->addrlen = ai_ptr->ai_addrlen;
1320 ph->addrfamily = ai_ptr->ai_family;
1322 #ifdef AI_CANONNAME
1323 if ((ai_ptr->ai_canonname != NULL)
1324 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1325 {
1326 char *old_hostname;
1328 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1329 ph->hostname, ai_ptr->ai_canonname);
1331 old_hostname = ph->hostname;
1332 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1333 {
1334 /* strdup failed, falling back to old hostname */
1335 ph->hostname = old_hostname;
1336 }
1337 else if (old_hostname != NULL)
1338 {
1339 free (old_hostname);
1340 }
1341 }
1342 #endif /* AI_CANONNAME */
1344 if (ph->addrfamily == AF_INET)
1345 {
1346 int opt = 1;
1348 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1349 &opt, sizeof (opt));
1350 }
1351 else if (ph->addrfamily == AF_INET6)
1352 {
1353 int opt = 1;
1355 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1356 &opt, sizeof (opt));
1357 }
1359 break;
1360 }
1362 freeaddrinfo (ai_list);
1364 if (ph->fd < 0)
1365 {
1366 ping_free (ph);
1367 return (-1);
1368 }
1370 /*
1371 * Adding in the front is much easier, but then the iterator will
1372 * return the host that was added last as first host. That's just not
1373 * nice. -octo
1374 */
1375 if (obj->head == NULL)
1376 {
1377 obj->head = ph;
1378 }
1379 else
1380 {
1381 pinghost_t *hptr;
1383 hptr = obj->head;
1384 while (hptr->next != NULL)
1385 hptr = hptr->next;
1387 assert ((hptr != NULL) && (hptr->next == NULL));
1388 hptr->next = ph;
1389 }
1391 ping_set_ttl (ph, obj->ttl);
1393 return (0);
1394 } /* int ping_host_add */
1396 int ping_host_remove (pingobj_t *obj, const char *host)
1397 {
1398 pinghost_t *pre, *cur;
1400 pre = NULL;
1401 cur = obj->head;
1403 while (cur != NULL)
1404 {
1405 if (strcasecmp (host, cur->username) == 0)
1406 break;
1408 pre = cur;
1409 cur = cur->next;
1410 }
1412 if (cur == NULL)
1413 {
1414 ping_set_error (obj, "ping_host_remove", "Host not found");
1415 return (-1);
1416 }
1418 if (pre == NULL)
1419 obj->head = cur->next;
1420 else
1421 pre->next = cur->next;
1423 ping_free (cur);
1425 return (0);
1426 }
1428 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1429 {
1430 return ((pingobj_iter_t *) obj->head);
1431 }
1433 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1434 {
1435 return ((pingobj_iter_t *) iter->next);
1436 }
1438 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1439 void *buffer, size_t *buffer_len)
1440 {
1441 int ret = EINVAL;
1443 size_t orig_buffer_len = *buffer_len;
1445 switch (info)
1446 {
1447 case PING_INFO_USERNAME:
1448 ret = ENOMEM;
1449 *buffer_len = strlen (iter->username) + 1;
1450 if (orig_buffer_len <= *buffer_len)
1451 break;
1452 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1453 * will copy `*buffer_len' and pad the rest of
1454 * `buffer' with null-bytes */
1455 strncpy (buffer, iter->username, orig_buffer_len);
1456 ret = 0;
1457 break;
1459 case PING_INFO_HOSTNAME:
1460 ret = ENOMEM;
1461 *buffer_len = strlen (iter->hostname) + 1;
1462 if (orig_buffer_len < *buffer_len)
1463 break;
1464 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1465 * will copy `*buffer_len' and pad the rest of
1466 * `buffer' with null-bytes */
1467 strncpy (buffer, iter->hostname, orig_buffer_len);
1468 ret = 0;
1469 break;
1471 case PING_INFO_ADDRESS:
1472 ret = getnameinfo ((struct sockaddr *) iter->addr,
1473 iter->addrlen,
1474 (char *) buffer,
1475 *buffer_len,
1476 NULL, 0,
1477 NI_NUMERICHOST);
1478 if (ret != 0)
1479 {
1480 if ((ret == EAI_MEMORY)
1481 #ifdef EAI_OVERFLOW
1482 || (ret == EAI_OVERFLOW)
1483 #endif
1484 )
1485 ret = ENOMEM;
1486 #if defined(EAI_SYSTEM)
1487 else if (ret == EAI_SYSTEM)
1488 ret = errno;
1489 #endif
1490 else
1491 ret = EINVAL;
1492 }
1493 break;
1495 case PING_INFO_FAMILY:
1496 ret = ENOMEM;
1497 *buffer_len = sizeof (int);
1498 if (orig_buffer_len < sizeof (int))
1499 break;
1500 *((int *) buffer) = iter->addrfamily;
1501 ret = 0;
1502 break;
1504 case PING_INFO_LATENCY:
1505 ret = ENOMEM;
1506 *buffer_len = sizeof (double);
1507 if (orig_buffer_len < sizeof (double))
1508 break;
1509 *((double *) buffer) = iter->latency;
1510 ret = 0;
1511 break;
1513 case PING_INFO_DROPPED:
1514 ret = ENOMEM;
1515 *buffer_len = sizeof (uint32_t);
1516 if (orig_buffer_len < sizeof (uint32_t))
1517 break;
1518 *((uint32_t *) buffer) = iter->dropped;
1519 ret = 0;
1520 break;
1522 case PING_INFO_SEQUENCE:
1523 ret = ENOMEM;
1524 *buffer_len = sizeof (unsigned int);
1525 if (orig_buffer_len < sizeof (unsigned int))
1526 break;
1527 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1528 ret = 0;
1529 break;
1531 case PING_INFO_IDENT:
1532 ret = ENOMEM;
1533 *buffer_len = sizeof (uint16_t);
1534 if (orig_buffer_len < sizeof (uint16_t))
1535 break;
1536 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1537 ret = 0;
1538 break;
1540 case PING_INFO_DATA:
1541 ret = ENOMEM;
1542 *buffer_len = strlen (iter->data);
1543 if (orig_buffer_len < *buffer_len)
1544 break;
1545 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1546 ret = 0;
1547 break;
1549 case PING_INFO_RECV_TTL:
1550 ret = ENOMEM;
1551 *buffer_len = sizeof (int);
1552 if (orig_buffer_len < sizeof (int))
1553 break;
1554 *((int *) buffer) = iter->recv_ttl;
1555 ret = 0;
1556 break;
1557 }
1559 return (ret);
1560 } /* ping_iterator_get_info */
1562 void *ping_iterator_get_context (pingobj_iter_t *iter)
1563 {
1564 return (iter->context);
1565 }
1567 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1568 {
1569 iter->context = context;
1570 }