26#ifndef MYSQL_HARNESS_NET_TS_IMPL_NETIF_H_
27#define MYSQL_HARNESS_NET_TS_IMPL_NETIF_H_
30#include <forward_list>
35#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) || \
48#include <netinet/in.h>
54#pragma comment(lib, "iphlpapi.lib")
73 using value_type =
decltype(IP_ADAPTER_ADDRESSES::Flags);
89template <
class NetworkT>
114 template <
class... Args>
116 return nets_.emplace_back(std::forward<Args>(args)...);
136 std::string
id()
const {
return id_; }
205 template <
class BytesClass>
206 static constexpr int get_prefix_len(
const BytesClass &
mask) {
211 for (
size_t ndx{}; ndx <
mask.size(); ++ndx) {
212 uint8_t mask_byte =
mask[ndx];
214 for (uint8_t b = mask_byte; b & 0x80; b <<= 1, ++prefix_len)
218 if (mask_byte != 0xff)
break;
226 struct scoped_ifaddrs {
227 constexpr scoped_ifaddrs(ifaddrs *captured_ifaddrs)
228 : ifaddrs_{captured_ifaddrs} {}
230 if (ifaddrs_) ::freeifaddrs(ifaddrs_);
235 auto tail =
results_.before_begin();
249 for (
auto cur = ifs; cur !=
nullptr; cur = cur->ifa_next) {
252 [&cur](
const auto &v) {
253 return v.id() == cur->ifa_name;
256 tail =
results_.emplace_after(tail, cur->ifa_name, cur->ifa_name,
261 auto cur_res_it = std::find_if(
263 [&cur](
const auto &v) { return v.id() == cur->ifa_name; });
267 switch (cur->ifa_addr->sa_family) {
269 auto *sa =
reinterpret_cast<const sockaddr_in *
>(cur->ifa_addr);
273 std::memcpy(bytes.data(), &(sa->sin_addr),
sizeof(sa->sin_addr));
277 sa =
reinterpret_cast<const sockaddr_in *
>(cur->ifa_netmask);
279 std::memcpy(bytes.data(), &(sa->sin_addr.s_addr),
280 sizeof(sa->sin_addr.s_addr));
283 auto prefix_len = get_prefix_len(netmask.to_bytes());
284 cur_res_it->v4_networks().emplace_back(addr, prefix_len);
288 0x80, 0x00, 0x00, 0x00)) == 1,
291 0xff, 0x00, 0x00, 0x00)) == 8,
294 0xff, 0x80, 0x00, 0x00)) == 9,
299 0x00, 0x80, 0x00, 0x00)) == 0,
305 auto *sa =
reinterpret_cast<const sockaddr_in6 *
>(cur->ifa_addr);
308 if (bytes.size() <
sizeof(sa->sin6_addr.s6_addr))
std::terminate();
310 std::memcpy(bytes.data(), &(sa->sin6_addr.s6_addr),
311 sizeof(sa->sin6_addr.s6_addr));
314 sa =
reinterpret_cast<const sockaddr_in6 *
>(cur->ifa_netmask);
315 if (bytes.size() <
sizeof(sa->sin6_addr.s6_addr))
std::terminate();
316 std::memcpy(bytes.data(), &(sa->sin6_addr.s6_addr),
317 sizeof(sa->sin6_addr.s6_addr));
320 auto prefix_len = get_prefix_len(netmask.to_bytes());
321 cur_res_it->v6_networks().emplace_back(addr, prefix_len);
335 const std::wstring_view &ws) {
340 WideCharToMultiByte(CP_UTF8, 0, ws.data(), ws.size(),
nullptr, 0, 0, 0);
344 std::error_code(GetLastError(), std::system_category()));
349 out_len = WideCharToMultiByte(CP_UTF8, 0, ws.data(), ws.size(),
350 &out.front(), out.capacity(), 0, 0);
353 std::error_code(GetLastError(), std::system_category()));
363 auto tail =
results_.before_begin();
365 for (
auto cur = ifs.get(); cur; cur = cur->Next) {
366 tail =
results_.emplace_after(tail, std::string{cur->AdapterName},
367 convert_wstring_to_utf8(cur->Description)
368 .value_or(
"<invalid-wstring>"),
372 auto cur_res_it = std::find_if(
374 [&cur](
const auto &v) { return v.id() == cur->AdapterName; });
376 for (
auto cur_unicast_addr = cur->FirstUnicastAddress; cur_unicast_addr;
377 cur_unicast_addr = cur_unicast_addr->Next) {
378 if (cur_unicast_addr->Address.lpSockaddr->sa_family == AF_INET) {
379 auto *sa =
reinterpret_cast<const sockaddr_in *
>(
380 cur_unicast_addr->Address.lpSockaddr);
382 std::memcpy(bytes.data(), &(sa->sin_addr.s_addr),
383 sizeof(sa->sin_addr.s_addr));
386 cur_res_it->v4_networks().emplace_back(
387 addr, cur_unicast_addr->OnLinkPrefixLength);
389 auto *sa =
reinterpret_cast<const sockaddr_in6 *
>(
390 cur_unicast_addr->Address.lpSockaddr);
392 std::memcpy(bytes.data(), &(sa->sin6_addr.s6_addr),
393 sizeof(sa->sin6_addr.s6_addr));
395 cur_res_it->v6_networks().emplace_back(
396 addr, cur_unicast_addr->OnLinkPrefixLength);
411 unsigned long ifs_size{0};
413 ::GetAdaptersAddresses(AF_UNSPEC, 0,
nullptr,
nullptr, &ifs_size);
415 if (res != ERROR_BUFFER_OVERFLOW) {
417 std::error_code{
static_cast<int>(res), std::system_category()});
421 reinterpret_cast<IP_ADAPTER_ADDRESSES *
>(
malloc(ifs_size)), &
free);
423 res = ::GetAdaptersAddresses(AF_UNSPEC, 0,
nullptr, ifs.get(), &ifs_size);
424 if (ERROR_SUCCESS != res) {
426 std::error_code{
static_cast<int>(res), std::system_category()});
431 ifaddrs *ifs =
nullptr;
433 if (-1 == ::getifaddrs(&ifs)) {
flags of the network interface.
Definition: netif.h:66
constexpr value_type value() const
Definition: netif.h:80
constexpr InterfaceFlag(value_type v) noexcept
Definition: netif.h:78
const value_type v_
Definition: netif.h:83
decltype(ifaddrs::ifa_flags) value_type
Definition: netif.h:75
an entry in the network interface result.
Definition: netif.h:126
NetworkInterfaceNetworks< net::ip::network_v6 > & v6_networks()
Definition: netif.h:148
const NetworkInterfaceNetworks< net::ip::network_v4 > & v4_networks() const
Definition: netif.h:144
NetworkInterfaceNetworks< net::ip::network_v6 > net_v6_s_
Definition: netif.h:161
std::string id_
Definition: netif.h:157
flags_type flags() const
Definition: netif.h:138
const NetworkInterfaceNetworks< net::ip::network_v6 > & v6_networks() const
Definition: netif.h:152
std::string display_name_
Definition: netif.h:158
NetworkInterfaceEntry(std::string id, std::string display_name, flags_type::value_type flags)
Definition: netif.h:130
NetworkInterfaceNetworks< net::ip::network_v4 > net_v4_s_
Definition: netif.h:160
flags_type flags_
Definition: netif.h:159
std::string display_name() const
Definition: netif.h:137
NetworkInterfaceNetworks< net::ip::network_v4 > & v4_networks()
Definition: netif.h:140
std::string id() const
Definition: netif.h:136
networks of a network interface.
Definition: netif.h:90
std::list< value_type > container_type
Definition: netif.h:93
const_iterator end() const
Definition: netif.h:105
typename container_type::const_iterator const_iterator
Definition: netif.h:96
ptrdiff_t difference_type
Definition: netif.h:98
void push_back(const value_type &v)
Definition: netif.h:107
container_type nets_
Definition: netif.h:120
const value_type & const_reference
Definition: netif.h:94
value_type & reference
Definition: netif.h:95
const_iterator iterator
Definition: netif.h:97
NetworkT value_type
Definition: netif.h:92
size_t size_type
Definition: netif.h:99
bool empty() const noexcept
Definition: netif.h:102
const_iterator begin() const
Definition: netif.h:104
size_type max_size() const noexcept
Definition: netif.h:101
auto emplace_back(Args &&...args)
emplace an object in the container.
Definition: netif.h:115
stdx::expected< NetworkInterfaceResults, std::error_code > query()
Definition: netif.h:409
results of a NetworkInterfaceResolver::query().
Definition: netif.h:167
typename std::forward_list< value_type >::const_iterator const_iterator
Definition: netif.h:172
const_iterator cend() const
Definition: netif.h:186
const_iterator cbegin() const
Definition: netif.h:185
size_type size() const noexcept
Definition: netif.h:179
bool empty() const noexcept
Definition: netif.h:181
const_iterator begin() const
Definition: netif.h:183
const_iterator iterator
Definition: netif.h:173
ptrdiff_t difference_type
Definition: netif.h:174
size_type max_size() const noexcept
Definition: netif.h:180
const_iterator end() const
Definition: netif.h:184
size_t size_type
Definition: netif.h:175
std::forward_list< value_type > results_
Definition: netif.h:403
NetworkInterfaceResults()=default
size_t size_
Definition: netif.h:404
Definition: internet.h:93
IPv6 address with scope_id.
Definition: internet.h:182
Definition: expected.h:944
#define malloc(A)
Definition: lexyy.cc:914
#define free(A)
Definition: lexyy.cc:915
static mi_bit_type mask[]
Definition: mi_packrec.cc:141
bool terminate(THD *thd)
Drop all DD tables in case there is an error while upgrading server.
Definition: upgrade.cc:686
std::error_code last_error_code()
get last std::error_code for socket-errors.
Definition: socket_error.h:107
Definition: gcs_xcom_synode.h:64
constexpr auto make_unexpected(E &&e) -> unexpected< std::decay_t< E > >
Definition: expected.h:125
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr
The following is a common type that is returned by all the ut::make_unique (non-aligned) specializati...
Definition: ut0new.h:2439
Definition: internet.h:96
Definition: internet.h:184