fc9ed65c63198f081a9e5ecb2b7dc2ab474177a9
1 /**
2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2016 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 set_mark;
146 int mark;
148 char errmsg[PING_ERRMSG_LEN];
150 pinghost_t *head;
151 };
153 /*
154 * private (static) functions
155 */
156 /* Even though Posix requires "strerror_r" to return an "int",
157 * some systems (e.g. the GNU libc) return a "char *" _and_
158 * ignore the second argument ... -tokkee */
159 static char *sstrerror (int errnum, char *buf, size_t buflen)
160 {
161 buf[0] = 0;
163 #if !HAVE_STRERROR_R
164 {
165 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
166 }
167 /* #endif !HAVE_STRERROR_R */
169 #elif STRERROR_R_CHAR_P
170 {
171 char *temp;
172 temp = strerror_r (errnum, buf, buflen);
173 if (buf[0] == 0)
174 {
175 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
176 strncpy (buf, temp, buflen);
177 else
178 strncpy (buf, "strerror_r did not return "
179 "an error message", buflen);
180 }
181 }
182 /* #endif STRERROR_R_CHAR_P */
184 #else
185 if (strerror_r (errnum, buf, buflen) != 0)
186 {
187 snprintf (buf, buflen, "Error %i (%#x); "
188 "Additionally, strerror_r failed.",
189 errnum, errnum);
190 }
191 #endif /* STRERROR_R_CHAR_P */
193 buf[buflen - 1] = 0;
195 return (buf);
196 } /* char *sstrerror */
198 static void ping_set_error (pingobj_t *obj, const char *function,
199 const char *message)
200 {
201 snprintf (obj->errmsg, sizeof (obj->errmsg),
202 "%s: %s", function, message);
203 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
204 }
206 static void ping_set_errno (pingobj_t *obj, int error_number)
207 {
208 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
209 }
211 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
212 struct timeval *res)
213 {
214 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
215 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
217 while (res->tv_usec > 1000000)
218 {
219 res->tv_usec -= 1000000;
220 res->tv_sec++;
221 }
223 return (0);
224 }
226 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
227 struct timeval *res)
228 {
229 if ((tv1->tv_sec < tv2->tv_sec)
230 || ((tv1->tv_sec == tv2->tv_sec)
231 && (tv1->tv_usec < tv2->tv_usec)))
232 return (-1);
234 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
235 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
237 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
239 while (res->tv_usec < 0)
240 {
241 res->tv_usec += 1000000;
242 res->tv_sec--;
243 }
245 return (0);
246 }
248 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
249 {
250 uint32_t sum = 0;
251 uint16_t ret = 0;
253 uint16_t *ptr;
255 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
256 sum += *ptr;
258 if (len == 1)
259 {
260 *(char *) &ret = *(char *) ptr;
261 sum += ret;
262 }
264 /* Do this twice to get all possible carries.. */
265 sum = (sum >> 16) + (sum & 0xFFFF);
266 sum = (sum >> 16) + (sum & 0xFFFF);
268 ret = ~sum;
270 return (ret);
271 }
273 static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
274 size_t buffer_len)
275 {
276 struct ip *ip_hdr;
277 struct icmp *icmp_hdr;
279 size_t ip_hdr_len;
281 uint16_t recv_checksum;
282 uint16_t calc_checksum;
284 uint16_t ident;
285 uint16_t seq;
287 pinghost_t *ptr;
289 if (buffer_len < sizeof (struct ip))
290 return (NULL);
292 ip_hdr = (struct ip *) buffer;
293 ip_hdr_len = ip_hdr->ip_hl << 2;
295 if (buffer_len < ip_hdr_len)
296 return (NULL);
298 buffer += ip_hdr_len;
299 buffer_len -= ip_hdr_len;
301 if (buffer_len < ICMP_MINLEN)
302 return (NULL);
304 icmp_hdr = (struct icmp *) buffer;
305 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
306 {
307 dprintf ("Unexpected ICMP type: %"PRIu8"\n", icmp_hdr->icmp_type);
308 return (NULL);
309 }
311 recv_checksum = icmp_hdr->icmp_cksum;
312 /* This writes to buffer. */
313 icmp_hdr->icmp_cksum = 0;
314 calc_checksum = ping_icmp4_checksum (buffer, 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 < ICMP_MINLEN)
393 return (NULL);
395 icmp_hdr = (struct icmp6_hdr *) buffer;
396 buffer += ICMP_MINLEN;
397 buffer_len -= ICMP_MINLEN;
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 assert (ptr->fd < FD_SETSIZE);
698 FD_SET (ptr->fd, &read_fds);
699 FD_SET (ptr->fd, &err_fds);
700 num_fds++;
702 if (max_fd < ptr->fd)
703 max_fd = ptr->fd;
704 }
706 if (num_fds == 0)
707 break;
709 if (gettimeofday (&nowtime, NULL) == -1)
710 {
711 ping_set_errno (obj, errno);
712 return (-1);
713 }
715 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
716 break;
718 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
719 (int) timeout.tv_sec,
720 (int) timeout.tv_usec);
722 status = select (max_fd + 1, &read_fds, NULL, &err_fds, &timeout);
724 if (gettimeofday (&nowtime, NULL) == -1)
725 {
726 ping_set_errno (obj, errno);
727 return (-1);
728 }
730 if ((status == -1) && (errno == EINTR))
731 {
732 dprintf ("select was interrupted by signal..\n");
733 ping_set_errno (obj, EINTR);
734 return (-EINTR);
735 }
736 else if (status < 0)
737 {
738 #if WITH_DEBUG
739 char errbuf[PING_ERRMSG_LEN];
740 dprintf ("select: %s\n",
741 sstrerror (errno, errbuf, sizeof (errbuf)));
742 #endif
743 break;
744 }
745 else if (status == 0)
746 {
747 dprintf ("select timed out\n");
748 for (ptr = ph; ptr != NULL; ptr = ptr->next)
749 if (ptr->latency < 0.0)
750 ptr->dropped++;
751 break;
752 }
754 for (ptr = ph; ptr != NULL; ptr = ptr->next)
755 {
756 if (FD_ISSET (ptr->fd, &read_fds))
757 {
758 if (ping_receive_one (obj, ptr, &nowtime) == 0)
759 ret++;
760 }
761 else if (FD_ISSET (ptr->fd, &err_fds))
762 {
763 /* clear the timer in this case so that we
764 * don't run into an endless loop. */
765 /* TODO: Set an error flag in this case. */
766 timerclear (ptr->timer);
767 }
768 }
769 } /* while (1) */
771 return (ret);
772 } /* int ping_receive_all */
774 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
775 * Sending functions: *
776 * *
777 * ping_send_all *
778 * +-> ping_send_one_ipv4 *
779 * `-> ping_send_one_ipv6 *
780 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
781 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
782 const void *buf, size_t buflen)
783 {
784 ssize_t ret;
786 if (gettimeofday (ph->timer, NULL) == -1)
787 {
788 timerclear (ph->timer);
789 return (-1);
790 }
792 ret = sendto (ph->fd, buf, buflen, 0,
793 (struct sockaddr *) ph->addr, ph->addrlen);
795 if (ret < 0)
796 {
797 #if defined(EHOSTUNREACH)
798 if (errno == EHOSTUNREACH)
799 return (0);
800 #endif
801 #if defined(ENETUNREACH)
802 if (errno == ENETUNREACH)
803 return (0);
804 #endif
805 ping_set_errno (obj, errno);
806 }
808 return (ret);
809 }
811 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
812 {
813 struct icmp *icmp4;
814 int status;
816 char buf[4096] = {0};
817 size_t buflen;
819 char *data;
820 size_t datalen;
822 dprintf ("ph->hostname = %s\n", ph->hostname);
824 icmp4 = (struct icmp *) buf;
825 *icmp4 = (struct icmp) {
826 .icmp_type = ICMP_ECHO,
827 .icmp_id = htons (ph->ident),
828 .icmp_seq = htons (ph->sequence),
829 };
831 datalen = strlen (ph->data);
832 buflen = ICMP_MINLEN + datalen;
833 if (sizeof (buf) < buflen)
834 return (EINVAL);
836 data = buf + ICMP_MINLEN;
837 memcpy (data, ph->data, datalen);
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] = {0};
861 int buflen;
863 char *data;
864 int datalen;
866 dprintf ("ph->hostname = %s\n", ph->hostname);
868 icmp6 = (struct icmp6_hdr *) buf;
869 *icmp6 = (struct icmp6_hdr) {
870 .icmp6_type = ICMP6_ECHO_REQUEST,
871 .icmp6_id = htons (ph->ident),
872 .icmp6_seq = htons (ph->sequence),
873 };
875 datalen = strlen (ph->data);
876 buflen = sizeof (*icmp6) + datalen;
877 if (sizeof (buf) < buflen)
878 return (EINVAL);
880 data = buf + ICMP_MINLEN;
881 memcpy (data, ph->data, datalen);
883 /* The checksum will be calculated by the TCP/IP stack. */
885 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
887 status = ping_sendto (obj, ph, buf, buflen);
888 if (status < 0)
889 {
890 perror ("ping_sendto");
891 return (-1);
892 }
894 dprintf ("sendto: status = %i\n", status);
896 return (0);
897 }
899 static int ping_send_all (pingobj_t *obj)
900 {
901 pinghost_t *ph;
902 pinghost_t *ptr;
904 int ret;
906 ret = 0;
907 ph = obj->head;
909 for (ptr = ph; ptr != NULL; ptr = ptr->next)
910 {
911 /* start timer.. The GNU `ping6' starts the timer before
912 * sending the packet, so I will do that too */
913 if (gettimeofday (ptr->timer, NULL) == -1)
914 {
915 #if WITH_DEBUG
916 char errbuf[PING_ERRMSG_LEN];
917 dprintf ("gettimeofday: %s\n",
918 sstrerror (errno, errbuf, sizeof (errbuf)));
919 #endif
920 timerclear (ptr->timer);
921 ret--;
922 continue;
923 }
924 else
925 {
926 dprintf ("timer set for hostname = %s\n", ptr->hostname);
927 }
929 if (ptr->addrfamily == AF_INET6)
930 {
931 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
932 if (ping_send_one_ipv6 (obj, ptr) != 0)
933 {
934 timerclear (ptr->timer);
935 ret--;
936 continue;
937 }
938 }
939 else if (ptr->addrfamily == AF_INET)
940 {
941 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
942 if (ping_send_one_ipv4 (obj, ptr) != 0)
943 {
944 timerclear (ptr->timer);
945 ret--;
946 continue;
947 }
948 }
949 else /* this should not happen */
950 {
951 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
952 timerclear (ptr->timer);
953 ret--;
954 continue;
955 }
957 ptr->sequence++;
958 }
960 return (ret);
961 }
963 /*
964 * Set the TTL of a socket protocol independently.
965 */
966 static int ping_set_ttl (pinghost_t *ph, int ttl)
967 {
968 int ret = -2;
970 if (ph->addrfamily == AF_INET)
971 {
972 dprintf ("Setting TTLv4 to %i\n", ttl);
973 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
974 &ttl, sizeof (ttl));
975 }
976 else if (ph->addrfamily == AF_INET6)
977 {
978 dprintf ("Setting TTLv6 to %i\n", ttl);
979 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
980 &ttl, sizeof (ttl));
981 }
983 return (ret);
984 }
986 /*
987 * Set the TOS of a socket protocol independently.
988 *
989 * Using SOL_SOCKET / SO_PRIORITY might be a protocol independent way to
990 * set this. See socket(7) for details.
991 */
992 static int ping_set_qos (pingobj_t *obj, pinghost_t *ph, uint8_t qos)
993 {
994 int ret = EINVAL;
995 char errbuf[PING_ERRMSG_LEN];
997 if (ph->addrfamily == AF_INET)
998 {
999 dprintf ("Setting TP_TOS to %#04"PRIx8"\n", qos);
1000 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
1001 &qos, sizeof (qos));
1002 if (ret != 0)
1003 {
1004 ret = errno;
1005 ping_set_error (obj, "ping_set_qos",
1006 sstrerror (ret, errbuf, sizeof (errbuf)));
1007 dprintf ("Setting TP_TOS failed: %s\n", errbuf);
1008 }
1009 }
1010 else if (ph->addrfamily == AF_INET6)
1011 {
1012 /* IPV6_TCLASS requires an "int". */
1013 int tmp = (int) qos;
1015 dprintf ("Setting IPV6_TCLASS to %#04"PRIx8" (%i)\n", qos, tmp);
1016 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_TCLASS,
1017 &tmp, sizeof (tmp));
1018 if (ret != 0)
1019 {
1020 ret = errno;
1021 ping_set_error (obj, "ping_set_qos",
1022 sstrerror (ret, errbuf, sizeof (errbuf)));
1023 dprintf ("Setting IPV6_TCLASS failed: %s\n", errbuf);
1024 }
1025 }
1027 return (ret);
1028 }
1030 static int ping_get_ident (void)
1031 {
1032 int fd;
1033 static int did_seed = 0;
1035 int retval;
1037 if (did_seed == 0)
1038 {
1039 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
1040 {
1041 unsigned int seed;
1043 if (read (fd, &seed, sizeof (seed)) != -1)
1044 {
1045 did_seed = 1;
1046 dprintf ("Random seed: %#x\n", seed);
1047 srandom (seed);
1048 }
1050 close (fd);
1051 }
1052 #if WITH_DEBUG
1053 else
1054 {
1055 char errbuf[PING_ERRMSG_LEN];
1056 dprintf ("open (/dev/urandom): %s\n",
1057 sstrerror (errno, errbuf, sizeof (errbuf)));
1058 }
1059 #endif
1060 }
1062 retval = (int) random ();
1064 dprintf ("Random number: %#x\n", retval);
1066 return (retval);
1067 }
1069 static pinghost_t *ping_alloc (void)
1070 {
1071 pinghost_t *ph;
1072 size_t ph_size;
1074 ph_size = sizeof (pinghost_t)
1075 + sizeof (struct sockaddr_storage)
1076 + sizeof (struct timeval);
1078 ph = (pinghost_t *) malloc (ph_size);
1079 if (ph == NULL)
1080 return (NULL);
1082 memset (ph, '\0', ph_size);
1084 ph->timer = (struct timeval *) (ph + 1);
1085 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
1087 ph->addrlen = sizeof (struct sockaddr_storage);
1088 ph->fd = -1;
1089 ph->latency = -1.0;
1090 ph->dropped = 0;
1091 ph->ident = ping_get_ident () & 0xFFFF;
1093 return (ph);
1094 }
1096 static void ping_free (pinghost_t *ph)
1097 {
1098 if (ph->fd >= 0)
1099 close (ph->fd);
1101 if (ph->username != NULL)
1102 free (ph->username);
1104 if (ph->hostname != NULL)
1105 free (ph->hostname);
1107 if (ph->data != NULL)
1108 free (ph->data);
1110 free (ph);
1111 }
1113 /*
1114 * public methods
1115 */
1116 const char *ping_get_error (pingobj_t *obj)
1117 {
1118 if (obj == NULL)
1119 return (NULL);
1120 return (obj->errmsg);
1121 }
1123 pingobj_t *ping_construct (void)
1124 {
1125 pingobj_t *obj;
1127 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1128 return (NULL);
1129 memset (obj, 0, sizeof (pingobj_t));
1131 obj->timeout = PING_DEF_TIMEOUT;
1132 obj->ttl = PING_DEF_TTL;
1133 obj->addrfamily = PING_DEF_AF;
1134 obj->data = strdup (PING_DEF_DATA);
1135 obj->qos = 0;
1137 return (obj);
1138 }
1140 void ping_destroy (pingobj_t *obj)
1141 {
1142 pinghost_t *current;
1143 pinghost_t *next;
1145 if (obj == NULL)
1146 return;
1148 current = obj->head;
1149 next = NULL;
1151 while (current != NULL)
1152 {
1153 next = current->next;
1154 ping_free (current);
1155 current = next;
1156 }
1158 if (obj->data != NULL)
1159 free (obj->data);
1161 if (obj->srcaddr != NULL)
1162 free (obj->srcaddr);
1164 if (obj->device != NULL)
1165 free (obj->device);
1167 free (obj);
1169 return;
1170 }
1172 int ping_setopt (pingobj_t *obj, int option, void *value)
1173 {
1174 int ret = 0;
1176 if ((obj == NULL) || (value == NULL))
1177 return (-1);
1179 switch (option)
1180 {
1181 case PING_OPT_QOS:
1182 {
1183 pinghost_t *ph;
1185 obj->qos = *((uint8_t *) value);
1186 for (ph = obj->head; ph != NULL; ph = ph->next)
1187 ping_set_qos (obj, ph, obj->qos);
1188 break;
1189 }
1191 case PING_OPT_TIMEOUT:
1192 obj->timeout = *((double *) value);
1193 if (obj->timeout < 0.0)
1194 {
1195 obj->timeout = PING_DEF_TIMEOUT;
1196 ret = -1;
1197 }
1198 break;
1200 case PING_OPT_TTL:
1201 obj->ttl = *((int *) value);
1202 if ((obj->ttl < 1) || (obj->ttl > 255))
1203 {
1204 obj->ttl = PING_DEF_TTL;
1205 ret = -1;
1206 }
1207 else
1208 {
1209 pinghost_t *ph;
1211 for (ph = obj->head; ph != NULL; ph = ph->next)
1212 ping_set_ttl (ph, obj->ttl);
1213 }
1214 break;
1216 case PING_OPT_AF:
1217 obj->addrfamily = *((int *) value);
1218 if ((obj->addrfamily != AF_UNSPEC)
1219 && (obj->addrfamily != AF_INET)
1220 && (obj->addrfamily != AF_INET6))
1221 {
1222 obj->addrfamily = PING_DEF_AF;
1223 ret = -1;
1224 }
1225 if (obj->srcaddr != NULL)
1226 {
1227 free (obj->srcaddr);
1228 obj->srcaddr = NULL;
1229 }
1230 break;
1232 case PING_OPT_DATA:
1233 if (obj->data != NULL)
1234 {
1235 free (obj->data);
1236 obj->data = NULL;
1237 }
1238 obj->data = strdup ((const char *) value);
1239 break;
1241 case PING_OPT_SOURCE:
1242 {
1243 char *hostname = (char *) value;
1244 struct addrinfo ai_hints;
1245 struct addrinfo *ai_list;
1246 int status;
1247 #if WITH_DEBUG
1248 if (obj->addrfamily != AF_UNSPEC)
1249 {
1250 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1251 }
1252 #endif
1253 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1254 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1255 #if defined(AI_ADDRCONFIG)
1256 ai_hints.ai_flags = AI_ADDRCONFIG;
1257 #endif
1258 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1259 if (status != 0)
1260 {
1261 #if defined(EAI_SYSTEM)
1262 char errbuf[PING_ERRMSG_LEN];
1263 #endif
1264 ping_set_error (obj, "getaddrinfo",
1265 #if defined(EAI_SYSTEM)
1266 (status == EAI_SYSTEM)
1267 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1268 #endif
1269 gai_strerror (status));
1270 ret = -1;
1271 break;
1272 }
1273 #if WITH_DEBUG
1274 if (ai_list->ai_next != NULL)
1275 {
1276 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1277 }
1278 #endif
1279 if (obj->srcaddr == NULL)
1280 {
1281 obj->srcaddrlen = 0;
1282 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1283 if (obj->srcaddr == NULL)
1284 {
1285 ping_set_errno (obj, errno);
1286 ret = -1;
1287 freeaddrinfo (ai_list);
1288 break;
1289 }
1290 }
1291 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1292 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1293 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1294 ai_list->ai_addrlen);
1295 obj->srcaddrlen = ai_list->ai_addrlen;
1296 obj->addrfamily = ai_list->ai_family;
1298 freeaddrinfo (ai_list);
1299 } /* case PING_OPT_SOURCE */
1300 break;
1302 case PING_OPT_DEVICE:
1303 {
1304 #ifdef SO_BINDTODEVICE
1305 char *device = strdup ((char *) value);
1307 if (device == NULL)
1308 {
1309 ping_set_errno (obj, errno);
1310 ret = -1;
1311 break;
1312 }
1314 if (obj->device != NULL)
1315 free (obj->device);
1316 obj->device = device;
1317 #else /* ! SO_BINDTODEVICE */
1318 ping_set_errno (obj, ENOTSUP);
1319 ret = -1;
1320 #endif /* ! SO_BINDTODEVICE */
1321 } /* case PING_OPT_DEVICE */
1322 break;
1324 case PING_OPT_MARK:
1325 {
1326 #ifdef SO_MARK
1327 obj->mark = *(int*)(value);
1328 obj->set_mark = 1;
1329 #else /* SO_MARK */
1330 ping_set_errno (obj, ENOTSUP);
1331 ret = -1;
1332 #endif /* !SO_MARK */
1334 } /* case PING_OPT_MARK */
1335 break;
1337 default:
1338 ret = -2;
1339 } /* switch (option) */
1341 return (ret);
1342 } /* int ping_setopt */
1345 int ping_send (pingobj_t *obj)
1346 {
1347 if (obj == NULL)
1348 return (-1);
1350 if (ping_send_all (obj) < 0)
1351 return (-1);
1353 return (ping_receive_all (obj));
1354 }
1356 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1357 {
1358 while (ph != NULL)
1359 {
1360 if (strcasecmp (ph->username, host) == 0)
1361 break;
1363 ph = ph->next;
1364 }
1366 return (ph);
1367 }
1369 int ping_host_add (pingobj_t *obj, const char *host)
1370 {
1371 pinghost_t *ph;
1373 struct addrinfo ai_hints;
1374 struct addrinfo *ai_list, *ai_ptr;
1375 int ai_return;
1377 if ((obj == NULL) || (host == NULL))
1378 return (-1);
1380 dprintf ("host = %s\n", host);
1382 if (ping_host_search (obj->head, host) != NULL)
1383 return (0);
1385 memset (&ai_hints, '\0', sizeof (ai_hints));
1386 ai_hints.ai_flags = 0;
1387 #ifdef AI_ADDRCONFIG
1388 ai_hints.ai_flags |= AI_ADDRCONFIG;
1389 #endif
1390 #ifdef AI_CANONNAME
1391 ai_hints.ai_flags |= AI_CANONNAME;
1392 #endif
1393 ai_hints.ai_family = obj->addrfamily;
1394 ai_hints.ai_socktype = SOCK_RAW;
1396 if ((ph = ping_alloc ()) == NULL)
1397 {
1398 dprintf ("Out of memory!\n");
1399 return (-1);
1400 }
1402 if ((ph->username = strdup (host)) == NULL)
1403 {
1404 dprintf ("Out of memory!\n");
1405 ping_set_errno (obj, errno);
1406 ping_free (ph);
1407 return (-1);
1408 }
1410 if ((ph->hostname = strdup (host)) == NULL)
1411 {
1412 dprintf ("Out of memory!\n");
1413 ping_set_errno (obj, errno);
1414 ping_free (ph);
1415 return (-1);
1416 }
1418 /* obj->data is not garuanteed to be != NULL */
1419 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1420 {
1421 dprintf ("Out of memory!\n");
1422 ping_set_errno (obj, errno);
1423 ping_free (ph);
1424 return (-1);
1425 }
1427 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1428 {
1429 #if defined(EAI_SYSTEM)
1430 char errbuf[PING_ERRMSG_LEN];
1431 #endif
1432 dprintf ("getaddrinfo failed\n");
1433 ping_set_error (obj, "getaddrinfo",
1434 #if defined(EAI_SYSTEM)
1435 (ai_return == EAI_SYSTEM)
1436 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1437 #endif
1438 gai_strerror (ai_return));
1439 ping_free (ph);
1440 return (-1);
1441 }
1443 if (ai_list == NULL)
1444 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1446 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1447 {
1448 ph->fd = -1;
1450 if (ai_ptr->ai_family == AF_INET)
1451 {
1452 ai_ptr->ai_socktype = SOCK_RAW;
1453 ai_ptr->ai_protocol = IPPROTO_ICMP;
1454 }
1455 else if (ai_ptr->ai_family == AF_INET6)
1456 {
1457 ai_ptr->ai_socktype = SOCK_RAW;
1458 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1459 }
1460 else
1461 {
1462 char errmsg[PING_ERRMSG_LEN];
1464 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1465 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1467 dprintf ("%s", errmsg);
1468 ping_set_error (obj, "getaddrinfo", errmsg);
1469 continue;
1470 }
1472 /* TODO: Move this to a static function `ping_open_socket' and
1473 * call it whenever the socket dies. */
1474 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1475 if (ph->fd == -1)
1476 {
1477 #if WITH_DEBUG
1478 char errbuf[PING_ERRMSG_LEN];
1479 dprintf ("socket: %s\n",
1480 sstrerror (errno, errbuf, sizeof (errbuf)));
1481 #endif
1482 ping_set_errno (obj, errno);
1483 continue;
1484 }
1485 else if (ph->fd >= FD_SETSIZE)
1486 {
1487 dprintf("socket(2) returned file descriptor %d, which is above the file "
1488 "descriptor limit for select(2) (FD_SETSIZE = %d)\n",
1489 ph->fd, FD_SETSIZE);
1490 close(ph->fd);
1491 ph->fd = -1;
1492 ping_set_errno(obj, EMFILE);
1493 continue;
1494 }
1496 if (obj->srcaddr != NULL)
1497 {
1498 assert (obj->srcaddrlen > 0);
1499 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1501 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1502 {
1503 #if WITH_DEBUG
1504 char errbuf[PING_ERRMSG_LEN];
1505 dprintf ("bind: %s\n",
1506 sstrerror (errno, errbuf, sizeof (errbuf)));
1507 #endif
1508 ping_set_errno (obj, errno);
1509 close (ph->fd);
1510 ph->fd = -1;
1511 continue;
1512 }
1513 }
1515 #ifdef SO_BINDTODEVICE
1516 if (obj->device != NULL)
1517 {
1518 if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE,
1519 obj->device, strlen (obj->device) + 1) != 0)
1520 {
1521 #if WITH_DEBUG
1522 char errbuf[PING_ERRMSG_LEN];
1523 dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
1524 sstrerror (errno, errbuf, sizeof (errbuf)));
1525 #endif
1526 ping_set_errno (obj, errno);
1527 close (ph->fd);
1528 ph->fd = -1;
1529 continue;
1530 }
1531 }
1532 #endif /* SO_BINDTODEVICE */
1533 #ifdef SO_MARK
1534 if(obj->set_mark)
1535 {
1536 if(setsockopt(ph->fd, SOL_SOCKET, SO_MARK, &(obj->mark), sizeof(obj->mark)) != 0)
1537 {
1538 #if WITH_DEBUG
1539 char errbuf[PING_ERRMSG_LEN];
1540 dprintf ("setsockopt (SO_MARK): %s\n",
1541 sstrerror (errno, errbuf, sizeof (errbuf)));
1542 #endif
1543 ping_set_errno (obj, errno);
1544 close (ph->fd);
1545 ph->fd = -1;
1546 continue;
1547 }
1548 }
1549 #endif
1550 #ifdef SO_TIMESTAMP
1551 if (1) /* {{{ */
1552 {
1553 int status;
1554 int opt = 1;
1556 status = setsockopt (ph->fd,
1557 SOL_SOCKET, SO_TIMESTAMP,
1558 &opt, sizeof (opt));
1559 if (status != 0)
1560 {
1561 #if WITH_DEBUG
1562 char errbuf[PING_ERRMSG_LEN];
1563 dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
1564 sstrerror (errno, errbuf, sizeof (errbuf)));
1565 #endif
1566 ping_set_errno (obj, errno);
1567 close (ph->fd);
1568 ph->fd = -1;
1569 continue;
1570 }
1571 } /* }}} if (1) */
1572 #endif /* SO_TIMESTAMP */
1573 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1574 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1575 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1576 ph->addrlen = ai_ptr->ai_addrlen;
1577 ph->addrfamily = ai_ptr->ai_family;
1579 #ifdef AI_CANONNAME
1580 if ((ai_ptr->ai_canonname != NULL)
1581 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1582 {
1583 char *old_hostname;
1585 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1586 ph->hostname, ai_ptr->ai_canonname);
1588 old_hostname = ph->hostname;
1589 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1590 {
1591 /* strdup failed, falling back to old hostname */
1592 ph->hostname = old_hostname;
1593 }
1594 else if (old_hostname != NULL)
1595 {
1596 free (old_hostname);
1597 }
1598 }
1599 #endif /* AI_CANONNAME */
1601 if (ph->addrfamily == AF_INET)
1602 {
1603 int opt;
1605 #ifdef IP_RECVTOS
1606 /* Enable receiving the TOS field */
1607 opt = 1;
1608 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
1609 &opt, sizeof (opt));
1610 #endif /* IP_RECVTOS */
1612 /* Enable receiving the TTL field */
1613 opt = 1;
1614 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1615 &opt, sizeof (opt));
1616 }
1617 #if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
1618 else if (ph->addrfamily == AF_INET6)
1619 {
1620 int opt;
1622 # if defined(IPV6_RECVHOPLIMIT)
1623 /* For details see RFC 3542, section 6.3. */
1624 opt = 1;
1625 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1626 &opt, sizeof (opt));
1627 # endif /* IPV6_RECVHOPLIMIT */
1629 # if defined(IPV6_RECVTCLASS)
1630 /* For details see RFC 3542, section 6.5. */
1631 opt = 1;
1632 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
1633 &opt, sizeof (opt));
1634 # endif /* IPV6_RECVTCLASS */
1635 }
1636 #endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
1638 break;
1639 } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
1641 freeaddrinfo (ai_list);
1643 if (ph->fd < 0)
1644 {
1645 ping_free (ph);
1646 return (-1);
1647 }
1649 /*
1650 * Adding in the front is much easier, but then the iterator will
1651 * return the host that was added last as first host. That's just not
1652 * nice. -octo
1653 */
1654 if (obj->head == NULL)
1655 {
1656 obj->head = ph;
1657 }
1658 else
1659 {
1660 pinghost_t *hptr;
1662 hptr = obj->head;
1663 while (hptr->next != NULL)
1664 hptr = hptr->next;
1666 assert ((hptr != NULL) && (hptr->next == NULL));
1667 hptr->next = ph;
1668 }
1670 ping_set_ttl (ph, obj->ttl);
1671 ping_set_qos (obj, ph, obj->qos);
1673 return (0);
1674 } /* int ping_host_add */
1676 int ping_host_remove (pingobj_t *obj, const char *host)
1677 {
1678 pinghost_t *pre, *cur;
1680 if ((obj == NULL) || (host == NULL))
1681 return (-1);
1683 pre = NULL;
1684 cur = obj->head;
1686 while (cur != NULL)
1687 {
1688 if (strcasecmp (host, cur->username) == 0)
1689 break;
1691 pre = cur;
1692 cur = cur->next;
1693 }
1695 if (cur == NULL)
1696 {
1697 ping_set_error (obj, "ping_host_remove", "Host not found");
1698 return (-1);
1699 }
1701 if (pre == NULL)
1702 obj->head = cur->next;
1703 else
1704 pre->next = cur->next;
1706 ping_free (cur);
1708 return (0);
1709 }
1711 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1712 {
1713 if (obj == NULL)
1714 return (NULL);
1715 return ((pingobj_iter_t *) obj->head);
1716 }
1718 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1719 {
1720 if (iter == NULL)
1721 return (NULL);
1722 return ((pingobj_iter_t *) iter->next);
1723 }
1725 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1726 void *buffer, size_t *buffer_len)
1727 {
1728 int ret = EINVAL;
1730 size_t orig_buffer_len = *buffer_len;
1732 if ((iter == NULL) || (buffer_len == NULL))
1733 return (-1);
1735 if ((buffer == NULL) && (*buffer_len != 0 ))
1736 return (-1);
1738 switch (info)
1739 {
1740 case PING_INFO_USERNAME:
1741 ret = ENOMEM;
1742 *buffer_len = strlen (iter->username) + 1;
1743 if (orig_buffer_len <= *buffer_len)
1744 break;
1745 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1746 * will copy `*buffer_len' and pad the rest of
1747 * `buffer' with null-bytes */
1748 strncpy (buffer, iter->username, orig_buffer_len);
1749 ret = 0;
1750 break;
1752 case PING_INFO_HOSTNAME:
1753 ret = ENOMEM;
1754 *buffer_len = strlen (iter->hostname) + 1;
1755 if (orig_buffer_len < *buffer_len)
1756 break;
1757 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1758 * will copy `*buffer_len' and pad the rest of
1759 * `buffer' with null-bytes */
1760 strncpy (buffer, iter->hostname, orig_buffer_len);
1761 ret = 0;
1762 break;
1764 case PING_INFO_ADDRESS:
1765 ret = getnameinfo ((struct sockaddr *) iter->addr,
1766 iter->addrlen,
1767 (char *) buffer,
1768 *buffer_len,
1769 NULL, 0,
1770 NI_NUMERICHOST);
1771 if (ret != 0)
1772 {
1773 if ((ret == EAI_MEMORY)
1774 #ifdef EAI_OVERFLOW
1775 || (ret == EAI_OVERFLOW)
1776 #endif
1777 )
1778 ret = ENOMEM;
1779 #if defined(EAI_SYSTEM)
1780 else if (ret == EAI_SYSTEM)
1781 ret = errno;
1782 #endif
1783 else
1784 ret = EINVAL;
1785 }
1786 break;
1788 case PING_INFO_FAMILY:
1789 ret = ENOMEM;
1790 *buffer_len = sizeof (int);
1791 if (orig_buffer_len < sizeof (int))
1792 break;
1793 *((int *) buffer) = iter->addrfamily;
1794 ret = 0;
1795 break;
1797 case PING_INFO_LATENCY:
1798 ret = ENOMEM;
1799 *buffer_len = sizeof (double);
1800 if (orig_buffer_len < sizeof (double))
1801 break;
1802 *((double *) buffer) = iter->latency;
1803 ret = 0;
1804 break;
1806 case PING_INFO_DROPPED:
1807 ret = ENOMEM;
1808 *buffer_len = sizeof (uint32_t);
1809 if (orig_buffer_len < sizeof (uint32_t))
1810 break;
1811 *((uint32_t *) buffer) = iter->dropped;
1812 ret = 0;
1813 break;
1815 case PING_INFO_SEQUENCE:
1816 ret = ENOMEM;
1817 *buffer_len = sizeof (unsigned int);
1818 if (orig_buffer_len < sizeof (unsigned int))
1819 break;
1820 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1821 ret = 0;
1822 break;
1824 case PING_INFO_IDENT:
1825 ret = ENOMEM;
1826 *buffer_len = sizeof (uint16_t);
1827 if (orig_buffer_len < sizeof (uint16_t))
1828 break;
1829 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1830 ret = 0;
1831 break;
1833 case PING_INFO_DATA:
1834 ret = ENOMEM;
1835 *buffer_len = strlen (iter->data);
1836 if (orig_buffer_len < *buffer_len)
1837 break;
1838 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1839 ret = 0;
1840 break;
1842 case PING_INFO_RECV_TTL:
1843 ret = ENOMEM;
1844 *buffer_len = sizeof (int);
1845 if (orig_buffer_len < sizeof (int))
1846 break;
1847 *((int *) buffer) = iter->recv_ttl;
1848 ret = 0;
1849 break;
1851 case PING_INFO_RECV_QOS:
1852 ret = ENOMEM;
1853 if (*buffer_len>sizeof(unsigned)) *buffer_len=sizeof(unsigned);
1854 if (!*buffer_len) *buffer_len=1;
1855 if (orig_buffer_len < *buffer_len)
1856 break;
1857 memcpy(buffer,&iter->recv_qos,*buffer_len);
1858 ret = 0;
1859 break;
1860 }
1862 return (ret);
1863 } /* ping_iterator_get_info */
1865 void *ping_iterator_get_context (pingobj_iter_t *iter)
1866 {
1867 if (iter == NULL)
1868 return (NULL);
1869 return (iter->context);
1870 }
1872 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1873 {
1874 if (iter == NULL)
1875 return;
1876 iter->context = context;
1877 }