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 (pingobj_t *obj, char *buffer,
263 size_t buffer_len)
264 {
265 struct ip *ip_hdr;
266 struct icmp *icmp_hdr;
268 size_t ip_hdr_len;
270 uint16_t recv_checksum;
271 uint16_t calc_checksum;
273 uint16_t ident;
274 uint16_t seq;
276 pinghost_t *ptr;
278 if (buffer_len < sizeof (struct ip))
279 return (NULL);
281 ip_hdr = (struct ip *) buffer;
282 ip_hdr_len = ip_hdr->ip_hl << 2;
284 if (buffer_len < ip_hdr_len)
285 return (NULL);
287 buffer += ip_hdr_len;
288 buffer_len -= ip_hdr_len;
290 if (buffer_len < sizeof (struct icmp))
291 return (NULL);
293 icmp_hdr = (struct icmp *) buffer;
294 buffer += sizeof (struct icmp);
295 buffer_len -= sizeof (struct icmp);
297 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
298 {
299 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
300 return (NULL);
301 }
303 recv_checksum = icmp_hdr->icmp_cksum;
304 icmp_hdr->icmp_cksum = 0;
305 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
306 sizeof (struct icmp) + buffer_len);
308 if (recv_checksum != calc_checksum)
309 {
310 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
311 recv_checksum, calc_checksum);
312 return (NULL);
313 }
315 ident = ntohs (icmp_hdr->icmp_id);
316 seq = ntohs (icmp_hdr->icmp_seq);
318 /* We have to iterate over all hosts, since ICMPv4 packets may
319 * be received on any raw v4 socket. */
320 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
321 {
322 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
323 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
325 if (ptr->addrfamily != AF_INET)
326 continue;
328 if (!timerisset (ptr->timer))
329 continue;
331 if (ptr->ident != ident)
332 continue;
334 if (((ptr->sequence - 1) & 0xFFFF) != seq)
335 continue;
337 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
338 ptr->hostname, ident, seq);
340 break;
341 }
343 if (ptr == NULL)
344 {
345 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
346 ident, seq);
347 }
349 if (ptr != NULL)
350 ptr->recv_ttl = ip_hdr->ip_ttl;
352 return (ptr);
353 }
355 #ifndef ICMP6_ECHO_REQUEST
356 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
357 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
358 # else
359 # define ICMP6_ECHO_REQUEST 128
360 # endif
361 #endif
363 #ifndef ICMP6_ECHO_REPLY
364 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
365 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
366 # else
367 # define ICMP6_ECHO_REPLY 129
368 # endif
369 #endif
371 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
372 size_t buffer_len)
373 {
374 struct icmp6_hdr *icmp_hdr;
376 uint16_t ident;
377 uint16_t seq;
379 pinghost_t *ptr;
381 if (buffer_len < sizeof (struct icmp6_hdr))
382 return (NULL);
384 icmp_hdr = (struct icmp6_hdr *) buffer;
385 buffer += sizeof (struct icmp);
386 buffer_len -= sizeof (struct icmp);
388 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
389 {
390 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
391 return (NULL);
392 }
394 if (icmp_hdr->icmp6_code != 0)
395 {
396 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
397 return (NULL);
398 }
400 ident = ntohs (icmp_hdr->icmp6_id);
401 seq = ntohs (icmp_hdr->icmp6_seq);
403 /* We have to iterate over all hosts, since ICMPv6 packets may
404 * be received on any raw v6 socket. */
405 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
406 {
407 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
408 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
410 if (ptr->addrfamily != AF_INET6)
411 continue;
413 if (!timerisset (ptr->timer))
414 continue;
416 if (ptr->ident != ident)
417 continue;
419 if (((ptr->sequence - 1) & 0xFFFF) != seq)
420 continue;
422 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
423 ptr->hostname, ident, seq);
425 break;
426 }
428 if (ptr == NULL)
429 {
430 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
431 ident, seq);
432 }
434 return (ptr);
435 }
437 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
438 struct timeval *now)
439 {
440 /* Note: 'ph' is not necessarily the host object for which we receive a
441 * reply. The right object will be returned by ping_receive_ipv*(). For
442 * now, we can only rely on ph->fd and ph->addrfamily. */
444 struct timeval diff;
445 pinghost_t *host = NULL;
446 int recv_ttl;
448 /*
449 * Set up the receive buffer..
450 */
451 struct msghdr msghdr;
452 struct cmsghdr *cmsg;
453 char payload_buffer[4096];
454 ssize_t payload_buffer_len;
455 char control_buffer[4096];
456 struct iovec payload_iovec;
458 memset (&payload_iovec, 0, sizeof (payload_iovec));
459 payload_iovec.iov_base = payload_buffer;
460 payload_iovec.iov_len = sizeof (payload_buffer);
462 memset (&msghdr, 0, sizeof (msghdr));
463 /* unspecified source address */
464 msghdr.msg_name = NULL;
465 msghdr.msg_namelen = 0;
466 /* output buffer vector, see readv(2) */
467 msghdr.msg_iov = &payload_iovec;
468 msghdr.msg_iovlen = 1;
469 /* output buffer for control messages */
470 msghdr.msg_control = control_buffer;
471 msghdr.msg_controllen = sizeof (control_buffer);
472 /* flags; this is an output only field.. */
473 msghdr.msg_flags = 0;
474 #ifdef MSG_XPG4_2
475 msghdr.msg_flags |= MSG_XPG4_2;
476 #endif
478 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
479 if (payload_buffer_len < 0)
480 {
481 #if WITH_DEBUG
482 char errbuf[PING_ERRMSG_LEN];
483 dprintf ("recvfrom: %s\n",
484 sstrerror (errno, errbuf, sizeof (errbuf)));
485 #endif
486 return (-1);
487 }
488 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
490 /* Iterate over all auxiliary data in msghdr */
491 recv_ttl = -1;
492 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
493 cmsg != NULL;
494 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
495 {
496 if (ph->addrfamily == AF_INET) /* {{{ */
497 {
498 if (cmsg->cmsg_level != IPPROTO_IP)
499 continue;
501 if (cmsg->cmsg_type == IP_TTL)
502 {
503 memcpy (&recv_ttl, CMSG_DATA (cmsg),
504 sizeof (recv_ttl));
505 dprintf ("TTLv4 = %i;\n", recv_ttl);
506 }
507 else
508 {
509 dprintf ("Not handling option %i.\n",
510 cmsg->cmsg_type);
511 }
512 } /* }}} */
513 else if (ph->addrfamily == AF_INET6) /* {{{ */
514 {
515 if (cmsg->cmsg_level != IPPROTO_IPV6)
516 continue;
518 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
519 {
520 memcpy (&recv_ttl, CMSG_DATA (cmsg),
521 sizeof (recv_ttl));
522 dprintf ("TTLv6 = %i;\n", recv_ttl);
523 }
524 else
525 {
526 dprintf ("Not handling option %i.\n",
527 cmsg->cmsg_type);
528 }
529 } /* }}} */
530 else
531 {
532 dprintf ("Don't know how to handle "
533 "unknown protocol %i.\n",
534 cmsg->cmsg_level);
535 }
536 } /* }}} for (cmsg) */
538 if (ph->addrfamily == AF_INET)
539 {
540 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
541 if (host == NULL)
542 return (-1);
543 }
544 else if (ph->addrfamily == AF_INET6)
545 {
546 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
547 if (host == NULL)
548 return (-1);
549 }
550 else
551 {
552 dprintf ("ping_receive_one: Unknown address family %i.\n",
553 ph->addrfamily);
554 return (-1);
555 }
557 dprintf ("rcvd: %12i.%06i\n",
558 (int) now->tv_sec,
559 (int) now->tv_usec);
560 dprintf ("sent: %12i.%06i\n",
561 (int) host->timer->tv_sec,
562 (int) host->timer->tv_usec);
564 if (ping_timeval_sub (now, host->timer, &diff) < 0)
565 {
566 timerclear (host->timer);
567 return (-1);
568 }
570 dprintf ("diff: %12i.%06i\n",
571 (int) diff.tv_sec,
572 (int) diff.tv_usec);
574 if (recv_ttl >= 0)
575 host->recv_ttl = recv_ttl;
577 host->latency = ((double) diff.tv_usec) / 1000.0;
578 host->latency += ((double) diff.tv_sec) * 1000.0;
580 timerclear (host->timer);
582 return (0);
583 }
585 static int ping_receive_all (pingobj_t *obj)
586 {
587 fd_set readfds;
588 int num_readfds;
589 int max_readfds;
591 pinghost_t *ph;
592 pinghost_t *ptr;
594 struct timeval endtime;
595 struct timeval nowtime;
596 struct timeval timeout;
597 int status;
599 int ret;
601 ph = obj->head;
602 ret = 0;
604 for (ptr = ph; ptr != NULL; ptr = ptr->next)
605 {
606 ptr->latency = -1.0;
607 ptr->recv_ttl = -1;
608 }
610 if (gettimeofday (&nowtime, NULL) == -1)
611 {
612 ping_set_errno (obj, errno);
613 return (-1);
614 }
616 /* Set up timeout */
617 timeout.tv_sec = (time_t) obj->timeout;
618 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
620 dprintf ("Set timeout to %i.%06i seconds\n",
621 (int) timeout.tv_sec,
622 (int) timeout.tv_usec);
624 ping_timeval_add (&nowtime, &timeout, &endtime);
626 while (1)
627 {
628 FD_ZERO (&readfds);
629 num_readfds = 0;
630 max_readfds = -1;
632 for (ptr = ph; ptr != NULL; ptr = ptr->next)
633 {
634 if (!timerisset (ptr->timer))
635 continue;
637 FD_SET (ptr->fd, &readfds);
638 num_readfds++;
640 if (max_readfds < ptr->fd)
641 max_readfds = ptr->fd;
642 }
644 if (num_readfds == 0)
645 break;
647 if (gettimeofday (&nowtime, NULL) == -1)
648 {
649 ping_set_errno (obj, errno);
650 return (-1);
651 }
653 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
654 break;
656 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
657 (int) timeout.tv_sec,
658 (int) timeout.tv_usec);
660 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
662 if (gettimeofday (&nowtime, NULL) == -1)
663 {
664 ping_set_errno (obj, errno);
665 return (-1);
666 }
668 if ((status == -1) && (errno == EINTR))
669 {
670 dprintf ("select was interrupted by signal..\n");
671 continue;
672 }
673 else if (status < 0)
674 {
675 #if WITH_DEBUG
676 char errbuf[PING_ERRMSG_LEN];
677 dprintf ("select: %s\n",
678 sstrerror (errno, errbuf, sizeof (errbuf)));
679 #endif
680 break;
681 }
682 else if (status == 0)
683 {
684 dprintf ("select timed out\n");
685 for (ptr = ph; ptr != NULL; ptr = ptr->next)
686 if (ptr->latency < 0.0)
687 ptr->dropped++;
688 break;
689 }
691 for (ptr = ph; ptr != NULL; ptr = ptr->next)
692 {
693 if (FD_ISSET (ptr->fd, &readfds))
694 if (ping_receive_one (obj, ptr, &nowtime) == 0)
695 ret++;
696 }
697 } /* while (1) */
699 return (ret);
700 }
702 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
703 * Sending functions: *
704 * *
705 * ping_send_all *
706 * +-> ping_send_one_ipv4 *
707 * `-> ping_send_one_ipv6 *
708 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
709 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
710 const void *buf, size_t buflen)
711 {
712 ssize_t ret;
714 if (gettimeofday (ph->timer, NULL) == -1)
715 {
716 timerclear (ph->timer);
717 return (-1);
718 }
720 ret = sendto (ph->fd, buf, buflen, 0,
721 (struct sockaddr *) ph->addr, ph->addrlen);
723 if (ret < 0)
724 {
725 #if defined(EHOSTUNREACH)
726 if (errno == EHOSTUNREACH)
727 return (0);
728 #endif
729 #if defined(ENETUNREACH)
730 if (errno == ENETUNREACH)
731 return (0);
732 #endif
733 ping_set_errno (obj, errno);
734 }
736 return (ret);
737 }
739 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
740 {
741 struct icmp *icmp4;
742 int status;
744 char buf[4096];
745 int buflen;
747 char *data;
748 int datalen;
750 dprintf ("ph->hostname = %s\n", ph->hostname);
752 memset (buf, '\0', sizeof (buf));
753 icmp4 = (struct icmp *) buf;
754 data = (char *) (icmp4 + 1);
756 icmp4->icmp_type = ICMP_ECHO;
757 icmp4->icmp_code = 0;
758 icmp4->icmp_cksum = 0;
759 icmp4->icmp_id = htons (ph->ident);
760 icmp4->icmp_seq = htons (ph->sequence);
762 buflen = 4096 - sizeof (struct icmp);
763 strncpy (data, ph->data, buflen);
764 datalen = strlen (data);
766 buflen = datalen + sizeof (struct icmp);
768 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
770 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
772 status = ping_sendto (obj, ph, buf, buflen);
773 if (status < 0)
774 {
775 perror ("ping_sendto");
776 return (-1);
777 }
779 dprintf ("sendto: status = %i\n", status);
781 return (0);
782 }
784 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
785 {
786 struct icmp6_hdr *icmp6;
787 int status;
789 char buf[4096];
790 int buflen;
792 char *data;
793 int datalen;
795 dprintf ("ph->hostname = %s\n", ph->hostname);
797 memset (buf, '\0', sizeof (buf));
798 icmp6 = (struct icmp6_hdr *) buf;
799 data = (char *) (icmp6 + 1);
801 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
802 icmp6->icmp6_code = 0;
803 /* The checksum will be calculated by the TCP/IP stack. */
804 /* FIXME */
805 icmp6->icmp6_cksum = 0;
806 icmp6->icmp6_id = htons (ph->ident);
807 icmp6->icmp6_seq = htons (ph->sequence);
809 buflen = 4096 - sizeof (struct icmp6_hdr);
810 strncpy (data, ph->data, buflen);
811 datalen = strlen (data);
813 buflen = datalen + sizeof (struct icmp6_hdr);
815 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
817 status = ping_sendto (obj, ph, buf, buflen);
818 if (status < 0)
819 {
820 perror ("ping_sendto");
821 return (-1);
822 }
824 dprintf ("sendto: status = %i\n", status);
826 return (0);
827 }
829 static int ping_send_all (pingobj_t *obj)
830 {
831 pinghost_t *ph;
832 pinghost_t *ptr;
834 int ret;
836 ret = 0;
837 ph = obj->head;
839 for (ptr = ph; ptr != NULL; ptr = ptr->next)
840 {
841 /* start timer.. The GNU `ping6' starts the timer before
842 * sending the packet, so I will do that too */
843 if (gettimeofday (ptr->timer, NULL) == -1)
844 {
845 #if WITH_DEBUG
846 char errbuf[PING_ERRMSG_LEN];
847 dprintf ("gettimeofday: %s\n",
848 sstrerror (errno, errbuf, sizeof (errbuf)));
849 #endif
850 timerclear (ptr->timer);
851 ret--;
852 continue;
853 }
854 else
855 {
856 dprintf ("timer set for hostname = %s\n", ptr->hostname);
857 }
859 if (ptr->addrfamily == AF_INET6)
860 {
861 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
862 if (ping_send_one_ipv6 (obj, ptr) != 0)
863 {
864 timerclear (ptr->timer);
865 ret--;
866 continue;
867 }
868 }
869 else if (ptr->addrfamily == AF_INET)
870 {
871 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
872 if (ping_send_one_ipv4 (obj, ptr) != 0)
873 {
874 timerclear (ptr->timer);
875 ret--;
876 continue;
877 }
878 }
879 else /* this should not happen */
880 {
881 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
882 timerclear (ptr->timer);
883 ret--;
884 continue;
885 }
887 ptr->sequence++;
888 }
890 return (ret);
891 }
893 /*
894 * Set the TTL of a socket protocol independently.
895 */
896 static int ping_set_ttl (pinghost_t *ph, int ttl)
897 {
898 int ret = -2;
900 if (ph->addrfamily == AF_INET)
901 {
902 dprintf ("Setting TTLv4 to %i\n", ttl);
903 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
904 &ttl, sizeof (ttl));
905 }
906 else if (ph->addrfamily == AF_INET6)
907 {
908 dprintf ("Setting TTLv6 to %i\n", ttl);
909 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
910 &ttl, sizeof (ttl));
911 }
913 return (ret);
914 }
916 static int ping_get_ident (void)
917 {
918 int fd;
919 static int did_seed = 0;
921 int retval;
923 if (did_seed == 0)
924 {
925 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
926 {
927 unsigned int seed;
929 if (read (fd, &seed, sizeof (seed)) != -1)
930 {
931 did_seed = 1;
932 dprintf ("Random seed: %#x\n", seed);
933 srandom (seed);
934 }
936 close (fd);
937 }
938 #if WITH_DEBUG
939 else
940 {
941 char errbuf[PING_ERRMSG_LEN];
942 dprintf ("open (/dev/urandom): %s\n",
943 sstrerror (errno, errbuf, sizeof (errbuf)));
944 }
945 #endif
946 }
948 retval = (int) random ();
950 dprintf ("Random number: %#x\n", retval);
952 return (retval);
953 }
955 static pinghost_t *ping_alloc (void)
956 {
957 pinghost_t *ph;
958 size_t ph_size;
960 ph_size = sizeof (pinghost_t)
961 + sizeof (struct sockaddr_storage)
962 + sizeof (struct timeval);
964 ph = (pinghost_t *) malloc (ph_size);
965 if (ph == NULL)
966 return (NULL);
968 memset (ph, '\0', ph_size);
970 ph->timer = (struct timeval *) (ph + 1);
971 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
973 ph->addrlen = sizeof (struct sockaddr_storage);
974 ph->fd = -1;
975 ph->latency = -1.0;
976 ph->dropped = 0;
977 ph->ident = ping_get_ident () & 0xFFFF;
979 return (ph);
980 }
982 static void ping_free (pinghost_t *ph)
983 {
984 if (ph->fd >= 0)
985 close (ph->fd);
987 if (ph->username != NULL)
988 free (ph->username);
990 if (ph->hostname != NULL)
991 free (ph->hostname);
993 if (ph->data != NULL)
994 free (ph->data);
996 free (ph);
997 }
999 /*
1000 * public methods
1001 */
1002 const char *ping_get_error (pingobj_t *obj)
1003 {
1004 return (obj->errmsg);
1005 }
1007 pingobj_t *ping_construct (void)
1008 {
1009 pingobj_t *obj;
1011 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1012 return (NULL);
1013 memset (obj, '\0', sizeof (pingobj_t));
1015 obj->timeout = PING_DEF_TIMEOUT;
1016 obj->ttl = PING_DEF_TTL;
1017 obj->addrfamily = PING_DEF_AF;
1018 obj->data = strdup (PING_DEF_DATA);
1020 return (obj);
1021 }
1023 void ping_destroy (pingobj_t *obj)
1024 {
1025 pinghost_t *current;
1026 pinghost_t *next;
1028 current = obj->head;
1029 next = NULL;
1031 while (current != NULL)
1032 {
1033 next = current->next;
1034 ping_free (current);
1035 current = next;
1036 }
1038 if (obj->data != NULL)
1039 free (obj->data);
1041 if (obj->srcaddr != NULL)
1042 free (obj->srcaddr);
1044 free (obj);
1046 return;
1047 }
1049 int ping_setopt (pingobj_t *obj, int option, void *value)
1050 {
1051 int ret = 0;
1053 switch (option)
1054 {
1055 case PING_OPT_TIMEOUT:
1056 obj->timeout = *((double *) value);
1057 if (obj->timeout < 0.0)
1058 {
1059 obj->timeout = PING_DEF_TIMEOUT;
1060 ret = -1;
1061 }
1062 break;
1064 case PING_OPT_TTL:
1065 obj->ttl = *((int *) value);
1066 if ((obj->ttl < 1) || (obj->ttl > 255))
1067 {
1068 obj->ttl = PING_DEF_TTL;
1069 ret = -1;
1070 }
1071 else
1072 {
1073 pinghost_t *ph;
1075 for (ph = obj->head; ph != NULL; ph = ph->next)
1076 ping_set_ttl (ph, obj->ttl);
1077 }
1078 break;
1080 case PING_OPT_AF:
1081 obj->addrfamily = *((int *) value);
1082 if ((obj->addrfamily != AF_UNSPEC)
1083 && (obj->addrfamily != AF_INET)
1084 && (obj->addrfamily != AF_INET6))
1085 {
1086 obj->addrfamily = PING_DEF_AF;
1087 ret = -1;
1088 }
1089 if (obj->srcaddr != NULL)
1090 {
1091 free (obj->srcaddr);
1092 obj->srcaddr = NULL;
1093 }
1094 break;
1096 case PING_OPT_DATA:
1097 if (obj->data != NULL)
1098 {
1099 free (obj->data);
1100 obj->data = NULL;
1101 }
1102 obj->data = strdup ((const char *) value);
1103 break;
1105 case PING_OPT_SOURCE:
1106 {
1107 char *hostname = (char *) value;
1108 struct addrinfo ai_hints;
1109 struct addrinfo *ai_list;
1110 int status;
1111 #if WITH_DEBUG
1112 if (obj->addrfamily != AF_UNSPEC)
1113 {
1114 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1115 }
1116 #endif
1117 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1118 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1119 #if defined(AI_ADDRCONFIG)
1120 ai_hints.ai_flags = AI_ADDRCONFIG;
1121 #endif
1122 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1123 if (status != 0)
1124 {
1125 #if defined(EAI_SYSTEM)
1126 char errbuf[PING_ERRMSG_LEN];
1127 #endif
1128 ping_set_error (obj, "getaddrinfo",
1129 #if defined(EAI_SYSTEM)
1130 (status == EAI_SYSTEM)
1131 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1132 #endif
1133 gai_strerror (status));
1134 ret = -1;
1135 break;
1136 }
1137 #if WITH_DEBUG
1138 if (ai_list->ai_next != NULL)
1139 {
1140 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1141 }
1142 #endif
1143 if (obj->srcaddr == NULL)
1144 {
1145 obj->srcaddrlen = 0;
1146 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1147 if (obj->srcaddr == NULL)
1148 {
1149 ping_set_errno (obj, errno);
1150 ret = -1;
1151 freeaddrinfo (ai_list);
1152 break;
1153 }
1154 }
1155 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1156 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1157 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1158 ai_list->ai_addrlen);
1159 obj->srcaddrlen = ai_list->ai_addrlen;
1160 obj->addrfamily = ai_list->ai_family;
1162 freeaddrinfo (ai_list);
1163 } /* case PING_OPT_SOURCE */
1164 break;
1166 default:
1167 ret = -2;
1168 } /* switch (option) */
1170 return (ret);
1171 } /* int ping_setopt */
1174 int ping_send (pingobj_t *obj)
1175 {
1176 int ret;
1178 if (ping_send_all (obj) < 0)
1179 return (-1);
1181 if ((ret = ping_receive_all (obj)) < 0)
1182 return (-2);
1184 return (ret);
1185 }
1187 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1188 {
1189 while (ph != NULL)
1190 {
1191 if (strcasecmp (ph->username, host) == 0)
1192 break;
1194 ph = ph->next;
1195 }
1197 return (ph);
1198 }
1200 int ping_host_add (pingobj_t *obj, const char *host)
1201 {
1202 pinghost_t *ph;
1204 struct addrinfo ai_hints;
1205 struct addrinfo *ai_list, *ai_ptr;
1206 int ai_return;
1208 dprintf ("host = %s\n", host);
1210 if (ping_host_search (obj->head, host) != NULL)
1211 return (0);
1213 memset (&ai_hints, '\0', sizeof (ai_hints));
1214 ai_hints.ai_flags = 0;
1215 #ifdef AI_ADDRCONFIG
1216 ai_hints.ai_flags |= AI_ADDRCONFIG;
1217 #endif
1218 #ifdef AI_CANONNAME
1219 ai_hints.ai_flags |= AI_CANONNAME;
1220 #endif
1221 ai_hints.ai_family = obj->addrfamily;
1222 ai_hints.ai_socktype = SOCK_RAW;
1224 if ((ph = ping_alloc ()) == NULL)
1225 {
1226 dprintf ("Out of memory!\n");
1227 return (-1);
1228 }
1230 if ((ph->username = strdup (host)) == NULL)
1231 {
1232 dprintf ("Out of memory!\n");
1233 ping_set_errno (obj, errno);
1234 ping_free (ph);
1235 return (-1);
1236 }
1238 if ((ph->hostname = strdup (host)) == NULL)
1239 {
1240 dprintf ("Out of memory!\n");
1241 ping_set_errno (obj, errno);
1242 ping_free (ph);
1243 return (-1);
1244 }
1246 /* obj->data is not garuanteed to be != NULL */
1247 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1248 {
1249 dprintf ("Out of memory!\n");
1250 ping_set_errno (obj, errno);
1251 ping_free (ph);
1252 return (-1);
1253 }
1255 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1256 {
1257 #if defined(EAI_SYSTEM)
1258 char errbuf[PING_ERRMSG_LEN];
1259 #endif
1260 dprintf ("getaddrinfo failed\n");
1261 ping_set_error (obj, "getaddrinfo",
1262 #if defined(EAI_SYSTEM)
1263 (ai_return == EAI_SYSTEM)
1264 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1265 #endif
1266 gai_strerror (ai_return));
1267 ping_free (ph);
1268 return (-1);
1269 }
1271 if (ai_list == NULL)
1272 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1274 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1275 {
1276 ph->fd = -1;
1278 if (ai_ptr->ai_family == AF_INET)
1279 {
1280 ai_ptr->ai_socktype = SOCK_RAW;
1281 ai_ptr->ai_protocol = IPPROTO_ICMP;
1282 }
1283 else if (ai_ptr->ai_family == AF_INET6)
1284 {
1285 ai_ptr->ai_socktype = SOCK_RAW;
1286 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1287 }
1288 else
1289 {
1290 char errmsg[PING_ERRMSG_LEN];
1292 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1293 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1295 dprintf (errmsg);
1296 ping_set_error (obj, "getaddrinfo", errmsg);
1297 continue;
1298 }
1300 /* TODO: Move this to a static function `ping_open_socket' and
1301 * call it whenever the socket dies. */
1302 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1303 if (ph->fd == -1)
1304 {
1305 #if WITH_DEBUG
1306 char errbuf[PING_ERRMSG_LEN];
1307 dprintf ("socket: %s\n",
1308 sstrerror (errno, errbuf, sizeof (errbuf)));
1309 #endif
1310 ping_set_errno (obj, errno);
1311 continue;
1312 }
1314 if (obj->srcaddr != NULL)
1315 {
1316 assert (obj->srcaddrlen > 0);
1317 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1319 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1320 {
1321 #if WITH_DEBUG
1322 char errbuf[PING_ERRMSG_LEN];
1323 dprintf ("bind: %s\n",
1324 sstrerror (errno, errbuf, sizeof (errbuf)));
1325 #endif
1326 ping_set_errno (obj, errno);
1327 close (ph->fd);
1328 ph->fd = -1;
1329 continue;
1330 }
1331 }
1333 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1334 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1335 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1336 ph->addrlen = ai_ptr->ai_addrlen;
1337 ph->addrfamily = ai_ptr->ai_family;
1339 #ifdef AI_CANONNAME
1340 if ((ai_ptr->ai_canonname != NULL)
1341 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1342 {
1343 char *old_hostname;
1345 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1346 ph->hostname, ai_ptr->ai_canonname);
1348 old_hostname = ph->hostname;
1349 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1350 {
1351 /* strdup failed, falling back to old hostname */
1352 ph->hostname = old_hostname;
1353 }
1354 else if (old_hostname != NULL)
1355 {
1356 free (old_hostname);
1357 }
1358 }
1359 #endif /* AI_CANONNAME */
1361 if (ph->addrfamily == AF_INET)
1362 {
1363 int opt = 1;
1365 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1366 &opt, sizeof (opt));
1367 }
1368 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1369 else if (ph->addrfamily == AF_INET6)
1370 {
1371 int opt = 1;
1373 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1374 &opt, sizeof (opt));
1375 }
1376 #endif
1378 break;
1379 }
1381 freeaddrinfo (ai_list);
1383 if (ph->fd < 0)
1384 {
1385 ping_free (ph);
1386 return (-1);
1387 }
1389 /*
1390 * Adding in the front is much easier, but then the iterator will
1391 * return the host that was added last as first host. That's just not
1392 * nice. -octo
1393 */
1394 if (obj->head == NULL)
1395 {
1396 obj->head = ph;
1397 }
1398 else
1399 {
1400 pinghost_t *hptr;
1402 hptr = obj->head;
1403 while (hptr->next != NULL)
1404 hptr = hptr->next;
1406 assert ((hptr != NULL) && (hptr->next == NULL));
1407 hptr->next = ph;
1408 }
1410 ping_set_ttl (ph, obj->ttl);
1412 return (0);
1413 } /* int ping_host_add */
1415 int ping_host_remove (pingobj_t *obj, const char *host)
1416 {
1417 pinghost_t *pre, *cur;
1419 pre = NULL;
1420 cur = obj->head;
1422 while (cur != NULL)
1423 {
1424 if (strcasecmp (host, cur->username) == 0)
1425 break;
1427 pre = cur;
1428 cur = cur->next;
1429 }
1431 if (cur == NULL)
1432 {
1433 ping_set_error (obj, "ping_host_remove", "Host not found");
1434 return (-1);
1435 }
1437 if (pre == NULL)
1438 obj->head = cur->next;
1439 else
1440 pre->next = cur->next;
1442 ping_free (cur);
1444 return (0);
1445 }
1447 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1448 {
1449 return ((pingobj_iter_t *) obj->head);
1450 }
1452 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1453 {
1454 return ((pingobj_iter_t *) iter->next);
1455 }
1457 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1458 void *buffer, size_t *buffer_len)
1459 {
1460 int ret = EINVAL;
1462 size_t orig_buffer_len = *buffer_len;
1464 switch (info)
1465 {
1466 case PING_INFO_USERNAME:
1467 ret = ENOMEM;
1468 *buffer_len = strlen (iter->username) + 1;
1469 if (orig_buffer_len <= *buffer_len)
1470 break;
1471 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1472 * will copy `*buffer_len' and pad the rest of
1473 * `buffer' with null-bytes */
1474 strncpy (buffer, iter->username, orig_buffer_len);
1475 ret = 0;
1476 break;
1478 case PING_INFO_HOSTNAME:
1479 ret = ENOMEM;
1480 *buffer_len = strlen (iter->hostname) + 1;
1481 if (orig_buffer_len < *buffer_len)
1482 break;
1483 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1484 * will copy `*buffer_len' and pad the rest of
1485 * `buffer' with null-bytes */
1486 strncpy (buffer, iter->hostname, orig_buffer_len);
1487 ret = 0;
1488 break;
1490 case PING_INFO_ADDRESS:
1491 ret = getnameinfo ((struct sockaddr *) iter->addr,
1492 iter->addrlen,
1493 (char *) buffer,
1494 *buffer_len,
1495 NULL, 0,
1496 NI_NUMERICHOST);
1497 if (ret != 0)
1498 {
1499 if ((ret == EAI_MEMORY)
1500 #ifdef EAI_OVERFLOW
1501 || (ret == EAI_OVERFLOW)
1502 #endif
1503 )
1504 ret = ENOMEM;
1505 #if defined(EAI_SYSTEM)
1506 else if (ret == EAI_SYSTEM)
1507 ret = errno;
1508 #endif
1509 else
1510 ret = EINVAL;
1511 }
1512 break;
1514 case PING_INFO_FAMILY:
1515 ret = ENOMEM;
1516 *buffer_len = sizeof (int);
1517 if (orig_buffer_len < sizeof (int))
1518 break;
1519 *((int *) buffer) = iter->addrfamily;
1520 ret = 0;
1521 break;
1523 case PING_INFO_LATENCY:
1524 ret = ENOMEM;
1525 *buffer_len = sizeof (double);
1526 if (orig_buffer_len < sizeof (double))
1527 break;
1528 *((double *) buffer) = iter->latency;
1529 ret = 0;
1530 break;
1532 case PING_INFO_DROPPED:
1533 ret = ENOMEM;
1534 *buffer_len = sizeof (uint32_t);
1535 if (orig_buffer_len < sizeof (uint32_t))
1536 break;
1537 *((uint32_t *) buffer) = iter->dropped;
1538 ret = 0;
1539 break;
1541 case PING_INFO_SEQUENCE:
1542 ret = ENOMEM;
1543 *buffer_len = sizeof (unsigned int);
1544 if (orig_buffer_len < sizeof (unsigned int))
1545 break;
1546 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1547 ret = 0;
1548 break;
1550 case PING_INFO_IDENT:
1551 ret = ENOMEM;
1552 *buffer_len = sizeof (uint16_t);
1553 if (orig_buffer_len < sizeof (uint16_t))
1554 break;
1555 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1556 ret = 0;
1557 break;
1559 case PING_INFO_DATA:
1560 ret = ENOMEM;
1561 *buffer_len = strlen (iter->data);
1562 if (orig_buffer_len < *buffer_len)
1563 break;
1564 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1565 ret = 0;
1566 break;
1568 case PING_INFO_RECV_TTL:
1569 ret = ENOMEM;
1570 *buffer_len = sizeof (int);
1571 if (orig_buffer_len < sizeof (int))
1572 break;
1573 *((int *) buffer) = iter->recv_ttl;
1574 ret = 0;
1575 break;
1576 }
1578 return (ret);
1579 } /* ping_iterator_get_info */
1581 void *ping_iterator_get_context (pingobj_iter_t *iter)
1582 {
1583 return (iter->context);
1584 }
1586 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1587 {
1588 iter->context = context;
1589 }