Skip to content

Commit

Permalink
src: introduce node_sockaddr
Browse files Browse the repository at this point in the history
Introduce the SocketAddress utility class. The QUIC implementation
makes extensive use of this for handling of socket addresses. It
was separated out to make it generically reusable throughout core

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #32070
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
  • Loading branch information
jasnell authored and MylesBorins committed Mar 9, 2020
1 parent a727b13 commit 4ac1ce1
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 11 deletions.
4 changes: 4 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@
'src/node_process_methods.cc',
'src/node_process_object.cc',
'src/node_serdes.cc',
'src/node_sockaddr.cc',
'src/node_stat_watcher.cc',
'src/node_symbols.cc',
'src/node_task_queue.cc',
Expand Down Expand Up @@ -684,6 +685,8 @@
'src/node_process.h',
'src/node_revert.h',
'src/node_root_certs.h',
'src/node_sockaddr.h',
'src/node_sockaddr-inl.h',
'src/node_stat_watcher.h',
'src/node_union_bytes.h',
'src/node_url.h',
Expand Down Expand Up @@ -1150,6 +1153,7 @@
'test/cctest/test_linked_binding.cc',
'test/cctest/test_per_process.cc',
'test/cctest/test_platform.cc',
'test/cctest/test_sockaddr.cc',
'test/cctest/test_traced_value.cc',
'test/cctest/test_util.cc',
'test/cctest/test_url.cc',
Expand Down
170 changes: 170 additions & 0 deletions src/node_sockaddr-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#ifndef SRC_NODE_SOCKADDR_INL_H_
#define SRC_NODE_SOCKADDR_INL_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "node.h"
#include "node_internals.h"
#include "node_sockaddr.h"
#include "util-inl.h"

#include <string>

