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