projects
/
collectd.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
rrdtool plugin: Include the `rrd.h' header file in `rrdtool.c', not `collectd.h'.
[collectd.git]
/
src
/
ntpd.c
diff --git
a/src/ntpd.c
b/src/ntpd.c
index
83bf162
..
63b037a
100644
(file)
--- a/
src/ntpd.c
+++ b/
src/ntpd.c
@@
-24,12
+24,6
@@
#include "plugin.h"
#include "configfile.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
#if HAVE_STDINT_H
# include <stdint.h>
#endif
@@
-60,12
+54,11
@@
static const char *config_keys[] =
};
static int config_keys_num = 2;
};
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;
# 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 *
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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)
{
static int ntpd_config (const char *key, const char *value)
{
- if (strcasecmp (key, "
h
ost") == 0)
+ if (strcasecmp (key, "
H
ost") == 0)
{
if (ntpd_host != NULL)
free (ntpd_host);
if ((ntpd_host = strdup (value)) == NULL)
return (1);
}
{
if (ntpd_host != NULL)
free (ntpd_host);
if ((ntpd_host = strdup (value)) == NULL)
return (1);
}
- else if (strcasecmp (key, "
p
ort") == 0)
+ else if (strcasecmp (key, "
P
ort") == 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
{
}
else
{
@@
-348,11
+344,14
@@
static int ntpd_connect (void)
host = NTPD_DEFAULT_HOST;
port = ntpd_port;
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));
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;
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)
{
if (sock_descr < 0)
{
- DEBUG ("Unable to connect to server.");
ERROR ("ntpd plugin: Unable to connect to server.");
}
ERROR ("ntpd plugin: Unable to connect to server.");
}
@@
-568,6
+566,14
@@
static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
continue;
}
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. */
/* 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. */
@@
-584,9
+590,16
@@
static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
continue;
}
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;
/* 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;",
res_item_size, pkt_padding);
pkt_padding = res_item_size - pkt_item_len;
DEBUG ("res_item_size = %i; pkt_padding = %i;",
res_item_size, pkt_padding);
@@
-631,18
+644,26
@@
static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
(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) * 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;
}
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++)
{
*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));
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 */
void *src = (void *) (((char *) res.data) + (i * pkt_item_len));
/* Set the padding to zeros */
@@
-650,8
+671,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);
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)++;
(*res_items)++;
- }
+ }
/* for (pkt_item_num) */
pkt_recvd[pkt_sequence] = (char) 1;
pkt_recvd_num++;
pkt_recvd[pkt_sequence] = (char) 1;
pkt_recvd_num++;
@@
-661,7
+684,7
@@
static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
} /* while (done == 0) */
return (0);
} /* 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)
/* 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)
@@
-928,13
+951,10
@@
static int ntpd_read (void)
return (0);
} /* int ntpd_read */
return (0);
} /* int ntpd_read */
-#endif /* NTPD_HAVE_READ */
void module_register (void)
{
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);
plugin_register_config ("ntpd", ntpd_config,
config_keys, config_keys_num);
plugin_register_read ("ntpd", ntpd_read);
-#endif /* NTPD_HAVE_READ */
} /* void module_register */
} /* void module_register */