Skip to content

Commit

Permalink
Merge pull request #4935 from rgacogne/rec40-backport-4911
Browse files Browse the repository at this point in the history
Backport #4911: Fix negative port detection for IPv6 addresses on 32-bit
  • Loading branch information
pieterlexis committed Feb 17, 2017
2 parents d7fab80 + 3ada4e2 commit 829e2bd
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pdns/misc.cc
Expand Up @@ -733,14 +733,16 @@ int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret)
if(addr.empty())
return -1;
string ourAddr(addr);
int port = -1;
bool portSet = false;
unsigned int port;
if(addr[0]=='[') { // [::]:53 style address
string::size_type pos = addr.find(']');
if(pos == string::npos || pos + 2 > addr.size() || addr[pos+1]!=':')
return -1;
ourAddr.assign(addr.c_str() + 1, pos-1);
try {
port = pdns_stou(addr.substr(pos+2));
portSet = true;
}
catch(std::out_of_range) {
return -1;
Expand All @@ -766,12 +768,12 @@ int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret)
freeaddrinfo(res);
}

if(port > 65535)
// negative ports are found with the pdns_stou above
return -1;
if(portSet) {
if(port > 65535)
return -1;

if(port >= 0)
ret->sin6_port = htons(port);
}

return 0;
}
Expand Down

0 comments on commit 829e2bd

Please sign in to comment.