1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

NetworkInterface::name now works on Windows.

This commit is contained in:
Matias Fontanini
2014-09-07 16:40:50 -03:00
parent e00d6aaa7e
commit 9d4bdce7a9
2 changed files with 19 additions and 2 deletions

View File

@@ -106,7 +106,11 @@ public:
/**
* \brief Retrieves this interface's name.
*
*
* This name can be used as the interface name provided to the
* Sniffer class when starting a sniffing session.
*
* \sa Sniffer
* \return std::string containing this interface's name.
*/
std::string name() const;

View File

@@ -43,6 +43,7 @@
#include <net/if.h>
#else
#include <winsock2.h>
#include <Iphlpapi.h>
#endif
#include "network_interface.h"
#include "utils.h"
@@ -169,7 +170,19 @@ std::string NetworkInterface::name() const {
throw std::runtime_error("Error fetching this interface's name");
return iface_name;
#else // WIN32
return "Not implemented";
ULONG size;
::GetAdaptersAddresses(AF_INET, 0, 0, 0, &size);
std::vector<uint8_t> buffer(size);
if (::GetAdaptersAddresses(AF_INET, 0, 0, (IP_ADAPTER_ADDRESSES *)&buffer[0], &size) == ERROR_SUCCESS) {
PIP_ADAPTER_ADDRESSES iface = (IP_ADAPTER_ADDRESSES *)&buffer[0];
while (iface) {
if (iface->IfIndex == iface_id) {
return std::string("\\Device\\NPF_") + iface->AdapterName;
}
iface = iface->Next;
}
}
throw std::runtime_error("Failed to find interface name");
#endif // WIN32
}