lokinet/llarp/net/net.hpp

125 lines
2.9 KiB
C++
Raw Normal View History

2018-05-18 13:17:58 +00:00
#ifndef LLARP_NET_HPP
#define LLARP_NET_HPP
2020-02-24 17:22:24 +00:00
#include <net/uint128.hpp>
#include <net/address_info.hpp>
2020-05-06 20:38:44 +00:00
#include <net/ip_address.hpp>
#include <net/net_int.hpp>
#include <net/net.h>
#include <util/mem.hpp>
2019-02-08 19:43:25 +00:00
#include <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
#include <vector>
// for addrinfo
2018-09-25 08:31:29 +00:00
#ifndef _WIN32
#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>
#include <wspiapi.h>
#define inet_aton(x, y) inet_pton(AF_INET, x, y)
2018-09-25 08:31:29 +00:00
#endif
bool
operator==(const sockaddr& a, const sockaddr& b);
2018-05-18 13:17:58 +00:00
bool
operator==(const sockaddr_in& a, const sockaddr_in& b);
bool
operator==(const sockaddr_in6& a, const sockaddr_in6& b);
bool
operator<(const sockaddr_in6& a, const sockaddr_in6& b);
2018-05-18 13:54:15 +00:00
bool
operator<(const in6_addr& a, const in6_addr& b);
2018-05-18 13:54:15 +00:00
bool
operator==(const in6_addr& a, const in6_addr& b);
2018-10-10 15:14:45 +00:00
namespace llarp
{
2019-06-11 16:44:05 +00:00
/// get a netmask with the higest numset bits set
constexpr huint128_t
_netmask_ipv6_bits(uint32_t numset)
2019-06-11 16:44:05 +00:00
{
return (128 - numset) ? (huint128_t{1} << numset) | _netmask_ipv6_bits(numset + 1)
: huint128_t{0};
2019-06-11 16:44:05 +00:00
}
constexpr huint128_t
netmask_ipv6_bits(uint32_t numset)
{
return _netmask_ipv6_bits(128 - numset);
2019-06-11 16:44:05 +00:00
}
2018-10-16 15:25:50 +00:00
/// get a netmask with the higest numset bits set
constexpr uint32_t
_netmask_ipv4_bits(uint32_t numset)
2018-10-16 15:25:50 +00:00
{
return (32 - numset) ? (1 << numset) | _netmask_ipv4_bits(numset + 1) : 0;
2018-10-16 15:25:50 +00:00
}
2019-06-11 16:44:05 +00:00
/// get a netmask given some /N range
2018-10-15 12:02:32 +00:00
constexpr huint32_t
2018-10-16 15:25:50 +00:00
netmask_ipv4_bits(uint32_t num)
2018-10-15 12:02:32 +00:00
{
return huint32_t{_netmask_ipv4_bits(32 - num)};
2018-10-15 12:02:32 +00:00
}
constexpr huint32_t
ipaddr_ipv4_bits(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
{
2018-11-30 14:42:05 +00:00
return huint32_t{(d) | (c << 8) | (b << 16) | (a << 24)};
2018-10-15 12:02:32 +00:00
}
bool
IsIPv4Bogon(const huint32_t& addr);
constexpr bool
ipv6_is_siit(const in6_addr& addr)
{
return addr.s6_addr[11] == 0xff && addr.s6_addr[10] == 0xff && addr.s6_addr[9] == 0
&& addr.s6_addr[8] == 0 && addr.s6_addr[7] == 0 && addr.s6_addr[6] == 0
&& addr.s6_addr[5] == 0 && addr.s6_addr[4] == 0 && addr.s6_addr[3] == 0
&& addr.s6_addr[2] == 0 && addr.s6_addr[1] == 0 && addr.s6_addr[0] == 0;
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);
2018-09-03 12:03:43 +00:00
bool
2020-05-06 20:38:44 +00:00
AllInterfaces(int af, IpAddress& addr);
/// get first network interface with public address
bool
GetBestNetIF(std::string& ifname, int af = AF_INET);
/// look at adapter ranges and find a free one
2020-05-04 16:51:57 +00:00
std::optional<std::string>
FindFreeRange();
/// look at adapter names and find a free one
2020-05-04 16:51:57 +00:00
std::optional<std::string>
FindFreeTun();
/// get network interface address for network interface with ifname
2020-05-06 20:38:44 +00:00
std::optional<IpAddress>
2020-05-04 18:39:14 +00:00
GetIFAddr(const std::string& ifname, int af = AF_INET);
2018-06-20 17:45:44 +00:00
} // namespace llarp
2018-05-18 13:54:15 +00:00
2018-05-18 13:17:58 +00:00
#endif