2018-10-23 15:38:33 +00:00
|
|
|
#ifndef LLARP_NET_ADDR_HPP
|
|
|
|
#define LLARP_NET_ADDR_HPP
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <net/address_info.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.h>
|
|
|
|
#include <net/net.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/string_view.hpp>
|
2019-05-01 13:40:10 +00:00
|
|
|
#include <net/net_int.hpp>
|
2018-10-23 15:38:33 +00:00
|
|
|
|
2018-12-12 00:26:37 +00:00
|
|
|
#include <string>
|
2018-10-23 15:38:33 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
// real work
|
|
|
|
struct Addr
|
|
|
|
{
|
|
|
|
// network order
|
|
|
|
sockaddr_in6 _addr;
|
2019-11-20 16:10:52 +00:00
|
|
|
sockaddr_in _addr4;
|
2018-10-23 15:38:33 +00:00
|
|
|
~Addr();
|
|
|
|
|
|
|
|
Addr();
|
|
|
|
|
2019-07-02 21:28:28 +00:00
|
|
|
Addr(string_view str);
|
|
|
|
|
|
|
|
Addr(string_view str, const uint16_t p_port);
|
|
|
|
|
|
|
|
Addr(string_view addr_str, string_view port_str);
|
|
|
|
|
2018-10-23 15:38:33 +00:00
|
|
|
void
|
|
|
|
port(uint16_t port);
|
|
|
|
|
|
|
|
in6_addr*
|
|
|
|
addr6();
|
|
|
|
|
|
|
|
in_addr*
|
|
|
|
addr4();
|
|
|
|
|
|
|
|
const in6_addr*
|
|
|
|
addr6() const;
|
|
|
|
|
|
|
|
const in_addr*
|
|
|
|
addr4() const;
|
|
|
|
|
|
|
|
bool
|
2019-07-02 21:28:28 +00:00
|
|
|
from_char_array(string_view str);
|
2018-10-23 15:38:33 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
from_4int(const uint8_t one, const uint8_t two, const uint8_t three,
|
|
|
|
const uint8_t four);
|
|
|
|
|
|
|
|
Addr(const uint8_t one, const uint8_t two, const uint8_t three,
|
|
|
|
const uint8_t four);
|
|
|
|
|
|
|
|
Addr(const uint8_t one, const uint8_t two, const uint8_t three,
|
|
|
|
const uint8_t four, const uint16_t p_port);
|
|
|
|
|
|
|
|
Addr(const AddressInfo& other);
|
|
|
|
Addr(const sockaddr_in& other);
|
|
|
|
Addr(const sockaddr_in6& other);
|
|
|
|
Addr(const sockaddr& other);
|
2018-11-05 11:31:43 +00:00
|
|
|
|
2018-11-05 03:44:58 +00:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2018-10-23 15:38:33 +00:00
|
|
|
|
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const Addr& a);
|
|
|
|
|
|
|
|
operator const sockaddr*() const;
|
|
|
|
|
|
|
|
operator sockaddr*() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
CopyInto(sockaddr* other) const;
|
|
|
|
|
|
|
|
int
|
|
|
|
af() const;
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
port() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const Addr& other) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Addr& other) const;
|
|
|
|
|
|
|
|
Addr&
|
|
|
|
operator=(const sockaddr& other);
|
|
|
|
|
|
|
|
inline uint32_t
|
|
|
|
tohl() const
|
|
|
|
{
|
|
|
|
return ntohl(addr4()->s_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline huint32_t
|
|
|
|
xtohl() const
|
|
|
|
{
|
|
|
|
return huint32_t{ntohl(addr4()->s_addr)};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t
|
|
|
|
ton() const
|
|
|
|
{
|
|
|
|
return addr4()->s_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nuint32_t
|
|
|
|
xtonl() const
|
|
|
|
{
|
|
|
|
return nuint32_t{addr4()->s_addr};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sameAddr(const Addr& other) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const Addr& other) const;
|
|
|
|
|
|
|
|
inline uint32_t
|
|
|
|
getHostLong()
|
|
|
|
{
|
|
|
|
in_addr_t addr = this->addr4()->s_addr;
|
|
|
|
uint32_t byte = ntohl(addr);
|
|
|
|
return byte;
|
2019-04-24 23:27:31 +00:00
|
|
|
}
|
2018-10-23 15:38:33 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
isTenPrivate(uint32_t byte);
|
|
|
|
|
|
|
|
bool
|
|
|
|
isOneSevenPrivate(uint32_t byte);
|
|
|
|
|
|
|
|
bool
|
|
|
|
isOneNinePrivate(uint32_t byte);
|
|
|
|
|
|
|
|
/// return true if our ipv4 address is a bogon
|
|
|
|
/// TODO: ipv6
|
|
|
|
bool
|
|
|
|
IsBogon() const;
|
|
|
|
|
|
|
|
socklen_t
|
|
|
|
SockLen() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
isPrivate() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
isLoopback() const;
|
|
|
|
|
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
std::size_t
|
|
|
|
operator()(Addr const& a) const noexcept
|
|
|
|
{
|
|
|
|
if(a.af() == AF_INET)
|
|
|
|
{
|
|
|
|
return a.port() ^ a.addr4()->s_addr;
|
|
|
|
}
|
|
|
|
static const uint8_t empty[16] = {0};
|
|
|
|
return (a.af() + memcmp(a.addr6(), empty, 16)) ^ a.port();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}; // end struct
|
|
|
|
} // namespace llarp
|
|
|
|
#endif
|