Unverified Commit 72cfcfca authored by Kenton Varda's avatar Kenton Varda Committed by GitHub

Merge pull request #626 from capnproto/fix-long-host-lookup

Fix bug where DNS lookup would fail for very long hostnames.
parents a21a2975 bfa8a9aa
...@@ -732,31 +732,34 @@ public: ...@@ -732,31 +732,34 @@ public:
addrTarget = &result.addr.inet4.sin_addr; addrTarget = &result.addr.inet4.sin_addr;
} }
// addrPart is not necessarily NUL-terminated so we have to make a copy. :( if (addrPart.size() < INET6_ADDRSTRLEN - 1) {
KJ_REQUIRE(addrPart.size() < INET6_ADDRSTRLEN - 1, "IP address too long.", addrPart); // addrPart is not necessarily NUL-terminated so we have to make a copy. :(
char buffer[INET6_ADDRSTRLEN]; char buffer[INET6_ADDRSTRLEN];
memcpy(buffer, addrPart.begin(), addrPart.size()); memcpy(buffer, addrPart.begin(), addrPart.size());
buffer[addrPart.size()] = '\0'; buffer[addrPart.size()] = '\0';
// OK, parse it! // OK, parse it!
switch (inet_pton(af, buffer, addrTarget)) { switch (inet_pton(af, buffer, addrTarget)) {
case 1: { case 1: {
// success. // success.
if (!result.parseAllowedBy(filter)) { if (!result.parseAllowedBy(filter)) {
KJ_FAIL_REQUIRE("address family blocked by restrictPeers()"); KJ_FAIL_REQUIRE("address family blocked by restrictPeers()");
return Array<SocketAddress>(); return Array<SocketAddress>();
} }
auto array = kj::heapArrayBuilder<SocketAddress>(1); auto array = kj::heapArrayBuilder<SocketAddress>(1);
array.add(result); array.add(result);
return array.finish(); return array.finish();
}
case 0:
// It's apparently not a simple address... fall back to DNS.
break;
default:
KJ_FAIL_SYSCALL("inet_pton", errno, af, addrPart);
} }
case 0:
// It's apparently not a simple address... fall back to DNS.
return lookupHost(lowLevel, kj::heapString(addrPart), nullptr, port, filter);
default:
KJ_FAIL_SYSCALL("inet_pton", errno, af, addrPart);
} }
return lookupHost(lowLevel, kj::heapString(addrPart), nullptr, port, filter);
} }
static SocketAddress getLocalAddress(int sockfd) { static SocketAddress getLocalAddress(int sockfd) {
......
...@@ -634,31 +634,34 @@ public: ...@@ -634,31 +634,34 @@ public:
addrTarget = &result.addr.inet4.sin_addr; addrTarget = &result.addr.inet4.sin_addr;
} }
// addrPart is not necessarily NUL-terminated so we have to make a copy. :(
char buffer[64]; char buffer[64];
KJ_REQUIRE(addrPart.size() < sizeof(buffer) - 1, "IP address too long.", addrPart); if (addrPart.size() < sizeof(buffer) - 1) {
memcpy(buffer, addrPart.begin(), addrPart.size()); // addrPart is not necessarily NUL-terminated so we have to make a copy. :(
buffer[addrPart.size()] = '\0'; memcpy(buffer, addrPart.begin(), addrPart.size());
buffer[addrPart.size()] = '\0';
// OK, parse it!
switch (InetPtonA(af, buffer, addrTarget)) { // OK, parse it!
case 1: { switch (InetPtonA(af, buffer, addrTarget)) {
// success. case 1: {
if (!result.parseAllowedBy(filter)) { // success.
KJ_FAIL_REQUIRE("address family blocked by restrictPeers()"); if (!result.parseAllowedBy(filter)) {
return Array<SocketAddress>(); KJ_FAIL_REQUIRE("address family blocked by restrictPeers()");
} return Array<SocketAddress>();
}
auto array = kj::heapArrayBuilder<SocketAddress>(1); auto array = kj::heapArrayBuilder<SocketAddress>(1);
array.add(result); array.add(result);
return array.finish(); return array.finish();
}
case 0:
// It's apparently not a simple address... fall back to DNS.
break;
default:
KJ_FAIL_WIN32("InetPton", WSAGetLastError(), af, addrPart);
} }
case 0:
// It's apparently not a simple address... fall back to DNS.
return lookupHost(lowLevel, kj::heapString(addrPart), nullptr, port, filter);
default:
KJ_FAIL_WIN32("InetPton", WSAGetLastError(), af, addrPart);
} }
return lookupHost(lowLevel, kj::heapString(addrPart), nullptr, port, filter);
} }
static SocketAddress getLocalAddress(SOCKET sockfd) { static SocketAddress getLocalAddress(SOCKET sockfd) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment