2018-05-18 13:17:58 +00:00
|
|
|
#ifndef LLARP_NET_HPP
|
|
|
|
#define LLARP_NET_HPP
|
2018-08-30 18:48:43 +00:00
|
|
|
#include <llarp/address_info.hpp>
|
2018-05-18 13:17:58 +00:00
|
|
|
#include <llarp/net.h>
|
2018-07-27 00:21:57 +00:00
|
|
|
#include <functional>
|
2018-06-01 17:47:37 +00:00
|
|
|
#include <iostream>
|
2018-08-08 12:43:21 +00:00
|
|
|
#include "logger.hpp"
|
2018-05-23 13:49:00 +00:00
|
|
|
#include "mem.hpp"
|
2018-11-01 12:47:14 +00:00
|
|
|
#include <llarp/string_view.hpp>
|
2018-10-15 12:02:32 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-08-08 12:43:21 +00:00
|
|
|
#include <stdlib.h> // for itoa
|
|
|
|
|
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>
|
|
|
|
#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
|
|
|
|
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-08-08 12:43:21 +00:00
|
|
|
struct privatesInUse
|
|
|
|
{
|
2018-10-09 11:02:06 +00:00
|
|
|
// true if used by real NICs on start
|
|
|
|
// false if not used, and means we could potentially use it if needed
|
2018-08-09 11:28:55 +00:00
|
|
|
bool ten; // 16m ips
|
|
|
|
bool oneSeven; // 1m ips
|
|
|
|
bool oneNine; // 65k ips
|
2018-08-08 12:43:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct privatesInUse
|
|
|
|
llarp_getPrivateIfs();
|
|
|
|
|
2018-10-10 15:14:45 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
// clang-format off
|
|
|
|
|
|
|
|
struct huint32_t
|
|
|
|
{
|
|
|
|
uint32_t h;
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
constexpr huint32_t
|
|
|
|
operator &(huint32_t x) const { return huint32_t{uint32_t(h & x.h)}; }
|
|
|
|
constexpr huint32_t
|
|
|
|
operator |(huint32_t x) const { return huint32_t{uint32_t(h | x.h)}; }
|
2018-10-15 12:02:32 +00:00
|
|
|
constexpr huint32_t
|
|
|
|
operator ^(huint32_t x) const { return huint32_t{uint32_t(h ^ x.h)}; }
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
constexpr huint32_t
|
|
|
|
operator ~() const { return huint32_t{uint32_t(~h)}; }
|
|
|
|
|
2018-10-10 15:14:45 +00:00
|
|
|
inline huint32_t operator ++() { ++h; return *this; }
|
|
|
|
inline huint32_t operator --() { --h; return *this; }
|
2018-10-12 04:35:10 +00:00
|
|
|
|
2018-10-10 15:14:45 +00:00
|
|
|
constexpr bool operator <(huint32_t x) const { return h < x.h; }
|
|
|
|
constexpr bool operator ==(huint32_t x) const { return h == x.h; }
|
2018-10-19 14:53:06 +00:00
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const huint32_t& a)
|
|
|
|
{
|
2018-11-05 11:36:49 +00:00
|
|
|
uint32_t n = htonl(a.h);
|
2018-11-03 13:19:18 +00:00
|
|
|
char tmp[INET_ADDRSTRLEN] = {0};
|
2018-11-05 11:36:49 +00:00
|
|
|
if(inet_ntop(AF_INET, (void*)&n, tmp, sizeof(tmp)))
|
2018-11-03 13:19:18 +00:00
|
|
|
{
|
|
|
|
out << tmp;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:50:52 +00:00
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
inline size_t
|
2018-10-12 04:35:10 +00:00
|
|
|
operator ()(huint32_t x) const
|
2018-10-10 15:50:52 +00:00
|
|
|
{
|
|
|
|
return std::hash< uint32_t >{}(x.h);
|
|
|
|
}
|
|
|
|
};
|
2018-10-10 15:14:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nuint32_t
|
|
|
|
{
|
|
|
|
uint32_t n;
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
constexpr nuint32_t
|
|
|
|
operator &(nuint32_t x) const { return nuint32_t{uint32_t(n & x.n)}; }
|
|
|
|
constexpr nuint32_t
|
|
|
|
operator |(nuint32_t x) const { return nuint32_t{uint32_t(n | x.n)}; }
|
2018-10-15 12:02:32 +00:00
|
|
|
constexpr nuint32_t
|
|
|
|
operator ^(nuint32_t x) const { return nuint32_t{uint32_t(n ^ x.n)}; }
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
constexpr nuint32_t
|
|
|
|
operator ~() const { return nuint32_t{uint32_t(~n)}; }
|
|
|
|
|
2018-10-10 15:14:45 +00:00
|
|
|
inline nuint32_t operator ++() { ++n; return *this; }
|
|
|
|
inline nuint32_t operator --() { --n; return *this; }
|
2018-10-12 04:35:10 +00:00
|
|
|
|
2018-10-10 15:14:45 +00:00
|
|
|
constexpr bool operator <(nuint32_t x) const { return n < x.n; }
|
|
|
|
constexpr bool operator ==(nuint32_t x) const { return n == x.n; }
|
2018-10-10 15:50:52 +00:00
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const nuint32_t& a)
|
|
|
|
{
|
|
|
|
char tmp[INET_ADDRSTRLEN] = {0};
|
2018-11-05 11:36:49 +00:00
|
|
|
if(inet_ntop(AF_INET, (void*)&a.n, tmp, sizeof(tmp)))
|
2018-11-03 13:19:18 +00:00
|
|
|
{
|
|
|
|
out << tmp;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:50:52 +00:00
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
inline size_t
|
2018-10-12 04:35:10 +00:00
|
|
|
operator ()(nuint32_t x) const
|
2018-10-10 15:50:52 +00:00
|
|
|
{
|
|
|
|
return std::hash< uint32_t >{}(x.n);
|
|
|
|
}
|
|
|
|
};
|
2018-10-10 15:14:45 +00:00
|
|
|
};
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
struct huint16_t
|
|
|
|
{
|
|
|
|
uint16_t h;
|
|
|
|
|
|
|
|
constexpr huint16_t
|
|
|
|
operator &(huint16_t x) const { return huint16_t{uint16_t(h & x.h)}; }
|
|
|
|
constexpr huint16_t
|
|
|
|
operator |(huint16_t x) const { return huint16_t{uint16_t(h | x.h)}; }
|
|
|
|
constexpr huint16_t
|
|
|
|
operator ~() const { return huint16_t{uint16_t(~h)}; }
|
|
|
|
|
|
|
|
inline huint16_t operator ++() { ++h; return *this; }
|
|
|
|
inline huint16_t operator --() { --h; return *this; }
|
|
|
|
|
|
|
|
constexpr bool operator <(huint16_t x) const { return h < x.h; }
|
|
|
|
constexpr bool operator ==(huint16_t x) const { return h == x.h; }
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const huint16_t& a)
|
|
|
|
{
|
2018-11-05 11:37:17 +00:00
|
|
|
return out << a.h;
|
2018-11-03 13:19:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
inline size_t
|
|
|
|
operator ()(huint16_t x) const
|
|
|
|
{
|
|
|
|
return std::hash< uint16_t >{}(x.h);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nuint16_t
|
|
|
|
{
|
|
|
|
uint16_t n;
|
|
|
|
|
|
|
|
constexpr nuint16_t
|
|
|
|
operator &(nuint16_t x) const { return nuint16_t{uint16_t(n & x.n)}; }
|
|
|
|
constexpr nuint16_t
|
|
|
|
operator |(nuint16_t x) const { return nuint16_t{uint16_t(n | x.n)}; }
|
|
|
|
constexpr nuint16_t
|
|
|
|
operator ~() const { return nuint16_t{uint16_t(~n)}; }
|
|
|
|
|
|
|
|
inline nuint16_t operator ++() { ++n; return *this; }
|
|
|
|
inline nuint16_t operator --() { --n; return *this; }
|
|
|
|
|
|
|
|
constexpr bool operator <(nuint16_t x) const { return n < x.n; }
|
|
|
|
constexpr bool operator ==(nuint16_t x) const { return n == x.n; }
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const nuint16_t& a)
|
|
|
|
{
|
2018-11-05 11:36:49 +00:00
|
|
|
return out << ntohs(a.n);
|
2018-11-03 13:19:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
inline size_t
|
|
|
|
operator ()(nuint16_t x) const
|
|
|
|
{
|
|
|
|
return std::hash< uint16_t >{}(x.n);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-10-10 15:14:45 +00:00
|
|
|
// clang-format on
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
static inline nuint32_t
|
|
|
|
xhtonl(huint32_t x)
|
|
|
|
{
|
|
|
|
return nuint32_t{htonl(x.h)};
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:14:45 +00:00
|
|
|
static inline huint32_t
|
|
|
|
xntohl(nuint32_t x)
|
|
|
|
{
|
|
|
|
return huint32_t{ntohl(x.n)};
|
|
|
|
}
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
static inline nuint16_t
|
|
|
|
xhtons(huint16_t x)
|
2018-10-10 15:14:45 +00:00
|
|
|
{
|
2018-10-12 04:35:10 +00:00
|
|
|
return nuint16_t{htons(x.h)};
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline huint16_t
|
|
|
|
xntohs(nuint16_t x)
|
|
|
|
{
|
|
|
|
return huint16_t{ntohs(x.n)};
|
2018-10-10 15:14:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 12:02:32 +00:00
|
|
|
struct IPRange
|
|
|
|
{
|
|
|
|
huint32_t addr;
|
|
|
|
huint32_t netmask_bits;
|
|
|
|
|
|
|
|
/// return true if ip is contained in this ip range
|
|
|
|
bool
|
|
|
|
Contains(const huint32_t& ip) const
|
|
|
|
{
|
|
|
|
// TODO: do this "better"
|
2018-10-16 11:10:27 +00:00
|
|
|
return (addr & netmask_bits) == (ip & netmask_bits);
|
2018-10-15 12:02:32 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const IPRange& a)
|
2018-10-15 12:02:32 +00:00
|
|
|
{
|
|
|
|
char strbuf[32] = {0};
|
|
|
|
char netbuf[32] = {0};
|
2018-11-09 14:48:43 +00:00
|
|
|
inet_ntop(AF_INET, (void*)&a.addr, strbuf, sizeof(strbuf));
|
|
|
|
inet_ntop(AF_INET, (void*)&a.netmask_bits, netbuf, sizeof(netbuf));
|
2018-11-03 13:19:18 +00:00
|
|
|
out << std::string(strbuf) + "/" + std::string(netbuf);
|
|
|
|
return out;
|
2018-10-15 12:02:32 +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)
|
|
|
|
{
|
|
|
|
return (32 - numset)
|
|
|
|
? (1 << (32 - (numset + 1))) | __netmask_ipv4_bits(numset + 1)
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get an ipv4 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
|
|
|
{
|
2018-10-16 15:25:50 +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-10-16 15:25:50 +00:00
|
|
|
return huint32_t{(a) | (b << 8) | (c << 16) | (d << 24)};
|
2018-10-15 12:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr IPRange
|
|
|
|
iprange_ipv4(byte_t a, byte_t b, byte_t c, byte_t d, byte_t mask)
|
|
|
|
{
|
|
|
|
return IPRange{ipaddr_ipv4_bits(a, b, c, d), netmask_ipv4_bits(mask)};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsBogon(const in6_addr& addr);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsBogonRange(const in6_addr& host, const in6_addr& mask);
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
struct Addr; // fwd declr
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-09-03 12:03:43 +00:00
|
|
|
bool
|
|
|
|
AllInterfaces(int af, Addr& 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
|
|
|
|
std::string
|
|
|
|
findFreePrivateRange();
|
|
|
|
|
|
|
|
/// look at adapter names and find a free one
|
|
|
|
std::string
|
|
|
|
findFreeLokiTunIfName();
|
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
/// get network interface address for network interface with ifname
|
|
|
|
bool
|
|
|
|
GetIFAddr(const std::string& ifname, Addr& addr, int af = AF_INET);
|
|
|
|
|
2018-06-20 17:45:44 +00:00
|
|
|
} // namespace llarp
|
2018-05-18 13:54:15 +00:00
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
#include <llarp/net_inaddr.hpp>
|
|
|
|
#include <llarp/net_addr.hpp>
|
|
|
|
|
2018-05-18 13:17:58 +00:00
|
|
|
#endif
|