Skip to content

Commit 531a5b3

Browse files
committed
Network plugin
1 parent 60d1fd9 commit 531a5b3

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ build_windows ()
151151
--disable-all-plugins \
152152
--host="mingw32" \
153153
--enable-logfile \
154+
--enable-network \
154155
--enable-wmi
155156

156157
cp ${GNULIB_DIR}/../config.h src/gnulib_config.h

src/network.c

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#define _DEFAULT_SOURCE
2626
#define _BSD_SOURCE /* For struct ip_mreq */
2727

28+
#ifdef WIN32
29+
#include <ws2tcpip.h>
30+
#include <netioapi.h>
31+
#undef interface
32+
#endif
33+
2834
#include "collectd.h"
2935

3036
#include "common.h"
@@ -82,6 +88,12 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL;
8288
#endif
8389
#endif /* !IP_ADD_MEMBERSHIP */
8490

91+
#ifdef WIN32
92+
#define OPTION char
93+
#else
94+
#define OPTION void
95+
#endif
96+
8597
/*
8698
* Maximum size required for encryption / signing:
8799
*
@@ -1576,7 +1588,7 @@ static int network_set_ttl(const sockent_t *se, const struct addrinfo *ai) {
15761588
else
15771589
optname = IP_TTL;
15781590

1579-
if (setsockopt(se->data.client.fd, IPPROTO_IP, optname, &network_config_ttl,
1591+
if (setsockopt(se->data.client.fd, IPPROTO_IP, optname, (OPTION*)&network_config_ttl,
15801592
sizeof(network_config_ttl)) != 0) {
15811593
ERROR("network plugin: setsockopt (ipv4-ttl): %s", STRERRNO);
15821594
return -1;
@@ -1593,7 +1605,7 @@ static int network_set_ttl(const sockent_t *se, const struct addrinfo *ai) {
15931605
optname = IPV6_UNICAST_HOPS;
15941606

15951607
if (setsockopt(se->data.client.fd, IPPROTO_IPV6, optname,
1596-
&network_config_ttl, sizeof(network_config_ttl)) != 0) {
1608+
(OPTION*)&network_config_ttl, sizeof(network_config_ttl)) != 0) {
15971609
ERROR("network plugin: setsockopt(ipv6-ttl): %s", STRERRNO);
15981610
return -1;
15991611
}
@@ -1628,7 +1640,7 @@ static int network_set_interface(const sockent_t *se,
16281640
.imr_interface.s_addr = ntohl(INADDR_ANY)};
16291641
#endif
16301642

1631-
if (setsockopt(se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq,
1643+
if (setsockopt(se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF, (OPTION*)&mreq,
16321644
sizeof(mreq)) != 0) {
16331645
ERROR("network plugin: setsockopt (ipv4-multicast-if): %s", STRERRNO);
16341646
return -1;
@@ -1641,7 +1653,7 @@ static int network_set_interface(const sockent_t *se,
16411653

16421654
if (IN6_IS_ADDR_MULTICAST(&addr->sin6_addr)) {
16431655
if (setsockopt(se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1644-
&se->interface, sizeof(se->interface)) != 0) {
1656+
(OPTION*)&se->interface, sizeof(se->interface)) != 0) {
16451657
ERROR("network plugin: setsockopt (ipv6-multicast-if): %s", STRERRNO);
16461658
return -1;
16471659
}
@@ -1685,15 +1697,15 @@ static int network_set_interface(const sockent_t *se,
16851697

16861698
static int network_bind_socket(int fd, const struct addrinfo *ai,
16871699
const int interface_idx) {
1688-
#if KERNEL_SOLARIS
1700+
#if KERNEL_SOLARIS || WIN32
16891701
char loop = 0;
16901702
#else
16911703
int loop = 0;
16921704
#endif
16931705
int yes = 1;
16941706

16951707
/* allow multiple sockets to use the same PORT number */
1696-
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
1708+
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (OPTION*)&yes, sizeof(yes)) == -1) {
16971709
ERROR("network plugin: setsockopt (reuseaddr): %s", STRERRNO);
16981710
return -1;
16991711
}
@@ -1726,14 +1738,13 @@ static int network_bind_socket(int fd, const struct addrinfo *ai,
17261738
#else
17271739
mreq.imr_interface.s_addr = ntohl(INADDR_ANY);
17281740
#endif
1729-
17301741
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) ==
17311742
-1) {
17321743
ERROR("network plugin: setsockopt (multicast-loop): %s", STRERRNO);
17331744
return -1;
17341745
}
17351746

