src/liboping.c: Replace sizeof(struct icmphdr) with ICMP_MINLEN.
[liboping.git] / src / liboping.c
index 4616843..f3c850e 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * Object oriented C module to send ICMP and ICMPv6 `echo's.
- * Copyright (C) 2006-2010  Florian octo Forster <octo at verplant.org>
+ * Copyright (C) 2006-2016  Florian octo Forster <ff at octo.it>
  *
  * This library is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published by the
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#ifdef __APPLE__
+#define __APPLE_USE_RFC_3542
+#endif
+
 #if HAVE_CONFIG_H
 # include <config.h>
 #endif
@@ -138,6 +142,9 @@ struct pingobj
 
        char                    *device;
 
+       char                    set_mark;
+       int                     mark;
+
        char                     errmsg[PING_ERRMSG_LEN];
 
        pinghost_t              *head;
@@ -291,12 +298,12 @@ static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
        buffer     += ip_hdr_len;
        buffer_len -= ip_hdr_len;
 
-       if (buffer_len < sizeof (struct icmp))
+       if (buffer_len < ICMP_MINLEN)
                return (NULL);
 
        icmp_hdr = (struct icmp *) buffer;
-       buffer     += sizeof (struct icmp);
-       buffer_len -= sizeof (struct icmp);
+       buffer     += ICMP_MINLEN;
+       buffer_len -= ICMP_MINLEN;
 
        if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
        {
@@ -307,7 +314,7 @@ static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
        recv_checksum = icmp_hdr->icmp_cksum;
        icmp_hdr->icmp_cksum = 0;
        calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
-                       sizeof (struct icmp) + buffer_len);
+                       ICMP_MINLEN + buffer_len);
 
        if (recv_checksum != calc_checksum)
        {
@@ -385,12 +392,12 @@ static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
 
        pinghost_t *ptr;
 
-       if (buffer_len < sizeof (struct icmp6_hdr))
+       if (buffer_len < ICMP_MINLEN)
                return (NULL);
 
        icmp_hdr = (struct icmp6_hdr *) buffer;
-       buffer     += sizeof (struct icmp);
-       buffer_len -= sizeof (struct icmp);
+       buffer     += ICMP_MINLEN;
+       buffer_len -= ICMP_MINLEN;
 
        if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
        {
@@ -450,7 +457,7 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
         * reply. The right object will be returned by ping_receive_ipv*(). For
         * now, we can only rely on ph->fd and ph->addrfamily. */
 
-       struct timeval diff;
+       struct timeval diff, pkt_now = *now;
        pinghost_t *host = NULL;
        int recv_ttl;
        uint8_t recv_qos;
@@ -504,7 +511,14 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
                        cmsg != NULL;
                        cmsg = CMSG_NXTHDR (&msghdr, cmsg))
        {
-               if (ph->addrfamily == AF_INET) /* {{{ */
+               if (cmsg->cmsg_level == SOL_SOCKET)
+               {
+#ifdef SO_TIMESTAMP
+                       if (cmsg->cmsg_type == SO_TIMESTAMP)
+                               memcpy (&pkt_now, CMSG_DATA (cmsg), sizeof (pkt_now));
+#endif /* SO_TIMESTAMP */
+               }
+               else if (ph->addrfamily == AF_INET) /* {{{ */
                {
                        if (cmsg->cmsg_level != IPPROTO_IP)
                                continue;
@@ -538,6 +552,7 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
                                                sizeof (recv_qos));
                                dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
                        } else
+#ifdef IPV6_HOPLIMIT
                        if (cmsg->cmsg_type == IPV6_HOPLIMIT)
                        {
                                memcpy (&recv_ttl, CMSG_DATA (cmsg),
@@ -545,6 +560,25 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
                                dprintf ("TTLv6 = %i;\n", recv_ttl);
                        }
                        else
+#endif
+#ifdef IPV6_UNICAST_HOPS
+                       if (cmsg->cmsg_type == IPV6_UNICAST_HOPS)
+                       {
+                               memcpy (&recv_ttl, CMSG_DATA (cmsg),
+                                               sizeof (recv_ttl));
+                               dprintf ("TTLv6 = %i;\n", recv_ttl);
+                       }
+                       else
+#endif
+#ifdef IPV6_MULTICAST_HOPS
+                       if (cmsg->cmsg_type == IPV6_MULTICAST_HOPS)
+                       {
+                               memcpy (&recv_ttl, CMSG_DATA (cmsg),
+                                               sizeof (recv_ttl));
+                               dprintf ("TTLv6 = %i;\n", recv_ttl);
+                       }
+                       else
+#endif
                        {
                                dprintf ("Not handling option %i.\n",
                                                cmsg->cmsg_type);
@@ -578,13 +612,13 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
        }
 
        dprintf ("rcvd: %12i.%06i\n",
-                       (int) now->tv_sec,
-                       (int) now->tv_usec);
+                       (int) pkt_now.tv_sec,
+                       (int) pkt_now.tv_usec);
        dprintf ("sent: %12i.%06i\n",
                        (int) host->timer->tv_sec,
                        (int) host->timer->tv_usec);
 
-       if (ping_timeval_sub (now, host->timer, &diff) < 0)
+       if (ping_timeval_sub (&pkt_now, host->timer, &diff) < 0)
        {
                timerclear (host->timer);
                return (-1);
@@ -606,6 +640,9 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
        return (0);
 }
 
+/* Blocks until a packet was received from all hosts or the timeout is reached.
+ * When interrupted, (-EINTR) is returned. On error, -1 is returned. On
+ * success, returns zero. */
 static int ping_receive_all (pingobj_t *obj)
 {
        fd_set read_fds;
@@ -695,7 +732,8 @@ static int ping_receive_all (pingobj_t *obj)
                if ((status == -1) && (errno == EINTR))
                {
                        dprintf ("select was interrupted by signal..\n");
-                       continue;
+                       ping_set_errno (obj, EINTR);
+                       return (-EINTR);
                }
                else if (status < 0)
                {
@@ -733,7 +771,7 @@ static int ping_receive_all (pingobj_t *obj)
        } /* while (1) */
        
        return (ret);
-}
+} /* int ping_receive_all */
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * Sending functions:                                                        *
@@ -787,7 +825,7 @@ static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
 
        memset (buf, '\0', sizeof (buf));
        icmp4 = (struct icmp *) buf;
-       data  = (char *) (icmp4 + 1);
+       data  = buf + ICMP_MINLEN;
 
        icmp4->icmp_type  = ICMP_ECHO;
        icmp4->icmp_code  = 0;
@@ -795,11 +833,11 @@ static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
        icmp4->icmp_id    = htons (ph->ident);
        icmp4->icmp_seq   = htons (ph->sequence);
 
-       buflen = 4096 - sizeof (struct icmp);
+       buflen = sizeof(buf) - ICMP_MINLEN;
        strncpy (data, ph->data, buflen);
        datalen = strlen (data);
 
-       buflen = datalen + sizeof (struct icmp);
+       buflen = datalen + ICMP_MINLEN;
 
        icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
 
@@ -842,11 +880,11 @@ static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
        icmp6->icmp6_id    = htons (ph->ident);
        icmp6->icmp6_seq   = htons (ph->sequence);
 
-       buflen = 4096 - sizeof (struct icmp6_hdr);
+       buflen = 4096 - ICMP_MINLEN;
        strncpy (data, ph->data, buflen);
        datalen = strlen (data);
 
-       buflen = datalen + sizeof (struct icmp6_hdr);
+       buflen = datalen + ICMP_MINLEN;
 
        dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
 
@@ -1287,6 +1325,19 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
                } /* case PING_OPT_DEVICE */
                break;
 
+               case PING_OPT_MARK:
+               {
+#ifdef SO_MARK
+                       obj->mark     = *(int*)(value);
+                       obj->set_mark = 1;
+#else /* SO_MARK */
+                       ping_set_errno (obj, ENOTSUP);
+                       ret = -1;
+#endif /* !SO_MARK */
+                       
+               } /* case PING_OPT_MARK */
+               break;
+
                default:
                        ret = -2;
        } /* switch (option) */
@@ -1297,18 +1348,13 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
 
 int ping_send (pingobj_t *obj)
 {
-       int ret;
-
        if (obj == NULL)
                return (-1);
 
        if (ping_send_all (obj) < 0)
                return (-1);
 
-       if ((ret = ping_receive_all (obj)) < 0)
-               return (-2);
-
-       return (ret);
+       return (ping_receive_all (obj));
 }
 
 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
@@ -1422,7 +1468,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
                        errmsg[PING_ERRMSG_LEN - 1] = '\0';
 
-                       dprintf (errmsg);
+                       dprintf ("%s", errmsg);
                        ping_set_error (obj, "getaddrinfo", errmsg);
                        continue;
                }
@@ -1468,7 +1514,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        {
 #if WITH_DEBUG
                                char errbuf[PING_ERRMSG_LEN];
-                               dprintf ("setsockopt: %s\n",
+                               dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
                                                sstrerror (errno, errbuf, sizeof (errbuf)));
 #endif
                                ping_set_errno (obj, errno);
@@ -1478,7 +1524,46 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        }
                }
 #endif /* SO_BINDTODEVICE */
+#ifdef SO_MARK
+               if(obj->set_mark)
+               {
+                       if(setsockopt(ph->fd, SOL_SOCKET, SO_MARK, &(obj->mark), sizeof(obj->mark)) != 0)
+                       {
+#if WITH_DEBUG
+                               char errbuf[PING_ERRMSG_LEN];
+                               dprintf ("setsockopt (SO_MARK): %s\n",
+                                               sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+                               ping_set_errno (obj, errno);
+                               close (ph->fd);
+                               ph->fd = -1;
+                               continue;
+                       }
+               }
+#endif
+#ifdef SO_TIMESTAMP
+               if (1) /* {{{ */
+               {
+                       int status;
+                       int opt = 1;
 
+                       status = setsockopt (ph->fd,
+                                       SOL_SOCKET, SO_TIMESTAMP,
+                                       &opt, sizeof (opt));
+                       if (status != 0)
+                       {
+#if WITH_DEBUG
+                               char errbuf[PING_ERRMSG_LEN];
+                               dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
+                                               sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+                               ping_set_errno (obj, errno);
+                               close (ph->fd);
+                               ph->fd = -1;
+                               continue;
+                       }
+               } /* }}} if (1) */
+#endif /* SO_TIMESTAMP */
                assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
                memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
                memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
@@ -1511,10 +1596,12 @@ int ping_host_add (pingobj_t *obj, const char *host)
                {
                        int opt;
 
+#ifdef IP_RECVTOS
                        /* Enable receiving the TOS field */
                        opt = 1;
                        setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
                                        &opt, sizeof (opt));
+#endif /* IP_RECVTOS */
 
                        /* Enable receiving the TTL field */
                        opt = 1;