src/liboping.c: Fix some format strings.
[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         switch (option)
1058         {
1059                 case PING_OPT_TIMEOUT:
1060                         obj->timeout = *((double *) value);
1061                         if (obj->timeout < 0.0)
1062                         {
1063                                 obj->timeout = PING_DEF_TIMEOUT;
1064                                 ret = -1;
1065                         }
1066                         break;
1067
1068                 case PING_OPT_TTL:
1069                         obj->ttl = *((int *) value);
1070                         if ((obj->ttl < 1) || (obj->ttl > 255))
1071                         {
1072                                 obj->ttl = PING_DEF_TTL;
1073                                 ret = -1;
1074                         }
1075                         else
1076                         {
1077                                 pinghost_t *ph;
1078
1079                                 for (ph = obj->head; ph != NULL; ph = ph->next)
1080                                         ping_set_ttl (ph, obj->ttl);
1081                         }
1082                         break;
1083
1084                 case PING_OPT_AF:
1085                         obj->addrfamily = *((int *) value);
1086                         if ((obj->addrfamily != AF_UNSPEC)
1087                                         && (obj->addrfamily != AF_INET)
1088                                         && (obj->addrfamily != AF_INET6))
1089                         {
1090                                 obj->addrfamily = PING_DEF_AF;
1091                                 ret = -1;
1092                         }
1093                         if (obj->srcaddr != NULL)
1094                         {
1095                                 free (obj->srcaddr);
1096                                 obj->srcaddr = NULL;
1097                         }
1098                         break;
1099
1100                 case PING_OPT_DATA:
1101                         if (obj->data != NULL)
1102                         {
1103                                 free (obj->data);
1104                                 obj->data = NULL;
1105                         }
1106                         obj->data = strdup ((const char *) value);
1107                         break;
1108
1109                 case PING_OPT_SOURCE:
1110                 {
1111                         char            *hostname = (char *) value;
1112                         struct addrinfo  ai_hints;
1113                         struct addrinfo *ai_list;
1114                         int              status;
1115 #if WITH_DEBUG
1116                         if (obj->addrfamily != AF_UNSPEC)
1117                         {
1118                                 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1119                         }
1120 #endif
1121                         memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1122                         ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1123 #if defined(AI_ADDRCONFIG)
1124                         ai_hints.ai_flags = AI_ADDRCONFIG;
1125 #endif
1126                         status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1127                         if (status != 0)
1128                         {
1129 #if defined(EAI_SYSTEM)
1130                                 char errbuf[PING_ERRMSG_LEN];
1131 #endif
1132                                 ping_set_error (obj, "getaddrinfo",
1133 #if defined(EAI_SYSTEM)
1134                                                 (status == EAI_SYSTEM)
1135                                                 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1136 #endif
1137                                                 gai_strerror (status));
1138                                 ret = -1;
1139                                 break;
1140                         }
1141 #if WITH_DEBUG
1142                         if (ai_list->ai_next != NULL)
1143                         {
1144                                 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1145                         }
1146 #endif
1147                         if (obj->srcaddr == NULL)
1148                         {
1149                                 obj->srcaddrlen = 0;
1150                                 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1151                                 if (obj->srcaddr == NULL)
1152                                 {
1153                                         ping_set_errno (obj, errno);
1154                                         ret = -1;
1155                                         freeaddrinfo (ai_list);
1156                                         break;
1157                                 }
1158                         }
1159                         memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1160                         assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1161                         memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1162                                         ai_list->ai_addrlen);
1163                         obj->srcaddrlen = ai_list->ai_addrlen;
1164                         obj->addrfamily = ai_list->ai_family;
1165
1166                         freeaddrinfo (ai_list);
1167                 } /* case PING_OPT_SOURCE */
1168                 break;
1169
1170                 default:
1171                         ret = -2;
1172         } /* switch (option) */
1173
1174         return (ret);
1175 } /* int ping_setopt */
1176
1177
1178 int ping_send (pingobj_t *obj)
1179 {
1180         int ret;
1181
1182         if (ping_send_all (obj) < 0)
1183                 return (-1);
1184
1185         if ((ret = ping_receive_all (obj)) < 0)
1186                 return (-2);
1187
1188         return (ret);
1189 }
1190
1191 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1192 {
1193         while (ph != NULL)
1194         {
1195                 if (strcasecmp (ph->username, host) == 0)
1196                         break;
1197
1198                 ph = ph->next;
1199         }
1200
1201         return (ph);
1202 }
1203
1204 int ping_host_add (pingobj_t *obj, const char *host)
1205 {
1206         pinghost_t *ph;
1207
1208         struct addrinfo  ai_hints;
1209         struct addrinfo *ai_list, *ai_ptr;
1210         int              ai_return;
1211
1212         dprintf ("host = %s\n", host);
1213
1214         if (ping_host_search (obj->head, host) != NULL)
1215                 return (0);
1216
1217         memset (&ai_hints, '\0', sizeof (ai_hints));
1218         ai_hints.ai_flags     = 0;
1219 #ifdef AI_ADDRCONFIG
1220         ai_hints.ai_flags    |= AI_ADDRCONFIG;
1221 #endif
1222 #ifdef AI_CANONNAME
1223         ai_hints.ai_flags    |= AI_CANONNAME;
1224 #endif
1225         ai_hints.ai_family    = obj->addrfamily;
1226         ai_hints.ai_socktype  = SOCK_RAW;
1227
1228         if ((ph = ping_alloc ()) == NULL)
1229         {
1230                 dprintf ("Out of memory!\n");
1231                 return (-1);
1232         }
1233
1234         if ((ph->username = strdup (host)) == NULL)
1235         {
1236                 dprintf ("Out of memory!\n");
1237                 ping_set_errno (obj, errno);
1238                 ping_free (ph);
1239                 return (-1);
1240         }
1241
1242         if ((ph->hostname = strdup (host)) == NULL)
1243         {
1244                 dprintf ("Out of memory!\n");
1245                 ping_set_errno (obj, errno);
1246                 ping_free (ph);
1247                 return (-1);
1248         }
1249
1250         /* obj->data is not garuanteed to be != NULL */
1251         if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1252         {
1253                 dprintf ("Out of memory!\n");
1254                 ping_set_errno (obj, errno);
1255                 ping_free (ph);
1256                 return (-1);
1257         }
1258
1259         if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1260         {
1261 #if defined(EAI_SYSTEM)
1262                 char errbuf[PING_ERRMSG_LEN];
1263 #endif
1264                 dprintf ("getaddrinfo failed\n");
1265                 ping_set_error (obj, "getaddrinfo",
1266 #if defined(EAI_SYSTEM)
1267                                                 (ai_return == EAI_SYSTEM)
1268                                                 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1269 #endif
1270                                 gai_strerror (ai_return));
1271                 ping_free (ph);
1272                 return (-1);
1273         }
1274
1275         if (ai_list == NULL)
1276                 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1277
1278         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1279         {
1280                 ph->fd = -1;
1281
1282                 if (ai_ptr->ai_family == AF_INET)
1283                 {
1284                         ai_ptr->ai_socktype = SOCK_RAW;
1285                         ai_ptr->ai_protocol = IPPROTO_ICMP;
1286                 }
1287                 else if (ai_ptr->ai_family == AF_INET6)
1288                 {
1289                         ai_ptr->ai_socktype = SOCK_RAW;
1290                         ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1291                 }
1292                 else
1293                 {
1294                         char errmsg[PING_ERRMSG_LEN];
1295
1296                         snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1297                         errmsg[PING_ERRMSG_LEN - 1] = '\0';
1298
1299                         dprintf (errmsg);
1300                         ping_set_error (obj, "getaddrinfo", errmsg);
1301                         continue;
1302                 }
1303
1304                 /* TODO: Move this to a static function `ping_open_socket' and
1305                  * call it whenever the socket dies. */
1306                 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1307                 if (ph->fd == -1)
1308                 {
1309 #if WITH_DEBUG
1310                         char errbuf[PING_ERRMSG_LEN];
1311                         dprintf ("socket: %s\n",
1312                                         sstrerror (errno, errbuf, sizeof (errbuf)));
1313 #endif
1314                         ping_set_errno (obj, errno);
1315                         continue;
1316                 }
1317
1318                 if (obj->srcaddr != NULL)
1319                 {
1320                         assert (obj->srcaddrlen > 0);
1321                         assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1322
1323                         if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1324                         {
1325 #if WITH_DEBUG
1326                                 char errbuf[PING_ERRMSG_LEN];
1327                                 dprintf ("bind: %s\n",
1328                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
1329 #endif
1330                                 ping_set_errno (obj, errno);
1331                                 close (ph->fd);
1332                                 ph->fd = -1;
1333                                 continue;
1334                         }
1335                 }
1336
1337                 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1338                 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1339                 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1340                 ph->addrlen = ai_ptr->ai_addrlen;
1341                 ph->addrfamily = ai_ptr->ai_family;
1342
1343 #ifdef AI_CANONNAME
1344                 if ((ai_ptr->ai_canonname != NULL)
1345                                 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1346                 {
1347                         char *old_hostname;
1348
1349                         dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1350                                         ph->hostname, ai_ptr->ai_canonname);
1351
1352                         old_hostname = ph->hostname;
1353                         if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1354                         {
1355                                 /* strdup failed, falling back to old hostname */
1356                                 ph->hostname = old_hostname;
1357                         }
1358                         else if (old_hostname != NULL)
1359                         {
1360                                 free (old_hostname);
1361                         }
1362                 }
1363 #endif /* AI_CANONNAME */
1364
1365                 if (ph->addrfamily == AF_INET)
1366                 {
1367                         int opt = 1;
1368
1369                         setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1370                                         &opt, sizeof (opt));
1371                 }
1372 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1373                 else if (ph->addrfamily == AF_INET6)
1374                 {
1375                         int opt = 1;
1376
1377                         setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1378                                         &opt, sizeof (opt));
1379                 }
1380 #endif
1381
1382                 break;
1383         }
1384
1385         freeaddrinfo (ai_list);
1386
1387         if (ph->fd < 0)
1388         {
1389                 ping_free (ph);
1390                 return (-1);
1391         }
1392
1393         /*
1394          * Adding in the front is much easier, but then the iterator will
1395          * return the host that was added last as first host. That's just not
1396          * nice. -octo
1397          */
1398         if (obj->head == NULL)
1399         {
1400                 obj->head = ph;
1401         }
1402         else
1403         {
1404                 pinghost_t *hptr;
1405
1406                 hptr = obj->head;
1407                 while (hptr->next != NULL)
1408                         hptr = hptr->next;
1409
1410                 assert ((hptr != NULL) && (hptr->next == NULL));
1411                 hptr->next = ph;
1412         }
1413
1414         ping_set_ttl (ph, obj->ttl);
1415
1416         return (0);
1417 } /* int ping_host_add */
1418
1419 int ping_host_remove (pingobj_t *obj, const char *host)
1420 {
1421         pinghost_t *pre, *cur;
1422
1423         pre = NULL;
1424         cur = obj->head;
1425
1426         while (cur != NULL)
1427         {
1428                 if (strcasecmp (host, cur->username) == 0)
1429                         break;
1430
1431                 pre = cur;
1432                 cur = cur->next;
1433         }
1434
1435         if (cur == NULL)
1436         {
1437                 ping_set_error (obj, "ping_host_remove", "Host not found");
1438                 return (-1);
1439         }
1440
1441         if (pre == NULL)
1442                 obj->head = cur->next;
1443         else
1444                 pre->next = cur->next;
1445         
1446         ping_free (cur);
1447
1448         return (0);
1449 }
1450
1451 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1452 {
1453         return ((pingobj_iter_t *) obj->head);
1454 }
1455
1456 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1457 {
1458         return ((pingobj_iter_t *) iter->next);
1459 }
1460
1461 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1462                 void *buffer, size_t *buffer_len)
1463 {
1464         int ret = EINVAL;
1465
1466         size_t orig_buffer_len = *buffer_len;
1467
1468         switch (info)
1469         {
1470                 case PING_INFO_USERNAME:
1471                         ret = ENOMEM;
1472                         *buffer_len = strlen (iter->username) + 1;
1473                         if (orig_buffer_len <= *buffer_len)
1474                                 break;
1475                         /* Since (orig_buffer_len > *buffer_len) `strncpy'
1476                          * will copy `*buffer_len' and pad the rest of
1477                          * `buffer' with null-bytes */
1478                         strncpy (buffer, iter->username, orig_buffer_len);
1479                         ret = 0;
1480                         break;
1481
1482                 case PING_INFO_HOSTNAME:
1483                         ret = ENOMEM;
1484                         *buffer_len = strlen (iter->hostname) + 1;
1485                         if (orig_buffer_len < *buffer_len)
1486                                 break;
1487                         /* Since (orig_buffer_len > *buffer_len) `strncpy'
1488                          * will copy `*buffer_len' and pad the rest of
1489                          * `buffer' with null-bytes */
1490                         strncpy (buffer, iter->hostname, orig_buffer_len);
1491                         ret = 0;
1492                         break;
1493
1494                 case PING_INFO_ADDRESS:
1495                         ret = getnameinfo ((struct sockaddr *) iter->addr,
1496                                         iter->addrlen,
1497                                         (char *) buffer,
1498                                         *buffer_len,
1499                                         NULL, 0,
1500                                         NI_NUMERICHOST);
1501                         if (ret != 0)
1502                         {
1503                                 if ((ret == EAI_MEMORY)
1504 #ifdef EAI_OVERFLOW
1505                                                 || (ret == EAI_OVERFLOW)
1506 #endif
1507                                    )
1508                                         ret = ENOMEM;
1509 #if defined(EAI_SYSTEM)
1510                                 else if (ret == EAI_SYSTEM)
1511                                         ret = errno;
1512 #endif
1513                                 else
1514                                         ret = EINVAL;
1515                         }
1516                         break;
1517
1518                 case PING_INFO_FAMILY:
1519                         ret = ENOMEM;
1520                         *buffer_len = sizeof (int);
1521                         if (orig_buffer_len < sizeof (int))
1522                                 break;
1523                         *((int *) buffer) = iter->addrfamily;
1524                         ret = 0;
1525                         break;
1526
1527                 case PING_INFO_LATENCY:
1528                         ret = ENOMEM;
1529                         *buffer_len = sizeof (double);
1530                         if (orig_buffer_len < sizeof (double))
1531                                 break;
1532                         *((double *) buffer) = iter->latency;
1533                         ret = 0;
1534                         break;
1535
1536                 case PING_INFO_DROPPED:
1537                         ret = ENOMEM;
1538                         *buffer_len = sizeof (uint32_t);
1539                         if (orig_buffer_len < sizeof (uint32_t))
1540                                 break;
1541                         *((uint32_t *) buffer) = iter->dropped;
1542                         ret = 0;
1543                         break;
1544
1545                 case PING_INFO_SEQUENCE:
1546                         ret = ENOMEM;
1547                         *buffer_len = sizeof (unsigned int);
1548                         if (orig_buffer_len < sizeof (unsigned int))
1549                                 break;
1550                         *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1551                         ret = 0;
1552                         break;
1553
1554                 case PING_INFO_IDENT:
1555                         ret = ENOMEM;
1556                         *buffer_len = sizeof (uint16_t);
1557                         if (orig_buffer_len < sizeof (uint16_t))
1558                                 break;
1559                         *((uint16_t *) buffer) = (uint16_t) iter->ident;
1560                         ret = 0;
1561                         break;
1562
1563                 case PING_INFO_DATA:
1564                         ret = ENOMEM;
1565                         *buffer_len = strlen (iter->data);
1566                         if (orig_buffer_len < *buffer_len)
1567                                 break;
1568                         strncpy ((char *) buffer, iter->data, orig_buffer_len);
1569                         ret = 0;
1570                         break;
1571
1572                 case PING_INFO_RECV_TTL:
1573                         ret = ENOMEM;
1574                         *buffer_len = sizeof (int);
1575                         if (orig_buffer_len < sizeof (int))
1576                                 break;
1577                         *((int *) buffer) = iter->recv_ttl;
1578                         ret = 0;
1579                         break;
1580         }
1581
1582         return (ret);
1583 } /* ping_iterator_get_info */
1584
1585 void *ping_iterator_get_context (pingobj_iter_t *iter)
1586 {
1587         return (iter->context);
1588 }
1589
1590 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1591 {
1592         iter->context = context;
1593 }