Code

svn merge -r773:775 branches/collectd-3.9 branches/collectd-3.8
[collectd.git] / src / liboping / liboping.c
index 915de7bdcde357d2608caa55a46f3d5532ac0654..46db24471be5dd8d84e93da91018d2b1033e47c1 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#if STDC_HEADERS
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <errno.h>
+# include <assert.h>
+#else
+# error "You don't have the standard C99 header files installed"
+#endif /* STDC_HEADERS */
+
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
 
-#include <assert.h>
+#if HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
+#if HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+#if HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#endif
 
-#include <sys/socket.h>
-#include <netdb.h>
-#include <netinet/ip_icmp.h>
-#include <netinet/icmp6.h>
+#if HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#if HAVE_NETDB_H
+# include <netdb.h>
+#endif
 
-#include <sys/time.h>
-#include <time.h>
+#if HAVE_NETINET_IN_SYSTM_H
+# include <netinet/in_systm.h>
+#endif
+#if HAVE_NETINET_IN_H
+# include <netinet/in.h>
+#endif
+#if HAVE_NETINET_IP_H
+# include <netinet/ip.h>
+#endif
+#if HAVE_NETINET_IP_ICMP_H
+# include <netinet/ip_icmp.h>
+#endif
+#ifdef HAVE_NETINET_IP_VAR_H
+# include <netinet/ip_var.h>
+#endif
+#if HAVE_NETINET_IP6_H
+# include <netinet/ip6.h>
+#endif
+#if HAVE_NETINET_ICMP6_H
+# include <netinet/icmp6.h>
+#endif
 
 #include "liboping.h"
 
@@ -101,7 +143,7 @@ static uint16_t ping_icmp4_checksum (char *buf, size_t len)
 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
 {
        struct ip *ip_hdr;
-       struct icmphdr *icmp_hdr;
+       struct icmp *icmp_hdr;
 
        size_t ip_hdr_len;
 
@@ -125,23 +167,23 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
        buffer     += ip_hdr_len;
        buffer_len -= ip_hdr_len;
 
-       if (buffer_len < sizeof (struct icmphdr))
+       if (buffer_len < sizeof (struct icmp))
                return (NULL);
 
-       icmp_hdr = (struct icmphdr *) buffer;
-       buffer     += sizeof (struct icmphdr);
-       buffer_len -= sizeof (struct icmphdr);
+       icmp_hdr = (struct icmp *) buffer;
+       buffer     += sizeof (struct icmp);
+       buffer_len -= sizeof (struct icmp);
 
-       if (icmp_hdr->type != ICMP_ECHOREPLY)
+       if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
        {
-               dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->type);
+               dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
                return (NULL);
        }
 
-       recv_checksum = icmp_hdr->checksum;
-       icmp_hdr->checksum = 0;
+       recv_checksum = icmp_hdr->icmp_cksum;
+       icmp_hdr->icmp_cksum = 0;
        calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
-                       sizeof (struct icmphdr) + buffer_len);
+                       sizeof (struct icmp) + buffer_len);
 
        if (recv_checksum != calc_checksum)
        {
@@ -150,13 +192,13 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
                return (NULL);
        }
 
-       ident = ntohs (icmp_hdr->un.echo.id);
-       seq   = ntohs (icmp_hdr->un.echo.sequence);
+       ident = ntohs (icmp_hdr->icmp_id);
+       seq   = ntohs (icmp_hdr->icmp_seq);
 
        for (ptr = ph; ptr != NULL; ptr = ptr->next)
        {
                dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
-                               ptr->hostname, ptr->ident, ptr->sequence - 1);
+                               ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
 
                if (ptr->addrfamily != AF_INET)
                        continue;
@@ -167,7 +209,7 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
                if (ptr->ident != ident)
                        continue;
 
-               if ((ptr->sequence - 1) != seq)
+               if (((ptr->sequence - 1) & 0xFFFF) != seq)
                        continue;
 
                dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
@@ -198,8 +240,8 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
                return (NULL);
 
        icmp_hdr = (struct icmp6_hdr *) buffer;
-       buffer     += sizeof (struct icmphdr);
-       buffer_len -= sizeof (struct icmphdr);
+       buffer     += sizeof (struct icmp);
+       buffer_len -= sizeof (struct icmp);
 
        if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
        {
@@ -219,7 +261,7 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
        for (ptr = ph; ptr != NULL; ptr = ptr->next)
        {
                dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
-                               ptr->hostname, ptr->ident, ptr->sequence - 1);
+                               ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
 
                if (ptr->addrfamily != AF_INET6)
                        continue;
@@ -230,7 +272,7 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
                if (ptr->ident != ident)
                        continue;
 
-               if ((ptr->sequence - 1) != seq)
+               if (((ptr->sequence - 1) & 0xFFFF) != seq)
                        continue;
 
                dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
@@ -420,7 +462,7 @@ ssize_t ping_sendto (pinghost_t *ph, const void *buf, size_t buflen)
 
 static int ping_send_one_ipv4 (pinghost_t *ph)
 {
-       struct icmphdr *icmp4;
+       struct icmp *icmp4;
        int status;
 
        char buf[4096];
@@ -432,22 +474,21 @@ static int ping_send_one_ipv4 (pinghost_t *ph)
        dprintf ("ph->hostname = %s\n", ph->hostname);
 
        memset (buf, '\0', sizeof (buf));
-       icmp4 = (struct icmphdr *) buf;
+       icmp4 = (struct icmp *) buf;
        data  = (char *) (icmp4 + 1);
 
-       icmp4->type             = ICMP_ECHO;
-       icmp4->code             = 0;
-       /* The checksum will be calculated by the TCP/IP stack.  */
-       icmp4->checksum         = 0;
-       icmp4->un.echo.id       = htons (ph->ident);
-       icmp4->un.echo.sequence = htons (ph->sequence);
+       icmp4->icmp_type  = ICMP_ECHO;
+       icmp4->icmp_code  = 0;
+       icmp4->icmp_cksum = 0;
+       icmp4->icmp_id    = htons (ph->ident);
+       icmp4->icmp_seq   = htons (ph->sequence);
 
        strcpy (data, PING_DATA);
        datalen = strlen (data);
 
-       buflen = datalen + sizeof (struct icmphdr);
+       buflen = datalen + sizeof (struct icmp);
 
-       icmp4->checksum = ping_icmp4_checksum (buf, buflen);
+       icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
 
        dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
 
@@ -815,6 +856,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        continue;
                }
 
+#if 0
                if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
                {
                        dprintf ("bind: %s\n", strerror (errno));
@@ -822,6 +864,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        ph->fd = -1;
                        continue;
                }
+#endif
 
                assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
                memset (ph->addr, '\0', sizeof (struct sockaddr_storage));