Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit cddebf3

Browse files
committed
src: move SocketAddress into separate header
Allow for better future reuse of sockaddr related code by moving SocketAddress into it's own header PR-URL: #207 Reviewed-By: #207
1 parent 66ea869 commit cddebf3

12 files changed

+474
-206
lines changed

src/js_udp_wrap.cc

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "udp_wrap.h"
22
#include "async_wrap-inl.h"
33
#include "node_errors.h"
4+
#include "node_sockaddr-inl.h"
45

56
namespace node {
67

@@ -27,6 +28,8 @@ class JSUDPWrap final : public UDPWrapBase, public AsyncWrap {
2728
const sockaddr* addr) override;
2829
int GetPeerName(sockaddr* name, int* namelen) override;
2930
int GetSockName(sockaddr* name, int* namelen) override;
31+
SocketAddress* GetPeerName(SocketAddress* addr = nullptr) override;
32+
SocketAddress* GetSockName(SocketAddress* addr = nullptr) override;
3033
int GetSockaddr(sockaddr* name, int* namelen, bool peer);
3134
AsyncWrap* GetAsyncWrap() override { return this; }
3235

@@ -120,6 +123,16 @@ int JSUDPWrap::GetSockName(sockaddr* name, int* namelen) {
120123
return GetSockaddr(name, namelen, false);
121124
}
122125

126+
SocketAddress* JSUDPWrap::GetPeerName(SocketAddress* addr) {
127+
// TODO(jasnell): Maybe turn this into a real JS-based method.
128+
return SocketAddress::New("127.0.0.1", 1337, AF_INET, addr);
129+
}
130+
131+
SocketAddress* JSUDPWrap::GetSockName(SocketAddress* addr) {
132+
// TODO(jasnell): Maybe turn this into a real JS-based method.
133+
return SocketAddress::New("127.0.0.1", 1337, AF_INET, addr);
134+
}
135+
123136
int JSUDPWrap::GetSockaddr(sockaddr* name, int* namelen, bool peer) {
124137
// TODO(addaleax): Maybe turn this into a real JS-based method.
125138
sockaddr_in addr_in;

src/node_quic_crypto.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "node_crypto_common.h"
55
#include "node_quic_session-inl.h"
66
#include "node_quic_util-inl.h"
7+
#include "node_sockaddr-inl.h"
78
#include "node_url.h"
89
#include "string_bytes.h"
910
#include "v8.h"
@@ -144,7 +145,7 @@ bool GenerateRetryToken(
144145
ngtcp2_crypto_ctx ctx;
145146
ngtcp2_crypto_ctx_initial(&ctx);
146147

147-
const size_t addrlen = SocketAddress::GetAddressLen(addr);
148+
const size_t addrlen = SocketAddress::GetLength(addr);
148149
size_t ivlen = ngtcp2_crypto_packet_protection_ivlen(&ctx.aead);
149150

150151
uint64_t now = uv_hrtime();
@@ -203,7 +204,7 @@ bool InvalidRetryToken(
203204
ngtcp2_crypto_ctx_initial(&ctx);
204205

205206
size_t ivlen = ngtcp2_crypto_packet_protection_ivlen(&ctx.aead);
206-
const size_t addrlen = SocketAddress::GetAddressLen(addr);
207+
const size_t addrlen = SocketAddress::GetLength(addr);
207208

208209
if (hd->tokenlen < TOKEN_RAND_DATALEN)
209210
return true;
@@ -420,7 +421,7 @@ int VerifyHostnameIdentity(
420421
// check. It's possible that the X509_check_ip_asc covers this. If so,
421422
// we can remove this check.
422423

423-
if (SocketAddress::numeric_host(hostname)) {
424+
if (SocketAddress::is_numeric_host(hostname)) {
424425
auto ips = altnames.equal_range("ip");
425426
for (auto ip = ips.first; ip != ips.second; ++ip) {
426427
if (ip->second.compare(hostname) == 0) {
@@ -638,7 +639,7 @@ void SetHostname(SSL* ssl, const std::string& hostname) {
638639
// TODO(@jasnell): Need to determine if setting localhost
639640
// here is the right thing to do.
640641
if (hostname.length() == 0 ||
641-
SocketAddress::numeric_host(hostname.c_str())) {
642+
SocketAddress::is_numeric_host(hostname.c_str())) {
642643
SSL_set_tlsext_host_name(ssl, "localhost");
643644
} else {
644645
SSL_set_tlsext_host_name(ssl, hostname.c_str());

src/node_quic_session.cc

+15-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "node_quic_util-inl.h"
2121
#include "node_quic_default_application.h"
2222
#include "node_quic_http3_application.h"
23+
#include "node_sockaddr-inl.h"
2324
#include "v8.h"
2425
#include "uv.h"
2526

@@ -1348,7 +1349,7 @@ void QuicSession::PathValidation(
13481349
Debug(this,
13491350
"Path validation succeeded. Updating local and remote addresses");
13501351
SetLocalAddress(&path->local);
1351-
remote_address_.Update(&path->remote);
1352+
remote_address_.Update(path->remote.addr, path->remote.addrlen);
13521353
IncrementStat(
13531354
1, &session_stats_,
13541355
&session_stats::path_validation_success_count);
@@ -1466,7 +1467,7 @@ bool QuicSession::Receive(
14661467
// It's possible for the remote address to change from one
14671468
// packet to the next so we have to look at the addr on
14681469
// every packet.
1469-
remote_address_.Copy(addr);
1470+
remote_address_ = addr;
14701471
QuicPath path(Socket()->GetLocalAddress(), &remote_address_);
14711472

14721473
{
@@ -1765,7 +1766,7 @@ bool QuicSession::SelectPreferredAddress(
17651766
SocketAddress* local_address = Socket()->GetLocalAddress();
17661767
uv_getaddrinfo_t req;
17671768

1768-
if (!SocketAddress::ResolvePreferredAddress(
1769+
if (!ResolvePreferredAddress(
17691770
env(), local_address->GetFamily(),
17701771
paddr, &req)) {
17711772
return false;
@@ -1791,7 +1792,7 @@ bool QuicSession::SendPacket(
17911792
ngtcp2_path_storage* path) {
17921793
sendbuf_.Push(std::move(buf));
17931794
// TODO(@jasnell): Update the local endpoint also?
1794-
remote_address_.Update(&path->path.remote);
1795+
remote_address_.Update(path->path.remote.addr, path->path.remote.addrlen);
17951796
return SendPacket("stream data");
17961797
}
17971798

@@ -1986,7 +1987,8 @@ bool QuicSession::SetSocket(QuicSocket* socket, bool nat_rebinding) {
19861987
// Step 4: Update ngtcp2
19871988
SocketAddress* local_address = socket->GetLocalAddress();
19881989
if (nat_rebinding) {
1989-
ngtcp2_addr addr = local_address->ToAddr();
1990+
ngtcp2_addr addr;
1991+
ToNgtcp2Addr(local_address, &addr);
19901992
ngtcp2_conn_set_local_addr(Connection(), &addr);
19911993
} else {
19921994
QuicPath path(local_address, &remote_address_);
@@ -2274,7 +2276,7 @@ bool QuicSession::WritePackets(const char* diagnostic_label) {
22742276
}
22752277

22762278
data.Realloc(nwrite);
2277-
remote_address_.Update(&path.path.remote);
2279+
remote_address_.Update(path.path.remote.addr, path.path.remote.addrlen);
22782280
sendbuf_.Push(std::move(data));
22792281
if (!SendPacket(diagnostic_label))
22802282
return false;
@@ -2400,8 +2402,8 @@ void QuicSession::InitServer(
24002402
ExtendMaxStreamsBidi(DEFAULT_MAX_STREAMS_BIDI);
24012403
ExtendMaxStreamsUni(DEFAULT_MAX_STREAMS_UNI);
24022404

2403-
remote_address_.Copy(addr);
2404-
max_pktlen_ = SocketAddress::GetMaxPktLen(addr);
2405+
remote_address_ = addr;
2406+
max_pktlen_ = GetMaxPktLen(addr);
24052407

24062408
config->SetOriginalConnectionID(ocid);
24072409
config->GenerateStatelessResetToken();
@@ -2512,8 +2514,8 @@ bool QuicSession::InitClient(
25122514
QlogMode qlog) {
25132515
CHECK_NULL(connection_);
25142516

2515-
remote_address_.Copy(addr);
2516-
max_pktlen_ = SocketAddress::GetMaxPktLen(addr);
2517+
remote_address_ = addr;
2518+
max_pktlen_ = GetMaxPktLen(addr);
25172519

25182520
QuicSessionConfig config(env());
25192521
max_crypto_buffer_ = config.GetMaxCryptoBuffer();
@@ -3073,7 +3075,7 @@ void QuicSessionGetRemoteAddress(
30733075
Environment* env = session->env();
30743076
CHECK(args[0]->IsObject());
30753077
args.GetReturnValue().Set(
3076-
AddressToJS(env, **session->GetRemoteAddress(), args[0].As<Object>()));
3078+
session->GetRemoteAddress()->ToJS(env, args[0].As<Object>()));
30773079
}
30783080

30793081
void QuicSessionGetCertificate(
@@ -3131,9 +3133,8 @@ void NewQuicClientSession(const FunctionCallbackInfo<Value>& args) {
31313133
std::string hostname(*servername);
31323134

31333135
sockaddr_storage addr;
3134-
int err = SocketAddress::ToSockAddr(family, *address, port, &addr);
3135-
if (err != 0)
3136-
return args.GetReturnValue().Set(err);
3136+
if (SocketAddress::ToSockAddr(family, *address, port, &addr) == nullptr)
3137+
return args.GetReturnValue().Set(-1);
31373138

31383139
int select_preferred_address_policy = QUIC_PREFERRED_ADDRESS_IGNORE;
31393140
if (!args[10]->Int32Value(env->context())

src/node_quic_session.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "node_quic_buffer.h"
1515
#include "node_quic_crypto.h"
1616
#include "node_quic_util.h"
17+
#include "node_sockaddr.h"
1718
#include "v8.h"
1819
#include "uv.h"
1920

src/node_quic_socket.cc

+8-16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "node_quic_session-inl.h"
1212
#include "node_quic_socket.h"
1313
#include "node_quic_util-inl.h"
14+
#include "node_sockaddr-inl.h"
1415
#include "req_wrap-inl.h"
1516
#include "util.h"
1617
#include "uv.h"
@@ -44,7 +45,7 @@ namespace {
4445
inline uint32_t GenerateReservedVersion(
4546
const sockaddr* addr,
4647
uint32_t version) {
47-
socklen_t addrlen = SocketAddress::GetAddressLen(addr);
48+
socklen_t addrlen = SocketAddress::GetLength(addr);
4849
uint32_t h = 0x811C9DC5u;
4950
const uint8_t* p = reinterpret_cast<const uint8_t*>(addr);
5051
const uint8_t* ep = p + addrlen;
@@ -160,14 +161,8 @@ void QuicSocket::AssociateCID(
160161
}
161162

162163
void QuicSocket::OnAfterBind() {
163-
sockaddr_storage addr_buf;
164-
sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_buf);
165-
int addrlen = sizeof(addr_buf);
166-
167-
CHECK_EQ(udp_->GetSockName(addr, &addrlen), 0);
168-
local_address_.Copy(addr);
164+
udp_->GetSockName(&local_address_);
169165
Debug(this, "Socket bound");
170-
171166
socket_stats_.bound_at = uv_hrtime();
172167
}
173168

@@ -403,8 +398,7 @@ void QuicSocket::SendInitialConnectionClose(
403398
EntropySource(scid.data, NGTCP2_SV_SCIDLEN);
404399
scid.datalen = NGTCP2_SV_SCIDLEN;
405400

406-
SocketAddress remote_address;
407-
remote_address.Copy(addr);
401+
SocketAddress remote_address(addr);
408402
QuicPath path(GetLocalAddress(), &remote_address);
409403

410404
ngtcp2_conn_callbacks callbacks;
@@ -723,11 +717,9 @@ int QuicSocket::SendPacket(
723717
if (buffer->Length() == 0 || buffer->RemainingLength() == 0)
724718
return 0;
725719

726-
{
727-
char host[INET6_ADDRSTRLEN];
728-
SocketAddress::GetAddress(dest, host, sizeof(host));
729-
Debug(this, "Sending to %s at port %d", host, SocketAddress::GetPort(dest));
730-
}
720+
Debug(this, "Sending to %s at port %d",
721+
SocketAddress::GetAddress(dest).c_str(),
722+
SocketAddress::GetPort(dest));
731723

732724
// Remaining Length should never be zero at this point
733725
CHECK_GT(buffer->RemainingLength(), 0);
@@ -961,7 +953,7 @@ void QuicSocketListen(const FunctionCallbackInfo<Value>& args) {
961953
preferred_address_family,
962954
*preferred_address_host,
963955
preferred_address_port,
964-
&preferred_address_storage) == 0) {
956+
&preferred_address_storage) != nullptr) {
965957
preferred_address =
966958
reinterpret_cast<const sockaddr*>(&preferred_address_storage);
967959
}

src/node_quic_socket.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ngtcp2/ngtcp2.h"
1010
#include "node_quic_session.h"
1111
#include "node_quic_util.h"
12+
#include "node_sockaddr.h"
1213
#include "env.h"
1314
#include "udp_wrap.h"
1415
#include "v8.h"

src/node_quic_util-inl.h

+57
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,63 @@ std::string QuicCID::ToHex() const {
3030
return std::string(*dest, written);
3131
}
3232

33+
ngtcp2_addr* ToNgtcp2Addr(SocketAddress* addr, ngtcp2_addr* dest) {
34+
if (dest == nullptr)
35+
dest = new ngtcp2_addr();
36+
return ngtcp2_addr_init(dest, **addr, addr->GetLength(), nullptr);
37+
}
38+
39+
size_t GetMaxPktLen(const sockaddr* addr) {
40+
return addr->sa_family == AF_INET6 ?
41+
NGTCP2_MAX_PKTLEN_IPV6 :
42+
NGTCP2_MAX_PKTLEN_IPV4;
43+
}
44+
45+
bool ResolvePreferredAddress(
46+
Environment* env,
47+
int local_address_family,
48+
const ngtcp2_preferred_addr* paddr,
49+
uv_getaddrinfo_t* req) {
50+
int af;
51+
const uint8_t* binaddr;
52+
uint16_t port;
53+
constexpr uint8_t empty_addr[] = {0, 0, 0, 0, 0, 0, 0, 0,
54+
0, 0, 0, 0, 0, 0, 0, 0};
55+
56+
if (local_address_family == AF_INET &&
57+
memcmp(empty_addr, paddr->ipv4_addr, sizeof(paddr->ipv4_addr)) != 0) {
58+
af = AF_INET;
59+
binaddr = paddr->ipv4_addr;
60+
port = paddr->ipv4_port;
61+
} else if (local_address_family == AF_INET6 &&
62+
memcmp(empty_addr,
63+
paddr->ipv6_addr,
64+
sizeof(paddr->ipv6_addr)) != 0) {
65+
af = AF_INET6;
66+
binaddr = paddr->ipv6_addr;
67+
port = paddr->ipv6_port;
68+
} else {
69+
return false;
70+
}
71+
72+
char host[NI_MAXHOST];
73+
if (uv_inet_ntop(af, binaddr, host, sizeof(host)) != 0)
74+
return false;
75+
76+
addrinfo hints{};
77+
hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
78+
hints.ai_family = af;
79+
hints.ai_socktype = SOCK_DGRAM;
80+
81+
return
82+
uv_getaddrinfo(
83+
env->event_loop(),
84+
req,
85+
nullptr,
86+
host,
87+
std::to_string(port).c_str(),
88+
&hints) == 0;
89+
}
3390

3491
Timer::Timer(Environment* env, std::function<void()> fn)
3592
: env_(env),

0 commit comments

Comments
 (0)