Merged revisions 495:504, 508:509 and 510:513 from `trunk' to `tags/collectd-3.8.1'
[collectd.git] / src / liboping / liboping.c
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  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
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 */
33
34 #if HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37
38 #if HAVE_FCNTL_H
39 # include <fcntl.h>
40 #endif
41 #if HAVE_SYS_TYPES_H
42 # include <sys/types.h>
43 #endif
44 #if HAVE_SYS_STAT_H
45 # include <sys/stat.h>
46 #endif
47
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
50 # include <time.h>
51 #endif
52
53 #if HAVE_SYS_SOCKET_H
54 # include <sys/socket.h>
55 #endif
56 #if HAVE_NETDB_H
57 # include <netdb.h>
58 #endif
59
60 #if HAVE_NETINET_IN_SYSTM_H
61 # include <netinet/in_systm.h>
62 #endif
63 #if HAVE_NETINET_IN_H
64 # include <netinet/in.h>
65 #endif
66 #if HAVE_NETINET_IP_H
67 # include <netinet/ip.h>
68 #endif
69 #if HAVE_NETINET_IP_ICMP_H
70 # include <netinet/ip_icmp.h>
71 #endif
72 #ifdef HAVE_NETINET_IP_VAR_H
73 # include <netinet/ip_var.h>
74 #endif
75 #if HAVE_NETINET_IP6_H
76 # include <netinet/ip6.h>
77 #endif
78 #if HAVE_NETINET_ICMP6_H
79 # include <netinet/icmp6.h>
80 #endif
81
82 #include "liboping.h"
83
84 #if DEBUG
85 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
86 #else
87 # define dprintf(...) /**/
88 #endif
89
90 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
91
92 /*
93  * private (static) functions
94  */
95 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
96                 struct timeval *res)
97 {
98
99         if ((tv1->tv_sec < tv2->tv_sec)
100                         || ((tv1->tv_sec == tv2->tv_sec)
101                                 && (tv1->tv_usec < tv2->tv_usec)))
102                 return (-1);
103
104         res->tv_sec  = tv1->tv_sec  - tv2->tv_sec;
105         res->tv_usec = tv1->tv_usec - tv2->tv_usec;
106
107         assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
108
109         while (res->tv_usec < 0)
110         {
111                 res->tv_usec += 1000000;
112                 res->tv_sec--;
113         }
114
115         return (0);
116 }
117
118 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
119 {
120         uint32_t sum = 0;
121         uint16_t ret = 0;
122
123         uint16_t *ptr;
124
125         for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
126                 sum += *ptr;
127
128         if (len == 1)
129         {
130                 *(char *) &ret = *(char *) ptr;
131                 sum += ret;
132         }
133
134         /* Do this twice to get all possible carries.. */
135         sum = (sum >> 16) + (sum & 0xFFFF);
136         sum = (sum >> 16) + (sum & 0xFFFF);
137
138         ret = ~sum;
139
140         return (ret);
141 }
142
143 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
144 {
145         struct ip *ip_hdr;
146         struct icmp *icmp_hdr;
147
148         size_t ip_hdr_len;
149
150         uint16_t recv_checksum;
151         uint16_t calc_checksum;
152
153         uint16_t ident;
154         uint16_t seq;
155
156         pinghost_t *ptr;
157
158         if (buffer_len < sizeof (struct ip))
159                 return (NULL);
160
161         ip_hdr     = (struct ip *) buffer;
162         ip_hdr_len = ip_hdr->ip_hl << 2;
163
164         if (buffer_len < ip_hdr_len)
165                 return (NULL);
166
167         buffer     += ip_hdr_len;
168         buffer_len -= ip_hdr_len;
169
170         if (buffer_len < sizeof (struct icmp))
171                 return (NULL);
172
173         icmp_hdr = (struct icmp *) buffer;
174         buffer     += sizeof (struct icmp);
175         buffer_len -= sizeof (struct icmp);
176
177         if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
178         {
179                 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
180                 return (NULL);
181         }
182
183         recv_checksum = icmp_hdr->icmp_cksum;
184         icmp_hdr->icmp_cksum = 0;
185         calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
186                         sizeof (struct icmp) + buffer_len);
187
188         if (recv_checksum != calc_checksum)
189         {
190                 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
191                                 recv_checksum, calc_checksum);
192                 return (NULL);
193         }
194
195         ident = ntohs (icmp_hdr->icmp_id);
196         seq   = ntohs (icmp_hdr->icmp_seq);
197
198         for (ptr = ph; ptr != NULL; ptr = ptr->next)
199         {
200                 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
201                                 ptr->hostname, ptr->ident, ptr->sequence - 1);
202
203                 if (ptr->addrfamily != AF_INET)
204                         continue;
205
206                 if (!timerisset (ptr->timer))
207                         continue;
208
209                 if (ptr->ident != ident)
210                         continue;
211
212                 if ((ptr->sequence - 1) != seq)
213                         continue;
214
215                 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
216                                 ptr->hostname, ident, seq);
217
218                 break;
219         }
220
221         if (ptr == NULL)
222         {
223                 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
224                                 ident, seq);
225         }
226
227         return (ptr);
228 }
229
230 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
231 {
232         struct icmp6_hdr *icmp_hdr;
233
234         uint16_t ident;
235         uint16_t seq;
236
237         pinghost_t *ptr;
238
239         if (buffer_len < sizeof (struct icmp6_hdr))
240                 return (NULL);
241
242         icmp_hdr = (struct icmp6_hdr *) buffer;
243         buffer     += sizeof (struct icmp);
244         buffer_len -= sizeof (struct icmp);
245
246         if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
247         {
248                 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
249                 return (NULL);
250         }
251
252         if (icmp_hdr->icmp6_code != 0)
253         {
254                 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
255                 return (NULL);
256         }
257
258         ident = ntohs (icmp_hdr->icmp6_id);
259         seq   = ntohs (icmp_hdr->icmp6_seq);
260
261         for (ptr = ph; ptr != NULL; ptr = ptr->next)
262         {
263                 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
264                                 ptr->hostname, ptr->ident, ptr->sequence - 1);
265
266                 if (ptr->addrfamily != AF_INET6)
267                         continue;
268
269                 if (!timerisset (ptr->timer))
270                         continue;
271
272                 if (ptr->ident != ident)
273                         continue;
274
275                 if ((ptr->sequence - 1) != seq)
276                         continue;
277
278                 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
279                                 ptr->hostname, ident, seq);
280
281                 break;
282         }
283
284         if (ptr == NULL)
285         {
286                 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
287                                 ident, seq);
288         }
289
290         return (ptr);
291 }
292
293 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
294 {
295         char   buffer[4096];
296         size_t buffer_len;
297
298         struct timeval diff;
299
300         pinghost_t *host = NULL;
301
302         struct sockaddr_storage sa;
303         socklen_t               sa_len;
304
305         sa_len = sizeof (sa);
306
307         buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
308                         (struct sockaddr *) &sa, &sa_len);
309         if (buffer_len == -1)
310         {
311                 dprintf ("recvfrom: %s\n", strerror (errno));
312                 return (-1);
313         }
314
315         dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
316
317         if (sa.ss_family == AF_INET)
318         {
319                 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
320                         return (-1);
321         }
322         else if (sa.ss_family == AF_INET6)
323         {
324                 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
325                         return (-1);
326         }
327
328         dprintf ("rcvd: %12i.%06i\n",
329                         (int) now->tv_sec,
330                         (int) now->tv_usec);
331         dprintf ("sent: %12i.%06i\n",
332                         (int) host->timer->tv_sec,
333                         (int) host->timer->tv_usec);
334
335         if (ping_timeval_sub (now, host->timer, &diff) < 0)
336         {
337                 timerclear (host->timer);
338                 return (-1);
339         }
340
341         dprintf ("diff: %12i.%06i\n",
342                         (int) diff.tv_sec,
343                         (int) diff.tv_usec);
344
345         host->latency  = ((double) diff.tv_usec) / 1000.0;
346         host->latency += ((double) diff.tv_sec)  * 1000.0;
347
348         timerclear (host->timer);
349
350         return (0);
351 }
352
353 static int ping_receive_all (pinghost_t *ph)
354 {
355         fd_set readfds;
356         int num_readfds;
357         int max_readfds;
358
359         pinghost_t *ptr;
360
361         struct timeval endtime;
362         struct timeval nowtime;
363         struct timeval timeout;
364         int status;
365
366         int ret;
367
368         ret = 0;
369
370         for (ptr = ph; ptr != NULL; ptr = ptr->next)
371                 ptr->latency = -1.0;
372
373         if (gettimeofday (&endtime, NULL) == -1)
374                 return (-1);
375         endtime.tv_sec += 1;
376
377         while (1)
378         {
379                 FD_ZERO (&readfds);
380                 num_readfds =  0;
381                 max_readfds = -1;
382
383                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
384                 {
385                         if (!timerisset (ptr->timer))
386                                 continue;
387
388                         FD_SET (ptr->fd, &readfds);
389                         num_readfds++;
390
391                         if (max_readfds < ptr->fd)
392                                 max_readfds = ptr->fd;
393                 }
394
395                 if (num_readfds == 0)
396                         break;
397
398                 if (gettimeofday (&nowtime, NULL) == -1)
399                         return (-1);
400
401                 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
402                         break;
403
404                 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
405                                 (int) timeout.tv_sec,
406                                 (int) timeout.tv_usec);
407
408                 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
409
410                 if (gettimeofday (&nowtime, NULL) == -1)
411                         return (-1);
412                 
413                 if ((status == -1) && (errno == EINTR))
414                 {
415                         dprintf ("select was interrupted by signal..\n");
416                         continue;
417                 }
418                 else if (status < 0)
419                 {
420                         dprintf ("select: %s\n", strerror (errno));
421                         break;
422                 }
423                 else if (status == 0)
424                 {
425                         dprintf ("select timed out\n");
426                         break;
427                 }
428
429                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
430                 {
431                         if (FD_ISSET (ptr->fd, &readfds))
432                                 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
433                                         ret++;
434                 }
435         } /* while (1) */
436         
437         return (ret);
438 }
439
440 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
441  * Sending functions:                                                        *
442  *                                                                           *
443  * ping_send_all                                                             *
444  * +-> ping_send_one_ipv4                                                    *
445  * `-> ping_send_one_ipv6                                                    *
446  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
447 ssize_t ping_sendto (pinghost_t *ph, const void *buf, size_t buflen)
448 {
449         ssize_t ret;
450
451         if (gettimeofday (ph->timer, NULL) == -1)
452         {
453                 timerclear (ph->timer);
454                 return (-1);
455         }
456
457         ret = sendto (ph->fd, buf, buflen, 0,
458                         (struct sockaddr *) ph->addr, ph->addrlen);
459
460         return (ret);
461 }
462
463 static int ping_send_one_ipv4 (pinghost_t *ph)
464 {
465         struct icmp *icmp4;
466         int status;
467
468         char buf[4096];
469         int  buflen;
470
471         char *data;
472         int   datalen;
473
474         dprintf ("ph->hostname = %s\n", ph->hostname);
475
476         memset (buf, '\0', sizeof (buf));
477         icmp4 = (struct icmp *) buf;
478         data  = (char *) (icmp4 + 1);
479
480         icmp4->icmp_type  = ICMP_ECHO;
481         icmp4->icmp_code  = 0;
482         icmp4->icmp_cksum = 0;
483         icmp4->icmp_id    = htons (ph->ident);
484         icmp4->icmp_seq   = htons (ph->sequence);
485
486         strcpy (data, PING_DATA);
487         datalen = strlen (data);
488
489         buflen = datalen + sizeof (struct icmp);
490
491         icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
492
493         dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
494
495         status = ping_sendto (ph, buf, buflen);
496         if (status < 0)
497         {
498                 perror ("ping_sendto");
499                 return (-1);
500         }
501
502         dprintf ("sendto: status = %i\n", status);
503
504         return (0);
505 }
506
507 static int ping_send_one_ipv6 (pinghost_t *ph)
508 {
509         struct icmp6_hdr *icmp6;
510         int status;
511
512         char buf[4096];
513         int  buflen;
514
515         char *data;
516         int   datalen;
517
518         dprintf ("ph->hostname = %s\n", ph->hostname);
519
520         memset (buf, '\0', sizeof (buf));
521         icmp6 = (struct icmp6_hdr *) buf;
522         data  = (char *) (icmp6 + 1);
523
524         icmp6->icmp6_type  = ICMP6_ECHO_REQUEST;
525         icmp6->icmp6_code  = 0;
526         /* The checksum will be calculated by the TCP/IP stack.  */
527         icmp6->icmp6_cksum = 0;
528         icmp6->icmp6_id    = htons (ph->ident);
529         icmp6->icmp6_seq   = htons (ph->sequence);
530
531         strcpy (data, PING_DATA);
532         datalen = strlen (data);
533
534         buflen = datalen + sizeof (struct icmp6_hdr);
535
536         dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
537
538         status = ping_sendto (ph, buf, buflen);
539         if (status < 0)
540         {
541                 perror ("ping_sendto");
542                 return (-1);
543         }
544
545         dprintf ("sendto: status = %i\n", status);
546
547         return (0);
548 }
549
550 static int ping_send_all (pinghost_t *ph)
551 {
552         pinghost_t *ptr;
553
554         for (ptr = ph; ptr != NULL; ptr = ptr->next)
555         {
556                 /* start timer.. The GNU `ping6' starts the timer before
557                  * sending the packet, so I will do that too */
558                 if (gettimeofday (ptr->timer, NULL) == -1)
559                 {
560                         dprintf ("gettimeofday: %s\n", strerror (errno));
561                         timerclear (ptr->timer);
562                         continue;
563                 }
564                 else
565                 {
566                         dprintf ("timer set for hostname = %s\n", ptr->hostname);
567                 }
568
569                 if (ptr->addrfamily == AF_INET6)
570                 {       
571                         dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
572                         if (ping_send_one_ipv6 (ptr) != 0)
573                         {
574                                 timerclear (ptr->timer);
575                                 continue;
576                         }
577                 }
578                 else if (ptr->addrfamily == AF_INET)
579                 {
580                         dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
581                         if (ping_send_one_ipv4 (ptr) != 0)
582                         {
583                                 timerclear (ptr->timer);
584                                 continue;
585                         }
586                 }
587                 else /* this should not happen */
588                 {
589                         dprintf ("Unknown address family: %i\n", ptr->addrfamily);
590                         timerclear (ptr->timer);
591                         continue;
592                 }
593
594                 ptr->sequence++;
595         }
596
597         /* FIXME */
598         return (0);
599 }
600
601 static int ping_get_ident (void)
602 {
603         int fd;
604         static int did_seed = 0;
605
606         int retval;
607
608         if (did_seed == 0)
609         {
610                 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
611                 {
612                         unsigned int seed;
613
614                         if (read (fd, &seed, sizeof (seed)) != -1)
615                         {
616                                 did_seed = 1;
617                                 dprintf ("Random seed: %i\n", seed);
618                                 srandom (seed);
619                         }
620
621                         close (fd);
622                 }
623                 else
624                 {
625                         dprintf ("open (/dev/urandom): %s\n", strerror (errno));
626                 }
627         }
628
629         retval = (int) random ();
630
631         dprintf ("Random number: %i\n", retval);
632         
633         return (retval);
634 }
635
636 static pinghost_t *ping_alloc (void)
637 {
638         pinghost_t *ph;
639         size_t      ph_size;
640
641         ph_size = sizeof (pinghost_t)
642                 + sizeof (struct sockaddr_storage)
643                 + sizeof (struct timeval);
644
645         ph = (pinghost_t *) malloc (ph_size);
646         if (ph == NULL)
647                 return (NULL);
648
649         memset (ph, '\0', ph_size);
650
651         ph->timer   = (struct timeval *) (ph + 1);
652         ph->addr    = (struct sockaddr_storage *) (ph->timer + 1);
653
654         ph->addrlen = sizeof (struct sockaddr_storage);
655         ph->latency = -1.0;
656         ph->ident   = ping_get_ident () & 0xFFFF;
657
658         return (ph);
659 }
660
661 static void ping_free (pinghost_t *ph)
662 {
663         if (ph->hostname != NULL)
664                 free (ph->hostname);
665
666         free (ph);
667 }
668
669 /*
670  * public methods
671  */
672 pingobj_t *ping_construct (void)
673 {
674         pingobj_t *obj;
675
676         if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
677                 return (NULL);
678
679         obj->head = NULL;
680
681         return (obj);
682 }
683
684 void ping_destroy (pingobj_t *obj)
685 {
686         pinghost_t *current;
687         pinghost_t *next;
688
689         current = obj->head;
690         next = NULL;
691
692         while (current != NULL)
693         {
694                 next = current->next;
695                 ping_free (current);
696                 current = next;
697         }
698
699         free (obj);
700
701         return;
702 }
703
704 int ping_setopt (pingobj_t *obj, int option, void *value)
705 {
706         int ret = 0;
707
708         switch (option)
709         {
710                 case PING_OPT_TIMEOUT:
711                         obj->timeout = *((double *) value);
712                         if (obj->timeout < 0.0)
713                         {
714                                 obj->timeout = PING_DEF_TIMEOUT;
715                                 ret = -1;
716                         }
717                         break;
718
719                 case PING_OPT_TTL:
720                         obj->ttl = *((int *) value);
721                         if ((obj->ttl < 1) || (obj->ttl > 255))
722                         {
723                                 obj->ttl = PING_DEF_TTL;
724                                 ret = -1;
725                         }
726                         break;
727
728                 case PING_OPT_AF:
729                         obj->addrfamily = *((int *) value);
730                         if ((obj->addrfamily != AF_UNSPEC)
731                                         && (obj->addrfamily != AF_INET)
732                                         && (obj->addrfamily != AF_INET6))
733                         {
734                                 obj->addrfamily = PING_DEF_AF;
735                                 ret = -1;
736                         }
737                         break;
738
739                 default:
740                         ret = -2;
741         } /* switch (option) */
742
743         return (ret);
744 } /* int ping_setopt */
745
746
747 int ping_send (pingobj_t *obj)
748 {
749         int ret;
750
751         if (ping_send_all (obj->head) < 0)
752                 return (-1);
753
754         if ((ret = ping_receive_all (obj->head)) < 0)
755                 return (-2);
756
757         return (ret);
758 }
759
760 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
761 {
762         while (ph != NULL)
763         {
764                 if (strcasecmp (ph->hostname, host) == 0)
765                         break;
766
767                 ph = ph->next;
768         }
769
770         return (ph);
771 }
772
773 int ping_host_add (pingobj_t *obj, const char *host)
774 {
775         pinghost_t *ph;
776
777         struct sockaddr_storage sockaddr;
778         socklen_t               sockaddr_len;
779
780         struct addrinfo  ai_hints;
781         struct addrinfo *ai_list, *ai_ptr;
782         int              ai_return;
783
784         dprintf ("host = %s\n", host);
785
786         if (ping_host_search (obj->head, host) != NULL)
787                 return (0);
788
789         memset (&ai_hints, '\0', sizeof (ai_hints));
790         ai_hints.ai_flags     = 0;
791 #ifdef AI_ADDRCONFIG
792         ai_hints.ai_flags    |= AI_ADDRCONFIG;
793 #endif
794         ai_hints.ai_family    = PF_UNSPEC;
795         ai_hints.ai_socktype  = SOCK_RAW;
796
797         if ((ph = ping_alloc ()) == NULL)
798         {
799                 dprintf ("Out of memory!\n");
800                 return (-1);
801         }
802
803         if ((ph->hostname = strdup (host)) == NULL)
804         {
805                 dprintf ("Out of memory!\n");
806                 ping_free (ph);
807                 return (-1);
808         }
809
810         if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
811         {
812                 dprintf ("getaddrinfo failed\n");
813                 ping_free (ph);
814                 return (-1);
815         }
816
817         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
818         {
819                 ph->fd = -1;
820
821                 sockaddr_len = sizeof (sockaddr);
822                 memset (&sockaddr, '\0', sockaddr_len);
823
824                 if (ai_ptr->ai_family == AF_INET)
825                 {
826                         struct sockaddr_in *si;
827
828                         si = (struct sockaddr_in *) &sockaddr;
829                         si->sin_family = AF_INET;
830                         si->sin_port   = htons (ph->ident);
831                         si->sin_addr.s_addr = htonl (INADDR_ANY);
832
833                         ai_ptr->ai_protocol = IPPROTO_ICMP;
834                 }
835                 else if (ai_ptr->ai_family == AF_INET6)
836                 {
837                         struct sockaddr_in6 *si;
838
839                         si = (struct sockaddr_in6 *) &sockaddr;
840                         si->sin6_family = AF_INET6;
841                         si->sin6_port   = htons (ph->ident);
842                         si->sin6_addr   = in6addr_any;
843
844                         ai_ptr->ai_protocol = IPPROTO_ICMPV6;
845                 }
846                 else
847                 {
848                         dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
849                         continue;
850                 }
851
852                 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
853                 if (ph->fd == -1)
854                 {
855                         dprintf ("socket: %s\n", strerror (errno));
856                         continue;
857                 }
858
859                 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
860                 {
861                         dprintf ("bind: %s\n", strerror (errno));
862                         close (ph->fd);
863                         ph->fd = -1;
864                         continue;
865                 }
866
867                 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
868                 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
869                 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
870                 ph->addrlen = ai_ptr->ai_addrlen;
871                 ph->addrfamily = ai_ptr->ai_family;
872
873                 break;
874         }
875
876         freeaddrinfo (ai_list);
877
878         if (ph->fd < 0)
879         {
880                 free (ph->hostname);
881                 free (ph);
882                 return (-1);
883         }
884
885         ph->next  = obj->head;
886         obj->head = ph;
887
888         return (0);
889 }
890
891 int ping_host_remove (pingobj_t *obj, const char *host)
892 {
893         pinghost_t *pre, *cur;
894
895         pre = NULL;
896         cur = obj->head;
897
898         while (cur != NULL)
899         {
900                 if (strcasecmp (host, cur->hostname))
901                         break;
902
903                 pre = cur;
904                 cur = cur->next;
905         }
906
907         if (cur == NULL)
908                 return (-1);
909
910         if (pre == NULL)
911                 obj->head = cur->next;
912         else
913                 pre->next = cur->next;
914         
915         if (cur->fd >= 0)
916                 close (cur->fd);
917
918         ping_free (cur);
919
920         return (0);
921 }
922
923 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
924 {
925         return ((pingobj_iter_t *) obj->head);
926 }
927
928 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
929 {
930         return ((pingobj_iter_t *) iter->next);
931 }
932
933 const char *ping_iterator_get_host (pingobj_iter_t *iter)
934 {
935         return (iter->hostname);
936 }
937
938 double ping_iterator_get_latency (pingobj_iter_t *iter)
939 {
940         return (iter->latency);
941 }