40a0ba21b570bab19fea04fc71d8c69231125173
1 /**
2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2011 Florian octo Forster <ff at octo.it>
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 #ifdef __APPLE__
21 #define __APPLE_USE_RFC_3542
22 #endif
24 #if HAVE_CONFIG_H
25 # include <config.h>
26 #endif
28 #if STDC_HEADERS
29 # include <stdlib.h>
30 # include <stdio.h>
31 # include <string.h>
32 # include <inttypes.h>
33 # include <errno.h>
34 # include <assert.h>
35 #else
36 # error "You don't have the standard C99 header files installed"
37 #endif /* STDC_HEADERS */
39 #ifdef HAVE_STDINT_H
40 # include <stdint.h>
41 #endif
43 #if HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
47 #if HAVE_FCNTL_H
48 # include <fcntl.h>
49 #endif
50 #if HAVE_SYS_TYPES_H
51 # include <sys/types.h>
52 #endif
53 #if HAVE_SYS_STAT_H
54 # include <sys/stat.h>
55 #endif
57 #if TIME_WITH_SYS_TIME
58 # include <sys/time.h>
59 # include <time.h>
60 #else
61 # if HAVE_SYS_TIME_H
62 # include <sys/time.h>
63 # else
64 # include <time.h>
65 # endif
66 #endif
68 #if HAVE_SYS_SOCKET_H
69 # include <sys/socket.h>
70 #endif
72 #if HAVE_NETDB_H
73 # include <netdb.h>
74 #endif
76 #if HAVE_NETINET_IN_SYSTM_H
77 # include <netinet/in_systm.h>
78 #endif
79 #if HAVE_NETINET_IN_H
80 # include <netinet/in.h>
81 #endif
82 #if HAVE_NETINET_IP_H
83 # include <netinet/ip.h>
84 #endif
85 #if HAVE_NETINET_IP_ICMP_H
86 # include <netinet/ip_icmp.h>
87 #endif
88 #ifdef HAVE_NETINET_IP_VAR_H
89 # include <netinet/ip_var.h>
90 #endif
91 #if HAVE_NETINET_IP6_H
92 # include <netinet/ip6.h>
93 #endif
94 #if HAVE_NETINET_ICMP6_H
95 # include <netinet/icmp6.h>
96 #endif
98 #include "oping.h"
100 #if WITH_DEBUG
101 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
102 #else
103 # define dprintf(...) /**/
104 #endif
106 #define PING_ERRMSG_LEN 256
108 struct pinghost
109 {
110 /* username: name passed in by the user */
111 char *username;
112 /* hostname: name returned by the reverse lookup */
113 char *hostname;
114 struct sockaddr_storage *addr;
115 socklen_t addrlen;
116 int addrfamily;
117 int fd;
118 int ident;
119 int sequence;
120 struct timeval *timer;
121 double latency;
122 uint32_t dropped;
123 int recv_ttl;
124 uint8_t recv_qos;
125 char *data;
127 void *context;
129 struct pinghost *next;
130 };
132 struct pingobj
133 {
134 double timeout;
135 int ttl;
136 int addrfamily;
137 uint8_t qos;
138 char *data;
140 struct sockaddr *srcaddr;
141 socklen_t srcaddrlen;
143 char *device;
145 char errmsg[PING_ERRMSG_LEN];
147 pinghost_t *head;
148 };
150 /*
151 * private (static) functions
152 */
153 /* Even though Posix requires "strerror_r" to return an "int",
154 * some systems (e.g. the GNU libc) return a "char *" _and_
155 * ignore the second argument ... -tokkee */
156 static char *sstrerror (int errnum, char *buf, size_t buflen)
157 {
158 buf[0] = 0;
160 #if !HAVE_STRERROR_R
161 {
162 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
163 }
164 /* #endif !HAVE_STRERROR_R */
166 #elif STRERROR_R_CHAR_P
167 {
168 char *temp;
169 temp = strerror_r (errnum, buf, buflen);
170 if (buf[0] == 0)
171 {
172 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
173 strncpy (buf, temp, buflen);
174 else
175 strncpy (buf, "strerror_r did not return "
176 "an error message", buflen);
177 }
178 }
179 /* #endif STRERROR_R_CHAR_P */
181 #else
182 if (strerror_r (errnum, buf, buflen) != 0)
183 {
184 snprintf (buf, buflen, "Error %i (%#x); "
185 "Additionally, strerror_r failed.",
186 errnum, errnum);
187 }
188 #endif /* STRERROR_R_CHAR_P */
190 buf[buflen - 1] = 0;
192 return (buf);
193 } /* char *sstrerror */
195 static void ping_set_error (pingobj_t *obj, const char *function,
196 const char *message)
197 {
198 snprintf (obj->errmsg, sizeof (obj->errmsg),
199 "%s: %s", function, message);
200 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
201 }
203 static void ping_set_errno (pingobj_t *obj, int error_number)
204 {
205 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
206 }
208 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
209 struct timeval *res)
210 {
211 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
212 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
214 while (res->tv_usec > 1000000)
215 {
216 res->tv_usec -= 1000000;
217 res->tv_sec++;
218 }
220 return (0);
221 }
223 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
224 struct timeval *res)
225 {
226 if ((tv1->tv_sec < tv2->tv_sec)
227 || ((tv1->tv_sec == tv2->tv_sec)
228 && (tv1->tv_usec < tv2->tv_usec)))
229 return (-1);
231 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
232 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
234 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
236 while (res->tv_usec < 0)
237 {
238 res->tv_usec += 1000000;
239 res->tv_sec--;
240 }
242 return (0);
243 }
245 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
246 {
247 uint32_t sum = 0;
248 uint16_t ret = 0;
250 uint16_t *ptr;
252 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
253 sum += *ptr;
255 if (len == 1)
256 {
257 *(char *) &ret = *(char *) ptr;
258 sum += ret;
259 }
261 /* Do this twice to get all possible carries.. */
262 sum = (sum >> 16) + (sum & 0xFFFF);
263 sum = (sum >> 16) + (sum & 0xFFFF);
265 ret = ~sum;
267 return (ret);
268 }
270 static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
271 size_t buffer_len)
272 {
273 struct ip *ip_hdr;
274 struct icmp *icmp_hdr;
276 size_t ip_hdr_len;
278 uint16_t recv_checksum;
279 uint16_t calc_checksum;
281 uint16_t ident;
282 uint16_t seq;
284 pinghost_t *ptr;
286 if (buffer_len < sizeof (struct ip))
287 return (NULL);
289 ip_hdr = (struct ip *) buffer;
290 ip_hdr_len = ip_hdr->ip_hl << 2;
292 if (buffer_len < ip_hdr_len)
293 return (NULL);
295 buffer += ip_hdr_len;
296 buffer_len -= ip_hdr_len;
298 if (buffer_len < sizeof (struct icmp))
299 return (NULL);
301 icmp_hdr = (struct icmp *) buffer;
302 buffer += sizeof (struct icmp);
303 buffer_len -= sizeof (struct icmp);
305 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
306 {
307 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
308 return (NULL);
309 }
311 recv_checksum = icmp_hdr->icmp_cksum;
312 icmp_hdr->icmp_cksum = 0;
313 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
314 sizeof (struct icmp) + buffer_len);
316 if (recv_checksum != calc_checksum)
317 {
318 dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
319 "calculated 0x%04"PRIx16"\n",
320 recv_checksum, calc_checksum);
321 return (NULL);
322 }
324 ident = ntohs (icmp_hdr->icmp_id);
325 seq = ntohs (icmp_hdr->icmp_seq);
327 /* We have to iterate over all hosts, since ICMPv4 packets may
328 * be received on any raw v4 socket. */
329 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
330 {
331 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
332 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
334 if (ptr->addrfamily != AF_INET)
335 continue;
337 if (!timerisset (ptr->timer))
338 continue;
340 if (ptr->ident != ident)
341 continue;
343 if (((ptr->sequence - 1) & 0xFFFF) != seq)
344 continue;
346 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
347 "seq = %"PRIu16"\n",
348 ptr->hostname, ident, seq);
350 break;
351 }
353 if (ptr == NULL)
354 {
355 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
356 ident, seq);
357 }
359 if (ptr != NULL){
360 ptr->recv_ttl = (int) ip_hdr->ip_ttl;
361 ptr->recv_qos = (uint8_t) ip_hdr->ip_tos;
362 }
363 return (ptr);
364 }
366 #ifndef ICMP6_ECHO_REQUEST
367 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
368 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
369 # else
370 # define ICMP6_ECHO_REQUEST 128
371 # endif
372 #endif
374 #ifndef ICMP6_ECHO_REPLY
375 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
376 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
377 # else
378 # define ICMP6_ECHO_REPLY 129
379 # endif
380 #endif
382 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
383 size_t buffer_len)
384 {
385 struct icmp6_hdr *icmp_hdr;
387 uint16_t ident;
388 uint16_t seq;
390 pinghost_t *ptr;
392 if (buffer_len < sizeof (struct icmp6_hdr))
393 return (NULL);
395 icmp_hdr = (struct icmp6_hdr *) buffer;
396 buffer += sizeof (struct icmp);
397 buffer_len -= sizeof (struct icmp);
399 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
400 {
401 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
402 return (NULL);
403 }
405 if (icmp_hdr->icmp6_code != 0)
406 {
407 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
408 return (NULL);
409 }
411 ident = ntohs (icmp_hdr->icmp6_id);
412 seq = ntohs (icmp_hdr->icmp6_seq);
414 /* We have to iterate over all hosts, since ICMPv6 packets may
415 * be received on any raw v6 socket. */
416 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
417 {
418 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
419 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
421 if (ptr->addrfamily != AF_INET6)
422 continue;
424 if (!timerisset (ptr->timer))
425 continue;
427 if (ptr->ident != ident)
428 continue;
430 if (((ptr->sequence - 1) & 0xFFFF) != seq)
431 continue;
433 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
434 "seq = %"PRIu16"\n",
435 ptr->hostname, ident, seq);
437 break;
438 }
440 if (ptr == NULL)
441 {
442 dprintf ("No match found for ident = 0x%04"PRIx16", "
443 "seq = %"PRIu16"\n",
444 ident, seq);
445 }
447 return (ptr);
448 }
450 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
451 struct timeval *now)
452 {
453 /* Note: 'ph' is not necessarily the host object for which we receive a
454 * reply. The right object will be returned by ping_receive_ipv*(). For
455 * now, we can only rely on ph->fd and ph->addrfamily. */
457 struct timeval diff, pkt_now = *now;
458 pinghost_t *host = NULL;
459 int recv_ttl;
460 uint8_t recv_qos;
462 /*
463 * Set up the receive buffer..
464 */
465 struct msghdr msghdr;
466 struct cmsghdr *cmsg;
467 char payload_buffer[4096];
468 ssize_t payload_buffer_len;
469 char control_buffer[4096];
470 struct iovec payload_iovec;
472 memset (&payload_iovec, 0, sizeof (payload_iovec));
473 payload_iovec.iov_base = payload_buffer;
474 payload_iovec.iov_len = sizeof (payload_buffer);
476 memset (&msghdr, 0, sizeof (msghdr));
477 /* unspecified source address */
478 msghdr.msg_name = NULL;
479 msghdr.msg_namelen = 0;
480 /* output buffer vector, see readv(2) */
481 msghdr.msg_iov = &payload_iovec;
482 msghdr.msg_iovlen = 1;
483 /* output buffer for control messages */
484 msghdr.msg_control = control_buffer;
485 msghdr.msg_controllen = sizeof (control_buffer);
486 /* flags; this is an output only field.. */
487 msghdr.msg_flags = 0;
488 #ifdef MSG_XPG4_2
489 msghdr.msg_flags |= MSG_XPG4_2;
490 #endif
492 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
493 if (payload_buffer_len < 0)
494 {
495 #if WITH_DEBUG
496 char errbuf[PING_ERRMSG_LEN];
497 dprintf ("recvfrom: %s\n",
498 sstrerror (errno, errbuf, sizeof (errbuf)));
499 #endif
500 return (-1);
501 }
502 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
504 /* Iterate over all auxiliary data in msghdr */
505 recv_ttl = -1;
506 recv_qos = 0;
507 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
508 cmsg != NULL;
509 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
510 {
511 if (cmsg->cmsg_level == SOL_SOCKET)
512 {
513 #ifdef SO_TIMESTAMP
514 if (cmsg->cmsg_type == SO_TIMESTAMP)
515 memcpy (&pkt_now, CMSG_DATA (cmsg), sizeof (pkt_now));
516 #endif /* SO_TIMESTAMP */
517 }
518 else if (ph->addrfamily == AF_INET) /* {{{ */
519 {
520 if (cmsg->cmsg_level != IPPROTO_IP)
521 continue;
523 if (cmsg->cmsg_type == IP_TOS)
524 {
525 memcpy (&recv_qos, CMSG_DATA (cmsg),
526 sizeof (recv_qos));
527 dprintf ("TOSv4 = 0x%02"PRIx8";\n", recv_qos);
528 } else
529 if (cmsg->cmsg_type == IP_TTL)
530 {
531 memcpy (&recv_ttl, CMSG_DATA (cmsg),
532 sizeof (recv_ttl));
533 dprintf ("TTLv4 = %i;\n", recv_ttl);
534 }
535 else
536 {
537 dprintf ("Not handling option %i.\n",
538 cmsg->cmsg_type);
539 }
540 } /* }}} */
541 else if (ph->addrfamily == AF_INET6) /* {{{ */
542 {
543 if (cmsg->cmsg_level != IPPROTO_IPV6)
544 continue;
546 if (cmsg->cmsg_type == IPV6_TCLASS)
547 {
548 memcpy (&recv_qos, CMSG_DATA (cmsg),
549 sizeof (recv_qos));
550 dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
551 } else
552 #ifdef IPV6_HOPLIMIT
553 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
554 {
555 memcpy (&recv_ttl, CMSG_DATA (cmsg),
556 sizeof (recv_ttl));
557 dprintf ("TTLv6 = %i;\n", recv_ttl);
558 }
559 else
560 #endif
561 #ifdef IPV6_UNICAST_HOPS
562 if (cmsg->cmsg_type == IPV6_UNICAST_HOPS)
563 {
564 memcpy (&recv_ttl, CMSG_DATA (cmsg),
565 sizeof (recv_ttl));
566 dprintf ("TTLv6 = %i;\n", recv_ttl);
567 }
568 else
569 #endif
570 #ifdef IPV6_MULTICAST_HOPS
571 if (cmsg->cmsg_type == IPV6_MULTICAST_HOPS)
572 {
573 memcpy (&recv_ttl, CMSG_DATA (cmsg),
574 sizeof (recv_ttl));
575 dprintf ("TTLv6 = %i;\n", recv_ttl);
576 }
577 else
578 #endif
579 {
580 dprintf ("Not handling option %i.\n",
581 cmsg->cmsg_type);
582 }
583 } /* }}} */
584 else
585 {
586 dprintf ("Don't know how to handle "
587 "unknown protocol %i.\n",
588 cmsg->cmsg_level);
589 }
590 } /* }}} for (cmsg) */
592 if (ph->addrfamily == AF_INET)
593 {
594 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
595 if (host == NULL)
596 return (-1);
597 }
598 else if (ph->addrfamily == AF_INET6)
599 {
600 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
601 if (host == NULL)
602 return (-1);
603 }
604 else
605 {
606 dprintf ("ping_receive_one: Unknown address family %i.\n",
607 ph->addrfamily);
608 return (-1);
609 }
611 dprintf ("rcvd: %12i.%06i\n",
612 (int) pkt_now.tv_sec,
613 (int) pkt_now.tv_usec);
614 dprintf ("sent: %12i.%06i\n",
615 (int) host->timer->tv_sec,
616 (int) host->timer->tv_usec);
618 if (ping_timeval_sub (&pkt_now, host->timer, &diff) < 0)
619 {
620 timerclear (host->timer);
621 return (-1);
622 }
624 dprintf ("diff: %12i.%06i\n",
625 (int) diff.tv_sec,
626 (int) diff.tv_usec);
628 if (recv_ttl >= 0)
629 host->recv_ttl = recv_ttl;
630 host->recv_qos = recv_qos;
632 host->latency = ((double) diff.tv_usec) / 1000.0;
633 host->latency += ((double) diff.tv_sec) * 1000.0;
635 timerclear (host->timer);
637 return (0);
638 }
640 /* Blocks until a packet was received from all hosts or the timeout is reached.
641 * When interrupted, (-EINTR) is returned. On error, -1 is returned. On
642 * success, returns zero. */
643 static int ping_receive_all (pingobj_t *obj)
644 {
645 fd_set read_fds;
646 fd_set err_fds;
647 int num_fds;
648 int max_fd;
650 pinghost_t *ph;
651 pinghost_t *ptr;
653 struct timeval endtime;
654 struct timeval nowtime;
655 struct timeval timeout;
656 int status;
658 int ret;
660 ph = obj->head;
661 ret = 0;
663 for (ptr = ph; ptr != NULL; ptr = ptr->next)
664 {
665 ptr->latency = -1.0;
666 ptr->recv_ttl = -1;
667 }
669 if (gettimeofday (&nowtime, NULL) == -1)
670 {
671 ping_set_errno (obj, errno);
672 return (-1);
673 }
675 /* Set up timeout */
676 timeout.tv_sec = (time_t) obj->timeout;
677 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
679 dprintf ("Set timeout to %i.%06i seconds\n",
680 (int) timeout.tv_sec,
681 (int) timeout.tv_usec);
683 ping_timeval_add (&nowtime, &timeout, &endtime);
685 while (1)
686 {
687 FD_ZERO (&read_fds);
688 FD_ZERO (&err_fds);
689 num_fds = 0;
690 max_fd = -1;
692 for (ptr = ph; ptr != NULL; ptr = ptr->next)
693 {
694 if (!timerisset (ptr->timer))
695 continue;
697 FD_SET (ptr->fd, &read_fds);
698 FD_SET (ptr->fd, &err_fds);
699 num_fds++;
701 if (max_fd < ptr->fd)
702 max_fd = ptr->fd;
703 }
705 if (num_fds == 0)
706 break;
708 if (gettimeofday (&nowtime, NULL) == -1)
709 {
710 ping_set_errno (obj, errno);
711 return (-1);
712 }
714 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
715 break;
717 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
718 (int) timeout.tv_sec,
719 (int) timeout.tv_usec);
721 status = select (max_fd + 1, &read_fds, NULL, &err_fds, &timeout);
723 if (gettimeofday (&nowtime, NULL) == -1)
724 {
725 ping_set_errno (obj, errno);
726 return (-1);
727 }
729 if ((status == -1) && (errno == EINTR))
730 {
731 dprintf ("select was interrupted by signal..\n");
732 ping_set_errno (obj, EINTR);
733 return (-EINTR);
734 }
735 else if (status < 0)
736 {
737 #if WITH_DEBUG
738 char errbuf[PING_ERRMSG_LEN];
739 dprintf ("select: %s\n",
740 sstrerror (errno, errbuf, sizeof (errbuf)));
741 #endif
742 break;
743 }
744 else if (status == 0)
745 {
746 dprintf ("select timed out\n");
747 for (ptr = ph; ptr != NULL; ptr = ptr->next)
748 if (ptr->latency < 0.0)
749 ptr->dropped++;
750 break;
751 }
753 for (ptr = ph; ptr != NULL; ptr = ptr->next)
754 {
755 if (FD_ISSET (ptr->fd, &read_fds))
756 {
757 if (ping_receive_one (obj, ptr, &nowtime) == 0)
758 ret++;
759 }
760 else if (FD_ISSET (ptr->fd, &err_fds))
761 {
762 /* clear the timer in this case so that we
763 * don't run into an endless loop. */
764 /* TODO: Set an error flag in this case. */
765 timerclear (ptr->timer);
766 }
767 }
768 } /* while (1) */
770 return (ret);
771 } /* int ping_receive_all */
773 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
774 * Sending functions: *
775 * *
776 * ping_send_all *
777 * +-> ping_send_one_ipv4 *
778 * `-> ping_send_one_ipv6 *
779 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
780 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
781 const void *buf, size_t buflen)
782 {
783 ssize_t ret;
785 if (gettimeofday (ph->timer, NULL) == -1)
786 {
787 timerclear (ph->timer);
788 return (-1);
789 }
791 ret = sendto (ph->fd, buf, buflen, 0,
792 (struct sockaddr *) ph->addr, ph->addrlen);
794 if (ret < 0)
795 {
796 #if defined(EHOSTUNREACH)
797 if (errno == EHOSTUNREACH)
798 return (0);
799 #endif
800 #if defined(ENETUNREACH)
801 if (errno == ENETUNREACH)
802 return (0);
803 #endif
804 ping_set_errno (obj, errno);
805 }
807 return (ret);
808 }
810 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
811 {
812 struct icmp *icmp4;
813 int status;
815 char buf[4096];
816 int buflen;
818 char *data;
819 int datalen;
821 dprintf ("ph->hostname = %s\n", ph->hostname);
823 memset (buf, '\0', sizeof (buf));
824 icmp4 = (struct icmp *) buf;
825 data = (char *) (icmp4 + 1);
827 icmp4->icmp_type = ICMP_ECHO;
828 icmp4->icmp_code = 0;
829 icmp4->icmp_cksum = 0;
830 icmp4->icmp_id = htons (ph->ident);
831 icmp4->icmp_seq = htons (ph->sequence);
833 buflen = 4096 - sizeof (struct icmp);
834 strncpy (data, ph->data, buflen);
835 datalen = strlen (data);
837 buflen = datalen + sizeof (struct icmp);
839 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
841 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
843 status = ping_sendto (obj, ph, buf, buflen);
844 if (status < 0)
845 {
846 perror ("ping_sendto");
847 return (-1);
848 }
850 dprintf ("sendto: status = %i\n", status);
852 return (0);
853 }
855 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
856 {
857 struct icmp6_hdr *icmp6;
858 int status;
860 char buf[4096];
861 int buflen;
863 char *data;
864 int datalen;
866 dprintf ("ph->hostname = %s\n", ph->hostname);
868 memset (buf, '\0', sizeof (buf));
869 icmp6 = (struct icmp6_hdr *) buf;
870 data = (char *) (icmp6 + 1);
872 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
873 icmp6->icmp6_code = 0;
874 /* The checksum will be calculated by the TCP/IP stack. */
875 /* FIXME */
876 icmp6->icmp6_cksum = 0;
877 icmp6->icmp6_id = htons (ph->ident);
878 icmp6->icmp6_seq = htons (ph->sequence);
880 buflen = 4096 - sizeof (struct icmp6_hdr);
881 strncpy (data, ph->data, buflen);
882 datalen = strlen (data);
884 buflen = datalen + sizeof (struct icmp6_hdr);
886 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
888 status = ping_sendto (obj, ph, buf, buflen);
889 if (status < 0)
890 {
891 perror ("ping_sendto");
892 return (-1);
893 }
895 dprintf ("sendto: status = %i\n", status);
897 return (0);
898 }
900 static int ping_send_all (pingobj_t *obj)
901 {
902 pinghost_t *ph;
903 pinghost_t *ptr;
905 int ret;
907 ret = 0;
908 ph = obj->head;
910 for (ptr = ph; ptr != NULL; ptr = ptr->next)
911 {
912 /* start timer.. The GNU `ping6' starts the timer before
913 * sending the packet, so I will do that too */
914 if (gettimeofday (ptr->timer, NULL) == -1)
915 {
916 #if WITH_DEBUG
917 char errbuf[PING_ERRMSG_LEN];
918 dprintf ("gettimeofday: %s\n",
919 sstrerror (errno, errbuf, sizeof (errbuf)));
920 #endif
921 timerclear (ptr->timer);
922 ret--;
923 continue;
924 }
925 else
926 {
927 dprintf ("timer set for hostname = %s\n", ptr->hostname);
928 }
930 if (ptr->addrfamily == AF_INET6)
931 {
932 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
933 if (ping_send_one_ipv6 (obj, ptr) != 0)
934 {
935 timerclear (ptr->timer);
936 ret--;
937 continue;
938 }
939 }
940 else if (ptr->addrfamily == AF_INET)
941 {
942 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
943 if (ping_send_one_ipv4 (obj, ptr) != 0)
944 {
945 timerclear (ptr->timer);
946 ret--;
947 continue;
948 }
949 }
950 else /* this should not happen */
951 {
952 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
953 timerclear (ptr->timer);
954 ret--;
955 continue;
956 }
958 ptr->sequence++;
959 }
961 return (ret);
962 }
964 /*
965 * Set the TTL of a socket protocol independently.
966 */
967 static int ping_set_ttl (pinghost_t *ph, int ttl)
968 {
969 int ret = -2;
971 if (ph->addrfamily == AF_INET)
972 {
973 dprintf ("Setting TTLv4 to %i\n", ttl);
974 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
975 &ttl, sizeof (ttl));
976 }
977 else if (ph->addrfamily == AF_INET6)
978 {
979 dprintf ("Setting TTLv6 to %i\n", ttl);
980 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
981 &ttl, sizeof (ttl));
982 }
984 return (ret);
985 }
987 /*
988 * Set the TOS of a socket protocol independently.
989 *
990 * Using SOL_SOCKET / SO_PRIORITY might be a protocol independent way to
991 * set this. See socket(7) for details.
992 */
993 static int ping_set_qos (pingobj_t *obj, pinghost_t *ph, uint8_t qos)
994 {
995 int ret = EINVAL;
996 char errbuf[PING_ERRMSG_LEN];
998 if (ph->addrfamily == AF_INET)
999 {
1000 dprintf ("Setting TP_TOS to %#04"PRIx8"\n", qos);
1001 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
1002 &qos, sizeof (qos));
1003 if (ret != 0)
1004 {
1005 ret = errno;
1006 ping_set_error (obj, "ping_set_qos",
1007 sstrerror (ret, errbuf, sizeof (errbuf)));
1008 dprintf ("Setting TP_TOS failed: %s\n", errbuf);
1009 }
1010 }
1011 else if (ph->addrfamily == AF_INET6)
1012 {
1013 /* IPV6_TCLASS requires an "int". */
1014 int tmp = (int) qos;
1016 dprintf ("Setting IPV6_TCLASS to %#04"PRIx8" (%i)\n", qos, tmp);
1017 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_TCLASS,
1018 &tmp, sizeof (tmp));
1019 if (ret != 0)
1020 {
1021 ret = errno;
1022 ping_set_error (obj, "ping_set_qos",
1023 sstrerror (ret, errbuf, sizeof (errbuf)));
1024 dprintf ("Setting IPV6_TCLASS failed: %s\n", errbuf);
1025 }
1026 }
1028 return (ret);
1029 }
1031 static int ping_get_ident (void)
1032 {
1033 int fd;
1034 static int did_seed = 0;
1036 int retval;
1038 if (did_seed == 0)
1039 {
1040 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
1041 {
1042 unsigned int seed;
1044 if (read (fd, &seed, sizeof (seed)) != -1)
1045 {
1046 did_seed = 1;
1047 dprintf ("Random seed: %#x\n", seed);
1048 srandom (seed);
1049 }
1051 close (fd);
1052 }
1053 #if WITH_DEBUG
1054 else
1055 {
1056 char errbuf[PING_ERRMSG_LEN];
1057 dprintf ("open (/dev/urandom): %s\n",
1058 sstrerror (errno, errbuf, sizeof (errbuf)));
1059 }
1060 #endif
1061 }
1063 retval = (int) random ();
1065 dprintf ("Random number: %#x\n", retval);
1067 return (retval);
1068 }
1070 static pinghost_t *ping_alloc (void)
1071 {
1072 pinghost_t *ph;
1073 size_t ph_size;
1075 ph_size = sizeof (pinghost_t)
1076 + sizeof (struct sockaddr_storage)
1077 + sizeof (struct timeval);
1079 ph = (pinghost_t *) malloc (ph_size);
1080 if (ph == NULL)
1081 return (NULL);
1083 memset (ph, '\0', ph_size);
1085 ph->timer = (struct timeval *) (ph + 1);
1086 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
1088 ph->addrlen = sizeof (struct sockaddr_storage);
1089 ph->fd = -1;
1090 ph->latency = -1.0;
1091 ph->dropped = 0;
1092 ph->ident = ping_get_ident () & 0xFFFF;
1094 return (ph);
1095 }
1097 static void ping_free (pinghost_t *ph)
1098 {
1099 if (ph->fd >= 0)
1100 close (ph->fd);
1102 if (ph->username != NULL)
1103 free (ph->username);
1105 if (ph->hostname != NULL)
1106 free (ph->hostname);
1108 if (ph->data != NULL)
1109 free (ph->data);
1111 free (ph);
1112 }
1114 /*
1115 * public methods
1116 */
1117 const char *ping_get_error (pingobj_t *obj)
1118 {
1119 if (obj == NULL)
1120 return (NULL);
1121 return (obj->errmsg);
1122 }
1124 pingobj_t *ping_construct (void)
1125 {
1126 pingobj_t *obj;
1128 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1129 return (NULL);
1130 memset (obj, 0, sizeof (pingobj_t));
1132 obj->timeout = PING_DEF_TIMEOUT;
1133 obj->ttl = PING_DEF_TTL;
1134 obj->addrfamily = PING_DEF_AF;
1135 obj->data = strdup (PING_DEF_DATA);
1136 obj->qos = 0;
1138 return (obj);
1139 }
1141 void ping_destroy (pingobj_t *obj)
1142 {
1143 pinghost_t *current;
1144 pinghost_t *next;
1146 if (obj == NULL)
1147 return;
1149 current = obj->head;
1150 next = NULL;
1152 while (current != NULL)
1153 {
1154 next = current->next;
1155 ping_free (current);
1156 current = next;
1157 }
1159 if (obj->data != NULL)
1160 free (obj->data);
1162 if (obj->srcaddr != NULL)
1163 free (obj->srcaddr);
1165 if (obj->device != NULL)
1166 free (obj->device);
1168 free (obj);
1170 return;
1171 }
1173 int ping_setopt (pingobj_t *obj, int option, void *value)
1174 {
1175 int ret = 0;
1177 if ((obj == NULL) || (value == NULL))
1178 return (-1);
1180 switch (option)
1181 {
1182 case PING_OPT_QOS:
1183 {
1184 pinghost_t *ph;
1186 obj->qos = *((uint8_t *) value);
1187 for (ph = obj->head; ph != NULL; ph = ph->next)
1188 ping_set_qos (obj, ph, obj->qos);
1189 break;
1190 }
1192 case PING_OPT_TIMEOUT:
1193 obj->timeout = *((double *) value);
1194 if (obj->timeout < 0.0)
1195 {
1196 obj->timeout = PING_DEF_TIMEOUT;
1197 ret = -1;
1198 }
1199 break;
1201 case PING_OPT_TTL:
1202 obj->ttl = *((int *) value);
1203 if ((obj->ttl < 1) || (obj->ttl > 255))
1204 {
1205 obj->ttl = PING_DEF_TTL;
1206 ret = -1;
1207 }
1208 else
1209 {
1210 pinghost_t *ph;
1212 for (ph = obj->head; ph != NULL; ph = ph->next)
1213 ping_set_ttl (ph, obj->ttl);
1214 }
1215 break;
1217 case PING_OPT_AF:
1218 obj->addrfamily = *((int *) value);
1219 if ((obj->addrfamily != AF_UNSPEC)
1220 && (obj->addrfamily != AF_INET)
1221 && (obj->addrfamily != AF_INET6))
1222 {
1223 obj->addrfamily = PING_DEF_AF;
1224 ret = -1;
1225 }
1226 if (obj->srcaddr != NULL)
1227 {
1228 free (obj->srcaddr);
1229 obj->srcaddr = NULL;
1230 }
1231 break;
1233 case PING_OPT_DATA:
1234 if (obj->data != NULL)
1235 {
1236 free (obj->data);
1237 obj->data = NULL;
1238 }
1239 obj->data = strdup ((const char *) value);
1240 break;
1242 case PING_OPT_SOURCE:
1243 {
1244 char *hostname = (char *) value;
1245 struct addrinfo ai_hints;
1246 struct addrinfo *ai_list;
1247 int status;
1248 #if WITH_DEBUG
1249 if (obj->addrfamily != AF_UNSPEC)
1250 {
1251 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1252 }
1253 #endif
1254 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1255 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1256 #if defined(AI_ADDRCONFIG)
1257 ai_hints.ai_flags = AI_ADDRCONFIG;
1258 #endif
1259 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1260 if (status != 0)
1261 {
1262 #if defined(EAI_SYSTEM)
1263 char errbuf[PING_ERRMSG_LEN];
1264 #endif
1265 ping_set_error (obj, "getaddrinfo",
1266 #if defined(EAI_SYSTEM)
1267 (status == EAI_SYSTEM)
1268 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1269 #endif
1270 gai_strerror (status));
1271 ret = -1;
1272 break;
1273 }
1274 #if WITH_DEBUG
1275 if (ai_list->ai_next != NULL)
1276 {
1277 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1278 }
1279 #endif
1280 if (obj->srcaddr == NULL)
1281 {
1282 obj->srcaddrlen = 0;
1283 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1284 if (obj->srcaddr == NULL)
1285 {
1286 ping_set_errno (obj, errno);
1287 ret = -1;
1288 freeaddrinfo (ai_list);
1289 break;
1290 }
1291 }
1292 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1293 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1294 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1295 ai_list->ai_addrlen);
1296 obj->srcaddrlen = ai_list->ai_addrlen;
1297 obj->addrfamily = ai_list->ai_family;
1299 freeaddrinfo (ai_list);
1300 } /* case PING_OPT_SOURCE */
1301 break;
1303 case PING_OPT_DEVICE:
1304 {
1305 #ifdef SO_BINDTODEVICE
1306 char *device = strdup ((char *) value);
1308 if (device == NULL)
1309 {
1310 ping_set_errno (obj, errno);
1311 ret = -1;
1312 break;
1313 }
1315 if (obj->device != NULL)
1316 free (obj->device);
1317 obj->device = device;
1318 #else /* ! SO_BINDTODEVICE */
1319 ping_set_errno (obj, ENOTSUP);
1320 ret = -1;
1321 #endif /* ! SO_BINDTODEVICE */
1322 } /* case PING_OPT_DEVICE */
1323 break;
1325 default:
1326 ret = -2;
1327 } /* switch (option) */
1329 return (ret);
1330 } /* int ping_setopt */
1333 int ping_send (pingobj_t *obj)
1334 {
1335 if (obj == NULL)
1336 return (-1);
1338 if (ping_send_all (obj) < 0)
1339 return (-1);
1341 return (ping_receive_all (obj));
1342 }
1344 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1345 {
1346 while (ph != NULL)
1347 {
1348 if (strcasecmp (ph->username, host) == 0)
1349 break;
1351 ph = ph->next;
1352 }
1354 return (ph);
1355 }
1357 int ping_host_add (pingobj_t *obj, const char *host)
1358 {
1359 pinghost_t *ph;
1361 struct addrinfo ai_hints;
1362 struct addrinfo *ai_list, *ai_ptr;
1363 int ai_return;
1365 if ((obj == NULL) || (host == NULL))
1366 return (-1);
1368 dprintf ("host = %s\n", host);
1370 if (ping_host_search (obj->head, host) != NULL)
1371 return (0);
1373 memset (&ai_hints, '\0', sizeof (ai_hints));
1374 ai_hints.ai_flags = 0;
1375 #ifdef AI_ADDRCONFIG
1376 ai_hints.ai_flags |= AI_ADDRCONFIG;
1377 #endif
1378 #ifdef AI_CANONNAME
1379 ai_hints.ai_flags |= AI_CANONNAME;
1380 #endif
1381 ai_hints.ai_family = obj->addrfamily;
1382 ai_hints.ai_socktype = SOCK_RAW;
1384 if ((ph = ping_alloc ()) == NULL)
1385 {
1386 dprintf ("Out of memory!\n");
1387 return (-1);
1388 }
1390 if ((ph->username = strdup (host)) == NULL)
1391 {
1392 dprintf ("Out of memory!\n");
1393 ping_set_errno (obj, errno);
1394 ping_free (ph);
1395 return (-1);
1396 }
1398 if ((ph->hostname = strdup (host)) == NULL)
1399 {
1400 dprintf ("Out of memory!\n");
1401 ping_set_errno (obj, errno);
1402 ping_free (ph);
1403 return (-1);
1404 }
1406 /* obj->data is not garuanteed to be != NULL */
1407 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1408 {
1409 dprintf ("Out of memory!\n");
1410 ping_set_errno (obj, errno);
1411 ping_free (ph);
1412 return (-1);
1413 }
1415 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1416 {
1417 #if defined(EAI_SYSTEM)
1418 char errbuf[PING_ERRMSG_LEN];
1419 #endif
1420 dprintf ("getaddrinfo failed\n");
1421 ping_set_error (obj, "getaddrinfo",
1422 #if defined(EAI_SYSTEM)
1423 (ai_return == EAI_SYSTEM)
1424 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1425 #endif
1426 gai_strerror (ai_return));
1427 ping_free (ph);
1428 return (-1);
1429 }
1431 if (ai_list == NULL)
1432 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1434 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1435 {
1436 ph->fd = -1;
1438 if (ai_ptr->ai_family == AF_INET)
1439 {
1440 ai_ptr->ai_socktype = SOCK_RAW;
1441 ai_ptr->ai_protocol = IPPROTO_ICMP;
1442 }
1443 else if (ai_ptr->ai_family == AF_INET6)
1444 {
1445 ai_ptr->ai_socktype = SOCK_RAW;
1446 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1447 }
1448 else
1449 {
1450 char errmsg[PING_ERRMSG_LEN];
1452 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1453 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1455 dprintf ("%s", errmsg);
1456 ping_set_error (obj, "getaddrinfo", errmsg);
1457 continue;
1458 }
1460 /* TODO: Move this to a static function `ping_open_socket' and
1461 * call it whenever the socket dies. */
1462 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1463 if (ph->fd == -1)
1464 {
1465 #if WITH_DEBUG
1466 char errbuf[PING_ERRMSG_LEN];
1467 dprintf ("socket: %s\n",
1468 sstrerror (errno, errbuf, sizeof (errbuf)));
1469 #endif
1470 ping_set_errno (obj, errno);
1471 continue;
1472 }
1474 if (obj->srcaddr != NULL)
1475 {
1476 assert (obj->srcaddrlen > 0);
1477 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1479 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1480 {
1481 #if WITH_DEBUG
1482 char errbuf[PING_ERRMSG_LEN];
1483 dprintf ("bind: %s\n",
1484 sstrerror (errno, errbuf, sizeof (errbuf)));
1485 #endif
1486 ping_set_errno (obj, errno);
1487 close (ph->fd);
1488 ph->fd = -1;
1489 continue;
1490 }
1491 }
1493 #ifdef SO_BINDTODEVICE
1494 if (obj->device != NULL)
1495 {
1496 if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE,
1497 obj->device, strlen (obj->device) + 1) != 0)
1498 {
1499 #if WITH_DEBUG
1500 char errbuf[PING_ERRMSG_LEN];
1501 dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
1502 sstrerror (errno, errbuf, sizeof (errbuf)));
1503 #endif
1504 ping_set_errno (obj, errno);
1505 close (ph->fd);
1506 ph->fd = -1;
1507 continue;
1508 }
1509 }
1510 #endif /* SO_BINDTODEVICE */
1511 #ifdef SO_TIMESTAMP
1512 if (1) /* {{{ */
1513 {
1514 int status;
1515 int opt = 1;
1517 status = setsockopt (ph->fd,
1518 SOL_SOCKET, SO_TIMESTAMP,
1519 &opt, sizeof (opt));
1520 if (status != 0)
1521 {
1522 #if WITH_DEBUG
1523 char errbuf[PING_ERRMSG_LEN];
1524 dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
1525 sstrerror (errno, errbuf, sizeof (errbuf)));
1526 #endif
1527 ping_set_errno (obj, errno);
1528 close (ph->fd);
1529 ph->fd = -1;
1530 continue;
1531 }
1532 } /* }}} if (1) */
1533 #endif /* SO_TIMESTAMP */
1534 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1535 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1536 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1537 ph->addrlen = ai_ptr->ai_addrlen;
1538 ph->addrfamily = ai_ptr->ai_family;
1540 #ifdef AI_CANONNAME
1541 if ((ai_ptr->ai_canonname != NULL)
1542 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1543 {
1544 char *old_hostname;
1546 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1547 ph->hostname, ai_ptr->ai_canonname);
1549 old_hostname = ph->hostname;
1550 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1551 {
1552 /* strdup failed, falling back to old hostname */
1553 ph->hostname = old_hostname;
1554 }
1555 else if (old_hostname != NULL)
1556 {
1557 free (old_hostname);
1558 }
1559 }
1560 #endif /* AI_CANONNAME */
1562 if (ph->addrfamily == AF_INET)
1563 {
1564 int opt;
1566 #ifdef IP_RECVTOS
1567 /* Enable receiving the TOS field */
1568 opt = 1;
1569 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
1570 &opt, sizeof (opt));
1571 #endif /* IP_RECVTOS */
1573 /* Enable receiving the TTL field */
1574 opt = 1;
1575 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1576 &opt, sizeof (opt));
1577 }
1578 #if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
1579 else if (ph->addrfamily == AF_INET6)
1580 {
1581 int opt;
1583 # if defined(IPV6_RECVHOPLIMIT)
1584 /* For details see RFC 3542, section 6.3. */
1585 opt = 1;
1586 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1587 &opt, sizeof (opt));
1588 # endif /* IPV6_RECVHOPLIMIT */
1590 # if defined(IPV6_RECVTCLASS)
1591 /* For details see RFC 3542, section 6.5. */
1592 opt = 1;
1593 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
1594 &opt, sizeof (opt));
1595 # endif /* IPV6_RECVTCLASS */
1596 }
1597 #endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
1599 break;
1600 } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
1602 freeaddrinfo (ai_list);
1604 if (ph->fd < 0)
1605 {
1606 ping_free (ph);
1607 return (-1);
1608 }
1610 /*
1611 * Adding in the front is much easier, but then the iterator will
1612 * return the host that was added last as first host. That's just not
1613 * nice. -octo
1614 */
1615 if (obj->head == NULL)
1616 {
1617 obj->head = ph;
1618 }
1619 else
1620 {
1621 pinghost_t *hptr;
1623 hptr = obj->head;
1624 while (hptr->next != NULL)
1625 hptr = hptr->next;
1627 assert ((hptr != NULL) && (hptr->next == NULL));
1628 hptr->next = ph;
1629 }
1631 ping_set_ttl (ph, obj->ttl);
1632 ping_set_qos (obj, ph, obj->qos);
1634 return (0);
1635 } /* int ping_host_add */
1637 int ping_host_remove (pingobj_t *obj, const char *host)
1638 {
1639 pinghost_t *pre, *cur;
1641 if ((obj == NULL) || (host == NULL))
1642 return (-1);
1644 pre = NULL;
1645 cur = obj->head;
1647 while (cur != NULL)
1648 {
1649 if (strcasecmp (host, cur->username) == 0)
1650 break;
1652 pre = cur;
1653 cur = cur->next;
1654 }
1656 if (cur == NULL)
1657 {
1658 ping_set_error (obj, "ping_host_remove", "Host not found");
1659 return (-1);
1660 }
1662 if (pre == NULL)
1663 obj->head = cur->next;
1664 else
1665 pre->next = cur->next;
1667 ping_free (cur);
1669 return (0);
1670 }
1672 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1673 {
1674 if (obj == NULL)
1675 return (NULL);
1676 return ((pingobj_iter_t *) obj->head);
1677 }
1679 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1680 {
1681 if (iter == NULL)
1682 return (NULL);
1683 return ((pingobj_iter_t *) iter->next);
1684 }
1686 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1687 void *buffer, size_t *buffer_len)
1688 {
1689 int ret = EINVAL;
1691 size_t orig_buffer_len = *buffer_len;
1693 if ((iter == NULL) || (buffer_len == NULL))
1694 return (-1);
1696 if ((buffer == NULL) && (*buffer_len != 0 ))
1697 return (-1);
1699 switch (info)
1700 {
1701 case PING_INFO_USERNAME:
1702 ret = ENOMEM;
1703 *buffer_len = strlen (iter->username) + 1;
1704 if (orig_buffer_len <= *buffer_len)
1705 break;
1706 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1707 * will copy `*buffer_len' and pad the rest of
1708 * `buffer' with null-bytes */
1709 strncpy (buffer, iter->username, orig_buffer_len);
1710 ret = 0;
1711 break;
1713 case PING_INFO_HOSTNAME:
1714 ret = ENOMEM;
1715 *buffer_len = strlen (iter->hostname) + 1;
1716 if (orig_buffer_len < *buffer_len)
1717 break;
1718 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1719 * will copy `*buffer_len' and pad the rest of
1720 * `buffer' with null-bytes */
1721 strncpy (buffer, iter->hostname, orig_buffer_len);
1722 ret = 0;
1723 break;
1725 case PING_INFO_ADDRESS:
1726 ret = getnameinfo ((struct sockaddr *) iter->addr,
1727 iter->addrlen,
1728 (char *) buffer,
1729 *buffer_len,
1730 NULL, 0,
1731 NI_NUMERICHOST);
1732 if (ret != 0)
1733 {
1734 if ((ret == EAI_MEMORY)
1735 #ifdef EAI_OVERFLOW
1736 || (ret == EAI_OVERFLOW)
1737 #endif
1738 )
1739 ret = ENOMEM;
1740 #if defined(EAI_SYSTEM)
1741 else if (ret == EAI_SYSTEM)
1742 ret = errno;
1743 #endif
1744 else
1745 ret = EINVAL;
1746 }
1747 break;
1749 case PING_INFO_FAMILY:
1750 ret = ENOMEM;
1751 *buffer_len = sizeof (int);
1752 if (orig_buffer_len < sizeof (int))
1753 break;
1754 *((int *) buffer) = iter->addrfamily;
1755 ret = 0;
1756 break;
1758 case PING_INFO_LATENCY:
1759 ret = ENOMEM;
1760 *buffer_len = sizeof (double);
1761 if (orig_buffer_len < sizeof (double))
1762 break;
1763 *((double *) buffer) = iter->latency;
1764 ret = 0;
1765 break;
1767 case PING_INFO_DROPPED:
1768 ret = ENOMEM;
1769 *buffer_len = sizeof (uint32_t);
1770 if (orig_buffer_len < sizeof (uint32_t))
1771 break;
1772 *((uint32_t *) buffer) = iter->dropped;
1773 ret = 0;
1774 break;
1776 case PING_INFO_SEQUENCE:
1777 ret = ENOMEM;
1778 *buffer_len = sizeof (unsigned int);
1779 if (orig_buffer_len < sizeof (unsigned int))
1780 break;
1781 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1782 ret = 0;
1783 break;
1785 case PING_INFO_IDENT:
1786 ret = ENOMEM;
1787 *buffer_len = sizeof (uint16_t);
1788 if (orig_buffer_len < sizeof (uint16_t))
1789 break;
1790 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1791 ret = 0;
1792 break;
1794 case PING_INFO_DATA:
1795 ret = ENOMEM;
1796 *buffer_len = strlen (iter->data);
1797 if (orig_buffer_len < *buffer_len)
1798 break;
1799 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1800 ret = 0;
1801 break;
1803 case PING_INFO_RECV_TTL:
1804 ret = ENOMEM;
1805 *buffer_len = sizeof (int);
1806 if (orig_buffer_len < sizeof (int))
1807 break;
1808 *((int *) buffer) = iter->recv_ttl;
1809 ret = 0;
1810 break;
1812 case PING_INFO_RECV_QOS:
1813 ret = ENOMEM;
1814 if (*buffer_len>sizeof(unsigned)) *buffer_len=sizeof(unsigned);
1815 if (!*buffer_len) *buffer_len=1;
1816 if (orig_buffer_len < *buffer_len)
1817 break;
1818 memcpy(buffer,&iter->recv_qos,*buffer_len);
1819 ret = 0;
1820 break;
1821 }
1823 return (ret);
1824 } /* ping_iterator_get_info */
1826 void *ping_iterator_get_context (pingobj_iter_t *iter)
1827 {
1828 if (iter == NULL)
1829 return (NULL);
1830 return (iter->context);
1831 }
1833 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1834 {
1835 if (iter == NULL)
1836 return;
1837 iter->context = context;
1838 }