* Florian octo Forster <octo at verplant.org>
**/
+#define _BSD_SOURCE /* For NI_MAXHOST */
+
#include "collectd.h"
#include "common.h"
#include "plugin.h"
#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) \
{
int port = (int) (atof (value));
if ((port > 0) && (port <= 65535))
- snprintf (ntpd_port, sizeof (ntpd_port),
+ ssnprintf (ntpd_port, sizeof (ntpd_port),
"%i", port);
else
- strncpy (ntpd_port, value, sizeof (ntpd_port));
- ntpd_port[sizeof (ntpd_port) - 1] = '\0';
+ sstrncpy (ntpd_port, value, sizeof (ntpd_port));
}
else if (strcasecmp (key, "ReverseLookups") == 0)
{
- if ((strcasecmp (value, "True") == 0)
- || (strcasecmp (value, "Yes") == 0)
- || (strcasecmp (value, "On") == 0))
+ if (IS_TRUE (value))
do_reverse_lookups = 1;
else
do_reverse_lookups = 0;
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)
done = 0;
while (done == 0)
{
+ struct timeval time_left;
+
if (gettimeofday (&time_now, NULL) < 0)
{
char errbuf[1024];
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;
pkt_padding = 0;
if (pkt_item_len < res_item_size)
pkt_padding = res_item_size - pkt_item_len;
- DEBUG ("res_item_size = %i; pkt_padding = %i;",
- res_item_size, (int) pkt_padding);
+ DEBUG ("res_item_size = %i; pkt_padding = %zi;",
+ res_item_size, pkt_padding);
/* Extract the sequence number */
pkt_sequence = INFO_SEQ (res.auth_seq);
* Enough with the checks. Copy the data now.
* We start by allocating some more memory.
*/
- DEBUG ("realloc (%p, %i)", (void *) *res_data,
- (int) (items_num + pkt_item_num) * res_item_size);
+ 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)
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 /* Normal network host. */
if (ptr->v6_flag)
{
- struct sockaddr_in6 *sa_ptr;
- sa_ptr = (struct sockaddr_in6 *) &sa;
+ struct sockaddr_in6 sa6;
- sa_ptr->sin6_family = AF_INET6;
- sa_ptr->sin6_port = htons (123);
- memcpy (&sa_ptr->sin6_addr, &ptr->srcadr6,
+ 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 (struct sockaddr_in6);
+ sa_len = sizeof (sa6);
+
+ memcpy (&sa, &sa6, sizeof (sa6));
}
else
{
- struct sockaddr_in *sa_ptr;
- sa_ptr = (struct sockaddr_in *) &sa;
+ struct sockaddr_in sa4;
- sa_ptr->sin_family = AF_INET;
- sa_ptr->sin_port = htons (123);
- memcpy (&sa_ptr->sin_addr, &ptr->srcadr,
+ 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 (struct sockaddr_in);
+ sa_len = sizeof (sa4);
+
+ memcpy (&sa, &sa4, sizeof (sa4));
}
if (do_reverse_lookups == 0)