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 return (ptr);
347 }
349 #ifndef ICMP6_ECHO_REQUEST
350 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
351 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
352 # else
353 # define ICMP6_ECHO_REQUEST 128
354 # endif
355 #endif
357 #ifndef ICMP6_ECHO_REPLY
358 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
359 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
360 # else
361 # define ICMP6_ECHO_REPLY 129
362 # endif
363 #endif
365 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
366 {
367 struct icmp6_hdr *icmp_hdr;
369 uint16_t ident;
370 uint16_t seq;
372 pinghost_t *ptr;
374 if (buffer_len < sizeof (struct icmp6_hdr))
375 return (NULL);
377 icmp_hdr = (struct icmp6_hdr *) buffer;
378 buffer += sizeof (struct icmp);
379 buffer_len -= sizeof (struct icmp);
381 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
382 {
383 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
384 return (NULL);
385 }
387 if (icmp_hdr->icmp6_code != 0)
388 {
389 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
390 return (NULL);
391 }
393 ident = ntohs (icmp_hdr->icmp6_id);
394 seq = ntohs (icmp_hdr->icmp6_seq);
396 for (ptr = ph; ptr != NULL; ptr = ptr->next)
397 {
398 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
399 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
401 if (ptr->addrfamily != AF_INET6)
402 continue;
404 if (!timerisset (ptr->timer))
405 continue;
407 if (ptr->ident != ident)
408 continue;
410 if (((ptr->sequence - 1) & 0xFFFF) != seq)
411 continue;
413 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
414 ptr->hostname, ident, seq);
416 break;
417 }
419 if (ptr == NULL)
420 {
421 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
422 ident, seq);
423 }
425 return (ptr);
426 }
428 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
429 {
430 struct timeval diff;
431 pinghost_t *host = NULL;
432 int family;
433 int recv_ttl;
435 /*
436 * Set up the receive buffer..
437 */
438 struct msghdr msghdr;
439 struct cmsghdr *cmsg;
440 char payload_buffer[4096];
441 ssize_t payload_buffer_len;
442 char control_buffer[4096];
443 struct iovec payload_iovec;
445 memset (&payload_iovec, 0, sizeof (payload_iovec));
446 payload_iovec.iov_base = payload_buffer;
447 payload_iovec.iov_len = sizeof (payload_buffer);
449 memset (&msghdr, 0, sizeof (msghdr));
450 /* unspecified source address */
451 msghdr.msg_name = NULL;
452 msghdr.msg_namelen = 0;
453 /* output buffer vector, see readv(2) */
454 msghdr.msg_iov = &payload_iovec;
455 msghdr.msg_iovlen = 1;
456 /* output buffer for control messages */
457 msghdr.msg_control = control_buffer;
458 msghdr.msg_controllen = sizeof (control_buffer);
459 /* flags; this is an output only field.. */
460 msghdr.msg_flags = 0;
461 #ifdef MSG_XPG4_2
462 msghdr.msg_flags |= MSG_XPG4_2;
463 #endif
465 payload_buffer_len = recvmsg (fd, &msghdr, /* flags = */ 0);
466 if (payload_buffer_len < 0)
467 {
468 #if WITH_DEBUG
469 char errbuf[PING_ERRMSG_LEN];
470 dprintf ("recvfrom: %s\n",
471 sstrerror (errno, errbuf, sizeof (errbuf)));
472 #endif
473 return (-1);
474 }
475 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, fd);
477 /* Iterate over all auxiliary data in msghdr */
478 family = -1;
479 recv_ttl = -1;
480 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
481 cmsg != NULL;
482 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
483 {
484 if (cmsg->cmsg_level == IPPROTO_IP) /* {{{ */
485 {
486 family = AF_INET;
487 if (cmsg->cmsg_type == IP_TTL)
488 {
489 memcpy (&recv_ttl, CMSG_DATA (cmsg),
490 sizeof (recv_ttl));
491 dprintf ("TTLv4 = %i;\n", recv_ttl);
492 }
493 else
494 {
495 dprintf ("Not handling option %i.\n",
496 cmsg->cmsg_type);
497 }
498 } /* }}} */
499 else if (cmsg->cmsg_level == IPPROTO_IPV6) /* {{{ */
500 {
501 family = AF_INET6;
502 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
503 {
504 memcpy (&recv_ttl, CMSG_DATA (cmsg),
505 sizeof (recv_ttl));
506 dprintf ("TTLv6 = %i;\n", recv_ttl);
507 }
508 else
509 {
510 dprintf ("Not handling option %i.\n",
511 cmsg->cmsg_type);
512 }
513 } /* }}} */
514 else
515 {
516 dprintf ("Don't know how to handle "
517 "unknown protocol %i.\n",
518 cmsg->cmsg_level);
519 }
520 } /* }}} for (cmsg) */
522 if (family == AF_INET)
523 {
524 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
525 if (host == NULL)
526 return (-1);
527 }
528 else if (family == AF_INET6)
529 {
530 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
531 if (host == NULL)
532 return (-1);
533 }
534 else
535 {
536 dprintf ("ping_receive_one: Unknown address family %i.\n",
537 family);
538 return (-1);
539 }
541 dprintf ("rcvd: %12i.%06i\n",
542 (int) now->tv_sec,
543 (int) now->tv_usec);
544 dprintf ("sent: %12i.%06i\n",
545 (int) host->timer->tv_sec,
546 (int) host->timer->tv_usec);
548 if (ping_timeval_sub (now, host->timer, &diff) < 0)
549 {
550 timerclear (host->timer);
551 return (-1);
552 }
554 dprintf ("diff: %12i.%06i\n",
555 (int) diff.tv_sec,
556 (int) diff.tv_usec);
558 host->latency = ((double) diff.tv_usec) / 1000.0;
559 host->latency += ((double) diff.tv_sec) * 1000.0;
561 host->recv_ttl = recv_ttl;
563 timerclear (host->timer);
565 return (0);
566 }
568 static int ping_receive_all (pingobj_t *obj)
569 {
570 fd_set readfds;
571 int num_readfds;
572 int max_readfds;
574 pinghost_t *ph;
575 pinghost_t *ptr;
577 struct timeval endtime;
578 struct timeval nowtime;
579 struct timeval timeout;
580 int status;
582 int ret;
584 ph = obj->head;
585 ret = 0;
587 for (ptr = ph; ptr != NULL; ptr = ptr->next)
588 ptr->latency = -1.0;
590 if (gettimeofday (&nowtime, NULL) == -1)
591 {
592 ping_set_errno (obj, errno);
593 return (-1);
594 }
596 /* Set up timeout */
597 timeout.tv_sec = (time_t) obj->timeout;
598 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
600 dprintf ("Set timeout to %i.%06i seconds\n",
601 (int) timeout.tv_sec,
602 (int) timeout.tv_usec);
604 ping_timeval_add (&nowtime, &timeout, &endtime);
606 while (1)
607 {
608 FD_ZERO (&readfds);
609 num_readfds = 0;
610 max_readfds = -1;
612 for (ptr = ph; ptr != NULL; ptr = ptr->next)
613 {
614 if (!timerisset (ptr->timer))
615 continue;
617 FD_SET (ptr->fd, &readfds);
618 num_readfds++;
620 if (max_readfds < ptr->fd)
621 max_readfds = ptr->fd;
622 }
624 if (num_readfds == 0)
625 break;
627 if (gettimeofday (&nowtime, NULL) == -1)
628 {
629 ping_set_errno (obj, errno);
630 return (-1);
631 }
633 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
634 break;
636 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
637 (int) timeout.tv_sec,
638 (int) timeout.tv_usec);
640 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
642 if (gettimeofday (&nowtime, NULL) == -1)
643 {
644 ping_set_errno (obj, errno);
645 return (-1);
646 }
648 if ((status == -1) && (errno == EINTR))
649 {
650 dprintf ("select was interrupted by signal..\n");
651 continue;
652 }
653 else if (status < 0)
654 {
655 #if WITH_DEBUG
656 char errbuf[PING_ERRMSG_LEN];
657 dprintf ("select: %s\n",
658 sstrerror (errno, errbuf, sizeof (errbuf)));
659 #endif
660 break;
661 }
662 else if (status == 0)
663 {
664 dprintf ("select timed out\n");
665 for (ptr = ph; ptr != NULL; ptr = ptr->next)
666 if (ptr->latency < 0.0)
667 ptr->dropped++;
668 break;
669 }
671 for (ptr = ph; ptr != NULL; ptr = ptr->next)
672 {
673 if (FD_ISSET (ptr->fd, &readfds))
674 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
675 ret++;
676 }
677 } /* while (1) */
679 return (ret);
680 }
682 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
683 * Sending functions: *
684 * *
685 * ping_send_all *
686 * +-> ping_send_one_ipv4 *
687 * `-> ping_send_one_ipv6 *
688 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
689 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
690 const void *buf, size_t buflen)
691 {
692 ssize_t ret;
694 if (gettimeofday (ph->timer, NULL) == -1)
695 {
696 timerclear (ph->timer);
697 return (-1);
698 }
700 ret = sendto (ph->fd, buf, buflen, 0,
701 (struct sockaddr *) ph->addr, ph->addrlen);
703 if (ret < 0)
704 {
705 #if defined(EHOSTUNREACH)
706 if (errno == EHOSTUNREACH)
707 return (0);
708 #endif
709 #if defined(ENETUNREACH)
710 if (errno == ENETUNREACH)
711 return (0);
712 #endif
713 ping_set_errno (obj, errno);
714 }
716 return (ret);
717 }
719 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
720 {
721 struct icmp *icmp4;
722 int status;
724 char buf[4096];
725 int buflen;
727 char *data;
728 int datalen;
730 dprintf ("ph->hostname = %s\n", ph->hostname);
732 memset (buf, '\0', sizeof (buf));
733 icmp4 = (struct icmp *) buf;
734 data = (char *) (icmp4 + 1);
736 icmp4->icmp_type = ICMP_ECHO;
737 icmp4->icmp_code = 0;
738 icmp4->icmp_cksum = 0;
739 icmp4->icmp_id = htons (ph->ident);
740 icmp4->icmp_seq = htons (ph->sequence);
742 buflen = 4096 - sizeof (struct icmp);
743 strncpy (data, ph->data, buflen);
744 datalen = strlen (data);
746 buflen = datalen + sizeof (struct icmp);
748 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
750 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
752 status = ping_sendto (obj, ph, buf, buflen);
753 if (status < 0)
754 {
755 perror ("ping_sendto");
756 return (-1);
757 }
759 dprintf ("sendto: status = %i\n", status);
761 return (0);
762 }
764 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
765 {
766 struct icmp6_hdr *icmp6;
767 int status;
769 char buf[4096];
770 int buflen;
772 char *data;
773 int datalen;
775 dprintf ("ph->hostname = %s\n", ph->hostname);
777 memset (buf, '\0', sizeof (buf));
778 icmp6 = (struct icmp6_hdr *) buf;
779 data = (char *) (icmp6 + 1);
781 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
782 icmp6->icmp6_code = 0;
783 /* The checksum will be calculated by the TCP/IP stack. */
784 /* FIXME */
785 icmp6->icmp6_cksum = 0;
786 icmp6->icmp6_id = htons (ph->ident);
787 icmp6->icmp6_seq = htons (ph->sequence);
789 buflen = 4096 - sizeof (struct icmp6_hdr);
790 strncpy (data, ph->data, buflen);
791 datalen = strlen (data);
793 buflen = datalen + sizeof (struct icmp6_hdr);
795 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
797 status = ping_sendto (obj, ph, buf, buflen);
798 if (status < 0)
799 {
800 perror ("ping_sendto");
801 return (-1);
802 }
804 dprintf ("sendto: status = %i\n", status);
806 return (0);
807 }
809 static int ping_send_all (pingobj_t *obj)
810 {
811 pinghost_t *ph;
812 pinghost_t *ptr;
814 int ret;
816 ret = 0;
817 ph = obj->head;
819 for (ptr = ph; ptr != NULL; ptr = ptr->next)
820 {
821 /* start timer.. The GNU `ping6' starts the timer before
822 * sending the packet, so I will do that too */
823 if (gettimeofday (ptr->timer, NULL) == -1)
824 {
825 #if WITH_DEBUG
826 char errbuf[PING_ERRMSG_LEN];
827 dprintf ("gettimeofday: %s\n",
828 sstrerror (errno, errbuf, sizeof (errbuf)));
829 #endif
830 timerclear (ptr->timer);
831 ret--;
832 continue;
833 }
834 else
835 {
836 dprintf ("timer set for hostname = %s\n", ptr->hostname);
837 }
839 if (ptr->addrfamily == AF_INET6)
840 {
841 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
842 if (ping_send_one_ipv6 (obj, ptr) != 0)
843 {
844 timerclear (ptr->timer);
845 ret--;
846 continue;
847 }
848 }
849 else if (ptr->addrfamily == AF_INET)
850 {
851 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
852 if (ping_send_one_ipv4 (obj, ptr) != 0)
853 {
854 timerclear (ptr->timer);
855 ret--;
856 continue;
857 }
858 }
859 else /* this should not happen */
860 {
861 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
862 timerclear (ptr->timer);
863 ret--;
864 continue;
865 }
867 ptr->sequence++;
868 }
870 return (ret);
871 }
873 /*
874 * Set the TTL of a socket protocol independently.
875 */
876 static int ping_set_ttl (pinghost_t *ph, int ttl)
877 {
878 int ret = -2;
880 if (ph->addrfamily == AF_INET)
881 {
882 dprintf ("Setting TTLv4 to %i\n", ttl);
883 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
884 &ttl, sizeof (ttl));
885 }
886 else if (ph->addrfamily == AF_INET6)
887 {
888 dprintf ("Setting TTLv6 to %i\n", ttl);
889 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
890 &ttl, sizeof (ttl));
891 }
893 return (ret);
894 }
896 static int ping_get_ident (void)
897 {
898 int fd;
899 static int did_seed = 0;
901 int retval;
903 if (did_seed == 0)
904 {
905 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
906 {
907 unsigned int seed;
909 if (read (fd, &seed, sizeof (seed)) != -1)
910 {
911 did_seed = 1;
912 dprintf ("Random seed: %#x\n", seed);
913 srandom (seed);
914 }
916 close (fd);
917 }
918 #if WITH_DEBUG
919 else
920 {
921 char errbuf[PING_ERRMSG_LEN];
922 dprintf ("open (/dev/urandom): %s\n",
923 sstrerror (errno, errbuf, sizeof (errbuf)));
924 }
925 #endif
926 }
928 retval = (int) random ();
930 dprintf ("Random number: %#x\n", retval);
932 return (retval);
933 }
935 static pinghost_t *ping_alloc (void)
936 {
937 pinghost_t *ph;
938 size_t ph_size;
940 ph_size = sizeof (pinghost_t)
941 + sizeof (struct sockaddr_storage)
942 + sizeof (struct timeval);
944 ph = (pinghost_t *) malloc (ph_size);
945 if (ph == NULL)
946 return (NULL);
948 memset (ph, '\0', ph_size);
950 ph->timer = (struct timeval *) (ph + 1);
951 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
953 ph->addrlen = sizeof (struct sockaddr_storage);
954 ph->fd = -1;
955 ph->latency = -1.0;
956 ph->dropped = 0;
957 ph->ident = ping_get_ident () & 0xFFFF;
959 return (ph);
960 }
962 static void ping_free (pinghost_t *ph)
963 {
964 if (ph->fd >= 0)
965 close (ph->fd);
967 if (ph->username != NULL)
968 free (ph->username);
970 if (ph->hostname != NULL)
971 free (ph->hostname);
973 if (ph->data != NULL)
974 free (ph->data);
976 free (ph);
977 }
979 /*
980 * public methods
981 */
982 const char *ping_get_error (pingobj_t *obj)
983 {
984 return (obj->errmsg);
985 }
987 pingobj_t *ping_construct (void)
988 {
989 pingobj_t *obj;
991 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
992 return (NULL);
993 memset (obj, '\0', sizeof (pingobj_t));
995 obj->timeout = PING_DEF_TIMEOUT;
996 obj->ttl = PING_DEF_TTL;
997 obj->addrfamily = PING_DEF_AF;
998 obj->data = strdup (PING_DEF_DATA);
1000 return (obj);
1001 }
1003 void ping_destroy (pingobj_t *obj)
1004 {
1005 pinghost_t *current;
1006 pinghost_t *next;
1008 current = obj->head;
1009 next = NULL;
1011 while (current != NULL)
1012 {
1013 next = current->next;
1014 ping_free (current);
1015 current = next;
1016 }
1018 if (obj->data != NULL)
1019 free (obj->data);
1021 if (obj->srcaddr != NULL)
1022 free (obj->srcaddr);
1024 free (obj);
1026 return;
1027 }
1029 int ping_setopt (pingobj_t *obj, int option, void *value)
1030 {
1031 int ret = 0;
1033 switch (option)
1034 {
1035 case PING_OPT_TIMEOUT:
1036 obj->timeout = *((double *) value);
1037 if (obj->timeout < 0.0)
1038 {
1039 obj->timeout = PING_DEF_TIMEOUT;
1040 ret = -1;
1041 }
1042 break;
1044 case PING_OPT_TTL:
1045 obj->ttl = *((int *) value);
1046 if ((obj->ttl < 1) || (obj->ttl > 255))
1047 {
1048 obj->ttl = PING_DEF_TTL;
1049 ret = -1;
1050 }
1051 else
1052 {
1053 pinghost_t *ph;
1055 for (ph = obj->head; ph != NULL; ph = ph->next)
1056 ping_set_ttl (ph, obj->ttl);
1057 }
1058 break;
1060 case PING_OPT_AF:
1061 obj->addrfamily = *((int *) value);
1062 if ((obj->addrfamily != AF_UNSPEC)
1063 && (obj->addrfamily != AF_INET)
1064 && (obj->addrfamily != AF_INET6))
1065 {
1066 obj->addrfamily = PING_DEF_AF;
1067 ret = -1;
1068 }
1069 if (obj->srcaddr != NULL)
1070 {
1071 free (obj->srcaddr);
1072 obj->srcaddr = NULL;
1073 }
1074 break;
1076 case PING_OPT_DATA:
1077 if (obj->data != NULL)
1078 {
1079 free (obj->data);
1080 obj->data = NULL;
1081 }
1082 obj->data = strdup ((const char *) value);
1083 break;
1085 case PING_OPT_SOURCE:
1086 {
1087 char *hostname = (char *) value;
1088 struct addrinfo ai_hints;
1089 struct addrinfo *ai_list;
1090 int status;
1091 #if WITH_DEBUG
1092 if (obj->addrfamily != AF_UNSPEC)
1093 {
1094 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1095 }
1096 #endif
1097 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1098 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1099 #if defined(AI_ADDRCONFIG)
1100 ai_hints.ai_flags = AI_ADDRCONFIG;
1101 #endif
1102 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1103 if (status != 0)
1104 {
1105 #if defined(EAI_SYSTEM)
1106 char errbuf[PING_ERRMSG_LEN];
1107 #endif
1108 ping_set_error (obj, "getaddrinfo",
1109 #if defined(EAI_SYSTEM)
1110 (status == EAI_SYSTEM)
1111 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1112 #endif
1113 gai_strerror (status));
1114 ret = -1;
1115 break;
1116 }
1117 #if WITH_DEBUG
1118 if (ai_list->ai_next != NULL)
1119 {
1120 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1121 }
1122 #endif
1123 if (obj->srcaddr == NULL)
1124 {
1125 obj->srcaddrlen = 0;
1126 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1127 if (obj->srcaddr == NULL)
1128 {
1129 ping_set_errno (obj, errno);
1130 ret = -1;
1131 freeaddrinfo (ai_list);
1132 break;
1133 }
1134 }
1135 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1136 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1137 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1138 ai_list->ai_addrlen);
1139 obj->srcaddrlen = ai_list->ai_addrlen;
1140 obj->addrfamily = ai_list->ai_family;
1142 freeaddrinfo (ai_list);
1143 } /* case PING_OPT_SOURCE */
1144 break;
1146 default:
1147 ret = -2;
1148 } /* switch (option) */
1150 return (ret);
1151 } /* int ping_setopt */
1154 int ping_send (pingobj_t *obj)
1155 {
1156 int ret;
1158 if (ping_send_all (obj) < 0)
1159 return (-1);
1161 if ((ret = ping_receive_all (obj)) < 0)
1162 return (-2);
1164 return (ret);
1165 }
1167 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1168 {
1169 while (ph != NULL)
1170 {
1171 if (strcasecmp (ph->username, host) == 0)
1172 break;
1174 ph = ph->next;
1175 }
1177 return (ph);
1178 }
1180 int ping_host_add (pingobj_t *obj, const char *host)
1181 {
1182 pinghost_t *ph;
1184 struct addrinfo ai_hints;
1185 struct addrinfo *ai_list, *ai_ptr;
1186 int ai_return;
1188 dprintf ("host = %s\n", host);
1190 if (ping_host_search (obj->head, host) != NULL)
1191 return (0);
1193 memset (&ai_hints, '\0', sizeof (ai_hints));
1194 ai_hints.ai_flags = 0;
1195 #ifdef AI_ADDRCONFIG
1196 ai_hints.ai_flags |= AI_ADDRCONFIG;
1197 #endif
1198 #ifdef AI_CANONNAME
1199 ai_hints.ai_flags |= AI_CANONNAME;
1200 #endif
1201 ai_hints.ai_family = obj->addrfamily;
1202 ai_hints.ai_socktype = SOCK_RAW;
1204 if ((ph = ping_alloc ()) == NULL)
1205 {
1206 dprintf ("Out of memory!\n");
1207 return (-1);
1208 }
1210 if ((ph->username = strdup (host)) == NULL)
1211 {
1212 dprintf ("Out of memory!\n");
1213 ping_set_errno (obj, errno);
1214 ping_free (ph);
1215 return (-1);
1216 }
1218 if ((ph->hostname = strdup (host)) == NULL)
1219 {
1220 dprintf ("Out of memory!\n");
1221 ping_set_errno (obj, errno);
1222 ping_free (ph);
1223 return (-1);
1224 }
1226 /* obj->data is not garuanteed to be != NULL */
1227 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1228 {
1229 dprintf ("Out of memory!\n");
1230 ping_set_errno (obj, errno);
1231 ping_free (ph);
1232 return (-1);
1233 }
1235 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1236 {
1237 #if defined(EAI_SYSTEM)
1238 char errbuf[PING_ERRMSG_LEN];
1239 #endif
1240 dprintf ("getaddrinfo failed\n");
1241 ping_set_error (obj, "getaddrinfo",
1242 #if defined(EAI_SYSTEM)
1243 (ai_return == EAI_SYSTEM)
1244 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1245 #endif
1246 gai_strerror (ai_return));
1247 ping_free (ph);
1248 return (-1);
1249 }
1251 if (ai_list == NULL)
1252 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1254 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1255 {
1256 ph->fd = -1;
1258 if (ai_ptr->ai_family == AF_INET)
1259 {
1260 ai_ptr->ai_socktype = SOCK_RAW;
1261 ai_ptr->ai_protocol = IPPROTO_ICMP;
1262 }
1263 else if (ai_ptr->ai_family == AF_INET6)
1264 {
1265 ai_ptr->ai_socktype = SOCK_RAW;
1266 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1267 }
1268 else
1269 {
1270 char errmsg[PING_ERRMSG_LEN];
1272 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1273 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1275 dprintf (errmsg);
1276 ping_set_error (obj, "getaddrinfo", errmsg);
1277 continue;
1278 }
1280 /* TODO: Move this to a static function `ping_open_socket' and
1281 * call it whenever the socket dies. */
1282 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1283 if (ph->fd == -1)
1284 {
1285 #if WITH_DEBUG
1286 char errbuf[PING_ERRMSG_LEN];
1287 dprintf ("socket: %s\n",
1288 sstrerror (errno, errbuf, sizeof (errbuf)));
1289 #endif
1290 ping_set_errno (obj, errno);
1291 continue;
1292 }
1294 if (obj->srcaddr != NULL)
1295 {
1296 assert (obj->srcaddrlen > 0);
1297 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1299 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1300 {
1301 #if WITH_DEBUG
1302 char errbuf[PING_ERRMSG_LEN];
1303 dprintf ("bind: %s\n",
1304 sstrerror (errno, errbuf, sizeof (errbuf)));
1305 #endif
1306 ping_set_errno (obj, errno);
1307 close (ph->fd);
1308 ph->fd = -1;
1309 continue;
1310 }
1311 }
1313 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1314 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1315 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1316 ph->addrlen = ai_ptr->ai_addrlen;
1317 ph->addrfamily = ai_ptr->ai_family;
1319 #ifdef AI_CANONNAME
1320 if ((ai_ptr->ai_canonname != NULL)
1321 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1322 {
1323 char *old_hostname;
1325 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1326 ph->hostname, ai_ptr->ai_canonname);
1328 old_hostname = ph->hostname;
1329 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1330 {
1331 /* strdup failed, falling back to old hostname */
1332 ph->hostname = old_hostname;
1333 }
1334 else if (old_hostname != NULL)
1335 {
1336 free (old_hostname);
1337 }
1338 }
1339 #endif /* AI_CANONNAME */
1341 if (ph->addrfamily == AF_INET)
1342 {
1343 int opt = 1;
1345 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1346 &opt, sizeof (opt));
1347 }
1348 else if (ph->addrfamily == AF_INET6)
1349 {
1350 int opt = 1;
1352 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1353 &opt, sizeof (opt));
1354 }
1356 break;
1357 }
1359 freeaddrinfo (ai_list);
1361 if (ph->fd < 0)
1362 {
1363 ping_free (ph);
1364 return (-1);
1365 }
1367 /*
1368 * Adding in the front is much easier, but then the iterator will
1369 * return the host that was added last as first host. That's just not
1370 * nice. -octo
1371 */
1372 if (obj->head == NULL)
1373 {
1374 obj->head = ph;
1375 }
1376 else
1377 {
1378 pinghost_t *hptr;
1380 hptr = obj->head;
1381 while (hptr->next != NULL)
1382 hptr = hptr->next;
1384 assert ((hptr != NULL) && (hptr->next == NULL));
1385 hptr->next = ph;
1386 }
1388 ping_set_ttl (ph, obj->ttl);
1390 return (0);
1391 } /* int ping_host_add */
1393 int ping_host_remove (pingobj_t *obj, const char *host)
1394 {
1395 pinghost_t *pre, *cur;
1397 pre = NULL;
1398 cur = obj->head;
1400 while (cur != NULL)
1401 {
1402 if (strcasecmp (host, cur->username) == 0)
1403 break;
1405 pre = cur;
1406 cur = cur->next;
1407 }
1409 if (cur == NULL)
1410 {
1411 ping_set_error (obj, "ping_host_remove", "Host not found");
1412 return (-1);
1413 }
1415 if (pre == NULL)
1416 obj->head = cur->next;
1417 else
1418 pre->next = cur->next;
1420 ping_free (cur);
1422 return (0);
1423 }
1425 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1426 {
1427 return ((pingobj_iter_t *) obj->head);
1428 }
1430 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1431 {
1432 return ((pingobj_iter_t *) iter->next);
1433 }
1435 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1436 void *buffer, size_t *buffer_len)
1437 {
1438 int ret = EINVAL;
1440 size_t orig_buffer_len = *buffer_len;
1442 switch (info)
1443 {
1444 case PING_INFO_USERNAME:
1445 ret = ENOMEM;
1446 *buffer_len = strlen (iter->username) + 1;
1447 if (orig_buffer_len <= *buffer_len)
1448 break;
1449 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1450 * will copy `*buffer_len' and pad the rest of
1451 * `buffer' with null-bytes */
1452 strncpy (buffer, iter->username, orig_buffer_len);
1453 ret = 0;
1454 break;
1456 case PING_INFO_HOSTNAME:
1457 ret = ENOMEM;
1458 *buffer_len = strlen (iter->hostname) + 1;
1459 if (orig_buffer_len < *buffer_len)
1460 break;
1461 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1462 * will copy `*buffer_len' and pad the rest of
1463 * `buffer' with null-bytes */
1464 strncpy (buffer, iter->hostname, orig_buffer_len);
1465 ret = 0;
1466 break;
1468 case PING_INFO_ADDRESS:
1469 ret = getnameinfo ((struct sockaddr *) iter->addr,
1470 iter->addrlen,
1471 (char *) buffer,
1472 *buffer_len,
1473 NULL, 0,
1474 NI_NUMERICHOST);
1475 if (ret != 0)
1476 {
1477 if ((ret == EAI_MEMORY)
1478 #ifdef EAI_OVERFLOW
1479 || (ret == EAI_OVERFLOW)
1480 #endif
1481 )
1482 ret = ENOMEM;
1483 #if defined(EAI_SYSTEM)
1484 else if (ret == EAI_SYSTEM)
1485 ret = errno;
1486 #endif
1487 else
1488 ret = EINVAL;
1489 }
1490 break;
1492 case PING_INFO_FAMILY:
1493 ret = ENOMEM;
1494 *buffer_len = sizeof (int);
1495 if (orig_buffer_len < sizeof (int))
1496 break;
1497 *((int *) buffer) = iter->addrfamily;
1498 ret = 0;
1499 break;
1501 case PING_INFO_LATENCY:
1502 ret = ENOMEM;
1503 *buffer_len = sizeof (double);
1504 if (orig_buffer_len < sizeof (double))
1505 break;
1506 *((double *) buffer) = iter->latency;
1507 ret = 0;
1508 break;
1510 case PING_INFO_DROPPED:
1511 ret = ENOMEM;
1512 *buffer_len = sizeof (uint32_t);
1513 if (orig_buffer_len < sizeof (uint32_t))
1514 break;
1515 *((uint32_t *) buffer) = iter->dropped;
1516 ret = 0;
1517 break;
1519 case PING_INFO_SEQUENCE:
1520 ret = ENOMEM;
1521 *buffer_len = sizeof (unsigned int);
1522 if (orig_buffer_len < sizeof (unsigned int))
1523 break;
1524 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1525 ret = 0;
1526 break;
1528 case PING_INFO_IDENT:
1529 ret = ENOMEM;
1530 *buffer_len = sizeof (uint16_t);
1531 if (orig_buffer_len < sizeof (uint16_t))
1532 break;
1533 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1534 ret = 0;
1535 break;
1537 case PING_INFO_DATA:
1538 ret = ENOMEM;
1539 *buffer_len = strlen (iter->data);
1540 if (orig_buffer_len < *buffer_len)
1541 break;
1542 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1543 ret = 0;
1544 break;
1546 case PING_INFO_RECV_TTL:
1547 ret = ENOMEM;
1548 *buffer_len = sizeof (int);
1549 if (orig_buffer_len < sizeof (int))
1550 break;
1551 *((int *) buffer) = iter->recv_ttl;
1552 ret = 0;
1553 break;
1554 }
1556 return (ret);
1557 } /* ping_iterator_get_info */
1559 void *ping_iterator_get_context (pingobj_iter_t *iter)
1560 {
1561 return (iter->context);
1562 }
1564 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1565 {
1566 iter->context = context;
1567 }