1736-
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) ==
1747+
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (OPTION*)&mreq, sizeof(mreq)) ==
17371748
-1) {
17381749
ERROR("network plugin: setsockopt (add-membership): %s", STRERRNO);
17391750
return -1;
@@ -1769,7 +1780,7 @@ static int network_bind_socket(int fd, const struct addrinfo *ai,
17691780
return -1;
17701781
}
17711782

1772-
if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq,
1783+
if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (OPTION*)&mreq,
17731784
sizeof(mreq)) == -1) {
17741785
ERROR("network plugin: setsockopt (ipv6-add-membership): %s", STRERRNO);
17751786
return -1;
@@ -1945,9 +1956,12 @@ static int sockent_client_connect(sockent_t *se) /* {{{ */
19451956
return 0;
19461957

19471958
struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
1948-
.ai_flags = AI_ADDRCONFIG,
1959+
.ai_flags = 0,
19491960
.ai_protocol = IPPROTO_UDP,
19501961
.ai_socktype = SOCK_DGRAM};
1962+
#ifndef WIN32
1963+
ai_hints.ai_flags = AI_ADDDRCONFIG;
1964+
#endif
19511965

19521966
status = getaddrinfo(se->node,
19531967
(se->service != NULL) ? se->service : NET_DEFAULT_PORT,
@@ -2029,9 +2043,12 @@ static int sockent_server_listen(sockent_t *se) /* {{{ */
20292043
service);
20302044

20312045
struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
2032-
.ai_flags = AI_ADDRCONFIG | AI_PASSIVE,
2046+
.ai_flags = AI_PASSIVE,
20332047
.ai_protocol = IPPROTO_UDP,
20342048
.ai_socktype = SOCK_DGRAM};
2049+
#ifndef WIN32
2050+
ai_hints |= AI_ADDRCONFIG;
2051+
#endif
20352052

20362053
status = getaddrinfo(node, service, &ai_hints, &ai_list);
20372054
if (status != 0) {
@@ -2762,7 +2779,7 @@ static int network_config_add_listen(const oconfig_item_t *ci) /* {{{ */
27622779
else
27632780
#endif /* HAVE_GCRYPT_H */
27642781
if (strcasecmp("Interface", child->key) == 0)
2765-
network_config_set_interface(child, &se->interface);
2782+
network_config_set_interface(child, &se->interface);
27662783
else {
27672784
WARNING("network plugin: Option `%s' is not allowed here.", child->key);
27682785
}
@@ -2842,7 +2859,7 @@ static int network_config_add_server(const oconfig_item_t *ci) /* {{{ */
28422859
else
28432860
#endif /* HAVE_GCRYPT_H */
28442861
if (strcasecmp("Interface", child->key) == 0)
2845-
network_config_set_interface(child, &se->interface);
2862+
network_config_set_interface(child, &se->interface);
28462863
else if (strcasecmp("ResolveInterval", child->key) == 0)
28472864
cf_util_get_cdtime(child, &se->data.client.resolve_interval);
28482865
else {
@@ -3188,4 +3205,4 @@ void module_register(void) {
31883205
plugin_register_init("network", network_init);
31893206
plugin_register_flush("network", network_flush,
31903207
/* user_data = */ NULL);
3191-
} /* void module_register */
3208+
} /* void module_register */

src/utils_fbhash.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include "utils_avltree.h"
3232
#include "utils_fbhash.h"
3333

34+
#ifdef WIN32
35+
#include <windows.h>
36+
#endif
37+
3438
struct fbhash_s {
3539
char *filename;
3640
time_t mtime;
@@ -64,23 +68,30 @@ static void fbh_free_tree(c_avl_tree_t *tree) /* {{{ */
6468
c_avl_destroy(tree);
6569
} /* }}} void fbh_free_tree */
6670

71+
static int lock_file(FILE *fh) {
72+
#ifdef WIN32
73+
return LockFile((HANDLE)_get_osfhandle(fileno(fh)), 0, 0, 0, 0);
74+
#else
75+
/* TODO: Lock file? -> fcntl */
76+
struct flock fl = {0};
77+
fl.l_type = F_RDLCK;
78+
fl.l_whence = SEEK_SET;
79+
return fcntl(fileno(fh), F_SETLK, &fl);
80+
#endif
81+
}
82+
6783
static int fbh_read_file(fbhash_t *h) /* {{{ */
6884
{
6985
FILE *fh;
7086
char buffer[4096];
71-
struct flock fl = {0};
7287
c_avl_tree_t *tree;
7388
int status;
7489

7590
fh = fopen(h->filename, "r");
7691
if (fh == NULL)
7792
return -1;
7893

79-
fl.l_type = F_RDLCK;
80-
fl.l_whence = SEEK_SET;
81-
/* TODO: Lock file? -> fcntl */
82-
83-
status = fcntl(fileno(fh), F_SETLK, &fl);
94+
status = lock_file(fh);
8495
if (status != 0) {
8596
fclose(fh);
8697
return -1;
@@ -254,4 +265,4 @@ char *fbh_get(fbhash_t *h, const char *key) /* {{{ */
254265
pthread_mutex_unlock(&h->lock);
255266

256267
return value_copy;
257-
} /* }}} char *fbh_get */
268+
} /* }}} char *fbh_get */

0 commit comments

Comments
 (0)