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