4f7d955a6ad5baab241f46f12d636b0c7df22244
[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                 dprintf ("Setting TTLv4 to %i\n", ttl);
876                 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
877                                 &ttl, sizeof (ttl));
878         }
879         else if (ph->addrfamily == AF_INET6)
880         {
881                 dprintf ("Setting TTLv6 to %i\n", ttl);
882                 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
883                                 &ttl, sizeof (ttl));
884         }
885
886         return (ret);
887 }
888
889 static int ping_get_ident (void)
890 {
891         int fd;
892         static int did_seed = 0;
893
894         int retval;
895
896         if (did_seed == 0)
897         {
898                 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
899                 {
900                         unsigned int seed;
901
902                         if (read (fd, &seed, sizeof (seed)) != -1)
903                         {
904                                 did_seed = 1;
905                                 dprintf ("Random seed:   %#x\n", seed);
906                                 srandom (seed);
907                         }
908
909                         close (fd);
910                 }
911 #if WITH_DEBUG
912                 else
913                 {
914                         char errbuf[PING_ERRMSG_LEN];
915                         dprintf ("open (/dev/urandom): %s\n",
916                                         sstrerror (errno, errbuf, sizeof (errbuf)));
917                 }
918 #endif
919         }
920
921         retval = (int) random ();
922
923         dprintf ("Random number: %#x\n", retval);
924         
925         return (retval);
926 }
927
928 static pinghost_t *ping_alloc (void)
929 {
930         pinghost_t *ph;
931         size_t      ph_size;
932
933         ph_size = sizeof (pinghost_t)
934                 + sizeof (struct sockaddr_storage)
935                 + sizeof (struct timeval);
936
937         ph = (pinghost_t *) malloc (ph_size);
938         if (ph == NULL)
939                 return (NULL);
940
941         memset (ph, '\0', ph_size);
942
943         ph->timer   = (struct timeval *) (ph + 1);
944         ph->addr    = (struct sockaddr_storage *) (ph->timer + 1);
945
946         ph->addrlen = sizeof (struct sockaddr_storage);
947         ph->fd      = -1;
948         ph->latency = -1.0;
949         ph->dropped = 0;
950         ph->ident   = ping_get_ident () & 0xFFFF;
951
952         return (ph);
953 }
954
955 static void ping_free (pinghost_t *ph)
956 {
957         if (ph->fd >= 0)
958                 close (ph->fd);
959         
960         if (ph->username != NULL)
961                 free (ph->username);
962
963         if (ph->hostname != NULL)
964                 free (ph->hostname);
965
966         if (ph->data != NULL)
967                 free (ph->data);
968
969         free (ph);
970 }
971
972 /*
973  * public methods
974  */
975 const char *ping_get_error (pingobj_t *obj)
976 {
977         return (obj->errmsg);
978 }
979
980 pingobj_t *ping_construct (void)
981 {
982         pingobj_t *obj;
983
984         if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
985                 return (NULL);
986         memset (obj, '\0', sizeof (pingobj_t));
987
988         obj->timeout    = PING_DEF_TIMEOUT;
989         obj->ttl        = PING_DEF_TTL;
990         obj->addrfamily = PING_DEF_AF;
991         obj->data       = strdup (PING_DEF_DATA);
992
993         return (obj);
994 }
995
996 void ping_destroy (pingobj_t *obj)
997 {
998         pinghost_t *current;
999         pinghost_t *next;
1000
1001         current = obj->head;
1002         next = NULL;
1003
1004         while (current != NULL)
1005         {
1006                 next = current->next;
1007                 ping_free (current);
1008                 current = next;
1009         }
1010
1011         if (obj->data != NULL)
1012                 free (obj->data);
1013
1014         if (obj->srcaddr != NULL)
1015                 free (obj->srcaddr);
1016
1017         free (obj);
1018
1019         return;
1020 }
1021
1022 int ping_setopt (pingobj_t *obj, int option, void *value)
1023 {
1024         int ret = 0;
1025
1026         switch (option)
1027         {
1028                 case PING_OPT_TIMEOUT:
1029                         obj->timeout = *((double *) value);
1030                         if (obj->timeout < 0.0)
1031                         {
1032                                 obj->timeout = PING_DEF_TIMEOUT;
1033                                 ret = -1;
1034                         }
1035                         break;
1036
1037                 case PING_OPT_TTL:
1038                         obj->ttl = *((int *) value);
1039                         if ((obj->ttl < 1) || (obj->ttl > 255))
1040                         {
1041                                 obj->ttl = PING_DEF_TTL;
1042                                 ret = -1;
1043                         }
1044                         else
1045                         {
1046                                 pinghost_t *ph;
1047
1048                                 for (ph = obj->head; ph != NULL; ph = ph->next)
1049                                         ping_set_ttl (ph, obj->ttl);
1050                         }
1051                         break;
1052
1053                 case PING_OPT_AF:
1054                         obj->addrfamily = *((int *) value);
1055                         if ((obj->addrfamily != AF_UNSPEC)
1056                                         && (obj->addrfamily != AF_INET)
1057                                         && (obj->addrfamily != AF_INET6))
1058                         {
1059                                 obj->addrfamily = PING_DEF_AF;
1060                                 ret = -1;
1061                         }
1062                         if (obj->srcaddr != NULL)
1063                         {
1064                                 free (obj->srcaddr);
1065                                 obj->srcaddr = NULL;
1066                         }
1067                         break;
1068
1069                 case PING_OPT_DATA:
1070                         if (obj->data != NULL)
1071                         {
1072                                 free (obj->data);
1073                                 obj->data = NULL;
1074                         }
1075                         obj->data = strdup ((const char *) value);
1076                         break;
1077
1078                 case PING_OPT_SOURCE:
1079                 {
1080                         char            *hostname = (char *) value;
1081                         struct addrinfo  ai_hints;
1082                         struct addrinfo *ai_list;
1083                         int              status;
1084 #if WITH_DEBUG
1085                         if (obj->addrfamily != AF_UNSPEC)
1086                         {
1087                                 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1088                         }
1089 #endif
1090                         memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1091                         ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1092 #if defined(AI_ADDRCONFIG)
1093                         ai_hints.ai_flags = AI_ADDRCONFIG;
1094 #endif
1095                         status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1096                         if (status != 0)
1097                         {
1098 #if defined(EAI_SYSTEM)
1099                                 char errbuf[PING_ERRMSG_LEN];
1100 #endif
1101                                 ping_set_error (obj, "getaddrinfo",
1102 #if defined(EAI_SYSTEM)
1103                                                 (status == EAI_SYSTEM)
1104                                                 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1105 #endif
1106                                                 gai_strerror (status));
1107                                 ret = -1;
1108                                 break;
1109                         }
1110 #if WITH_DEBUG
1111                         if (ai_list->ai_next != NULL)
1112                         {
1113                                 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1114                         }
1115 #endif
1116                         if (obj->srcaddr == NULL)
1117                         {
1118                                 obj->srcaddrlen = 0;
1119                                 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1120                                 if (obj->srcaddr == NULL)
1121                                 {
1122                                         ping_set_errno (obj, errno);
1123                                         ret = -1;
1124                                         freeaddrinfo (ai_list);
1125                                         break;
1126                                 }
1127                         }
1128                         memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1129                         assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1130                         memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1131                                         ai_list->ai_addrlen);
1132                         obj->srcaddrlen = ai_list->ai_addrlen;
1133                         obj->addrfamily = ai_list->ai_family;
1134
1135                         freeaddrinfo (ai_list);
1136                 } /* case PING_OPT_SOURCE */
1137                 break;
1138
1139                 default:
1140                         ret = -2;
1141         } /* switch (option) */
1142
1143         return (ret);
1144 } /* int ping_setopt */
1145
1146
1147 int ping_send (pingobj_t *obj)
1148 {
1149         int ret;
1150
1151         if (ping_send_all (obj) < 0)
1152                 return (-1);
1153
1154         if ((ret = ping_receive_all (obj)) < 0)
1155                 return (-2);
1156
1157         return (ret);
1158 }
1159
1160 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1161 {
1162         while (ph != NULL)
1163         {
1164                 if (strcasecmp (ph->username, host) == 0)
1165                         break;
1166
1167                 ph = ph->next;
1168         }
1169
1170         return (ph);
1171 }
1172
1173 int ping_host_add (pingobj_t *obj, const char *host)
1174 {
1175         pinghost_t *ph;
1176
1177         struct addrinfo  ai_hints;
1178         struct addrinfo *ai_list, *ai_ptr;
1179         int              ai_return;
1180
1181         dprintf ("host = %s\n", host);
1182
1183         if (ping_host_search (obj->head, host) != NULL)
1184                 return (0);
1185
1186         memset (&ai_hints, '\0', sizeof (ai_hints));
1187         ai_hints.ai_flags     = 0;
1188 #ifdef AI_ADDRCONFIG
1189         ai_hints.ai_flags    |= AI_ADDRCONFIG;
1190 #endif
1191 #ifdef AI_CANONNAME
1192         ai_hints.ai_flags    |= AI_CANONNAME;
1193 #endif
1194         ai_hints.ai_family    = obj->addrfamily;
1195         ai_hints.ai_socktype  = SOCK_RAW;
1196
1197         if ((ph = ping_alloc ()) == NULL)
1198         {
1199                 dprintf ("Out of memory!\n");
1200                 return (-1);
1201         }
1202
1203         if ((ph->username = strdup (host)) == NULL)
1204         {
1205                 dprintf ("Out of memory!\n");
1206                 ping_set_errno (obj, errno);
1207                 ping_free (ph);
1208                 return (-1);
1209         }
1210
1211         if ((ph->hostname = strdup (host)) == NULL)
1212         {
1213                 dprintf ("Out of memory!\n");
1214                 ping_set_errno (obj, errno);
1215                 ping_free (ph);
1216                 return (-1);
1217         }
1218
1219         /* obj->data is not garuanteed to be != NULL */
1220         if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1221         {
1222                 dprintf ("Out of memory!\n");
1223                 ping_set_errno (obj, errno);
1224                 ping_free (ph);
1225                 return (-1);
1226         }
1227
1228         if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1229         {
1230 #if defined(EAI_SYSTEM)
1231                 char errbuf[PING_ERRMSG_LEN];
1232 #endif
1233                 dprintf ("getaddrinfo failed\n");
1234                 ping_set_error (obj, "getaddrinfo",
1235 #if defined(EAI_SYSTEM)
1236                                                 (ai_return == EAI_SYSTEM)
1237                                                 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1238 #endif
1239                                 gai_strerror (ai_return));
1240                 ping_free (ph);
1241                 return (-1);
1242         }
1243
1244         if (ai_list == NULL)
1245                 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1246
1247         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1248         {
1249                 ph->fd = -1;
1250
1251                 if (ai_ptr->ai_family == AF_INET)
1252                 {
1253                         ai_ptr->ai_socktype = SOCK_RAW;
1254                         ai_ptr->ai_protocol = IPPROTO_ICMP;
1255                 }
1256                 else if (ai_ptr->ai_family == AF_INET6)
1257                 {
1258                         ai_ptr->ai_socktype = SOCK_RAW;
1259                         ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1260                 }
1261                 else
1262                 {
1263                         char errmsg[PING_ERRMSG_LEN];
1264
1265                         snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1266                         errmsg[PING_ERRMSG_LEN - 1] = '\0';
1267
1268                         dprintf (errmsg);
1269                         ping_set_error (obj, "getaddrinfo", errmsg);
1270                         continue;
1271                 }
1272
1273                 /* TODO: Move this to a static function `ping_open_socket' and
1274                  * call it whenever the socket dies. */
1275                 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1276                 if (ph->fd == -1)
1277                 {
1278 #if WITH_DEBUG
1279                         char errbuf[PING_ERRMSG_LEN];
1280                         dprintf ("socket: %s\n",
1281                                         sstrerror (errno, errbuf, sizeof (errbuf)));
1282 #endif
1283                         ping_set_errno (obj, errno);
1284                         continue;
1285                 }
1286
1287                 if (obj->srcaddr != NULL)
1288                 {
1289                         assert (obj->srcaddrlen > 0);
1290                         assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1291
1292                         if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1293                         {
1294 #if WITH_DEBUG
1295                                 char errbuf[PING_ERRMSG_LEN];
1296                                 dprintf ("bind: %s\n",
1297                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
1298 #endif
1299                                 ping_set_errno (obj, errno);
1300                                 close (ph->fd);
1301                                 ph->fd = -1;
1302                                 continue;
1303                         }
1304                 }
1305
1306                 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1307                 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1308                 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1309                 ph->addrlen = ai_ptr->ai_addrlen;
1310                 ph->addrfamily = ai_ptr->ai_family;
1311
1312 #ifdef AI_CANONNAME
1313                 if ((ai_ptr->ai_canonname != NULL)
1314                                 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1315                 {
1316                         char *old_hostname;
1317
1318                         dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1319                                         ph->hostname, ai_ptr->ai_canonname);
1320
1321                         old_hostname = ph->hostname;
1322                         if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1323                         {
1324                                 /* strdup failed, falling back to old hostname */
1325                                 ph->hostname = old_hostname;
1326                         }
1327                         else if (old_hostname != NULL)
1328                         {
1329                                 free (old_hostname);
1330                         }
1331                 }
1332 #endif /* AI_CANONNAME */
1333
1334                 if (ph->addrfamily == AF_INET)
1335                 {
1336                         int opt = 1;
1337
1338                         setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1339                                         &opt, sizeof (opt));
1340                 }
1341                 else if (ph->addrfamily == AF_INET6)
1342                 {
1343                         int opt = 1;
1344
1345                         setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1346                                         &opt, sizeof (opt));
1347                 }
1348
1349                 break;
1350         }
1351
1352         freeaddrinfo (ai_list);
1353
1354         if (ph->fd < 0)
1355         {
1356                 ping_free (ph);
1357                 return (-1);
1358         }
1359
1360         /*
1361          * Adding in the front is much easier, but then the iterator will
1362          * return the host that was added last as first host. That's just not
1363          * nice. -octo
1364          */
1365         if (obj->head == NULL)
1366         {
1367                 obj->head = ph;
1368         }
1369         else
1370         {
1371                 pinghost_t *hptr;
1372
1373                 hptr = obj->head;
1374                 while (hptr->next != NULL)
1375                         hptr = hptr->next;
1376
1377                 assert ((hptr != NULL) && (hptr->next == NULL));
1378                 hptr->next = ph;
1379         }
1380
1381         ping_set_ttl (ph, obj->ttl);
1382
1383         return (0);
1384 } /* int ping_host_add */
1385
1386 int ping_host_remove (pingobj_t *obj, const char *host)
1387 {
1388         pinghost_t *pre, *cur;
1389
1390         pre = NULL;
1391         cur = obj->head;
1392
1393         while (cur != NULL)
1394         {
1395                 if (strcasecmp (host, cur->username) == 0)
1396                         break;
1397
1398                 pre = cur;
1399                 cur = cur->next;
1400         }
1401
1402         if (cur == NULL)
1403         {
1404                 ping_set_error (obj, "ping_host_remove", "Host not found");
1405                 return (-1);
1406         }
1407
1408         if (pre == NULL)
1409                 obj->head = cur->next;
1410         else
1411                 pre->next = cur->next;
1412         
1413         ping_free (cur);
1414
1415         return (0);
1416 }
1417
1418 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1419 {
1420         return ((pingobj_iter_t *) obj->head);
1421 }
1422
1423 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1424 {
1425         return ((pingobj_iter_t *) iter->next);
1426 }
1427
1428 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1429                 void *buffer, size_t *buffer_len)
1430 {
1431         int ret = EINVAL;
1432
1433         size_t orig_buffer_len = *buffer_len;
1434
1435         switch (info)
1436         {
1437                 case PING_INFO_USERNAME:
1438                         ret = ENOMEM;
1439                         *buffer_len = strlen (iter->username) + 1;
1440                         if (orig_buffer_len <= *buffer_len)
1441                                 break;
1442                         /* Since (orig_buffer_len > *buffer_len) `strncpy'
1443                          * will copy `*buffer_len' and pad the rest of
1444                          * `buffer' with null-bytes */
1445                         strncpy (buffer, iter->username, orig_buffer_len);
1446                         ret = 0;
1447                         break;
1448
1449                 case PING_INFO_HOSTNAME:
1450                         ret = ENOMEM;
1451                         *buffer_len = strlen (iter->hostname) + 1;
1452                         if (orig_buffer_len < *buffer_len)
1453                                 break;
1454                         /* Since (orig_buffer_len > *buffer_len) `strncpy'
1455                          * will copy `*buffer_len' and pad the rest of
1456                          * `buffer' with null-bytes */
1457                         strncpy (buffer, iter->hostname, orig_buffer_len);
1458                         ret = 0;
1459                         break;
1460
1461                 case PING_INFO_ADDRESS:
1462                         ret = getnameinfo ((struct sockaddr *) iter->addr,
1463                                         iter->addrlen,
1464                                         (char *) buffer,
1465                                         *buffer_len,
1466                                         NULL, 0,
1467                                         NI_NUMERICHOST);
1468                         if (ret != 0)
1469                         {
1470                                 if ((ret == EAI_MEMORY)
1471 #ifdef EAI_OVERFLOW
1472                                                 || (ret == EAI_OVERFLOW)
1473 #endif
1474                                    )
1475                                         ret = ENOMEM;
1476 #if defined(EAI_SYSTEM)
1477                                 else if (ret == EAI_SYSTEM)
1478                                         ret = errno;
1479 #endif
1480                                 else
1481                                         ret = EINVAL;
1482                         }
1483                         break;
1484
1485                 case PING_INFO_FAMILY:
1486                         ret = ENOMEM;
1487                         *buffer_len = sizeof (int);
1488                         if (orig_buffer_len < sizeof (int))
1489                                 break;
1490                         *((int *) buffer) = iter->addrfamily;
1491                         ret = 0;
1492                         break;
1493
1494                 case PING_INFO_LATENCY:
1495                         ret = ENOMEM;
1496                         *buffer_len = sizeof (double);
1497                         if (orig_buffer_len < sizeof (double))
1498                                 break;
1499                         *((double *) buffer) = iter->latency;
1500                         ret = 0;
1501                         break;
1502
1503                 case PING_INFO_DROPPED:
1504                         ret = ENOMEM;
1505                         *buffer_len = sizeof (uint32_t);
1506                         if (orig_buffer_len < sizeof (uint32_t))
1507                                 break;
1508                         *((uint32_t *) buffer) = iter->dropped;
1509                         ret = 0;
1510                         break;
1511
1512                 case PING_INFO_SEQUENCE:
1513                         ret = ENOMEM;
1514                         *buffer_len = sizeof (unsigned int);
1515                         if (orig_buffer_len < sizeof (unsigned int))
1516                                 break;
1517                         *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1518                         ret = 0;
1519                         break;
1520
1521                 case PING_INFO_IDENT:
1522                         ret = ENOMEM;
1523                         *buffer_len = sizeof (uint16_t);
1524                         if (orig_buffer_len < sizeof (uint16_t))
1525                                 break;
1526                         *((uint16_t *) buffer) = (uint16_t) iter->ident;
1527                         ret = 0;
1528                         break;
1529
1530                 case PING_INFO_DATA:
1531                         ret = ENOMEM;
1532                         *buffer_len = strlen (iter->data);
1533                         if (orig_buffer_len < *buffer_len)
1534                                 break;
1535                         strncpy ((char *) buffer, iter->data, orig_buffer_len);
1536                         ret = 0;
1537                         break;
1538
1539                 case PING_INFO_RECV_TTL:
1540                         ret = ENOMEM;
1541                         *buffer_len = sizeof (int);
1542                         if (orig_buffer_len < sizeof (int))
1543                                 break;
1544                         *((int *) buffer) = iter->recv_ttl;
1545                         ret = 0;
1546                         break;
1547         }
1548
1549         return (ret);
1550 } /* ping_iterator_get_info */
1551
1552 void *ping_iterator_get_context (pingobj_iter_t *iter)
1553 {
1554         return (iter->context);
1555 }
1556
1557 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1558 {
1559         iter->context = context;
1560 }