namespace node {

static constexpr uint32_t kLabelMask = 0xFFFFF;

inline void hash_combine(size_t* seed) { }

template <typename T, typename... Args>
inline void hash_combine(size_t* seed, const T& value, Args... rest) {
*seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (*seed << 6) + (*seed >> 2);
hash_combine(seed, rest...);
}

bool SocketAddress::is_numeric_host(const char* hostname) {
return is_numeric_host(hostname, AF_INET) ||
is_numeric_host(hostname, AF_INET6);
}

bool SocketAddress::is_numeric_host(const char* hostname, int family) {
in6_addr dst;
return inet_pton(family, hostname, &dst) == 1;
}

int SocketAddress::GetPort(const sockaddr* addr) {
CHECK(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
return ntohs(addr->sa_family == AF_INET ?
reinterpret_cast<const sockaddr_in*>(addr)->sin_port :
reinterpret_cast<const sockaddr_in6*>(addr)->sin6_port);
}

int SocketAddress::GetPort(const sockaddr_storage* addr) {
return GetPort(reinterpret_cast<const sockaddr*>(addr));
}

std::string SocketAddress::GetAddress(const sockaddr* addr) {
CHECK(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
char host[INET6_ADDRSTRLEN];
const void* src = addr->sa_family == AF_INET ?
static_cast<const void*>(
&(reinterpret_cast<const sockaddr_in*>(addr)->sin_addr)) :
static_cast<const void*>(
&(reinterpret_cast<const sockaddr_in6*>(addr)->sin6_addr));
uv_inet_ntop(addr->sa_family, src, host, INET6_ADDRSTRLEN);
return std::string(host);
}

std::string SocketAddress::GetAddress(const sockaddr_storage* addr) {
return GetAddress(reinterpret_cast<const sockaddr*>(addr));
}

size_t SocketAddress::GetLength(const sockaddr* addr) {
return addr->sa_family == AF_INET ?
sizeof(sockaddr_in) : sizeof(sockaddr_in6);
}

size_t SocketAddress::GetLength(const sockaddr_storage* addr) {
return GetLength(reinterpret_cast<const sockaddr*>(addr));
}

SocketAddress::SocketAddress(const sockaddr* addr) {
memcpy(&address_, addr, GetLength(addr));
}

SocketAddress::SocketAddress(const SocketAddress& addr) {
memcpy(&address_, &addr.address_, addr.length());
}

SocketAddress& SocketAddress::operator=(const sockaddr* addr) {
memcpy(&address_, addr, GetLength(addr));
return *this;
}

SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
memcpy(&address_, &addr.address_, addr.length());
return *this;
}

const sockaddr& SocketAddress::operator*() const {
return *this->data();
}

const sockaddr* SocketAddress::operator->() const {
return this->data();
}

size_t SocketAddress::length() const {
return GetLength(&address_);
}

const sockaddr* SocketAddress::data() const {
return reinterpret_cast<const sockaddr*>(&address_);
}

const uint8_t* SocketAddress::raw() const {
return reinterpret_cast<const uint8_t*>(&address_);
}

sockaddr* SocketAddress::storage() {
return reinterpret_cast<sockaddr*>(&address_);
}

int SocketAddress::family() const {
return address_.ss_family;
}

std::string SocketAddress::address() const {
return GetAddress(&address_);
}

int SocketAddress::port() const {
return GetPort(&address_);
}

uint32_t SocketAddress::flow_label() const {
if (family() != AF_INET6)
return 0;
const sockaddr_in6* in = reinterpret_cast<const sockaddr_in6*>(data());
return in->sin6_flowinfo;
}

void SocketAddress::set_flow_label(uint32_t label) {
if (family() != AF_INET6)
return;
CHECK_LE(label, kLabelMask);
sockaddr_in6* in = reinterpret_cast<sockaddr_in6*>(&address_);
in->sin6_flowinfo = label;
}

std::string SocketAddress::ToString() const {
if (family() != AF_INET && family() != AF_INET6) return "";
return (family() == AF_INET6 ?
std::string("[") + address() + "]:" :
address() + ":") +
std::to_string(port());
}

void SocketAddress::Update(uint8_t* data, size_t len) {
CHECK_LE(len, sizeof(address_));
memcpy(&address_, data, len);
}

v8::Local<v8::Object> SocketAddress::ToJS(
Environment* env,
v8::Local<v8::Object> info) const {
return AddressToJS(env, data(), info);
}

bool SocketAddress::operator==(const SocketAddress& other) const {
if (family() != other.family()) return false;
return memcmp(raw(), other.raw(), length()) == 0;
}

bool SocketAddress::operator!=(const SocketAddress& other) const {
return !(*this == other);
}
} // namespace node

#endif // NODE_WANT_INTERNALS
#endif // SRC_NODE_SOCKADDR_INL_H_
95 changes: 95 additions & 0 deletions src/node_sockaddr.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "node_sockaddr-inl.h" // NOLINT(build/include)
#include "uv.h"

namespace node {

namespace {
template <typename T, typename F>
SocketAddress FromUVHandle(F fn, const T& handle) {
SocketAddress addr;
int len = sizeof(sockaddr_storage);
if (fn(&handle, addr.storage(), &len) == 0)
CHECK_EQ(static_cast<size_t>(len), addr.length());
else
addr.storage()->sa_family = 0;
return addr;
}
} // namespace

bool SocketAddress::ToSockAddr(
int32_t family,
const char* host,
uint32_t port,
sockaddr_storage* addr) {
switch (family) {
case AF_INET:
return uv_ip4_addr(
host,
port,
reinterpret_cast<sockaddr_in*>(addr)) == 0;
case AF_INET6:
return uv_ip6_addr(
host,
port,
reinterpret_cast<sockaddr_in6*>(addr)) == 0;
default:
UNREACHABLE();
}
}

bool SocketAddress::New(
const char* host,
uint32_t port,
SocketAddress* addr) {
return New(AF_INET, host, port, addr) || New(AF_INET6, host, port, addr);
}

bool SocketAddress::New(
int32_t family,
const char* host,
uint32_t port,
SocketAddress* addr) {
return ToSockAddr(family, host, port,
reinterpret_cast<sockaddr_storage*>(addr->storage()));
}

size_t SocketAddress::Hash::operator()(const SocketAddress& addr) const {
size_t hash = 0;
switch (addr.family()) {
case AF_INET: {
const sockaddr_in* ipv4 =
reinterpret_cast<const sockaddr_in*>(addr.raw());
hash_combine(&hash, ipv4->sin_port, ipv4->sin_addr.s_addr);
break;
}
case AF_INET6: {
const sockaddr_in6* ipv6 =
reinterpret_cast<const sockaddr_in6*>(addr.raw());
const uint64_t* a =
reinterpret_cast<const uint64_t*>(&ipv6->sin6_addr);
hash_combine(&hash, ipv6->sin6_port, a[0], a[1]);
break;
}
default:
UNREACHABLE();
}
return hash;
}

SocketAddress SocketAddress::FromSockName(const uv_tcp_t& handle) {
return FromUVHandle(uv_tcp_getsockname, handle);
}

SocketAddress SocketAddress::FromSockName(const uv_udp_t& handle) {
return FromUVHandle(uv_udp_getsockname, handle);
}

SocketAddress SocketAddress::FromPeerName(const uv_tcp_t& handle) {
return FromUVHandle(uv_tcp_getpeername, handle);
}

SocketAddress SocketAddress::FromPeerName(const uv_udp_t& handle) {
return FromUVHandle(uv_udp_getpeername, handle);
}

} // namespace node
Loading

0 comments on commit 4ac1ce1

Please sign in to comment.