From: Florian Forster Date: Thu, 13 Feb 2014 12:31:10 +0000 (+0100) Subject: libcollectdclient: Implement if_nametoindex() for Windows. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=96804ef69033c0d9d8ebe4cd122105140161a536;p=collectd.git libcollectdclient: Implement if_nametoindex() for Windows. The lcc_server_set_interface() function now compiles unaltered. --- diff --git a/src/libcollectdclient/network.c b/src/libcollectdclient/network.c index aa599384..7105774e 100644 --- a/src/libcollectdclient/network.c +++ b/src/libcollectdclient/network.c @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -398,11 +399,42 @@ int lcc_server_set_ttl (lcc_server_t *srv, uint8_t ttl) /* {{{ */ } /* }}} int lcc_server_set_ttl */ #if WIN32 -int lcc_server_set_interface (lcc_server_t *srv, char const *ifname) /* {{{ */ +static unsigned if_nametoindex (LPCSTR pszIfName) /* {{{ */ { - return (-1); -} /* }}} int lcc_server_set_interface */ -#else + IP_ADAPTER_ADDRESSES *pAddrList; + IP_ADAPTER_ADDRESSES *pAddrPtr; + BYTE bBuffer[8192]; + ULONG dwSize; + ULONG dwStatus; + unsigned dwIndex = 0; + + dwSize = (ULONG) sizeof (bBuffer); + pAddrList = (IP_ADAPTER_ADDRESSES *) &bBuffer[0]; + + dwStatus = GetAdaptersAddresses ( + /* Family = */ AF_UNSPEC, + /* Flags = */ GAA_FLAG_SKIP_ANYCAST, + /* Reserved = */ NULL, + /* AdapterAddresses = */ pAddrList, + /* SizePointer = */ &dwSize); + if (dwStatus != ERROR_SUCCESS) + return (0); + + for (pAddrPtr = pAddrList; + pAddrPtr != NULL; + pAddrPtr = pAddrPtr->Next) + { + if (strcmp (pszIfName, pAddrPtr->AdapterName) != 0) + continue; + + dwIndex = (unsigned) pAddrPtr->IfIndex; + break; + } + + return (dwIndex); +} /* }}} unsigned if_nametoindex */ +#endif /* WIN32 */ + int lcc_server_set_interface (lcc_server_t *srv, char const *ifname) /* {{{ */ { int if_index; @@ -477,7 +509,6 @@ int lcc_server_set_interface (lcc_server_t *srv, char const *ifname) /* {{{ */ return (0); } /* }}} int lcc_server_set_interface */ -#endif /* !WIN32 */ int lcc_server_set_security_level (lcc_server_t *srv, /* {{{ */ lcc_security_level_t level,