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