1 /**
2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2010 Florian octo Forster <octo at verplant.org>
4 *
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by the
7 * Free Software Foundation; either version 2.1 of the License, or (at your
8 * option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #if STDC_HEADERS
25 # include <stdlib.h>
26 # include <stdio.h>
27 # include <string.h>
28 # include <inttypes.h>
29 # include <errno.h>
30 # include <assert.h>
31 #else
32 # error "You don't have the standard C99 header files installed"
33 #endif /* STDC_HEADERS */
35 #ifdef HAVE_STDINT_H
36 # include <stdint.h>
37 #endif
39 #if HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
43 #if HAVE_FCNTL_H
44 # include <fcntl.h>
45 #endif
46 #if HAVE_SYS_TYPES_H
47 # include <sys/types.h>
48 #endif
49 #if HAVE_SYS_STAT_H
50 # include <sys/stat.h>
51 #endif
53 #if TIME_WITH_SYS_TIME
54 # include <sys/time.h>
55 # include <time.h>
56 #else
57 # if HAVE_SYS_TIME_H
58 # include <sys/time.h>
59 # else
60 # include <time.h>
61 # endif
62 #endif
64 #if HAVE_SYS_SOCKET_H
65 # include <sys/socket.h>
66 #endif
68 #if HAVE_NETDB_H
69 # include <netdb.h>
70 #endif
72 #if HAVE_NETINET_IN_SYSTM_H
73 # include <netinet/in_systm.h>
74 #endif
75 #if HAVE_NETINET_IN_H
76 # include <netinet/in.h>
77 #endif
78 #if HAVE_NETINET_IP_H
79 # include <netinet/ip.h>
80 #endif
81 #if HAVE_NETINET_IP_ICMP_H
82 # include <netinet/ip_icmp.h>
83 #endif
84 #ifdef HAVE_NETINET_IP_VAR_H
85 # include <netinet/ip_var.h>
86 #endif
87 #if HAVE_NETINET_IP6_H
88 # include <netinet/ip6.h>
89 #endif
90 #if HAVE_NETINET_ICMP6_H
91 # include <netinet/icmp6.h>
92 #endif
94 #include "oping.h"
96 #if WITH_DEBUG
97 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
98 #else
99 # define dprintf(...) /**/
100 #endif
102 #define PING_ERRMSG_LEN 256
104 struct pinghost
105 {
106 /* username: name passed in by the user */
107 char *username;
108 /* hostname: name returned by the reverse lookup */
109 char *hostname;
110 struct sockaddr_storage *addr;
111 socklen_t addrlen;
112 int addrfamily;
113 int fd;
114 int ident;
115 int sequence;
116 struct timeval *timer;
117 double latency;
118 uint32_t dropped;
119 int recv_ttl;
120 char *data;
122 void *context;
124 struct pinghost *next;
125 };
127 struct pingobj
128 {
129 double timeout;
130 int ttl;
131 int addrfamily;
132 char *data;
134 struct sockaddr *srcaddr;
135 socklen_t srcaddrlen;
137 char *device;
139 char errmsg[PING_ERRMSG_LEN];
141 pinghost_t *head;
142 };
144 /*
145 * private (static) functions
146 */
147 /* Even though Posix requires "strerror_r" to return an "int",
148 * some systems (e.g. the GNU libc) return a "char *" _and_
149 * ignore the second argument ... -tokkee */
150 static char *sstrerror (int errnum, char *buf, size_t buflen)
151 {
152 buf[0] = 0;
154 #if !HAVE_STRERROR_R
155 {
156 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
157 }
158 /* #endif !HAVE_STRERROR_R */
160 #elif STRERROR_R_CHAR_P
161 {
162 char *temp;
163 temp = strerror_r (errnum, buf, buflen);
164 if (buf[0] == 0)
165 {
166 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
167 strncpy (buf, temp, buflen);
168 else
169 strncpy (buf, "strerror_r did not return "
170 "an error message", buflen);
171 }
172 }
173 /* #endif STRERROR_R_CHAR_P */
175 #else
176 if (strerror_r (errnum, buf, buflen) != 0)
177 {
178 snprintf (buf, buflen, "Error %i (%#x); "
179 "Additionally, strerror_r failed.",
180 errnum, errnum);
181 }
182 #endif /* STRERROR_R_CHAR_P */
184 buf[buflen - 1] = 0;
186 return (buf);
187 } /* char *sstrerror */
189 static void ping_set_error (pingobj_t *obj, const char *function,
190 const char *message)
191 {
192 snprintf (obj->errmsg, sizeof (obj->errmsg),
193 "%s: %s", function, message);
194 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
195 }
197 static void ping_set_errno (pingobj_t *obj, int error_number)
198 {
199 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
200 }
202 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
203 struct timeval *res)
204 {
205 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
206 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
208 while (res->tv_usec > 1000000)
209 {
210 res->tv_usec -= 1000000;
211 res->tv_sec++;
212 }
214 return (0);
215 }
217 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
218 struct timeval *res)
219 {
220 if ((tv1->tv_sec < tv2->tv_sec)
221 || ((tv1->tv_sec == tv2->tv_sec)
222 && (tv1->tv_usec < tv2->tv_usec)))
223 return (-1);
225 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
226 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
228 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
230 while (res->tv_usec < 0)
231 {
232 res->tv_usec += 1000000;
233 res->tv_sec--;
234 }
236 return (0);
237 }
239 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
240 {
241 uint32_t sum = 0;
242 uint16_t ret = 0;
244 uint16_t *ptr;
246 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
247 sum += *ptr;
249 if (len == 1)
250 {
251 *(char *) &ret = *(char *) ptr;
252 sum += ret;
253 }
255 /* Do this twice to get all possible carries.. */
256 sum = (sum >> 16) + (sum & 0xFFFF);
257 sum = (sum >> 16) + (sum & 0xFFFF);
259 ret = ~sum;
261 return (ret);
262 }
264 static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
265 size_t buffer_len)
266 {
267 struct ip *ip_hdr;
268 struct icmp *icmp_hdr;
270 size_t ip_hdr_len;
272 uint16_t recv_checksum;
273 uint16_t calc_checksum;
275 uint16_t ident;
276 uint16_t seq;
278 pinghost_t *ptr;
280 if (buffer_len < sizeof (struct ip))
281 return (NULL);
283 ip_hdr = (struct ip *) buffer;
284 ip_hdr_len = ip_hdr->ip_hl << 2;
286 if (buffer_len < ip_hdr_len)
287 return (NULL);
289 buffer += ip_hdr_len;
290 buffer_len -= ip_hdr_len;
292 if (buffer_len < sizeof (struct icmp))
293 return (NULL);
295 icmp_hdr = (struct icmp *) buffer;
296 buffer += sizeof (struct icmp);
297 buffer_len -= sizeof (struct icmp);
299 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
300 {
301 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
302 return (NULL);
303 }
305 recv_checksum = icmp_hdr->icmp_cksum;
306 icmp_hdr->icmp_cksum = 0;
307 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
308 sizeof (struct icmp) + buffer_len);
310 if (recv_checksum != calc_checksum)
311 {
312 dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
313 "calculated 0x%04"PRIx16"\n",
314 recv_checksum, calc_checksum);
315 return (NULL);
316 }
318 ident = ntohs (icmp_hdr->icmp_id);
319 seq = ntohs (icmp_hdr->icmp_seq);
321 /* We have to iterate over all hosts, since ICMPv4 packets may
322 * be received on any raw v4 socket. */
323 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
324 {
325 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
326 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
328 if (ptr->addrfamily != AF_INET)
329 continue;
331 if (!timerisset (ptr->timer))
332 continue;
334 if (ptr->ident != ident)
335 continue;
337 if (((ptr->sequence - 1) & 0xFFFF) != seq)
338 continue;
340 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
341 "seq = %"PRIu16"\n",
342 ptr->hostname, ident, seq);
344 break;
345 }
347 if (ptr == NULL)
348 {
349 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
350 ident, seq);
351 }
353 if (ptr != NULL)
354 ptr->recv_ttl = ip_hdr->ip_ttl;
356 return (ptr);
357 }
359 #ifndef ICMP6_ECHO_REQUEST
360 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
361 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
362 # else
363 # define ICMP6_ECHO_REQUEST 128
364 # endif
365 #endif
367 #ifndef ICMP6_ECHO_REPLY
368 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
369 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
370 # else
371 # define ICMP6_ECHO_REPLY 129
372 # endif
373 #endif
375 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
376 size_t buffer_len)
377 {
378 struct icmp6_hdr *icmp_hdr;
380 uint16_t ident;
381 uint16_t seq;
383 pinghost_t *ptr;
385 if (buffer_len < sizeof (struct icmp6_hdr))
386 return (NULL);
388 icmp_hdr = (struct icmp6_hdr *) buffer;
389 buffer += sizeof (struct icmp);
390 buffer_len -= sizeof (struct icmp);
392 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
393 {
394 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
395 return (NULL);
396 }
398 if (icmp_hdr->icmp6_code != 0)
399 {
400 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
401 return (NULL);
402 }
404 ident = ntohs (icmp_hdr->icmp6_id);
405 seq = ntohs (icmp_hdr->icmp6_seq);
407 /* We have to iterate over all hosts, since ICMPv6 packets may
408 * be received on any raw v6 socket. */
409 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
410 {
411 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
412 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
414 if (ptr->addrfamily != AF_INET6)
415 continue;
417 if (!timerisset (ptr->timer))
418 continue;
420 if (ptr->ident != ident)
421 continue;
423 if (((ptr->sequence - 1) & 0xFFFF) != seq)
424 continue;
426 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
427 "seq = %"PRIu16"\n",
428 ptr->hostname, ident, seq);
430 break;
431 }
433 if (ptr == NULL)
434 {
435 dprintf ("No match found for ident = 0x%04"PRIx16", "
436 "seq = %"PRIu16"\n",
437 ident, seq);
438 }
440 return (ptr);
441 }
443 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
444 struct timeval *now)
445 {
446 /* Note: 'ph' is not necessarily the host object for which we receive a
447 * reply. The right object will be returned by ping_receive_ipv*(). For
448 * now, we can only rely on ph->fd and ph->addrfamily. */
450 struct timeval diff;
451 pinghost_t *host = NULL;
452 int recv_ttl;
454 /*
455 * Set up the receive buffer..
456 */
457 struct msghdr msghdr;
458 struct cmsghdr *cmsg;
459 char payload_buffer[4096];
460 ssize_t payload_buffer_len;
461 char control_buffer[4096];
462 struct iovec payload_iovec;
464 memset (&payload_iovec, 0, sizeof (payload_iovec));
465 payload_iovec.iov_base = payload_buffer;
466 payload_iovec.iov_len = sizeof (payload_buffer);
468 memset (&msghdr, 0, sizeof (msghdr));
469 /* unspecified source address */
470 msghdr.msg_name = NULL;
471 msghdr.msg_namelen = 0;
472 /* output buffer vector, see readv(2) */
473 msghdr.msg_iov = &payload_iovec;
474 msghdr.msg_iovlen = 1;
475 /* output buffer for control messages */
476 msghdr.msg_control = control_buffer;
477 msghdr.msg_controllen = sizeof (control_buffer);
478 /* flags; this is an output only field.. */
479 msghdr.msg_flags = 0;
480 #ifdef MSG_XPG4_2
481 msghdr.msg_flags |= MSG_XPG4_2;
482 #endif
484 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
485 if (payload_buffer_len < 0)
486 {
487 #if WITH_DEBUG
488 char errbuf[PING_ERRMSG_LEN];
489 dprintf ("recvfrom: %s\n",
490 sstrerror (errno, errbuf, sizeof (errbuf)));
491 #endif
492 return (-1);
493 }
494 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
496 /* Iterate over all auxiliary data in msghdr */
497 recv_ttl = -1;
498 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
499 cmsg != NULL;
500 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
501 {
502 if (ph->addrfamily == AF_INET) /* {{{ */
503 {
504 if (cmsg->cmsg_level != IPPROTO_IP)
505 continue;
507 if (cmsg->cmsg_type == IP_TTL)
508 {
509 memcpy (&recv_ttl, CMSG_DATA (cmsg),
510 sizeof (recv_ttl));
511 dprintf ("TTLv4 = %i;\n", recv_ttl);
512 }
513 else
514 {
515 dprintf ("Not handling option %i.\n",
516 cmsg->cmsg_type);
517 }
518 } /* }}} */
519 else if (ph->addrfamily == AF_INET6) /* {{{ */
520 {
521 if (cmsg->cmsg_level != IPPROTO_IPV6)
522 continue;
524 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
525 {
526 memcpy (&recv_ttl, CMSG_DATA (cmsg),
527 sizeof (recv_ttl));
528 dprintf ("TTLv6 = %i;\n", recv_ttl);
529 }
530 else
531 {
532 dprintf ("Not handling option %i.\n",
533 cmsg->cmsg_type);
534 }
535 } /* }}} */
536 else
537 {
538 dprintf ("Don't know how to handle "
539 "unknown protocol %i.\n",
540 cmsg->cmsg_level);
541 }
542 } /* }}} for (cmsg) */
544 if (ph->addrfamily == AF_INET)
545 {
546 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
547 if (host == NULL)
548 return (-1);
549 }
550 else if (ph->addrfamily == AF_INET6)
551 {
552 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
553 if (host == NULL)
554 return (-1);
555 }
556 else
557 {
558 dprintf ("ping_receive_one: Unknown address family %i.\n",
559 ph->addrfamily);
560 return (-1);
561 }
563 dprintf ("rcvd: %12i.%06i\n",
564 (int) now->tv_sec,
565 (int) now->tv_usec);
566 dprintf ("sent: %12i.%06i\n",
567 (int) host->timer->tv_sec,
568 (int) host->timer->tv_usec);
570 if (ping_timeval_sub (now, host->timer, &diff) < 0)
571 {
572 timerclear (host->timer);
573 return (-1);
574 }
576 dprintf ("diff: %12i.%06i\n",
577 (int) diff.tv_sec,
578 (int) diff.tv_usec);
580 if (recv_ttl >= 0)
581 host->recv_ttl = recv_ttl;
583 host->latency = ((double) diff.tv_usec) / 1000.0;
584 host->latency += ((double) diff.tv_sec) * 1000.0;
586 timerclear (host->timer);
588 return (0);
589 }
591 static int ping_receive_all (pingobj_t *obj)
592 {
593 fd_set read_fds;
594 fd_set err_fds;
595 int num_fds;
596 int max_fd;
598 pinghost_t *ph;
599 pinghost_t *ptr;
601 struct timeval endtime;
602 struct timeval nowtime;
603 struct timeval timeout;
604 int status;
606 int ret;
608 ph = obj->head;
609 ret = 0;
611 for (ptr = ph; ptr != NULL; ptr = ptr->next)
612 {
613 ptr->latency = -1.0;
614 ptr->recv_ttl = -1;
615 }
617 if (gettimeofday (&nowtime, NULL) == -1)
618 {
619 ping_set_errno (obj, errno);
620 return (-1);
621 }
623 /* Set up timeout */
624 timeout.tv_sec = (time_t) obj->timeout;
625 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
627 dprintf ("Set timeout to %i.%06i seconds\n",
628 (int) timeout.tv_sec,
629 (int) timeout.tv_usec);
631 ping_timeval_add (&nowtime, &timeout, &endtime);
633 while (1)
634 {
635 FD_ZERO (&read_fds);
636 FD_ZERO (&err_fds);
637 num_fds = 0;
638 max_fd = -1;
640 for (ptr = ph; ptr != NULL; ptr = ptr->next)
641 {
642 if (!timerisset (ptr->timer))
643 continue;
645 FD_SET (ptr->fd, &read_fds);
646 FD_SET (ptr->fd, &err_fds);
647 num_fds++;
649 if (max_fd < ptr->fd)
650 max_fd = ptr->fd;
651 }
653 if (num_fds == 0)
654 break;
656 if (gettimeofday (&nowtime, NULL) == -1)
657 {
658 ping_set_errno (obj, errno);
659 return (-1);
660 }
662 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
663 break;
665 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
666 (int) timeout.tv_sec,
667 (int) timeout.tv_usec);
669 status = select (max_fd + 1, &read_fds, NULL, &err_fds, &timeout);
671 if (gettimeofday (&nowtime, NULL) == -1)
672 {
673 ping_set_errno (obj, errno);
674 return (-1);
675 }
677 if ((status == -1) && (errno == EINTR))
678 {
679 dprintf ("select was interrupted by signal..\n");
680 continue;
681 }
682 else if (status < 0)
683 {
684 #if WITH_DEBUG
685 char errbuf[PING_ERRMSG_LEN];
686 dprintf ("select: %s\n",
687 sstrerror (errno, errbuf, sizeof (errbuf)));
688 #endif
689 break;
690 }
691 else if (status == 0)
692 {
693 dprintf ("select timed out\n");
694 for (ptr = ph; ptr != NULL; ptr = ptr->next)
695 if (ptr->latency < 0.0)
696 ptr->dropped++;
697 break;
698 }
700 for (ptr = ph; ptr != NULL; ptr = ptr->next)
701 {
702 if (FD_ISSET (ptr->fd, &read_fds))
703 {
704 if (ping_receive_one (obj, ptr, &nowtime) == 0)
705 ret++;
706 }
707 else if (FD_ISSET (ptr->fd, &err_fds))
708 {
709 /* clear the timer in this case so that we
710 * don't run into an endless loop. */
711 /* TODO: Set an error flag in this case. */
712 timerclear (ptr->timer);
713 }
714 }
715 } /* while (1) */
717 return (ret);
718 }
720 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
721 * Sending functions: *
722 * *
723 * ping_send_all *
724 * +-> ping_send_one_ipv4 *
725 * `-> ping_send_one_ipv6 *
726 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
727 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
728 const void *buf, size_t buflen)
729 {
730 ssize_t ret;
732 if (gettimeofday (ph->timer, NULL) == -1)
733 {
734 timerclear (ph->timer);
735 return (-1);
736 }
738 ret = sendto (ph->fd, buf, buflen, 0,
739 (struct sockaddr *) ph->addr, ph->addrlen);
741 if (ret < 0)
742 {
743 #if defined(EHOSTUNREACH)
744 if (errno == EHOSTUNREACH)
745 return (0);
746 #endif
747 #if defined(ENETUNREACH)
748 if (errno == ENETUNREACH)
749 return (0);
750 #endif
751 ping_set_errno (obj, errno);
752 }
754 return (ret);
755 }
757 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
758 {
759 struct icmp *icmp4;
760 int status;
762 char buf[4096];
763 int buflen;
765 char *data;
766 int datalen;
768 dprintf ("ph->hostname = %s\n", ph->hostname);
770 memset (buf, '\0', sizeof (buf));
771 icmp4 = (struct icmp *) buf;
772 data = (char *) (icmp4 + 1);
774 icmp4->icmp_type = ICMP_ECHO;
775 icmp4->icmp_code = 0;
776 icmp4->icmp_cksum = 0;
777 icmp4->icmp_id = htons (ph->ident);
778 icmp4->icmp_seq = htons (ph->sequence);
780 buflen = 4096 - sizeof (struct icmp);
781 strncpy (data, ph->data, buflen);
782 datalen = strlen (data);
784 buflen = datalen + sizeof (struct icmp);
786 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
788 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
790 status = ping_sendto (obj, ph, buf, buflen);
791 if (status < 0)
792 {
793 perror ("ping_sendto");
794 return (-1);
795 }
797 dprintf ("sendto: status = %i\n", status);
799 return (0);
800 }
802 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
803 {
804 struct icmp6_hdr *icmp6;
805 int status;
807 char buf[4096];
808 int buflen;
810 char *data;
811 int datalen;
813 dprintf ("ph->hostname = %s\n", ph->hostname);
815 memset (buf, '\0', sizeof (buf));
816 icmp6 = (struct icmp6_hdr *) buf;
817 data = (char *) (icmp6 + 1);
819 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
820 icmp6->icmp6_code = 0;
821 /* The checksum will be calculated by the TCP/IP stack. */
822 /* FIXME */
823 icmp6->icmp6_cksum = 0;
824 icmp6->icmp6_id = htons (ph->ident);
825 icmp6->icmp6_seq = htons (ph->sequence);
827 buflen = 4096 - sizeof (struct icmp6_hdr);
828 strncpy (data, ph->data, buflen);
829 datalen = strlen (data);
831 buflen = datalen + sizeof (struct icmp6_hdr);
833 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
835 status = ping_sendto (obj, ph, buf, buflen);
836 if (status < 0)
837 {
838 perror ("ping_sendto");
839 return (-1);
840 }
842 dprintf ("sendto: status = %i\n", status);
844 return (0);
845 }
847 static int ping_send_all (pingobj_t *obj)
848 {
849 pinghost_t *ph;
850 pinghost_t *ptr;
852 int ret;
854 ret = 0;
855 ph = obj->head;
857 for (ptr = ph; ptr != NULL; ptr = ptr->next)
858 {
859 /* start timer.. The GNU `ping6' starts the timer before
860 * sending the packet, so I will do that too */
861 if (gettimeofday (ptr->timer, NULL) == -1)
862 {
863 #if WITH_DEBUG
864 char errbuf[PING_ERRMSG_LEN];
865 dprintf ("gettimeofday: %s\n",
866 sstrerror (errno, errbuf, sizeof (errbuf)));
867 #endif
868 timerclear (ptr->timer);
869 ret--;
870 continue;
871 }
872 else
873 {
874 dprintf ("timer set for hostname = %s\n", ptr->hostname);
875 }
877 if (ptr->addrfamily == AF_INET6)
878 {
879 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
880 if (ping_send_one_ipv6 (obj, ptr) != 0)
881 {
882 timerclear (ptr->timer);
883 ret--;
884 continue;
885 }
886 }
887 else if (ptr->addrfamily == AF_INET)
888 {
889 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
890 if (ping_send_one_ipv4 (obj, ptr) != 0)
891 {
892 timerclear (ptr->timer);
893 ret--;
894 continue;
895 }
896 }
897 else /* this should not happen */
898 {
899 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
900 timerclear (ptr->timer);
901 ret--;
902 continue;
903 }
905 ptr->sequence++;
906 }
908 return (ret);
909 }
911 /*
912 * Set the TTL of a socket protocol independently.
913 */
914 static int ping_set_ttl (pinghost_t *ph, int ttl)
915 {
916 int ret = -2;
918 if (ph->addrfamily == AF_INET)
919 {
920 dprintf ("Setting TTLv4 to %i\n", ttl);
921 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
922 &ttl, sizeof (ttl));
923 }
924 else if (ph->addrfamily == AF_INET6)
925 {
926 dprintf ("Setting TTLv6 to %i\n", ttl);
927 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
928 &ttl, sizeof (ttl));
929 }
931 return (ret);
932 }
934 static int ping_get_ident (void)
935 {
936 int fd;
937 static int did_seed = 0;
939 int retval;
941 if (did_seed == 0)
942 {
943 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
944 {
945 unsigned int seed;
947 if (read (fd, &seed, sizeof (seed)) != -1)
948 {
949 did_seed = 1;
950 dprintf ("Random seed: %#x\n", seed);
951 srandom (seed);
952 }
954 close (fd);
955 }
956 #if WITH_DEBUG
957 else
958 {
959 char errbuf[PING_ERRMSG_LEN];
960 dprintf ("open (/dev/urandom): %s\n",
961 sstrerror (errno, errbuf, sizeof (errbuf)));
962 }
963 #endif
964 }
966 retval = (int) random ();
968 dprintf ("Random number: %#x\n", retval);
970 return (retval);
971 }
973 static pinghost_t *ping_alloc (void)
974 {
975 pinghost_t *ph;
976 size_t ph_size;
978 ph_size = sizeof (pinghost_t)
979 + sizeof (struct sockaddr_storage)
980 + sizeof (struct timeval);
982 ph = (pinghost_t *) malloc (ph_size);
983 if (ph == NULL)
984 return (NULL);
986 memset (ph, '\0', ph_size);
988 ph->timer = (struct timeval *) (ph + 1);
989 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
991 ph->addrlen = sizeof (struct sockaddr_storage);
992 ph->fd = -1;
993 ph->latency = -1.0;
994 ph->dropped = 0;
995 ph->ident = ping_get_ident () & 0xFFFF;
997 return (ph);
998 }
1000 static void ping_free (pinghost_t *ph)
1001 {
1002 if (ph->fd >= 0)
1003 close (ph->fd);
1005 if (ph->username != NULL)
1006 free (ph->username);
1008 if (ph->hostname != NULL)
1009 free (ph->hostname);
1011 if (ph->data != NULL)
1012 free (ph->data);
1014 free (ph);
1015 }
1017 /*
1018 * public methods
1019 */
1020 const char *ping_get_error (pingobj_t *obj)
1021 {
1022 if (obj == NULL)
1023 return (NULL);
1024 return (obj->errmsg);
1025 }
1027 pingobj_t *ping_construct (void)
1028 {
1029 pingobj_t *obj;
1031 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1032 return (NULL);
1033 memset (obj, '\0', sizeof (pingobj_t));
1035 obj->timeout = PING_DEF_TIMEOUT;
1036 obj->ttl = PING_DEF_TTL;
1037 obj->addrfamily = PING_DEF_AF;
1038 obj->data = strdup (PING_DEF_DATA);
1040 return (obj);
1041 }
1043 void ping_destroy (pingobj_t *obj)
1044 {
1045 pinghost_t *current;
1046 pinghost_t *next;
1048 if (obj == NULL)
1049 return;
1051 current = obj->head;
1052 next = NULL;
1054 while (current != NULL)
1055 {
1056 next = current->next;
1057 ping_free (current);
1058 current = next;
1059 }
1061 if (obj->data != NULL)
1062 free (obj->data);
1064 if (obj->srcaddr != NULL)
1065 free (obj->srcaddr);
1067 if (obj->device != NULL)
1068 free (obj->device);
1070 free (obj);
1072 return;
1073 }
1075 int ping_setopt (pingobj_t *obj, int option, void *value)
1076 {
1077 int ret = 0;
1079 if ((obj == NULL) || (value == NULL))
1080 return (-1);
1082 switch (option)
1083 {
1084 case PING_OPT_TIMEOUT:
1085 obj->timeout = *((double *) value);
1086 if (obj->timeout < 0.0)
1087 {
1088 obj->timeout = PING_DEF_TIMEOUT;
1089 ret = -1;
1090 }
1091 break;
1093 case PING_OPT_TTL:
1094 obj->ttl = *((int *) value);
1095 if ((obj->ttl < 1) || (obj->ttl > 255))
1096 {
1097 obj->ttl = PING_DEF_TTL;
1098 ret = -1;
1099 }
1100 else
1101 {
1102 pinghost_t *ph;
1104 for (ph = obj->head; ph != NULL; ph = ph->next)
1105 ping_set_ttl (ph, obj->ttl);
1106 }
1107 break;
1109 case PING_OPT_AF:
1110 obj->addrfamily = *((int *) value);
1111 if ((obj->addrfamily != AF_UNSPEC)
1112 && (obj->addrfamily != AF_INET)
1113 && (obj->addrfamily != AF_INET6))
1114 {
1115 obj->addrfamily = PING_DEF_AF;
1116 ret = -1;
1117 }
1118 if (obj->srcaddr != NULL)
1119 {
1120 free (obj->srcaddr);
1121 obj->srcaddr = NULL;
1122 }
1123 break;
1125 case PING_OPT_DATA:
1126 if (obj->data != NULL)
1127 {
1128 free (obj->data);
1129 obj->data = NULL;
1130 }
1131 obj->data = strdup ((const char *) value);
1132 break;
1134 case PING_OPT_SOURCE:
1135 {
1136 char *hostname = (char *) value;
1137 struct addrinfo ai_hints;
1138 struct addrinfo *ai_list;
1139 int status;
1140 #if WITH_DEBUG
1141 if (obj->addrfamily != AF_UNSPEC)
1142 {
1143 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1144 }
1145 #endif
1146 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1147 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1148 #if defined(AI_ADDRCONFIG)
1149 ai_hints.ai_flags = AI_ADDRCONFIG;
1150 #endif
1151 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1152 if (status != 0)
1153 {
1154 #if defined(EAI_SYSTEM)
1155 char errbuf[PING_ERRMSG_LEN];
1156 #endif
1157 ping_set_error (obj, "getaddrinfo",
1158 #if defined(EAI_SYSTEM)
1159 (status == EAI_SYSTEM)
1160 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1161 #endif
1162 gai_strerror (status));
1163 ret = -1;
1164 break;
1165 }
1166 #if WITH_DEBUG
1167 if (ai_list->ai_next != NULL)
1168 {
1169 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1170 }
1171 #endif
1172 if (obj->srcaddr == NULL)
1173 {
1174 obj->srcaddrlen = 0;
1175 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1176 if (obj->srcaddr == NULL)
1177 {
1178 ping_set_errno (obj, errno);
1179 ret = -1;
1180 freeaddrinfo (ai_list);
1181 break;
1182 }
1183 }
1184 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1185 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1186 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1187 ai_list->ai_addrlen);
1188 obj->srcaddrlen = ai_list->ai_addrlen;
1189 obj->addrfamily = ai_list->ai_family;
1191 freeaddrinfo (ai_list);
1192 } /* case PING_OPT_SOURCE */
1193 break;
1195 case PING_OPT_DEVICE:
1196 {
1197 #ifdef SO_BINDTODEVICE
1198 char *device = strdup ((char *) value);
1200 if (device == NULL)
1201 {
1202 ping_set_errno (obj, errno);
1203 ret = -1;
1204 break;
1205 }
1207 if (obj->device != NULL)
1208 free (obj->device);
1209 obj->device = device;
1210 #else /* ! SO_BINDTODEVICE */
1211 ping_set_errno (obj, ENOTSUP);
1212 ret = -1;
1213 #endif /* ! SO_BINDTODEVICE */
1214 } /* case PING_OPT_DEVICE */
1215 break;
1217 default:
1218 ret = -2;
1219 } /* switch (option) */
1221 return (ret);
1222 } /* int ping_setopt */
1225 int ping_send (pingobj_t *obj)
1226 {
1227 int ret;
1229 if (obj == NULL)
1230 return (-1);
1232 if (ping_send_all (obj) < 0)
1233 return (-1);
1235 if ((ret = ping_receive_all (obj)) < 0)
1236 return (-2);
1238 return (ret);
1239 }
1241 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1242 {
1243 while (ph != NULL)
1244 {
1245 if (strcasecmp (ph->username, host) == 0)
1246 break;
1248 ph = ph->next;
1249 }
1251 return (ph);
1252 }
1254 int ping_host_add (pingobj_t *obj, const char *host)
1255 {
1256 pinghost_t *ph;
1258 struct addrinfo ai_hints;
1259 struct addrinfo *ai_list, *ai_ptr;
1260 int ai_return;
1262 if ((obj == NULL) || (host == NULL))
1263 return (-1);
1265 dprintf ("host = %s\n", host);
1267 if (ping_host_search (obj->head, host) != NULL)
1268 return (0);
1270 memset (&ai_hints, '\0', sizeof (ai_hints));
1271 ai_hints.ai_flags = 0;
1272 #ifdef AI_ADDRCONFIG
1273 ai_hints.ai_flags |= AI_ADDRCONFIG;
1274 #endif
1275 #ifdef AI_CANONNAME
1276 ai_hints.ai_flags |= AI_CANONNAME;
1277 #endif
1278 ai_hints.ai_family = obj->addrfamily;
1279 ai_hints.ai_socktype = SOCK_RAW;
1281 if ((ph = ping_alloc ()) == NULL)
1282 {
1283 dprintf ("Out of memory!\n");
1284 return (-1);
1285 }
1287 if ((ph->username = strdup (host)) == NULL)
1288 {
1289 dprintf ("Out of memory!\n");
1290 ping_set_errno (obj, errno);
1291 ping_free (ph);
1292 return (-1);
1293 }
1295 if ((ph->hostname = strdup (host)) == NULL)
1296 {
1297 dprintf ("Out of memory!\n");
1298 ping_set_errno (obj, errno);
1299 ping_free (ph);
1300 return (-1);
1301 }
1303 /* obj->data is not garuanteed to be != NULL */
1304 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1305 {
1306 dprintf ("Out of memory!\n");
1307 ping_set_errno (obj, errno);
1308 ping_free (ph);
1309 return (-1);
1310 }
1312 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1313 {
1314 #if defined(EAI_SYSTEM)
1315 char errbuf[PING_ERRMSG_LEN];
1316 #endif
1317 dprintf ("getaddrinfo failed\n");
1318 ping_set_error (obj, "getaddrinfo",
1319 #if defined(EAI_SYSTEM)
1320 (ai_return == EAI_SYSTEM)
1321 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1322 #endif
1323 gai_strerror (ai_return));
1324 ping_free (ph);
1325 return (-1);
1326 }
1328 if (ai_list == NULL)
1329 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1331 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1332 {
1333 ph->fd = -1;
1335 if (ai_ptr->ai_family == AF_INET)
1336 {
1337 ai_ptr->ai_socktype = SOCK_RAW;
1338 ai_ptr->ai_protocol = IPPROTO_ICMP;
1339 }
1340 else if (ai_ptr->ai_family == AF_INET6)
1341 {
1342 ai_ptr->ai_socktype = SOCK_RAW;
1343 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1344 }
1345 else
1346 {
1347 char errmsg[PING_ERRMSG_LEN];
1349 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1350 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1352 dprintf (errmsg);
1353 ping_set_error (obj, "getaddrinfo", errmsg);
1354 continue;
1355 }
1357 /* TODO: Move this to a static function `ping_open_socket' and
1358 * call it whenever the socket dies. */
1359 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1360 if (ph->fd == -1)
1361 {
1362 #if WITH_DEBUG
1363 char errbuf[PING_ERRMSG_LEN];
1364 dprintf ("socket: %s\n",
1365 sstrerror (errno, errbuf, sizeof (errbuf)));
1366 #endif
1367 ping_set_errno (obj, errno);
1368 continue;
1369 }
1371 if (obj->srcaddr != NULL)
1372 {
1373 assert (obj->srcaddrlen > 0);
1374 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1376 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1377 {
1378 #if WITH_DEBUG
1379 char errbuf[PING_ERRMSG_LEN];
1380 dprintf ("bind: %s\n",
1381 sstrerror (errno, errbuf, sizeof (errbuf)));
1382 #endif
1383 ping_set_errno (obj, errno);
1384 close (ph->fd);
1385 ph->fd = -1;
1386 continue;
1387 }
1388 }
1390 #ifdef SO_BINDTODEVICE
1391 if (obj->device != NULL)
1392 {
1393 if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE,
1394 obj->device, strlen (obj->device) + 1) != 0)
1395 {
1396 #if WITH_DEBUG
1397 char errbuf[PING_ERRMSG_LEN];
1398 dprintf ("setsockopt: %s\n",
1399 sstrerror (errno, errbuf, sizeof (errbuf)));
1400 #endif
1401 ping_set_errno (obj, errno);
1402 close (ph->fd);
1403 ph->fd = -1;
1404 continue;
1405 }
1406 }
1407 #endif /* SO_BINDTODEVICE */
1409 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1410 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1411 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1412 ph->addrlen = ai_ptr->ai_addrlen;
1413 ph->addrfamily = ai_ptr->ai_family;
1415 #ifdef AI_CANONNAME
1416 if ((ai_ptr->ai_canonname != NULL)
1417 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1418 {
1419 char *old_hostname;
1421 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1422 ph->hostname, ai_ptr->ai_canonname);
1424 old_hostname = ph->hostname;
1425 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1426 {
1427 /* strdup failed, falling back to old hostname */
1428 ph->hostname = old_hostname;
1429 }
1430 else if (old_hostname != NULL)
1431 {
1432 free (old_hostname);
1433 }
1434 }
1435 #endif /* AI_CANONNAME */
1437 if (ph->addrfamily == AF_INET)
1438 {
1439 int opt = 1;
1441 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1442 &opt, sizeof (opt));
1443 }
1444 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1445 else if (ph->addrfamily == AF_INET6)
1446 {
1447 int opt = 1;
1449 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1450 &opt, sizeof (opt));
1451 }
1452 #endif
1454 break;
1455 }
1457 freeaddrinfo (ai_list);
1459 if (ph->fd < 0)
1460 {
1461 ping_free (ph);
1462 return (-1);
1463 }
1465 /*
1466 * Adding in the front is much easier, but then the iterator will
1467 * return the host that was added last as first host. That's just not
1468 * nice. -octo
1469 */
1470 if (obj->head == NULL)
1471 {
1472 obj->head = ph;
1473 }
1474 else
1475 {
1476 pinghost_t *hptr;
1478 hptr = obj->head;
1479 while (hptr->next != NULL)
1480 hptr = hptr->next;
1482 assert ((hptr != NULL) && (hptr->next == NULL));
1483 hptr->next = ph;
1484 }
1486 ping_set_ttl (ph, obj->ttl);
1488 return (0);
1489 } /* int ping_host_add */
1491 int ping_host_remove (pingobj_t *obj, const char *host)
1492 {
1493 pinghost_t *pre, *cur;
1495 if ((obj == NULL) || (host == NULL))
1496 return (-1);
1498 pre = NULL;
1499 cur = obj->head;
1501 while (cur != NULL)
1502 {
1503 if (strcasecmp (host, cur->username) == 0)
1504 break;
1506 pre = cur;
1507 cur = cur->next;
1508 }
1510 if (cur == NULL)
1511 {
1512 ping_set_error (obj, "ping_host_remove", "Host not found");
1513 return (-1);
1514 }
1516 if (pre == NULL)
1517 obj->head = cur->next;
1518 else
1519 pre->next = cur->next;
1521 ping_free (cur);
1523 return (0);
1524 }
1526 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1527 {
1528 if (obj == NULL)
1529 return (NULL);
1530 return ((pingobj_iter_t *) obj->head);
1531 }
1533 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1534 {
1535 if (iter == NULL)
1536 return (NULL);
1537 return ((pingobj_iter_t *) iter->next);
1538 }
1540 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1541 void *buffer, size_t *buffer_len)
1542 {
1543 int ret = EINVAL;
1545 size_t orig_buffer_len = *buffer_len;
1547 if ((iter == NULL) || (buffer_len == NULL))
1548 return (-1);
1550 if ((buffer == NULL) && (*buffer_len != 0 ))
1551 return (-1);
1553 switch (info)
1554 {
1555 case PING_INFO_USERNAME:
1556 ret = ENOMEM;
1557 *buffer_len = strlen (iter->username) + 1;
1558 if (orig_buffer_len <= *buffer_len)
1559 break;
1560 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1561 * will copy `*buffer_len' and pad the rest of
1562 * `buffer' with null-bytes */
1563 strncpy (buffer, iter->username, orig_buffer_len);
1564 ret = 0;
1565 break;
1567 case PING_INFO_HOSTNAME:
1568 ret = ENOMEM;
1569 *buffer_len = strlen (iter->hostname) + 1;
1570 if (orig_buffer_len < *buffer_len)
1571 break;
1572 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1573 * will copy `*buffer_len' and pad the rest of
1574 * `buffer' with null-bytes */
1575 strncpy (buffer, iter->hostname, orig_buffer_len);
1576 ret = 0;
1577 break;
1579 case PING_INFO_ADDRESS:
1580 ret = getnameinfo ((struct sockaddr *) iter->addr,
1581 iter->addrlen,
1582 (char *) buffer,
1583 *buffer_len,
1584 NULL, 0,
1585 NI_NUMERICHOST);
1586 if (ret != 0)
1587 {
1588 if ((ret == EAI_MEMORY)
1589 #ifdef EAI_OVERFLOW
1590 || (ret == EAI_OVERFLOW)
1591 #endif
1592 )
1593 ret = ENOMEM;
1594 #if defined(EAI_SYSTEM)
1595 else if (ret == EAI_SYSTEM)
1596 ret = errno;
1597 #endif
1598 else
1599 ret = EINVAL;
1600 }
1601 break;
1603 case PING_INFO_FAMILY:
1604 ret = ENOMEM;
1605 *buffer_len = sizeof (int);
1606 if (orig_buffer_len < sizeof (int))
1607 break;
1608 *((int *) buffer) = iter->addrfamily;
1609 ret = 0;
1610 break;
1612 case PING_INFO_LATENCY:
1613 ret = ENOMEM;
1614 *buffer_len = sizeof (double);
1615 if (orig_buffer_len < sizeof (double))
1616 break;
1617 *((double *) buffer) = iter->latency;
1618 ret = 0;
1619 break;
1621 case PING_INFO_DROPPED:
1622 ret = ENOMEM;
1623 *buffer_len = sizeof (uint32_t);
1624 if (orig_buffer_len < sizeof (uint32_t))
1625 break;
1626 *((uint32_t *) buffer) = iter->dropped;
1627 ret = 0;
1628 break;
1630 case PING_INFO_SEQUENCE:
1631 ret = ENOMEM;
1632 *buffer_len = sizeof (unsigned int);
1633 if (orig_buffer_len < sizeof (unsigned int))
1634 break;
1635 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1636 ret = 0;
1637 break;
1639 case PING_INFO_IDENT:
1640 ret = ENOMEM;
1641 *buffer_len = sizeof (uint16_t);
1642 if (orig_buffer_len < sizeof (uint16_t))
1643 break;
1644 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1645 ret = 0;
1646 break;
1648 case PING_INFO_DATA:
1649 ret = ENOMEM;
1650 *buffer_len = strlen (iter->data);
1651 if (orig_buffer_len < *buffer_len)
1652 break;
1653 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1654 ret = 0;
1655 break;
1657 case PING_INFO_RECV_TTL:
1658 ret = ENOMEM;
1659 *buffer_len = sizeof (int);
1660 if (orig_buffer_len < sizeof (int))
1661 break;
1662 *((int *) buffer) = iter->recv_ttl;
1663 ret = 0;
1664 break;
1665 }
1667 return (ret);
1668 } /* ping_iterator_get_info */
1670 void *ping_iterator_get_context (pingobj_iter_t *iter)
1671 {
1672 if (iter == NULL)
1673 return (NULL);
1674 return (iter->context);
1675 }
1677 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1678 {
1679 if (iter == NULL)
1680 return;
1681 iter->context = context;
1682 }