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%04"PRIx16", "
311 "calculated 0x%04"PRIx16"\n",
312 recv_checksum, calc_checksum);
313 return (NULL);
314 }
316 ident = ntohs (icmp_hdr->icmp_id);
317 seq = ntohs (icmp_hdr->icmp_seq);
319 /* We have to iterate over all hosts, since ICMPv4 packets may
320 * be received on any raw v4 socket. */
321 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
322 {
323 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
324 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
326 if (ptr->addrfamily != AF_INET)
327 continue;
329 if (!timerisset (ptr->timer))
330 continue;
332 if (ptr->ident != ident)
333 continue;
335 if (((ptr->sequence - 1) & 0xFFFF) != seq)
336 continue;
338 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
339 "seq = %"PRIu16"\n",
340 ptr->hostname, ident, seq);
342 break;
343 }
345 if (ptr == NULL)
346 {
347 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
348 ident, seq);
349 }
351 if (ptr != NULL)
352 ptr->recv_ttl = ip_hdr->ip_ttl;
354 return (ptr);
355 }
357 #ifndef ICMP6_ECHO_REQUEST
358 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
359 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
360 # else
361 # define ICMP6_ECHO_REQUEST 128
362 # endif
363 #endif
365 #ifndef ICMP6_ECHO_REPLY
366 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
367 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
368 # else
369 # define ICMP6_ECHO_REPLY 129
370 # endif
371 #endif
373 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
374 size_t buffer_len)
375 {
376 struct icmp6_hdr *icmp_hdr;
378 uint16_t ident;
379 uint16_t seq;
381 pinghost_t *ptr;
383 if (buffer_len < sizeof (struct icmp6_hdr))
384 return (NULL);
386 icmp_hdr = (struct icmp6_hdr *) buffer;
387 buffer += sizeof (struct icmp);
388 buffer_len -= sizeof (struct icmp);
390 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
391 {
392 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
393 return (NULL);
394 }
396 if (icmp_hdr->icmp6_code != 0)
397 {
398 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
399 return (NULL);
400 }
402 ident = ntohs (icmp_hdr->icmp6_id);
403 seq = ntohs (icmp_hdr->icmp6_seq);
405 /* We have to iterate over all hosts, since ICMPv6 packets may
406 * be received on any raw v6 socket. */
407 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
408 {
409 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
410 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
412 if (ptr->addrfamily != AF_INET6)
413 continue;
415 if (!timerisset (ptr->timer))
416 continue;
418 if (ptr->ident != ident)
419 continue;
421 if (((ptr->sequence - 1) & 0xFFFF) != seq)
422 continue;
424 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
425 "seq = %"PRIu16"\n",
426 ptr->hostname, ident, seq);
428 break;
429 }
431 if (ptr == NULL)
432 {
433 dprintf ("No match found for ident = 0x%04"PRIx16", "
434 "seq = %"PRIu16"\n",
435 ident, seq);
436 }
438 return (ptr);
439 }
441 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
442 struct timeval *now)
443 {
444 /* Note: 'ph' is not necessarily the host object for which we receive a
445 * reply. The right object will be returned by ping_receive_ipv*(). For
446 * now, we can only rely on ph->fd and ph->addrfamily. */
448 struct timeval diff;
449 pinghost_t *host = NULL;
450 int recv_ttl;
452 /*
453 * Set up the receive buffer..
454 */
455 struct msghdr msghdr;
456 struct cmsghdr *cmsg;
457 char payload_buffer[4096];
458 ssize_t payload_buffer_len;
459 char control_buffer[4096];
460 struct iovec payload_iovec;
462 memset (&payload_iovec, 0, sizeof (payload_iovec));
463 payload_iovec.iov_base = payload_buffer;
464 payload_iovec.iov_len = sizeof (payload_buffer);
466 memset (&msghdr, 0, sizeof (msghdr));
467 /* unspecified source address */
468 msghdr.msg_name = NULL;
469 msghdr.msg_namelen = 0;
470 /* output buffer vector, see readv(2) */
471 msghdr.msg_iov = &payload_iovec;
472 msghdr.msg_iovlen = 1;
473 /* output buffer for control messages */
474 msghdr.msg_control = control_buffer;
475 msghdr.msg_controllen = sizeof (control_buffer);
476 /* flags; this is an output only field.. */
477 msghdr.msg_flags = 0;
478 #ifdef MSG_XPG4_2
479 msghdr.msg_flags |= MSG_XPG4_2;
480 #endif
482 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
483 if (payload_buffer_len < 0)
484 {
485 #if WITH_DEBUG
486 char errbuf[PING_ERRMSG_LEN];
487 dprintf ("recvfrom: %s\n",
488 sstrerror (errno, errbuf, sizeof (errbuf)));
489 #endif
490 return (-1);
491 }
492 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
494 /* Iterate over all auxiliary data in msghdr */
495 recv_ttl = -1;
496 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
497 cmsg != NULL;
498 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
499 {
500 if (ph->addrfamily == AF_INET) /* {{{ */
501 {
502 if (cmsg->cmsg_level != IPPROTO_IP)
503 continue;
505 if (cmsg->cmsg_type == IP_TTL)
506 {
507 memcpy (&recv_ttl, CMSG_DATA (cmsg),
508 sizeof (recv_ttl));
509 dprintf ("TTLv4 = %i;\n", recv_ttl);
510 }
511 else
512 {
513 dprintf ("Not handling option %i.\n",
514 cmsg->cmsg_type);
515 }
516 } /* }}} */
517 else if (ph->addrfamily == AF_INET6) /* {{{ */
518 {
519 if (cmsg->cmsg_level != IPPROTO_IPV6)
520 continue;
522 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
523 {
524 memcpy (&recv_ttl, CMSG_DATA (cmsg),
525 sizeof (recv_ttl));
526 dprintf ("TTLv6 = %i;\n", recv_ttl);
527 }
528 else
529 {
530 dprintf ("Not handling option %i.\n",
531 cmsg->cmsg_type);
532 }
533 } /* }}} */
534 else
535 {
536 dprintf ("Don't know how to handle "
537 "unknown protocol %i.\n",
538 cmsg->cmsg_level);
539 }
540 } /* }}} for (cmsg) */
542 if (ph->addrfamily == AF_INET)
543 {
544 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
545 if (host == NULL)
546 return (-1);
547 }
548 else if (ph->addrfamily == AF_INET6)
549 {
550 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
551 if (host == NULL)
552 return (-1);
553 }
554 else
555 {
556 dprintf ("ping_receive_one: Unknown address family %i.\n",
557 ph->addrfamily);
558 return (-1);
559 }
561 dprintf ("rcvd: %12i.%06i\n",
562 (int) now->tv_sec,
563 (int) now->tv_usec);
564 dprintf ("sent: %12i.%06i\n",
565 (int) host->timer->tv_sec,
566 (int) host->timer->tv_usec);
568 if (ping_timeval_sub (now, host->timer, &diff) < 0)
569 {
570 timerclear (host->timer);
571 return (-1);
572 }
574 dprintf ("diff: %12i.%06i\n",
575 (int) diff.tv_sec,
576 (int) diff.tv_usec);
578 if (recv_ttl >= 0)
579 host->recv_ttl = recv_ttl;
581 host->latency = ((double) diff.tv_usec) / 1000.0;
582 host->latency += ((double) diff.tv_sec) * 1000.0;
584 timerclear (host->timer);
586 return (0);
587 }
589 static int ping_receive_all (pingobj_t *obj)
590 {
591 fd_set readfds;
592 int num_readfds;
593 int max_readfds;
595 pinghost_t *ph;
596 pinghost_t *ptr;
598 struct timeval endtime;
599 struct timeval nowtime;
600 struct timeval timeout;
601 int status;
603 int ret;
605 ph = obj->head;
606 ret = 0;
608 for (ptr = ph; ptr != NULL; ptr = ptr->next)
609 {
610 ptr->latency = -1.0;
611 ptr->recv_ttl = -1;
612 }
614 if (gettimeofday (&nowtime, NULL) == -1)
615 {
616 ping_set_errno (obj, errno);
617 return (-1);
618 }
620 /* Set up timeout */
621 timeout.tv_sec = (time_t) obj->timeout;
622 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
624 dprintf ("Set timeout to %i.%06i seconds\n",
625 (int) timeout.tv_sec,
626 (int) timeout.tv_usec);
628 ping_timeval_add (&nowtime, &timeout, &endtime);
630 while (1)
631 {
632 FD_ZERO (&readfds);
633 num_readfds = 0;
634 max_readfds = -1;
636 for (ptr = ph; ptr != NULL; ptr = ptr->next)
637 {
638 if (!timerisset (ptr->timer))
639 continue;
641 FD_SET (ptr->fd, &readfds);
642 num_readfds++;
644 if (max_readfds < ptr->fd)
645 max_readfds = ptr->fd;
646 }
648 if (num_readfds == 0)
649 break;
651 if (gettimeofday (&nowtime, NULL) == -1)
652 {
653 ping_set_errno (obj, errno);
654 return (-1);
655 }
657 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
658 break;
660 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
661 (int) timeout.tv_sec,
662 (int) timeout.tv_usec);
664 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
666 if (gettimeofday (&nowtime, NULL) == -1)
667 {
668 ping_set_errno (obj, errno);
669 return (-1);
670 }
672 if ((status == -1) && (errno == EINTR))
673 {
674 dprintf ("select was interrupted by signal..\n");
675 continue;
676 }
677 else if (status < 0)
678 {
679 #if WITH_DEBUG
680 char errbuf[PING_ERRMSG_LEN];
681 dprintf ("select: %s\n",
682 sstrerror (errno, errbuf, sizeof (errbuf)));
683 #endif
684 break;
685 }
686 else if (status == 0)
687 {
688 dprintf ("select timed out\n");
689 for (ptr = ph; ptr != NULL; ptr = ptr->next)
690 if (ptr->latency < 0.0)
691 ptr->dropped++;
692 break;
693 }
695 for (ptr = ph; ptr != NULL; ptr = ptr->next)
696 {
697 if (FD_ISSET (ptr->fd, &readfds))
698 if (ping_receive_one (obj, ptr, &nowtime) == 0)
699 ret++;
700 }
701 } /* while (1) */
703 return (ret);
704 }
706 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
707 * Sending functions: *
708 * *
709 * ping_send_all *
710 * +-> ping_send_one_ipv4 *
711 * `-> ping_send_one_ipv6 *
712 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
713 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
714 const void *buf, size_t buflen)
715 {
716 ssize_t ret;
718 if (gettimeofday (ph->timer, NULL) == -1)
719 {
720 timerclear (ph->timer);
721 return (-1);
722 }
724 ret = sendto (ph->fd, buf, buflen, 0,
725 (struct sockaddr *) ph->addr, ph->addrlen);
727 if (ret < 0)
728 {
729 #if defined(EHOSTUNREACH)
730 if (errno == EHOSTUNREACH)
731 return (0);
732 #endif
733 #if defined(ENETUNREACH)
734 if (errno == ENETUNREACH)
735 return (0);
736 #endif
737 ping_set_errno (obj, errno);
738 }
740 return (ret);
741 }
743 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
744 {
745 struct icmp *icmp4;
746 int status;
748 char buf[4096];
749 int buflen;
751 char *data;
752 int datalen;
754 dprintf ("ph->hostname = %s\n", ph->hostname);
756 memset (buf, '\0', sizeof (buf));
757 icmp4 = (struct icmp *) buf;
758 data = (char *) (icmp4 + 1);
760 icmp4->icmp_type = ICMP_ECHO;
761 icmp4->icmp_code = 0;
762 icmp4->icmp_cksum = 0;
763 icmp4->icmp_id = htons (ph->ident);
764 icmp4->icmp_seq = htons (ph->sequence);
766 buflen = 4096 - sizeof (struct icmp);
767 strncpy (data, ph->data, buflen);
768 datalen = strlen (data);
770 buflen = datalen + sizeof (struct icmp);
772 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
774 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
776 status = ping_sendto (obj, ph, buf, buflen);
777 if (status < 0)
778 {
779 perror ("ping_sendto");
780 return (-1);
781 }
783 dprintf ("sendto: status = %i\n", status);
785 return (0);
786 }
788 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
789 {
790 struct icmp6_hdr *icmp6;
791 int status;
793 char buf[4096];
794 int buflen;
796 char *data;
797 int datalen;
799 dprintf ("ph->hostname = %s\n", ph->hostname);
801 memset (buf, '\0', sizeof (buf));
802 icmp6 = (struct icmp6_hdr *) buf;
803 data = (char *) (icmp6 + 1);
805 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
806 icmp6->icmp6_code = 0;
807 /* The checksum will be calculated by the TCP/IP stack. */
808 /* FIXME */
809 icmp6->icmp6_cksum = 0;
810 icmp6->icmp6_id = htons (ph->ident);
811 icmp6->icmp6_seq = htons (ph->sequence);
813 buflen = 4096 - sizeof (struct icmp6_hdr);
814 strncpy (data, ph->data, buflen);
815 datalen = strlen (data);
817 buflen = datalen + sizeof (struct icmp6_hdr);
819 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
821 status = ping_sendto (obj, ph, buf, buflen);
822 if (status < 0)
823 {
824 perror ("ping_sendto");
825 return (-1);
826 }
828 dprintf ("sendto: status = %i\n", status);
830 return (0);
831 }
833 static int ping_send_all (pingobj_t *obj)
834 {
835 pinghost_t *ph;
836 pinghost_t *ptr;
838 int ret;
840 ret = 0;
841 ph = obj->head;
843 for (ptr = ph; ptr != NULL; ptr = ptr->next)
844 {
845 /* start timer.. The GNU `ping6' starts the timer before
846 * sending the packet, so I will do that too */
847 if (gettimeofday (ptr->timer, NULL) == -1)
848 {
849 #if WITH_DEBUG
850 char errbuf[PING_ERRMSG_LEN];
851 dprintf ("gettimeofday: %s\n",
852 sstrerror (errno, errbuf, sizeof (errbuf)));
853 #endif
854 timerclear (ptr->timer);
855 ret--;
856 continue;
857 }
858 else
859 {
860 dprintf ("timer set for hostname = %s\n", ptr->hostname);
861 }
863 if (ptr->addrfamily == AF_INET6)
864 {
865 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
866 if (ping_send_one_ipv6 (obj, ptr) != 0)
867 {
868 timerclear (ptr->timer);
869 ret--;
870 continue;
871 }
872 }
873 else if (ptr->addrfamily == AF_INET)
874 {
875 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
876 if (ping_send_one_ipv4 (obj, ptr) != 0)
877 {
878 timerclear (ptr->timer);
879 ret--;
880 continue;
881 }
882 }
883 else /* this should not happen */
884 {
885 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
886 timerclear (ptr->timer);
887 ret--;
888 continue;
889 }
891 ptr->sequence++;
892 }
894 return (ret);
895 }
897 /*
898 * Set the TTL of a socket protocol independently.
899 */
900 static int ping_set_ttl (pinghost_t *ph, int ttl)
901 {
902 int ret = -2;
904 if (ph->addrfamily == AF_INET)
905 {
906 dprintf ("Setting TTLv4 to %i\n", ttl);
907 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
908 &ttl, sizeof (ttl));
909 }
910 else if (ph->addrfamily == AF_INET6)
911 {
912 dprintf ("Setting TTLv6 to %i\n", ttl);
913 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
914 &ttl, sizeof (ttl));
915 }
917 return (ret);
918 }
920 static int ping_get_ident (void)
921 {
922 int fd;
923 static int did_seed = 0;
925 int retval;
927 if (did_seed == 0)
928 {
929 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
930 {
931 unsigned int seed;
933 if (read (fd, &seed, sizeof (seed)) != -1)
934 {
935 did_seed = 1;
936 dprintf ("Random seed: %#x\n", seed);
937 srandom (seed);
938 }
940 close (fd);
941 }
942 #if WITH_DEBUG
943 else
944 {
945 char errbuf[PING_ERRMSG_LEN];
946 dprintf ("open (/dev/urandom): %s\n",
947 sstrerror (errno, errbuf, sizeof (errbuf)));
948 }
949 #endif
950 }
952 retval = (int) random ();
954 dprintf ("Random number: %#x\n", retval);
956 return (retval);
957 }
959 static pinghost_t *ping_alloc (void)
960 {
961 pinghost_t *ph;
962 size_t ph_size;
964 ph_size = sizeof (pinghost_t)
965 + sizeof (struct sockaddr_storage)
966 + sizeof (struct timeval);
968 ph = (pinghost_t *) malloc (ph_size);
969 if (ph == NULL)
970 return (NULL);
972 memset (ph, '\0', ph_size);
974 ph->timer = (struct timeval *) (ph + 1);
975 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
977 ph->addrlen = sizeof (struct sockaddr_storage);
978 ph->fd = -1;
979 ph->latency = -1.0;
980 ph->dropped = 0;
981 ph->ident = ping_get_ident () & 0xFFFF;
983 return (ph);
984 }
986 static void ping_free (pinghost_t *ph)
987 {
988 if (ph->fd >= 0)
989 close (ph->fd);
991 if (ph->username != NULL)
992 free (ph->username);
994 if (ph->hostname != NULL)
995 free (ph->hostname);
997 if (ph->data != NULL)
998 free (ph->data);
1000 free (ph);
1001 }
1003 /*
1004 * public methods
1005 */
1006 const char *ping_get_error (pingobj_t *obj)
1007 {
1008 if (obj == NULL)
1009 return (NULL);
1010 return (obj->errmsg);
1011 }
1013 pingobj_t *ping_construct (void)
1014 {
1015 pingobj_t *obj;
1017 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1018 return (NULL);
1019 memset (obj, '\0', sizeof (pingobj_t));
1021 obj->timeout = PING_DEF_TIMEOUT;
1022 obj->ttl = PING_DEF_TTL;
1023 obj->addrfamily = PING_DEF_AF;
1024 obj->data = strdup (PING_DEF_DATA);
1026 return (obj);
1027 }
1029 void ping_destroy (pingobj_t *obj)
1030 {
1031 pinghost_t *current;
1032 pinghost_t *next;
1034 if (obj == NULL)
1035 return;
1037 current = obj->head;
1038 next = NULL;
1040 while (current != NULL)
1041 {
1042 next = current->next;
1043 ping_free (current);
1044 current = next;
1045 }
1047 if (obj->data != NULL)
1048 free (obj->data);
1050 if (obj->srcaddr != NULL)
1051 free (obj->srcaddr);
1053 free (obj);
1055 return;
1056 }
1058 int ping_setopt (pingobj_t *obj, int option, void *value)
1059 {
1060 int ret = 0;
1062 if ((obj == NULL) || (value == NULL))
1063 return (-1);
1065 switch (option)
1066 {
1067 case PING_OPT_TIMEOUT:
1068 obj->timeout = *((double *) value);
1069 if (obj->timeout < 0.0)
1070 {
1071 obj->timeout = PING_DEF_TIMEOUT;
1072 ret = -1;
1073 }
1074 break;
1076 case PING_OPT_TTL:
1077 obj->ttl = *((int *) value);
1078 if ((obj->ttl < 1) || (obj->ttl > 255))
1079 {
1080 obj->ttl = PING_DEF_TTL;
1081 ret = -1;
1082 }
1083 else
1084 {
1085 pinghost_t *ph;
1087 for (ph = obj->head; ph != NULL; ph = ph->next)
1088 ping_set_ttl (ph, obj->ttl);
1089 }
1090 break;
1092 case PING_OPT_AF:
1093 obj->addrfamily = *((int *) value);
1094 if ((obj->addrfamily != AF_UNSPEC)
1095 && (obj->addrfamily != AF_INET)
1096 && (obj->addrfamily != AF_INET6))
1097 {
1098 obj->addrfamily = PING_DEF_AF;
1099 ret = -1;
1100 }
1101 if (obj->srcaddr != NULL)
1102 {
1103 free (obj->srcaddr);
1104 obj->srcaddr = NULL;
1105 }
1106 break;
1108 case PING_OPT_DATA:
1109 if (obj->data != NULL)
1110 {
1111 free (obj->data);
1112 obj->data = NULL;
1113 }
1114 obj->data = strdup ((const char *) value);
1115 break;
1117 case PING_OPT_SOURCE:
1118 {
1119 char *hostname = (char *) value;
1120 struct addrinfo ai_hints;
1121 struct addrinfo *ai_list;
1122 int status;
1123 #if WITH_DEBUG
1124 if (obj->addrfamily != AF_UNSPEC)
1125 {
1126 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1127 }
1128 #endif
1129 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1130 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1131 #if defined(AI_ADDRCONFIG)
1132 ai_hints.ai_flags = AI_ADDRCONFIG;
1133 #endif
1134 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1135 if (status != 0)
1136 {
1137 #if defined(EAI_SYSTEM)
1138 char errbuf[PING_ERRMSG_LEN];
1139 #endif
1140 ping_set_error (obj, "getaddrinfo",
1141 #if defined(EAI_SYSTEM)
1142 (status == EAI_SYSTEM)
1143 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1144 #endif
1145 gai_strerror (status));
1146 ret = -1;
1147 break;
1148 }
1149 #if WITH_DEBUG
1150 if (ai_list->ai_next != NULL)
1151 {
1152 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1153 }
1154 #endif
1155 if (obj->srcaddr == NULL)
1156 {
1157 obj->srcaddrlen = 0;
1158 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1159 if (obj->srcaddr == NULL)
1160 {
1161 ping_set_errno (obj, errno);
1162 ret = -1;
1163 freeaddrinfo (ai_list);
1164 break;
1165 }
1166 }
1167 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1168 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1169 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1170 ai_list->ai_addrlen);
1171 obj->srcaddrlen = ai_list->ai_addrlen;
1172 obj->addrfamily = ai_list->ai_family;
1174 freeaddrinfo (ai_list);
1175 } /* case PING_OPT_SOURCE */
1176 break;
1178 default:
1179 ret = -2;
1180 } /* switch (option) */
1182 return (ret);
1183 } /* int ping_setopt */
1186 int ping_send (pingobj_t *obj)
1187 {
1188 int ret;
1190 if (obj == NULL)
1191 return (-1);
1193 if (ping_send_all (obj) < 0)
1194 return (-1);
1196 if ((ret = ping_receive_all (obj)) < 0)
1197 return (-2);
1199 return (ret);
1200 }
1202 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1203 {
1204 while (ph != NULL)
1205 {
1206 if (strcasecmp (ph->username, host) == 0)
1207 break;
1209 ph = ph->next;
1210 }
1212 return (ph);
1213 }
1215 int ping_host_add (pingobj_t *obj, const char *host)
1216 {
1217 pinghost_t *ph;
1219 struct addrinfo ai_hints;
1220 struct addrinfo *ai_list, *ai_ptr;
1221 int ai_return;
1223 if ((obj == NULL) || (host == NULL))
1224 return (-1);
1226 dprintf ("host = %s\n", host);
1228 if (ping_host_search (obj->head, host) != NULL)
1229 return (0);
1231 memset (&ai_hints, '\0', sizeof (ai_hints));
1232 ai_hints.ai_flags = 0;
1233 #ifdef AI_ADDRCONFIG
1234 ai_hints.ai_flags |= AI_ADDRCONFIG;
1235 #endif
1236 #ifdef AI_CANONNAME
1237 ai_hints.ai_flags |= AI_CANONNAME;
1238 #endif
1239 ai_hints.ai_family = obj->addrfamily;
1240 ai_hints.ai_socktype = SOCK_RAW;
1242 if ((ph = ping_alloc ()) == NULL)
1243 {
1244 dprintf ("Out of memory!\n");
1245 return (-1);
1246 }
1248 if ((ph->username = strdup (host)) == NULL)
1249 {
1250 dprintf ("Out of memory!\n");
1251 ping_set_errno (obj, errno);
1252 ping_free (ph);
1253 return (-1);
1254 }
1256 if ((ph->hostname = strdup (host)) == NULL)
1257 {
1258 dprintf ("Out of memory!\n");
1259 ping_set_errno (obj, errno);
1260 ping_free (ph);
1261 return (-1);
1262 }
1264 /* obj->data is not garuanteed to be != NULL */
1265 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1266 {
1267 dprintf ("Out of memory!\n");
1268 ping_set_errno (obj, errno);
1269 ping_free (ph);
1270 return (-1);
1271 }
1273 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1274 {
1275 #if defined(EAI_SYSTEM)
1276 char errbuf[PING_ERRMSG_LEN];
1277 #endif
1278 dprintf ("getaddrinfo failed\n");
1279 ping_set_error (obj, "getaddrinfo",
1280 #if defined(EAI_SYSTEM)
1281 (ai_return == EAI_SYSTEM)
1282 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1283 #endif
1284 gai_strerror (ai_return));
1285 ping_free (ph);
1286 return (-1);
1287 }
1289 if (ai_list == NULL)
1290 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1292 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1293 {
1294 ph->fd = -1;
1296 if (ai_ptr->ai_family == AF_INET)
1297 {
1298 ai_ptr->ai_socktype = SOCK_RAW;
1299 ai_ptr->ai_protocol = IPPROTO_ICMP;
1300 }
1301 else if (ai_ptr->ai_family == AF_INET6)
1302 {
1303 ai_ptr->ai_socktype = SOCK_RAW;
1304 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1305 }
1306 else
1307 {
1308 char errmsg[PING_ERRMSG_LEN];
1310 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1311 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1313 dprintf (errmsg);
1314 ping_set_error (obj, "getaddrinfo", errmsg);
1315 continue;
1316 }
1318 /* TODO: Move this to a static function `ping_open_socket' and
1319 * call it whenever the socket dies. */
1320 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1321 if (ph->fd == -1)
1322 {
1323 #if WITH_DEBUG
1324 char errbuf[PING_ERRMSG_LEN];
1325 dprintf ("socket: %s\n",
1326 sstrerror (errno, errbuf, sizeof (errbuf)));
1327 #endif
1328 ping_set_errno (obj, errno);
1329 continue;
1330 }
1332 if (obj->srcaddr != NULL)
1333 {
1334 assert (obj->srcaddrlen > 0);
1335 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1337 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1338 {
1339 #if WITH_DEBUG
1340 char errbuf[PING_ERRMSG_LEN];
1341 dprintf ("bind: %s\n",
1342 sstrerror (errno, errbuf, sizeof (errbuf)));
1343 #endif
1344 ping_set_errno (obj, errno);
1345 close (ph->fd);
1346 ph->fd = -1;
1347 continue;
1348 }
1349 }
1351 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1352 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1353 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1354 ph->addrlen = ai_ptr->ai_addrlen;
1355 ph->addrfamily = ai_ptr->ai_family;
1357 #ifdef AI_CANONNAME
1358 if ((ai_ptr->ai_canonname != NULL)
1359 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1360 {
1361 char *old_hostname;
1363 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1364 ph->hostname, ai_ptr->ai_canonname);
1366 old_hostname = ph->hostname;
1367 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1368 {
1369 /* strdup failed, falling back to old hostname */
1370 ph->hostname = old_hostname;
1371 }
1372 else if (old_hostname != NULL)
1373 {
1374 free (old_hostname);
1375 }
1376 }
1377 #endif /* AI_CANONNAME */
1379 if (ph->addrfamily == AF_INET)
1380 {
1381 int opt = 1;
1383 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1384 &opt, sizeof (opt));
1385 }
1386 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1387 else if (ph->addrfamily == AF_INET6)
1388 {
1389 int opt = 1;
1391 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1392 &opt, sizeof (opt));
1393 }
1394 #endif
1396 break;
1397 }
1399 freeaddrinfo (ai_list);
1401 if (ph->fd < 0)
1402 {
1403 ping_free (ph);
1404 return (-1);
1405 }
1407 /*
1408 * Adding in the front is much easier, but then the iterator will
1409 * return the host that was added last as first host. That's just not
1410 * nice. -octo
1411 */
1412 if (obj->head == NULL)
1413 {
1414 obj->head = ph;
1415 }
1416 else
1417 {
1418 pinghost_t *hptr;
1420 hptr = obj->head;
1421 while (hptr->next != NULL)
1422 hptr = hptr->next;
1424 assert ((hptr != NULL) && (hptr->next == NULL));
1425 hptr->next = ph;
1426 }
1428 ping_set_ttl (ph, obj->ttl);
1430 return (0);
1431 } /* int ping_host_add */
1433 int ping_host_remove (pingobj_t *obj, const char *host)
1434 {
1435 pinghost_t *pre, *cur;
1437 if ((obj == NULL) || (host == NULL))
1438 return (-1);
1440 pre = NULL;
1441 cur = obj->head;
1443 while (cur != NULL)
1444 {
1445 if (strcasecmp (host, cur->username) == 0)
1446 break;
1448 pre = cur;
1449 cur = cur->next;
1450 }
1452 if (cur == NULL)
1453 {
1454 ping_set_error (obj, "ping_host_remove", "Host not found");
1455 return (-1);
1456 }
1458 if (pre == NULL)
1459 obj->head = cur->next;
1460 else
1461 pre->next = cur->next;
1463 ping_free (cur);
1465 return (0);
1466 }
1468 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1469 {
1470 if (obj == NULL)
1471 return (NULL);
1472 return ((pingobj_iter_t *) obj->head);
1473 }
1475 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1476 {
1477 if (iter == NULL)
1478 return (NULL);
1479 return ((pingobj_iter_t *) iter->next);
1480 }
1482 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1483 void *buffer, size_t *buffer_len)
1484 {
1485 int ret = EINVAL;
1487 size_t orig_buffer_len = *buffer_len;
1489 if ((iter == NULL) || (buffer == NULL) || (buffer_len == NULL))
1490 return (-1);
1492 switch (info)
1493 {
1494 case PING_INFO_USERNAME:
1495 ret = ENOMEM;
1496 *buffer_len = strlen (iter->username) + 1;
1497 if (orig_buffer_len <= *buffer_len)
1498 break;
1499 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1500 * will copy `*buffer_len' and pad the rest of
1501 * `buffer' with null-bytes */
1502 strncpy (buffer, iter->username, orig_buffer_len);
1503 ret = 0;
1504 break;
1506 case PING_INFO_HOSTNAME:
1507 ret = ENOMEM;
1508 *buffer_len = strlen (iter->hostname) + 1;
1509 if (orig_buffer_len < *buffer_len)
1510 break;
1511 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1512 * will copy `*buffer_len' and pad the rest of
1513 * `buffer' with null-bytes */
1514 strncpy (buffer, iter->hostname, orig_buffer_len);
1515 ret = 0;
1516 break;
1518 case PING_INFO_ADDRESS:
1519 ret = getnameinfo ((struct sockaddr *) iter->addr,
1520 iter->addrlen,
1521 (char *) buffer,
1522 *buffer_len,
1523 NULL, 0,
1524 NI_NUMERICHOST);
1525 if (ret != 0)
1526 {
1527 if ((ret == EAI_MEMORY)
1528 #ifdef EAI_OVERFLOW
1529 || (ret == EAI_OVERFLOW)
1530 #endif
1531 )
1532 ret = ENOMEM;
1533 #if defined(EAI_SYSTEM)
1534 else if (ret == EAI_SYSTEM)
1535 ret = errno;
1536 #endif
1537 else
1538 ret = EINVAL;
1539 }
1540 break;
1542 case PING_INFO_FAMILY:
1543 ret = ENOMEM;
1544 *buffer_len = sizeof (int);
1545 if (orig_buffer_len < sizeof (int))
1546 break;
1547 *((int *) buffer) = iter->addrfamily;
1548 ret = 0;
1549 break;
1551 case PING_INFO_LATENCY:
1552 ret = ENOMEM;
1553 *buffer_len = sizeof (double);
1554 if (orig_buffer_len < sizeof (double))
1555 break;
1556 *((double *) buffer) = iter->latency;
1557 ret = 0;
1558 break;
1560 case PING_INFO_DROPPED:
1561 ret = ENOMEM;
1562 *buffer_len = sizeof (uint32_t);
1563 if (orig_buffer_len < sizeof (uint32_t))
1564 break;
1565 *((uint32_t *) buffer) = iter->dropped;
1566 ret = 0;
1567 break;
1569 case PING_INFO_SEQUENCE:
1570 ret = ENOMEM;
1571 *buffer_len = sizeof (unsigned int);
1572 if (orig_buffer_len < sizeof (unsigned int))
1573 break;
1574 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1575 ret = 0;
1576 break;
1578 case PING_INFO_IDENT:
1579 ret = ENOMEM;
1580 *buffer_len = sizeof (uint16_t);
1581 if (orig_buffer_len < sizeof (uint16_t))
1582 break;
1583 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1584 ret = 0;
1585 break;
1587 case PING_INFO_DATA:
1588 ret = ENOMEM;
1589 *buffer_len = strlen (iter->data);
1590 if (orig_buffer_len < *buffer_len)
1591 break;
1592 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1593 ret = 0;
1594 break;
1596 case PING_INFO_RECV_TTL:
1597 ret = ENOMEM;
1598 *buffer_len = sizeof (int);
1599 if (orig_buffer_len < sizeof (int))
1600 break;
1601 *((int *) buffer) = iter->recv_ttl;
1602 ret = 0;
1603 break;
1604 }
1606 return (ret);
1607 } /* ping_iterator_get_info */
1609 void *ping_iterator_get_context (pingobj_iter_t *iter)
1610 {
1611 if (iter == NULL)
1612 return (NULL);
1613 return (iter->context);
1614 }
1616 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1617 {
1618 if (iter == NULL)
1619 return;
1620 iter->context = context;
1621 }