Skip to content

Commit

Permalink
Return IPv6 addresses when scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
fgo-ableton committed Sep 19, 2023
1 parent d42f1f7 commit e55789a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
41 changes: 29 additions & 12 deletions include/ableton/platforms/posix/ScanIpIfAddrs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <ableton/discovery/AsioTypes.hpp>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <map>
#include <net/if.h>
#include <string>
#include <vector>

namespace ableton
Expand Down Expand Up @@ -77,31 +79,46 @@ struct ScanIpIfAddrs
std::vector<discovery::IpAddress> operator()()
{
std::vector<discovery::IpAddress> addrs;
std::map<std::string, discovery::IpAddress> IpInterfaceNames;

detail::GetIfAddrs getIfAddrs;
getIfAddrs.withIfAddrs([&addrs](const struct ifaddrs& interfaces) {
getIfAddrs.withIfAddrs([&addrs, &IpInterfaceNames](const struct ifaddrs& interfaces) {
const struct ifaddrs* interface;
for (interface = &interfaces; interface; interface = interface->ifa_next)
{
auto addr = reinterpret_cast<const struct sockaddr_in*>(interface->ifa_addr);
if (addr && interface->ifa_flags & IFF_UP)
if (addr && interface->ifa_flags & IFF_RUNNING && addr->sin_family == AF_INET)
{
if (addr->sin_family == AF_INET)
{
auto bytes = reinterpret_cast<const char*>(&addr->sin_addr);
addrs.emplace_back(discovery::makeAddress<discovery::IpAddressV4>(bytes));
}
else if (addr->sin_family == AF_INET6)
auto bytes = reinterpret_cast<const char*>(&addr->sin_addr);
auto address = discovery::makeAddress<discovery::IpAddressV4>(bytes);
addrs.emplace_back(std::move(address));
IpInterfaceNames.insert(std::make_pair(interface->ifa_name, address));
}
}
});

getIfAddrs.withIfAddrs([&addrs, &IpInterfaceNames](const struct ifaddrs& interfaces) {
const struct ifaddrs* interface;
for (interface = &interfaces; interface; interface = interface->ifa_next)
{
auto addr = reinterpret_cast<const struct sockaddr_in*>(interface->ifa_addr);
if (addr && interface->ifa_flags & IFF_RUNNING && addr->sin_family == AF_INET6)
{
auto addr6 = reinterpret_cast<const struct sockaddr_in6*>(addr);
auto bytes = reinterpret_cast<const char*>(&addr6->sin6_addr);
auto scopeId = addr6->sin6_scope_id;
auto address = discovery::makeAddress<discovery::IpAddressV6>(bytes, scopeId);
if (IpInterfaceNames.find(interface->ifa_name) != IpInterfaceNames.end()
&& !address.is_loopback() && address.is_link_local())
{
auto addr6 = reinterpret_cast<const struct sockaddr_in6*>(addr);
auto bytes = reinterpret_cast<const char*>(&addr6->sin6_addr);
addrs.emplace_back(discovery::makeAddress<discovery::IpAddressV6>(bytes));
addrs.emplace_back(std::move(address));
}
}
}
});

return addrs;
}
};
};

} // namespace posix
Expand Down
38 changes: 32 additions & 6 deletions include/ableton/platforms/windows/ScanIpIfAddrs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

#include <ableton/discovery/AsioTypes.hpp>
#include <iphlpapi.h>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
#include <winsock2.h>
#include <ws2tcpip.h>
Expand Down Expand Up @@ -101,9 +103,10 @@ struct ScanIpIfAddrs
std::vector<discovery::IpAddress> operator()()
{
std::vector<discovery::IpAddress> addrs;
std::map<std::string, discovery::IpAddress> IpInterfaceNames;

detail::GetIfAddrs getIfAddrs;
getIfAddrs.withIfAddrs([&addrs](const IP_ADAPTER_ADDRESSES& interfaces) {
getIfAddrs.withIfAddrs([&](const IP_ADAPTER_ADDRESSES& interfaces) {
const IP_ADAPTER_ADDRESSES* networkInterface;
for (networkInterface = &interfaces; networkInterface;
networkInterface = networkInterface->Next)
Expand All @@ -118,18 +121,41 @@ struct ScanIpIfAddrs
SOCKADDR_IN* addr4 =
reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
auto bytes = reinterpret_cast<const char*>(&addr4->sin_addr);
addrs.emplace_back(discovery::makeAddress<discovery::IpAddressV4>(bytes));
auto ipv4address = discovery::makeAddress<discovery::IpAddressV4>(bytes);
addrs.emplace_back(ipv4address);
IpInterfaceNames.insert(
std::make_pair(networkInterface->AdapterName, ipv4address));
}
else if (AF_INET6 == family)
}
}
});

getIfAddrs.withIfAddrs([&](const IP_ADAPTER_ADDRESSES& interfaces) {
const IP_ADAPTER_ADDRESSES* networkInterface;
for (networkInterface = &interfaces; networkInterface;
networkInterface = networkInterface->Next)
{
for (IP_ADAPTER_UNICAST_ADDRESS* address = networkInterface->FirstUnicastAddress;
NULL != address; address = address->Next)
{
auto family = address->Address.lpSockaddr->sa_family;
if (AF_INET6 == family
&& IpInterfaceNames.find(networkInterface->AdapterName)
!= IpInterfaceNames.end())
{
SOCKADDR_IN6* addr6 =
SOCKADDR_IN6* sockAddr =
reinterpret_cast<SOCKADDR_IN6*>(address->Address.lpSockaddr);
auto bytes = reinterpret_cast<const char*>(&addr6->sin6_addr);
addrs.emplace_back(discovery::makeAddress<discovery::IpAddressV6>(bytes));
auto bytes = reinterpret_cast<const char*>(&sockAddr->sin6_addr);
auto addr6 = discovery::makeAddress<discovery::IpAddressV6>(bytes);
if (!addr6.is_loopback() && addr6.is_link_local())
{
addrs.emplace_back(addr6);
}
}
}
}
});

return addrs;
}
};
Expand Down

0 comments on commit e55789a

Please sign in to comment.