Added `--enable-debug' to the configure script.
[liboping.git] / src / liboping.c
index de1ff02..c6e9df5 100644 (file)
@@ -87,7 +87,7 @@
 
 #include "oping.h"
 
-#if DEBUG
+#if WITH_DEBUG
 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
 #else
 # define dprintf(...) /**/
@@ -95,8 +95,6 @@
 
 #define PING_ERRMSG_LEN 256
 
-#define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
-
 struct pinghost
 {
        char                    *hostname;
@@ -108,6 +106,7 @@ struct pinghost
        int                      sequence;
        struct timeval          *timer;
        double                   latency;
+       char                    *data;
 
        void                    *context;
 
@@ -119,6 +118,7 @@ struct pingobj
        double      timeout;
        int         ttl;
        int         addrfamily;
+       char       *data;
 
        char        errmsg[PING_ERRMSG_LEN];
 
@@ -565,7 +565,8 @@ static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
        icmp4->icmp_id    = htons (ph->ident);
        icmp4->icmp_seq   = htons (ph->sequence);
 
-       strcpy (data, PING_DATA);
+       buflen = 4096 - sizeof (struct icmp);
+       strncpy (data, ph->data, buflen);
        datalen = strlen (data);
 
        buflen = datalen + sizeof (struct icmp);
@@ -606,11 +607,13 @@ static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
        icmp6->icmp6_type  = ICMP6_ECHO_REQUEST;
        icmp6->icmp6_code  = 0;
        /* The checksum will be calculated by the TCP/IP stack.  */
+       /* FIXME */
        icmp6->icmp6_cksum = 0;
        icmp6->icmp6_id    = htons (ph->ident);
        icmp6->icmp6_seq   = htons (ph->sequence);
 
-       strcpy (data, PING_DATA);
+       buflen = 4096 - sizeof (struct icmp6_hdr);
+       strncpy (data, ph->data, buflen);
        datalen = strlen (data);
 
        buflen = datalen + sizeof (struct icmp6_hdr);
@@ -773,6 +776,9 @@ static void ping_free (pinghost_t *ph)
        if (ph->hostname != NULL)
                free (ph->hostname);
 
+       if (ph->data != NULL)
+               free (ph->data);
+
        free (ph);
 }
 
@@ -795,6 +801,7 @@ pingobj_t *ping_construct (void)
        obj->timeout    = PING_DEF_TIMEOUT;
        obj->ttl        = PING_DEF_TTL;
        obj->addrfamily = PING_DEF_AF;
+       obj->data       = strdup (PING_DEF_DATA);
 
        return (obj);
 }
@@ -814,6 +821,9 @@ void ping_destroy (pingobj_t *obj)
                current = next;
        }
 
+       if (obj->data != NULL)
+               free (obj->data);
+
        free (obj);
 
        return;
@@ -854,6 +864,15 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
                        }
                        break;
 
+               case PING_OPT_DATA:
+                       if (obj->data != NULL)
+                       {
+                               free (obj->data);
+                               obj->data = NULL;
+                       }
+                       obj->data = strdup ((const char *) value);
+                       break;
+
                default:
                        ret = -2;
        } /* switch (option) */
@@ -926,6 +945,15 @@ int ping_host_add (pingobj_t *obj, const char *host)
                return (-1);
        }
 
+       /* obj->data is not garuanteed to be != NULL */
+       if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
+       {
+               dprintf ("Out of memory!\n");
+               ping_set_error (obj, "strdup", strerror (errno));
+               ping_free (ph);
+               return (-1);
+       }
+
        if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
        {
                dprintf ("getaddrinfo failed\n");
@@ -1020,8 +1048,26 @@ int ping_host_add (pingobj_t *obj, const char *host)
                return (-1);
        }
 
-       ph->next  = obj->head;
-       obj->head = ph;
+       /*
+        * Adding in the front is much easier, but then the iterator will
+        * return the host that was added last as first host. That's just not
+        * nice. -octo
+        */
+       if (obj->head == NULL)
+       {
+               obj->head = ph;
+       }
+       else
+       {
+               pinghost_t *hptr;
+
+               hptr = obj->head;
+               while (hptr->next != NULL)
+                       hptr = hptr->next;
+
+               assert ((hptr != NULL) && (hptr->next == NULL));
+               hptr->next = ph;
+       }
 
        ping_set_ttl (ph, obj->ttl);
 
@@ -1135,7 +1181,6 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info,
                        ret = 0;
                        break;
 
-               /* FIXME Return the sequence as an unsigned int */
                case PING_INFO_SEQUENCE:
                        ret = ENOMEM;
                        *buffer_len = sizeof (unsigned int);
@@ -1153,6 +1198,15 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info,
                        *((uint16_t *) buffer) = (uint16_t) iter->ident;
                        ret = 0;
                        break;
+
+               case PING_INFO_DATA:
+                       ret = ENOMEM;
+                       *buffer_len = strlen (iter->data);
+                       if (orig_buffer_len < *buffer_len)
+                               break;
+                       strncpy ((char *) buffer, iter->data, orig_buffer_len);
+                       ret = 0;
+                       break;
        }
 
        return (ret);