Code

Use `AI_CANONNAME' if it's defined.
[liboping.git] / src / 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  */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
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 */
34 #if HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
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
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
50 # include <time.h>
51 #else
52 # if HAVE_SYS_TIME_H
53 #  include <sys/time.h>
54 # else
55 #  include <time.h>
56 # endif
57 #endif
59 #if HAVE_SYS_SOCKET_H
60 # include <sys/socket.h>
61 #endif
62 #if HAVE_NETDB_H
63 # include <netdb.h>
64 #endif
66 #if HAVE_NETINET_IN_SYSTM_H
67 # include <netinet/in_systm.h>
68 #endif
69 #if HAVE_NETINET_IN_H
70 # include <netinet/in.h>
71 #endif
72 #if HAVE_NETINET_IP_H
73 # include <netinet/ip.h>
74 #endif
75 #if HAVE_NETINET_IP_ICMP_H
76 # include <netinet/ip_icmp.h>
77 #endif
78 #ifdef HAVE_NETINET_IP_VAR_H
79 # include <netinet/ip_var.h>
80 #endif
81 #if HAVE_NETINET_IP6_H
82 # include <netinet/ip6.h>
83 #endif
84 #if HAVE_NETINET_ICMP6_H
85 # include <netinet/icmp6.h>
86 #endif
88 #include "oping.h"
90 #if WITH_DEBUG
91 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
92 #else
93 # define dprintf(...) /**/
94 #endif
96 #define PING_ERRMSG_LEN 256
98 struct pinghost
99 {
100         char                    *hostname;
101         struct sockaddr_storage *addr;
102         socklen_t                addrlen;
103         int                      addrfamily;
104         int                      fd;
105         int                      ident;
106         int                      sequence;
107         struct timeval          *timer;
108         double                   latency;
109         char                    *data;
111         void                    *context;
113         struct pinghost         *next;
114 };
116 struct pingobj
118         double      timeout;
119         int         ttl;
120         int         addrfamily;
121         char       *data;
123         char        errmsg[PING_ERRMSG_LEN];
125         pinghost_t *head;
126 };
128 /*
129  * private (static) functions
130  */
131 static void ping_set_error (pingobj_t *obj, const char *function,
132                 const char *message)
134         snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message);
135         obj->errmsg[PING_ERRMSG_LEN - 1] = '\0';
138 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
139                 struct timeval *res)
141         res->tv_sec  = tv1->tv_sec  + tv2->tv_sec;
142         res->tv_usec = tv1->tv_usec + tv2->tv_usec;
144         while (res->tv_usec > 1000000)
145         {
146                 res->tv_usec -= 1000000;
147                 res->tv_sec++;
148         }
150         return (0);
153 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
154                 struct timeval *res)
157         if ((tv1->tv_sec < tv2->tv_sec)
158                         || ((tv1->tv_sec == tv2->tv_sec)
159                                 && (tv1->tv_usec < tv2->tv_usec)))
160                 return (-1);
162         res->tv_sec  = tv1->tv_sec  - tv2->tv_sec;
163         res->tv_usec = tv1->tv_usec - tv2->tv_usec;
165         assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
167         while (res->tv_usec < 0)
168         {
169                 res->tv_usec += 1000000;
170                 res->tv_sec--;
171         }
173         return (0);
176 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
178         uint32_t sum = 0;
179         uint16_t ret = 0;
181         uint16_t *ptr;
183         for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
184                 sum += *ptr;
186         if (len == 1)
187         {
188                 *(char *) &ret = *(char *) ptr;
189                 sum += ret;
190         }
192         /* Do this twice to get all possible carries.. */
193         sum = (sum >> 16) + (sum & 0xFFFF);
194         sum = (sum >> 16) + (sum & 0xFFFF);
196         ret = ~sum;
198         return (ret);
201 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
203         struct ip *ip_hdr;
204         struct icmp *icmp_hdr;
206         size_t ip_hdr_len;
208         uint16_t recv_checksum;
209         uint16_t calc_checksum;
211         uint16_t ident;
212         uint16_t seq;
214         pinghost_t *ptr;
216         if (buffer_len < sizeof (struct ip))
217                 return (NULL);
219         ip_hdr     = (struct ip *) buffer;
220         ip_hdr_len = ip_hdr->ip_hl << 2;
222         if (buffer_len < ip_hdr_len)
223                 return (NULL);
225         buffer     += ip_hdr_len;
226         buffer_len -= ip_hdr_len;
228         if (buffer_len < sizeof (struct icmp))
229                 return (NULL);
231         icmp_hdr = (struct icmp *) buffer;
232         buffer     += sizeof (struct icmp);
233         buffer_len -= sizeof (struct icmp);
235         if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
236         {
237                 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
238                 return (NULL);
239         }
241         recv_checksum = icmp_hdr->icmp_cksum;
242         icmp_hdr->icmp_cksum = 0;
243         calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
244                         sizeof (struct icmp) + buffer_len);
246         if (recv_checksum != calc_checksum)
247         {
248                 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
249                                 recv_checksum, calc_checksum);
250                 return (NULL);
251         }
253         ident = ntohs (icmp_hdr->icmp_id);
254         seq   = ntohs (icmp_hdr->icmp_seq);
256         for (ptr = ph; ptr != NULL; ptr = ptr->next)
257         {
258                 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
259                                 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
261                 if (ptr->addrfamily != AF_INET)
262                         continue;
264                 if (!timerisset (ptr->timer))
265                         continue;
267                 if (ptr->ident != ident)
268                         continue;
270                 if (((ptr->sequence - 1) & 0xFFFF) != seq)
271                         continue;
273                 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
274                                 ptr->hostname, ident, seq);
276                 break;
277         }
279         if (ptr == NULL)
280         {
281                 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
282                                 ident, seq);
283         }
285         return (ptr);
288 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
290         struct icmp6_hdr *icmp_hdr;
292         uint16_t ident;
293         uint16_t seq;
295         pinghost_t *ptr;
297         if (buffer_len < sizeof (struct icmp6_hdr))
298                 return (NULL);
300         icmp_hdr = (struct icmp6_hdr *) buffer;
301         buffer     += sizeof (struct icmp);
302         buffer_len -= sizeof (struct icmp);
304         if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
305         {
306                 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
307                 return (NULL);
308         }
310         if (icmp_hdr->icmp6_code != 0)
311         {
312                 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
313                 return (NULL);
314         }
316         ident = ntohs (icmp_hdr->icmp6_id);
317         seq   = ntohs (icmp_hdr->icmp6_seq);
319         for (ptr = ph; ptr != NULL; ptr = ptr->next)
320         {
321                 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
322                                 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
324                 if (ptr->addrfamily != AF_INET6)
325                         continue;
327                 if (!timerisset (ptr->timer))
328                         continue;
330                 if (ptr->ident != ident)
331                         continue;
333                 if (((ptr->sequence - 1) & 0xFFFF) != seq)
334                         continue;
336                 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
337                                 ptr->hostname, ident, seq);
339                 break;
340         }
342         if (ptr == NULL)
343         {
344                 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
345                                 ident, seq);
346         }
348         return (ptr);
351 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
353         char   buffer[4096];
354         size_t buffer_len;
356         struct timeval diff;
358         pinghost_t *host = NULL;
360         struct sockaddr_storage sa;
361         socklen_t               sa_len;
363         sa_len = sizeof (sa);
365         buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
366                         (struct sockaddr *) &sa, &sa_len);
367         if (buffer_len == -1)
368         {
369                 dprintf ("recvfrom: %s\n", strerror (errno));
370                 return (-1);
371         }
373         dprintf ("Read %u bytes from fd = %i\n", (unsigned int) buffer_len, fd);
375         if (sa.ss_family == AF_INET)
376         {
377                 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
378                         return (-1);
379         }
380         else if (sa.ss_family == AF_INET6)
381         {
382                 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
383                         return (-1);
384         }
386         dprintf ("rcvd: %12i.%06i\n",
387                         (int) now->tv_sec,
388                         (int) now->tv_usec);
389         dprintf ("sent: %12i.%06i\n",
390                         (int) host->timer->tv_sec,
391                         (int) host->timer->tv_usec);
393         if (ping_timeval_sub (now, host->timer, &diff) < 0)
394         {
395                 timerclear (host->timer);
396                 return (-1);
397         }
399         dprintf ("diff: %12i.%06i\n",
400                         (int) diff.tv_sec,
401                         (int) diff.tv_usec);
403         host->latency  = ((double) diff.tv_usec) / 1000.0;
404         host->latency += ((double) diff.tv_sec)  * 1000.0;
406         timerclear (host->timer);
408         return (0);
411 static int ping_receive_all (pingobj_t *obj)
413         fd_set readfds;
414         int num_readfds;
415         int max_readfds;
417         pinghost_t *ph;
418         pinghost_t *ptr;
420         struct timeval endtime;
421         struct timeval nowtime;
422         struct timeval timeout;
423         int status;
425         int ret;
427         ph = obj->head;
428         ret = 0;
430         for (ptr = ph; ptr != NULL; ptr = ptr->next)
431                 ptr->latency = -1.0;
433         if (gettimeofday (&nowtime, NULL) == -1)
434         {
435                 ping_set_error (obj, "gettimeofday", strerror (errno));
436                 return (-1);
437         }
439         /* Set up timeout */
440         timeout.tv_sec = (time_t) obj->timeout;
441         timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
443         dprintf ("Set timeout to %i.%06i seconds\n",
444                         (int) timeout.tv_sec,
445                         (int) timeout.tv_usec);
447         ping_timeval_add (&nowtime, &timeout, &endtime);
449         while (1)
450         {
451                 FD_ZERO (&readfds);
452                 num_readfds =  0;
453                 max_readfds = -1;
455                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
456                 {
457                         if (!timerisset (ptr->timer))
458                                 continue;
460                         FD_SET (ptr->fd, &readfds);
461                         num_readfds++;
463                         if (max_readfds < ptr->fd)
464                                 max_readfds = ptr->fd;
465                 }
467                 if (num_readfds == 0)
468                         break;
470                 if (gettimeofday (&nowtime, NULL) == -1)
471                 {
472                         ping_set_error (obj, "gettimeofday", strerror (errno));
473                         return (-1);
474                 }
476                 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
477                         break;
479                 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
480                                 (int) timeout.tv_sec,
481                                 (int) timeout.tv_usec);
483                 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
485                 if (gettimeofday (&nowtime, NULL) == -1)
486                 {
487                         ping_set_error (obj, "gettimeofday", strerror (errno));
488                         return (-1);
489                 }
490                 
491                 if ((status == -1) && (errno == EINTR))
492                 {
493                         dprintf ("select was interrupted by signal..\n");
494                         continue;
495                 }
496                 else if (status < 0)
497                 {
498                         dprintf ("select: %s\n", strerror (errno));
499                         break;
500                 }
501                 else if (status == 0)
502                 {
503                         dprintf ("select timed out\n");
504                         break;
505                 }
507                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
508                 {
509                         if (FD_ISSET (ptr->fd, &readfds))
510                                 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
511                                         ret++;
512                 }
513         } /* while (1) */
514         
515         return (ret);
518 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
519  * Sending functions:                                                        *
520  *                                                                           *
521  * ping_send_all                                                             *
522  * +-> ping_send_one_ipv4                                                    *
523  * `-> ping_send_one_ipv6                                                    *
524  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
525 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
526                 const void *buf, size_t buflen)
528         ssize_t ret;
530         if (gettimeofday (ph->timer, NULL) == -1)
531         {
532                 timerclear (ph->timer);
533                 return (-1);
534         }
536         ret = sendto (ph->fd, buf, buflen, 0,
537                         (struct sockaddr *) ph->addr, ph->addrlen);
539         if (ret < 0)
540                 ping_set_error (obj, "sendto", strerror (errno));
542         return (ret);
545 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
547         struct icmp *icmp4;
548         int status;
550         char buf[4096];
551         int  buflen;
553         char *data;
554         int   datalen;
556         dprintf ("ph->hostname = %s\n", ph->hostname);
558         memset (buf, '\0', sizeof (buf));
559         icmp4 = (struct icmp *) buf;
560         data  = (char *) (icmp4 + 1);
562         icmp4->icmp_type  = ICMP_ECHO;
563         icmp4->icmp_code  = 0;
564         icmp4->icmp_cksum = 0;
565         icmp4->icmp_id    = htons (ph->ident);
566         icmp4->icmp_seq   = htons (ph->sequence);
568         buflen = 4096 - sizeof (struct icmp);
569         strncpy (data, ph->data, buflen);
570         datalen = strlen (data);
572         buflen = datalen + sizeof (struct icmp);
574         icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
576         dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
578         status = ping_sendto (obj, ph, buf, buflen);
579         if (status < 0)
580         {
581                 perror ("ping_sendto");
582                 return (-1);
583         }
585         dprintf ("sendto: status = %i\n", status);
587         return (0);
590 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
592         struct icmp6_hdr *icmp6;
593         int status;
595         char buf[4096];
596         int  buflen;
598         char *data;
599         int   datalen;
601         dprintf ("ph->hostname = %s\n", ph->hostname);
603         memset (buf, '\0', sizeof (buf));
604         icmp6 = (struct icmp6_hdr *) buf;
605         data  = (char *) (icmp6 + 1);
607         icmp6->icmp6_type  = ICMP6_ECHO_REQUEST;
608         icmp6->icmp6_code  = 0;
609         /* The checksum will be calculated by the TCP/IP stack.  */
610         /* FIXME */
611         icmp6->icmp6_cksum = 0;
612         icmp6->icmp6_id    = htons (ph->ident);
613         icmp6->icmp6_seq   = htons (ph->sequence);
615         buflen = 4096 - sizeof (struct icmp6_hdr);
616         strncpy (data, ph->data, buflen);
617         datalen = strlen (data);
619         buflen = datalen + sizeof (struct icmp6_hdr);
621         dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
623         status = ping_sendto (obj, ph, buf, buflen);
624         if (status < 0)
625         {
626                 perror ("ping_sendto");
627                 return (-1);
628         }
630         dprintf ("sendto: status = %i\n", status);
632         return (0);
635 static int ping_send_all (pingobj_t *obj)
637         pinghost_t *ph;
638         pinghost_t *ptr;
640         int ret;
642         ret = 0;
643         ph = obj->head;
645         for (ptr = ph; ptr != NULL; ptr = ptr->next)
646         {
647                 /* start timer.. The GNU `ping6' starts the timer before
648                  * sending the packet, so I will do that too */
649                 if (gettimeofday (ptr->timer, NULL) == -1)
650                 {
651                         dprintf ("gettimeofday: %s\n", strerror (errno));
652                         timerclear (ptr->timer);
653                         ret--;
654                         continue;
655                 }
656                 else
657                 {
658                         dprintf ("timer set for hostname = %s\n", ptr->hostname);
659                 }
661                 if (ptr->addrfamily == AF_INET6)
662                 {       
663                         dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
664                         if (ping_send_one_ipv6 (obj, ptr) != 0)
665                         {
666                                 timerclear (ptr->timer);
667                                 ret--;
668                                 continue;
669                         }
670                 }
671                 else if (ptr->addrfamily == AF_INET)
672                 {
673                         dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
674                         if (ping_send_one_ipv4 (obj, ptr) != 0)
675                         {
676                                 timerclear (ptr->timer);
677                                 ret--;
678                                 continue;
679                         }
680                 }
681                 else /* this should not happen */
682                 {
683                         dprintf ("Unknown address family: %i\n", ptr->addrfamily);
684                         timerclear (ptr->timer);
685                         ret--;
686                         continue;
687                 }
689                 ptr->sequence++;
690         }
692         return (ret);
695 /*
696  * Set the TTL of a socket protocol independently.
697  */
698 static int ping_set_ttl (pinghost_t *ph, int ttl)
700         int ret = -2;
702         if (ph->addrfamily == AF_INET)
703         {
704                 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
705         }
706         else if (ph->addrfamily == AF_INET6)
707         {
708                 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
709         }
711         return (ret);
714 static int ping_get_ident (void)
716         int fd;
717         static int did_seed = 0;
719         int retval;
721         if (did_seed == 0)
722         {
723                 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
724                 {
725                         unsigned int seed;
727                         if (read (fd, &seed, sizeof (seed)) != -1)
728                         {
729                                 did_seed = 1;
730                                 dprintf ("Random seed: %i\n", seed);
731                                 srandom (seed);
732                         }
734                         close (fd);
735                 }
736                 else
737                 {
738                         dprintf ("open (/dev/urandom): %s\n", strerror (errno));
739                 }
740         }
742         retval = (int) random ();
744         dprintf ("Random number: %i\n", retval);
745         
746         return (retval);
749 static pinghost_t *ping_alloc (void)
751         pinghost_t *ph;
752         size_t      ph_size;
754         ph_size = sizeof (pinghost_t)
755                 + sizeof (struct sockaddr_storage)
756                 + sizeof (struct timeval);
758         ph = (pinghost_t *) malloc (ph_size);
759         if (ph == NULL)
760                 return (NULL);
762         memset (ph, '\0', ph_size);
764         ph->timer   = (struct timeval *) (ph + 1);
765         ph->addr    = (struct sockaddr_storage *) (ph->timer + 1);
767         ph->addrlen = sizeof (struct sockaddr_storage);
768         ph->latency = -1.0;
769         ph->ident   = ping_get_ident () & 0xFFFF;
771         return (ph);
774 static void ping_free (pinghost_t *ph)
776         if (ph->hostname != NULL)
777                 free (ph->hostname);
779         if (ph->data != NULL)
780                 free (ph->data);
782         free (ph);
785 /*
786  * public methods
787  */
788 const char *ping_get_error (pingobj_t *obj)
790         return (obj->errmsg);
793 pingobj_t *ping_construct (void)
795         pingobj_t *obj;
797         if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
798                 return (NULL);
799         memset (obj, '\0', sizeof (pingobj_t));
801         obj->timeout    = PING_DEF_TIMEOUT;
802         obj->ttl        = PING_DEF_TTL;
803         obj->addrfamily = PING_DEF_AF;
804         obj->data       = strdup (PING_DEF_DATA);
806         return (obj);
809 void ping_destroy (pingobj_t *obj)
811         pinghost_t *current;
812         pinghost_t *next;
814         current = obj->head;
815         next = NULL;
817         while (current != NULL)
818         {
819                 next = current->next;
820                 ping_free (current);
821                 current = next;
822         }
824         if (obj->data != NULL)
825                 free (obj->data);
827         free (obj);
829         return;
832 int ping_setopt (pingobj_t *obj, int option, void *value)
834         int ret = 0;
836         switch (option)
837         {
838                 case PING_OPT_TIMEOUT:
839                         obj->timeout = *((double *) value);
840                         if (obj->timeout < 0.0)
841                         {
842                                 obj->timeout = PING_DEF_TIMEOUT;
843                                 ret = -1;
844                         }
845                         break;
847                 case PING_OPT_TTL:
848                         obj->ttl = *((int *) value);
849                         if ((obj->ttl < 1) || (obj->ttl > 255))
850                         {
851                                 obj->ttl = PING_DEF_TTL;
852                                 ret = -1;
853                         }
854                         break;
856                 case PING_OPT_AF:
857                         obj->addrfamily = *((int *) value);
858                         if ((obj->addrfamily != AF_UNSPEC)
859                                         && (obj->addrfamily != AF_INET)
860                                         && (obj->addrfamily != AF_INET6))
861                         {
862                                 obj->addrfamily = PING_DEF_AF;
863                                 ret = -1;
864                         }
865                         break;
867                 case PING_OPT_DATA:
868                         if (obj->data != NULL)
869                         {
870                                 free (obj->data);
871                                 obj->data = NULL;
872                         }
873                         obj->data = strdup ((const char *) value);
874                         break;
876                 default:
877                         ret = -2;
878         } /* switch (option) */
880         return (ret);
881 } /* int ping_setopt */
884 int ping_send (pingobj_t *obj)
886         int ret;
888         if (ping_send_all (obj) < 0)
889                 return (-1);
891         if ((ret = ping_receive_all (obj)) < 0)
892                 return (-2);
894         return (ret);
897 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
899         while (ph != NULL)
900         {
901                 if (strcasecmp (ph->hostname, host) == 0)
902                         break;
904                 ph = ph->next;
905         }
907         return (ph);
910 int ping_host_add (pingobj_t *obj, const char *host)
912         pinghost_t *ph;
914         struct sockaddr_storage sockaddr;
915         socklen_t               sockaddr_len;
917         struct addrinfo  ai_hints;
918         struct addrinfo *ai_list, *ai_ptr;
919         int              ai_return;
921         dprintf ("host = %s\n", host);
923         if (ping_host_search (obj->head, host) != NULL)
924                 return (0);
926         memset (&ai_hints, '\0', sizeof (ai_hints));
927         ai_hints.ai_flags     = 0;
928 #ifdef AI_ADDRCONFIG
929         ai_hints.ai_flags    |= AI_ADDRCONFIG;
930 #endif
931 #ifdef AI_CANONNAME
932         ai_hints.ai_flags    |= AI_CANONNAME;
933 #endif
934         ai_hints.ai_family    = obj->addrfamily;
935         ai_hints.ai_socktype  = SOCK_RAW;
937         if ((ph = ping_alloc ()) == NULL)
938         {
939                 dprintf ("Out of memory!\n");
940                 return (-1);
941         }
943         if ((ph->hostname = strdup (host)) == NULL)
944         {
945                 dprintf ("Out of memory!\n");
946                 ping_set_error (obj, "strdup", strerror (errno));
947                 ping_free (ph);
948                 return (-1);
949         }
951         /* obj->data is not garuanteed to be != NULL */
952         if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
953         {
954                 dprintf ("Out of memory!\n");
955                 ping_set_error (obj, "strdup", strerror (errno));
956                 ping_free (ph);
957                 return (-1);
958         }
960         if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
961         {
962                 dprintf ("getaddrinfo failed\n");
963                 ping_set_error (obj, "getaddrinfo",
964                                 (ai_return == EAI_SYSTEM)
965                                 ? strerror (errno)
966                                 : gai_strerror (ai_return));
967                 ping_free (ph);
968                 return (-1);
969         }
971         if (ai_list == NULL)
972                 ping_set_error (obj, "getaddrinfo", "No hosts returned");
974         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
975         {
976                 ph->fd = -1;
978                 sockaddr_len = sizeof (sockaddr);
979                 memset (&sockaddr, '\0', sockaddr_len);
981                 if (ai_ptr->ai_family == AF_INET)
982                 {
983                         struct sockaddr_in *si;
985                         si = (struct sockaddr_in *) &sockaddr;
986                         si->sin_family = AF_INET;
987                         si->sin_port   = htons (ph->ident);
988                         si->sin_addr.s_addr = htonl (INADDR_ANY);
990                         ai_ptr->ai_socktype = SOCK_RAW;
991                         ai_ptr->ai_protocol = IPPROTO_ICMP;
992                 }
993                 else if (ai_ptr->ai_family == AF_INET6)
994                 {
995                         struct sockaddr_in6 *si;
997                         si = (struct sockaddr_in6 *) &sockaddr;
998                         si->sin6_family = AF_INET6;
999                         si->sin6_port   = htons (ph->ident);
1000                         si->sin6_addr   = in6addr_any;
1002                         ai_ptr->ai_socktype = SOCK_RAW;
1003                         ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1004                 }
1005                 else
1006                 {
1007                         char errmsg[PING_ERRMSG_LEN];
1009                         snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1010                         errmsg[PING_ERRMSG_LEN - 1] = '\0';
1012                         dprintf (errmsg);
1013                         ping_set_error (obj, "getaddrinfo", errmsg);
1014                         continue;
1015                 }
1017                 /* TODO: Move this to a static function `ping_open_socket' and
1018                  * call it whenever the socket dies. */
1019                 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1020                 if (ph->fd == -1)
1021                 {
1022                         dprintf ("socket: %s\n", strerror (errno));
1023                         ping_set_error (obj, "socket", strerror (errno));
1024                         continue;
1025                 }
1027 /*
1028  * The majority vote of operating systems has decided that you don't need to
1029  * bind here. This code should be reactivated to bind to a specific address,
1030  * though. See the `-I' option of `ping(1)' (GNU).  -octo
1031  */
1032 #if 0
1033                 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
1034                 {
1035                         dprintf ("bind: %s\n", strerror (errno));
1036                         ping_set_error (obj, "bind", strerror (errno));
1037                         close (ph->fd);
1038                         ph->fd = -1;
1039                         continue;
1040                 }
1041 #endif
1043                 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1044                 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1045                 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1046                 ph->addrlen = ai_ptr->ai_addrlen;
1047                 ph->addrfamily = ai_ptr->ai_family;
1049 #ifdef AI_CANONNAME
1050                 if ((ai_ptr->ai_canonname != NULL)
1051                                 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1052                 {
1053                         char *old_hostname;
1055                         dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1056                                         ph->hostname, ai_ptr->ai_canonname);
1058                         old_hostname = ph->hostname;
1059                         if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1060                         {
1061                                 /* strdup failed, falling back to old hostname */
1062                                 ph->hostname = old_hostname;
1063                         }
1064                         else if (old_hostname != NULL)
1065                         {
1066                                 free (old_hostname);
1067                         }
1068                 }
1069 #endif /* AI_CANONNAME */
1071                 break;
1072         }
1074         freeaddrinfo (ai_list);
1076         if (ph->fd < 0)
1077         {
1078                 free (ph->hostname);
1079                 free (ph);
1080                 return (-1);
1081         }
1083         /*
1084          * Adding in the front is much easier, but then the iterator will
1085          * return the host that was added last as first host. That's just not
1086          * nice. -octo
1087          */
1088         if (obj->head == NULL)
1089         {
1090                 obj->head = ph;
1091         }
1092         else
1093         {
1094                 pinghost_t *hptr;
1096                 hptr = obj->head;
1097                 while (hptr->next != NULL)
1098                         hptr = hptr->next;
1100                 assert ((hptr != NULL) && (hptr->next == NULL));
1101                 hptr->next = ph;
1102         }
1104         ping_set_ttl (ph, obj->ttl);
1106         return (0);
1109 int ping_host_remove (pingobj_t *obj, const char *host)
1111         pinghost_t *pre, *cur;
1113         pre = NULL;
1114         cur = obj->head;
1116         while (cur != NULL)
1117         {
1118                 if (strcasecmp (host, cur->hostname))
1119                         break;
1121                 pre = cur;
1122                 cur = cur->next;
1123         }
1125         if (cur == NULL)
1126         {
1127                 ping_set_error (obj, "ping_host_remove", "Host not found");
1128                 return (-1);
1129         }
1131         if (pre == NULL)
1132                 obj->head = cur->next;
1133         else
1134                 pre->next = cur->next;
1135         
1136         if (cur->fd >= 0)
1137                 close (cur->fd);
1139         ping_free (cur);
1141         return (0);
1144 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1146         return ((pingobj_iter_t *) obj->head);
1149 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1151         return ((pingobj_iter_t *) iter->next);
1154 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1155                 void *buffer, size_t *buffer_len)
1157         int ret = EINVAL;
1159         size_t orig_buffer_len = *buffer_len;
1161         switch (info)
1162         {
1163                 case PING_INFO_HOSTNAME:
1164                         ret = ENOMEM;
1165                         *buffer_len = strlen (iter->hostname);
1166                         if (orig_buffer_len <= *buffer_len)
1167                                 break;
1168                         /* Since (orig_buffer_len > *buffer_len) `strncpy'
1169                          * will copy `*buffer_len' and pad the rest of
1170                          * `buffer' with null-bytes */
1171                         strncpy (buffer, iter->hostname, orig_buffer_len);
1172                         ret = 0;
1173                         break;
1175                 case PING_INFO_ADDRESS:
1176                         ret = getnameinfo ((struct sockaddr *) iter->addr,
1177                                         iter->addrlen,
1178                                         (char *) buffer,
1179                                         *buffer_len,
1180                                         NULL, 0,
1181                                         NI_NUMERICHOST);
1182                         if (ret != 0)
1183                         {
1184                                 if ((ret == EAI_MEMORY)
1185 #ifdef EAI_OVERFLOW
1186                                                 || (ret == EAI_OVERFLOW)
1187 #endif
1188                                    )
1189                                         ret = ENOMEM;
1190                                 else if (ret == EAI_SYSTEM)
1191                                         /* XXX: Not thread-safe! */
1192                                         ret = errno;
1193                                 else
1194                                         ret = EINVAL;
1195                         }
1196                         break;
1198                 case PING_INFO_FAMILY:
1199                         ret = ENOMEM;
1200                         *buffer_len = sizeof (int);
1201                         if (orig_buffer_len < sizeof (int))
1202                                 break;
1203                         *((int *) buffer) = iter->addrfamily;
1204                         ret = 0;
1205                         break;
1207                 case PING_INFO_LATENCY:
1208                         ret = ENOMEM;
1209                         *buffer_len = sizeof (double);
1210                         if (orig_buffer_len < sizeof (double))
1211                                 break;
1212                         *((double *) buffer) = iter->latency;
1213                         ret = 0;
1214                         break;
1216                 case PING_INFO_SEQUENCE:
1217                         ret = ENOMEM;
1218                         *buffer_len = sizeof (unsigned int);
1219                         if (orig_buffer_len < sizeof (unsigned int))
1220                                 break;
1221                         *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1222                         ret = 0;
1223                         break;
1225                 case PING_INFO_IDENT:
1226                         ret = ENOMEM;
1227                         *buffer_len = sizeof (uint16_t);
1228                         if (orig_buffer_len < sizeof (uint16_t))
1229                                 break;
1230                         *((uint16_t *) buffer) = (uint16_t) iter->ident;
1231                         ret = 0;
1232                         break;
1234                 case PING_INFO_DATA:
1235                         ret = ENOMEM;
1236                         *buffer_len = strlen (iter->data);
1237                         if (orig_buffer_len < *buffer_len)
1238                                 break;
1239                         strncpy ((char *) buffer, iter->data, orig_buffer_len);
1240                         ret = 0;
1241                         break;
1242         }
1244         return (ret);
1247 void *ping_iterator_get_context (pingobj_iter_t *iter)
1249         return (iter->context);
1252 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1254         iter->context = context;