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