diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Network/NetworkInterface.h | 12 | ||||
-rw-r--r-- | Swiften/Network/WindowsNetworkEnvironment.cpp | 47 |
2 files changed, 17 insertions, 42 deletions
diff --git a/Swiften/Network/NetworkInterface.h b/Swiften/Network/NetworkInterface.h index 6395758..1d302cb 100644 --- a/Swiften/Network/NetworkInterface.h +++ b/Swiften/Network/NetworkInterface.h @@ -13,16 +13,7 @@ namespace Swift { class NetworkInterface { public: - enum Type { - Unknown, - WLAN, - Ethernet, - Mobile, - VPN, - }; - - public: - NetworkInterface(const std::string& name, bool loopback, Type type = Unknown) : name(name), loopback(loopback), type(type) { + NetworkInterface(const std::string& name, bool loopback) : name(name), loopback(loopback) { } void addAddress(const HostAddress& address) { @@ -44,7 +35,6 @@ namespace Swift { private: std::string name; bool loopback; - Type type; std::vector<HostAddress> addresses; }; } diff --git a/Swiften/Network/WindowsNetworkEnvironment.cpp b/Swiften/Network/WindowsNetworkEnvironment.cpp index 0eedea0..5df04a7 100644 --- a/Swiften/Network/WindowsNetworkEnvironment.cpp +++ b/Swiften/Network/WindowsNetworkEnvironment.cpp @@ -9,10 +9,7 @@ #include <string> #include <vector> #include <map> - -#include <boost/shared_ptr.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <Swiften/Base/boost_bsignals.h> +#include <boost/optional.hpp> #include <Swiften/Network/HostAddress.h> #include <Swiften/Network/NetworkInterface.h> @@ -21,21 +18,9 @@ namespace Swift { -std::string winSocketAddressToStdString(const SOCKET_ADDRESS& socketAddress) { - char text[46]; - ULONG bufferSize = sizeof(text); - std::string result; - - int ret = WSAAddressToString(socketAddress.lpSockaddr, socketAddress.iSockaddrLength, NULL, text, &bufferSize); - if (ret == 0) { - result.assign(text, sizeof(text)); - } - return result; -} - -std::vector<NetworkInterface> WindowsNetworkEnvironment::getNetworkInterfaces() { +std::vector<NetworkInterface> WindowsNetworkEnvironment::getNetworkInterfaces() const { std::vector<NetworkInterface> result; - std::map<std::string, WindowsNetworkInterface> interfaces; + std::map<std::string,NetworkInterface> interfaces; IP_ADAPTER_ADDRESSES preBuffer[5]; PIP_ADAPTER_ADDRESSES adapterStart = preBuffer; @@ -68,18 +53,18 @@ std::vector<NetworkInterface> WindowsNetworkEnvironment::getNetworkInterfaces() // iterate over addresses for (PIP_ADAPTER_UNICAST_ADDRESS address = adapter->FirstUnicastAddress; address; address = address->Next) { - std::string ip; - - if (address->Address.lpSockaddr->sa_family == PF_INET || - address->Address.lpSockaddr->sa_family == PF_INET6) { - ip = winSocketAddressToStdString(address->Address); - if (!ip.empty()) { - if (interfaces.find(name) == interfaces.end()) { - interfaces[name] = boost::make_shared<WindowsNetworkInterface>(); - interfaces[name]->setName(name); - } - interfaces[name]->addAddress(HostAddress(ip)); - } + boost::optional<HostAddress> hostAddress; + if (address->Address.lpSockaddr->sa_family == PF_INET) { + sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr); + hostAddress = HostAddress(reinterpret_cast<const unsigned char*>(&(sa->sin_addr)), 4); + } + else if (address->Address.lpSockaddr->sa_family == PF_INET6) { + sockaddr_in6* sa = reinterpret_cast<sockaddr_in6*>(address->Address.lpSockaddr); + hostAddress = HostAddress(reinterpret_cast<const unsigned char*>(&(sa->sin6_addr)), 16); + } + if (hostAddress) { + std::map<std::string, NetworkInterface>::iterator i = interfaces.insert(std::make_pair(name, NetworkInterface(name, false))).first; + i->second.addAddress(*hostAddress); } } } @@ -88,7 +73,7 @@ std::vector<NetworkInterface> WindowsNetworkEnvironment::getNetworkInterfaces() //delete adapterStart; } - for (std::map<std::string, WindowsNetworkInterface>::const_iterator i = interfaces.begin(); i != interfaces.end(); ++i) { + for (std::map<std::string,NetworkInterface>::const_iterator i = interfaces.begin(); i != interfaces.end(); ++i) { result.push_back(i->second); } return result; |