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