2021-03-09 22:24:35 +00:00
|
|
|
#include "sock_addr.hpp"
|
2023-10-19 21:59:57 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "ip.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "ip_range.hpp"
|
2022-07-28 16:07:38 +00:00
|
|
|
#include "net.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "net_bits.hpp"
|
|
|
|
|
|
|
|
#include <llarp/util/mem.hpp>
|
|
|
|
#include <llarp/util/str.hpp>
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2020-05-08 17:23:21 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
/// shared utility functions
|
|
|
|
///
|
|
|
|
|
|
|
|
void
|
|
|
|
SockAddr::init()
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
llarp::Zero(&addr6, sizeof(addr6));
|
|
|
|
addr6.sin6_family = AF_INET6;
|
|
|
|
llarp::Zero(&addr4, sizeof(addr4));
|
|
|
|
addr4.sin_family = AF_INET;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 22:52:00 +00:00
|
|
|
void
|
2021-02-24 23:34:42 +00:00
|
|
|
SockAddr::applyIPv4MapBytes()
|
2020-05-08 22:52:00 +00:00
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
std::memcpy(addr6.sin6_addr.s6_addr, ipv4_map_prefix.data(), ipv4_map_prefix.size());
|
2020-05-08 22:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
SockAddr::SockAddr()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:02:41 +00:00
|
|
|
SockAddr::SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, huint16_t port)
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
setIPv4(a, b, c, d);
|
2021-03-09 16:02:41 +00:00
|
|
|
setPort(port);
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 16:02:41 +00:00
|
|
|
SockAddr::SockAddr(nuint32_t ip, nuint16_t port)
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
2021-03-09 16:02:41 +00:00
|
|
|
init();
|
|
|
|
setIPv4(ip);
|
2020-05-08 17:23:21 +00:00
|
|
|
setPort(port);
|
|
|
|
}
|
2021-03-02 18:18:22 +00:00
|
|
|
|
2021-04-19 15:20:46 +00:00
|
|
|
SockAddr::SockAddr(huint32_t ip, huint16_t port) : SockAddr{ToNet(ip), ToNet(port)}
|
|
|
|
{}
|
|
|
|
|
2021-03-09 16:02:41 +00:00
|
|
|
SockAddr::SockAddr(huint128_t ip, huint16_t port)
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
|
|
|
init();
|
2021-03-09 16:02:41 +00:00
|
|
|
setIPv6(ip);
|
|
|
|
setPort(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr::SockAddr(nuint128_t ip, nuint16_t port)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
setIPv6(ip);
|
|
|
|
setPort(port);
|
2021-03-02 18:18:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
SockAddr::SockAddr(std::string_view addr)
|
|
|
|
{
|
2020-05-08 20:33:44 +00:00
|
|
|
init();
|
|
|
|
fromString(addr);
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
2021-05-30 17:29:00 +00:00
|
|
|
SockAddr::SockAddr(std::string_view addr, huint16_t port)
|
2021-02-24 23:34:42 +00:00
|
|
|
{
|
|
|
|
init();
|
2021-05-30 17:29:00 +00:00
|
|
|
setPort(port);
|
2021-02-24 23:34:42 +00:00
|
|
|
fromString(addr, false);
|
|
|
|
}
|
2020-05-08 17:23:21 +00:00
|
|
|
|
2020-05-08 22:52:00 +00:00
|
|
|
SockAddr::SockAddr(const SockAddr& other)
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
2020-05-08 22:52:00 +00:00
|
|
|
*this = other;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr&
|
2020-05-08 22:52:00 +00:00
|
|
|
SockAddr::operator=(const SockAddr& other)
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
*this = other.addr6;
|
2020-05-08 22:52:00 +00:00
|
|
|
return *this;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr::SockAddr(const sockaddr& addr)
|
|
|
|
{
|
2020-05-08 22:52:00 +00:00
|
|
|
*this = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr&
|
|
|
|
SockAddr::operator=(const sockaddr& other)
|
|
|
|
{
|
|
|
|
if (other.sa_family == AF_INET6)
|
2021-02-24 23:34:42 +00:00
|
|
|
*this = reinterpret_cast<const sockaddr_in6&>(other);
|
2020-05-08 22:52:00 +00:00
|
|
|
else if (other.sa_family == AF_INET)
|
2021-02-24 23:34:42 +00:00
|
|
|
*this = reinterpret_cast<const sockaddr_in&>(other);
|
2020-05-08 22:52:00 +00:00
|
|
|
else
|
2022-04-07 20:44:23 +00:00
|
|
|
throw std::invalid_argument{
|
|
|
|
fmt::format("Invalid sockaddr (not AF_INET or AF_INET6) was {}", other.sa_family)};
|
2020-05-08 22:52:00 +00:00
|
|
|
|
|
|
|
return *this;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr::SockAddr(const sockaddr_in& addr)
|
|
|
|
{
|
2020-05-08 22:52:00 +00:00
|
|
|
*this = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr&
|
|
|
|
SockAddr::operator=(const sockaddr_in& other)
|
|
|
|
{
|
|
|
|
init();
|
2021-02-24 23:34:42 +00:00
|
|
|
applyIPv4MapBytes();
|
2020-05-08 22:52:00 +00:00
|
|
|
|
|
|
|
// avoid byte order conversion (this is NBO -> NBO)
|
2023-11-16 15:29:27 +00:00
|
|
|
memcpy(addr6.sin6_addr.s6_addr + 12, &other.sin_addr.s_addr, sizeof(in_addr));
|
|
|
|
addr6.sin6_port = other.sin_port;
|
|
|
|
addr4.sin_addr.s_addr = other.sin_addr.s_addr;
|
|
|
|
addr4.sin_port = other.sin_port;
|
2020-05-08 22:52:00 +00:00
|
|
|
m_empty = false;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr::SockAddr(const sockaddr_in6& addr)
|
|
|
|
{
|
|
|
|
*this = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr&
|
|
|
|
SockAddr::operator=(const sockaddr_in6& other)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
2023-11-16 15:29:27 +00:00
|
|
|
memcpy(&addr6, &other, sizeof(sockaddr_in6));
|
2022-07-28 16:07:38 +00:00
|
|
|
if (IPRange::V4MappedRange().Contains(asIPv6()))
|
2021-03-09 16:02:41 +00:00
|
|
|
{
|
2020-10-27 21:34:09 +00:00
|
|
|
setIPv4(
|
|
|
|
other.sin6_addr.s6_addr[12],
|
|
|
|
other.sin6_addr.s6_addr[13],
|
|
|
|
other.sin6_addr.s6_addr[14],
|
|
|
|
other.sin6_addr.s6_addr[15]);
|
2023-11-16 15:29:27 +00:00
|
|
|
addr4.sin_port = addr6.sin6_port;
|
2021-03-09 16:02:41 +00:00
|
|
|
}
|
2020-05-08 22:52:00 +00:00
|
|
|
m_empty = false;
|
|
|
|
|
|
|
|
return *this;
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 16:14:07 +00:00
|
|
|
SockAddr::SockAddr(const in6_addr& addr)
|
|
|
|
{
|
|
|
|
*this = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr&
|
|
|
|
SockAddr::operator=(const in6_addr& other)
|
|
|
|
{
|
|
|
|
init();
|
2023-11-16 15:29:27 +00:00
|
|
|
memcpy(&addr6.sin6_addr.s6_addr, &other.s6_addr, sizeof(addr6.sin6_addr.s6_addr));
|
2022-07-28 16:07:38 +00:00
|
|
|
if (IPRange::V4MappedRange().Contains(asIPv6()))
|
2021-03-09 16:02:41 +00:00
|
|
|
{
|
2020-10-27 21:34:09 +00:00
|
|
|
setIPv4(other.s6_addr[12], other.s6_addr[13], other.s6_addr[14], other.s6_addr[15]);
|
2023-11-16 15:29:27 +00:00
|
|
|
addr4.sin_port = addr6.sin6_port;
|
2021-03-09 16:02:41 +00:00
|
|
|
}
|
2020-05-11 16:14:07 +00:00
|
|
|
m_empty = false;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
SockAddr::operator const sockaddr*() const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return isIPv4() ? reinterpret_cast<const sockaddr*>(&addr4)
|
|
|
|
: reinterpret_cast<const sockaddr*>(&addr6);
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 21:34:09 +00:00
|
|
|
SockAddr::operator const sockaddr_in*() const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return &addr4;
|
2020-10-27 21:34:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:11:44 +00:00
|
|
|
SockAddr::operator const sockaddr_in6*() const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return &addr6;
|
2020-05-11 15:11:44 +00:00
|
|
|
}
|
|
|
|
|
QUIC lokinet integration refactor
Refactors how quic packets get handled: the actual tunnels now live in
tunnel.hpp's TunnelManager which holds and manages all the quic<->tcp
tunnelling. service::Endpoint now holds a TunnelManager rather than a
quic::Server. We only need one quic server, but we need a separate quic
client instance per outgoing quic tunnel, and TunnelManager handles all
that glue now.
Adds QUIC packet handling to get to the right tunnel code. This
required multiplexing incoming quic packets, as follows:
Adds a very small quic tunnel packet header of 4 bytes:
[1, SPORT, ECN] for client->server packets, where SPORT is our
source "port" (really: just a uint16_t unique quic instance
identifier)
or
[2, DPORT, ECN] for server->client packets where the DPORT is the SPORT
from above.
(This also reworks ECN bits to get properly carried over lokinet.)
We don't need a destination/source port for the server-side because
there is only ever one quic server (and we know we're going to it when
the first byte of the header is 1).
Removes the config option for quic exposing ports; a full lokinet will
simply accept anything incoming on quic and tunnel it to the requested
port on the the local endpoint IP (this handler will come in a following
commit).
Replace ConvoTags with full addresses: we need to carry the port, as
well, which the ConvoTag can't give us, so change those to more general
SockAddrs from which we can extract both the ConvoTag *and* the port.
Add a pending connection queue along with new quic-side handlers to call
when a stream becomes available (TunnelManager uses this to wire up
pending incoming conns with quic streams as streams open up).
Completely get rid of tunnel_server/tunnel_client.cpp code; it is now
moved to tunnel.hpp.
Add listen()/forget() methods in TunnelManager for setting up quic
listening sockets (for liblokinet usage).
Add open()/close() methods in TunnelManager for spinning up new quic
clients for outgoing quic connections.
2021-03-23 19:26:32 +00:00
|
|
|
size_t
|
|
|
|
SockAddr::sockaddr_len() const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return isIPv6() ? sizeof(addr6) : sizeof(addr4);
|
QUIC lokinet integration refactor
Refactors how quic packets get handled: the actual tunnels now live in
tunnel.hpp's TunnelManager which holds and manages all the quic<->tcp
tunnelling. service::Endpoint now holds a TunnelManager rather than a
quic::Server. We only need one quic server, but we need a separate quic
client instance per outgoing quic tunnel, and TunnelManager handles all
that glue now.
Adds QUIC packet handling to get to the right tunnel code. This
required multiplexing incoming quic packets, as follows:
Adds a very small quic tunnel packet header of 4 bytes:
[1, SPORT, ECN] for client->server packets, where SPORT is our
source "port" (really: just a uint16_t unique quic instance
identifier)
or
[2, DPORT, ECN] for server->client packets where the DPORT is the SPORT
from above.
(This also reworks ECN bits to get properly carried over lokinet.)
We don't need a destination/source port for the server-side because
there is only ever one quic server (and we know we're going to it when
the first byte of the header is 1).
Removes the config option for quic exposing ports; a full lokinet will
simply accept anything incoming on quic and tunnel it to the requested
port on the the local endpoint IP (this handler will come in a following
commit).
Replace ConvoTags with full addresses: we need to carry the port, as
well, which the ConvoTag can't give us, so change those to more general
SockAddrs from which we can extract both the ConvoTag *and* the port.
Add a pending connection queue along with new quic-side handlers to call
when a stream becomes available (TunnelManager uses this to wire up
pending incoming conns with quic streams as streams open up).
Completely get rid of tunnel_server/tunnel_client.cpp code; it is now
moved to tunnel.hpp.
Add listen()/forget() methods in TunnelManager for setting up quic
listening sockets (for liblokinet usage).
Add open()/close() methods in TunnelManager for spinning up new quic
clients for outgoing quic connections.
2021-03-23 19:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 17:55:36 +00:00
|
|
|
bool
|
|
|
|
SockAddr::operator<(const SockAddr& other) const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return addr6 < other.addr6;
|
2020-05-11 17:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SockAddr::operator==(const SockAddr& other) const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return addr6 == other.addr6;
|
2020-05-11 17:55:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 15:59:18 +00:00
|
|
|
huint128_t
|
|
|
|
SockAddr::asIPv6() const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return net::In6ToHUInt(addr6.sin6_addr);
|
2021-02-16 15:59:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:01:05 +00:00
|
|
|
huint32_t
|
|
|
|
SockAddr::asIPv4() const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
const nuint32_t n{addr4.sin_addr.s_addr};
|
2021-02-22 15:01:05 +00:00
|
|
|
return ToHost(n);
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
void
|
2021-02-24 23:34:42 +00:00
|
|
|
SockAddr::fromString(std::string_view str, bool allow_port)
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
|
|
|
if (str.empty())
|
2020-05-11 15:11:44 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
m_empty = true;
|
|
|
|
return;
|
|
|
|
}
|
2020-05-08 17:23:21 +00:00
|
|
|
|
|
|
|
// NOTE: this potentially involves multiple memory allocations,
|
|
|
|
// reimplement without split() if it is performance bottleneck
|
2021-08-11 03:12:26 +00:00
|
|
|
auto splits = split(str, ":");
|
2020-05-08 17:23:21 +00:00
|
|
|
|
|
|
|
// TODO: having ":port" at the end makes this ambiguous with IPv6
|
|
|
|
// come up with a strategy for implementing
|
|
|
|
if (splits.size() > 2)
|
2021-02-16 15:59:18 +00:00
|
|
|
{
|
|
|
|
std::string data{str};
|
2023-11-16 15:29:27 +00:00
|
|
|
if (inet_pton(AF_INET6, data.c_str(), addr6.sin6_addr.s6_addr) == -1)
|
2021-02-16 15:59:18 +00:00
|
|
|
throw std::runtime_error{"invalid ip6 address: " + data};
|
|
|
|
return;
|
|
|
|
}
|
2020-05-08 17:23:21 +00:00
|
|
|
|
|
|
|
// split() shouldn't return an empty list if str is empty (checked above)
|
|
|
|
assert(splits.size() > 0);
|
|
|
|
|
|
|
|
// splits[0] should be dot-separated IPv4
|
2021-08-11 03:12:26 +00:00
|
|
|
auto ipSplits = split(splits[0], ".");
|
2020-05-08 17:23:21 +00:00
|
|
|
if (ipSplits.size() != 4)
|
2022-07-16 00:41:14 +00:00
|
|
|
throw std::invalid_argument(fmt::format("{} is not a valid IPv4 address", str));
|
2020-05-08 17:23:21 +00:00
|
|
|
|
2021-02-24 23:34:42 +00:00
|
|
|
std::array<uint8_t, 4> ipBytes;
|
2020-05-08 17:23:21 +00:00
|
|
|
for (int i = 0; i < 4; ++i)
|
2021-02-24 23:34:42 +00:00
|
|
|
if (not parse_int(ipSplits[i], ipBytes[i]))
|
2022-07-16 00:41:14 +00:00
|
|
|
throw std::runtime_error(fmt::format("{} contains invalid numeric value", str));
|
2020-05-08 17:23:21 +00:00
|
|
|
|
|
|
|
// attempt port before setting IPv4 bytes
|
|
|
|
if (splits.size() == 2)
|
|
|
|
{
|
2021-02-24 23:34:42 +00:00
|
|
|
if (not allow_port)
|
2022-07-16 00:41:14 +00:00
|
|
|
throw std::runtime_error{
|
|
|
|
fmt::format("invalid ip address (port not allowed here): {}", str)};
|
2021-02-24 23:34:42 +00:00
|
|
|
uint16_t port;
|
|
|
|
if (not parse_int(splits[1], port))
|
2022-07-16 00:41:14 +00:00
|
|
|
throw std::runtime_error{fmt::format("{} is not a valid port", splits[1])};
|
2020-05-08 17:23:21 +00:00
|
|
|
setPort(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
setIPv4(ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2022-07-16 00:41:14 +00:00
|
|
|
SockAddr::ToString() const
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
|
|
|
// TODO: review
|
|
|
|
if (isEmpty())
|
|
|
|
return "";
|
2022-09-10 01:33:47 +00:00
|
|
|
return fmt::format("{}:{}", hostString(), port());
|
2021-05-30 17:29:00 +00:00
|
|
|
}
|
2020-05-08 17:23:21 +00:00
|
|
|
|
2021-05-30 17:29:00 +00:00
|
|
|
std::string
|
2023-05-29 21:45:16 +00:00
|
|
|
SockAddr::hostString(bool ipv6_brackets) const
|
2021-05-30 17:29:00 +00:00
|
|
|
{
|
2023-05-20 22:18:40 +00:00
|
|
|
std::array<char, 128> buf{};
|
2021-03-23 19:19:39 +00:00
|
|
|
if (isIPv4())
|
2020-05-11 20:52:30 +00:00
|
|
|
{
|
2023-05-20 22:18:40 +00:00
|
|
|
// IPv4 mapped addrs
|
2023-11-16 15:29:27 +00:00
|
|
|
inet_ntop(AF_INET, &addr4.sin_addr.s_addr, buf.data(), buf.size());
|
2023-05-20 22:18:40 +00:00
|
|
|
return buf.data();
|
2020-05-11 20:52:30 +00:00
|
|
|
}
|
2023-05-20 22:18:40 +00:00
|
|
|
|
2023-11-16 15:29:27 +00:00
|
|
|
inet_ntop(AF_INET6, &addr6.sin6_addr.s6_addr, buf.data(), buf.size());
|
2023-05-29 21:45:16 +00:00
|
|
|
if (not ipv6_brackets)
|
2023-05-20 22:18:40 +00:00
|
|
|
return buf.data();
|
|
|
|
|
|
|
|
return fmt::format("[{}]", buf.data());
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SockAddr::isEmpty() const
|
|
|
|
{
|
|
|
|
return m_empty;
|
|
|
|
}
|
|
|
|
|
2021-03-23 19:19:39 +00:00
|
|
|
bool
|
|
|
|
SockAddr::isIPv4() const
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
return IPRange::V4MappedRange().Contains(asIPv6());
|
2021-03-23 19:19:39 +00:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
SockAddr::isIPv6() const
|
|
|
|
{
|
|
|
|
return not isIPv4();
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:02:41 +00:00
|
|
|
nuint32_t
|
2021-03-02 18:18:22 +00:00
|
|
|
SockAddr::getIPv4() const
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return {addr4.sin_addr.s_addr};
|
2021-03-02 18:18:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 18:41:14 +00:00
|
|
|
nuint128_t
|
|
|
|
SockAddr::getIPv6() const
|
|
|
|
{
|
|
|
|
nuint128_t a;
|
|
|
|
// Explicit cast to void* here to avoid non-trivial type copying warnings (technically this
|
|
|
|
// isn't trivial because of the zeroing default constructor, but it's trivial enough that this
|
|
|
|
// copy is safe).
|
2023-11-16 15:29:27 +00:00
|
|
|
std::memcpy(static_cast<void*>(&a), &addr6.sin6_addr, 16);
|
2021-04-28 18:41:14 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:10:04 +00:00
|
|
|
std::variant<nuint32_t, nuint128_t>
|
|
|
|
SockAddr::getIP() const
|
|
|
|
{
|
|
|
|
if (isIPv4())
|
|
|
|
return getIPv4();
|
|
|
|
return getIPv6();
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
void
|
2021-03-09 16:02:41 +00:00
|
|
|
SockAddr::setIPv4(nuint32_t ip)
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
uint8_t* ip6 = addr6.sin6_addr.s6_addr;
|
|
|
|
llarp::Zero(ip6, sizeof(addr6.sin6_addr.s6_addr));
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
applyIPv4MapBytes();
|
|
|
|
|
|
|
|
std::memcpy(ip6 + 12, &ip, 4);
|
2023-11-16 15:29:27 +00:00
|
|
|
addr4.sin_addr.s_addr = ip.n;
|
2021-03-02 18:18:22 +00:00
|
|
|
m_empty = false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:02:41 +00:00
|
|
|
void
|
|
|
|
SockAddr::setIPv4(huint32_t ip)
|
|
|
|
{
|
|
|
|
setIPv4(ToNet(ip));
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
void
|
|
|
|
SockAddr::setIPv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
uint8_t* ip6 = addr6.sin6_addr.s6_addr;
|
|
|
|
llarp::Zero(ip6, sizeof(addr6.sin6_addr.s6_addr));
|
2020-05-08 17:23:21 +00:00
|
|
|
|
2021-02-24 23:34:42 +00:00
|
|
|
applyIPv4MapBytes();
|
2020-05-08 17:23:21 +00:00
|
|
|
|
|
|
|
ip6[12] = a;
|
|
|
|
ip6[13] = b;
|
|
|
|
ip6[14] = c;
|
|
|
|
ip6[15] = d;
|
2020-10-27 21:34:09 +00:00
|
|
|
const auto ip = ipaddr_ipv4_bits(a, b, c, d);
|
2023-11-16 15:29:27 +00:00
|
|
|
addr4.sin_addr.s_addr = htonl(ip.h);
|
2020-05-08 17:23:21 +00:00
|
|
|
m_empty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-09 16:02:41 +00:00
|
|
|
SockAddr::setIPv6(huint128_t ip)
|
|
|
|
{
|
|
|
|
return setIPv6(ToNet(ip));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SockAddr::setIPv6(nuint128_t ip)
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
std::memcpy(&addr6.sin6_addr, &ip, sizeof(addr6.sin6_addr));
|
2021-03-23 19:19:39 +00:00
|
|
|
if (isIPv4())
|
2021-03-09 16:02:41 +00:00
|
|
|
{
|
|
|
|
setIPv4(
|
2023-11-16 15:29:27 +00:00
|
|
|
addr6.sin6_addr.s6_addr[12],
|
|
|
|
addr6.sin6_addr.s6_addr[13],
|
|
|
|
addr6.sin6_addr.s6_addr[14],
|
|
|
|
addr6.sin6_addr.s6_addr[15]);
|
|
|
|
addr4.sin_port = addr6.sin6_port;
|
2021-03-09 16:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SockAddr::setPort(nuint16_t port)
|
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
addr6.sin6_port = port.n;
|
|
|
|
addr4.sin_port = port.n;
|
2021-03-09 16:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SockAddr::setPort(huint16_t port)
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
2021-03-09 16:02:41 +00:00
|
|
|
setPort(ToNet(port));
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
net::port_t
|
|
|
|
SockAddr::port() const
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
2023-11-16 15:29:27 +00:00
|
|
|
return net::port_t{addr6.sin6_port};
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|