Merge branch 'pull/collectd-4.1' into collectd-4.1
[collectd.git] / src / ntpd.c
index b5f1a46..9e09f81 100644 (file)
 #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
@@ -60,12 +54,11 @@ static const char *config_keys[] =
 };
 static int config_keys_num = 2;
 
-#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  *
@@ -263,19 +256,22 @@ static int refclock_names_num = 45;
 
 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))
+                       snprintf (ntpd_port, sizeof (ntpd_port),
+                                       "%i", port);
+               else
+                       strncpy (ntpd_port, value, sizeof (ntpd_port));
+               ntpd_port[sizeof (ntpd_port) - 1] = '\0';
        }
        else
        {
@@ -348,11 +344,14 @@ 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;
@@ -391,7 +390,6 @@ static int ntpd_connect (void)
 
        if (sock_descr < 0)
        {
-               DEBUG ("Unable to connect to server.");
                ERROR ("ntpd plugin: Unable to connect to server.");
        }
 
@@ -495,7 +493,7 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
                if (status < 0)
                {
                        char errbuf[1024];
-                       DEBUG ("recv(2) failed: %s",
+                       INFO ("recv(2) failed: %s",
                                        sstrerror (errno, errbuf, sizeof (errbuf)));
                        DEBUG ("Closing socket #%i", sd);
                        close (sd);
@@ -570,7 +568,7 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
 
                if (pkt_item_len > res_item_size)
                {
-                       syslog (LOG_ERR, "ntpd plugin: (pkt_item_len = %i) "
+                       ERROR ("ntpd plugin: (pkt_item_len = %i) "
                                        ">= (res_item_size = %i)",
                                        pkt_item_len, res_item_size);
                        continue;
@@ -953,13 +951,10 @@ static int ntpd_read (void)
 
        return (0);
 } /* int ntpd_read */
-#endif /* NTPD_HAVE_READ */
 
 void module_register (void)
 {
-#if NTPD_HAVE_READ
        plugin_register_config ("ntpd", ntpd_config,
                        config_keys, config_keys_num);
        plugin_register_read ("ntpd", ntpd_read);
-#endif /* NTPD_HAVE_READ */
 } /* void module_register */