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 ph->recv_ttl = -1;
482 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
483 cmsg != NULL;
484 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
485 {
486 if (ph->addrfamily == AF_INET) /* {{{ */
487 {
488 if (cmsg->cmsg_level != IPPROTO_IP)
489 continue;
491 if (cmsg->cmsg_type == IP_TTL)
492 {
493 memcpy (&recv_ttl, CMSG_DATA (cmsg),
494 sizeof (recv_ttl));
495 dprintf ("TTLv4 = %i;\n", recv_ttl);
496 }
497 else
498 {
499 dprintf ("Not handling option %i.\n",
500 cmsg->cmsg_type);
501 }
502 } /* }}} */
503 else if (ph->addrfamily == AF_INET6) /* {{{ */
504 {
505 if (cmsg->cmsg_level != IPPROTO_IPV6)
506 continue;
508 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
509 {
510 memcpy (&recv_ttl, CMSG_DATA (cmsg),
511 sizeof (recv_ttl));
512 dprintf ("TTLv6 = %i;\n", recv_ttl);
513 }
514 else
515 {
516 dprintf ("Not handling option %i.\n",
517 cmsg->cmsg_type);
518 }
519 } /* }}} */
520 else
521 {
522 dprintf ("Don't know how to handle "
523 "unknown protocol %i.\n",
524 cmsg->cmsg_level);
525 }
526 } /* }}} for (cmsg) */
528 if (ph->addrfamily == AF_INET)
529 {
530 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
531 if (host == NULL)
532 return (-1);
533 }
534 else if (ph->addrfamily == AF_INET6)
535 {
536 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
537 if (host == NULL)
538 return (-1);
539 }
540 else
541 {
542 dprintf ("ping_receive_one: Unknown address family %i.\n",
543 ph->addrfamily);
544 return (-1);
545 }
547 dprintf ("rcvd: %12i.%06i\n",
548 (int) now->tv_sec,
549 (int) now->tv_usec);
550 dprintf ("sent: %12i.%06i\n",
551 (int) host->timer->tv_sec,
552 (int) host->timer->tv_usec);
554 if (ping_timeval_sub (now, host->timer, &diff) < 0)
555 {
556 timerclear (host->timer);
557 return (-1);
558 }
560 dprintf ("diff: %12i.%06i\n",
561 (int) diff.tv_sec,
562 (int) diff.tv_usec);
564 if (recv_ttl >= 0)
565 host->recv_ttl = recv_ttl;
567 host->latency = ((double) diff.tv_usec) / 1000.0;
568 host->latency += ((double) diff.tv_sec) * 1000.0;
570 timerclear (host->timer);
572 return (0);
573 }
575 static int ping_receive_all (pingobj_t *obj)
576 {
577 fd_set readfds;
578 int num_readfds;
579 int max_readfds;
581 pinghost_t *ph;
582 pinghost_t *ptr;
584 struct timeval endtime;
585 struct timeval nowtime;
586 struct timeval timeout;
587 int status;
589 int ret;
591 ph = obj->head;
592 ret = 0;
594 for (ptr = ph; ptr != NULL; ptr = ptr->next)
595 ptr->latency = -1.0;
597 if (gettimeofday (&nowtime, NULL) == -1)
598 {
599 ping_set_errno (obj, errno);
600 return (-1);
601 }
603 /* Set up timeout */
604 timeout.tv_sec = (time_t) obj->timeout;
605 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
607 dprintf ("Set timeout to %i.%06i seconds\n",
608 (int) timeout.tv_sec,
609 (int) timeout.tv_usec);
611 ping_timeval_add (&nowtime, &timeout, &endtime);
613 while (1)
614 {
615 FD_ZERO (&readfds);
616 num_readfds = 0;
617 max_readfds = -1;
619 for (ptr = ph; ptr != NULL; ptr = ptr->next)
620 {
621 if (!timerisset (ptr->timer))
622 continue;
624 FD_SET (ptr->fd, &readfds);
625 num_readfds++;
627 if (max_readfds < ptr->fd)
628 max_readfds = ptr->fd;
629 }
631 if (num_readfds == 0)
632 break;
634 if (gettimeofday (&nowtime, NULL) == -1)
635 {
636 ping_set_errno (obj, errno);
637 return (-1);
638 }
640 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
641 break;
643 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
644 (int) timeout.tv_sec,
645 (int) timeout.tv_usec);
647 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
649 if (gettimeofday (&nowtime, NULL) == -1)
650 {
651 ping_set_errno (obj, errno);
652 return (-1);
653 }
655 if ((status == -1) && (errno == EINTR))
656 {
657 dprintf ("select was interrupted by signal..\n");
658 continue;
659 }
660 else if (status < 0)
661 {
662 #if WITH_DEBUG
663 char errbuf[PING_ERRMSG_LEN];
664 dprintf ("select: %s\n",
665 sstrerror (errno, errbuf, sizeof (errbuf)));
666 #endif
667 break;
668 }
669 else if (status == 0)
670 {
671 dprintf ("select timed out\n");
672 for (ptr = ph; ptr != NULL; ptr = ptr->next)
673 if (ptr->latency < 0.0)
674 ptr->dropped++;
675 break;
676 }
678 for (ptr = ph; ptr != NULL; ptr = ptr->next)
679 {
680 if (FD_ISSET (ptr->fd, &readfds))
681 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
682 ret++;
683 }
684 } /* while (1) */
686 return (ret);
687 }
689 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
690 * Sending functions: *
691 * *
692 * ping_send_all *
693 * +-> ping_send_one_ipv4 *
694 * `-> ping_send_one_ipv6 *
695 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
696 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
697 const void *buf, size_t buflen)
698 {
699 ssize_t ret;
701 if (gettimeofday (ph->timer, NULL) == -1)
702 {
703 timerclear (ph->timer);
704 return (-1);
705 }
707 ret = sendto (ph->fd, buf, buflen, 0,
708 (struct sockaddr *) ph->addr, ph->addrlen);
710 if (ret < 0)
711 {
712 #if defined(EHOSTUNREACH)
713 if (errno == EHOSTUNREACH)
714 return (0);
715 #endif
716 #if defined(ENETUNREACH)
717 if (errno == ENETUNREACH)
718 return (0);
719 #endif
720 ping_set_errno (obj, errno);
721 }
723 return (ret);
724 }
726 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
727 {
728 struct icmp *icmp4;
729 int status;
731 char buf[4096];
732 int buflen;
734 char *data;
735 int datalen;
737 dprintf ("ph->hostname = %s\n", ph->hostname);
739 memset (buf, '\0', sizeof (buf));
740 icmp4 = (struct icmp *) buf;
741 data = (char *) (icmp4 + 1);
743 icmp4->icmp_type = ICMP_ECHO;
744 icmp4->icmp_code = 0;
745 icmp4->icmp_cksum = 0;
746 icmp4->icmp_id = htons (ph->ident);
747 icmp4->icmp_seq = htons (ph->sequence);
749 buflen = 4096 - sizeof (struct icmp);
750 strncpy (data, ph->data, buflen);
751 datalen = strlen (data);
753 buflen = datalen + sizeof (struct icmp);
755 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
757 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
759 status = ping_sendto (obj, ph, buf, buflen);
760 if (status < 0)
761 {
762 perror ("ping_sendto");
763 return (-1);
764 }
766 dprintf ("sendto: status = %i\n", status);
768 return (0);
769 }
771 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
772 {
773 struct icmp6_hdr *icmp6;
774 int status;
776 char buf[4096];
777 int buflen;
779 char *data;
780 int datalen;
782 dprintf ("ph->hostname = %s\n", ph->hostname);
784 memset (buf, '\0', sizeof (buf));
785 icmp6 = (struct icmp6_hdr *) buf;
786 data = (char *) (icmp6 + 1);
788 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
789 icmp6->icmp6_code = 0;
790 /* The checksum will be calculated by the TCP/IP stack. */
791 /* FIXME */
792 icmp6->icmp6_cksum = 0;
793 icmp6->icmp6_id = htons (ph->ident);
794 icmp6->icmp6_seq = htons (ph->sequence);
796 buflen = 4096 - sizeof (struct icmp6_hdr);
797 strncpy (data, ph->data, buflen);
798 datalen = strlen (data);
800 buflen = datalen + sizeof (struct icmp6_hdr);
802 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
804 status = ping_sendto (obj, ph, buf, buflen);
805 if (status < 0)
806 {
807 perror ("ping_sendto");
808 return (-1);
809 }
811 dprintf ("sendto: status = %i\n", status);
813 return (0);
814 }
816 static int ping_send_all (pingobj_t *obj)
817 {
818 pinghost_t *ph;
819 pinghost_t *ptr;
821 int ret;
823 ret = 0;
824 ph = obj->head;
826 for (ptr = ph; ptr != NULL; ptr = ptr->next)
827 {
828 /* start timer.. The GNU `ping6' starts the timer before
829 * sending the packet, so I will do that too */
830 if (gettimeofday (ptr->timer, NULL) == -1)
831 {
832 #if WITH_DEBUG
833 char errbuf[PING_ERRMSG_LEN];
834 dprintf ("gettimeofday: %s\n",
835 sstrerror (errno, errbuf, sizeof (errbuf)));
836 #endif
837 timerclear (ptr->timer);
838 ret--;
839 continue;
840 }
841 else
842 {
843 dprintf ("timer set for hostname = %s\n", ptr->hostname);
844 }
846 if (ptr->addrfamily == AF_INET6)
847 {
848 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
849 if (ping_send_one_ipv6 (obj, ptr) != 0)
850 {
851 timerclear (ptr->timer);
852 ret--;
853 continue;
854 }
855 }
856 else if (ptr->addrfamily == AF_INET)
857 {
858 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
859 if (ping_send_one_ipv4 (obj, ptr) != 0)
860 {
861 timerclear (ptr->timer);
862 ret--;
863 continue;
864 }
865 }
866 else /* this should not happen */
867 {
868 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
869 timerclear (ptr->timer);
870 ret--;
871 continue;
872 }
874 ptr->sequence++;
875 }
877 return (ret);
878 }
880 /*
881 * Set the TTL of a socket protocol independently.
882 */
883 static int ping_set_ttl (pinghost_t *ph, int ttl)
884 {
885 int ret = -2;
887 if (ph->addrfamily == AF_INET)
888 {
889 dprintf ("Setting TTLv4 to %i\n", ttl);
890 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
891 &ttl, sizeof (ttl));
892 }
893 else if (ph->addrfamily == AF_INET6)
894 {
895 dprintf ("Setting TTLv6 to %i\n", ttl);
896 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
897 &ttl, sizeof (ttl));
898 }
900 return (ret);
901 }
903 static int ping_get_ident (void)
904 {
905 int fd;
906 static int did_seed = 0;
908 int retval;
910 if (did_seed == 0)
911 {
912 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
913 {
914 unsigned int seed;
916 if (read (fd, &seed, sizeof (seed)) != -1)
917 {
918 did_seed = 1;
919 dprintf ("Random seed: %#x\n", seed);
920 srandom (seed);
921 }
923 close (fd);
924 }
925 #if WITH_DEBUG
926 else
927 {
928 char errbuf[PING_ERRMSG_LEN];
929 dprintf ("open (/dev/urandom): %s\n",
930 sstrerror (errno, errbuf, sizeof (errbuf)));
931 }
932 #endif
933 }
935 retval = (int) random ();
937 dprintf ("Random number: %#x\n", retval);
939 return (retval);
940 }
942 static pinghost_t *ping_alloc (void)
943 {
944 pinghost_t *ph;
945 size_t ph_size;
947 ph_size = sizeof (pinghost_t)
948 + sizeof (struct sockaddr_storage)
949 + sizeof (struct timeval);
951 ph = (pinghost_t *) malloc (ph_size);
952 if (ph == NULL)
953 return (NULL);
955 memset (ph, '\0', ph_size);
957 ph->timer = (struct timeval *) (ph + 1);
958 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
960 ph->addrlen = sizeof (struct sockaddr_storage);
961 ph->fd = -1;
962 ph->latency = -1.0;
963 ph->dropped = 0;
964 ph->ident = ping_get_ident () & 0xFFFF;
966 return (ph);
967 }
969 static void ping_free (pinghost_t *ph)
970 {
971 if (ph->fd >= 0)
972 close (ph->fd);
974 if (ph->username != NULL)
975 free (ph->username);
977 if (ph->hostname != NULL)
978 free (ph->hostname);
980 if (ph->data != NULL)
981 free (ph->data);
983 free (ph);
984 }
986 /*
987 * public methods
988 */
989 const char *ping_get_error (pingobj_t *obj)
990 {
991 return (obj->errmsg);
992 }
994 pingobj_t *ping_construct (void)
995 {
996 pingobj_t *obj;
998 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
999 return (NULL);
1000 memset (obj, '\0', sizeof (pingobj_t));
1002 obj->timeout = PING_DEF_TIMEOUT;
1003 obj->ttl = PING_DEF_TTL;
1004 obj->addrfamily = PING_DEF_AF;
1005 obj->data = strdup (PING_DEF_DATA);
1007 return (obj);
1008 }
1010 void ping_destroy (pingobj_t *obj)
1011 {
1012 pinghost_t *current;
1013 pinghost_t *next;
1015 current = obj->head;
1016 next = NULL;
1018 while (current != NULL)
1019 {
1020 next = current->next;
1021 ping_free (current);
1022 current = next;
1023 }
1025 if (obj->data != NULL)
1026 free (obj->data);
1028 if (obj->srcaddr != NULL)
1029 free (obj->srcaddr);
1031 free (obj);
1033 return;
1034 }
1036 int ping_setopt (pingobj_t *obj, int option, void *value)
1037 {
1038 int ret = 0;
1040 switch (option)
1041 {
1042 case PING_OPT_TIMEOUT:
1043 obj->timeout = *((double *) value);
1044 if (obj->timeout < 0.0)
1045 {
1046 obj->timeout = PING_DEF_TIMEOUT;
1047 ret = -1;
1048 }
1049 break;
1051 case PING_OPT_TTL:
1052 obj->ttl = *((int *) value);
1053 if ((obj->ttl < 1) || (obj->ttl > 255))
1054 {
1055 obj->ttl = PING_DEF_TTL;
1056 ret = -1;
1057 }
1058 else
1059 {
1060 pinghost_t *ph;
1062 for (ph = obj->head; ph != NULL; ph = ph->next)
1063 ping_set_ttl (ph, obj->ttl);
1064 }
1065 break;
1067 case PING_OPT_AF:
1068 obj->addrfamily = *((int *) value);
1069 if ((obj->addrfamily != AF_UNSPEC)
1070 && (obj->addrfamily != AF_INET)
1071 && (obj->addrfamily != AF_INET6))
1072 {
1073 obj->addrfamily = PING_DEF_AF;
1074 ret = -1;
1075 }
1076 if (obj->srcaddr != NULL)
1077 {
1078 free (obj->srcaddr);
1079 obj->srcaddr = NULL;
1080 }
1081 break;
1083 case PING_OPT_DATA:
1084 if (obj->data != NULL)
1085 {
1086 free (obj->data);
1087 obj->data = NULL;
1088 }
1089 obj->data = strdup ((const char *) value);
1090 break;
1092 case PING_OPT_SOURCE:
1093 {
1094 char *hostname = (char *) value;
1095 struct addrinfo ai_hints;
1096 struct addrinfo *ai_list;
1097 int status;
1098 #if WITH_DEBUG
1099 if (obj->addrfamily != AF_UNSPEC)
1100 {
1101 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1102 }
1103 #endif
1104 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1105 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1106 #if defined(AI_ADDRCONFIG)
1107 ai_hints.ai_flags = AI_ADDRCONFIG;
1108 #endif
1109 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1110 if (status != 0)
1111 {
1112 #if defined(EAI_SYSTEM)
1113 char errbuf[PING_ERRMSG_LEN];
1114 #endif
1115 ping_set_error (obj, "getaddrinfo",
1116 #if defined(EAI_SYSTEM)
1117 (status == EAI_SYSTEM)
1118 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1119 #endif
1120 gai_strerror (status));
1121 ret = -1;
1122 break;
1123 }
1124 #if WITH_DEBUG
1125 if (ai_list->ai_next != NULL)
1126 {
1127 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1128 }
1129 #endif
1130 if (obj->srcaddr == NULL)
1131 {
1132 obj->srcaddrlen = 0;
1133 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1134 if (obj->srcaddr == NULL)
1135 {
1136 ping_set_errno (obj, errno);
1137 ret = -1;
1138 freeaddrinfo (ai_list);
1139 break;
1140 }
1141 }
1142 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1143 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1144 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1145 ai_list->ai_addrlen);
1146 obj->srcaddrlen = ai_list->ai_addrlen;
1147 obj->addrfamily = ai_list->ai_family;
1149 freeaddrinfo (ai_list);
1150 } /* case PING_OPT_SOURCE */
1151 break;
1153 default:
1154 ret = -2;
1155 } /* switch (option) */
1157 return (ret);
1158 } /* int ping_setopt */
1161 int ping_send (pingobj_t *obj)
1162 {
1163 int ret;
1165 if (ping_send_all (obj) < 0)
1166 return (-1);
1168 if ((ret = ping_receive_all (obj)) < 0)
1169 return (-2);
1171 return (ret);
1172 }
1174 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1175 {
1176 while (ph != NULL)
1177 {
1178 if (strcasecmp (ph->username, host) == 0)
1179 break;
1181 ph = ph->next;
1182 }
1184 return (ph);
1185 }
1187 int ping_host_add (pingobj_t *obj, const char *host)
1188 {
1189 pinghost_t *ph;
1191 struct addrinfo ai_hints;
1192 struct addrinfo *ai_list, *ai_ptr;
1193 int ai_return;
1195 dprintf ("host = %s\n", host);
1197 if (ping_host_search (obj->head, host) != NULL)
1198 return (0);
1200 memset (&ai_hints, '\0', sizeof (ai_hints));
1201 ai_hints.ai_flags = 0;
1202 #ifdef AI_ADDRCONFIG
1203 ai_hints.ai_flags |= AI_ADDRCONFIG;
1204 #endif
1205 #ifdef AI_CANONNAME
1206 ai_hints.ai_flags |= AI_CANONNAME;
1207 #endif
1208 ai_hints.ai_family = obj->addrfamily;
1209 ai_hints.ai_socktype = SOCK_RAW;
1211 if ((ph = ping_alloc ()) == NULL)
1212 {
1213 dprintf ("Out of memory!\n");
1214 return (-1);
1215 }
1217 if ((ph->username = strdup (host)) == NULL)
1218 {
1219 dprintf ("Out of memory!\n");
1220 ping_set_errno (obj, errno);
1221 ping_free (ph);
1222 return (-1);
1223 }
1225 if ((ph->hostname = strdup (host)) == NULL)
1226 {
1227 dprintf ("Out of memory!\n");
1228 ping_set_errno (obj, errno);
1229 ping_free (ph);
1230 return (-1);
1231 }
1233 /* obj->data is not garuanteed to be != NULL */
1234 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1235 {
1236 dprintf ("Out of memory!\n");
1237 ping_set_errno (obj, errno);
1238 ping_free (ph);
1239 return (-1);
1240 }
1242 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1243 {
1244 #if defined(EAI_SYSTEM)
1245 char errbuf[PING_ERRMSG_LEN];
1246 #endif
1247 dprintf ("getaddrinfo failed\n");
1248 ping_set_error (obj, "getaddrinfo",
1249 #if defined(EAI_SYSTEM)
1250 (ai_return == EAI_SYSTEM)
1251 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1252 #endif
1253 gai_strerror (ai_return));
1254 ping_free (ph);
1255 return (-1);
1256 }
1258 if (ai_list == NULL)
1259 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1261 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1262 {
1263 ph->fd = -1;
1265 if (ai_ptr->ai_family == AF_INET)
1266 {
1267 ai_ptr->ai_socktype = SOCK_RAW;
1268 ai_ptr->ai_protocol = IPPROTO_ICMP;
1269 }
1270 else if (ai_ptr->ai_family == AF_INET6)
1271 {
1272 ai_ptr->ai_socktype = SOCK_RAW;
1273 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1274 }
1275 else
1276 {
1277 char errmsg[PING_ERRMSG_LEN];
1279 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1280 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1282 dprintf (errmsg);
1283 ping_set_error (obj, "getaddrinfo", errmsg);
1284 continue;
1285 }
1287 /* TODO: Move this to a static function `ping_open_socket' and
1288 * call it whenever the socket dies. */
1289 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1290 if (ph->fd == -1)
1291 {
1292 #if WITH_DEBUG
1293 char errbuf[PING_ERRMSG_LEN];
1294 dprintf ("socket: %s\n",
1295 sstrerror (errno, errbuf, sizeof (errbuf)));
1296 #endif
1297 ping_set_errno (obj, errno);
1298 continue;
1299 }
1301 if (obj->srcaddr != NULL)
1302 {
1303 assert (obj->srcaddrlen > 0);
1304 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1306 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1307 {
1308 #if WITH_DEBUG
1309 char errbuf[PING_ERRMSG_LEN];
1310 dprintf ("bind: %s\n",
1311 sstrerror (errno, errbuf, sizeof (errbuf)));
1312 #endif
1313 ping_set_errno (obj, errno);
1314 close (ph->fd);
1315 ph->fd = -1;
1316 continue;
1317 }
1318 }
1320 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1321 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1322 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1323 ph->addrlen = ai_ptr->ai_addrlen;
1324 ph->addrfamily = ai_ptr->ai_family;
1326 #ifdef AI_CANONNAME
1327 if ((ai_ptr->ai_canonname != NULL)
1328 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1329 {
1330 char *old_hostname;
1332 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1333 ph->hostname, ai_ptr->ai_canonname);
1335 old_hostname = ph->hostname;
1336 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1337 {
1338 /* strdup failed, falling back to old hostname */
1339 ph->hostname = old_hostname;
1340 }
1341 else if (old_hostname != NULL)
1342 {
1343 free (old_hostname);
1344 }
1345 }
1346 #endif /* AI_CANONNAME */
1348 if (ph->addrfamily == AF_INET)
1349 {
1350 int opt = 1;
1352 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1353 &opt, sizeof (opt));
1354 }
1355 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1356 else if (ph->addrfamily == AF_INET6)
1357 {
1358 int opt = 1;
1360 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1361 &opt, sizeof (opt));
1362 }
1363 #endif
1365 break;
1366 }
1368 freeaddrinfo (ai_list);
1370 if (ph->fd < 0)
1371 {
1372 ping_free (ph);
1373 return (-1);
1374 }
1376 /*
1377 * Adding in the front is much easier, but then the iterator will
1378 * return the host that was added last as first host. That's just not
1379 * nice. -octo
1380 */
1381 if (obj->head == NULL)
1382 {
1383 obj->head = ph;
1384 }
1385 else
1386 {
1387 pinghost_t *hptr;
1389 hptr = obj->head;
1390 while (hptr->next != NULL)
1391 hptr = hptr->next;
1393 assert ((hptr != NULL) && (hptr->next == NULL));
1394 hptr->next = ph;
1395 }
1397 ping_set_ttl (ph, obj->ttl);
1399 return (0);
1400 } /* int ping_host_add */
1402 int ping_host_remove (pingobj_t *obj, const char *host)
1403 {
1404 pinghost_t *pre, *cur;
1406 pre = NULL;
1407 cur = obj->head;
1409 while (cur != NULL)
1410 {
1411 if (strcasecmp (host, cur->username) == 0)
1412 break;
1414 pre = cur;
1415 cur = cur->next;
1416 }
1418 if (cur == NULL)
1419 {
1420 ping_set_error (obj, "ping_host_remove", "Host not found");
1421 return (-1);
1422 }
1424 if (pre == NULL)
1425 obj->head = cur->next;
1426 else
1427 pre->next = cur->next;
1429 ping_free (cur);
1431 return (0);
1432 }
1434 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1435 {
1436 return ((pingobj_iter_t *) obj->head);
1437 }
1439 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1440 {
1441 return ((pingobj_iter_t *) iter->next);
1442 }
1444 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1445 void *buffer, size_t *buffer_len)
1446 {
1447 int ret = EINVAL;
1449 size_t orig_buffer_len = *buffer_len;
1451 switch (info)
1452 {
1453 case PING_INFO_USERNAME:
1454 ret = ENOMEM;
1455 *buffer_len = strlen (iter->username) + 1;
1456 if (orig_buffer_len <= *buffer_len)
1457 break;
1458 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1459 * will copy `*buffer_len' and pad the rest of
1460 * `buffer' with null-bytes */
1461 strncpy (buffer, iter->username, orig_buffer_len);
1462 ret = 0;
1463 break;
1465 case PING_INFO_HOSTNAME:
1466 ret = ENOMEM;
1467 *buffer_len = strlen (iter->hostname) + 1;
1468 if (orig_buffer_len < *buffer_len)
1469 break;
1470 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1471 * will copy `*buffer_len' and pad the rest of
1472 * `buffer' with null-bytes */
1473 strncpy (buffer, iter->hostname, orig_buffer_len);
1474 ret = 0;
1475 break;
1477 case PING_INFO_ADDRESS:
1478 ret = getnameinfo ((struct sockaddr *) iter->addr,
1479 iter->addrlen,
1480 (char *) buffer,
1481 *buffer_len,
1482 NULL, 0,
1483 NI_NUMERICHOST);
1484 if (ret != 0)
1485 {
1486 if ((ret == EAI_MEMORY)
1487 #ifdef EAI_OVERFLOW
1488 || (ret == EAI_OVERFLOW)
1489 #endif
1490 )
1491 ret = ENOMEM;
1492 #if defined(EAI_SYSTEM)
1493 else if (ret == EAI_SYSTEM)
1494 ret = errno;
1495 #endif
1496 else
1497 ret = EINVAL;
1498 }
1499 break;
1501 case PING_INFO_FAMILY:
1502 ret = ENOMEM;
1503 *buffer_len = sizeof (int);
1504 if (orig_buffer_len < sizeof (int))
1505 break;
1506 *((int *) buffer) = iter->addrfamily;
1507 ret = 0;
1508 break;
1510 case PING_INFO_LATENCY:
1511 ret = ENOMEM;
1512 *buffer_len = sizeof (double);
1513 if (orig_buffer_len < sizeof (double))
1514 break;
1515 *((double *) buffer) = iter->latency;
1516 ret = 0;
1517 break;
1519 case PING_INFO_DROPPED:
1520 ret = ENOMEM;
1521 *buffer_len = sizeof (uint32_t);
1522 if (orig_buffer_len < sizeof (uint32_t))
1523 break;
1524 *((uint32_t *) buffer) = iter->dropped;
1525 ret = 0;
1526 break;
1528 case PING_INFO_SEQUENCE:
1529 ret = ENOMEM;
1530 *buffer_len = sizeof (unsigned int);
1531 if (orig_buffer_len < sizeof (unsigned int))
1532 break;
1533 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1534 ret = 0;
1535 break;
1537 case PING_INFO_IDENT:
1538 ret = ENOMEM;
1539 *buffer_len = sizeof (uint16_t);
1540 if (orig_buffer_len < sizeof (uint16_t))
1541 break;
1542 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1543 ret = 0;
1544 break;
1546 case PING_INFO_DATA:
1547 ret = ENOMEM;
1548 *buffer_len = strlen (iter->data);
1549 if (orig_buffer_len < *buffer_len)
1550 break;
1551 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1552 ret = 0;
1553 break;
1555 case PING_INFO_RECV_TTL:
1556 ret = ENOMEM;
1557 *buffer_len = sizeof (int);
1558 if (orig_buffer_len < sizeof (int))
1559 break;
1560 *((int *) buffer) = iter->recv_ttl;
1561 ret = 0;
1562 break;
1563 }
1565 return (ret);
1566 } /* ping_iterator_get_info */
1568 void *ping_iterator_get_context (pingobj_iter_t *iter)
1569 {
1570 return (iter->context);
1571 }
1573 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1574 {
1575 iter->context = context;
1576 }