1 /**
2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006 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; either version 2 of the License, or
8 * (at your option) any later version.
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 <errno.h>
29 # include <assert.h>
30 #else
31 # error "You don't have the standard C99 header files installed"
32 #endif /* STDC_HEADERS */
34 #if HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
38 #ifndef __USE_BSD
39 # define __USE_BSD
40 #endif
42 #if HAVE_FCNTL_H
43 # include <fcntl.h>
44 #endif
45 #if HAVE_SYS_TYPES_H
46 # include <sys/types.h>
47 #endif
48 #if HAVE_SYS_STAT_H
49 # include <sys/stat.h>
50 #endif
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
54 # include <time.h>
55 #else
56 # if HAVE_SYS_TIME_H
57 # include <sys/time.h>
58 # else
59 # include <time.h>
60 # endif
61 #endif
63 #if HAVE_SYS_SOCKET_H
64 # include <sys/socket.h>
65 #endif
66 #if HAVE_NETDB_H
67 # include <netdb.h>
68 #endif
70 #if HAVE_NETINET_IN_SYSTM_H
71 # include <netinet/in_systm.h>
72 #endif
73 #if HAVE_NETINET_IN_H
74 # include <netinet/in.h>
75 #endif
76 #if HAVE_NETINET_IP_H
77 # include <netinet/ip.h>
78 #endif
79 #if HAVE_NETINET_IP_ICMP_H
80 # include <netinet/ip_icmp.h>
81 #endif
82 #ifdef HAVE_NETINET_IP_VAR_H
83 # include <netinet/ip_var.h>
84 #endif
85 #if HAVE_NETINET_IP6_H
86 # include <netinet/ip6.h>
87 #endif
88 #if HAVE_NETINET_ICMP6_H
89 # include <netinet/icmp6.h>
90 #endif
92 #include "oping.h"
94 #if DEBUG
95 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
96 #else
97 # define dprintf(...) /**/
98 #endif
100 #define PING_ERRMSG_LEN 256
102 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
104 struct pinghost
105 {
106 char *hostname;
107 struct sockaddr_storage *addr;
108 socklen_t addrlen;
109 int addrfamily;
110 int fd;
111 int ident;
112 int sequence;
113 struct timeval *timer;
114 double latency;
116 void *context;
118 struct pinghost *next;
119 };
121 struct pingobj
122 {
123 double timeout;
124 int ttl;
125 int addrfamily;
127 char errmsg[PING_ERRMSG_LEN];
129 pinghost_t *head;
130 };
132 /*
133 * private (static) functions
134 */
135 static void ping_set_error (pingobj_t *obj, const char *function,
136 const char *message)
137 {
138 snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message);
139 obj->errmsg[PING_ERRMSG_LEN - 1] = '\0';
140 }
142 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
143 struct timeval *res)
144 {
145 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
146 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
148 while (res->tv_usec > 1000000)
149 {
150 res->tv_usec -= 1000000;
151 res->tv_sec++;
152 }
154 return (0);
155 }
157 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
158 struct timeval *res)
159 {
161 if ((tv1->tv_sec < tv2->tv_sec)
162 || ((tv1->tv_sec == tv2->tv_sec)
163 && (tv1->tv_usec < tv2->tv_usec)))
164 return (-1);
166 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
167 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
169 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
171 while (res->tv_usec < 0)
172 {
173 res->tv_usec += 1000000;
174 res->tv_sec--;
175 }
177 return (0);
178 }
180 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
181 {
182 uint32_t sum = 0;
183 uint16_t ret = 0;
185 uint16_t *ptr;
187 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
188 sum += *ptr;
190 if (len == 1)
191 {
192 *(char *) &ret = *(char *) ptr;
193 sum += ret;
194 }
196 /* Do this twice to get all possible carries.. */
197 sum = (sum >> 16) + (sum & 0xFFFF);
198 sum = (sum >> 16) + (sum & 0xFFFF);
200 ret = ~sum;
202 return (ret);
203 }
205 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
206 {
207 struct ip *ip_hdr;
208 struct icmp *icmp_hdr;
210 size_t ip_hdr_len;
212 uint16_t recv_checksum;
213 uint16_t calc_checksum;
215 uint16_t ident;
216 uint16_t seq;
218 pinghost_t *ptr;
220 if (buffer_len < sizeof (struct ip))
221 return (NULL);
223 ip_hdr = (struct ip *) buffer;
224 ip_hdr_len = ip_hdr->ip_hl << 2;
226 if (buffer_len < ip_hdr_len)
227 return (NULL);
229 buffer += ip_hdr_len;
230 buffer_len -= ip_hdr_len;
232 if (buffer_len < sizeof (struct icmp))
233 return (NULL);
235 icmp_hdr = (struct icmp *) buffer;
236 buffer += sizeof (struct icmp);
237 buffer_len -= sizeof (struct icmp);
239 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
240 {
241 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
242 return (NULL);
243 }
245 recv_checksum = icmp_hdr->icmp_cksum;
246 icmp_hdr->icmp_cksum = 0;
247 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
248 sizeof (struct icmp) + buffer_len);
250 if (recv_checksum != calc_checksum)
251 {
252 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
253 recv_checksum, calc_checksum);
254 return (NULL);
255 }
257 ident = ntohs (icmp_hdr->icmp_id);
258 seq = ntohs (icmp_hdr->icmp_seq);
260 for (ptr = ph; ptr != NULL; ptr = ptr->next)
261 {
262 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
263 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
265 if (ptr->addrfamily != AF_INET)
266 continue;
268 if (!timerisset (ptr->timer))
269 continue;
271 if (ptr->ident != ident)
272 continue;
274 if (((ptr->sequence - 1) & 0xFFFF) != seq)
275 continue;
277 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
278 ptr->hostname, ident, seq);
280 break;
281 }
283 if (ptr == NULL)
284 {
285 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
286 ident, seq);
287 }
289 return (ptr);
290 }
292 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
293 {
294 struct icmp6_hdr *icmp_hdr;
296 uint16_t ident;
297 uint16_t seq;
299 pinghost_t *ptr;
301 if (buffer_len < sizeof (struct icmp6_hdr))
302 return (NULL);
304 icmp_hdr = (struct icmp6_hdr *) buffer;
305 buffer += sizeof (struct icmp);
306 buffer_len -= sizeof (struct icmp);
308 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
309 {
310 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
311 return (NULL);
312 }
314 if (icmp_hdr->icmp6_code != 0)
315 {
316 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
317 return (NULL);
318 }
320 ident = ntohs (icmp_hdr->icmp6_id);
321 seq = ntohs (icmp_hdr->icmp6_seq);
323 for (ptr = ph; ptr != NULL; ptr = ptr->next)
324 {
325 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
326 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
328 if (ptr->addrfamily != AF_INET6)
329 continue;
331 if (!timerisset (ptr->timer))
332 continue;
334 if (ptr->ident != ident)
335 continue;
337 if (((ptr->sequence - 1) & 0xFFFF) != seq)
338 continue;
340 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
341 ptr->hostname, ident, seq);
343 break;
344 }
346 if (ptr == NULL)
347 {
348 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
349 ident, seq);
350 }
352 return (ptr);
353 }
355 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
356 {
357 char buffer[4096];
358 size_t buffer_len;
360 struct timeval diff;
362 pinghost_t *host = NULL;
364 struct sockaddr_storage sa;
365 socklen_t sa_len;
367 sa_len = sizeof (sa);
369 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
370 (struct sockaddr *) &sa, &sa_len);
371 if (buffer_len == -1)
372 {
373 dprintf ("recvfrom: %s\n", strerror (errno));
374 return (-1);
375 }
377 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
379 if (sa.ss_family == AF_INET)
380 {
381 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
382 return (-1);
383 }
384 else if (sa.ss_family == AF_INET6)
385 {
386 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
387 return (-1);
388 }
390 dprintf ("rcvd: %12i.%06i\n",
391 (int) now->tv_sec,
392 (int) now->tv_usec);
393 dprintf ("sent: %12i.%06i\n",
394 (int) host->timer->tv_sec,
395 (int) host->timer->tv_usec);
397 if (ping_timeval_sub (now, host->timer, &diff) < 0)
398 {
399 timerclear (host->timer);
400 return (-1);
401 }
403 dprintf ("diff: %12i.%06i\n",
404 (int) diff.tv_sec,
405 (int) diff.tv_usec);
407 host->latency = ((double) diff.tv_usec) / 1000.0;
408 host->latency += ((double) diff.tv_sec) * 1000.0;
410 timerclear (host->timer);
412 return (0);
413 }
415 static int ping_receive_all (pingobj_t *obj)
416 {
417 fd_set readfds;
418 int num_readfds;
419 int max_readfds;
421 pinghost_t *ph;
422 pinghost_t *ptr;
424 struct timeval endtime;
425 struct timeval nowtime;
426 struct timeval timeout;
427 int status;
429 int ret;
431 ph = obj->head;
432 ret = 0;
434 for (ptr = ph; ptr != NULL; ptr = ptr->next)
435 ptr->latency = -1.0;
437 if (gettimeofday (&nowtime, NULL) == -1)
438 {
439 ping_set_error (obj, "gettimeofday", strerror (errno));
440 return (-1);
441 }
443 /* Set up timeout */
444 timeout.tv_sec = (time_t) obj->timeout;
445 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
447 dprintf ("Set timeout to %i.%06i seconds\n",
448 (int) timeout.tv_sec,
449 (int) timeout.tv_usec);
451 ping_timeval_add (&nowtime, &timeout, &endtime);
453 while (1)
454 {
455 FD_ZERO (&readfds);
456 num_readfds = 0;
457 max_readfds = -1;
459 for (ptr = ph; ptr != NULL; ptr = ptr->next)
460 {
461 if (!timerisset (ptr->timer))
462 continue;
464 FD_SET (ptr->fd, &readfds);
465 num_readfds++;
467 if (max_readfds < ptr->fd)
468 max_readfds = ptr->fd;
469 }
471 if (num_readfds == 0)
472 break;
474 if (gettimeofday (&nowtime, NULL) == -1)
475 {
476 ping_set_error (obj, "gettimeofday", strerror (errno));
477 return (-1);
478 }
480 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
481 break;
483 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
484 (int) timeout.tv_sec,
485 (int) timeout.tv_usec);
487 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
489 if (gettimeofday (&nowtime, NULL) == -1)
490 {
491 ping_set_error (obj, "gettimeofday", strerror (errno));
492 return (-1);
493 }
495 if ((status == -1) && (errno == EINTR))
496 {
497 dprintf ("select was interrupted by signal..\n");
498 continue;
499 }
500 else if (status < 0)
501 {
502 dprintf ("select: %s\n", strerror (errno));
503 break;
504 }
505 else if (status == 0)
506 {
507 dprintf ("select timed out\n");
508 break;
509 }
511 for (ptr = ph; ptr != NULL; ptr = ptr->next)
512 {
513 if (FD_ISSET (ptr->fd, &readfds))
514 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
515 ret++;
516 }
517 } /* while (1) */
519 return (ret);
520 }
522 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
523 * Sending functions: *
524 * *
525 * ping_send_all *
526 * +-> ping_send_one_ipv4 *
527 * `-> ping_send_one_ipv6 *
528 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
529 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
530 const void *buf, size_t buflen)
531 {
532 ssize_t ret;
534 if (gettimeofday (ph->timer, NULL) == -1)
535 {
536 timerclear (ph->timer);
537 return (-1);
538 }
540 ret = sendto (ph->fd, buf, buflen, 0,
541 (struct sockaddr *) ph->addr, ph->addrlen);
543 if (ret < 0)
544 ping_set_error (obj, "sendto", strerror (errno));
546 return (ret);
547 }
549 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
550 {
551 struct icmp *icmp4;
552 int status;
554 char buf[4096];
555 int buflen;
557 char *data;
558 int datalen;
560 dprintf ("ph->hostname = %s\n", ph->hostname);
562 memset (buf, '\0', sizeof (buf));
563 icmp4 = (struct icmp *) buf;
564 data = (char *) (icmp4 + 1);
566 icmp4->icmp_type = ICMP_ECHO;
567 icmp4->icmp_code = 0;
568 icmp4->icmp_cksum = 0;
569 icmp4->icmp_id = htons (ph->ident);
570 icmp4->icmp_seq = htons (ph->sequence);
572 strcpy (data, PING_DATA);
573 datalen = strlen (data);
575 buflen = datalen + sizeof (struct icmp);
577 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
579 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
581 status = ping_sendto (obj, ph, buf, buflen);
582 if (status < 0)
583 {
584 perror ("ping_sendto");
585 return (-1);
586 }
588 dprintf ("sendto: status = %i\n", status);
590 return (0);
591 }
593 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
594 {
595 struct icmp6_hdr *icmp6;
596 int status;
598 char buf[4096];
599 int buflen;
601 char *data;
602 int datalen;
604 dprintf ("ph->hostname = %s\n", ph->hostname);
606 memset (buf, '\0', sizeof (buf));
607 icmp6 = (struct icmp6_hdr *) buf;
608 data = (char *) (icmp6 + 1);
610 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
611 icmp6->icmp6_code = 0;
612 /* The checksum will be calculated by the TCP/IP stack. */
613 icmp6->icmp6_cksum = 0;
614 icmp6->icmp6_id = htons (ph->ident);
615 icmp6->icmp6_seq = htons (ph->sequence);
617 strcpy (data, PING_DATA);
618 datalen = strlen (data);
620 buflen = datalen + sizeof (struct icmp6_hdr);
622 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
624 status = ping_sendto (obj, ph, buf, buflen);
625 if (status < 0)
626 {
627 perror ("ping_sendto");
628 return (-1);
629 }
631 dprintf ("sendto: status = %i\n", status);
633 return (0);
634 }
636 static int ping_send_all (pingobj_t *obj)
637 {
638 pinghost_t *ph;
639 pinghost_t *ptr;
641 int ret;
643 ret = 0;
644 ph = obj->head;
646 for (ptr = ph; ptr != NULL; ptr = ptr->next)
647 {
648 /* start timer.. The GNU `ping6' starts the timer before
649 * sending the packet, so I will do that too */
650 if (gettimeofday (ptr->timer, NULL) == -1)
651 {
652 dprintf ("gettimeofday: %s\n", strerror (errno));
653 timerclear (ptr->timer);
654 ret--;
655 continue;
656 }
657 else
658 {
659 dprintf ("timer set for hostname = %s\n", ptr->hostname);
660 }
662 if (ptr->addrfamily == AF_INET6)
663 {
664 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
665 if (ping_send_one_ipv6 (obj, ptr) != 0)
666 {
667 timerclear (ptr->timer);
668 ret--;
669 continue;
670 }
671 }
672 else if (ptr->addrfamily == AF_INET)
673 {
674 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
675 if (ping_send_one_ipv4 (obj, ptr) != 0)
676 {
677 timerclear (ptr->timer);
678 ret--;
679 continue;
680 }
681 }
682 else /* this should not happen */
683 {
684 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
685 timerclear (ptr->timer);
686 ret--;
687 continue;
688 }
690 ptr->sequence++;
691 }
693 return (ret);
694 }
696 /*
697 * Set the TTL of a socket protocol independently.
698 */
699 static int ping_set_ttl (pinghost_t *ph, int ttl)
700 {
701 int ret = -2;
703 if (ph->addrfamily == AF_INET)
704 {
705 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
706 }
707 else if (ph->addrfamily == AF_INET6)
708 {
709 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
710 }
712 return (ret);
713 }
715 static int ping_get_ident (void)
716 {
717 int fd;
718 static int did_seed = 0;
720 int retval;
722 if (did_seed == 0)
723 {
724 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
725 {
726 unsigned int seed;
728 if (read (fd, &seed, sizeof (seed)) != -1)
729 {
730 did_seed = 1;
731 dprintf ("Random seed: %i\n", seed);
732 srandom (seed);
733 }
735 close (fd);
736 }
737 else
738 {
739 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
740 }
741 }
743 retval = (int) random ();
745 dprintf ("Random number: %i\n", retval);
747 return (retval);
748 }
750 static pinghost_t *ping_alloc (void)
751 {
752 pinghost_t *ph;
753 size_t ph_size;
755 ph_size = sizeof (pinghost_t)
756 + sizeof (struct sockaddr_storage)
757 + sizeof (struct timeval);
759 ph = (pinghost_t *) malloc (ph_size);
760 if (ph == NULL)
761 return (NULL);
763 memset (ph, '\0', ph_size);
765 ph->timer = (struct timeval *) (ph + 1);
766 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
768 ph->addrlen = sizeof (struct sockaddr_storage);
769 ph->latency = -1.0;
770 ph->ident = ping_get_ident () & 0xFFFF;
772 return (ph);
773 }
775 static void ping_free (pinghost_t *ph)
776 {
777 if (ph->hostname != NULL)
778 free (ph->hostname);
780 free (ph);
781 }
783 /*
784 * public methods
785 */
786 const char *ping_get_error (pingobj_t *obj)
787 {
788 return (obj->errmsg);
789 }
791 pingobj_t *ping_construct (void)
792 {
793 pingobj_t *obj;
795 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
796 return (NULL);
797 memset (obj, '\0', sizeof (pingobj_t));
799 obj->timeout = PING_DEF_TIMEOUT;
800 obj->ttl = PING_DEF_TTL;
801 obj->addrfamily = PING_DEF_AF;
803 return (obj);
804 }
806 void ping_destroy (pingobj_t *obj)
807 {
808 pinghost_t *current;
809 pinghost_t *next;
811 current = obj->head;
812 next = NULL;
814 while (current != NULL)
815 {
816 next = current->next;
817 ping_free (current);
818 current = next;
819 }
821 free (obj);
823 return;
824 }
826 int ping_setopt (pingobj_t *obj, int option, void *value)
827 {
828 int ret = 0;
830 switch (option)
831 {
832 case PING_OPT_TIMEOUT:
833 obj->timeout = *((double *) value);
834 if (obj->timeout < 0.0)
835 {
836 obj->timeout = PING_DEF_TIMEOUT;
837 ret = -1;
838 }
839 break;
841 case PING_OPT_TTL:
842 obj->ttl = *((int *) value);
843 if ((obj->ttl < 1) || (obj->ttl > 255))
844 {
845 obj->ttl = PING_DEF_TTL;
846 ret = -1;
847 }
848 break;
850 case PING_OPT_AF:
851 obj->addrfamily = *((int *) value);
852 if ((obj->addrfamily != AF_UNSPEC)
853 && (obj->addrfamily != AF_INET)
854 && (obj->addrfamily != AF_INET6))
855 {
856 obj->addrfamily = PING_DEF_AF;
857 ret = -1;
858 }
859 break;
861 default:
862 ret = -2;
863 } /* switch (option) */
865 return (ret);
866 } /* int ping_setopt */
869 int ping_send (pingobj_t *obj)
870 {
871 int ret;
873 if (ping_send_all (obj) < 0)
874 return (-1);
876 if ((ret = ping_receive_all (obj)) < 0)
877 return (-2);
879 return (ret);
880 }
882 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
883 {
884 while (ph != NULL)
885 {
886 if (strcasecmp (ph->hostname, host) == 0)
887 break;
889 ph = ph->next;
890 }
892 return (ph);
893 }
895 int ping_host_add (pingobj_t *obj, const char *host)
896 {
897 pinghost_t *ph;
899 struct sockaddr_storage sockaddr;
900 socklen_t sockaddr_len;
902 struct addrinfo ai_hints;
903 struct addrinfo *ai_list, *ai_ptr;
904 int ai_return;
906 dprintf ("host = %s\n", host);
908 if (ping_host_search (obj->head, host) != NULL)
909 return (0);
911 memset (&ai_hints, '\0', sizeof (ai_hints));
912 ai_hints.ai_flags = 0;
913 #ifdef AI_ADDRCONFIG
914 ai_hints.ai_flags |= AI_ADDRCONFIG;
915 #endif
916 ai_hints.ai_family = obj->addrfamily;
917 ai_hints.ai_socktype = SOCK_RAW;
919 if ((ph = ping_alloc ()) == NULL)
920 {
921 dprintf ("Out of memory!\n");
922 return (-1);
923 }
925 if ((ph->hostname = strdup (host)) == NULL)
926 {
927 dprintf ("Out of memory!\n");
928 ping_set_error (obj, "strdup", strerror (errno));
929 ping_free (ph);
930 return (-1);
931 }
933 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
934 {
935 dprintf ("getaddrinfo failed\n");
936 ping_set_error (obj, "getaddrinfo",
937 (ai_return == EAI_SYSTEM)
938 ? strerror (errno)
939 : gai_strerror (ai_return));
940 ping_free (ph);
941 return (-1);
942 }
944 if (ai_list == NULL)
945 ping_set_error (obj, "getaddrinfo", "No hosts returned");
947 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
948 {
949 ph->fd = -1;
951 sockaddr_len = sizeof (sockaddr);
952 memset (&sockaddr, '\0', sockaddr_len);
954 if (ai_ptr->ai_family == AF_INET)
955 {
956 struct sockaddr_in *si;
958 si = (struct sockaddr_in *) &sockaddr;
959 si->sin_family = AF_INET;
960 si->sin_port = htons (ph->ident);
961 si->sin_addr.s_addr = htonl (INADDR_ANY);
963 ai_ptr->ai_socktype = SOCK_RAW;
964 ai_ptr->ai_protocol = IPPROTO_ICMP;
965 }
966 else if (ai_ptr->ai_family == AF_INET6)
967 {
968 struct sockaddr_in6 *si;
970 si = (struct sockaddr_in6 *) &sockaddr;
971 si->sin6_family = AF_INET6;
972 si->sin6_port = htons (ph->ident);
973 si->sin6_addr = in6addr_any;
975 ai_ptr->ai_socktype = SOCK_RAW;
976 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
977 }
978 else
979 {
980 char errmsg[PING_ERRMSG_LEN];
982 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
983 errmsg[PING_ERRMSG_LEN - 1] = '\0';
985 dprintf (errmsg);
986 ping_set_error (obj, "getaddrinfo", errmsg);
987 continue;
988 }
990 /* TODO: Move this to a static function `ping_open_socket' and
991 * call it whenever the socket dies. */
992 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
993 if (ph->fd == -1)
994 {
995 dprintf ("socket: %s\n", strerror (errno));
996 ping_set_error (obj, "socket", strerror (errno));
997 continue;
998 }
1000 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
1001 {
1002 dprintf ("bind: %s\n", strerror (errno));
1003 ping_set_error (obj, "bind", strerror (errno));
1004 close (ph->fd);
1005 ph->fd = -1;
1006 continue;
1007 }
1009 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1010 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1011 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1012 ph->addrlen = ai_ptr->ai_addrlen;
1013 ph->addrfamily = ai_ptr->ai_family;
1015 break;
1016 }
1018 freeaddrinfo (ai_list);
1020 if (ph->fd < 0)
1021 {
1022 free (ph->hostname);
1023 free (ph);
1024 return (-1);
1025 }
1027 ph->next = obj->head;
1028 obj->head = ph;
1030 ping_set_ttl (ph, obj->ttl);
1032 return (0);
1033 }
1035 int ping_host_remove (pingobj_t *obj, const char *host)
1036 {
1037 pinghost_t *pre, *cur;
1039 pre = NULL;
1040 cur = obj->head;
1042 while (cur != NULL)
1043 {
1044 if (strcasecmp (host, cur->hostname))
1045 break;
1047 pre = cur;
1048 cur = cur->next;
1049 }
1051 if (cur == NULL)
1052 {
1053 ping_set_error (obj, "ping_host_remove", "Host not found");
1054 return (-1);
1055 }
1057 if (pre == NULL)
1058 obj->head = cur->next;
1059 else
1060 pre->next = cur->next;
1062 if (cur->fd >= 0)
1063 close (cur->fd);
1065 ping_free (cur);
1067 return (0);
1068 }
1070 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1071 {
1072 return ((pingobj_iter_t *) obj->head);
1073 }
1075 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1076 {
1077 return ((pingobj_iter_t *) iter->next);
1078 }
1080 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1081 void *buffer, size_t *buffer_len)
1082 {
1083 int ret = EINVAL;
1085 size_t orig_buffer_len = *buffer_len;
1087 switch (info)
1088 {
1089 case PING_INFO_HOSTNAME:
1090 ret = ENOMEM;
1091 *buffer_len = strlen (iter->hostname);
1092 if (orig_buffer_len <= *buffer_len)
1093 break;
1094 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1095 * will copy `*buffer_len' and pad the rest of
1096 * `buffer' with null-bytes */
1097 strncpy (buffer, iter->hostname, orig_buffer_len);
1098 ret = 0;
1099 break;
1101 case PING_INFO_ADDRESS:
1102 ret = getnameinfo ((struct sockaddr *) iter->addr,
1103 iter->addrlen,
1104 (char *) buffer,
1105 *buffer_len,
1106 NULL, 0,
1107 NI_NUMERICHOST);
1108 if (ret != 0)
1109 {
1110 if ((ret == EAI_MEMORY)
1111 #ifdef EAI_OVERFLOW
1112 || (ret == EAI_OVERFLOW)
1113 #endif
1114 )
1115 ret = ENOMEM;
1116 else if (ret == EAI_SYSTEM)
1117 /* XXX: Not thread-safe! */
1118 ret = errno;
1119 else
1120 ret = EINVAL;
1121 }
1122 break;
1124 case PING_INFO_FAMILY:
1125 ret = ENOMEM;
1126 *buffer_len = sizeof (int);
1127 if (orig_buffer_len < sizeof (int))
1128 break;
1129 *((int *) buffer) = iter->addrfamily;
1130 ret = 0;
1131 break;
1133 case PING_INFO_LATENCY:
1134 ret = ENOMEM;
1135 *buffer_len = sizeof (double);
1136 if (orig_buffer_len < sizeof (double))
1137 break;
1138 *((double *) buffer) = iter->latency;
1139 ret = 0;
1140 break;
1142 /* FIXME Return the sequence as an unsigned int */
1143 case PING_INFO_SEQUENCE:
1144 ret = ENOMEM;
1145 *buffer_len = sizeof (uint16_t);
1146 if (orig_buffer_len < sizeof (uint16_t))
1147 break;
1148 *((uint16_t *) buffer) = (uint16_t) iter->sequence;
1149 ret = 0;
1150 break;
1152 case PING_INFO_IDENT:
1153 ret = ENOMEM;
1154 *buffer_len = sizeof (uint16_t);
1155 if (orig_buffer_len < sizeof (uint16_t))
1156 break;
1157 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1158 ret = 0;
1159 break;
1160 }
1162 return (ret);
1163 }
1165 void *ping_iterator_get_context (pingobj_iter_t *iter)
1166 {
1167 return (iter->context);
1168 }
1170 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1171 {
1172 iter->context = context;
1173 }