Skip to content

Commit 73310af

Browse files
committed
fixed issue with network utils
1 parent b4aa707 commit 73310af

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

Include/Asio/NetworkUtils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <string>
3030
#include <vector>
3131
#include <cstdint>
32+
#include <utility>
3233
#include "Platform/PlatformDefines.h"
3334
#if defined(IS_CPP17)
3435
#include <string_view>
@@ -136,6 +137,8 @@ CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string_view a
136137
CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string const& address, std::string const& subnetMask);
137138
#endif
138139

140+
// Check if 2 addresses (with their subnet masks) are on the same subnet. This is done by checking if the broadcast address
141+
// for the address and subnet mask is the same as the broadcast address for the adapter address and adapter subnet mask.
139142
#if defined(IS_CPP17)
140143
CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string_view ipAddress, std::string_view netmask,
141144
std::string_view adapterAddress,
@@ -147,6 +150,14 @@ CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::s
147150
std::string const& adapterNetmask);
148151
#endif
149152

153+
// Function to look up the IP address and subnet mask for a given adapter name. The adapter name is the friendly name
154+
// of the adapter, e.g. "Ethernet", "Wi-Fi" etc.
155+
#if defined(IS_CPP17)
156+
CORE_LIBRARY_DLL_SHARED_API std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string_view adapterName);
157+
#else
158+
CORE_LIBRARY_DLL_SHARED_API std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string const& adapterName);
159+
#endif
160+
150161

151162
} // namespace core_lib
152163

Source/Asio/NetworkUtils.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string const& ipAddress,
289289
}
290290
}
291291

292-
std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string const& adapterName)
292+
#if defined(IS_CPP17)
293+
std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string_view adapterName);
294+
#else
295+
std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string const& adapterName);
296+
#endif
293297
#if BOOST_OS_LINUX
294298
{
295299
// Create a temporary socket so we can grab our adapter details.
@@ -301,7 +305,7 @@ std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string const& ad
301305

302306
// Set the adapter we want to find info for.
303307
memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
304-
adapterName.copy(ifr.ifr_name, IFNAMSIZ - 1);
308+
string_utils::SafeCopyCharArray(ifr.ifr_name, IFNAMSIZ, adapterName.data(), adapterName.size());
305309

306310
// Use icotl to retrieve IP address.
307311
if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)

0 commit comments

Comments
 (0)