src/liboping.c: Let ping_setopt() return -1 if 'value' is NULL.
[liboping.git] / src / liboping.c
1 /**
2  * Object oriented C module to send ICMP and ICMPv6 `echo's.
3  * Copyright (C) 2006-2009  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; only version 2 of the License is
8  * applicable.
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 <inttypes.h>
29 # include <errno.h>
30 # include <assert.h>
31 #else
32 # error "You don't have the standard C99 header files installed"
33 #endif /* STDC_HEADERS */
34
35 #ifdef HAVE_STDINT_H
36 # include <stdint.h>
37 #endif
38
39 #if HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42
43 #if HAVE_FCNTL_H
44 # include <fcntl.h>
45 #endif
46 #if HAVE_SYS_TYPES_H
47 # include <sys/types.h>
48 #endif
49 #if HAVE_SYS_STAT_H
50 # include <sys/stat.h>
51 #endif
52
53 #if TIME_WITH_SYS_TIME
54 # include <sys/time.h>
55 # include <time.h>
56 #else
57 # if HAVE_SYS_TIME_H
58 #  include <sys/time.h>
59 # else
60 #  include <time.h>
61 # endif
62 #endif
63
64 #if HAVE_SYS_SOCKET_H
65 # include <sys/socket.h>
66 #endif
67
68 #if HAVE_NETDB_H
69 # include <netdb.h>
70 #endif
71
72 #if HAVE_NETINET_IN_SYSTM_H
73 # include <netinet/in_systm.h>
74 #endif
75 #if HAVE_NETINET_IN_H
76 # include <netinet/in.h>
77 #endif
78 #if HAVE_NETINET_IP_H
79 # include <netinet/ip.h>
80 #endif
81 #if HAVE_NETINET_IP_ICMP_H
82 # include <netinet/ip_icmp.h>
83 #endif
84 #ifdef HAVE_NETINET_IP_VAR_H
85 # include <netinet/ip_var.h>
86 #endif
87 #if HAVE_NETINET_IP6_H
88 # include <netinet/ip6.h>
89 #endif
90 #if HAVE_NETINET_ICMP6_H
91 # include <netinet/icmp6.h>
92 #endif
93
94 #include "oping.h"
95
96 #if WITH_DEBUG
97 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
98 #else
99 # define dprintf(...) /**/
100 #endif
101
102 #define PING_ERRMSG_LEN 256
103
104 struct pinghost
105 {
106         /* username: name passed in by the user */
107         char                    *username;
108         /* hostname: name returned by the reverse lookup */
109         char                    *hostname;
110         struct sockaddr_storage *addr;
111         socklen_t                addrlen;
112         int                      addrfamily;
113         int                      fd;
114         int                      ident;
115         int                      sequence;
116         struct timeval          *timer;
117         double                   latency;
118         uint32_t                 dropped;
119         int                      recv_ttl;
120         char                    *data;
121
122         void                    *context;
123
124         struct pinghost         *next;
125 };
126
127 struct pingobj
128 {
129         double                   timeout;
130         int                      ttl;
131         int                      addrfamily;
132         char                    *data;
133
134         struct sockaddr         *srcaddr;
135         socklen_t                srcaddrlen;
136
137         char                     errmsg[PING_ERRMSG_LEN];
138
139         pinghost_t              *head;
140 };
141
142 /*
143  * private (static) functions
144  */
145 /* Even though Posix requires "strerror_r" to return an "int",
146  * some systems (e.g. the GNU libc) return a "char *" _and_
147  * ignore the second argument ... -tokkee */
148 static char *sstrerror (int errnum, char *buf, size_t buflen)
149 {
150         buf[0] = 0;
151
152 #if !HAVE_STRERROR_R
153         {
154                 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
155         }
156 /* #endif !HAVE_STRERROR_R */
157
158 #elif STRERROR_R_CHAR_P
159         {
160                 char *temp;
161                 temp = strerror_r (errnum, buf, buflen);
162                 if (buf[0] == 0)
163                 {
164                         if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
165                                 strncpy (buf, temp, buflen);
166                         else
167                                 strncpy (buf, "strerror_r did not return "
168                                                 "an error message", buflen);
169                 }
170         }
171 /* #endif STRERROR_R_CHAR_P */
172
173 #else
174         if (strerror_r (errnum, buf, buflen) != 0)
175         {
176                 snprintf (buf, buflen, "Error %i (%#x); "
177                                 "Additionally, strerror_r failed.",
178                                 errnum, errnum);
179         }
180 #endif /* STRERROR_R_CHAR_P */
181
182         buf[buflen - 1] = 0;
183
184         return (buf);
185 } /* char *sstrerror */
186
187 static void ping_set_error (pingobj_t *obj, const char *function,
188                 const char *message)
189 {
190         snprintf (obj->errmsg, sizeof (obj->errmsg),
191                         "%s: %s", function, message);
192         obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
193 }
194
195 static void ping_set_errno (pingobj_t *obj, int error_number)
196 {
197         sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
198 }
199
200 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
201                 struct timeval *res)
202 {
203         res->tv_sec  = tv1->tv_sec  + tv2->tv_sec;
204         res->tv_usec = tv1->tv_usec + tv2->tv_usec;
205
206         while (res->tv_usec > 1000000)
207         {
208                 res->tv_usec -= 1000000;
209                 res->tv_sec++;
210         }
211
212         return (0);
213 }
214
215 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
216                 struct timeval *res)
217 {
218         if ((tv1->tv_sec < tv2->tv_sec)
219                         || ((tv1->tv_sec == tv2->tv_sec)
220                                 && (tv1->tv_usec < tv2->tv_usec)))
221                 return (-1);
222
223         res->tv_sec  = tv1->tv_sec  - tv2->tv_sec;
224         res->tv_usec = tv1->tv_usec - tv2->tv_usec;
225
226         assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
227
228         while (res->tv_usec < 0)
229         {
230                 res->tv_usec += 1000000;
231                 res->tv_sec--;
232         }
233
234         return (0);
235 }
236
237 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
238 {
239         uint32_t sum = 0;
240         uint16_t ret = 0;
241
242         uint16_t *ptr;
243
244         for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
245                 sum += *ptr;
246
247         if (len == 1)
248         {
249                 *(char *) &ret = *(char *) ptr;
250                 sum += ret;
251         }
252
253         /* Do this twice to get all possible carries.. */
254         sum = (sum >> 16) + (sum & 0xFFFF);
255         sum = (sum >> 16) + (sum & 0xFFFF);
256
257         ret = ~sum;
258
259         return (ret);
260 }
261
262 static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
263                 size_t buffer_len)
264 {
265         struct ip *ip_hdr;
266         struct icmp *icmp_hdr;
267
268         size_t ip_hdr_len;
269
270         uint16_t recv_checksum;
271         uint16_t calc_checksum;
272
273         uint16_t ident;
274         uint16_t seq;
275
276         pinghost_t *ptr;
277
278         if (buffer_len < sizeof (struct ip))
279                 return (NULL);
280
281         ip_hdr     = (struct ip *) buffer;
282         ip_hdr_len = ip_hdr->ip_hl << 2;
283
284         if (buffer_len < ip_hdr_len)
285                 return (NULL);
286
287         buffer     += ip_hdr_len;
288         buffer_len -= ip_hdr_len;
289
290         if (buffer_len < sizeof (struct icmp))
291                 return (NULL);
292
293         icmp_hdr = (struct icmp *) buffer;
294         buffer     += sizeof (struct icmp);
295         buffer_len -= sizeof (struct icmp);
296
297         if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
298         {
299                 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
300                 return (NULL);
301         }
302
303         recv_checksum = icmp_hdr->icmp_cksum;
304         icmp_hdr->icmp_cksum = 0;
305         calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
306                         sizeof (struct icmp) + buffer_len);
307
308         if (recv_checksum != calc_checksum)
309         {
310                 dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
311                                 "calculated 0x%04"PRIx16"\n",
312                                 recv_checksum, calc_checksum);
313                 return (NULL);
314         }
315
316         ident = ntohs (icmp_hdr->icmp_id);
317         seq   = ntohs (icmp_hdr->icmp_seq);
318
319         /* We have to iterate over all hosts, since ICMPv4 packets may
320          * be received on any raw v4 socket. */
321         for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
322         {
323                 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
324                                 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
325
326                 if (ptr->addrfamily != AF_INET)
327                         continue;
328
329                 if (!timerisset (ptr->timer))
330                         continue;
331
332                 if (ptr->ident != ident)
333                         continue;
334
335                 if (((ptr->sequence - 1) & 0xFFFF) != seq)
336                         continue;
337
338                 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
339                                 "seq = %"PRIu16"\n",
340                                 ptr->hostname, ident, seq);
341
342                 break;
343         }
344
345         if (ptr == NULL)
346         {
347                 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
348                                 ident, seq);
349         }
350
351         if (ptr != NULL)
352                 ptr->recv_ttl = ip_hdr->ip_ttl;
353
354         return (ptr);
355 }
356
357 #ifndef ICMP6_ECHO_REQUEST
358 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
359 #  define ICMP6_ECHO_REQUEST ICMP6_ECHO
360 # else
361 #  define ICMP6_ECHO_REQUEST 128
362 # endif
363 #endif
364
365 #ifndef ICMP6_ECHO_REPLY
366 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
367 #  define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
368 # else
369 #  define ICMP6_ECHO_REPLY 129
370 # endif
371 #endif
372
373 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
374                 size_t buffer_len)
375 {
376         struct icmp6_hdr *icmp_hdr;
377
378         uint16_t ident;
379         uint16_t seq;
380
381         pinghost_t *ptr;
382
383         if (buffer_len < sizeof (struct icmp6_hdr))
384                 return (NULL);
385
386         icmp_hdr = (struct icmp6_hdr *) buffer;
387         buffer     += sizeof (struct icmp);
388         buffer_len -= sizeof (struct icmp);
389
390         if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
391         {
392                 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
393                 return (NULL);
394         }
395
396         if (icmp_hdr->icmp6_code != 0)
397         {
398                 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
399                 return (NULL);
400         }
401
402         ident = ntohs (icmp_hdr->icmp6_id);
403         seq   = ntohs (icmp_hdr->icmp6_seq);
404
405         /* We have to iterate over all hosts, since ICMPv6 packets may
406          * be received on any raw v6 socket. */
407         for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
408         {
409                 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
410                                 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
411
412                 if (ptr->addrfamily != AF_INET6)
413                         continue;
414
415                 if (!timerisset (ptr->timer))
416                         continue;
417
418                 if (ptr->ident != ident)
419                         continue;
420
421                 if (((ptr->sequence - 1) & 0xFFFF) != seq)
422                         continue;
423
424                 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
425                                 "seq = %"PRIu16"\n",
426                                 ptr->hostname, ident, seq);
427
428                 break;
429         }
430
431         if (ptr == NULL)
432         {
433                 dprintf ("No match found for ident = 0x%04"PRIx16", "
434                                 "seq = %"PRIu16"\n",
435                                 ident, seq);
436         }
437
438         return (ptr);
439 }
440
441 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
442                 struct timeval *now)
443 {
444         /* Note: 'ph' is not necessarily the host object for which we receive a
445          * reply. The right object will be returned by ping_receive_ipv*(). For
446          * now, we can only rely on ph->fd and ph->addrfamily. */
447
448         struct timeval diff;
449         pinghost_t *host = NULL;
450         int recv_ttl;
451         
452         /*
453          * Set up the receive buffer..
454          */
455         struct msghdr msghdr;
456         struct cmsghdr *cmsg;
457         char payload_buffer[4096];
458         ssize_t payload_buffer_len;
459         char control_buffer[4096];
460         struct iovec payload_iovec;
461
462         memset (&payload_iovec, 0, sizeof (payload_iovec));
463         payload_iovec.iov_base = payload_buffer;
464         payload_iovec.iov_len = sizeof (payload_buffer);
465
466         memset (&msghdr, 0, sizeof (msghdr));
467         /* unspecified source address */
468         msghdr.msg_name = NULL;
469         msghdr.msg_namelen = 0;
470         /* output buffer vector, see readv(2) */
471         msghdr.msg_iov = &payload_iovec;
472         msghdr.msg_iovlen = 1;
473         /* output buffer for control messages */
474         msghdr.msg_control = control_buffer;
475         msghdr.msg_controllen = sizeof (control_buffer);
476         /* flags; this is an output only field.. */
477         msghdr.msg_flags = 0;
478 #ifdef MSG_XPG4_2
479         msghdr.msg_flags |= MSG_XPG4_2;
480 #endif
481
482         payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
483         if (payload_buffer_len < 0)
484         {
485 #if WITH_DEBUG
486                 char errbuf[PING_ERRMSG_LEN];
487                 dprintf ("recvfrom: %s\n",
488                                 sstrerror (errno, errbuf, sizeof (errbuf)));
489 #endif
490                 return (-1);
491         }
492         dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
493
494         /* Iterate over all auxiliary data in msghdr */
495         recv_ttl = -1;
496         for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
497                         cmsg != NULL;
498                         cmsg = CMSG_NXTHDR (&msghdr, cmsg))
499         {
500                 if (ph->addrfamily == AF_INET) /* {{{ */
501                 {
502                         if (cmsg->cmsg_level != IPPROTO_IP)
503                                 continue;
504
505                         if (cmsg->cmsg_type == IP_TTL)
506                         {
507                                 memcpy (&recv_ttl, CMSG_DATA (cmsg),
508                                                 sizeof (recv_ttl));
509                                 dprintf ("TTLv4 = %i;\n", recv_ttl);
510                         }
511                         else
512                         {
513                                 dprintf ("Not handling option %i.\n",
514                                                 cmsg->cmsg_type);
515                         }
516                 } /* }}} */
517                 else if (ph->addrfamily == AF_INET6) /* {{{ */
518                 {
519                         if (cmsg->cmsg_level != IPPROTO_IPV6)
520                                 continue;
521
522                         if (cmsg->cmsg_type == IPV6_HOPLIMIT)
523                         {
524                                 memcpy (&recv_ttl, CMSG_DATA (cmsg),
525                                                 sizeof (recv_ttl));
526                                 dprintf ("TTLv6 = %i;\n", recv_ttl);
527                         }
528                         else
529                         {
530                                 dprintf ("Not handling option %i.\n",
531                                                 cmsg->cmsg_type);
532                         }
533                 } /* }}} */
534                 else
535                 {
536                         dprintf ("Don't know how to handle "
537                                         "unknown protocol %i.\n",
538                                         cmsg->cmsg_level);
539                 }
540         } /* }}} for (cmsg) */
541
542         if (ph->addrfamily == AF_INET)
543         {
544                 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
545                 if (host == NULL)
546                         return (-1);
547         }
548         else if (ph->addrfamily == AF_INET6)
549         {
550                 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
551                 if (host == NULL)
552                         return (-1);
553         }
554         else
555         {
556                 dprintf ("ping_receive_one: Unknown address family %i.\n",
557                                 ph->addrfamily);
558                 return (-1);
559         }
560
561         dprintf ("rcvd: %12i.%06i\n",
562                         (int) now->tv_sec,
563                         (int) now->tv_usec);
564         dprintf ("sent: %12i.%06i\n",
565                         (int) host->timer->tv_sec,
566                         (int) host->timer->tv_usec);
567
568         if (ping_timeval_sub (now, host->timer, &diff) < 0)
569         {
570                 timerclear (host->timer);
571                 return (-1);
572         }
573
574         dprintf ("diff: %12i.%06i\n",
575                         (int) diff.tv_sec,
576                         (int) diff.tv_usec);
577
578         if (recv_ttl >= 0)
579                 host->recv_ttl = recv_ttl;
580
581         host->latency  = ((double) diff.tv_usec) / 1000.0;
582         host->latency += ((double) diff.tv_sec)  * 1000.0;
583
584         timerclear (host->timer);
585
586         return (0);
587 }
588
589 static int ping_receive_all (pingobj_t *obj)
590 {
591         fd_set readfds;
592         int num_readfds;
593         int max_readfds;
594
595         pinghost_t *ph;
596         pinghost_t *ptr;
597
598         struct timeval endtime;
599         struct timeval nowtime;
600         struct timeval timeout;
601         int status;
602
603         int ret;
604
605         ph = obj->head;
606         ret = 0;
607
608         for (ptr = ph; ptr != NULL; ptr = ptr->next)
609         {
610                 ptr->latency  = -1.0;
611                 ptr->recv_ttl = -1;
612         }
613
614         if (gettimeofday (&nowtime, NULL) == -1)
615         {
616                 ping_set_errno (obj, errno);
617                 return (-1);
618         }
619
620         /* Set up timeout */
621         timeout.tv_sec = (time_t) obj->timeout;
622         timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
623
624         dprintf ("Set timeout to %i.%06i seconds\n",
625                         (int) timeout.tv_sec,
626                         (int) timeout.tv_usec);
627
628         ping_timeval_add (&nowtime, &timeout, &endtime);
629
630         while (1)
631         {
632                 FD_ZERO (&readfds);
633                 num_readfds =  0;
634                 max_readfds = -1;
635
636                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
637                 {
638                         if (!timerisset (ptr->timer))
639                                 continue;
640
641                         FD_SET (ptr->fd, &readfds);
642                         num_readfds++;
643
644                         if (max_readfds < ptr->fd)
645                                 max_readfds = ptr->fd;
646                 }
647
648                 if (num_readfds == 0)
649                         break;
650
651                 if (gettimeofday (&nowtime, NULL) == -1)
652                 {
653                         ping_set_errno (obj, errno);
654                         return (-1);
655                 }
656
657                 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
658                         break;
659
660                 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
661                                 (int) timeout.tv_sec,
662                                 (int) timeout.tv_usec);
663
664                 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
665
666                 if (gettimeofday (&nowtime, NULL) == -1)
667                 {
668                         ping_set_errno (obj, errno);
669                         return (-1);
670                 }
671                 
672                 if ((status == -1) && (errno == EINTR))
673                 {
674                         dprintf ("select was interrupted by signal..\n");
675                         continue;
676                 }
677                 else if (status < 0)
678                 {
679 #if WITH_DEBUG
680                         char errbuf[PING_ERRMSG_LEN];
681                         dprintf ("select: %s\n",
682                                         sstrerror (errno, errbuf, sizeof (errbuf)));
683 #endif
684                         break;
685                 }
686                 else if (status == 0)
687                 {
688                         dprintf ("select timed out\n");
689                         for (ptr = ph; ptr != NULL; ptr = ptr->next)
690                                 if (ptr->latency < 0.0)
691                                         ptr->dropped++;
692                         break;
693                 }
694
695                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
696                 {
697                         if (FD_ISSET (ptr->fd, &readfds))
698                                 if (ping_receive_one (obj, ptr, &nowtime) == 0)
699                                         ret++;
700                 }
701         } /* while (1) */
702         
703         return (ret);
704 }
705
706 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
707  * Sending functions:                                                        *
708  *                                                                           *
709  * ping_send_all                                                             *
710  * +-> ping_send_one_ipv4                                                    *
711  * `-> ping_send_one_ipv6                                                    *
712  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
713 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
714                 const void *buf, size_t buflen)
715 {
716         ssize_t ret;
717
718         if (gettimeofday (ph->timer, NULL) == -1)
719         {
720                 timerclear (ph->timer);
721                 return (-1);
722         }
723
724         ret = sendto (ph->fd, buf, buflen, 0,
725                         (struct sockaddr *) ph->addr, ph->addrlen);
726
727         if (ret < 0)
728         {
729 #if defined(EHOSTUNREACH)
730                 if (errno == EHOSTUNREACH)
731                         return (0);
732 #endif
733 #if defined(ENETUNREACH)
734                 if (errno == ENETUNREACH)
735                         return (0);
736 #endif
737                 ping_set_errno (obj, errno);
738         }
739
740         return (ret);
741 }
742
743 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
744 {
745         struct icmp *icmp4;
746         int status;
747
748         char buf[4096];
749         int  buflen;
750
751         char *data;
752         int   datalen;
753
754         dprintf ("ph->hostname = %s\n", ph->hostname);
755
756         memset (buf, '\0', sizeof (buf));
757         icmp4 = (struct icmp *) buf;
758         data  = (char *) (icmp4 + 1);
759
760         icmp4->icmp_type  = ICMP_ECHO;
761         icmp4->icmp_code  = 0;
762         icmp4->icmp_cksum = 0;
763         icmp4->icmp_id    = htons (ph->ident);
764         icmp4->icmp_seq   = htons (ph->sequence);
765
766         buflen = 4096 - sizeof (struct icmp);
767         strncpy (data, ph->data, buflen);
768         datalen = strlen (data);
769
770         buflen = datalen + sizeof (struct icmp);
771
772         icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
773
774         dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
775
776         status = ping_sendto (obj, ph, buf, buflen);
777         if (status < 0)
778         {
779                 perror ("ping_sendto");
780                 return (-1);
781         }
782
783         dprintf ("sendto: status = %i\n", status);
784
785         return (0);
786 }
787
788 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
789 {
790         struct icmp6_hdr *icmp6;
791         int status;
792
793         char buf[4096];
794         int  buflen;
795
796         char *data;
797         int   datalen;
798
799         dprintf ("ph->hostname = %s\n", ph->hostname);
800
801         memset (buf, '\0', sizeof (buf));
802         icmp6 = (struct icmp6_hdr *) buf;
803         data  = (char *) (icmp6 + 1);
804
805         icmp6->icmp6_type  = ICMP6_ECHO_REQUEST;
806         icmp6->icmp6_code  = 0;
807         /* The checksum will be calculated by the TCP/IP stack.  */
808         /* FIXME */
809         icmp6->icmp6_cksum = 0;
810         icmp6->icmp6_id    = htons (ph->ident);
811         icmp6->icmp6_seq   = htons (ph->sequence);
812
813         buflen = 4096 - sizeof (struct icmp6_hdr);
814         strncpy (data, ph->data, buflen);
815         datalen = strlen (data);
816
817         buflen = datalen + sizeof (struct icmp6_hdr);
818
819         dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
820
821         status = ping_sendto (obj, ph, buf, buflen);
822         if (status < 0)
823         {
824                 perror ("ping_sendto");
825                 return (-1);
826         }
827
828         dprintf ("sendto: status = %i\n", status);
829
830         return (0);
831 }
832
833 static int ping_send_all (pingobj_t *obj)
834 {
835         pinghost_t *ph;
836         pinghost_t *ptr;
837
838         int ret;
839
840         ret = 0;
841         ph = obj->head;
842
843         for (ptr = ph; ptr != NULL; ptr = ptr->next)
844         {
845                 /* start timer.. The GNU `ping6' starts the timer before
846                  * sending the packet, so I will do that too */
847                 if (gettimeofday (ptr->timer, NULL) == -1)
848                 {
849 #if WITH_DEBUG
850                         char errbuf[PING_ERRMSG_LEN];
851                         dprintf ("gettimeofday: %s\n",
852                                         sstrerror (errno, errbuf, sizeof (errbuf)));
853 #endif
854                         timerclear (ptr->timer);
855                         ret--;
856                         continue;
857                 }
858                 else
859                 {
860                         dprintf ("timer set for hostname = %s\n", ptr->hostname);
861                 }
862
863                 if (ptr->addrfamily == AF_INET6)
864                 {       
865                         dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
866                         if (ping_send_one_ipv6 (obj, ptr) != 0)
867                         {
868                                 timerclear (ptr->timer);
869                                 ret--;
870                                 continue;
871                         }
872                 }
873                 else if (ptr->addrfamily == AF_INET)
874                 {
875                         dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
876                         if (ping_send_one_ipv4 (obj, ptr) != 0)
877                         {
878                                 timerclear (ptr->timer);
879                                 ret--;
880                                 continue;
881                         }
882                 }
883                 else /* this should not happen */
884                 {
885                         dprintf ("Unknown address family: %i\n", ptr->addrfamily);
886                         timerclear (ptr->timer);
887                         ret--;
888                         continue;
889                 }
890
891                 ptr->sequence++;
892         }
893
894         return (ret);
895 }
896
897 /*
898  * Set the TTL of a socket protocol independently.
899  */
900 static int ping_set_ttl (pinghost_t *ph, int ttl)
901 {
902         int ret = -2;
903
904         if (ph->addrfamily == AF_INET)
905         {
906                 dprintf ("Setting TTLv4 to %i\n", ttl);
907                 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
908                                 &ttl, sizeof (ttl));
909         }
910         else if (ph->addrfamily == AF_INET6)
911         {
912                 dprintf ("Setting TTLv6 to %i\n", ttl);
913                 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
914                                 &ttl, sizeof (ttl));
915         }
916
917         return (ret);
918 }
919
920 static int ping_get_ident (void)
921 {
922         int fd;
923         static int did_seed = 0;
924
925         int retval;
926
927         if (did_seed == 0)
928         {
929                 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
930                 {
931                         unsigned int seed;
932
933                         if (read (fd, &seed, sizeof (seed)) != -1)
934                         {
935                                 did_seed = 1;
936                                 dprintf ("Random seed:   %#x\n", seed);
937                                 srandom (seed);
938                         }
939
940                         close (fd);
941                 }
942 #if WITH_DEBUG
943                 else
944                 {
945                         char errbuf[PING_ERRMSG_LEN];
946                         dprintf ("open (/dev/urandom): %s\n",
947                                         sstrerror (errno, errbuf, sizeof (errbuf)));
948                 }
949 #endif
950         }
951
952         retval = (int) random ();
953
954         dprintf ("Random number: %#x\n", retval);
955         
956         return (retval);
957 }
958
959 static pinghost_t *ping_alloc (void)
960 {
961         pinghost_t *ph;
962         size_t      ph_size;
963
964         ph_size = sizeof (pinghost_t)
965                 + sizeof (struct sockaddr_storage)
966                 + sizeof (struct timeval);
967
968         ph = (pinghost_t *) malloc (ph_size);
969         if (ph == NULL)
970                 return (NULL);
971
972         memset (ph, '\0', ph_size);
973
974         ph->timer   = (struct timeval *) (ph + 1);
975         ph->addr    = (struct sockaddr_storage *) (ph->timer + 1);
976
977         ph->addrlen = sizeof (struct sockaddr_storage);
978         ph->fd      = -1;
979         ph->latency = -1.0;
980         ph->dropped = 0;
981         ph->ident   = ping_get_ident () & 0xFFFF;
982
983         return (ph);
984 }
985
986 static void ping_free (pinghost_t *ph)
987 {
988         if (ph->fd >= 0)
989                 close (ph->fd);
990         
991         if (ph->username != NULL)
992                 free (ph->username);
993
994         if (ph->hostname != NULL)
995                 free (ph->hostname);
996
997         if (ph->data != NULL)
998                 free (ph->data);
999
1000         free (ph);
1001 }
1002
1003 /*
1004  * public methods
1005  */
1006 const char *ping_get_error (pingobj_t *obj)
1007 {
1008         return (obj->errmsg);
1009 }
1010
1011 pingobj_t *ping_construct (void)
1012 {
1013         pingobj_t *obj;
1014
1015         if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1016                 return (NULL);
1017         memset (obj, '\0', sizeof (pingobj_t));
1018
1019         obj->timeout    = PING_DEF_TIMEOUT;
1020         obj->ttl        = PING_DEF_TTL;
1021         obj->addrfamily = PING_DEF_AF;
1022         obj->data       = strdup (PING_DEF_DATA);
1023
1024         return (obj);
1025 }
1026
1027 void ping_destroy (pingobj_t *obj)
1028 {
1029         pinghost_t *current;
1030         pinghost_t *next;
1031
1032         current = obj->head;
1033         next = NULL;
1034
1035         while (current != NULL)
1036         {
1037                 next = current->next;
1038                 ping_free (current);
1039                 current = next;
1040         }
1041
1042         if (obj->data != NULL)
1043                 free (obj->data);
1044
1045         if (obj->srcaddr != NULL)
1046                 free (obj->srcaddr);
1047
1048         free (obj);
1049
1050         return;
1051 }
1052
1053 int ping_setopt (pingobj_t *obj, int option, void *value)
1054 {
1055         int ret = 0;
1056
1057         if (value == NULL)
1058                 return (-1);
1059
1060         switch (option)
1061         {
1062                 case PING_OPT_TIMEOUT:
1063                         obj->timeout = *((double *) value);
1064                         if (obj->timeout < 0.0)
1065                         {
1066                                 obj->timeout = PING_DEF_TIMEOUT;
1067                                 ret = -1;
1068                         }
1069                         break;
1070
1071                 case PING_OPT_TTL:
1072                         obj->ttl = *((int *) value);
1073                         if ((obj->ttl < 1) || (obj->ttl > 255))
1074                         {
1075                                 obj->ttl = PING_DEF_TTL;
1076                                 ret = -1;
1077                         }
1078                         else
1079                         {
1080                                 pinghost_t *ph;
1081
1082                                 for (ph = obj->head; ph != NULL; ph = ph->next)
1083                                         ping_set_ttl (ph, obj->ttl);
1084                         }
1085                         break;
1086
1087                 case PING_OPT_AF:
1088                         obj->addrfamily = *((int *) value);
1089                         if ((obj->addrfamily != AF_UNSPEC)
1090                                         && (obj->addrfamily != AF_INET)
1091                                         && (obj->addrfamily != AF_INET6))
1092                         {
1093                                 obj->addrfamily = PING_DEF_AF;
1094                                 ret = -1;
1095                         }
1096                         if (obj->srcaddr != NULL)
1097                         {
1098                                 free (obj->srcaddr);
1099                                 obj->srcaddr = NULL;
1100                         }
1101                         break;
1102
1103                 case PING_OPT_DATA:
1104                         if (obj->data != NULL)
1105                         {
1106                                 free (obj->data);
1107                                 obj->data = NULL;
1108                         }
1109                         obj->data = strdup ((const char *) value);
1110                         break;
1111
1112                 case PING_OPT_SOURCE:
1113                 {
1114                         char            *hostname = (char *) value;
1115                         struct addrinfo  ai_hints;
1116                         struct addrinfo *ai_list;
1117                         int              status;
1118 #if WITH_DEBUG
1119                         if (obj->addrfamily != AF_UNSPEC)
1120                         {
1121                                 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1122                         }
1123 #endif
1124                         memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1125                         ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1126 #if defined(AI_ADDRCONFIG)
1127                         ai_hints.ai_flags = AI_ADDRCONFIG;
1128 #endif
1129                         status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1130                         if (status != 0)
1131                         {
1132 #if defined(EAI_SYSTEM)
1133                                 char errbuf[PING_ERRMSG_LEN];
1134 #endif
1135                                 ping_set_error (obj, "getaddrinfo",
1136 #if defined(EAI_SYSTEM)
1137                                                 (status == EAI_SYSTEM)
1138                                                 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1139 #endif
1140                                                 gai_strerror (status));
1141                                 ret = -1;
1142                                 break;
1143                         }
1144 #if WITH_DEBUG
1145                         if (ai_list->ai_next != NULL)
1146                         {
1147                                 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1148                         }
1149 #endif
1150                         if (obj->srcaddr == NULL)
1151                         {
1152                                 obj->srcaddrlen = 0;
1153                                 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1154                                 if (obj->srcaddr == NULL)
1155                                 {
1156                                         ping_set_errno (obj, errno);
1157                                         ret = -1;
1158                                         freeaddrinfo (ai_list);
1159                                         break;
1160                                 }
1161                         }
1162                         memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1163                         assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1164                         memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1165                                         ai_list->ai_addrlen);
1166                         obj->srcaddrlen = ai_list->ai_addrlen;
1167                         obj->addrfamily = ai_list->ai_family;
1168
1169                         freeaddrinfo (ai_list);
1170                 } /* case PING_OPT_SOURCE */
1171                 break;
1172
1173                 default:
1174                         ret = -2;
1175         } /* switch (option) */
1176
1177         return (ret);
1178 } /* int ping_setopt */
1179
1180
1181 int ping_send (pingobj_t *obj)
1182 {
1183         int ret;
1184
1185         if (ping_send_all (obj) < 0)
1186                 return (-1);
1187
1188         if ((ret = ping_receive_all (obj)) < 0)
1189                 return (-2);
1190
1191         return (ret);
1192 }
1193
1194 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1195 {
1196         while (ph != NULL)
1197         {
1198                 if (strcasecmp (ph->username, host) == 0)
1199                         break;
1200
1201                 ph = ph->next;
1202         }
1203
1204         return (ph);
1205 }
1206
1207 int ping_host_add (pingobj_t *obj, const char *host)
1208 {
1209         pinghost_t *ph;
1210
1211         struct addrinfo  ai_hints;
1212         struct addrinfo *ai_list, *ai_ptr;
1213         int              ai_return;
1214
1215         dprintf ("host = %s\n", host);
1216
1217         if (ping_host_search (obj->head, host) != NULL)
1218                 return (0);
1219
1220         memset (&ai_hints, '\0', sizeof (ai_hints));
1221         ai_hints.ai_flags     = 0;
1222 #ifdef AI_ADDRCONFIG
1223         ai_hints.ai_flags    |= AI_ADDRCONFIG;
1224 #endif
1225 #ifdef AI_CANONNAME
1226         ai_hints.ai_flags    |= AI_CANONNAME;
1227 #endif
1228         ai_hints.ai_family    = obj->addrfamily;
1229         ai_hints.ai_socktype  = SOCK_RAW;
1230
1231         if ((ph = ping_alloc ()) == NULL)
1232         {
1233                 dprintf ("Out of memory!\n");
1234                 return (-1);
1235         }
1236
1237         if ((ph->username = strdup (host)) == NULL)
1238         {
1239                 dprintf ("Out of memory!\n");
1240                 ping_set_errno (obj, errno);
1241                 ping_free (ph);
1242                 return (-1);
1243         }
1244
1245         if ((ph->hostname = strdup (host)) == NULL)
1246         {
1247                 dprintf ("Out of memory!\n");
1248                 ping_set_errno (obj, errno);
1249                 ping_free (ph);
1250                 return (-1);
1251         }
1252
1253         /* obj->data is not garuanteed to be != NULL */
1254         if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1255         {
1256                 dprintf ("Out of memory!\n");
1257                 ping_set_errno (obj, errno);
1258                 ping_free (ph);
1259                 return (-1);
1260         }
1261
1262         if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1263         {
1264 #if defined(EAI_SYSTEM)
1265                 char errbuf[PING_ERRMSG_LEN];
1266 #endif
1267                 dprintf ("getaddrinfo failed\n");
1268                 ping_set_error (obj, "getaddrinfo",
1269 #if defined(EAI_SYSTEM)
1270                                                 (ai_return == EAI_SYSTEM)
1271                                                 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1272 #endif
1273                                 gai_strerror (ai_return));
1274                 ping_free (ph);
1275                 return (-1);
1276         }
1277
1278         if (ai_list == NULL)
1279                 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1280
1281         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1282         {
1283                 ph->fd = -1;
1284
1285                 if (ai_ptr->ai_family == AF_INET)
1286                 {
1287                         ai_ptr->ai_socktype = SOCK_RAW;
1288                         ai_ptr->ai_protocol = IPPROTO_ICMP;
1289                 }
1290                 else if (ai_ptr->ai_family == AF_INET6)
1291                 {
1292                         ai_ptr->ai_socktype = SOCK_RAW;
1293                         ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1294                 }
1295                 else
1296                 {
1297                         char errmsg[PING_ERRMSG_LEN];
1298
1299                         snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1300                         errmsg[PING_ERRMSG_LEN - 1] = '\0';
1301
1302                         dprintf (errmsg);
1303                         ping_set_error (obj, "getaddrinfo", errmsg);
1304                         continue;
1305                 }
1306
1307                 /* TODO: Move this to a static function `ping_open_socket' and
1308                  * call it whenever the socket dies. */
1309                 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1310                 if (ph->fd == -1)
1311                 {
1312 #if WITH_DEBUG
1313                         char errbuf[PING_ERRMSG_LEN];
1314                         dprintf ("socket: %s\n",
1315                                         sstrerror (errno, errbuf, sizeof (errbuf)));
1316 #endif
1317                         ping_set_errno (obj, errno);
1318                         continue;
1319                 }
1320
1321                 if (obj->srcaddr != NULL)
1322                 {
1323                         assert (obj->srcaddrlen > 0);
1324                         assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1325
1326                         if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1327                         {
1328 #if WITH_DEBUG
1329                                 char errbuf[PING_ERRMSG_LEN];
1330                                 dprintf ("bind: %s\n",
1331                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
1332 #endif
1333                                 ping_set_errno (obj, errno);
1334                                 close (ph->fd);
1335                                 ph->fd = -1;
1336                                 continue;
1337                         }
1338                 }
1339
1340                 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1341                 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1342                 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1343                 ph->addrlen = ai_ptr->ai_addrlen;
1344                 ph->addrfamily = ai_ptr->ai_family;
1345
1346 #ifdef AI_CANONNAME
1347                 if ((ai_ptr->ai_canonname != NULL)
1348                                 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1349                 {
1350                         char *old_hostname;
1351
1352                         dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1353                                         ph->hostname, ai_ptr->ai_canonname);
1354
1355                         old_hostname = ph->hostname;
1356                         if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1357                         {
1358                                 /* strdup failed, falling back to old hostname */
1359                                 ph->hostname = old_hostname;
1360                         }
1361                         else if (old_hostname != NULL)
1362                         {
1363                                 free (old_hostname);
1364                         }
1365                 }
1366 #endif /* AI_CANONNAME */
1367
1368                 if (ph->addrfamily == AF_INET)
1369                 {
1370                         int opt = 1;
1371
1372                         setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1373                                         &opt, sizeof (opt));
1374                 }
1375 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1376                 else if (ph->addrfamily == AF_INET6)
1377                 {
1378                         int opt = 1;
1379
1380                         setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1381                                         &opt, sizeof (opt));
1382                 }
1383 #endif
1384
1385                 break;
1386         }
1387
1388         freeaddrinfo (ai_list);
1389
1390         if (ph->fd < 0)
1391         {
1392                 ping_free (ph);
1393                 return (-1);
1394         }
1395
1396         /*
1397          * Adding in the front is much easier, but then the iterator will
1398          * return the host that was added last as first host. That's just not
1399          * nice. -octo
1400          */
1401         if (obj->head == NULL)
1402         {
1403                 obj->head = ph;
1404         }
1405         else
1406         {
1407                 pinghost_t *hptr;
1408
1409                 hptr = obj->head;
1410                 while (hptr->next != NULL)
1411                         hptr = hptr->next;
1412
1413                 assert ((hptr != NULL) && (hptr->next == NULL));
1414                 hptr->next = ph;
1415         }
1416
1417         ping_set_ttl (ph, obj->ttl);
1418
1419         return (0);
1420 } /* int ping_host_add */
1421
1422 int ping_host_remove (pingobj_t *obj, const char *host)
1423 {
1424         pinghost_t *pre, *cur;
1425
1426         pre = NULL;
1427         cur = obj->head;
1428
1429         while (cur != NULL)
1430         {
1431                 if (strcasecmp (host, cur->username) == 0)
1432                         break;
1433
1434                 pre = cur;
1435                 cur = cur->next;
1436         }
1437
1438         if (cur == NULL)
1439         {
1440                 ping_set_error (obj, "ping_host_remove", "Host not found");
1441                 return (-1);
1442         }
1443
1444         if (pre == NULL)
1445                 obj->head = cur->next;
1446         else
1447                 pre->next = cur->next;
1448         
1449         ping_free (cur);
1450
1451         return (0);
1452 }
1453
1454 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1455 {
1456         return ((pingobj_iter_t *) obj->head);
1457 }
1458
1459 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1460 {
1461         return ((pingobj_iter_t *) iter->next);
1462 }
1463
1464 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1465                 void *buffer, size_t *buffer_len)
1466 {
1467         int ret = EINVAL;
1468
1469         size_t orig_buffer_len = *buffer_len;
1470
1471         switch (info)
1472         {
1473                 case PING_INFO_USERNAME:
1474                         ret = ENOMEM;
1475                         *buffer_len = strlen (iter->username) + 1;
1476                         if (orig_buffer_len <= *buffer_len)
1477                                 break;
1478                         /* Since (orig_buffer_len > *buffer_len) `strncpy'
1479                          * will copy `*buffer_len' and pad the rest of
1480                          * `buffer' with null-bytes */
1481                         strncpy (buffer, iter->username, orig_buffer_len);
1482                         ret = 0;
1483                         break;
1484
1485                 case PING_INFO_HOSTNAME:
1486                         ret = ENOMEM;
1487                         *buffer_len = strlen (iter->hostname) + 1;
1488                         if (orig_buffer_len < *buffer_len)
1489                                 break;
1490                         /* Since (orig_buffer_len > *buffer_len) `strncpy'
1491                          * will copy `*buffer_len' and pad the rest of
1492                          * `buffer' with null-bytes */
1493                         strncpy (buffer, iter->hostname, orig_buffer_len);
1494                         ret = 0;
1495                         break;
1496
1497                 case PING_INFO_ADDRESS:
1498                         ret = getnameinfo ((struct sockaddr *) iter->addr,
1499                                         iter->addrlen,
1500                                         (char *) buffer,
1501                                         *buffer_len,
1502                                         NULL, 0,
1503                                         NI_NUMERICHOST);
1504                         if (ret != 0)
1505                         {
1506                                 if ((ret == EAI_MEMORY)
1507 #ifdef EAI_OVERFLOW
1508                                                 || (ret == EAI_OVERFLOW)
1509 #endif
1510                                    )
1511                                         ret = ENOMEM;
1512 #if defined(EAI_SYSTEM)
1513                                 else if (ret == EAI_SYSTEM)
1514                                         ret = errno;
1515 #endif
1516                                 else
1517                                         ret = EINVAL;
1518                         }
1519                         break;
1520
1521                 case PING_INFO_FAMILY:
1522                         ret = ENOMEM;
1523                         *buffer_len = sizeof (int);
1524                         if (orig_buffer_len < sizeof (int))
1525                                 break;
1526                         *((int *) buffer) = iter->addrfamily;
1527                         ret = 0;
1528                         break;
1529
1530                 case PING_INFO_LATENCY:
1531                         ret = ENOMEM;
1532                         *buffer_len = sizeof (double);
1533                         if (orig_buffer_len < sizeof (double))
1534                                 break;
1535                         *((double *) buffer) = iter->latency;
1536                         ret = 0;
1537                         break;
1538
1539                 case PING_INFO_DROPPED:
1540                         ret = ENOMEM;
1541                         *buffer_len = sizeof (uint32_t);
1542                         if (orig_buffer_len < sizeof (uint32_t))
1543                                 break;
1544                         *((uint32_t *) buffer) = iter->dropped;
1545                         ret = 0;
1546                         break;
1547
1548                 case PING_INFO_SEQUENCE:
1549                         ret = ENOMEM;
1550                         *buffer_len = sizeof (unsigned int);
1551                         if (orig_buffer_len < sizeof (unsigned int))
1552                                 break;
1553                         *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1554                         ret = 0;
1555                         break;
1556
1557                 case PING_INFO_IDENT:
1558                         ret = ENOMEM;
1559                         *buffer_len = sizeof (uint16_t);
1560                         if (orig_buffer_len < sizeof (uint16_t))
1561                                 break;
1562                         *((uint16_t *) buffer) = (uint16_t) iter->ident;
1563                         ret = 0;
1564                         break;
1565
1566                 case PING_INFO_DATA:
1567                         ret = ENOMEM;
1568                         *buffer_len = strlen (iter->data);
1569                         if (orig_buffer_len < *buffer_len)
1570                                 break;
1571                         strncpy ((char *) buffer, iter->data, orig_buffer_len);
1572                         ret = 0;
1573                         break;
1574
1575                 case PING_INFO_RECV_TTL:
1576                         ret = ENOMEM;
1577                         *buffer_len = sizeof (int);
1578                         if (orig_buffer_len < sizeof (int))
1579                                 break;
1580                         *((int *) buffer) = iter->recv_ttl;
1581                         ret = 0;
1582                         break;
1583         }
1584
1585         return (ret);
1586 } /* ping_iterator_get_info */
1587
1588 void *ping_iterator_get_context (pingobj_iter_t *iter)
1589 {
1590         return (iter->context);
1591 }
1592
1593 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1594 {
1595         iter->context = context;
1596 }