src/common.[ch]: Implement "parse_identifier_vl".
[collectd.git] / src / ntpd.c
index 668e302..ecc87c7 100644 (file)
  *   Florian octo Forster <octo at verplant.org>
  **/
 
+#define _BSD_SOURCE /* For NI_MAXHOST */
+
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
 #include "configfile.h"
 
-#if HAVE_SYS_SOCKET_H
-# define NTPD_HAVE_READ 1
-#else
-# define NTPD_HAVE_READ 0
-#endif
-
 #if HAVE_STDINT_H
 # include <stdint.h>
 #endif
 # include <poll.h>
 #endif
 
-static data_source_t seconds_dsrc[1] =
-{
-       {"seconds", DS_TYPE_GAUGE, -1000000.0, 1000000.0}
-};
-
-static data_set_t time_offset_ds =
-{
-       "time_offset", 1, seconds_dsrc
-};
-
-static data_set_t time_dispersion_ds =
-{
-       "time_dispersion", 1, seconds_dsrc
-};
-
-static data_set_t delay_ds =
-{
-       "delay", 1, seconds_dsrc
-};
-
-static data_source_t ppm_dsrc[1] =
-{
-       {"ppm", DS_TYPE_GAUGE, -1000000.0, 1000000.0}
-};
-
-static data_set_t frequency_offset_ds =
-{
-       "frequency_offset", 1, ppm_dsrc
-};
-
 static const char *config_keys[] =
 {
        "Host",
        "Port",
-       NULL
+       "ReverseLookups"
 };
-static int config_keys_num = 2;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
+static int do_reverse_lookups = 1;
 
-#if NTPD_HAVE_READ
 # define NTPD_DEFAULT_HOST "localhost"
 # define NTPD_DEFAULT_PORT "123"
 static int   sock_descr = -1;
 static char *ntpd_host = NULL;
-static char *ntpd_port = NULL;
+static char  ntpd_port[16];
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * The following definitions were copied from the NTPd distribution  *
@@ -103,7 +70,7 @@ static char *ntpd_port = NULL;
 #define MAXFILENAME 128
 #define MAXSEQ  127
 #define MODE_PRIVATE 7
-#define NTP_OLDVERSION ((u_char) 1) /* oldest credible version */
+#define NTP_OLDVERSION ((uint8_t) 1) /* oldest credible version */
 #define IMPL_XNTPD 3
 #define FP_FRAC 65536.0
 
@@ -152,27 +119,27 @@ struct resp_pkt
 
 #define        ISRESPONSE(rm_vn_mode)  (((rm_vn_mode)&RESP_BIT)!=0)
 #define        ISMORE(rm_vn_mode)      (((rm_vn_mode)&MORE_BIT)!=0)
-#define INFO_VERSION(rm_vn_mode) ((u_char)(((rm_vn_mode)>>3)&0x7))
+#define INFO_VERSION(rm_vn_mode) ((uint8_t)(((rm_vn_mode)>>3)&0x7))
 #define        INFO_MODE(rm_vn_mode)   ((rm_vn_mode)&0x7)
 
 #define        RM_VN_MODE(resp, more, version)         \
-                               ((u_char)(((resp)?RESP_BIT:0)\
+                               ((uint8_t)(((resp)?RESP_BIT:0)\
                                |((more)?MORE_BIT:0)\
                                |((version?version:(NTP_OLDVERSION+1))<<3)\
                                |(MODE_PRIVATE)))
 
 #define        INFO_IS_AUTH(auth_seq)  (((auth_seq) & 0x80) != 0)
 #define        INFO_SEQ(auth_seq)      ((auth_seq)&0x7f)
-#define        AUTH_SEQ(auth, seq)     ((u_char)((((auth)!=0)?0x80:0)|((seq)&0x7f)))
+#define        AUTH_SEQ(auth, seq)     ((uint8_t)((((auth)!=0)?0x80:0)|((seq)&0x7f)))
 
-#define        INFO_ERR(err_nitems)    ((u_short)((ntohs(err_nitems)>>12)&0xf))
-#define        INFO_NITEMS(err_nitems) ((u_short)(ntohs(err_nitems)&0xfff))
-#define        ERR_NITEMS(err, nitems) (htons((u_short)((((u_short)(err)<<12)&0xf000)\
-                               |((u_short)(nitems)&0xfff))))
+#define        INFO_ERR(err_nitems)    ((uint16_t)((ntohs(err_nitems)>>12)&0xf))
+#define        INFO_NITEMS(err_nitems) ((uint16_t)(ntohs(err_nitems)&0xfff))
+#define        ERR_NITEMS(err, nitems) (htons((uint16_t)((((uint16_t)(err)<<12)&0xf000)\
+                               |((uint16_t)(nitems)&0xfff))))
 
 #define        INFO_MBZ(mbz_itemsize)  ((ntohs(mbz_itemsize)>>12)&0xf)
