libcollectdclient: Implement if_nametoindex() for Windows. ff/win32
authorFlorian Forster <octo@ssc-serv.com>
Thu, 13 Feb 2014 12:31:10 +0000 (13:31 +0100)
committerFlorian Forster <octo@collectd.org>
Thu, 13 Feb 2014 12:31:10 +0000 (13:31 +0100)
The lcc_server_set_interface() function now compiles unaltered.

src/libcollectdclient/network.c

index aa59938..7105774 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <winsock2.h>
 #include <wspiapi.h>
+#include <iphlpapi.h>
 #include <windows.h>
 #include <io.h>
 #include <assert.h>
@@ -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,