1 /**
2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006 Florian octo Forster <octo at verplant.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <string.h>
26 #include <assert.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
32 #include <sys/socket.h>
33 #include <netdb.h>
34 #include <netinet/ip_icmp.h>
35 #include <netinet/icmp6.h>
37 #include <sys/time.h>
38 #include <time.h>
40 #include "liboping.h"
42 #if DEBUG
43 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
44 #else
45 # define dprintf(...) /**/
46 #endif
48 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
50 /*
51 * private (static) functions
52 */
53 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
54 struct timeval *res)
55 {
57 if ((tv1->tv_sec < tv2->tv_sec)
58 || ((tv1->tv_sec == tv2->tv_sec)
59 && (tv1->tv_usec < tv2->tv_usec)))
60 return (-1);
62 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
63 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
65 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
67 while (res->tv_usec < 0)
68 {
69 res->tv_usec += 1000000;
70 res->tv_sec--;
71 }
73 return (0);
74 }
76 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
77 {
78 uint32_t sum = 0;
79 uint16_t ret = 0;
81 uint16_t *ptr;
83 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
84 sum += *ptr;
86 if (len == 1)
87 {
88 *(char *) &ret = *(char *) ptr;
89 sum += ret;
90 }
92 /* Do this twice to get all possible carries.. */
93 sum = (sum >> 16) + (sum & 0xFFFF);
94 sum = (sum >> 16) + (sum & 0xFFFF);
96 ret = ~sum;
98 return (ret);
99 }
101 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
102 {
103 struct ip *ip_hdr;
104 struct icmphdr *icmp_hdr;
106 size_t ip_hdr_len;
108 uint16_t recv_checksum;
109 uint16_t calc_checksum;
111 uint16_t ident;
112 uint16_t seq;
114 pinghost_t *ptr;
116 if (buffer_len < sizeof (struct ip))
117 return (NULL);
119 ip_hdr = (struct ip *) buffer;
120 ip_hdr_len = ip_hdr->ip_hl << 2;
122 if (buffer_len < ip_hdr_len)
123 return (NULL);
125 buffer += ip_hdr_len;
126 buffer_len -= ip_hdr_len;
128 if (buffer_len < sizeof (struct icmphdr))
129 return (NULL);
131 icmp_hdr = (struct icmphdr *) buffer;
132 buffer += sizeof (struct icmphdr);
133 buffer_len -= sizeof (struct icmphdr);
135 if (icmp_hdr->type != ICMP_ECHOREPLY)
136 {
137 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->type);
138 return (NULL);
139 }
141 recv_checksum = icmp_hdr->checksum;
142 icmp_hdr->checksum = 0;
143 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
144 sizeof (struct icmphdr) + buffer_len);
146 if (recv_checksum != calc_checksum)
147 {
148 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
149 recv_checksum, calc_checksum);
150 return (NULL);
151 }
153 ident = ntohs (icmp_hdr->un.echo.id);
154 seq = ntohs (icmp_hdr->un.echo.sequence);
156 for (ptr = ph; ptr != NULL; ptr = ptr->next)
157 {
158 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
159 ptr->hostname, ptr->ident, ptr->sequence - 1);
161 if (ptr->addrfamily != AF_INET)
162 continue;
164 if (!timerisset (ptr->timer))
165 continue;
167 if (ptr->ident != ident)
168 continue;
170 if ((ptr->sequence - 1) != seq)
171 continue;
173 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
174 ptr->hostname, ident, seq);
176 break;
177 }
179 if (ptr == NULL)
180 {
181 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
182 ident, seq);
183 }
185 return (ptr);
186 }
188 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
189 {
190 struct icmp6_hdr *icmp_hdr;
192 uint16_t ident;
193 uint16_t seq;
195 pinghost_t *ptr;
197 if (buffer_len < sizeof (struct icmp6_hdr))
198 return (NULL);
200 icmp_hdr = (struct icmp6_hdr *) buffer;
201 buffer += sizeof (struct icmphdr);
202 buffer_len -= sizeof (struct icmphdr);
204 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
205 {
206 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
207 return (NULL);
208 }
210 if (icmp_hdr->icmp6_code != 0)
211 {
212 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
213 return (NULL);
214 }
216 ident = ntohs (icmp_hdr->icmp6_id);
217 seq = ntohs (icmp_hdr->icmp6_seq);
219 for (ptr = ph; ptr != NULL; ptr = ptr->next)
220 {
221 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
222 ptr->hostname, ptr->ident, ptr->sequence - 1);
224 if (ptr->addrfamily != AF_INET6)
225 continue;
227 if (!timerisset (ptr->timer))
228 continue;
230 if (ptr->ident != ident)
231 continue;
233 if ((ptr->sequence - 1) != seq)
234 continue;
236 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
237 ptr->hostname, ident, seq);
239 break;
240 }
242 if (ptr == NULL)
243 {
244 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
245 ident, seq);
246 }
248 return (ptr);
249 }
251 static int ping_receive_one (int fd, pinghost_t *ph)
252 {
253 char buffer[4096];
254 size_t buffer_len;
256 struct timeval now;
257 struct timeval diff;
259 pinghost_t *host = NULL;
261 struct sockaddr_storage sa;
262 socklen_t sa_len;
264 sa_len = sizeof (sa);
266 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
267 (struct sockaddr *) &sa, &sa_len);
268 if (buffer_len == -1)
269 {
270 dprintf ("recvfrom: %s\n", strerror (errno));
271 return (-1);
272 }
274 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
276 if (sa.ss_family == AF_INET)
277 {
278 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
279 return (-1);
280 }
281 else if (sa.ss_family == AF_INET6)
282 {
283 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
284 return (-1);
285 }
287 if (gettimeofday (&now, NULL) == -1)
288 {
289 dprintf ("gettimeofday: %s\n", strerror (errno));
290 timerclear (host->timer);
291 return (-1);
292 }
294 dprintf ("sent: %12i.%06i\n",
295 (int) host->timer->tv_sec,
296 (int) host->timer->tv_usec);
297 dprintf ("rcvd: %12i.%06i\n",
298 (int) now.tv_sec,
299 (int) now.tv_usec);
301 if (ping_timeval_sub (&now, host->timer, &diff) < 0)
302 {
303 timerclear (host->timer);
304 return (-1);
305 }
307 dprintf ("diff: %12i.%06i\n",
308 (int) diff.tv_sec,
309 (int) diff.tv_usec);
311 host->latency = ((double) diff.tv_usec) / 1000.0;
312 host->latency += ((double) diff.tv_sec) * 1000.0;
314 timerclear (host->timer);
316 return (0);
317 }
319 static int ping_receive_all (pinghost_t *ph)
320 {
321 fd_set readfds;
322 int num_readfds;
323 int max_readfds;
325 pinghost_t *ptr;
327 struct timeval endtime;
328 struct timeval nowtime;
329 struct timeval timeout;
330 int status;
332 int ret;
334 ret = 0;
336 if (gettimeofday (&endtime, NULL) == -1)
337 return (-1);
338 endtime.tv_sec += 1;
340 for (ptr = ph; ptr != NULL; ptr = ptr->next)
341 ptr->latency = -1.0;
343 while (1)
344 {
345 FD_ZERO (&readfds);
346 num_readfds = 0;
347 max_readfds = -1;
349 if (gettimeofday (&nowtime, NULL) == -1)
350 return (-1);
352 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
353 return (0);
355 for (ptr = ph; ptr != NULL; ptr = ptr->next)
356 {
357 if (!timerisset (ptr->timer))
358 continue;
360 FD_SET (ptr->fd, &readfds);
361 num_readfds++;
363 if (max_readfds < ptr->fd)
364 max_readfds = ptr->fd;
365 }
367 if (num_readfds == 0)
368 break;
370 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
371 (int) timeout.tv_sec,
372 (int) timeout.tv_usec);
374 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
376 if ((status == -1) && (errno == EINTR))
377 {
378 dprintf ("select was interrupted by signal..\n");
379 continue;
380 }
381 else if (status < 0)
382 {
383 dprintf ("select: %s\n", strerror (errno));
384 break;
385 }
386 else if (status == 0)
387 {
388 dprintf ("select timed out\n");
389 break;
390 }
392 for (ptr = ph; ptr != NULL; ptr = ptr->next)
393 {
394 if (FD_ISSET (ptr->fd, &readfds))
395 if (ping_receive_one (ptr->fd, ph) == 0)
396 ret++;
397 }
398 }
400 return (ret);
401 }
403 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
404 * Sending functions: *
405 * *
406 * ping_send_all *
407 * +-> ping_send_one_ipv4 *
408 * `-> ping_send_one_ipv6 *
409 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
410 static int ping_send_one_ipv4 (pinghost_t *ph)
411 {
412 struct icmphdr *icmp4;
413 int status;
415 char buf[4096];
416 int buflen;
418 char *data;
419 int datalen;
421 dprintf ("ph->hostname = %s\n", ph->hostname);
423 memset (buf, '\0', sizeof (buf));
424 icmp4 = (struct icmphdr *) buf;
425 data = (char *) (icmp4 + 1);
427 icmp4->type = ICMP_ECHO;
428 icmp4->code = 0;
429 /* The checksum will be calculated by the TCP/IP stack. */
430 icmp4->checksum = 0;
431 icmp4->un.echo.id = htons (ph->ident);
432 icmp4->un.echo.sequence = htons (ph->sequence);
434 strcpy (data, PING_DATA);
435 datalen = strlen (data);
437 buflen = datalen + sizeof (struct icmphdr);
439 icmp4->checksum = ping_icmp4_checksum (buf, buflen);
441 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
443 status = sendto (ph->fd, buf, buflen, 0,
444 (struct sockaddr *) ph->addr, ph->addrlen);
445 if (status < 0)
446 {
447 perror ("sendto");
448 return (-1);
449 }
451 dprintf ("sendto: status = %i\n", status);
453 return (0);
454 }
456 static int ping_send_one_ipv6 (pinghost_t *ph)
457 {
458 struct icmp6_hdr *icmp6;
459 int status;
461 char buf[4096];
462 int buflen;
464 char *data;
465 int datalen;
467 dprintf ("ph->hostname = %s\n", ph->hostname);
469 memset (buf, '\0', sizeof (buf));
470 icmp6 = (struct icmp6_hdr *) buf;
471 data = (char *) (icmp6 + 1);
473 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
474 icmp6->icmp6_code = 0;
475 /* The checksum will be calculated by the TCP/IP stack. */
476 icmp6->icmp6_cksum = 0;
477 icmp6->icmp6_id = htons (ph->ident);
478 icmp6->icmp6_seq = htons (ph->sequence);
480 strcpy (data, PING_DATA);
481 datalen = strlen (data);
483 buflen = datalen + sizeof (struct icmp6_hdr);
485 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
487 status = sendto (ph->fd, buf, buflen, 0,
488 (struct sockaddr *) ph->addr, ph->addrlen);
489 if (status < 0)
490 {
491 perror ("sendto");
492 return (-1);
493 }
495 dprintf ("sendto: status = %i\n", status);
497 return (0);
498 }
500 static int ping_send_all (pinghost_t *ph)
501 {
502 pinghost_t *ptr;
504 for (ptr = ph; ptr != NULL; ptr = ptr->next)
505 {
506 /* start timer.. The GNU `ping6' starts the timer before
507 * sending the packet, so I will do that too */
508 if (gettimeofday (ptr->timer, NULL) == -1)
509 {
510 dprintf ("gettimeofday: %s\n", strerror (errno));
511 timerclear (ptr->timer);
512 continue;
513 }
514 else
515 {
516 dprintf ("timer set for hostname = %s\n", ptr->hostname);
517 }
519 if (ptr->addrfamily == AF_INET6)
520 {
521 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
522 if (ping_send_one_ipv6 (ptr) != 0)
523 {
524 timerclear (ptr->timer);
525 continue;
526 }
527 }
528 else if (ptr->addrfamily == AF_INET)
529 {
530 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
531 if (ping_send_one_ipv4 (ptr) != 0)
532 {
533 timerclear (ptr->timer);
534 continue;
535 }
536 }
537 else /* this should not happen */
538 {
539 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
540 timerclear (ptr->timer);
541 continue;
542 }
544 ptr->sequence++;
545 }
547 /* FIXME */
548 return (0);
549 }
551 static int ping_get_ident (void)
552 {
553 int fd;
554 static int did_seed = 0;
556 int retval;
558 if (did_seed == 0)
559 {
560 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
561 {
562 unsigned int seed;
564 if (read (fd, &seed, sizeof (seed)) != -1)
565 {
566 did_seed = 1;
567 dprintf ("Random seed: %i\n", seed);
568 srandom (seed);
569 }
571 close (fd);
572 }
573 else
574 {
575 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
576 }
577 }
579 retval = (int) random ();
581 dprintf ("Random number: %i\n", retval);
583 return (retval);
584 }
586 static pinghost_t *ping_alloc (void)
587 {
588 pinghost_t *ph;
589 size_t ph_size;
591 ph_size = sizeof (pinghost_t)
592 + sizeof (struct sockaddr_storage)
593 + sizeof (struct timeval);
595 ph = (pinghost_t *) malloc (ph_size);
596 if (ph == NULL)
597 return (NULL);
599 memset (ph, '\0', ph_size);
601 ph->timer = (struct timeval *) (ph + 1);
602 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
604 ph->addrlen = sizeof (struct sockaddr_storage);
605 ph->latency = -1.0;
606 ph->ident = ping_get_ident () & 0xFFFF;
608 return (ph);
609 }
611 static void ping_free (pinghost_t *ph)
612 {
613 if (ph->hostname != NULL)
614 free (ph->hostname);
616 free (ph);
617 }
619 /*
620 * public methods
621 */
622 pingobj_t *ping_construct (void)
623 {
624 pingobj_t *obj;
626 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
627 return (NULL);
629 obj->head = NULL;
631 return (obj);
632 }
634 void ping_destroy (pingobj_t *obj)
635 {
636 pinghost_t *current;
637 pinghost_t *next;
639 current = obj->head;
640 next = NULL;
642 while (current != NULL)
643 {
644 next = current->next;
645 ping_free (current);
646 current = next;
647 }
649 free (obj);
651 return;
652 }
654 int ping_setopt (pingobj_t *obj, int option, void *value)
655 {
656 int ret = 0;
658 switch (option)
659 {
660 case PING_OPT_TIMEOUT:
661 obj->timeout = *((double *) value);
662 if (obj->timeout < 0.0)
663 {
664 obj->timeout = PING_DEF_TIMEOUT;
665 ret = -1;
666 }
667 break;
669 case PING_OPT_TTL:
670 obj->ttl = *((int *) value);
671 if ((obj->ttl < 1) || (obj->ttl > 255))
672 {
673 obj->ttl = PING_DEF_TTL;
674 ret = -1;
675 }
676 break;
678 case PING_OPT_AF:
679 obj->addrfamily = *((int *) value);
680 if ((obj->addrfamily != AF_UNSPEC)
681 && (obj->addrfamily != AF_INET)
682 && (obj->addrfamily != AF_INET6))
683 {
684 obj->addrfamily = PING_DEF_AF;
685 ret = -1;
686 }
687 break;
689 default:
690 ret = -2;
691 } /* switch (option) */
693 return (ret);
694 } /* int ping_setopt */
697 int ping_send (pingobj_t *obj)
698 {
699 int ret;
701 if (ping_send_all (obj->head) < 0)
702 return (-1);
704 if ((ret = ping_receive_all (obj->head)) < 0)
705 return (-2);
707 return (ret);
708 }
710 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
711 {
712 while (ph != NULL)
713 {
714 if (strcasecmp (ph->hostname, host) == 0)
715 break;
717 ph = ph->next;
718 }
720 return (ph);
721 }
723 int ping_host_add (pingobj_t *obj, const char *host)
724 {
725 pinghost_t *ph;
727 struct sockaddr_storage sockaddr;
728 socklen_t sockaddr_len;
730 struct addrinfo ai_hints;
731 struct addrinfo *ai_list, *ai_ptr;
732 int ai_return;
734 dprintf ("host = %s\n", host);
736 if (ping_host_search (obj->head, host) != NULL)
737 return (0);
739 memset (&ai_hints, '\0', sizeof (ai_hints));
740 ai_hints.ai_flags = 0;
741 #ifdef AI_ADDRCONFIG
742 ai_hints.ai_flags |= AI_ADDRCONFIG;
743 #endif
744 ai_hints.ai_family = PF_UNSPEC;
745 ai_hints.ai_socktype = SOCK_RAW;
747 if ((ph = ping_alloc ()) == NULL)
748 {
749 dprintf ("Out of memory!\n");
750 return (-1);
751 }
753 if ((ph->hostname = strdup (host)) == NULL)
754 {
755 dprintf ("Out of memory!\n");
756 ping_free (ph);
757 return (-1);
758 }
760 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
761 {
762 dprintf ("getaddrinfo failed\n");
763 ping_free (ph);
764 return (-1);
765 }
767 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
768 {
769 ph->fd = -1;
771 sockaddr_len = sizeof (sockaddr);
772 memset (&sockaddr, '\0', sockaddr_len);
774 if (ai_ptr->ai_family == AF_INET)
775 {
776 struct sockaddr_in *si;
778 si = (struct sockaddr_in *) &sockaddr;
779 si->sin_family = AF_INET;
780 si->sin_port = htons (ph->ident);
781 si->sin_addr.s_addr = htonl (INADDR_ANY);
783 ai_ptr->ai_protocol = IPPROTO_ICMP;
784 }
785 else if (ai_ptr->ai_family == AF_INET6)
786 {
787 struct sockaddr_in6 *si;
789 si = (struct sockaddr_in6 *) &sockaddr;
790 si->sin6_family = AF_INET6;
791 si->sin6_port = htons (ph->ident);
792 si->sin6_addr = in6addr_any;
794 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
795 }
796 else
797 {
798 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
799 continue;
800 }
802 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
803 if (ph->fd == -1)
804 {
805 dprintf ("socket: %s\n", strerror (errno));
806 continue;
807 }
809 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
810 {
811 dprintf ("bind: %s\n", strerror (errno));
812 close (ph->fd);
813 ph->fd = -1;
814 continue;
815 }
817 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
818 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
819 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
820 ph->addrlen = ai_ptr->ai_addrlen;
821 ph->addrfamily = ai_ptr->ai_family;
823 break;
824 }
826 freeaddrinfo (ai_list);
828 if (ph->fd < 0)
829 {
830 free (ph->hostname);
831 free (ph);
832 return (-1);
833 }
835 ph->next = obj->head;
836 obj->head = ph;
838 return (0);
839 }
841 int ping_host_remove (pingobj_t *obj, const char *host)
842 {
843 pinghost_t *pre, *cur;
845 pre = NULL;
846 cur = obj->head;
848 while (cur != NULL)
849 {
850 if (strcasecmp (host, cur->hostname))
851 break;
853 pre = cur;
854 cur = cur->next;
855 }
857 if (cur == NULL)
858 return (-1);
860 if (pre == NULL)
861 obj->head = cur->next;
862 else
863 pre->next = cur->next;
865 if (cur->fd >= 0)
866 close (cur->fd);
868 ping_free (cur);
870 return (0);
871 }
873 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
874 {
875 return ((pingobj_iter_t *) obj->head);
876 }
878 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
879 {
880 return ((pingobj_iter_t *) iter->next);
881 }
883 const char *ping_iterator_get_host (pingobj_iter_t *iter)
884 {
885 return (iter->hostname);
886 }
888 double ping_iterator_get_latency (pingobj_iter_t *iter)
889 {
890 return (iter->latency);
891 }