579aba31daf41087195d9d523115ea1e662be3b9
1 /**
2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2009 Florian octo Forster <octo at verplant.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; only version 2 of the License is
8 * applicable.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #if STDC_HEADERS
25 # include <stdlib.h>
26 # include <stdio.h>
27 # include <string.h>
28 # include <inttypes.h>
29 # include <errno.h>
30 # include <assert.h>
31 #else
32 # error "You don't have the standard C99 header files installed"
33 #endif /* STDC_HEADERS */
35 #ifdef HAVE_STDINT_H
36 # include <stdint.h>
37 #endif
39 #if HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
43 #if HAVE_FCNTL_H
44 # include <fcntl.h>
45 #endif
46 #if HAVE_SYS_TYPES_H
47 # include <sys/types.h>
48 #endif
49 #if HAVE_SYS_STAT_H
50 # include <sys/stat.h>
51 #endif
53 #if TIME_WITH_SYS_TIME
54 # include <sys/time.h>
55 # include <time.h>
56 #else
57 # if HAVE_SYS_TIME_H
58 # include <sys/time.h>
59 # else
60 # include <time.h>
61 # endif
62 #endif
64 #if HAVE_SYS_SOCKET_H
65 # include <sys/socket.h>
66 #endif
68 #if HAVE_NETDB_H
69 # include <netdb.h>
70 #endif
72 #if HAVE_NETINET_IN_SYSTM_H
73 # include <netinet/in_systm.h>
74 #endif
75 #if HAVE_NETINET_IN_H
76 # include <netinet/in.h>
77 #endif
78 #if HAVE_NETINET_IP_H
79 # include <netinet/ip.h>
80 #endif
81 #if HAVE_NETINET_IP_ICMP_H
82 # include <netinet/ip_icmp.h>
83 #endif
84 #ifdef HAVE_NETINET_IP_VAR_H
85 # include <netinet/ip_var.h>
86 #endif
87 #if HAVE_NETINET_IP6_H
88 # include <netinet/ip6.h>
89 #endif
90 #if HAVE_NETINET_ICMP6_H
91 # include <netinet/icmp6.h>
92 #endif
94 #include "oping.h"
96 #if WITH_DEBUG
97 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
98 #else
99 # define dprintf(...) /**/
100 #endif
102 #define PING_ERRMSG_LEN 256
104 struct pinghost
105 {
106 /* username: name passed in by the user */
107 char *username;
108 /* hostname: name returned by the reverse lookup */
109 char *hostname;
110 struct sockaddr_storage *addr;
111 socklen_t addrlen;
112 int addrfamily;
113 int fd;
114 int ident;
115 int sequence;
116 struct timeval *timer;
117 double latency;
118 uint32_t dropped;
119 int recv_ttl;
120 char *data;
122 void *context;
124 struct pinghost *next;
125 };
127 struct pingobj
128 {
129 double timeout;
130 int ttl;
131 int addrfamily;
132 char *data;
134 struct sockaddr *srcaddr;
135 socklen_t srcaddrlen;
137 char errmsg[PING_ERRMSG_LEN];
139 pinghost_t *head;
140 };
142 /*
143 * private (static) functions
144 */
145 /* Even though Posix requires "strerror_r" to return an "int",
146 * some systems (e.g. the GNU libc) return a "char *" _and_
147 * ignore the second argument ... -tokkee */
148 char *sstrerror (int errnum, char *buf, size_t buflen)
149 {
150 buf[0] = 0;
152 #if !HAVE_STRERROR_R
153 {
154 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
155 }
156 /* #endif !HAVE_STRERROR_R */
158 #elif STRERROR_R_CHAR_P
159 {
160 char *temp;
161 temp = strerror_r (errnum, buf, buflen);
162 if (buf[0] == 0)
163 {
164 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
165 strncpy (buf, temp, buflen);
166 else
167 strncpy (buf, "strerror_r did not return "
168 "an error message", buflen);
169 }
170 }
171 /* #endif STRERROR_R_CHAR_P */
173 #else
174 if (strerror_r (errnum, buf, buflen) != 0)
175 {
176 snprintf (buf, buflen, "Error %i (%#x); "
177 "Additionally, strerror_r failed.",
178 errnum, errnum);
179 }
180 #endif /* STRERROR_R_CHAR_P */
182 buf[buflen - 1] = 0;
184 return (buf);
185 } /* char *sstrerror */
187 static void ping_set_error (pingobj_t *obj, const char *function,
188 const char *message)
189 {
190 snprintf (obj->errmsg, sizeof (obj->errmsg),
191 "%s: %s", function, message);
192 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
193 }
195 static void ping_set_errno (pingobj_t *obj, int error_number)
196 {
197 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
198 }
200 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
201 struct timeval *res)
202 {
203 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
204 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
206 while (res->tv_usec > 1000000)
207 {
208 res->tv_usec -= 1000000;
209 res->tv_sec++;
210 }
212 return (0);
213 }
215 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
216 struct timeval *res)
217 {
218 if ((tv1->tv_sec < tv2->tv_sec)
219 || ((tv1->tv_sec == tv2->tv_sec)
220 && (tv1->tv_usec < tv2->tv_usec)))
221 return (-1);
223 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
224 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
226 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
228 while (res->tv_usec < 0)
229 {
230 res->tv_usec += 1000000;
231 res->tv_sec--;
232 }
234 return (0);
235 }
237 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
238 {
239 uint32_t sum = 0;
240 uint16_t ret = 0;
242 uint16_t *ptr;
244 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
245 sum += *ptr;
247 if (len == 1)
248 {
249 *(char *) &ret = *(char *) ptr;
250 sum += ret;
251 }
253 /* Do this twice to get all possible carries.. */
254 sum = (sum >> 16) + (sum & 0xFFFF);
255 sum = (sum >> 16) + (sum & 0xFFFF);
257 ret = ~sum;
259 return (ret);
260 }
262 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
263 {
264 struct ip *ip_hdr;
265 struct icmp *icmp_hdr;
267 size_t ip_hdr_len;
269 uint16_t recv_checksum;
270 uint16_t calc_checksum;
272 uint16_t ident;
273 uint16_t seq;
275 pinghost_t *ptr;
277 if (buffer_len < sizeof (struct ip))
278 return (NULL);
280 ip_hdr = (struct ip *) buffer;
281 ip_hdr_len = ip_hdr->ip_hl << 2;
283 if (buffer_len < ip_hdr_len)
284 return (NULL);
286 buffer += ip_hdr_len;
287 buffer_len -= ip_hdr_len;
289 if (buffer_len < sizeof (struct icmp))
290 return (NULL);
292 icmp_hdr = (struct icmp *) buffer;
293 buffer += sizeof (struct icmp);
294 buffer_len -= sizeof (struct icmp);
296 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
297 {
298 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
299 return (NULL);
300 }
302 recv_checksum = icmp_hdr->icmp_cksum;
303 icmp_hdr->icmp_cksum = 0;
304 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
305 sizeof (struct icmp) + buffer_len);
307 if (recv_checksum != calc_checksum)
308 {
309 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
310 recv_checksum, calc_checksum);
311 return (NULL);
312 }
314 ident = ntohs (icmp_hdr->icmp_id);
315 seq = ntohs (icmp_hdr->icmp_seq);
317 for (ptr = ph; ptr != NULL; ptr = ptr->next)
318 {
319 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
320 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
322 if (ptr->addrfamily != AF_INET)
323 continue;
325 if (!timerisset (ptr->timer))
326 continue;
328 if (ptr->ident != ident)
329 continue;
331 if (((ptr->sequence - 1) & 0xFFFF) != seq)
332 continue;
334 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
335 ptr->hostname, ident, seq);
337 break;
338 }
340 if (ptr == NULL)
341 {
342 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
343 ident, seq);
344 }
346 ptr->recv_ttl = ip_hdr->ip_ttl;
348 return (ptr);
349 }
351 #ifndef ICMP6_ECHO_REQUEST
352 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
353 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
354 # else
355 # define ICMP6_ECHO_REQUEST 128
356 # endif
357 #endif
359 #ifndef ICMP6_ECHO_REPLY
360 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
361 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
362 # else
363 # define ICMP6_ECHO_REPLY 129
364 # endif
365 #endif
367 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
368 {
369 struct icmp6_hdr *icmp_hdr;
371 uint16_t ident;
372 uint16_t seq;
374 pinghost_t *ptr;
376 if (buffer_len < sizeof (struct icmp6_hdr))
377 return (NULL);
379 icmp_hdr = (struct icmp6_hdr *) buffer;
380 buffer += sizeof (struct icmp);
381 buffer_len -= sizeof (struct icmp);
383 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
384 {
385 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
386 return (NULL);
387 }
389 if (icmp_hdr->icmp6_code != 0)
390 {
391 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
392 return (NULL);
393 }
395 ident = ntohs (icmp_hdr->icmp6_id);
396 seq = ntohs (icmp_hdr->icmp6_seq);
398 for (ptr = ph; ptr != NULL; ptr = ptr->next)
399 {
400 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
401 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
403 if (ptr->addrfamily != AF_INET6)
404 continue;
406 if (!timerisset (ptr->timer))
407 continue;
409 if (ptr->ident != ident)
410 continue;
412 if (((ptr->sequence - 1) & 0xFFFF) != seq)
413 continue;
415 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
416 ptr->hostname, ident, seq);
418 break;
419 }
421 if (ptr == NULL)
422 {
423 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
424 ident, seq);
425 }
427 return (ptr);
428 }
430 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
431 {
432 struct timeval diff;
433 pinghost_t *host = NULL;
434 int recv_ttl;
436 /*
437 * Set up the receive buffer..
438 */
439 struct msghdr msghdr;
440 struct cmsghdr *cmsg;
441 char payload_buffer[4096];
442 ssize_t payload_buffer_len;
443 char control_buffer[4096];
444 struct iovec payload_iovec;
446 memset (&payload_iovec, 0, sizeof (payload_iovec));
447 payload_iovec.iov_base = payload_buffer;
448 payload_iovec.iov_len = sizeof (payload_buffer);
450 memset (&msghdr, 0, sizeof (msghdr));
451 /* unspecified source address */
452 msghdr.msg_name = NULL;
453 msghdr.msg_namelen = 0;
454 /* output buffer vector, see readv(2) */
455 msghdr.msg_iov = &payload_iovec;
456 msghdr.msg_iovlen = 1;
457 /* output buffer for control messages */
458 msghdr.msg_control = control_buffer;
459 msghdr.msg_controllen = sizeof (control_buffer);
460 /* flags; this is an output only field.. */
461 msghdr.msg_flags = 0;
462 #ifdef MSG_XPG4_2
463 msghdr.msg_flags |= MSG_XPG4_2;
464 #endif
466 payload_buffer_len = recvmsg (fd, &msghdr, /* flags = */ 0);
467 if (payload_buffer_len < 0)
468 {
469 #if WITH_DEBUG
470 char errbuf[PING_ERRMSG_LEN];
471 dprintf ("recvfrom: %s\n",
472 sstrerror (errno, errbuf, sizeof (errbuf)));
473 #endif
474 return (-1);
475 }
476 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, fd);
478 /* Iterate over all auxiliary data in msghdr */
479 recv_ttl = -1;
480 ph->recv_ttl = -1;
481 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
482 cmsg != NULL;
483 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
484 {
485 if (ph->addrfamily == AF_INET) /* {{{ */
486 {
487 if (cmsg->cmsg_level != IPPROTO_IP)
488 continue;
490 if (cmsg->cmsg_type == IP_TTL)
491 {
492 memcpy (&recv_ttl, CMSG_DATA (cmsg),
493 sizeof (recv_ttl));
494 dprintf ("TTLv4 = %i;\n", recv_ttl);
495 }
496 else
497 {
498 dprintf ("Not handling option %i.\n",
499 cmsg->cmsg_type);
500 }
501 } /* }}} */
502 else if (ph->addrfamily == AF_INET6) /* {{{ */
503 {
504 if (cmsg->cmsg_level != IPPROTO_IPV6)
505 continue;
507 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
508 {
509 memcpy (&recv_ttl, CMSG_DATA (cmsg),
510 sizeof (recv_ttl));
511 dprintf ("TTLv6 = %i;\n", recv_ttl);
512 }
513 else
514 {
515 dprintf ("Not handling option %i.\n",
516 cmsg->cmsg_type);
517 }
518 } /* }}} */
519 else
520 {
521 dprintf ("Don't know how to handle "
522 "unknown protocol %i.\n",
523 cmsg->cmsg_level);
524 }
525 } /* }}} for (cmsg) */
527 if (ph->addrfamily == AF_INET)
528 {
529 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
530 if (host == NULL)
531 return (-1);
532 }
533 else if (ph->addrfamily == AF_INET6)
534 {
535 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
536 if (host == NULL)
537 return (-1);
538 }
539 else
540 {
541 dprintf ("ping_receive_one: Unknown address family %i.\n",
542 ph->addrfamily);
543 return (-1);
544 }
546 dprintf ("rcvd: %12i.%06i\n",
547 (int) now->tv_sec,
548 (int) now->tv_usec);
549 dprintf ("sent: %12i.%06i\n",
550 (int) host->timer->tv_sec,
551 (int) host->timer->tv_usec);
553 if (ping_timeval_sub (now, host->timer, &diff) < 0)
554 {
555 timerclear (host->timer);
556 return (-1);
557 }
559 dprintf ("diff: %12i.%06i\n",
560 (int) diff.tv_sec,
561 (int) diff.tv_usec);
563 if (recv_ttl >= 0)
564 host->recv_ttl = recv_ttl;
566 host->latency = ((double) diff.tv_usec) / 1000.0;
567 host->latency += ((double) diff.tv_sec) * 1000.0;
569 timerclear (host->timer);
571 return (0);
572 }
574 static int ping_receive_all (pingobj_t *obj)
575 {
576 fd_set readfds;
577 int num_readfds;
578 int max_readfds;
580 pinghost_t *ph;
581 pinghost_t *ptr;
583 struct timeval endtime;
584 struct timeval nowtime;
585 struct timeval timeout;
586 int status;
588 int ret;
590 ph = obj->head;
591 ret = 0;
593 for (ptr = ph; ptr != NULL; ptr = ptr->next)
594 ptr->latency = -1.0;
596 if (gettimeofday (&nowtime, NULL) == -1)
597 {
598 ping_set_errno (obj, errno);
599 return (-1);
600 }
602 /* Set up timeout */
603 timeout.tv_sec = (time_t) obj->timeout;
604 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
606 dprintf ("Set timeout to %i.%06i seconds\n",
607 (int) timeout.tv_sec,
608 (int) timeout.tv_usec);
610 ping_timeval_add (&nowtime, &timeout, &endtime);
612 while (1)
613 {
614 FD_ZERO (&readfds);
615 num_readfds = 0;
616 max_readfds = -1;
618 for (ptr = ph; ptr != NULL; ptr = ptr->next)
619 {
620 if (!timerisset (ptr->timer))
621 continue;
623 FD_SET (ptr->fd, &readfds);
624 num_readfds++;
626 if (max_readfds < ptr->fd)
627 max_readfds = ptr->fd;
628 }
630 if (num_readfds == 0)
631 break;
633 if (gettimeofday (&nowtime, NULL) == -1)
634 {
635 ping_set_errno (obj, errno);
636 return (-1);
637 }
639 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
640 break;
642 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
643 (int) timeout.tv_sec,
644 (int) timeout.tv_usec);
646 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
648 if (gettimeofday (&nowtime, NULL) == -1)
649 {
650 ping_set_errno (obj, errno);
651 return (-1);
652 }
654 if ((status == -1) && (errno == EINTR))
655 {
656 dprintf ("select was interrupted by signal..\n");
657 continue;
658 }
659 else if (status < 0)
660 {
661 #if WITH_DEBUG
662 char errbuf[PING_ERRMSG_LEN];
663 dprintf ("select: %s\n",
664 sstrerror (errno, errbuf, sizeof (errbuf)));
665 #endif
666 break;
667 }
668 else if (status == 0)
669 {
670 dprintf ("select timed out\n");
671 for (ptr = ph; ptr != NULL; ptr = ptr->next)
672 if (ptr->latency < 0.0)
673 ptr->dropped++;
674 break;
675 }
677 for (ptr = ph; ptr != NULL; ptr = ptr->next)
678 {
679 if (FD_ISSET (ptr->fd, &readfds))
680 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
681 ret++;
682 }
683 } /* while (1) */
685 return (ret);
686 }
688 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
689 * Sending functions: *
690 * *
691 * ping_send_all *
692 * +-> ping_send_one_ipv4 *
693 * `-> ping_send_one_ipv6 *
694 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
695 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
696 const void *buf, size_t buflen)
697 {
698 ssize_t ret;
700 if (gettimeofday (ph->timer, NULL) == -1)
701 {
702 timerclear (ph->timer);
703 return (-1);
704 }
706 ret = sendto (ph->fd, buf, buflen, 0,
707 (struct sockaddr *) ph->addr, ph->addrlen);
709 if (ret < 0)
710 {
711 #if defined(EHOSTUNREACH)
712 if (errno == EHOSTUNREACH)
713 return (0);
714 #endif
715 #if defined(ENETUNREACH)
716 if (errno == ENETUNREACH)
717 return (0);
718 #endif
719 ping_set_errno (obj, errno);
720 }
722 return (ret);
723 }
725 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
726 {
727 struct icmp *icmp4;
728 int status;
730 char buf[4096];
731 int buflen;
733 char *data;
734 int datalen;
736 dprintf ("ph->hostname = %s\n", ph->hostname);
738 memset (buf, '\0', sizeof (buf));
739 icmp4 = (struct icmp *) buf;
740 data = (char *) (icmp4 + 1);
742 icmp4->icmp_type = ICMP_ECHO;
743 icmp4->icmp_code = 0;
744 icmp4->icmp_cksum = 0;
745 icmp4->icmp_id = htons (ph->ident);
746 icmp4->icmp_seq = htons (ph->sequence);
748 buflen = 4096 - sizeof (struct icmp);
749 strncpy (data, ph->data, buflen);
750 datalen = strlen (data);
752 buflen = datalen + sizeof (struct icmp);
754 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
756 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
758 status = ping_sendto (obj, ph, buf, buflen);
759 if (status < 0)
760 {
761 perror ("ping_sendto");
762 return (-1);
763 }
765 dprintf ("sendto: status = %i\n", status);
767 return (0);
768 }
770 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
771 {
772 struct icmp6_hdr *icmp6;
773 int status;
775 char buf[4096];
776 int buflen;
778 char *data;
779 int datalen;
781 dprintf ("ph->hostname = %s\n", ph->hostname);
783 memset (buf, '\0', sizeof (buf));
784 icmp6 = (struct icmp6_hdr *) buf;
785 data = (char *) (icmp6 + 1);
787 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
788 icmp6->icmp6_code = 0;
789 /* The checksum will be calculated by the TCP/IP stack. */
790 /* FIXME */
791 icmp6->icmp6_cksum = 0;
792 icmp6->icmp6_id = htons (ph->ident);
793 icmp6->icmp6_seq = htons (ph->sequence);
795 buflen = 4096 - sizeof (struct icmp6_hdr);
796 strncpy (data, ph->data, buflen);
797 datalen = strlen (data);
799 buflen = datalen + sizeof (struct icmp6_hdr);
801 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
803 status = ping_sendto (obj, ph, buf, buflen);
804 if (status < 0)
805 {
806 perror ("ping_sendto");
807 return (-1);
808 }
810 dprintf ("sendto: status = %i\n", status);
812 return (0);
813 }
815 static int ping_send_all (pingobj_t *obj)
816 {
817 pinghost_t *ph;
818 pinghost_t *ptr;
820 int ret;
822 ret = 0;
823 ph = obj->head;
825 for (ptr = ph; ptr != NULL; ptr = ptr->next)
826 {
827 /* start timer.. The GNU `ping6' starts the timer before
828 * sending the packet, so I will do that too */
829 if (gettimeofday (ptr->timer, NULL) == -1)
830 {
831 #if WITH_DEBUG
832 char errbuf[PING_ERRMSG_LEN];
833 dprintf ("gettimeofday: %s\n",
834 sstrerror (errno, errbuf, sizeof (errbuf)));
835 #endif
836 timerclear (ptr->timer);
837 ret--;
838 continue;
839 }
840 else
841 {
842 dprintf ("timer set for hostname = %s\n", ptr->hostname);
843 }
845 if (ptr->addrfamily == AF_INET6)
846 {
847 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
848 if (ping_send_one_ipv6 (obj, ptr) != 0)
849 {
850 timerclear (ptr->timer);
851 ret--;
852 continue;
853 }
854 }
855 else if (ptr->addrfamily == AF_INET)
856 {
857 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
858 if (ping_send_one_ipv4 (obj, ptr) != 0)
859 {
860 timerclear (ptr->timer);
861 ret--;
862 continue;
863 }
864 }
865 else /* this should not happen */
866 {
867 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
868 timerclear (ptr->timer);
869 ret--;
870 continue;
871 }
873 ptr->sequence++;
874 }
876 return (ret);
877 }
879 /*
880 * Set the TTL of a socket protocol independently.
881 */
882 static int ping_set_ttl (pinghost_t *ph, int ttl)
883 {
884 int ret = -2;
886 if (ph->addrfamily == AF_INET)
887 {
888 dprintf ("Setting TTLv4 to %i\n", ttl);
889 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
890 &ttl, sizeof (ttl));
891 }
892 else if (ph->addrfamily == AF_INET6)
893 {
894 dprintf ("Setting TTLv6 to %i\n", ttl);
895 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
896 &ttl, sizeof (ttl));
897 }
899 return (ret);
900 }
902 static int ping_get_ident (void)
903 {
904 int fd;
905 static int did_seed = 0;
907 int retval;
909 if (did_seed == 0)
910 {
911 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
912 {
913 unsigned int seed;
915 if (read (fd, &seed, sizeof (seed)) != -1)
916 {
917 did_seed = 1;
918 dprintf ("Random seed: %#x\n", seed);
919 srandom (seed);
920 }
922 close (fd);
923 }
924 #if WITH_DEBUG
925 else
926 {
927 char errbuf[PING_ERRMSG_LEN];
928 dprintf ("open (/dev/urandom): %s\n",
929 sstrerror (errno, errbuf, sizeof (errbuf)));
930 }
931 #endif
932 }
934 retval = (int) random ();
936 dprintf ("Random number: %#x\n", retval);
938 return (retval);
939 }
941 static pinghost_t *ping_alloc (void)
942 {
943 pinghost_t *ph;
944 size_t ph_size;
946 ph_size = sizeof (pinghost_t)
947 + sizeof (struct sockaddr_storage)
948 + sizeof (struct timeval);
950 ph = (pinghost_t *) malloc (ph_size);
951 if (ph == NULL)
952 return (NULL);
954 memset (ph, '\0', ph_size);
956 ph->timer = (struct timeval *) (ph + 1);
957 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
959 ph->addrlen = sizeof (struct sockaddr_storage);
960 ph->fd = -1;
961 ph->latency = -1.0;
962 ph->dropped = 0;
963 ph->ident = ping_get_ident () & 0xFFFF;
965 return (ph);
966 }
968 static void ping_free (pinghost_t *ph)
969 {
970 if (ph->fd >= 0)
971 close (ph->fd);
973 if (ph->username != NULL)
974 free (ph->username);
976 if (ph->hostname != NULL)
977 free (ph->hostname);
979 if (ph->data != NULL)
980 free (ph->data);
982 free (ph);
983 }
985 /*
986 * public methods
987 */
988 const char *ping_get_error (pingobj_t *obj)
989 {
990 return (obj->errmsg);
991 }
993 pingobj_t *ping_construct (void)
994 {
995 pingobj_t *obj;
997 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
998 return (NULL);
999 memset (obj, '\0', sizeof (pingobj_t));
1001 obj->timeout = PING_DEF_TIMEOUT;
1002 obj->ttl = PING_DEF_TTL;
1003 obj->addrfamily = PING_DEF_AF;
1004 obj->data = strdup (PING_DEF_DATA);
1006 return (obj);
1007 }
1009 void ping_destroy (pingobj_t *obj)
1010 {
1011 pinghost_t *current;
1012 pinghost_t *next;
1014 current = obj->head;
1015 next = NULL;
1017 while (current != NULL)
1018 {
1019 next = current->next;
1020 ping_free (current);
1021 current = next;
1022 }
1024 if (obj->data != NULL)
1025 free (obj->data);
1027 if (obj->srcaddr != NULL)
1028 free (obj->srcaddr);
1030 free (obj);
1032 return;
1033 }
1035 int ping_setopt (pingobj_t *obj, int option, void *value)
1036 {
1037 int ret = 0;
1039 switch (option)
1040 {
1041 case PING_OPT_TIMEOUT:
1042 obj->timeout = *((double *) value);
1043 if (obj->timeout < 0.0)
1044 {
1045 obj->timeout = PING_DEF_TIMEOUT;
1046 ret = -1;
1047 }
1048 break;
1050 case PING_OPT_TTL:
1051 obj->ttl = *((int *) value);
1052 if ((obj->ttl < 1) || (obj->ttl > 255))
1053 {
1054 obj->ttl = PING_DEF_TTL;
1055 ret = -1;
1056 }
1057 else
1058 {
1059 pinghost_t *ph;
1061 for (ph = obj->head; ph != NULL; ph = ph->next)
1062 ping_set_ttl (ph, obj->ttl);
1063 }
1064 break;
1066 case PING_OPT_AF:
1067 obj->addrfamily = *((int *) value);
1068 if ((obj->addrfamily != AF_UNSPEC)
1069 && (obj->addrfamily != AF_INET)
1070 && (obj->addrfamily != AF_INET6))
1071 {
1072 obj->addrfamily = PING_DEF_AF;
1073 ret = -1;
1074 }
1075 if (obj->srcaddr != NULL)
1076 {
1077 free (obj->srcaddr);
1078 obj->srcaddr = NULL;
1079 }
1080 break;
1082 case PING_OPT_DATA:
1083 if (obj->data != NULL)
1084 {
1085 free (obj->data);
1086 obj->data = NULL;
1087 }
1088 obj->data = strdup ((const char *) value);
1089 break;
1091 case PING_OPT_SOURCE:
1092 {
1093 char *hostname = (char *) value;
1094 struct addrinfo ai_hints;
1095 struct addrinfo *ai_list;
1096 int status;
1097 #if WITH_DEBUG
1098 if (obj->addrfamily != AF_UNSPEC)
1099 {
1100 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1101 }
1102 #endif
1103 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1104 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1105 #if defined(AI_ADDRCONFIG)
1106 ai_hints.ai_flags = AI_ADDRCONFIG;
1107 #endif
1108 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1109 if (status != 0)
1110 {
1111 #if defined(EAI_SYSTEM)
1112 char errbuf[PING_ERRMSG_LEN];
1113 #endif
1114 ping_set_error (obj, "getaddrinfo",
1115 #if defined(EAI_SYSTEM)
1116 (status == EAI_SYSTEM)
1117 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1118 #endif
1119 gai_strerror (status));
1120 ret = -1;
1121 break;
1122 }
1123 #if WITH_DEBUG
1124 if (ai_list->ai_next != NULL)
1125 {
1126 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1127 }
1128 #endif
1129 if (obj->srcaddr == NULL)
1130 {
1131 obj->srcaddrlen = 0;
1132 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1133 if (obj->srcaddr == NULL)
1134 {
1135 ping_set_errno (obj, errno);
1136 ret = -1;
1137 freeaddrinfo (ai_list);
1138 break;
1139 }
1140 }
1141 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1142 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1143 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1144 ai_list->ai_addrlen);
1145 obj->srcaddrlen = ai_list->ai_addrlen;
1146 obj->addrfamily = ai_list->ai_family;
1148 freeaddrinfo (ai_list);
1149 } /* case PING_OPT_SOURCE */
1150 break;
1152 default:
1153 ret = -2;
1154 } /* switch (option) */
1156 return (ret);
1157 } /* int ping_setopt */
1160 int ping_send (pingobj_t *obj)
1161 {
1162 int ret;
1164 if (ping_send_all (obj) < 0)
1165 return (-1);
1167 if ((ret = ping_receive_all (obj)) < 0)
1168 return (-2);
1170 return (ret);
1171 }
1173 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1174 {
1175 while (ph != NULL)
1176 {
1177 if (strcasecmp (ph->username, host) == 0)
1178 break;
1180 ph = ph->next;
1181 }
1183 return (ph);
1184 }
1186 int ping_host_add (pingobj_t *obj, const char *host)
1187 {
1188 pinghost_t *ph;
1190 struct addrinfo ai_hints;
1191 struct addrinfo *ai_list, *ai_ptr;
1192 int ai_return;
1194 dprintf ("host = %s\n", host);
1196 if (ping_host_search (obj->head, host) != NULL)
1197 return (0);
1199 memset (&ai_hints, '\0', sizeof (ai_hints));
1200 ai_hints.ai_flags = 0;
1201 #ifdef AI_ADDRCONFIG
1202 ai_hints.ai_flags |= AI_ADDRCONFIG;
1203 #endif
1204 #ifdef AI_CANONNAME
1205 ai_hints.ai_flags |= AI_CANONNAME;
1206 #endif
1207 ai_hints.ai_family = obj->addrfamily;
1208 ai_hints.ai_socktype = SOCK_RAW;
1210 if ((ph = ping_alloc ()) == NULL)
1211 {
1212 dprintf ("Out of memory!\n");
1213 return (-1);
1214 }
1216 if ((ph->username = strdup (host)) == NULL)
1217 {
1218 dprintf ("Out of memory!\n");
1219 ping_set_errno (obj, errno);
1220 ping_free (ph);
1221 return (-1);
1222 }
1224 if ((ph->hostname = strdup (host)) == NULL)
1225 {
1226 dprintf ("Out of memory!\n");
1227 ping_set_errno (obj, errno);
1228 ping_free (ph);
1229 return (-1);
1230 }
1232 /* obj->data is not garuanteed to be != NULL */
1233 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1234 {
1235 dprintf ("Out of memory!\n");
1236 ping_set_errno (obj, errno);
1237 ping_free (ph);
1238 return (-1);
1239 }
1241 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1242 {
1243 #if defined(EAI_SYSTEM)
1244 char errbuf[PING_ERRMSG_LEN];
1245 #endif
1246 dprintf ("getaddrinfo failed\n");
1247 ping_set_error (obj, "getaddrinfo",
1248 #if defined(EAI_SYSTEM)
1249 (ai_return == EAI_SYSTEM)
1250 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1251 #endif
1252 gai_strerror (ai_return));
1253 ping_free (ph);
1254 return (-1);
1255 }
1257 if (ai_list == NULL)
1258 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1260 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1261 {
1262 ph->fd = -1;
1264 if (ai_ptr->ai_family == AF_INET)
1265 {
1266 ai_ptr->ai_socktype = SOCK_RAW;
1267 ai_ptr->ai_protocol = IPPROTO_ICMP;
1268 }
1269 else if (ai_ptr->ai_family == AF_INET6)
1270 {
1271 ai_ptr->ai_socktype = SOCK_RAW;
1272 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1273 }
1274 else
1275 {
1276 char errmsg[PING_ERRMSG_LEN];
1278 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1279 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1281 dprintf (errmsg);
1282 ping_set_error (obj, "getaddrinfo", errmsg);
1283 continue;
1284 }
1286 /* TODO: Move this to a static function `ping_open_socket' and
1287 * call it whenever the socket dies. */
1288 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1289 if (ph->fd == -1)
1290 {
1291 #if WITH_DEBUG
1292 char errbuf[PING_ERRMSG_LEN];
1293 dprintf ("socket: %s\n",
1294 sstrerror (errno, errbuf, sizeof (errbuf)));
1295 #endif
1296 ping_set_errno (obj, errno);
1297 continue;
1298 }
1300 if (obj->srcaddr != NULL)
1301 {
1302 assert (obj->srcaddrlen > 0);
1303 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1305 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1306 {
1307 #if WITH_DEBUG
1308 char errbuf[PING_ERRMSG_LEN];
1309 dprintf ("bind: %s\n",
1310 sstrerror (errno, errbuf, sizeof (errbuf)));
1311 #endif
1312 ping_set_errno (obj, errno);
1313 close (ph->fd);
1314 ph->fd = -1;
1315 continue;
1316 }
1317 }
1319 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1320 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1321 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1322 ph->addrlen = ai_ptr->ai_addrlen;
1323 ph->addrfamily = ai_ptr->ai_family;
1325 #ifdef AI_CANONNAME
1326 if ((ai_ptr->ai_canonname != NULL)
1327 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1328 {
1329 char *old_hostname;
1331 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1332 ph->hostname, ai_ptr->ai_canonname);
1334 old_hostname = ph->hostname;
1335 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1336 {
1337 /* strdup failed, falling back to old hostname */
1338 ph->hostname = old_hostname;
1339 }
1340 else if (old_hostname != NULL)
1341 {
1342 free (old_hostname);
1343 }
1344 }
1345 #endif /* AI_CANONNAME */
1347 if (ph->addrfamily == AF_INET)
1348 {
1349 int opt = 1;
1351 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1352 &opt, sizeof (opt));
1353 }
1354 else if (ph->addrfamily == AF_INET6)
1355 {
1356 int opt = 1;
1358 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1359 &opt, sizeof (opt));
1360 }
1362 break;
1363 }
1365 freeaddrinfo (ai_list);
1367 if (ph->fd < 0)
1368 {
1369 ping_free (ph);
1370 return (-1);
1371 }
1373 /*
1374 * Adding in the front is much easier, but then the iterator will
1375 * return the host that was added last as first host. That's just not
1376 * nice. -octo
1377 */
1378 if (obj->head == NULL)
1379 {
1380 obj->head = ph;
1381 }
1382 else
1383 {
1384 pinghost_t *hptr;
1386 hptr = obj->head;
1387 while (hptr->next != NULL)
1388 hptr = hptr->next;
1390 assert ((hptr != NULL) && (hptr->next == NULL));
1391 hptr->next = ph;
1392 }
1394 ping_set_ttl (ph, obj->ttl);
1396 return (0);
1397 } /* int ping_host_add */
1399 int ping_host_remove (pingobj_t *obj, const char *host)
1400 {
1401 pinghost_t *pre, *cur;
1403 pre = NULL;
1404 cur = obj->head;
1406 while (cur != NULL)
1407 {
1408 if (strcasecmp (host, cur->username) == 0)
1409 break;
1411 pre = cur;
1412 cur = cur->next;
1413 }
1415 if (cur == NULL)
1416 {
1417 ping_set_error (obj, "ping_host_remove", "Host not found");
1418 return (-1);
1419 }
1421 if (pre == NULL)
1422 obj->head = cur->next;
1423 else
1424 pre->next = cur->next;
1426 ping_free (cur);
1428 return (0);
1429 }
1431 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1432 {
1433 return ((pingobj_iter_t *) obj->head);
1434 }
1436 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1437 {
1438 return ((pingobj_iter_t *) iter->next);
1439 }
1441 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1442 void *buffer, size_t *buffer_len)
1443 {
1444 int ret = EINVAL;
1446 size_t orig_buffer_len = *buffer_len;
1448 switch (info)
1449 {
1450 case PING_INFO_USERNAME:
1451 ret = ENOMEM;
1452 *buffer_len = strlen (iter->username) + 1;
1453 if (orig_buffer_len <= *buffer_len)
1454 break;
1455 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1456 * will copy `*buffer_len' and pad the rest of
1457 * `buffer' with null-bytes */
1458 strncpy (buffer, iter->username, orig_buffer_len);
1459 ret = 0;
1460 break;
1462 case PING_INFO_HOSTNAME:
1463 ret = ENOMEM;
1464 *buffer_len = strlen (iter->hostname) + 1;
1465 if (orig_buffer_len < *buffer_len)
1466 break;
1467 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1468 * will copy `*buffer_len' and pad the rest of
1469 * `buffer' with null-bytes */
1470 strncpy (buffer, iter->hostname, orig_buffer_len);
1471 ret = 0;
1472 break;
1474 case PING_INFO_ADDRESS:
1475 ret = getnameinfo ((struct sockaddr *) iter->addr,
1476 iter->addrlen,
1477 (char *) buffer,
1478 *buffer_len,
1479 NULL, 0,
1480 NI_NUMERICHOST);
1481 if (ret != 0)
1482 {
1483 if ((ret == EAI_MEMORY)
1484 #ifdef EAI_OVERFLOW
1485 || (ret == EAI_OVERFLOW)
1486 #endif
1487 )
1488 ret = ENOMEM;
1489 #if defined(EAI_SYSTEM)
1490 else if (ret == EAI_SYSTEM)
1491 ret = errno;
1492 #endif
1493 else
1494 ret = EINVAL;
1495 }
1496 break;
1498 case PING_INFO_FAMILY:
1499 ret = ENOMEM;
1500 *buffer_len = sizeof (int);
1501 if (orig_buffer_len < sizeof (int))
1502 break;
1503 *((int *) buffer) = iter->addrfamily;
1504 ret = 0;
1505 break;
1507 case PING_INFO_LATENCY:
1508 ret = ENOMEM;
1509 *buffer_len = sizeof (double);
1510 if (orig_buffer_len < sizeof (double))
1511 break;
1512 *((double *) buffer) = iter->latency;
1513 ret = 0;
1514 break;
1516 case PING_INFO_DROPPED:
1517 ret = ENOMEM;
1518 *buffer_len = sizeof (uint32_t);
1519 if (orig_buffer_len < sizeof (uint32_t))
1520 break;
1521 *((uint32_t *) buffer) = iter->dropped;
1522 ret = 0;
1523 break;
1525 case PING_INFO_SEQUENCE:
1526 ret = ENOMEM;
1527 *buffer_len = sizeof (unsigned int);
1528 if (orig_buffer_len < sizeof (unsigned int))
1529 break;
1530 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1531 ret = 0;
1532 break;
1534 case PING_INFO_IDENT:
1535 ret = ENOMEM;
1536 *buffer_len = sizeof (uint16_t);
1537 if (orig_buffer_len < sizeof (uint16_t))
1538 break;
1539 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1540 ret = 0;
1541 break;
1543 case PING_INFO_DATA:
1544 ret = ENOMEM;
1545 *buffer_len = strlen (iter->data);
1546 if (orig_buffer_len < *buffer_len)
1547 break;
1548 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1549 ret = 0;
1550 break;
1552 case PING_INFO_RECV_TTL:
1553 ret = ENOMEM;
1554 *buffer_len = sizeof (int);
1555 if (orig_buffer_len < sizeof (int))
1556 break;
1557 *((int *) buffer) = iter->recv_ttl;
1558 ret = 0;
1559 break;
1560 }
1562 return (ret);
1563 } /* ping_iterator_get_info */
1565 void *ping_iterator_get_context (pingobj_iter_t *iter)
1566 {
1567 return (iter->context);
1568 }
1570 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1571 {
1572 iter->context = context;
1573 }