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