Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX committed May 30, 2021
1 parent 6508655 commit 75a0d47
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ constexpr char kEOL = -1;
// Used in ToUSVString().
constexpr char16_t kUnicodeReplacementCharacter = 0xFFFD;

#define NS_IN6ADDRSZ 16
#define NS_INT16SZ 2

// https://url.spec.whatwg.org/#concept-host
class URLHost {
public:
Expand Down Expand Up @@ -78,7 +81,7 @@ class URLHost {
union Value {
std::string domain_or_opaque;
uint32_t ipv4;
uint16_t ipv6[8];
uint16_t ipv6[NS_IN6ADDRSZ / NS_INT16SZ];

~Value() {}
Value() : ipv4(0) {}
Expand Down Expand Up @@ -801,20 +804,20 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
CHECK_EQ(type_, HostType::H_FAILED);

unsigned char buf[sizeof(struct in6_addr)];
MaybeStackBuffer<char> ipv6(length + 1);
*(*ipv6 + length) = 0;
memset(buf, 0, sizeof(buf));
memcpy(*ipv6, input, sizeof(const char) * length);

char* zero = const_cast<char*>(input + length);
char origin_zero_char = *zero;
*zero = 0;
int ret = inet_pton(AF_INET6, input, buf);
*zero = origin_zero_char;
int ret = uv_inet_pton(AF_INET6, *ipv6, buf);

if (ret <= 0) {
if (ret != 0) {
return;
}

for (int i = 0; i < 16; i += 2) {
value_.ipv6[i / 2] = (buf[i] << 8) | buf[i + 1];
// Ref: https://sourceware.org/git/?p=glibc.git;a=blob;f=resolv/inet_ntop.c;h=c4d38c0f951013e51a4fc6eaa8a9b82e146abe5a;hb=HEAD#l119
for (int i = 0; i < NS_IN6ADDRSZ; i += 2) {
value_.ipv6[i >> 1] = (buf[i] << 8) | buf[i + 1];
}

type_ = HostType::H_IPV6;
Expand Down

0 comments on commit 75a0d47

Please sign in to comment.