2020-05-08 17:23:21 +00:00
|
|
|
#include <net/ip_address.hpp>
|
|
|
|
|
2020-05-11 15:11:44 +00:00
|
|
|
#include <net/net.hpp>
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
IpAddress::IpAddress(std::string_view str)
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
setAddress(str);
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IpAddress::IpAddress(std::string_view str, std::optional<uint16_t> port)
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
setAddress(str, port);
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IpAddress::IpAddress(const SockAddr& addr)
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
m_ipAddress = addr.toString();
|
|
|
|
uint16_t port = addr.getPort();
|
|
|
|
if (port > 0)
|
|
|
|
m_port = port;
|
2020-05-11 15:47:26 +00:00
|
|
|
|
|
|
|
m_empty = addr.isEmpty();
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:11:44 +00:00
|
|
|
IpAddress&
|
2020-05-08 17:23:21 +00:00
|
|
|
IpAddress::operator=(const sockaddr& other)
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
SockAddr addr(other);
|
|
|
|
|
|
|
|
m_ipAddress = addr.toString();
|
|
|
|
uint16_t port = addr.getPort();
|
|
|
|
if (port > 0)
|
|
|
|
m_port = port;
|
|
|
|
|
2020-05-11 15:47:26 +00:00
|
|
|
m_empty = addr.isEmpty();
|
|
|
|
|
2020-05-11 15:11:44 +00:00
|
|
|
return *this;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<uint16_t>
|
|
|
|
IpAddress::getPort() const
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
return m_port;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IpAddress::setPort(std::optional<uint16_t> port)
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
m_port = port;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IpAddress::setAddress(std::string_view str)
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
SockAddr addr;
|
|
|
|
addr.fromString(str);
|
|
|
|
|
|
|
|
m_ipAddress = std::string(str);
|
|
|
|
uint16_t port = addr.getPort();
|
|
|
|
if (port > 0)
|
|
|
|
m_port = port;
|
2020-05-11 15:47:26 +00:00
|
|
|
|
|
|
|
m_empty = addr.isEmpty();
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IpAddress::setAddress(std::string_view str, std::optional<uint16_t> port)
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
SockAddr addr;
|
|
|
|
addr.fromString(str);
|
|
|
|
|
|
|
|
m_ipAddress = std::string(str);
|
|
|
|
m_port = port;
|
2020-05-11 15:47:26 +00:00
|
|
|
|
|
|
|
m_empty = addr.isEmpty();
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IpAddress::isIPv4()
|
|
|
|
{
|
2020-05-11 16:00:03 +00:00
|
|
|
throw std::runtime_error("FIXME - IpAddress::isIPv4()");
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IpAddress::isEmpty() const
|
|
|
|
{
|
2020-05-11 15:47:26 +00:00
|
|
|
return m_empty;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr
|
|
|
|
IpAddress::createSockAddr() const
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
SockAddr addr(m_ipAddress);
|
|
|
|
if (m_port)
|
2020-05-20 19:46:08 +00:00
|
|
|
addr.setPort(*m_port);
|
2020-05-11 15:11:44 +00:00
|
|
|
|
|
|
|
return addr;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IpAddress::isBogon() const
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
SockAddr addr(m_ipAddress);
|
|
|
|
const sockaddr_in6* addr6 = addr;
|
2020-05-15 18:07:22 +00:00
|
|
|
const uint8_t* raw = addr6->sin6_addr.s6_addr;
|
2020-05-11 15:11:44 +00:00
|
|
|
return IsIPv4Bogon(ipaddr_ipv4_bits(raw[12], raw[13], raw[14], raw[15]));
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
IpAddress::toString() const
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
return m_ipAddress; // TODO: port
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IpAddress::operator<(const IpAddress& other) const
|
|
|
|
{
|
2020-05-11 17:55:36 +00:00
|
|
|
return createSockAddr() < other.createSockAddr();
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IpAddress::operator==(const IpAddress& other) const
|
|
|
|
{
|
2020-05-11 17:55:36 +00:00
|
|
|
return createSockAddr() == other.createSockAddr();
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& out, const IpAddress& address)
|
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
out << address.toString();
|
2020-05-11 20:47:09 +00:00
|
|
|
return out;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|