2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "uint128.hpp"
|
|
|
|
#include "address_info.hpp"
|
|
|
|
#include "ip_address.hpp"
|
|
|
|
#include "net_int.hpp"
|
|
|
|
#include "net.h"
|
|
|
|
#include "ip_range.hpp"
|
|
|
|
#include <llarp/util/mem.hpp>
|
|
|
|
#include <llarp/util/bits.hpp>
|
2018-12-12 00:26:37 +00:00
|
|
|
|
|
|
|
#include <functional>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <cstdlib> // for itoa
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <vector>
|
2018-08-08 12:43:21 +00:00
|
|
|
|
2018-09-23 16:45:51 +00:00
|
|
|
// for addrinfo
|
2018-09-25 08:31:29 +00:00
|
|
|
#ifndef _WIN32
|
2018-09-23 16:45:51 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
2018-09-25 08:31:29 +00:00
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2018-11-19 07:55:19 +00:00
|
|
|
#include <wspiapi.h>
|
2018-10-01 02:08:03 +00:00
|
|
|
#define inet_aton(x, y) inet_pton(AF_INET, x, y)
|
2018-09-25 08:31:29 +00:00
|
|
|
#endif
|
2018-09-23 16:45:51 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
operator==(const sockaddr& a, const sockaddr& b);
|
2018-05-18 13:17:58 +00:00
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
bool
|
|
|
|
operator==(const sockaddr_in& a, const sockaddr_in& b);
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const sockaddr_in6& a, const sockaddr_in6& b);
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
operator<(const sockaddr_in6& a, const sockaddr_in6& b);
|
2018-05-18 13:54:15 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
operator<(const in6_addr& a, const in6_addr& b);
|
2018-05-18 13:54:15 +00:00
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
bool
|
|
|
|
operator==(const in6_addr& a, const in6_addr& b);
|
|
|
|
|
2018-10-10 15:14:45 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-10-15 12:02:32 +00:00
|
|
|
bool
|
|
|
|
IsIPv4Bogon(const huint32_t& addr);
|
|
|
|
|
2022-05-20 17:10:04 +00:00
|
|
|
inline bool
|
|
|
|
IsIPv4Bogon(const nuint32_t& addr)
|
|
|
|
{
|
|
|
|
return IsIPv4Bogon(ToHost(addr));
|
|
|
|
}
|
|
|
|
|
2018-10-15 12:02:32 +00:00
|
|
|
bool
|
|
|
|
IsBogon(const in6_addr& addr);
|
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
bool
|
|
|
|
IsBogon(const huint128_t addr);
|
|
|
|
|
2018-10-15 12:02:32 +00:00
|
|
|
bool
|
|
|
|
IsBogonRange(const in6_addr& host, const in6_addr& mask);
|
|
|
|
|
2022-05-20 17:10:04 +00:00
|
|
|
namespace net
|
|
|
|
{
|
2022-07-09 15:05:52 +00:00
|
|
|
/// network platform (all methods virtual so it can be mocked by unit tests)
|
|
|
|
class Platform
|
2022-05-20 17:10:04 +00:00
|
|
|
{
|
2022-07-09 15:05:52 +00:00
|
|
|
public:
|
|
|
|
Platform() = default;
|
|
|
|
virtual ~Platform() = default;
|
|
|
|
Platform(const Platform&) = delete;
|
|
|
|
Platform(Platform&&) = delete;
|
|
|
|
|
|
|
|
/// get a pointer to our signleton instance used by main lokinet
|
|
|
|
/// unit test mocks will not call this
|
|
|
|
static const Platform*
|
|
|
|
Default_ptr();
|
|
|
|
|
|
|
|
virtual std::optional<SockAddr>
|
|
|
|
AllInterfaces(SockAddr pubaddr) const = 0;
|
|
|
|
|
|
|
|
virtual SockAddr
|
|
|
|
Wildcard(int af = AF_INET) const = 0;
|
|
|
|
|
|
|
|
inline SockAddr
|
|
|
|
WildcardWithPort(port_t port, int af = AF_INET) const
|
|
|
|
{
|
|
|
|
auto addr = Wildcard(af);
|
|
|
|
addr.setPort(port);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string
|
|
|
|
LoopbackInterfaceName() const = 0;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
HasInterfaceAddress(ipaddr_t ip) const = 0;
|
|
|
|
|
|
|
|
/// return true if ip is considered a loopback address
|
|
|
|
virtual bool
|
|
|
|
IsLoopbackAddress(ipaddr_t ip) const = 0;
|
|
|
|
|
|
|
|
/// return true if ip is considered a wildcard address
|
|
|
|
virtual bool
|
|
|
|
IsWildcardAddress(ipaddr_t ip) const = 0;
|
|
|
|
|
|
|
|
virtual std::optional<std::string>
|
|
|
|
GetBestNetIF(int af = AF_INET) const = 0;
|
|
|
|
|
|
|
|
inline std::optional<SockAddr>
|
|
|
|
MaybeInferPublicAddr(port_t default_port, int af = AF_INET) const
|
|
|
|
{
|
|
|
|
std::optional<SockAddr> maybe_addr;
|
|
|
|
if (auto maybe_ifname = GetBestNetIF(af))
|
|
|
|
maybe_addr = GetInterfaceAddr(*maybe_ifname, af);
|
|
|
|
|
|
|
|
if (maybe_addr)
|
|
|
|
maybe_addr->setPort(default_port);
|
|
|
|
return maybe_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::optional<IPRange>
|
|
|
|
FindFreeRange() const = 0;
|
|
|
|
|
|
|
|
virtual std::optional<std::string>
|
|
|
|
FindFreeTun() const = 0;
|
|
|
|
|
|
|
|
virtual std::optional<SockAddr>
|
|
|
|
GetInterfaceAddr(std::string_view ifname, int af = AF_INET) const = 0;
|
|
|
|
|
|
|
|
inline std::optional<huint128_t>
|
|
|
|
GetInterfaceIPv6Address(std::string_view ifname) const
|
|
|
|
{
|
|
|
|
if (auto maybe_addr = GetInterfaceAddr(ifname, AF_INET6))
|
|
|
|
return maybe_addr->asIPv6();
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
IsBogon(const SockAddr& addr) const = 0;
|
|
|
|
|
|
|
|
virtual std::optional<int>
|
|
|
|
GetInterfaceIndex(ipaddr_t ip) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace net
|
2022-05-20 17:10:04 +00:00
|
|
|
|
2018-06-20 17:45:44 +00:00
|
|
|
} // namespace llarp
|