-#define        INFO_ITEMSIZE(mbz_itemsize)     ((u_short)(ntohs(mbz_itemsize)&0xfff))
-#define        MBZ_ITEMSIZE(itemsize)  (htons((u_short)(itemsize)))
+#define        INFO_ITEMSIZE(mbz_itemsize)     ((uint16_t)(ntohs(mbz_itemsize)&0xfff))
+#define        MBZ_ITEMSIZE(itemsize)  (htons((uint16_t)(itemsize)))
 
 /* negate a long float type */
 #define M_NEG(v_i, v_f) \
@@ -284,28 +251,37 @@ static char *refclock_names[] =
        "CHRONOLOG",  "DUMBCLOCK",    "ULINK_M320", "PCF",         /* 32-35 */
        "WWV_AUDIO",  "GPS_FG",       "HOPF_S",     "HOPF_P",      /* 36-39 */
        "JJY",        "TT_IRIG",      "GPS_ZYFER",  "GPS_RIPENCC", /* 40-43 */
-       "NEOCLK4X",   NULL                                         /* 44    */
+       "NEOCLK4X"                                                 /* 44    */
 };
-static int refclock_names_num = 45;
+static int refclock_names_num = STATIC_ARRAY_SIZE (refclock_names);
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * End of the copied stuff..                                         *
  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 static int ntpd_config (const char *key, const char *value)
 {
-       if (strcasecmp (key, "host") == 0)
+       if (strcasecmp (key, "Host") == 0)
        {
                if (ntpd_host != NULL)
                        free (ntpd_host);
                if ((ntpd_host = strdup (value)) == NULL)
                        return (1);
        }
-       else if (strcasecmp (key, "port") == 0)
+       else if (strcasecmp (key, "Port") == 0)
        {
-               if (ntpd_port != NULL)
-                       free (ntpd_port);
-               if ((ntpd_port = strdup (value)) == NULL)
-                       return (1);
+               int port = (int) (atof (value));
+               if ((port > 0) && (port <= 65535))
+                       ssnprintf (ntpd_port, sizeof (ntpd_port),
+                                       "%i", port);
+               else
+                       sstrncpy (ntpd_port, value, sizeof (ntpd_port));
+       }
+       else if (strcasecmp (key, "ReverseLookups") == 0)
+       {
+               if (IS_TRUE (value))
+                       do_reverse_lookups = 1;
+               else
+                       do_reverse_lookups = 0;
        }
        else
        {
@@ -324,38 +300,13 @@ static void ntpd_submit (char *type, char *type_inst, double value)
 
        vl.values = values;
        vl.values_len = 1;
-       vl.time = time (NULL);
-       strcpy (vl.host, hostname_g);
-       strcpy (vl.plugin, "ntpd");
-       strcpy (vl.plugin_instance, "");
-       strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "ntpd", sizeof (vl.plugin));
+       sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
-}
-
-/* returns `tv0 - tv1' in milliseconds or 0 if `tv1 > tv0' */
-static int timeval_sub (const struct timeval *tv0, const struct timeval *tv1)
-{
-       int sec;
-       int usec;
-
-       if ((tv0->tv_sec < tv1->tv_sec)
-                       || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
-               return (0);
-
-       sec  = tv0->tv_sec  - tv1->tv_sec;
-       usec = tv0->tv_usec - tv1->tv_usec;
-
-       while (usec < 0)
-       {
-               usec += 1000000;
-               sec  -= 1;
-       }
-
-       if (sec < 0)
-               return (0);
-
-       return ((sec * 1000) + ((usec + 500) / 1000));
+       plugin_dispatch_values (&vl);
 }
 
 static int ntpd_connect (void)
@@ -378,23 +329,26 @@ static int ntpd_connect (void)
                host = NTPD_DEFAULT_HOST;
 
        port = ntpd_port;
-       if (port == NULL)
+       if (strlen (port) == 0)
                port = NTPD_DEFAULT_PORT;
 
        memset (&ai_hints, '\0', sizeof (ai_hints));
-       ai_hints.ai_flags    = AI_ADDRCONFIG;
+       ai_hints.ai_flags    = 0;
+#ifdef AI_ADDRCONFIG
+       ai_hints.ai_flags   |= AI_ADDRCONFIG;
+#endif
        ai_hints.ai_family   = PF_UNSPEC;
        ai_hints.ai_socktype = SOCK_DGRAM;
        ai_hints.ai_protocol = IPPROTO_UDP;
 
        if ((status = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
        {
-               DEBUG ("getaddrinfo (%s, %s): %s",
-                               host, port,
-                               status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
+               char errbuf[1024];
                ERROR ("ntpd plugin: getaddrinfo (%s, %s): %s",
                                host, port,
-                               status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
+                               (status == EAI_SYSTEM)
+                               ? sstrerror (errno, errbuf, sizeof (errbuf))
+                               : gai_strerror (status));
                return (-1);
        }
 
@@ -421,7 +375,6 @@ static int ntpd_connect (void)
 
        if (sock_descr < 0)
        {
-               DEBUG ("Unable to connect to server.");
                ERROR ("ntpd plugin: Unable to connect to server.");
        }
 
@@ -429,7 +382,7 @@ static int ntpd_connect (void)
 }
 
 /* For a description of the arguments see `ntpd_do_query' below. */
-static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
+static int ntpd_receive_response (int *res_items, int *res_size,
                char **res_data, int res_item_size)
 {
        int              sd;
@@ -470,8 +423,9 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
 
        if (gettimeofday (&time_end, NULL) < 0)
        {
+               char errbuf[1024];
                ERROR ("ntpd plugin: gettimeofday failed: %s",
-                               strerror (errno));
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return (-1);
        }
        time_end.tv_sec++; /* wait for a most one second */
@@ -479,15 +433,24 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
        done = 0;
        while (done == 0)
        {
+               struct timeval time_left;
+
                if (gettimeofday (&time_now, NULL) < 0)
                {
+                       char errbuf[1024];
                        ERROR ("ntpd plugin: gettimeofday failed: %s",
-                                       strerror (errno));
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
                        return (-1);
                }
 
+               if (timeval_cmp (time_end, time_now, &time_left) <= 0)
+                       timeout = 0;
+               else
+                       timeout = 1000 * time_left.tv_sec
+                               + ((time_left.tv_usec + 500) / 1000);
+
                /* timeout reached */
-               if ((timeout = timeval_sub (&time_end, &time_now)) == 0)
+               if (timeout <= 0)
                        break;
 
                poll_s.fd      = sd;
@@ -502,9 +465,9 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
 
                if (status < 0)
                {
-                       DEBUG ("poll failed: %s", strerror (errno));
+                       char errbuf[1024];
                        ERROR ("ntpd plugin: poll failed: %s",
-                                       strerror (errno));
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
                        return (-1);
                }
 
@@ -522,7 +485,9 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
 
                if (status < 0)
                {
-                       DEBUG ("recv(2) failed: %s", strerror (errno));
+                       char errbuf[1024];
+                       INFO ("recv(2) failed: %s",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
                        DEBUG ("Closing socket #%i", sd);
                        close (sd);
                        sock_descr = sd = -1;
@@ -594,6 +559,14 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
                        continue;
                }
 
+               if (pkt_item_len > res_item_size)
+               {
+                       ERROR ("ntpd plugin: (pkt_item_len = %i) "
+                                       ">= (res_item_size = %i)",
+                                       pkt_item_len, res_item_size);
+                       continue;
+               }
+
                /* If this is the first packet (time wise, not sequence wise),
                 * set `res_size'. If it's not the first packet check if the
                 * items have the same size. Discard invalid packets. */
@@ -610,11 +583,18 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
                        continue;
                }
 
+               /*
+                * Because the items in the packet may be smaller than the
+                * items requested, the following holds true:
+                */
+               assert ((*res_size == pkt_item_len)
+                               && (pkt_item_len <= res_item_size));
+
                /* Calculate the padding. No idea why there might be any padding.. */
                pkt_padding = 0;
-               if (res_item_size > pkt_item_len)
+               if (pkt_item_len < res_item_size)
                        pkt_padding = res_item_size - pkt_item_len;
-               DEBUG ("res_item_size = %i; pkt_padding = %i;",
+               DEBUG ("res_item_size = %i; pkt_padding = %zi;",
                                res_item_size, pkt_padding);
 
                /* Extract the sequence number */
@@ -653,22 +633,30 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
                 * Enough with the checks. Copy the data now.
                 * We start by allocating some more memory.
                 */
-               DEBUG ("realloc (%p, %i)", (void *) *res_data,
+               DEBUG ("realloc (%p, %zu)", (void *) *res_data,
                                (items_num + pkt_item_num) * res_item_size);
                items = realloc ((void *) *res_data,
                                (items_num + pkt_item_num) * res_item_size);
-               items_num += pkt_item_num;
                if (items == NULL)
                {
                        items = *res_data;
                        ERROR ("ntpd plugin: realloc failed.");
                        continue;
                }
+               items_num += pkt_item_num;
                *res_data = items;
 
                for (i = 0; i < pkt_item_num; i++)
                {
+                       /* dst: There are already `*res_items' items with
+                        *      res_item_size bytes each in in `*res_data'. Set
+                        *      dst to the first byte after that. */
                        void *dst = (void *) (*res_data + ((*res_items) * res_item_size));
+                       /* src: We use `pkt_item_len' to calculate the offset
+                        *      from the beginning of the packet, because the
+                        *      items in the packet may be smaller than the
+                        *      items that were requested. We skip `i' such
+                        *      items. */
                        void *src = (void *) (((char *) res.data) + (i * pkt_item_len));
 
                        /* Set the padding to zeros */
@@ -676,8 +664,10 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
                                memset (dst, '\0', res_item_size);
                        memcpy (dst, src, (size_t) pkt_item_len);
 
+                       /* Increment `*res_items' by one, so `dst' will end up
+                        * one further in the next round. */
                        (*res_items)++;
-               }
+               } /* for (pkt_item_num) */
 
                pkt_recvd[pkt_sequence] = (char) 1;
                pkt_recvd_num++;
@@ -687,7 +677,7 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
        } /* while (done == 0) */
 
        return (0);
-}
+} /* int ntpd_receive_response */
 
 /* For a description of the arguments see `ntpd_do_query' below. */
 static int ntpd_send_request (int req_code, int req_items, int req_size, char *req_data)
@@ -758,7 +748,7 @@ static int ntpd_do_query (int req_code, int req_items, int req_size, char *req_d
        if (status != 0)
                return (status);
 
-       status = ntpd_receive_response (req_code, res_items, res_size, res_data,
+       status = ntpd_receive_response (res_items, res_size, res_data,
                        res_item_size);
        return (status);
 }
@@ -851,37 +841,13 @@ static int ntpd_read (void)
                ptr = ps + i;
                refclock_id = 0;
 
-               /*
-               if (((ntohl (ptr->dstadr) & 0xFFFFFF00) == 0x7F000000) || (ptr->dstadr == 0))
-                       continue;
-                       */
-
                /* Convert the `long floating point' offset value to double */
                M_LFPTOD (ntohl (ptr->offset_int), ntohl (ptr->offset_frc), offset);
 
-               if (ptr->v6_flag)
-               {
-                       struct sockaddr_in6 sa;
-
-                       memset (&sa, 0, sizeof (sa));
-                       sa.sin6_family = AF_INET6;
-                       sa.sin6_port = htons (123);
-                       memcpy (&sa.sin6_addr, &ptr->srcadr6, sizeof (struct in6_addr));
-
-                       status = getnameinfo ((const struct sockaddr *) &sa,
-                                       sizeof (sa),
-                                       peername, sizeof (peername),
-                                       NULL, 0, 0 /* no flags */);
-                       if (status != 0)
-                       {
-                               ERROR ("ntpd plugin: getnameinfo failed: %s",
-                                               status == EAI_SYSTEM
-                                               ? strerror (errno)
-                                               : gai_strerror (status));
-                               continue;
-                       }
-               }
-               else if ((ntohl (ptr->srcadr) & REFCLOCK_MASK) == REFCLOCK_ADDR)
+               /* Special IP addresses for hardware clocks and stuff.. */
+               if (!ptr->v6_flag
+                               && ((ntohl (ptr->srcadr) & REFCLOCK_MASK)
+                                       == REFCLOCK_ADDR))
                {
                        struct in_addr  addr_obj;
                        char *addr_str;
@@ -890,7 +856,7 @@ static int ntpd_read (void)
 
                        if (refclock_id < refclock_names_num)
                        {
-                               strncpy (peername, refclock_names[refclock_id],
+                               sstrncpy (peername, refclock_names[refclock_id],
                                                sizeof (peername));
                        }
                        else
@@ -899,28 +865,64 @@ static int ntpd_read (void)
                                addr_obj.s_addr = ptr->srcadr;
                                addr_str = inet_ntoa (addr_obj);
 
-                               strncpy (peername, addr_str, sizeof (peername));
+                               sstrncpy (peername, addr_str, sizeof (peername));
                        }
                }
-               else /* IPv4 */
+               else /* Normal network host. */
                {
-                       struct in_addr  addr_obj;
-                       struct hostent *addr_he;
-                       char           *addr_str;
+                       struct sockaddr_storage sa;
+                       socklen_t sa_len;
+                       int flags = 0;
 
-                       memset ((void *) &addr_obj, '\0', sizeof (addr_obj));
-                       addr_obj.s_addr = ptr->srcadr;
-                       addr_str = inet_ntoa (addr_obj);
+                       memset (&sa, '\0', sizeof (sa));
 
-                       addr_he = gethostbyaddr ((const void *) &addr_obj,
-                                       sizeof (addr_obj), AF_INET);
-                       if (addr_he != NULL)
+                       if (ptr->v6_flag)
                        {
-                               strncpy (peername, addr_he->h_name, sizeof (peername));
+                               struct sockaddr_in6 sa6;
+
+                               assert (sizeof (sa) >= sizeof (sa6));
+
+                               memset (&sa6, 0, sizeof (sa6));
+                               sa6.sin6_family = AF_INET6;
+                               sa6.sin6_port = htons (123);
+                               memcpy (&sa6.sin6_addr, &ptr->srcadr6,
+                                               sizeof (struct in6_addr));
+                               sa_len = sizeof (sa6);
+
+                               memcpy (&sa, &sa6, sizeof (sa6));
                        }
                        else
                        {
-                               strncpy (peername, addr_str, sizeof (peername));
+                               struct sockaddr_in sa4;
+
+                               assert (sizeof (sa) >= sizeof (sa4));
+
+                               memset (&sa4, 0, sizeof (sa4));
+                               sa4.sin_family = AF_INET;
+                               sa4.sin_port = htons (123);
+                               memcpy (&sa4.sin_addr, &ptr->srcadr,
+                                               sizeof (struct in_addr));
+                               sa_len = sizeof (sa4);
+
+                               memcpy (&sa, &sa4, sizeof (sa4));
+                       }
+
+                       if (do_reverse_lookups == 0)
+                               flags |= NI_NUMERICHOST;
+
+                       status = getnameinfo ((const struct sockaddr *) &sa,
+                                       sa_len,
+                                       peername, sizeof (peername),
+                                       NULL, 0, /* No port name */
+                                       flags);
+                       if (status != 0)
+                       {
+                               char errbuf[1024];
+                               ERROR ("ntpd plugin: getnameinfo failed: %s",
+                                               (status == EAI_SYSTEM)
+                                               ? sstrerror (errno, errbuf, sizeof (errbuf))
+                                               : gai_strerror (status));
+                               continue;
                        }
                }
 
@@ -953,17 +955,10 @@ static int ntpd_read (void)
 
        return (0);
 } /* int ntpd_read */
-#endif /* NTPD_HAVE_READ */
 
 void module_register (void)
 {
-       plugin_register_data_set (&time_offset_ds);
-       plugin_register_data_set (&time_dispersion_ds);
-       plugin_register_data_set (&delay_ds);
-       plugin_register_data_set (&frequency_offset_ds);
-
-#if NTPD_HAVE_READ
-       plugin_register_config ("ntpd", ntpd_config, config_keys, config_keys_num);
+       plugin_register_config ("ntpd", ntpd_config,
+                       config_keys, config_keys_num);
        plugin_register_read ("ntpd", ntpd_read);
-#endif /* NTPD_HAVE_READ */
-}
+} /* void module_register */