2018-05-18 13:17:58 +00:00
|
|
|
#ifndef LLARP_NET_HPP
|
|
|
|
#define LLARP_NET_HPP
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2020-02-24 17:22:24 +00:00
|
|
|
#include <net/uint128.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <net/address_info.hpp>
|
2020-05-06 20:38:44 +00:00
|
|
|
#include <net/ip_address.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net_int.hpp>
|
|
|
|
#include <net/net.h>
|
2020-06-24 13:24:07 +00:00
|
|
|
#include <net/ip_range.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#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
|
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);
|
|
|
|
|
|
|
|
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
|
2021-02-22 15:01:05 +00:00
|
|
|
AllInterfaces(int af, SockAddr& addr);
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2018-07-27 00:21:57 +00:00
|
|
|
/// get first network interface with public address
|
|
|
|
bool
|
|
|
|
GetBestNetIF(std::string& ifname, int af = AF_INET);
|
|
|
|
|
2018-10-03 10:35:39 +00:00
|
|
|
/// look at adapter ranges and find a free one
|
2020-06-24 13:24:07 +00:00
|
|
|
std::optional<IPRange>
|
2019-07-03 14:32:51 +00:00
|
|
|
FindFreeRange();
|
2018-10-03 10:35:39 +00:00
|
|
|
|
|
|
|
/// look at adapter names and find a free one
|
2020-05-04 16:51:57 +00:00
|
|
|
std::optional<std::string>
|
2019-07-03 14:32:51 +00:00
|
|
|
FindFreeTun();
|
2018-10-03 10:35:39 +00:00
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
/// get network interface address for network interface with ifname
|
2021-02-22 15:01:05 +00:00
|
|
|
std::optional<SockAddr>
|
2021-02-17 11:37:21 +00:00
|
|
|
GetInterfaceAddr(const std::string& ifname, int af = AF_INET);
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2021-02-16 15:59:18 +00:00
|
|
|
/// get an interface's ip6 address
|
|
|
|
std::optional<huint128_t>
|
2021-02-17 11:37:21 +00:00
|
|
|
GetInterfaceIPv6Address(std::string ifname);
|
2021-02-16 15:59:18 +00:00
|
|
|
|
2021-02-16 20:01:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
namespace net
|
|
|
|
{
|
|
|
|
std::optional<int>
|
2021-02-17 11:37:21 +00:00
|
|
|
GetInterfaceIndex(huint32_t ip);
|
2021-02-16 20:01:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|