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(format, ...) /**/
46 #endif
48 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
50 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
51 struct timeval *res)
52 {
54 if ((tv1->tv_sec < tv2->tv_sec)
55 || ((tv1->tv_sec == tv2->tv_sec)
56 && (tv1->tv_usec < tv2->tv_usec)))
57 return (-1);
59 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
60 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
62 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
64 while (res->tv_usec < 0)
65 {
66 res->tv_usec += 1000000;
67 res->tv_sec--;
68 }
70 return (0);
71 }
73 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
74 {
75 uint32_t sum = 0;
76 uint16_t ret = 0;
78 uint16_t *ptr;
80 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
81 sum += *ptr;
83 if (len == 1)
84 {
85 *(char *) &ret = *(char *) ptr;
86 sum += ret;
87 }
89 /* Do this twice to get all possible carries.. */
90 sum = (sum >> 16) + (sum & 0xFFFF);
91 sum = (sum >> 16) + (sum & 0xFFFF);
93 ret = ~sum;
95 return (ret);
96 }
98 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
99 {
100 struct ip *ip_hdr;
101 struct icmphdr *icmp_hdr;
103 size_t ip_hdr_len;
105 uint16_t recv_checksum;
106 uint16_t calc_checksum;
108 uint16_t ident;
109 uint16_t seq;
111 pinghost_t *ptr;
113 if (buffer_len < sizeof (struct ip))
114 return (NULL);
116 ip_hdr = (struct ip *) buffer;
117 ip_hdr_len = ip_hdr->ip_hl << 2;
119 if (buffer_len < ip_hdr_len)
120 return (NULL);
122 buffer += ip_hdr_len;
123 buffer_len -= ip_hdr_len;
125 if (buffer_len < sizeof (struct icmphdr))
126 return (NULL);
128 icmp_hdr = (struct icmphdr *) buffer;
129 buffer += sizeof (struct icmphdr);
130 buffer_len -= sizeof (struct icmphdr);
132 if (icmp_hdr->type != ICMP_ECHOREPLY)
133 {
134 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->type);
135 return (NULL);
136 }
138 recv_checksum = icmp_hdr->checksum;
139 icmp_hdr->checksum = 0;
140 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
141 sizeof (struct icmphdr) + buffer_len);
143 if (recv_checksum != calc_checksum)
144 {
145 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
146 recv_checksum, calc_checksum);
147 return (NULL);
148 }
150 ident = ntohs (icmp_hdr->un.echo.id);
151 seq = ntohs (icmp_hdr->un.echo.sequence);
153 for (ptr = ph; ptr != NULL; ptr = ptr->next)
154 {
155 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
156 ptr->hostname, ptr->ident, ptr->sequence - 1);
158 if (ptr->addrfamily != AF_INET)
159 continue;
161 if (!timerisset (ptr->timer))
162 continue;
164 if (ptr->ident != ident)
165 continue;
167 if ((ptr->sequence - 1) != seq)
168 continue;
170 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
171 ptr->hostname, ident, seq);
173 break;
174 }
176 if (ptr == NULL)
177 {
178 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
179 ident, seq);
180 }
182 return (ptr);
183 }
185 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
186 {
187 struct icmp6_hdr *icmp_hdr;
189 uint16_t ident;
190 uint16_t seq;
192 pinghost_t *ptr;
194 if (buffer_len < sizeof (struct icmp6_hdr))
195 return (NULL);
197 icmp_hdr = (struct icmp6_hdr *) buffer;
198 buffer += sizeof (struct icmphdr);
199 buffer_len -= sizeof (struct icmphdr);
201 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
202 {
203 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
204 return (NULL);
205 }
207 if (icmp_hdr->icmp6_code != 0)
208 {
209 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
210 return (NULL);
211 }
213 ident = ntohs (icmp_hdr->icmp6_id);
214 seq = ntohs (icmp_hdr->icmp6_seq);
216 for (ptr = ph; ptr != NULL; ptr = ptr->next)
217 {
218 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
219 ptr->hostname, ptr->ident, ptr->sequence - 1);
221 if (ptr->addrfamily != AF_INET6)
222 continue;
224 if (!timerisset (ptr->timer))
225 continue;
227 if (ptr->ident != ident)
228 continue;
230 if ((ptr->sequence - 1) != seq)
231 continue;
233 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
234 ptr->hostname, ident, seq);
236 break;
237 }
239 if (ptr == NULL)
240 {
241 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
242 ident, seq);
243 }
245 return (ptr);
246 }
248 static void ping_receive_one (int fd, pinghost_t *ph)
249 {
250 char buffer[4096];
251 size_t buffer_len;
253 struct timeval now;
254 struct timeval diff;
256 pinghost_t *host = NULL;
258 struct sockaddr_storage sa;
259 socklen_t sa_len;
261 sa_len = sizeof (sa);
263 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
264 (struct sockaddr *) &sa, &sa_len);
265 if (buffer_len == -1)
266 {
267 dprintf ("recvfrom: %s\n", strerror (errno));
268 return;
269 }
271 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
273 if (sa.ss_family == AF_INET)
274 {
275 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
276 return;
277 }
278 else if (sa.ss_family == AF_INET6)
279 {
280 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
281 return;
282 }
284 if (gettimeofday (&now, NULL) == -1)
285 {
286 dprintf ("gettimeofday: %s\n", strerror (errno));
287 timerclear (host->timer);
288 return;
289 }
291 dprintf ("sent: %12i.%06i\n",
292 (int) host->timer->tv_sec,
293 (int) host->timer->tv_usec);
294 dprintf ("rcvd: %12i.%06i\n",
295 (int) now.tv_sec,
296 (int) now.tv_usec);
298 if (ping_timeval_sub (&now, host->timer, &diff) < 0)
299 {
300 timerclear (host->timer);
301 return;
302 }
304 dprintf ("diff: %12i.%06i\n",
305 (int) diff.tv_sec,
306 (int) diff.tv_usec);
308 host->latency = ((double) diff.tv_usec) / 1000.0;
309 host->latency += ((double) diff.tv_sec) * 1000.0;
311 timerclear (host->timer);
312 }
314 static int ping_receive_all (pinghost_t *ph)
315 {
316 fd_set readfds;
317 int num_readfds;
318 int max_readfds;
320 pinghost_t *ptr;
322 struct timeval endtime;
323 struct timeval nowtime;
324 struct timeval timeout;
325 int status;
327 if (gettimeofday (&endtime, NULL) == -1)
328 return (-1);
329 endtime.tv_sec += 1;
331 for (ptr = ph; ptr != NULL; ptr = ptr->next)
332 ptr->latency = -1.0;
334 while (1)
335 {
336 FD_ZERO (&readfds);
337 num_readfds = 0;
338 max_readfds = -1;
340 if (gettimeofday (&nowtime, NULL) == -1)
341 return (-1);
343 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
344 return (0);
346 for (ptr = ph; ptr != NULL; ptr = ptr->next)
347 {
348 if (!timerisset (ptr->timer))
349 continue;
351 FD_SET (ptr->fd, &readfds);
352 num_readfds++;
354 if (max_readfds < ptr->fd)
355 max_readfds = ptr->fd;
356 }
358 if (num_readfds == 0)
359 break;
361 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
362 (int) timeout.tv_sec,
363 (int) timeout.tv_usec);
365 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
367 if ((status == -1) && (errno == EINTR))
368 {
369 dprintf ("select was interrupted by signal..\n");
370 continue;
371 }
372 else if (status < 0)
373 {
374 dprintf ("select: %s\n", strerror (errno));
375 break;
376 }
377 else if (status == 0)
378 {
379 dprintf ("select timed out\n");
380 break;
381 }
383 for (ptr = ph; ptr != NULL; ptr = ptr->next)
384 {
385 if (FD_ISSET (ptr->fd, &readfds))
386 ping_receive_one (ptr->fd, ph);
387 }
388 }
390 /* FIXME - return correct status */
391 return (0);
392 }
394 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
395 * Sending functions: *
396 * *
397 * ping_send_all *
398 * +-> ping_send_one_ipv4 *
399 * `-> ping_send_one_ipv6 *
400 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
401 static int ping_send_one_ipv4 (pinghost_t *ph)
402 {
403 struct icmphdr *icmp4;
404 int status;
406 char buf[4096];
407 int buflen;
409 char *data;
410 int datalen;
412 dprintf ("ph->hostname = %s\n", ph->hostname);
414 memset (buf, '\0', sizeof (buf));
415 icmp4 = (struct icmphdr *) buf;
416 data = (char *) (icmp4 + 1);
418 icmp4->type = ICMP_ECHO;
419 icmp4->code = 0;
420 /* The checksum will be calculated by the TCP/IP stack. */
421 icmp4->checksum = 0;
422 icmp4->un.echo.id = htons (ph->ident);
423 icmp4->un.echo.sequence = htons (ph->sequence);
425 strcpy (data, PING_DATA);
426 datalen = strlen (data);
428 buflen = datalen + sizeof (struct icmphdr);
430 icmp4->checksum = ping_icmp4_checksum (buf, buflen);
432 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
434 status = sendto (ph->fd, buf, buflen, 0,
435 (struct sockaddr *) ph->addr, ph->addrlen);
436 if (status < 0)
437 {
438 perror ("sendto");
439 return (-1);
440 }
442 dprintf ("sendto: status = %i\n", status);
444 return (0);
445 }
447 static int ping_send_one_ipv6 (pinghost_t *ph)
448 {
449 struct icmp6_hdr *icmp6;
450 int status;
452 char buf[4096];
453 int buflen;
455 char *data;
456 int datalen;
458 dprintf ("ph->hostname = %s\n", ph->hostname);
460 memset (buf, '\0', sizeof (buf));
461 icmp6 = (struct icmp6_hdr *) buf;
462 data = (char *) (icmp6 + 1);
464 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
465 icmp6->icmp6_code = 0;
466 /* The checksum will be calculated by the TCP/IP stack. */
467 icmp6->icmp6_cksum = 0;
468 icmp6->icmp6_id = htons (ph->ident);
469 icmp6->icmp6_seq = htons (ph->sequence);
471 strcpy (data, PING_DATA);
472 datalen = strlen (data);
474 buflen = datalen + sizeof (struct icmp6_hdr);
476 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
478 status = sendto (ph->fd, buf, buflen, 0,
479 (struct sockaddr *) ph->addr, ph->addrlen);
480 if (status < 0)
481 {
482 perror ("sendto");
483 return (-1);
484 }
486 dprintf ("sendto: status = %i\n", status);
488 return (0);
489 }
491 static int ping_send_all (pinghost_t *ph)
492 {
493 pinghost_t *ptr;
495 for (ptr = ph; ptr != NULL; ptr = ptr->next)
496 {
497 /* start timer.. The GNU `ping6' starts the timer before
498 * sending the packet, so I will do that too */
499 if (gettimeofday (ptr->timer, NULL) == -1)
500 {
501 dprintf ("gettimeofday: %s\n", strerror (errno));
502 timerclear (ptr->timer);
503 continue;
504 }
505 else
506 {
507 dprintf ("timer set for hostname = %s\n", ptr->hostname);
508 }
510 if (ptr->addrfamily == AF_INET6)
511 {
512 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
513 if (ping_send_one_ipv6 (ptr) != 0)
514 {
515 timerclear (ptr->timer);
516 continue;
517 }
518 }
519 else if (ptr->addrfamily == AF_INET)
520 {
521 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
522 if (ping_send_one_ipv4 (ptr) != 0)
523 {
524 timerclear (ptr->timer);
525 continue;
526 }
527 }
528 else /* this should not happen */
529 {
530 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
531 timerclear (ptr->timer);
532 continue;
533 }
535 ptr->sequence++;
536 }
538 /* FIXME */
539 return (0);
540 }
542 static int ping_get_ident (void)
543 {
544 int fd;
545 static int did_seed = 0;
547 int retval;
549 if (did_seed == 0)
550 {
551 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
552 {
553 unsigned int seed;
555 if (read (fd, &seed, sizeof (seed)) != -1)
556 {
557 did_seed = 1;
558 dprintf ("Random seed: %i\n", seed);
559 srandom (seed);
560 }
562 close (fd);
563 }
564 else
565 {
566 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
567 }
568 }
570 retval = (int) random ();
572 dprintf ("Random number: %i\n", retval);
574 return (retval);
575 }
577 static pinghost_t *ping_alloc (void)
578 {
579 pinghost_t *ph;
580 size_t ph_size;
582 ph_size = sizeof (pinghost_t)
583 + sizeof (struct sockaddr_storage)
584 + sizeof (struct timeval);
586 ph = (pinghost_t *) malloc (ph_size);
587 if (ph == NULL)
588 return (NULL);
590 memset (ph, '\0', ph_size);
592 ph->timer = (struct timeval *) (ph + 1);
593 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
595 ph->addrlen = sizeof (struct sockaddr_storage);
596 ph->latency = -1.0;
597 ph->ident = ping_get_ident () & 0xFFFF;
599 return (ph);
600 }
602 static void ping_free (pinghost_t *ph)
603 {
604 if (ph->hostname != NULL)
605 free (ph->hostname);
607 free (ph);
608 }
610 /*
611 * public methods
612 */
613 pingobj_t *ping_construct (int flags)
614 {
615 pingobj_t *obj;
617 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
618 return (NULL);
620 obj->flags = flags;
621 obj->head = NULL;
623 return (obj);
624 }
626 void ping_destroy (pingobj_t *obj)
627 {
628 pinghost_t *current;
629 pinghost_t *next;
631 current = obj->head;
632 next = NULL;
634 while (current != NULL)
635 {
636 next = current->next;
637 ping_free (current);
638 current = next;
639 }
641 free (obj);
643 return;
644 }
646 int ping_send (pingobj_t *obj)
647 {
648 if (ping_send_all (obj->head) < 0)
649 return (-1);
651 if (ping_receive_all (obj->head) < 0)
652 return (-2);
654 return (0);
655 }
657 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
658 {
659 while (ph != NULL)
660 {
661 if (strcasecmp (ph->hostname, host) == 0)
662 break;
664 ph = ph->next;
665 }
667 return (ph);
668 }
670 int ping_host_add (pingobj_t *obj, const char *host)
671 {
672 pinghost_t *ph;
674 struct sockaddr_storage sockaddr;
675 socklen_t sockaddr_len;
677 struct addrinfo ai_hints;
678 struct addrinfo *ai_list, *ai_ptr;
679 int ai_return;
681 dprintf ("host = %s\n", host);
683 if (ping_host_search (obj->head, host) != NULL)
684 return (0);
686 memset (&ai_hints, '\0', sizeof (ai_hints));
687 ai_hints.ai_flags = AI_ADDRCONFIG;
688 ai_hints.ai_family = PF_UNSPEC;
689 ai_hints.ai_socktype = SOCK_RAW;
691 if ((ph = ping_alloc ()) == NULL)
692 {
693 dprintf ("Out of memory!\n");
694 return (-1);
695 }
697 if ((ph->hostname = strdup (host)) == NULL)
698 {
699 dprintf ("Out of memory!\n");
700 ping_free (ph);
701 return (-1);
702 }
704 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
705 {
706 dprintf ("getaddrinfo failed\n");
707 ping_free (ph);
708 return (-1);
709 }
711 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
712 {
713 ph->fd = -1;
715 sockaddr_len = sizeof (sockaddr);
716 memset (&sockaddr, '\0', sockaddr_len);
718 if (ai_ptr->ai_family == AF_INET)
719 {
720 struct sockaddr_in *si;
722 si = (struct sockaddr_in *) &sockaddr;
723 si->sin_family = AF_INET;
724 si->sin_port = htons (ph->ident);
725 si->sin_addr.s_addr = htonl (INADDR_ANY);
727 ai_ptr->ai_protocol = IPPROTO_ICMP;
728 }
729 else if (ai_ptr->ai_family == AF_INET6)
730 {
731 struct sockaddr_in6 *si;
733 si = (struct sockaddr_in6 *) &sockaddr;
734 si->sin6_family = AF_INET6;
735 si->sin6_port = htons (ph->ident);
736 si->sin6_addr = in6addr_any;
738 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
739 }
740 else
741 {
742 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
743 continue;
744 }
746 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
747 if (ph->fd == -1)
748 {
749 dprintf ("socket: %s\n", strerror (errno));
750 continue;
751 }
753 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
754 {
755 dprintf ("bind: %s\n", strerror (errno));
756 close (ph->fd);
757 ph->fd = -1;
758 continue;
759 }
761 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
762 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
763 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
764 ph->addrlen = ai_ptr->ai_addrlen;
765 ph->addrfamily = ai_ptr->ai_family;
767 break;
768 }
770 freeaddrinfo (ai_list);
772 if (ph->fd < 0)
773 {
774 free (ph->hostname);
775 free (ph);
776 return (-1);
777 }
779 ph->next = obj->head;
780 obj->head = ph;
782 return (0);
783 }
785 int ping_host_remove (pingobj_t *obj, const char *host)
786 {
787 pinghost_t *pre, *cur;
789 pre = NULL;
790 cur = obj->head;
792 while (cur != NULL)
793 {
794 if (strcasecmp (host, cur->hostname))
795 break;
797 pre = cur;
798 cur = cur->next;
799 }
801 if (cur == NULL)
802 return (-1);
804 if (pre == NULL)
805 obj->head = cur->next;
806 else
807 pre->next = cur->next;
809 if (cur->fd >= 0)
810 close (cur->fd);
812 ping_free (cur);
814 return (0);
815 }
817 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
818 {
819 return ((pingobj_iter_t *) obj->head);
820 }
822 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
823 {
824 return ((pingobj_iter_t *) iter->next);
825 }
827 const char *ping_iterator_get_host (pingobj_iter_t *iter)
828 {
829 return (iter->hostname);
830 }
832 double ping_iterator_get_latency (pingobj_iter_t *iter)
833 {
834 return (iter->latency);
835 }