Implemented first version of `ping_setopt'
[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)
252 {
253         char   buffer[4096];
254         size_t buffer_len;
255
256         struct timeval now;
257         struct timeval diff;
258
259         pinghost_t *host = NULL;
260
261         struct sockaddr_storage sa;
262         socklen_t               sa_len;
263
264         sa_len = sizeof (sa);
265
266         buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
267                         (struct sockaddr *) &sa, &sa_len);
268         if (buffer_len == -1)
269         {
270                 dprintf ("recvfrom: %s\n", strerror (errno));
271                 return (-1);
272         }
273
274         dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
275
276         if (sa.ss_family == AF_INET)
277         {
278                 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
279                         return (-1);
280         }
281         else if (sa.ss_family == AF_INET6)
282         {
283                 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
284                         return (-1);
285         }
286
287         if (gettimeofday (&now, NULL) == -1)
288         {
289                 dprintf ("gettimeofday: %s\n", strerror (errno));
290                 timerclear (host->timer);
291                 return (-1);
292         }
293
294         dprintf ("sent: %12i.%06i\n",
295                         (int) host->timer->tv_sec,
296                         (int) host->timer->tv_usec);
297         dprintf ("rcvd: %12i.%06i\n",
298                         (int) now.tv_sec,
299                         (int) now.tv_usec);
300
301         if (ping_timeval_sub (&now, host->timer, &diff) < 0)
302         {
303                 timerclear (host->timer);
304                 return (-1);
305         }
306
307         dprintf ("diff: %12i.%06i\n",
308                         (int) diff.tv_sec,
309                         (int) diff.tv_usec);
310
311         host->latency  = ((double) diff.tv_usec) / 1000.0;
312         host->latency += ((double) diff.tv_sec)  * 1000.0;
313
314         timerclear (host->timer);
315
316         return (0);
317 }
318
319 static int ping_receive_all (pinghost_t *ph)
320 {
321         fd_set readfds;
322         int num_readfds;
323         int max_readfds;
324
325         pinghost_t *ptr;
326
327         struct timeval endtime;
328         struct timeval nowtime;
329         struct timeval timeout;
330         int status;
331
332         int ret;
333
334         ret = 0;
335
336         if (gettimeofday (&endtime, NULL) == -1)
337                 return (-1);
338         endtime.tv_sec += 1;
339
340         for (ptr = ph; ptr != NULL; ptr = ptr->next)
341                 ptr->latency = -1.0;
342
343         while (1)
344         {
345                 FD_ZERO (&readfds);
346                 num_readfds =  0;
347                 max_readfds = -1;
348
349                 if (gettimeofday (&nowtime, NULL) == -1)
350                         return (-1);
351
352                 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
353                         return (0);
354
355                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
356                 {
357                         if (!timerisset (ptr->timer))
358                                 continue;
359
360                         FD_SET (ptr->fd, &readfds);
361                         num_readfds++;
362
363                         if (max_readfds < ptr->fd)
364                                 max_readfds = ptr->fd;
365                 }
366
367                 if (num_readfds == 0)
368                         break;
369
370                 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
371                                 (int) timeout.tv_sec,
372                                 (int) timeout.tv_usec);
373
374                 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
375                 
376                 if ((status == -1) && (errno == EINTR))
377                 {
378                         dprintf ("select was interrupted by signal..\n");
379                         continue;
380                 }
381                 else if (status < 0)
382                 {
383                         dprintf ("select: %s\n", strerror (errno));
384                         break;
385                 }
386                 else if (status == 0)
387                 {
388                         dprintf ("select timed out\n");
389                         break;
390                 }
391
392                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
393                 {
394                         if (FD_ISSET (ptr->fd, &readfds))
395                                 if (ping_receive_one (ptr->fd, ph) == 0)
396                                         ret++;
397                 }
398         }
399         
400         return (ret);
401 }
402
403 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
404  * Sending functions:                                                        *
405  *                                                                           *
406  * ping_send_all                                                             *
407  * +-> ping_send_one_ipv4                                                    *
408  * `-> ping_send_one_ipv6                                                    *
409  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
410 static int ping_send_one_ipv4 (pinghost_t *ph)
411 {
412         struct icmphdr *icmp4;
413         int status;
414
415         char buf[4096];
416         int  buflen;
417
418         char *data;
419         int   datalen;
420
421         dprintf ("ph->hostname = %s\n", ph->hostname);
422
423         memset (buf, '\0', sizeof (buf));
424         icmp4 = (struct icmphdr *) buf;
425         data  = (char *) (icmp4 + 1);
426
427         icmp4->type             = ICMP_ECHO;
428         icmp4->code             = 0;
429         /* The checksum will be calculated by the TCP/IP stack.  */
430         icmp4->checksum         = 0;
431         icmp4->un.echo.id       = htons (ph->ident);
432         icmp4->un.echo.sequence = htons (ph->sequence);
433
434         strcpy (data, PING_DATA);
435         datalen = strlen (data);
436
437         buflen = datalen + sizeof (struct icmphdr);
438
439         icmp4->checksum = ping_icmp4_checksum (buf, buflen);
440
441         dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
442
443         status = sendto (ph->fd, buf, buflen, 0,
444                         (struct sockaddr *) ph->addr, ph->addrlen);
445         if (status < 0)
446         {
447                 perror ("sendto");
448                 return (-1);
449         }
450
451         dprintf ("sendto: status = %i\n", status);
452
453         return (0);
454 }
455
456 static int ping_send_one_ipv6 (pinghost_t *ph)
457 {
458         struct icmp6_hdr *icmp6;
459         int status;
460
461         char buf[4096];
462         int  buflen;
463
464         char *data;
465         int   datalen;
466
467         dprintf ("ph->hostname = %s\n", ph->hostname);
468
469         memset (buf, '\0', sizeof (buf));
470         icmp6 = (struct icmp6_hdr *) buf;
471         data  = (char *) (icmp6 + 1);
472
473         icmp6->icmp6_type  = ICMP6_ECHO_REQUEST;
474         icmp6->icmp6_code  = 0;
475         /* The checksum will be calculated by the TCP/IP stack.  */
476         icmp6->icmp6_cksum = 0;
477         icmp6->icmp6_id    = htons (ph->ident);
478         icmp6->icmp6_seq   = htons (ph->sequence);
479
480         strcpy (data, PING_DATA);
481         datalen = strlen (data);
482
483         buflen = datalen + sizeof (struct icmp6_hdr);
484
485         dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
486
487         status = sendto (ph->fd, buf, buflen, 0,
488                         (struct sockaddr *) ph->addr, ph->addrlen);
489         if (status < 0)
490         {
491                 perror ("sendto");
492                 return (-1);
493         }
494
495         dprintf ("sendto: status = %i\n", status);
496
497         return (0);
498 }
499
500 static int ping_send_all (pinghost_t *ph)
501 {
502         pinghost_t *ptr;
503
504         for (ptr = ph; ptr != NULL; ptr = ptr->next)
505         {
506                 /* start timer.. The GNU `ping6' starts the timer before
507                  * sending the packet, so I will do that too */
508                 if (gettimeofday (ptr->timer, NULL) == -1)
509                 {
510                         dprintf ("gettimeofday: %s\n", strerror (errno));
511                         timerclear (ptr->timer);
512                         continue;
513                 }
514                 else
515                 {
516                         dprintf ("timer set for hostname = %s\n", ptr->hostname);
517                 }
518
519                 if (ptr->addrfamily == AF_INET6)
520                 {       
521                         dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
522                         if (ping_send_one_ipv6 (ptr) != 0)
523                         {
524                                 timerclear (ptr->timer);
525                                 continue;
526                         }
527                 }
528                 else if (ptr->addrfamily == AF_INET)
529                 {
530                         dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
531                         if (ping_send_one_ipv4 (ptr) != 0)
532                         {
533                                 timerclear (ptr->timer);
534                                 continue;
535                         }
536                 }
537                 else /* this should not happen */
538                 {
539                         dprintf ("Unknown address family: %i\n", ptr->addrfamily);
540                         timerclear (ptr->timer);
541                         continue;
542                 }
543
544                 ptr->sequence++;
545         }
546
547         /* FIXME */
548         return (0);
549 }
550
551 static int ping_get_ident (void)
552 {
553         int fd;
554         static int did_seed = 0;
555
556         int retval;
557
558         if (did_seed == 0)
559         {
560                 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
561                 {
562                         unsigned int seed;
563
564                         if (read (fd, &seed, sizeof (seed)) != -1)
565                         {
566                                 did_seed = 1;
567                                 dprintf ("Random seed: %i\n", seed);
568                                 srandom (seed);
569                         }
570
571                         close (fd);
572                 }
573                 else
574                 {
575                         dprintf ("open (/dev/urandom): %s\n", strerror (errno));
576                 }
577         }
578
579         retval = (int) random ();
580
581         dprintf ("Random number: %i\n", retval);
582         
583         return (retval);
584 }
585
586 static pinghost_t *ping_alloc (void)
587 {
588         pinghost_t *ph;
589         size_t      ph_size;
590
591         ph_size = sizeof (pinghost_t)
592                 + sizeof (struct sockaddr_storage)
593                 + sizeof (struct timeval);
594
595         ph = (pinghost_t *) malloc (ph_size);
596         if (ph == NULL)
597                 return (NULL);
598
599         memset (ph, '\0', ph_size);
600
601         ph->timer   = (struct timeval *) (ph + 1);
602         ph->addr    = (struct sockaddr_storage *) (ph->timer + 1);
603
604         ph->addrlen = sizeof (struct sockaddr_storage);
605         ph->latency = -1.0;
606         ph->ident   = ping_get_ident () & 0xFFFF;
607
608         return (ph);
609 }
610
611 static void ping_free (pinghost_t *ph)
612 {
613         if (ph->hostname != NULL)
614                 free (ph->hostname);
615
616         free (ph);
617 }
618
619 /*
620  * public methods
621  */
622 pingobj_t *ping_construct (void)
623 {
624         pingobj_t *obj;
625
626         if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
627                 return (NULL);
628
629         obj->head = NULL;
630
631         return (obj);
632 }
633
634 void ping_destroy (pingobj_t *obj)
635 {
636         pinghost_t *current;
637         pinghost_t *next;
638
639         current = obj->head;
640         next = NULL;
641
642         while (current != NULL)
643         {
644                 next = current->next;
645                 ping_free (current);
646                 current = next;
647         }
648
649         free (obj);
650
651         return;
652 }
653
654 int ping_setopt (pingobj_t *obj, int option, void *value)
655 {
656         int ret = 0;
657
658         switch (option)
659         {
660                 case PING_OPT_TIMEOUT:
661                         obj->timeout = *((double *) value);
662                         if (obj->timeout < 0.0)
663                         {
664                                 obj->timeout = PING_DEF_TIMEOUT;
665                                 ret = -1;
666                         }
667                         break;
668
669                 case PING_OPT_TTL:
670                         obj->ttl = *((int *) value);
671                         if ((obj->ttl < 1) || (obj->ttl > 255))
672                         {
673                                 obj->ttl = PING_DEF_TTL;
674                                 ret = -1;
675                         }
676                         break;
677
678                 case PING_OPT_AF:
679                         obj->addrfamily = *((int *) value);
680                         if ((obj->addrfamily != AF_UNSPEC)
681                                         && (obj->addrfamily != AF_INET)
682                                         && (obj->addrfamily != AF_INET6))
683                         {
684                                 obj->addrfamily = PING_DEF_AF;
685                                 ret = -1;
686                         }
687                         break;
688
689                 default:
690                         ret = -2;
691         } /* switch (option) */
692
693         return (ret);
694 } /* int ping_setopt */
695
696
697 int ping_send (pingobj_t *obj)
698 {
699         int ret;
700
701         if (ping_send_all (obj->head) < 0)
702                 return (-1);
703
704         if ((ret = ping_receive_all (obj->head)) < 0)
705                 return (-2);
706
707         return (ret);
708 }
709
710 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
711 {
712         while (ph != NULL)
713         {
714                 if (strcasecmp (ph->hostname, host) == 0)
715                         break;
716
717                 ph = ph->next;
718         }
719
720         return (ph);
721 }
722
723 int ping_host_add (pingobj_t *obj, const char *host)
724 {
725         pinghost_t *ph;
726
727         struct sockaddr_storage sockaddr;
728         socklen_t               sockaddr_len;
729
730         struct addrinfo  ai_hints;
731         struct addrinfo *ai_list, *ai_ptr;
732         int              ai_return;
733
734         dprintf ("host = %s\n", host);
735
736         if (ping_host_search (obj->head, host) != NULL)
737                 return (0);
738
739         memset (&ai_hints, '\0', sizeof (ai_hints));
740         ai_hints.ai_flags     = 0;
741 #ifdef AI_ADDRCONFIG
742         ai_hints.ai_flags    |= AI_ADDRCONFIG;
743 #endif
744         ai_hints.ai_family    = PF_UNSPEC;
745         ai_hints.ai_socktype  = SOCK_RAW;
746
747         if ((ph = ping_alloc ()) == NULL)
748         {
749                 dprintf ("Out of memory!\n");
750                 return (-1);
751         }
752
753         if ((ph->hostname = strdup (host)) == NULL)
754         {
755                 dprintf ("Out of memory!\n");
756                 ping_free (ph);
757                 return (-1);
758         }
759
760         if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
761         {
762                 dprintf ("getaddrinfo failed\n");
763                 ping_free (ph);
764                 return (-1);
765         }
766
767         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
768         {
769                 ph->fd = -1;
770
771                 sockaddr_len = sizeof (sockaddr);
772                 memset (&sockaddr, '\0', sockaddr_len);
773
774                 if (ai_ptr->ai_family == AF_INET)
775                 {
776                         struct sockaddr_in *si;
777
778                         si = (struct sockaddr_in *) &sockaddr;
779                         si->sin_family = AF_INET;
780                         si->sin_port   = htons (ph->ident);
781                         si->sin_addr.s_addr = htonl (INADDR_ANY);
782
783                         ai_ptr->ai_protocol = IPPROTO_ICMP;
784                 }
785                 else if (ai_ptr->ai_family == AF_INET6)
786                 {
787                         struct sockaddr_in6 *si;
788
789                         si = (struct sockaddr_in6 *) &sockaddr;
790                         si->sin6_family = AF_INET6;
791                         si->sin6_port   = htons (ph->ident);
792                         si->sin6_addr   = in6addr_any;
793
794                         ai_ptr->ai_protocol = IPPROTO_ICMPV6;
795                 }
796                 else
797                 {
798                         dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
799                         continue;
800                 }
801
802                 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
803                 if (ph->fd == -1)
804                 {
805                         dprintf ("socket: %s\n", strerror (errno));
806                         continue;
807                 }
808
809                 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
810                 {
811                         dprintf ("bind: %s\n", strerror (errno));
812                         close (ph->fd);
813                         ph->fd = -1;
814                         continue;
815                 }
816
817                 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
818                 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
819                 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
820                 ph->addrlen = ai_ptr->ai_addrlen;
821                 ph->addrfamily = ai_ptr->ai_family;
822
823                 break;
824         }
825
826         freeaddrinfo (ai_list);
827
828         if (ph->fd < 0)
829         {
830                 free (ph->hostname);
831                 free (ph);
832                 return (-1);
833         }
834
835         ph->next  = obj->head;
836         obj->head = ph;
837
838         return (0);
839 }
840
841 int ping_host_remove (pingobj_t *obj, const char *host)
842 {
843         pinghost_t *pre, *cur;
844
845         pre = NULL;
846         cur = obj->head;
847
848         while (cur != NULL)
849         {
850                 if (strcasecmp (host, cur->hostname))
851                         break;
852
853                 pre = cur;
854                 cur = cur->next;
855         }
856
857         if (cur == NULL)
858                 return (-1);
859
860         if (pre == NULL)
861                 obj->head = cur->next;
862         else
863                 pre->next = cur->next;
864         
865         if (cur->fd >= 0)
866                 close (cur->fd);
867
868         ping_free (cur);
869
870         return (0);
871 }
872
873 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
874 {
875         return ((pingobj_iter_t *) obj->head);
876 }
877
878 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
879 {
880         return ((pingobj_iter_t *) iter->next);
881 }
882
883 const char *ping_iterator_get_host (pingobj_iter_t *iter)
884 {
885         return (iter->hostname);
886 }
887
888 double ping_iterator_get_latency (pingobj_iter_t *iter)
889 {
890         return (iter->latency);
891 }