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 uint8_t recv_qos;
121 char *data;
123 void *context;
125 struct pinghost *next;
126 };
128 struct pingobj
129 {
130 double timeout;
131 int ttl;
132 int addrfamily;
133 uint8_t qos;
134 char *data;
136 struct sockaddr *srcaddr;
137 socklen_t srcaddrlen;
139 char *device;
141 char errmsg[PING_ERRMSG_LEN];
143 pinghost_t *head;
144 };
146 /*
147 * private (static) functions
148 */
149 /* Even though Posix requires "strerror_r" to return an "int",
150 * some systems (e.g. the GNU libc) return a "char *" _and_
151 * ignore the second argument ... -tokkee */
152 static char *sstrerror (int errnum, char *buf, size_t buflen)
153 {
154 buf[0] = 0;
156 #if !HAVE_STRERROR_R
157 {
158 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
159 }
160 /* #endif !HAVE_STRERROR_R */
162 #elif STRERROR_R_CHAR_P
163 {
164 char *temp;
165 temp = strerror_r (errnum, buf, buflen);
166 if (buf[0] == 0)
167 {
168 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
169 strncpy (buf, temp, buflen);
170 else
171 strncpy (buf, "strerror_r did not return "
172 "an error message", buflen);
173 }
174 }
175 /* #endif STRERROR_R_CHAR_P */
177 #else
178 if (strerror_r (errnum, buf, buflen) != 0)
179 {
180 snprintf (buf, buflen, "Error %i (%#x); "
181 "Additionally, strerror_r failed.",
182 errnum, errnum);
183 }
184 #endif /* STRERROR_R_CHAR_P */
186 buf[buflen - 1] = 0;
188 return (buf);
189 } /* char *sstrerror */
191 static void ping_set_error (pingobj_t *obj, const char *function,
192 const char *message)
193 {
194 snprintf (obj->errmsg, sizeof (obj->errmsg),
195 "%s: %s", function, message);
196 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
197 }
199 static void ping_set_errno (pingobj_t *obj, int error_number)
200 {
201 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
202 }
204 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
205 struct timeval *res)
206 {
207 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
208 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
210 while (res->tv_usec > 1000000)
211 {
212 res->tv_usec -= 1000000;
213 res->tv_sec++;
214 }
216 return (0);
217 }
219 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
220 struct timeval *res)
221 {
222 if ((tv1->tv_sec < tv2->tv_sec)
223 || ((tv1->tv_sec == tv2->tv_sec)
224 && (tv1->tv_usec < tv2->tv_usec)))
225 return (-1);
227 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
228 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
230 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
232 while (res->tv_usec < 0)
233 {
234 res->tv_usec += 1000000;
235 res->tv_sec--;
236 }
238 return (0);
239 }
241 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
242 {
243 uint32_t sum = 0;
244 uint16_t ret = 0;
246 uint16_t *ptr;
248 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
249 sum += *ptr;
251 if (len == 1)
252 {
253 *(char *) &ret = *(char *) ptr;
254 sum += ret;
255 }
257 /* Do this twice to get all possible carries.. */
258 sum = (sum >> 16) + (sum & 0xFFFF);
259 sum = (sum >> 16) + (sum & 0xFFFF);
261 ret = ~sum;
263 return (ret);
264 }
266 static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
267 size_t buffer_len)
268 {
269 struct ip *ip_hdr;
270 struct icmp *icmp_hdr;
272 size_t ip_hdr_len;
274 uint16_t recv_checksum;
275 uint16_t calc_checksum;
277 uint16_t ident;
278 uint16_t seq;
280 pinghost_t *ptr;
282 if (buffer_len < sizeof (struct ip))
283 return (NULL);
285 ip_hdr = (struct ip *) buffer;
286 ip_hdr_len = ip_hdr->ip_hl << 2;
288 if (buffer_len < ip_hdr_len)
289 return (NULL);
291 buffer += ip_hdr_len;
292 buffer_len -= ip_hdr_len;
294 if (buffer_len < sizeof (struct icmp))
295 return (NULL);
297 icmp_hdr = (struct icmp *) buffer;
298 buffer += sizeof (struct icmp);
299 buffer_len -= sizeof (struct icmp);
301 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
302 {
303 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
304 return (NULL);
305 }
307 recv_checksum = icmp_hdr->icmp_cksum;
308 icmp_hdr->icmp_cksum = 0;
309 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
310 sizeof (struct icmp) + buffer_len);
312 if (recv_checksum != calc_checksum)
313 {
314 dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
315 "calculated 0x%04"PRIx16"\n",
316 recv_checksum, calc_checksum);
317 return (NULL);
318 }
320 ident = ntohs (icmp_hdr->icmp_id);
321 seq = ntohs (icmp_hdr->icmp_seq);
323 /* We have to iterate over all hosts, since ICMPv4 packets may
324 * be received on any raw v4 socket. */
325 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
326 {
327 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
328 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
330 if (ptr->addrfamily != AF_INET)
331 continue;
333 if (!timerisset (ptr->timer))
334 continue;
336 if (ptr->ident != ident)
337 continue;
339 if (((ptr->sequence - 1) & 0xFFFF) != seq)
340 continue;
342 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
343 "seq = %"PRIu16"\n",
344 ptr->hostname, ident, seq);
346 break;
347 }
349 if (ptr == NULL)
350 {
351 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
352 ident, seq);
353 }
355 if (ptr != NULL){
356 ptr->recv_ttl = (int) ip_hdr->ip_ttl;
357 ptr->recv_qos = (uint8_t) ip_hdr->ip_tos;
358 }
359 return (ptr);
360 }
362 #ifndef ICMP6_ECHO_REQUEST
363 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
364 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
365 # else
366 # define ICMP6_ECHO_REQUEST 128
367 # endif
368 #endif
370 #ifndef ICMP6_ECHO_REPLY
371 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
372 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
373 # else
374 # define ICMP6_ECHO_REPLY 129
375 # endif
376 #endif
378 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
379 size_t buffer_len)
380 {
381 struct icmp6_hdr *icmp_hdr;
383 uint16_t ident;
384 uint16_t seq;
386 pinghost_t *ptr;
388 if (buffer_len < sizeof (struct icmp6_hdr))
389 return (NULL);
391 icmp_hdr = (struct icmp6_hdr *) buffer;
392 buffer += sizeof (struct icmp);
393 buffer_len -= sizeof (struct icmp);
395 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
396 {
397 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
398 return (NULL);
399 }
401 if (icmp_hdr->icmp6_code != 0)
402 {
403 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
404 return (NULL);
405 }
407 ident = ntohs (icmp_hdr->icmp6_id);
408 seq = ntohs (icmp_hdr->icmp6_seq);
410 /* We have to iterate over all hosts, since ICMPv6 packets may
411 * be received on any raw v6 socket. */
412 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
413 {
414 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
415 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
417 if (ptr->addrfamily != AF_INET6)
418 continue;
420 if (!timerisset (ptr->timer))
421 continue;
423 if (ptr->ident != ident)
424 continue;
426 if (((ptr->sequence - 1) & 0xFFFF) != seq)
427 continue;
429 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
430 "seq = %"PRIu16"\n",
431 ptr->hostname, ident, seq);
433 break;
434 }
436 if (ptr == NULL)
437 {
438 dprintf ("No match found for ident = 0x%04"PRIx16", "
439 "seq = %"PRIu16"\n",
440 ident, seq);
441 }
443 return (ptr);
444 }
446 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
447 struct timeval *now)
448 {
449 /* Note: 'ph' is not necessarily the host object for which we receive a
450 * reply. The right object will be returned by ping_receive_ipv*(). For
451 * now, we can only rely on ph->fd and ph->addrfamily. */
453 struct timeval diff, pkt_now = *now;
454 pinghost_t *host = NULL;
455 int recv_ttl;
456 uint8_t recv_qos;
458 /*
459 * Set up the receive buffer..
460 */
461 struct msghdr msghdr;
462 struct cmsghdr *cmsg;
463 char payload_buffer[4096];
464 ssize_t payload_buffer_len;
465 char control_buffer[4096];
466 struct iovec payload_iovec;
468 memset (&payload_iovec, 0, sizeof (payload_iovec));
469 payload_iovec.iov_base = payload_buffer;
470 payload_iovec.iov_len = sizeof (payload_buffer);
472 memset (&msghdr, 0, sizeof (msghdr));
473 /* unspecified source address */
474 msghdr.msg_name = NULL;
475 msghdr.msg_namelen = 0;
476 /* output buffer vector, see readv(2) */
477 msghdr.msg_iov = &payload_iovec;
478 msghdr.msg_iovlen = 1;
479 /* output buffer for control messages */
480 msghdr.msg_control = control_buffer;
481 msghdr.msg_controllen = sizeof (control_buffer);
482 /* flags; this is an output only field.. */
483 msghdr.msg_flags = 0;
484 #ifdef MSG_XPG4_2
485 msghdr.msg_flags |= MSG_XPG4_2;
486 #endif
488 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
489 if (payload_buffer_len < 0)
490 {
491 #if WITH_DEBUG
492 char errbuf[PING_ERRMSG_LEN];
493 dprintf ("recvfrom: %s\n",
494 sstrerror (errno, errbuf, sizeof (errbuf)));
495 #endif
496 return (-1);
497 }
498 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
500 /* Iterate over all auxiliary data in msghdr */
501 recv_ttl = -1;
502 recv_qos = 0;
503 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
504 cmsg != NULL;
505 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
506 {
507 #ifdef SO_TIMESTAMP
508 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMP)
509 memcpy(&pkt_now, CMSG_DATA(cmsg), sizeof(pkt_now));
510 #endif /* SO_TIMESTAMP */
512 if (ph->addrfamily == AF_INET) /* {{{ */
513 {
514 if (cmsg->cmsg_level != IPPROTO_IP)
515 continue;
517 if (cmsg->cmsg_type == IP_TOS)
518 {
519 memcpy (&recv_qos, CMSG_DATA (cmsg),
520 sizeof (recv_qos));
521 dprintf ("TOSv4 = 0x%02"PRIx8";\n", recv_qos);
522 } else
523 if (cmsg->cmsg_type == IP_TTL)
524 {
525 memcpy (&recv_ttl, CMSG_DATA (cmsg),
526 sizeof (recv_ttl));
527 dprintf ("TTLv4 = %i;\n", recv_ttl);
528 }
529 else
530 {
531 dprintf ("Not handling option %i.\n",
532 cmsg->cmsg_type);
533 }
534 } /* }}} */
535 else if (ph->addrfamily == AF_INET6) /* {{{ */
536 {
537 if (cmsg->cmsg_level != IPPROTO_IPV6)
538 continue;
540 if (cmsg->cmsg_type == IPV6_TCLASS)
541 {
542 memcpy (&recv_qos, CMSG_DATA (cmsg),
543 sizeof (recv_qos));
544 dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
545 } else
546 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
547 {
548 memcpy (&recv_ttl, CMSG_DATA (cmsg),
549 sizeof (recv_ttl));
550 dprintf ("TTLv6 = %i;\n", recv_ttl);
551 }
552 else
553 {
554 dprintf ("Not handling option %i.\n",
555 cmsg->cmsg_type);
556 }
557 } /* }}} */
558 else
559 {
560 dprintf ("Don't know how to handle "
561 "unknown protocol %i.\n",
562 cmsg->cmsg_level);
563 }
564 } /* }}} for (cmsg) */
566 if (ph->addrfamily == AF_INET)
567 {
568 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
569 if (host == NULL)
570 return (-1);
571 }
572 else if (ph->addrfamily == AF_INET6)
573 {
574 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
575 if (host == NULL)
576 return (-1);
577 }
578 else
579 {
580 dprintf ("ping_receive_one: Unknown address family %i.\n",
581 ph->addrfamily);
582 return (-1);
583 }
585 dprintf ("rcvd: %12i.%06i\n",
586 (int) pkt_now.tv_sec,
587 (int) pkt_now.tv_usec);
588 dprintf ("sent: %12i.%06i\n",
589 (int) host->timer->tv_sec,
590 (int) host->timer->tv_usec);
592 if (ping_timeval_sub (&pkt_now, host->timer, &diff) < 0)
593 {
594 timerclear (host->timer);
595 return (-1);
596 }
598 dprintf ("diff: %12i.%06i\n",
599 (int) diff.tv_sec,
600 (int) diff.tv_usec);
602 if (recv_ttl >= 0)
603 host->recv_ttl = recv_ttl;
604 host->recv_qos = recv_qos;
606 host->latency = ((double) diff.tv_usec) / 1000.0;
607 host->latency += ((double) diff.tv_sec) * 1000.0;
609 timerclear (host->timer);
611 return (0);
612 }
614 static int ping_receive_all (pingobj_t *obj)
615 {
616 fd_set read_fds;
617 fd_set err_fds;
618 int num_fds;
619 int max_fd;
621 pinghost_t *ph;
622 pinghost_t *ptr;
624 struct timeval endtime;
625 struct timeval nowtime;
626 struct timeval timeout;
627 int status;
629 int ret;
631 ph = obj->head;
632 ret = 0;
634 for (ptr = ph; ptr != NULL; ptr = ptr->next)
635 {
636 ptr->latency = -1.0;
637 ptr->recv_ttl = -1;
638 }
640 if (gettimeofday (&nowtime, NULL) == -1)
641 {
642 ping_set_errno (obj, errno);
643 return (-1);
644 }
646 /* Set up timeout */
647 timeout.tv_sec = (time_t) obj->timeout;
648 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
650 dprintf ("Set timeout to %i.%06i seconds\n",
651 (int) timeout.tv_sec,
652 (int) timeout.tv_usec);
654 ping_timeval_add (&nowtime, &timeout, &endtime);
656 while (1)
657 {
658 FD_ZERO (&read_fds);
659 FD_ZERO (&err_fds);
660 num_fds = 0;
661 max_fd = -1;
663 for (ptr = ph; ptr != NULL; ptr = ptr->next)
664 {
665 if (!timerisset (ptr->timer))
666 continue;
668 FD_SET (ptr->fd, &read_fds);
669 FD_SET (ptr->fd, &err_fds);
670 num_fds++;
672 if (max_fd < ptr->fd)
673 max_fd = ptr->fd;
674 }
676 if (num_fds == 0)
677 break;
679 if (gettimeofday (&nowtime, NULL) == -1)
680 {
681 ping_set_errno (obj, errno);
682 return (-1);
683 }
685 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
686 break;
688 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
689 (int) timeout.tv_sec,
690 (int) timeout.tv_usec);
692 status = select (max_fd + 1, &read_fds, NULL, &err_fds, &timeout);
694 if (gettimeofday (&nowtime, NULL) == -1)
695 {
696 ping_set_errno (obj, errno);
697 return (-1);
698 }
700 if ((status == -1) && (errno == EINTR))
701 {
702 dprintf ("select was interrupted by signal..\n");
703 continue;
704 }
705 else if (status < 0)
706 {
707 #if WITH_DEBUG
708 char errbuf[PING_ERRMSG_LEN];
709 dprintf ("select: %s\n",
710 sstrerror (errno, errbuf, sizeof (errbuf)));
711 #endif
712 break;
713 }
714 else if (status == 0)
715 {
716 dprintf ("select timed out\n");
717 for (ptr = ph; ptr != NULL; ptr = ptr->next)
718 if (ptr->latency < 0.0)
719 ptr->dropped++;
720 break;
721 }
723 for (ptr = ph; ptr != NULL; ptr = ptr->next)
724 {
725 if (FD_ISSET (ptr->fd, &read_fds))
726 {
727 if (ping_receive_one (obj, ptr, &nowtime) == 0)
728 ret++;
729 }
730 else if (FD_ISSET (ptr->fd, &err_fds))
731 {
732 /* clear the timer in this case so that we
733 * don't run into an endless loop. */
734 /* TODO: Set an error flag in this case. */
735 timerclear (ptr->timer);
736 }
737 }
738 } /* while (1) */
740 return (ret);
741 }
743 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
744 * Sending functions: *
745 * *
746 * ping_send_all *
747 * +-> ping_send_one_ipv4 *
748 * `-> ping_send_one_ipv6 *
749 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
750 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
751 const void *buf, size_t buflen)
752 {
753 ssize_t ret;
755 if (gettimeofday (ph->timer, NULL) == -1)
756 {
757 timerclear (ph->timer);
758 return (-1);
759 }
761 ret = sendto (ph->fd, buf, buflen, 0,
762 (struct sockaddr *) ph->addr, ph->addrlen);
764 if (ret < 0)
765 {
766 #if defined(EHOSTUNREACH)
767 if (errno == EHOSTUNREACH)
768 return (0);
769 #endif
770 #if defined(ENETUNREACH)
771 if (errno == ENETUNREACH)
772 return (0);
773 #endif
774 ping_set_errno (obj, errno);
775 }
777 return (ret);
778 }
780 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
781 {
782 struct icmp *icmp4;
783 int status;
785 char buf[4096];
786 int buflen;
788 char *data;
789 int datalen;
791 dprintf ("ph->hostname = %s\n", ph->hostname);
793 memset (buf, '\0', sizeof (buf));
794 icmp4 = (struct icmp *) buf;
795 data = (char *) (icmp4 + 1);
797 icmp4->icmp_type = ICMP_ECHO;
798 icmp4->icmp_code = 0;
799 icmp4->icmp_cksum = 0;
800 icmp4->icmp_id = htons (ph->ident);
801 icmp4->icmp_seq = htons (ph->sequence);
803 buflen = 4096 - sizeof (struct icmp);
804 strncpy (data, ph->data, buflen);
805 datalen = strlen (data);
807 buflen = datalen + sizeof (struct icmp);
809 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
811 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
813 status = ping_sendto (obj, ph, buf, buflen);
814 if (status < 0)
815 {
816 perror ("ping_sendto");
817 return (-1);
818 }
820 dprintf ("sendto: status = %i\n", status);
822 return (0);
823 }
825 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
826 {
827 struct icmp6_hdr *icmp6;
828 int status;
830 char buf[4096];
831 int buflen;
833 char *data;
834 int datalen;
836 dprintf ("ph->hostname = %s\n", ph->hostname);
838 memset (buf, '\0', sizeof (buf));
839 icmp6 = (struct icmp6_hdr *) buf;
840 data = (char *) (icmp6 + 1);
842 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
843 icmp6->icmp6_code = 0;
844 /* The checksum will be calculated by the TCP/IP stack. */
845 /* FIXME */
846 icmp6->icmp6_cksum = 0;
847 icmp6->icmp6_id = htons (ph->ident);
848 icmp6->icmp6_seq = htons (ph->sequence);
850 buflen = 4096 - sizeof (struct icmp6_hdr);
851 strncpy (data, ph->data, buflen);
852 datalen = strlen (data);
854 buflen = datalen + sizeof (struct icmp6_hdr);
856 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
858 status = ping_sendto (obj, ph, buf, buflen);
859 if (status < 0)
860 {
861 perror ("ping_sendto");
862 return (-1);
863 }
865 dprintf ("sendto: status = %i\n", status);
867 return (0);
868 }
870 static int ping_send_all (pingobj_t *obj)
871 {
872 pinghost_t *ph;
873 pinghost_t *ptr;
875 int ret;
877 ret = 0;
878 ph = obj->head;
880 for (ptr = ph; ptr != NULL; ptr = ptr->next)
881 {
882 /* start timer.. The GNU `ping6' starts the timer before
883 * sending the packet, so I will do that too */
884 if (gettimeofday (ptr->timer, NULL) == -1)
885 {
886 #if WITH_DEBUG
887 char errbuf[PING_ERRMSG_LEN];
888 dprintf ("gettimeofday: %s\n",
889 sstrerror (errno, errbuf, sizeof (errbuf)));
890 #endif
891 timerclear (ptr->timer);
892 ret--;
893 continue;
894 }
895 else
896 {
897 dprintf ("timer set for hostname = %s\n", ptr->hostname);
898 }
900 if (ptr->addrfamily == AF_INET6)
901 {
902 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
903 if (ping_send_one_ipv6 (obj, ptr) != 0)
904 {
905 timerclear (ptr->timer);
906 ret--;
907 continue;
908 }
909 }
910 else if (ptr->addrfamily == AF_INET)
911 {
912 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
913 if (ping_send_one_ipv4 (obj, ptr) != 0)
914 {
915 timerclear (ptr->timer);
916 ret--;
917 continue;
918 }
919 }
920 else /* this should not happen */
921 {
922 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
923 timerclear (ptr->timer);
924 ret--;
925 continue;
926 }
928 ptr->sequence++;
929 }
931 return (ret);
932 }
934 /*
935 * Set the TTL of a socket protocol independently.
936 */
937 static int ping_set_ttl (pinghost_t *ph, int ttl)
938 {
939 int ret = -2;
941 if (ph->addrfamily == AF_INET)
942 {
943 dprintf ("Setting TTLv4 to %i\n", ttl);
944 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
945 &ttl, sizeof (ttl));
946 }
947 else if (ph->addrfamily == AF_INET6)
948 {
949 dprintf ("Setting TTLv6 to %i\n", ttl);
950 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
951 &ttl, sizeof (ttl));
952 }
954 return (ret);
955 }
957 /*
958 * Set the TOS of a socket protocol independently.
959 *
960 * Using SOL_SOCKET / SO_PRIORITY might be a protocol independent way to
961 * set this. See socket(7) for details.
962 */
963 static int ping_set_qos (pingobj_t *obj, pinghost_t *ph, uint8_t qos)
964 {
965 int ret = EINVAL;
966 char errbuf[PING_ERRMSG_LEN];
968 if (ph->addrfamily == AF_INET)
969 {
970 dprintf ("Setting TP_TOS to %#04"PRIx8"\n", qos);
971 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
972 &qos, sizeof (qos));
973 if (ret != 0)
974 {
975 ret = errno;
976 ping_set_error (obj, "ping_set_qos",
977 sstrerror (ret, errbuf, sizeof (errbuf)));
978 dprintf ("Setting TP_TOS failed: %s\n", errbuf);
979 }
980 }
981 else if (ph->addrfamily == AF_INET6)
982 {
983 /* IPV6_TCLASS requires an "int". */
984 int tmp = (int) qos;
986 dprintf ("Setting IPV6_TCLASS to %#04"PRIx8" (%i)\n", qos, tmp);
987 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_TCLASS,
988 &tmp, sizeof (tmp));
989 if (ret != 0)
990 {
991 ret = errno;
992 ping_set_error (obj, "ping_set_qos",
993 sstrerror (ret, errbuf, sizeof (errbuf)));
994 dprintf ("Setting IPV6_TCLASS failed: %s\n", errbuf);
995 }
996 }
998 return (ret);
999 }
1001 static int ping_get_ident (void)
1002 {
1003 int fd;
1004 static int did_seed = 0;
1006 int retval;
1008 if (did_seed == 0)
1009 {
1010 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
1011 {
1012 unsigned int seed;
1014 if (read (fd, &seed, sizeof (seed)) != -1)
1015 {
1016 did_seed = 1;
1017 dprintf ("Random seed: %#x\n", seed);
1018 srandom (seed);
1019 }
1021 close (fd);
1022 }
1023 #if WITH_DEBUG
1024 else
1025 {
1026 char errbuf[PING_ERRMSG_LEN];
1027 dprintf ("open (/dev/urandom): %s\n",
1028 sstrerror (errno, errbuf, sizeof (errbuf)));
1029 }
1030 #endif
1031 }
1033 retval = (int) random ();
1035 dprintf ("Random number: %#x\n", retval);
1037 return (retval);
1038 }
1040 static pinghost_t *ping_alloc (void)
1041 {
1042 pinghost_t *ph;
1043 size_t ph_size;
1045 ph_size = sizeof (pinghost_t)
1046 + sizeof (struct sockaddr_storage)
1047 + sizeof (struct timeval);
1049 ph = (pinghost_t *) malloc (ph_size);
1050 if (ph == NULL)
1051 return (NULL);
1053 memset (ph, '\0', ph_size);
1055 ph->timer = (struct timeval *) (ph + 1);
1056 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
1058 ph->addrlen = sizeof (struct sockaddr_storage);
1059 ph->fd = -1;
1060 ph->latency = -1.0;
1061 ph->dropped = 0;
1062 ph->ident = ping_get_ident () & 0xFFFF;
1064 return (ph);
1065 }
1067 static void ping_free (pinghost_t *ph)
1068 {
1069 if (ph->fd >= 0)
1070 close (ph->fd);
1072 if (ph->username != NULL)
1073 free (ph->username);
1075 if (ph->hostname != NULL)
1076 free (ph->hostname);
1078 if (ph->data != NULL)
1079 free (ph->data);
1081 free (ph);
1082 }
1084 /*
1085 * public methods
1086 */
1087 const char *ping_get_error (pingobj_t *obj)
1088 {
1089 if (obj == NULL)
1090 return (NULL);
1091 return (obj->errmsg);
1092 }
1094 pingobj_t *ping_construct (void)
1095 {
1096 pingobj_t *obj;
1098 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1099 return (NULL);
1100 memset (obj, 0, sizeof (pingobj_t));
1102 obj->timeout = PING_DEF_TIMEOUT;
1103 obj->ttl = PING_DEF_TTL;
1104 obj->addrfamily = PING_DEF_AF;
1105 obj->data = strdup (PING_DEF_DATA);
1106 obj->qos = 0;
1108 return (obj);
1109 }
1111 void ping_destroy (pingobj_t *obj)
1112 {
1113 pinghost_t *current;
1114 pinghost_t *next;
1116 if (obj == NULL)
1117 return;
1119 current = obj->head;
1120 next = NULL;
1122 while (current != NULL)
1123 {
1124 next = current->next;
1125 ping_free (current);
1126 current = next;
1127 }
1129 if (obj->data != NULL)
1130 free (obj->data);
1132 if (obj->srcaddr != NULL)
1133 free (obj->srcaddr);
1135 if (obj->device != NULL)
1136 free (obj->device);
1138 free (obj);
1140 return;
1141 }
1143 int ping_setopt (pingobj_t *obj, int option, void *value)
1144 {
1145 int ret = 0;
1147 if ((obj == NULL) || (value == NULL))
1148 return (-1);
1150 switch (option)
1151 {
1152 case PING_OPT_QOS:
1153 {
1154 pinghost_t *ph;
1156 obj->qos = *((uint8_t *) value);
1157 for (ph = obj->head; ph != NULL; ph = ph->next)
1158 ping_set_qos (obj, ph, obj->qos);
1159 break;
1160 }
1162 case PING_OPT_TIMEOUT:
1163 obj->timeout = *((double *) value);
1164 if (obj->timeout < 0.0)
1165 {
1166 obj->timeout = PING_DEF_TIMEOUT;
1167 ret = -1;
1168 }
1169 break;
1171 case PING_OPT_TTL:
1172 obj->ttl = *((int *) value);
1173 if ((obj->ttl < 1) || (obj->ttl > 255))
1174 {
1175 obj->ttl = PING_DEF_TTL;
1176 ret = -1;
1177 }
1178 else
1179 {
1180 pinghost_t *ph;
1182 for (ph = obj->head; ph != NULL; ph = ph->next)
1183 ping_set_ttl (ph, obj->ttl);
1184 }
1185 break;
1187 case PING_OPT_AF:
1188 obj->addrfamily = *((int *) value);
1189 if ((obj->addrfamily != AF_UNSPEC)
1190 && (obj->addrfamily != AF_INET)
1191 && (obj->addrfamily != AF_INET6))
1192 {
1193 obj->addrfamily = PING_DEF_AF;
1194 ret = -1;
1195 }
1196 if (obj->srcaddr != NULL)
1197 {
1198 free (obj->srcaddr);
1199 obj->srcaddr = NULL;
1200 }
1201 break;
1203 case PING_OPT_DATA:
1204 if (obj->data != NULL)
1205 {
1206 free (obj->data);
1207 obj->data = NULL;
1208 }
1209 obj->data = strdup ((const char *) value);
1210 break;
1212 case PING_OPT_SOURCE:
1213 {
1214 char *hostname = (char *) value;
1215 struct addrinfo ai_hints;
1216 struct addrinfo *ai_list;
1217 int status;
1218 #if WITH_DEBUG
1219 if (obj->addrfamily != AF_UNSPEC)
1220 {
1221 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1222 }
1223 #endif
1224 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1225 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1226 #if defined(AI_ADDRCONFIG)
1227 ai_hints.ai_flags = AI_ADDRCONFIG;
1228 #endif
1229 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1230 if (status != 0)
1231 {
1232 #if defined(EAI_SYSTEM)
1233 char errbuf[PING_ERRMSG_LEN];
1234 #endif
1235 ping_set_error (obj, "getaddrinfo",
1236 #if defined(EAI_SYSTEM)
1237 (status == EAI_SYSTEM)
1238 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1239 #endif
1240 gai_strerror (status));
1241 ret = -1;
1242 break;
1243 }
1244 #if WITH_DEBUG
1245 if (ai_list->ai_next != NULL)
1246 {
1247 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1248 }
1249 #endif
1250 if (obj->srcaddr == NULL)
1251 {
1252 obj->srcaddrlen = 0;
1253 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1254 if (obj->srcaddr == NULL)
1255 {
1256 ping_set_errno (obj, errno);
1257 ret = -1;
1258 freeaddrinfo (ai_list);
1259 break;
1260 }
1261 }
1262 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1263 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1264 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1265 ai_list->ai_addrlen);
1266 obj->srcaddrlen = ai_list->ai_addrlen;
1267 obj->addrfamily = ai_list->ai_family;
1269 freeaddrinfo (ai_list);
1270 } /* case PING_OPT_SOURCE */
1271 break;
1273 case PING_OPT_DEVICE:
1274 {
1275 #ifdef SO_BINDTODEVICE
1276 char *device = strdup ((char *) value);
1278 if (device == NULL)
1279 {
1280 ping_set_errno (obj, errno);
1281 ret = -1;
1282 break;
1283 }
1285 if (obj->device != NULL)
1286 free (obj->device);
1287 obj->device = device;
1288 #else /* ! SO_BINDTODEVICE */
1289 ping_set_errno (obj, ENOTSUP);
1290 ret = -1;
1291 #endif /* ! SO_BINDTODEVICE */
1292 } /* case PING_OPT_DEVICE */
1293 break;
1295 default:
1296 ret = -2;
1297 } /* switch (option) */
1299 return (ret);
1300 } /* int ping_setopt */
1303 int ping_send (pingobj_t *obj)
1304 {
1305 int ret;
1307 if (obj == NULL)
1308 return (-1);
1310 if (ping_send_all (obj) < 0)
1311 return (-1);
1313 if ((ret = ping_receive_all (obj)) < 0)
1314 return (-2);
1316 return (ret);
1317 }
1319 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1320 {
1321 while (ph != NULL)
1322 {
1323 if (strcasecmp (ph->username, host) == 0)
1324 break;
1326 ph = ph->next;
1327 }
1329 return (ph);
1330 }
1332 int ping_host_add (pingobj_t *obj, const char *host)
1333 {
1334 pinghost_t *ph;
1336 struct addrinfo ai_hints;
1337 struct addrinfo *ai_list, *ai_ptr;
1338 int ai_return;
1340 if ((obj == NULL) || (host == NULL))
1341 return (-1);
1343 dprintf ("host = %s\n", host);
1345 if (ping_host_search (obj->head, host) != NULL)
1346 return (0);
1348 memset (&ai_hints, '\0', sizeof (ai_hints));
1349 ai_hints.ai_flags = 0;
1350 #ifdef AI_ADDRCONFIG
1351 ai_hints.ai_flags |= AI_ADDRCONFIG;
1352 #endif
1353 #ifdef AI_CANONNAME
1354 ai_hints.ai_flags |= AI_CANONNAME;
1355 #endif
1356 ai_hints.ai_family = obj->addrfamily;
1357 ai_hints.ai_socktype = SOCK_RAW;
1359 if ((ph = ping_alloc ()) == NULL)
1360 {
1361 dprintf ("Out of memory!\n");
1362 return (-1);
1363 }
1365 if ((ph->username = strdup (host)) == NULL)
1366 {
1367 dprintf ("Out of memory!\n");
1368 ping_set_errno (obj, errno);
1369 ping_free (ph);
1370 return (-1);
1371 }
1373 if ((ph->hostname = strdup (host)) == NULL)
1374 {
1375 dprintf ("Out of memory!\n");
1376 ping_set_errno (obj, errno);
1377 ping_free (ph);
1378 return (-1);
1379 }
1381 /* obj->data is not garuanteed to be != NULL */
1382 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1383 {
1384 dprintf ("Out of memory!\n");
1385 ping_set_errno (obj, errno);
1386 ping_free (ph);
1387 return (-1);
1388 }
1390 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1391 {
1392 #if defined(EAI_SYSTEM)
1393 char errbuf[PING_ERRMSG_LEN];
1394 #endif
1395 dprintf ("getaddrinfo failed\n");
1396 ping_set_error (obj, "getaddrinfo",
1397 #if defined(EAI_SYSTEM)
1398 (ai_return == EAI_SYSTEM)
1399 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1400 #endif
1401 gai_strerror (ai_return));
1402 ping_free (ph);
1403 return (-1);
1404 }
1406 if (ai_list == NULL)
1407 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1409 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1410 {
1411 ph->fd = -1;
1413 if (ai_ptr->ai_family == AF_INET)
1414 {
1415 ai_ptr->ai_socktype = SOCK_RAW;
1416 ai_ptr->ai_protocol = IPPROTO_ICMP;
1417 }
1418 else if (ai_ptr->ai_family == AF_INET6)
1419 {
1420 ai_ptr->ai_socktype = SOCK_RAW;
1421 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1422 }
1423 else
1424 {
1425 char errmsg[PING_ERRMSG_LEN];
1427 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1428 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1430 dprintf (errmsg);
1431 ping_set_error (obj, "getaddrinfo", errmsg);
1432 continue;
1433 }
1435 /* TODO: Move this to a static function `ping_open_socket' and
1436 * call it whenever the socket dies. */
1437 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1438 if (ph->fd == -1)
1439 {
1440 #if WITH_DEBUG
1441 char errbuf[PING_ERRMSG_LEN];
1442 dprintf ("socket: %s\n",
1443 sstrerror (errno, errbuf, sizeof (errbuf)));
1444 #endif
1445 ping_set_errno (obj, errno);
1446 continue;
1447 }
1449 if (obj->srcaddr != NULL)
1450 {
1451 assert (obj->srcaddrlen > 0);
1452 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1454 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1455 {
1456 #if WITH_DEBUG
1457 char errbuf[PING_ERRMSG_LEN];
1458 dprintf ("bind: %s\n",
1459 sstrerror (errno, errbuf, sizeof (errbuf)));
1460 #endif
1461 ping_set_errno (obj, errno);
1462 close (ph->fd);
1463 ph->fd = -1;
1464 continue;
1465 }
1466 }
1468 #ifdef SO_BINDTODEVICE
1469 if (obj->device != NULL)
1470 {
1471 if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE,
1472 obj->device, strlen (obj->device) + 1) != 0)
1473 {
1474 #if WITH_DEBUG
1475 char errbuf[PING_ERRMSG_LEN];
1476 dprintf ("setsockopt: %s\n",
1477 sstrerror (errno, errbuf, sizeof (errbuf)));
1478 #endif
1479 ping_set_errno (obj, errno);
1480 close (ph->fd);
1481 ph->fd = -1;
1482 continue;
1483 }
1484 }
1485 #endif /* SO_BINDTODEVICE */
1486 #ifdef SO_TIMESTAMP
1487 if (1)
1488 {
1489 int c = 1;
1490 setsockopt(ph->fd, SOL_SOCKET, SO_TIMESTAMP, &c, sizeof(c));
1491 }
1492 #endif /* SO_TIMESTAMP */
1493 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1494 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1495 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1496 ph->addrlen = ai_ptr->ai_addrlen;
1497 ph->addrfamily = ai_ptr->ai_family;
1499 #ifdef AI_CANONNAME
1500 if ((ai_ptr->ai_canonname != NULL)
1501 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1502 {
1503 char *old_hostname;
1505 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1506 ph->hostname, ai_ptr->ai_canonname);
1508 old_hostname = ph->hostname;
1509 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1510 {
1511 /* strdup failed, falling back to old hostname */
1512 ph->hostname = old_hostname;
1513 }
1514 else if (old_hostname != NULL)
1515 {
1516 free (old_hostname);
1517 }
1518 }
1519 #endif /* AI_CANONNAME */
1521 if (ph->addrfamily == AF_INET)
1522 {
1523 int opt;
1525 /* Enable receiving the TOS field */
1526 opt = 1;
1527 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
1528 &opt, sizeof (opt));
1530 /* Enable receiving the TTL field */
1531 opt = 1;
1532 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1533 &opt, sizeof (opt));
1534 }
1535 #if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
1536 else if (ph->addrfamily == AF_INET6)
1537 {
1538 int opt;
1540 # if defined(IPV6_RECVHOPLIMIT)
1541 /* For details see RFC 3542, section 6.3. */
1542 opt = 1;
1543 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1544 &opt, sizeof (opt));
1545 # endif /* IPV6_RECVHOPLIMIT */
1547 # if defined(IPV6_RECVTCLASS)
1548 /* For details see RFC 3542, section 6.5. */
1549 opt = 1;
1550 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
1551 &opt, sizeof (opt));
1552 # endif /* IPV6_RECVTCLASS */
1553 }
1554 #endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
1556 break;
1557 } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
1559 freeaddrinfo (ai_list);
1561 if (ph->fd < 0)
1562 {
1563 ping_free (ph);
1564 return (-1);
1565 }
1567 /*
1568 * Adding in the front is much easier, but then the iterator will
1569 * return the host that was added last as first host. That's just not
1570 * nice. -octo
1571 */
1572 if (obj->head == NULL)
1573 {
1574 obj->head = ph;
1575 }
1576 else
1577 {
1578 pinghost_t *hptr;
1580 hptr = obj->head;
1581 while (hptr->next != NULL)
1582 hptr = hptr->next;
1584 assert ((hptr != NULL) && (hptr->next == NULL));
1585 hptr->next = ph;
1586 }
1588 ping_set_ttl (ph, obj->ttl);
1589 ping_set_qos (obj, ph, obj->qos);
1591 return (0);
1592 } /* int ping_host_add */
1594 int ping_host_remove (pingobj_t *obj, const char *host)
1595 {
1596 pinghost_t *pre, *cur;
1598 if ((obj == NULL) || (host == NULL))
1599 return (-1);
1601 pre = NULL;
1602 cur = obj->head;
1604 while (cur != NULL)
1605 {
1606 if (strcasecmp (host, cur->username) == 0)
1607 break;
1609 pre = cur;
1610 cur = cur->next;
1611 }
1613 if (cur == NULL)
1614 {
1615 ping_set_error (obj, "ping_host_remove", "Host not found");
1616 return (-1);
1617 }
1619 if (pre == NULL)
1620 obj->head = cur->next;
1621 else
1622 pre->next = cur->next;
1624 ping_free (cur);
1626 return (0);
1627 }
1629 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1630 {
1631 if (obj == NULL)
1632 return (NULL);
1633 return ((pingobj_iter_t *) obj->head);
1634 }
1636 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1637 {
1638 if (iter == NULL)
1639 return (NULL);
1640 return ((pingobj_iter_t *) iter->next);
1641 }
1643 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1644 void *buffer, size_t *buffer_len)
1645 {
1646 int ret = EINVAL;
1648 size_t orig_buffer_len = *buffer_len;
1650 if ((iter == NULL) || (buffer_len == NULL))
1651 return (-1);
1653 if ((buffer == NULL) && (*buffer_len != 0 ))
1654 return (-1);
1656 switch (info)
1657 {
1658 case PING_INFO_USERNAME:
1659 ret = ENOMEM;
1660 *buffer_len = strlen (iter->username) + 1;
1661 if (orig_buffer_len <= *buffer_len)
1662 break;
1663 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1664 * will copy `*buffer_len' and pad the rest of
1665 * `buffer' with null-bytes */
1666 strncpy (buffer, iter->username, orig_buffer_len);
1667 ret = 0;
1668 break;
1670 case PING_INFO_HOSTNAME:
1671 ret = ENOMEM;
1672 *buffer_len = strlen (iter->hostname) + 1;
1673 if (orig_buffer_len < *buffer_len)
1674 break;
1675 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1676 * will copy `*buffer_len' and pad the rest of
1677 * `buffer' with null-bytes */
1678 strncpy (buffer, iter->hostname, orig_buffer_len);
1679 ret = 0;
1680 break;
1682 case PING_INFO_ADDRESS:
1683 ret = getnameinfo ((struct sockaddr *) iter->addr,
1684 iter->addrlen,
1685 (char *) buffer,
1686 *buffer_len,
1687 NULL, 0,
1688 NI_NUMERICHOST);
1689 if (ret != 0)
1690 {
1691 if ((ret == EAI_MEMORY)
1692 #ifdef EAI_OVERFLOW
1693 || (ret == EAI_OVERFLOW)
1694 #endif
1695 )
1696 ret = ENOMEM;
1697 #if defined(EAI_SYSTEM)
1698 else if (ret == EAI_SYSTEM)
1699 ret = errno;
1700 #endif
1701 else
1702 ret = EINVAL;
1703 }
1704 break;
1706 case PING_INFO_FAMILY:
1707 ret = ENOMEM;
1708 *buffer_len = sizeof (int);
1709 if (orig_buffer_len < sizeof (int))
1710 break;
1711 *((int *) buffer) = iter->addrfamily;
1712 ret = 0;
1713 break;
1715 case PING_INFO_LATENCY:
1716 ret = ENOMEM;
1717 *buffer_len = sizeof (double);
1718 if (orig_buffer_len < sizeof (double))
1719 break;
1720 *((double *) buffer) = iter->latency;
1721 ret = 0;
1722 break;
1724 case PING_INFO_DROPPED:
1725 ret = ENOMEM;
1726 *buffer_len = sizeof (uint32_t);
1727 if (orig_buffer_len < sizeof (uint32_t))
1728 break;
1729 *((uint32_t *) buffer) = iter->dropped;
1730 ret = 0;
1731 break;
1733 case PING_INFO_SEQUENCE:
1734 ret = ENOMEM;
1735 *buffer_len = sizeof (unsigned int);
1736 if (orig_buffer_len < sizeof (unsigned int))
1737 break;
1738 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1739 ret = 0;
1740 break;
1742 case PING_INFO_IDENT:
1743 ret = ENOMEM;
1744 *buffer_len = sizeof (uint16_t);
1745 if (orig_buffer_len < sizeof (uint16_t))
1746 break;
1747 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1748 ret = 0;
1749 break;
1751 case PING_INFO_DATA:
1752 ret = ENOMEM;
1753 *buffer_len = strlen (iter->data);
1754 if (orig_buffer_len < *buffer_len)
1755 break;
1756 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1757 ret = 0;
1758 break;
1760 case PING_INFO_RECV_TTL:
1761 ret = ENOMEM;
1762 *buffer_len = sizeof (int);
1763 if (orig_buffer_len < sizeof (int))
1764 break;
1765 *((int *) buffer) = iter->recv_ttl;
1766 ret = 0;
1767 break;
1769 case PING_INFO_RECV_QOS:
1770 ret = ENOMEM;
1771 if (*buffer_len>sizeof(unsigned)) *buffer_len=sizeof(unsigned);
1772 if (!*buffer_len) *buffer_len=1;
1773 if (orig_buffer_len < *buffer_len)
1774 break;
1775 memcpy(buffer,&iter->recv_qos,*buffer_len);
1776 ret = 0;
1777 break;
1778 }
1780 return (ret);
1781 } /* ping_iterator_get_info */
1783 void *ping_iterator_get_context (pingobj_iter_t *iter)
1784 {
1785 if (iter == NULL)
1786 return (NULL);
1787 return (iter->context);
1788 }
1790 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1791 {
1792 if (iter == NULL)
1793 return;
1794 iter->context = context;
1795 }