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