diff --git a/src/ntpd.c b/src/ntpd.c
index 08c0c9fb129a80c660eb339d755518a82def5b92..ecc87c78977dbbdde88e920aa5f6156a1060fc0d 100644 (file)
--- a/src/ntpd.c
+++ b/src/ntpd.c
/**
* collectd - src/ntpd.c
- * Copyright (C) 2006 Florian octo Forster
+ * Copyright (C) 2006-2007 Florian octo Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* 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"
-#include "utils_debug.h"
-
-#define MODULE_NAME "ntpd"
-
-#if HAVE_SYS_SOCKET_H
-# define NTPD_HAVE_READ 1
-#else
-# define NTPD_HAVE_READ 0
-#endif
#if HAVE_STDINT_H
# include <stdint.h>
#if HAVE_NETINET_TCP_H
# include <netinet/tcp.h>
#endif
-#if HAVE_SYS_POLL_H
-# include <sys/poll.h>
+#if HAVE_POLL_H
+# include <poll.h>
#endif
-static char *config_keys[] =
+static const char *config_keys[] =
{
"Host",
"Port",
- NULL
-};
-static int config_keys_num = 2;
-
-/* drift */
-static char *time_offset_file = "ntpd/time_offset-%s.rrd";
-static char *time_dispersion_file = "ntpd/time_dispersion-%s.rrd";
-static char *time_delay_file = "ntpd/delay-%s.rrd";
-
-/* used for `time_offset', `time_dispersion', and `delay' */
-static char *sec_ds_def[] =
-{
- "DS:seconds:GAUGE:"COLLECTD_HEARTBEAT":-1000000:1000000",
- NULL
+ "ReverseLookups"
};
-static int sec_ds_num = 1;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-static char *frequency_offset_file = "ntpd/frequency_offset-%s.rrd";
-static char *frequency_offset_ds_def[] =
-{
- "DS:ppm:GAUGE:"COLLECTD_HEARTBEAT":-1000000:1000000",
- NULL
-};
-static int frequency_offset_ds_num = 1;
+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;
-#endif
+static char ntpd_port[16];
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* The following definitions were copied from the NTPd distribution *
#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
#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) \
"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 (char *key, char *value)
+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
{
return (0);
}
-static void ntpd_init (void)
+static void ntpd_submit (char *type, char *type_inst, double value)
{
- return;
-}
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
-static void ntpd_write_sec (char *host, char *inst, char *val, char *file)
-{
- char buf[256];
- int status;
+ values[0].gauge = value;
- status = snprintf (buf, 256, file, inst);
- if ((status < 1) || (status >= 256))
- return;
+ vl.values = values;
+ vl.values_len = 1;
+ 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));
- rrd_update_file (host, buf, val,
- sec_ds_def, sec_ds_num);
-}
-
-static void ntpd_write_time_offset (char *host, char *inst, char *val)
-{
- ntpd_write_sec (host, inst, val, time_offset_file);
-}
-
-static void ntpd_write_time_dispersion (char *host, char *inst, char *val)
-{
- ntpd_write_sec (host, inst, val, time_dispersion_file);
-}
-
-static void ntpd_write_delay (char *host, char *inst, char *val)
-{
- ntpd_write_sec (host, inst, val, time_delay_file);
-}
-
-static void ntpd_write_frequency_offset (char *host, char *inst, char *val)
-{
- char buf[256];
- int status;
-
- status = snprintf (buf, 256, frequency_offset_file, inst);
- if ((status < 1) || (status >= 256))
- return;
-
- rrd_update_file (host, buf, val,
- frequency_offset_ds_def, frequency_offset_ds_num);
-}
-
-#if NTPD_HAVE_READ
-static void ntpd_submit (char *type, char *inst, double value)
-{
- char buf[256];
-
- if (snprintf (buf, 256, "%u:%.8f", (unsigned int) curtime, value) >= 256)
- return;
-
- DBG ("type = %s; inst = %s; value = %s;",
- type, inst, buf);
-
- plugin_submit (type, inst, buf);
-}
-
-/* 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)
if (sock_descr >= 0)
return (sock_descr);
- DBG ("Opening a new socket");
+ DEBUG ("Opening a new socket");
host = ntpd_host;
if (host == NULL)
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)
{
- DBG ("getaddrinfo (%s, %s): %s",
+ char errbuf[1024];
+ ERROR ("ntpd plugin: getaddrinfo (%s, %s): %s",
host, port,
- status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
- syslog (LOG_ERR, "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);
}
if (sock_descr < 0)
{
- DBG ("Unable to connect to server.");
- syslog (LOG_ERR, "ntpd plugin: Unable to connect to server.");
+ ERROR ("ntpd plugin: Unable to connect to server.");
}
return (sock_descr);
}
/* 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;
if (gettimeofday (&time_end, NULL) < 0)
{
- syslog (LOG_ERR, "ntpd plugin: gettimeofday failed: %s",
- strerror (errno));
+ char errbuf[1024];
+ ERROR ("ntpd plugin: gettimeofday failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
return (-1);
}
time_end.tv_sec++; /* wait for a most one second */
@@ -516,22 +433,31 @@ 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)
{
- syslog (LOG_ERR, "ntpd plugin: gettimeofday failed: %s",
- strerror (errno));
+ char errbuf[1024];
+ ERROR ("ntpd plugin: gettimeofday failed: %s",
+ 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;
poll_s.events = POLLIN | POLLPRI;
poll_s.revents = 0;
- DBG ("Polling for %ims", timeout);
+ DEBUG ("Polling for %ims", timeout);
status = poll (&poll_s, 1, timeout);
if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
@@ -539,15 +465,15 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
if (status < 0)
{
- DBG ("poll failed: %s", strerror (errno));
- syslog (LOG_ERR, "ntpd plugin: poll failed: %s",
- strerror (errno));
+ char errbuf[1024];
+ ERROR ("ntpd plugin: poll failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
return (-1);
}
if (status == 0) /* timeout */
{
- DBG ("timeout reached.");
+ DEBUG ("timeout reached.");
break;
}
@@ -559,50 +485,52 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
if (status < 0)
{
- DBG ("recv(2) failed: %s", strerror (errno));
- DBG ("Closing socket #%i", sd);
+ char errbuf[1024];
+ INFO ("recv(2) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ DEBUG ("Closing socket #%i", sd);
close (sd);
sock_descr = sd = -1;
return (-1);
}
- DBG ("recv'd %i bytes", status);
+ DEBUG ("recv'd %i bytes", status);
/*
* Do some sanity checks first
*/
if (status < RESP_HEADER_SIZE)
{
- syslog (LOG_WARNING, "ntpd plugin: Short (%i bytes) packet received",
+ WARNING ("ntpd plugin: Short (%i bytes) packet received",
(int) status);
continue;
}
if (INFO_MODE (res.rm_vn_mode) != MODE_PRIVATE)
{
- syslog (LOG_NOTICE, "ntpd plugin: Packet received with mode %i",
+ NOTICE ("ntpd plugin: Packet received with mode %i",
INFO_MODE (res.rm_vn_mode));
continue;
}
if (INFO_IS_AUTH (res.auth_seq))
{
- syslog (LOG_NOTICE, "ntpd plugin: Encrypted packet received");
+ NOTICE ("ntpd plugin: Encrypted packet received");
continue;
}
if (!ISRESPONSE (res.rm_vn_mode))
{
- syslog (LOG_NOTICE, "ntpd plugin: Received request packet, "
+ NOTICE ("ntpd plugin: Received request packet, "
"wanted response");
continue;
}
if (INFO_MBZ (res.mbz_itemsize))
{
- syslog (LOG_WARNING, "ntpd plugin: Received packet with nonzero "
+ WARNING ("ntpd plugin: Received packet with nonzero "
"MBZ field!");
continue;
}
if (res.implementation != IMPL_XNTPD)
{
- syslog (LOG_WARNING, "ntpd plugin: Asked for request of type %i, "
+ WARNING ("ntpd plugin: Asked for request of type %i, "
"got %i", (int) IMPL_XNTPD, (int) res.implementation);
continue;
}
/* Check for error code */
if (INFO_ERR (res.err_nitems) != 0)
{
- syslog (LOG_ERR, "ntpd plugin: Received error code %i",
+ ERROR ("ntpd plugin: Received error code %i",
(int) INFO_ERR(res.err_nitems));
return ((int) INFO_ERR (res.err_nitems));
}
@@ -618,13 +546,13 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
/* extract number of items in this packet and the size of these items */
pkt_item_num = INFO_NITEMS (res.err_nitems);
pkt_item_len = INFO_ITEMSIZE (res.mbz_itemsize);
- DBG ("pkt_item_num = %i; pkt_item_len = %i;",
+ DEBUG ("pkt_item_num = %i; pkt_item_len = %i;",
pkt_item_num, pkt_item_len);
/* Check if the reported items fit in the packet */
if ((pkt_item_num * pkt_item_len) > (status - RESP_HEADER_SIZE))
{
- syslog (LOG_ERR, "ntpd plugin: %i items * %i bytes > "
+ ERROR ("ntpd plugin: %i items * %i bytes > "
"%i bytes - %i bytes header",
(int) pkt_item_num, (int) pkt_item_len,
(int) status, (int) RESP_HEADER_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;
@@ -644,14 +572,14 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
* items have the same size. Discard invalid packets. */
if (items_num == 0) /* first packet */
{
- DBG ("*res_size = %i", pkt_item_len);
+ DEBUG ("*res_size = %i", pkt_item_len);
*res_size = pkt_item_len;
}
else if (*res_size != pkt_item_len)
{
- DBG ("Error: *res_size = %i; pkt_item_len = %i;",
+ DEBUG ("Error: *res_size = %i; pkt_item_len = %i;",
*res_size, pkt_item_len);
- syslog (LOG_ERR, "Item sizes differ.");
+ ERROR ("Item sizes differ.");
continue;
}
@@ -666,14 +594,14 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
pkt_padding = 0;
if (pkt_item_len < res_item_size)
pkt_padding = res_item_size - pkt_item_len;
- DBG ("res_item_size = %i; pkt_padding = %i;",
+ DEBUG ("res_item_size = %i; pkt_padding = %zi;",
res_item_size, pkt_padding);
/* Extract the sequence number */
pkt_sequence = INFO_SEQ (res.auth_seq);
if ((pkt_sequence < 0) || (pkt_sequence > MAXSEQ))
{
- syslog (LOG_ERR, "ntpd plugin: Received packet with sequence %i",
+ ERROR ("ntpd plugin: Received packet with sequence %i",
pkt_sequence);
continue;
}
/* Check if this sequence has been received before. If so, discard it. */
if (pkt_recvd[pkt_sequence] != '\0')
{
- syslog (LOG_NOTICE, "ntpd plugin: Sequence %i received twice",
+ NOTICE ("ntpd plugin: Sequence %i received twice",
pkt_sequence);
continue;
}
@@ -692,27 +620,27 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
{
if (pkt_lastseq != -1)
{
- syslog (LOG_ERR, "ntpd plugin: Two packets which both "
+ ERROR ("ntpd plugin: Two packets which both "
"claim to be the last one in the "
"sequence have been received.");
continue;
}
pkt_lastseq = pkt_sequence;
- DBG ("Last sequence = %i;", pkt_lastseq);
+ DEBUG ("Last sequence = %i;", pkt_lastseq);
}
/*
* Enough with the checks. Copy the data now.
* We start by allocating some more memory.
*/
- DBG ("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);
if (items == NULL)
{
items = *res_data;
- syslog (LOG_ERR, "ntpd plugin: realloc failed.");
+ ERROR ("ntpd plugin: realloc failed.");
continue;
}
items_num += pkt_item_num;
@@ -782,13 +710,13 @@ static int ntpd_send_request (int req_code, int req_items, int req_size, char *r
if (req_data != NULL)
memcpy ((void *) req.data, (const void *) req_data, req_data_len);
- DBG ("req_items = %i; req_size = %i; req_data = %p;",
+ DEBUG ("req_items = %i; req_size = %i; req_data = %p;",
req_items, req_size, (void *) req_data);
status = swrite (sd, (const char *) &req, REQ_LEN_NOMAC);
if (status < 0)
{
- DBG ("`swrite' failed. Closing socket #%i", sd);
+ DEBUG ("`swrite' failed. Closing socket #%i", sd);
close (sd);
sock_descr = sd = -1;
return (status);
@@ -820,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);
}
return (val_double);
}
-static void ntpd_read (void)
+static int ntpd_read (void)
{
struct info_kernel *ik;
int ik_num;
if (status != 0)
{
- DBG ("ntpd_do_query failed with status %i", status);
- return;
+ DEBUG ("ntpd_do_query failed with status %i", status);
+ return (-1);
}
if ((ik == NULL) || (ik_num == 0) || (ik_size == 0))
{
- DBG ("ntpd_do_query returned: ik = %p; ik_num = %i; ik_size = %i;",
+ DEBUG ("ntpd_do_query returned: ik = %p; ik_num = %i; ik_size = %i;",
(void *) ik, ik_num, ik_size);
- return;
+ return (-1);
}
/* kerninfo -> estimated error */
- DBG ("info_kernel:\n"
+ DEBUG ("info_kernel:\n"
" pll offset = %.8f\n"
" pll frequency = %.8f\n" /* drift compensation */
" est error = %.8f\n",
ntpd_read_fp (ik->freq),
ntpd_read_fp (ik->esterror));
- ntpd_submit ("ntpd_frequency_offset", "loop", ntpd_read_fp (ik->freq));
- ntpd_submit ("ntpd_time_offset", "loop", ntpd_read_fp (ik->offset));
- ntpd_submit ("ntpd_time_offset", "error", ntpd_read_fp (ik->esterror));
+ ntpd_submit ("frequency_offset", "loop", ntpd_read_fp (ik->freq));
+ ntpd_submit ("time_offset", "loop", ntpd_read_fp (ik->offset));
+ ntpd_submit ("time_offset", "error", ntpd_read_fp (ik->esterror));
free (ik);
ik = NULL;
sizeof (struct info_peer_summary));
if (status != 0)
{
- DBG ("ntpd_do_query failed with status %i", status);
- return;
+ DEBUG ("ntpd_do_query failed with status %i", status);
+ return (-1);
}
if ((ps == NULL) || (ps_num == 0) || (ps_size == 0))
{
- DBG ("ntpd_do_query returned: ps = %p; ps_num = %i; ps_size = %i;",
+ DEBUG ("ntpd_do_query returned: ps = %p; ps_num = %i; ps_size = %i;",
(void *) ps, ps_num, ps_size);
- return;
+ return (-1);
}
for (i = 0; i < ps_num; i++)
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)
- {
- syslog (LOG_ERR, "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;
if (refclock_id < refclock_names_num)
{
- strncpy (peername, refclock_names[refclock_id],
+ sstrncpy (peername, refclock_names[refclock_id],
sizeof (peername));
}
else
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;
}
}
- DBG ("peer %i:\n"
+ DEBUG ("peer %i:\n"
" peername = %s\n"
" srcadr = 0x%08x\n"
" delay = %f\n"
ntpd_read_fp (ptr->dispersion));
if (refclock_id != 1) /* not the system clock (offset will always be zero.. */
- ntpd_submit ("ntpd_time_offset", peername, offset);
- ntpd_submit ("ntpd_time_dispersion", peername, ntpd_read_fp (ptr->dispersion));
+ ntpd_submit ("time_offset", peername, offset);
+ ntpd_submit ("time_dispersion", peername, ntpd_read_fp (ptr->dispersion));
if (refclock_id == 0) /* not a reference clock */
- ntpd_submit ("ntpd_delay", peername, ntpd_read_fp (ptr->delay));
+ ntpd_submit ("delay", peername, ntpd_read_fp (ptr->delay));
}
free (ps);
ps = NULL;
- return;
-}
-#else
-# define ntpd_read NULL
-#endif /* NTPD_HAVE_READ */
+ return (0);
+} /* int ntpd_read */
void module_register (void)
{
- plugin_register (MODULE_NAME, ntpd_init, ntpd_read, NULL);
- plugin_register ("ntpd_time_offset", NULL, NULL, ntpd_write_time_offset);
- plugin_register ("ntpd_time_dispersion", NULL, NULL, ntpd_write_time_dispersion);
- plugin_register ("ntpd_delay", NULL, NULL, ntpd_write_delay);
- plugin_register ("ntpd_frequency_offset", NULL, NULL, ntpd_write_frequency_offset);
- cf_register (MODULE_NAME, ntpd_config, config_keys, config_keys_num);
-}
-
-#undef MODULE_NAME
+ plugin_register_config ("ntpd", ntpd_config,
+ config_keys, config_keys_num);
+ plugin_register_read ("ntpd", ntpd_read);
+} /* void module_register */