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 static 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 if (ptr != NULL)
347 ptr->recv_ttl = ip_hdr->ip_ttl;
349 return (ptr);
350 }
352 #ifndef ICMP6_ECHO_REQUEST
353 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
354 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
355 # else
356 # define ICMP6_ECHO_REQUEST 128
357 # endif
358 #endif
360 #ifndef ICMP6_ECHO_REPLY
361 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
362 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
363 # else
364 # define ICMP6_ECHO_REPLY 129
365 # endif
366 #endif
368 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
369 {
370 struct icmp6_hdr *icmp_hdr;
372 uint16_t ident;
373 uint16_t seq;
375 pinghost_t *ptr;
377 if (buffer_len < sizeof (struct icmp6_hdr))
378 return (NULL);
380 icmp_hdr = (struct icmp6_hdr *) buffer;
381 buffer += sizeof (struct icmp);
382 buffer_len -= sizeof (struct icmp);
384 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
385 {
386 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
387 return (NULL);
388 }
390 if (icmp_hdr->icmp6_code != 0)
391 {
392 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
393 return (NULL);
394 }
396 ident = ntohs (icmp_hdr->icmp6_id);
397 seq = ntohs (icmp_hdr->icmp6_seq);
399 for (ptr = ph; ptr != NULL; ptr = ptr->next)
400 {
401 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
402 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
404 if (ptr->addrfamily != AF_INET6)
405 continue;
407 if (!timerisset (ptr->timer))
408 continue;
410 if (ptr->ident != ident)
411 continue;
413 if (((ptr->sequence - 1) & 0xFFFF) != seq)
414 continue;
416 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
417 ptr->hostname, ident, seq);
419 break;
420 }
422 if (ptr == NULL)
423 {
424 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
425 ident, seq);
426 }
428 return (ptr);
429 }
431 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
432 {
433 struct timeval diff;
434 pinghost_t *host = NULL;
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 recv_ttl = -1;
481 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
482 cmsg != NULL;
483 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
484 {
485 if (ph->addrfamily == AF_INET) /* {{{ */
486 {
487 if (cmsg->cmsg_level != IPPROTO_IP)
488 continue;
490 if (cmsg->cmsg_type == IP_TTL)
491 {
492 memcpy (&recv_ttl, CMSG_DATA (cmsg),
493 sizeof (recv_ttl));
494 dprintf ("TTLv4 = %i;\n", recv_ttl);
495 }
496 else
497 {
498 dprintf ("Not handling option %i.\n",
499 cmsg->cmsg_type);
500 }
501 } /* }}} */
502 else if (ph->addrfamily == AF_INET6) /* {{{ */
503 {
504 if (cmsg->cmsg_level != IPPROTO_IPV6)
505 continue;
507 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
508 {
509 memcpy (&recv_ttl, CMSG_DATA (cmsg),
510 sizeof (recv_ttl));
511 dprintf ("TTLv6 = %i;\n", recv_ttl);
512 }
513 else
514 {
515 dprintf ("Not handling option %i.\n",
516 cmsg->cmsg_type);
517 }
518 } /* }}} */
519 else
520 {
521 dprintf ("Don't know how to handle "
522 "unknown protocol %i.\n",
523 cmsg->cmsg_level);
524 }
525 } /* }}} for (cmsg) */
527 if (ph->addrfamily == AF_INET)
528 {
529 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
530 if (host == NULL)
531 return (-1);
532 }
533 else if (ph->addrfamily == AF_INET6)
534 {
535 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
536 if (host == NULL)
537 return (-1);
538 }
539 else
540 {
541 dprintf ("ping_receive_one: Unknown address family %i.\n",
542 ph->addrfamily);
543 return (-1);
544 }
546 dprintf ("rcvd: %12i.%06i\n",
547 (int) now->tv_sec,
548 (int) now->tv_usec);
549 dprintf ("sent: %12i.%06i\n",
550 (int) host->timer->tv_sec,
551 (int) host->timer->tv_usec);
553 if (ping_timeval_sub (now, host->timer, &diff) < 0)
554 {
555 timerclear (host->timer);
556 return (-1);
557 }
559 dprintf ("diff: %12i.%06i\n",
560 (int) diff.tv_sec,
561 (int) diff.tv_usec);
563 if (recv_ttl >= 0)
564 host->recv_ttl = recv_ttl;
566 host->latency = ((double) diff.tv_usec) / 1000.0;
567 host->latency += ((double) diff.tv_sec) * 1000.0;
569 timerclear (host->timer);
571 return (0);
572 }
574 static int ping_receive_all (pingobj_t *obj)
575 {
576 fd_set readfds;
577 int num_readfds;
578 int max_readfds;
580 pinghost_t *ph;
581 pinghost_t *ptr;
583 struct timeval endtime;
584 struct timeval nowtime;
585 struct timeval timeout;
586 int status;
588 int ret;
590 ph = obj->head;
591 ret = 0;
593 for (ptr = ph; ptr != NULL; ptr = ptr->next)
594 {
595 ptr->latency = -1.0;
596 ptr->recv_ttl = -1;
597 }
599 if (gettimeofday (&nowtime, NULL) == -1)
600 {
601 ping_set_errno (obj, errno);
602 return (-1);
603 }
605 /* Set up timeout */
606 timeout.tv_sec = (time_t) obj->timeout;
607 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
609 dprintf ("Set timeout to %i.%06i seconds\n",
610 (int) timeout.tv_sec,
611 (int) timeout.tv_usec);
613 ping_timeval_add (&nowtime, &timeout, &endtime);
615 while (1)
616 {
617 FD_ZERO (&readfds);
618 num_readfds = 0;
619 max_readfds = -1;
621 for (ptr = ph; ptr != NULL; ptr = ptr->next)
622 {
623 if (!timerisset (ptr->timer))
624 continue;
626 FD_SET (ptr->fd, &readfds);
627 num_readfds++;
629 if (max_readfds < ptr->fd)
630 max_readfds = ptr->fd;
631 }
633 if (num_readfds == 0)
634 break;
636 if (gettimeofday (&nowtime, NULL) == -1)
637 {
638 ping_set_errno (obj, errno);
639 return (-1);
640 }
642 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
643 break;
645 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
646 (int) timeout.tv_sec,
647 (int) timeout.tv_usec);
649 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
651 if (gettimeofday (&nowtime, NULL) == -1)
652 {
653 ping_set_errno (obj, errno);
654 return (-1);
655 }
657 if ((status == -1) && (errno == EINTR))
658 {
659 dprintf ("select was interrupted by signal..\n");
660 continue;
661 }
662 else if (status < 0)
663 {
664 #if WITH_DEBUG
665 char errbuf[PING_ERRMSG_LEN];
666 dprintf ("select: %s\n",
667 sstrerror (errno, errbuf, sizeof (errbuf)));
668 #endif
669 break;
670 }
671 else if (status == 0)
672 {
673 dprintf ("select timed out\n");
674 for (ptr = ph; ptr != NULL; ptr = ptr->next)
675 if (ptr->latency < 0.0)
676 ptr->dropped++;
677 break;
678 }
680 for (ptr = ph; ptr != NULL; ptr = ptr->next)
681 {
682 if (FD_ISSET (ptr->fd, &readfds))
683 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
684 ret++;
685 }
686 } /* while (1) */
688 return (ret);
689 }
691 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
692 * Sending functions: *
693 * *
694 * ping_send_all *
695 * +-> ping_send_one_ipv4 *
696 * `-> ping_send_one_ipv6 *
697 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
698 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
699 const void *buf, size_t buflen)
700 {
701 ssize_t ret;
703 if (gettimeofday (ph->timer, NULL) == -1)
704 {
705 timerclear (ph->timer);
706 return (-1);
707 }
709 ret = sendto (ph->fd, buf, buflen, 0,
710 (struct sockaddr *) ph->addr, ph->addrlen);
712 if (ret < 0)
713 {
714 #if defined(EHOSTUNREACH)
715 if (errno == EHOSTUNREACH)
716 return (0);
717 #endif
718 #if defined(ENETUNREACH)
719 if (errno == ENETUNREACH)
720 return (0);
721 #endif
722 ping_set_errno (obj, errno);
723 }
725 return (ret);
726 }
728 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
729 {
730 struct icmp *icmp4;
731 int status;
733 char buf[4096];
734 int buflen;
736 char *data;
737 int datalen;
739 dprintf ("ph->hostname = %s\n", ph->hostname);
741 memset (buf, '\0', sizeof (buf));
742 icmp4 = (struct icmp *) buf;
743 data = (char *) (icmp4 + 1);
745 icmp4->icmp_type = ICMP_ECHO;
746 icmp4->icmp_code = 0;
747 icmp4->icmp_cksum = 0;
748 icmp4->icmp_id = htons (ph->ident);
749 icmp4->icmp_seq = htons (ph->sequence);
751 buflen = 4096 - sizeof (struct icmp);
752 strncpy (data, ph->data, buflen);
753 datalen = strlen (data);
755 buflen = datalen + sizeof (struct icmp);
757 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
759 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
761 status = ping_sendto (obj, ph, buf, buflen);
762 if (status < 0)
763 {
764 perror ("ping_sendto");
765 return (-1);
766 }
768 dprintf ("sendto: status = %i\n", status);
770 return (0);
771 }
773 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
774 {
775 struct icmp6_hdr *icmp6;
776 int status;
778 char buf[4096];
779 int buflen;
781 char *data;
782 int datalen;
784 dprintf ("ph->hostname = %s\n", ph->hostname);
786 memset (buf, '\0', sizeof (buf));
787 icmp6 = (struct icmp6_hdr *) buf;
788 data = (char *) (icmp6 + 1);
790 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
791 icmp6->icmp6_code = 0;
792 /* The checksum will be calculated by the TCP/IP stack. */
793 /* FIXME */
794 icmp6->icmp6_cksum = 0;
795 icmp6->icmp6_id = htons (ph->ident);
796 icmp6->icmp6_seq = htons (ph->sequence);
798 buflen = 4096 - sizeof (struct icmp6_hdr);
799 strncpy (data, ph->data, buflen);
800 datalen = strlen (data);
802 buflen = datalen + sizeof (struct icmp6_hdr);
804 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
806 status = ping_sendto (obj, ph, buf, buflen);
807 if (status < 0)
808 {
809 perror ("ping_sendto");
810 return (-1);
811 }
813 dprintf ("sendto: status = %i\n", status);
815 return (0);
816 }
818 static int ping_send_all (pingobj_t *obj)
819 {
820 pinghost_t *ph;
821 pinghost_t *ptr;
823 int ret;
825 ret = 0;
826 ph = obj->head;
828 for (ptr = ph; ptr != NULL; ptr = ptr->next)
829 {
830 /* start timer.. The GNU `ping6' starts the timer before
831 * sending the packet, so I will do that too */
832 if (gettimeofday (ptr->timer, NULL) == -1)
833 {
834 #if WITH_DEBUG
835 char errbuf[PING_ERRMSG_LEN];
836 dprintf ("gettimeofday: %s\n",
837 sstrerror (errno, errbuf, sizeof (errbuf)));
838 #endif
839 timerclear (ptr->timer);
840 ret--;
841 continue;
842 }
843 else
844 {
845 dprintf ("timer set for hostname = %s\n", ptr->hostname);
846 }
848 if (ptr->addrfamily == AF_INET6)
849 {
850 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
851 if (ping_send_one_ipv6 (obj, ptr) != 0)
852 {
853 timerclear (ptr->timer);
854 ret--;
855 continue;
856 }
857 }
858 else if (ptr->addrfamily == AF_INET)
859 {
860 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
861 if (ping_send_one_ipv4 (obj, ptr) != 0)
862 {
863 timerclear (ptr->timer);
864 ret--;
865 continue;
866 }
867 }
868 else /* this should not happen */
869 {
870 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
871 timerclear (ptr->timer);
872 ret--;
873 continue;
874 }
876 ptr->sequence++;
877 }
879 return (ret);
880 }
882 /*
883 * Set the TTL of a socket protocol independently.
884 */
885 static int ping_set_ttl (pinghost_t *ph, int ttl)
886 {
887 int ret = -2;
889 if (ph->addrfamily == AF_INET)
890 {
891 dprintf ("Setting TTLv4 to %i\n", ttl);
892 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
893 &ttl, sizeof (ttl));
894 }
895 else if (ph->addrfamily == AF_INET6)
896 {
897 dprintf ("Setting TTLv6 to %i\n", ttl);
898 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
899 &ttl, sizeof (ttl));
900 }
902 return (ret);
903 }
905 static int ping_get_ident (void)
906 {
907 int fd;
908 static int did_seed = 0;
910 int retval;
912 if (did_seed == 0)
913 {
914 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
915 {
916 unsigned int seed;
918 if (read (fd, &seed, sizeof (seed)) != -1)
919 {
920 did_seed = 1;
921 dprintf ("Random seed: %#x\n", seed);
922 srandom (seed);
923 }
925 close (fd);
926 }
927 #if WITH_DEBUG
928 else
929 {
930 char errbuf[PING_ERRMSG_LEN];
931 dprintf ("open (/dev/urandom): %s\n",
932 sstrerror (errno, errbuf, sizeof (errbuf)));
933 }
934 #endif
935 }
937 retval = (int) random ();
939 dprintf ("Random number: %#x\n", retval);
941 return (retval);
942 }
944 static pinghost_t *ping_alloc (void)
945 {
946 pinghost_t *ph;
947 size_t ph_size;
949 ph_size = sizeof (pinghost_t)
950 + sizeof (struct sockaddr_storage)
951 + sizeof (struct timeval);
953 ph = (pinghost_t *) malloc (ph_size);
954 if (ph == NULL)
955 return (NULL);
957 memset (ph, '\0', ph_size);
959 ph->timer = (struct timeval *) (ph + 1);
960 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
962 ph->addrlen = sizeof (struct sockaddr_storage);
963 ph->fd = -1;
964 ph->latency = -1.0;
965 ph->dropped = 0;
966 ph->ident = ping_get_ident () & 0xFFFF;
968 return (ph);
969 }
971 static void ping_free (pinghost_t *ph)
972 {
973 if (ph->fd >= 0)
974 close (ph->fd);
976 if (ph->username != NULL)
977 free (ph->username);
979 if (ph->hostname != NULL)
980 free (ph->hostname);
982 if (ph->data != NULL)
983 free (ph->data);
985 free (ph);
986 }
988 /*
989 * public methods
990 */
991 const char *ping_get_error (pingobj_t *obj)
992 {
993 return (obj->errmsg);
994 }
996 pingobj_t *ping_construct (void)
997 {
998 pingobj_t *obj;
1000 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1001 return (NULL);
1002 memset (obj, '\0', sizeof (pingobj_t));
1004 obj->timeout = PING_DEF_TIMEOUT;
1005 obj->ttl = PING_DEF_TTL;
1006 obj->addrfamily = PING_DEF_AF;
1007 obj->data = strdup (PING_DEF_DATA);
1009 return (obj);
1010 }
1012 void ping_destroy (pingobj_t *obj)
1013 {
1014 pinghost_t *current;
1015 pinghost_t *next;
1017 current = obj->head;
1018 next = NULL;
1020 while (current != NULL)
1021 {
1022 next = current->next;
1023 ping_free (current);
1024 current = next;
1025 }
1027 if (obj->data != NULL)
1028 free (obj->data);
1030 if (obj->srcaddr != NULL)
1031 free (obj->srcaddr);
1033 free (obj);
1035 return;
1036 }
1038 int ping_setopt (pingobj_t *obj, int option, void *value)
1039 {
1040 int ret = 0;
1042 switch (option)
1043 {
1044 case PING_OPT_TIMEOUT:
1045 obj->timeout = *((double *) value);
1046 if (obj->timeout < 0.0)
1047 {
1048 obj->timeout = PING_DEF_TIMEOUT;
1049 ret = -1;
1050 }
1051 break;
1053 case PING_OPT_TTL:
1054 obj->ttl = *((int *) value);
1055 if ((obj->ttl < 1) || (obj->ttl > 255))
1056 {
1057 obj->ttl = PING_DEF_TTL;
1058 ret = -1;
1059 }
1060 else
1061 {
1062 pinghost_t *ph;
1064 for (ph = obj->head; ph != NULL; ph = ph->next)
1065 ping_set_ttl (ph, obj->ttl);
1066 }
1067 break;
1069 case PING_OPT_AF:
1070 obj->addrfamily = *((int *) value);
1071 if ((obj->addrfamily != AF_UNSPEC)
1072 && (obj->addrfamily != AF_INET)
1073 && (obj->addrfamily != AF_INET6))
1074 {
1075 obj->addrfamily = PING_DEF_AF;
1076 ret = -1;
1077 }
1078 if (obj->srcaddr != NULL)
1079 {
1080 free (obj->srcaddr);
1081 obj->srcaddr = NULL;
1082 }
1083 break;
1085 case PING_OPT_DATA:
1086 if (obj->data != NULL)
1087 {
1088 free (obj->data);
1089 obj->data = NULL;
1090 }
1091 obj->data = strdup ((const char *) value);
1092 break;
1094 case PING_OPT_SOURCE:
1095 {
1096 char *hostname = (char *) value;
1097 struct addrinfo ai_hints;
1098 struct addrinfo *ai_list;
1099 int status;
1100 #if WITH_DEBUG
1101 if (obj->addrfamily != AF_UNSPEC)
1102 {
1103 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1104 }
1105 #endif
1106 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1107 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1108 #if defined(AI_ADDRCONFIG)
1109 ai_hints.ai_flags = AI_ADDRCONFIG;
1110 #endif
1111 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1112 if (status != 0)
1113 {
1114 #if defined(EAI_SYSTEM)
1115 char errbuf[PING_ERRMSG_LEN];
1116 #endif
1117 ping_set_error (obj, "getaddrinfo",
1118 #if defined(EAI_SYSTEM)
1119 (status == EAI_SYSTEM)
1120 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1121 #endif
1122 gai_strerror (status));
1123 ret = -1;
1124 break;
1125 }
1126 #if WITH_DEBUG
1127 if (ai_list->ai_next != NULL)
1128 {
1129 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1130 }
1131 #endif
1132 if (obj->srcaddr == NULL)
1133 {
1134 obj->srcaddrlen = 0;
1135 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1136 if (obj->srcaddr == NULL)
1137 {
1138 ping_set_errno (obj, errno);
1139 ret = -1;
1140 freeaddrinfo (ai_list);
1141 break;
1142 }
1143 }
1144 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1145 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1146 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1147 ai_list->ai_addrlen);
1148 obj->srcaddrlen = ai_list->ai_addrlen;
1149 obj->addrfamily = ai_list->ai_family;
1151 freeaddrinfo (ai_list);
1152 } /* case PING_OPT_SOURCE */
1153 break;
1155 default:
1156 ret = -2;
1157 } /* switch (option) */
1159 return (ret);
1160 } /* int ping_setopt */
1163 int ping_send (pingobj_t *obj)
1164 {
1165 int ret;
1167 if (ping_send_all (obj) < 0)
1168 return (-1);
1170 if ((ret = ping_receive_all (obj)) < 0)
1171 return (-2);
1173 return (ret);
1174 }
1176 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1177 {
1178 while (ph != NULL)
1179 {
1180 if (strcasecmp (ph->username, host) == 0)
1181 break;
1183 ph = ph->next;
1184 }
1186 return (ph);
1187 }
1189 int ping_host_add (pingobj_t *obj, const char *host)
1190 {
1191 pinghost_t *ph;
1193 struct addrinfo ai_hints;
1194 struct addrinfo *ai_list, *ai_ptr;
1195 int ai_return;
1197 dprintf ("host = %s\n", host);
1199 if (ping_host_search (obj->head, host) != NULL)
1200 return (0);
1202 memset (&ai_hints, '\0', sizeof (ai_hints));
1203 ai_hints.ai_flags = 0;
1204 #ifdef AI_ADDRCONFIG
1205 ai_hints.ai_flags |= AI_ADDRCONFIG;
1206 #endif
1207 #ifdef AI_CANONNAME
1208 ai_hints.ai_flags |= AI_CANONNAME;
1209 #endif
1210 ai_hints.ai_family = obj->addrfamily;
1211 ai_hints.ai_socktype = SOCK_RAW;
1213 if ((ph = ping_alloc ()) == NULL)
1214 {
1215 dprintf ("Out of memory!\n");
1216 return (-1);
1217 }
1219 if ((ph->username = strdup (host)) == NULL)
1220 {
1221 dprintf ("Out of memory!\n");
1222 ping_set_errno (obj, errno);
1223 ping_free (ph);
1224 return (-1);
1225 }
1227 if ((ph->hostname = strdup (host)) == NULL)
1228 {
1229 dprintf ("Out of memory!\n");
1230 ping_set_errno (obj, errno);
1231 ping_free (ph);
1232 return (-1);
1233 }
1235 /* obj->data is not garuanteed to be != NULL */
1236 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1237 {
1238 dprintf ("Out of memory!\n");
1239 ping_set_errno (obj, errno);
1240 ping_free (ph);
1241 return (-1);
1242 }
1244 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1245 {
1246 #if defined(EAI_SYSTEM)
1247 char errbuf[PING_ERRMSG_LEN];
1248 #endif
1249 dprintf ("getaddrinfo failed\n");
1250 ping_set_error (obj, "getaddrinfo",
1251 #if defined(EAI_SYSTEM)
1252 (ai_return == EAI_SYSTEM)
1253 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1254 #endif
1255 gai_strerror (ai_return));
1256 ping_free (ph);
1257 return (-1);
1258 }
1260 if (ai_list == NULL)
1261 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1263 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1264 {
1265 ph->fd = -1;
1267 if (ai_ptr->ai_family == AF_INET)
1268 {
1269 ai_ptr->ai_socktype = SOCK_RAW;
1270 ai_ptr->ai_protocol = IPPROTO_ICMP;
1271 }
1272 else if (ai_ptr->ai_family == AF_INET6)
1273 {
1274 ai_ptr->ai_socktype = SOCK_RAW;
1275 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1276 }
1277 else
1278 {
1279 char errmsg[PING_ERRMSG_LEN];
1281 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1282 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1284 dprintf (errmsg);
1285 ping_set_error (obj, "getaddrinfo", errmsg);
1286 continue;
1287 }
1289 /* TODO: Move this to a static function `ping_open_socket' and
1290 * call it whenever the socket dies. */
1291 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1292 if (ph->fd == -1)
1293 {
1294 #if WITH_DEBUG
1295 char errbuf[PING_ERRMSG_LEN];
1296 dprintf ("socket: %s\n",
1297 sstrerror (errno, errbuf, sizeof (errbuf)));
1298 #endif
1299 ping_set_errno (obj, errno);
1300 continue;
1301 }
1303 if (obj->srcaddr != NULL)
1304 {
1305 assert (obj->srcaddrlen > 0);
1306 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1308 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1309 {
1310 #if WITH_DEBUG
1311 char errbuf[PING_ERRMSG_LEN];
1312 dprintf ("bind: %s\n",
1313 sstrerror (errno, errbuf, sizeof (errbuf)));
1314 #endif
1315 ping_set_errno (obj, errno);
1316 close (ph->fd);
1317 ph->fd = -1;
1318 continue;
1319 }
1320 }
1322 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1323 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1324 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1325 ph->addrlen = ai_ptr->ai_addrlen;
1326 ph->addrfamily = ai_ptr->ai_family;
1328 #ifdef AI_CANONNAME
1329 if ((ai_ptr->ai_canonname != NULL)
1330 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1331 {
1332 char *old_hostname;
1334 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1335 ph->hostname, ai_ptr->ai_canonname);
1337 old_hostname = ph->hostname;
1338 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1339 {
1340 /* strdup failed, falling back to old hostname */
1341 ph->hostname = old_hostname;
1342 }
1343 else if (old_hostname != NULL)
1344 {
1345 free (old_hostname);
1346 }
1347 }
1348 #endif /* AI_CANONNAME */
1350 if (ph->addrfamily == AF_INET)
1351 {
1352 int opt = 1;
1354 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1355 &opt, sizeof (opt));
1356 }
1357 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1358 else if (ph->addrfamily == AF_INET6)
1359 {
1360 int opt = 1;
1362 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1363 &opt, sizeof (opt));
1364 }
1365 #endif
1367 break;
1368 }
1370 freeaddrinfo (ai_list);
1372 if (ph->fd < 0)
1373 {
1374 ping_free (ph);
1375 return (-1);
1376 }
1378 /*
1379 * Adding in the front is much easier, but then the iterator will
1380 * return the host that was added last as first host. That's just not
1381 * nice. -octo
1382 */
1383 if (obj->head == NULL)
1384 {
1385 obj->head = ph;
1386 }
1387 else
1388 {
1389 pinghost_t *hptr;
1391 hptr = obj->head;
1392 while (hptr->next != NULL)
1393 hptr = hptr->next;
1395 assert ((hptr != NULL) && (hptr->next == NULL));
1396 hptr->next = ph;
1397 }
1399 ping_set_ttl (ph, obj->ttl);
1401 return (0);
1402 } /* int ping_host_add */
1404 int ping_host_remove (pingobj_t *obj, const char *host)
1405 {
1406 pinghost_t *pre, *cur;
1408 pre = NULL;
1409 cur = obj->head;
1411 while (cur != NULL)
1412 {
1413 if (strcasecmp (host, cur->username) == 0)
1414 break;
1416 pre = cur;
1417 cur = cur->next;
1418 }
1420 if (cur == NULL)
1421 {
1422 ping_set_error (obj, "ping_host_remove", "Host not found");
1423 return (-1);
1424 }
1426 if (pre == NULL)
1427 obj->head = cur->next;
1428 else
1429 pre->next = cur->next;
1431 ping_free (cur);
1433 return (0);
1434 }
1436 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1437 {
1438 return ((pingobj_iter_t *) obj->head);
1439 }
1441 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1442 {
1443 return ((pingobj_iter_t *) iter->next);
1444 }
1446 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1447 void *buffer, size_t *buffer_len)
1448 {
1449 int ret = EINVAL;
1451 size_t orig_buffer_len = *buffer_len;
1453 switch (info)
1454 {
1455 case PING_INFO_USERNAME:
1456 ret = ENOMEM;
1457 *buffer_len = strlen (iter->username) + 1;
1458 if (orig_buffer_len <= *buffer_len)
1459 break;
1460 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1461 * will copy `*buffer_len' and pad the rest of
1462 * `buffer' with null-bytes */
1463 strncpy (buffer, iter->username, orig_buffer_len);
1464 ret = 0;
1465 break;
1467 case PING_INFO_HOSTNAME:
1468 ret = ENOMEM;
1469 *buffer_len = strlen (iter->hostname) + 1;
1470 if (orig_buffer_len < *buffer_len)
1471 break;
1472 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1473 * will copy `*buffer_len' and pad the rest of
1474 * `buffer' with null-bytes */
1475 strncpy (buffer, iter->hostname, orig_buffer_len);
1476 ret = 0;
1477 break;
1479 case PING_INFO_ADDRESS:
1480 ret = getnameinfo ((struct sockaddr *) iter->addr,
1481 iter->addrlen,
1482 (char *) buffer,
1483 *buffer_len,
1484 NULL, 0,
1485 NI_NUMERICHOST);
1486 if (ret != 0)
1487 {
1488 if ((ret == EAI_MEMORY)
1489 #ifdef EAI_OVERFLOW
1490 || (ret == EAI_OVERFLOW)
1491 #endif
1492 )
1493 ret = ENOMEM;
1494 #if defined(EAI_SYSTEM)
1495 else if (ret == EAI_SYSTEM)
1496 ret = errno;
1497 #endif
1498 else
1499 ret = EINVAL;
1500 }
1501 break;
1503 case PING_INFO_FAMILY:
1504 ret = ENOMEM;
1505 *buffer_len = sizeof (int);
1506 if (orig_buffer_len < sizeof (int))
1507 break;
1508 *((int *) buffer) = iter->addrfamily;
1509 ret = 0;
1510 break;
1512 case PING_INFO_LATENCY:
1513 ret = ENOMEM;
1514 *buffer_len = sizeof (double);
1515 if (orig_buffer_len < sizeof (double))
1516 break;
1517 *((double *) buffer) = iter->latency;
1518 ret = 0;
1519 break;
1521 case PING_INFO_DROPPED:
1522 ret = ENOMEM;
1523 *buffer_len = sizeof (uint32_t);
1524 if (orig_buffer_len < sizeof (uint32_t))
1525 break;
1526 *((uint32_t *) buffer) = iter->dropped;
1527 ret = 0;
1528 break;
1530 case PING_INFO_SEQUENCE:
1531 ret = ENOMEM;
1532 *buffer_len = sizeof (unsigned int);
1533 if (orig_buffer_len < sizeof (unsigned int))
1534 break;
1535 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1536 ret = 0;
1537 break;
1539 case PING_INFO_IDENT:
1540 ret = ENOMEM;
1541 *buffer_len = sizeof (uint16_t);
1542 if (orig_buffer_len < sizeof (uint16_t))
1543 break;
1544 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1545 ret = 0;
1546 break;
1548 case PING_INFO_DATA:
1549 ret = ENOMEM;
1550 *buffer_len = strlen (iter->data);
1551 if (orig_buffer_len < *buffer_len)
1552 break;
1553 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1554 ret = 0;
1555 break;
1557 case PING_INFO_RECV_TTL:
1558 ret = ENOMEM;
1559 *buffer_len = sizeof (int);
1560 if (orig_buffer_len < sizeof (int))
1561 break;
1562 *((int *) buffer) = iter->recv_ttl;
1563 ret = 0;
1564 break;
1565 }
1567 return (ret);
1568 } /* ping_iterator_get_info */
1570 void *ping_iterator_get_context (pingobj_iter_t *iter)
1571 {
1572 return (iter->context);
1573 }
1575 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1576 {
1577 iter->context = context;
1578 }