Fix to allow parsing of addresses with 0 port.

This commit is contained in:
cheatfate 2019-03-27 21:42:46 +02:00
parent a2b85a8126
commit 51c87bf090
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
1 changed files with 8 additions and 8 deletions

View File

@ -200,19 +200,17 @@ proc initTAddress*(address: string): TransportAddress =
else: (sizeof(result.address_un) - 1)
copyMem(addr result.address_un[0], unsafeAddr address[0], size)
else:
var port: Port
var port: int
var parts = address.rsplit(":", maxsplit = 1)
if len(parts) != 2:
raise newException(TransportAddressError,
"Format is <address>:<port> or </address>!")
try:
let portint = parseInt(parts[1])
doAssert(portint > 0 and portint < 65536)
port = Port(portint)
port = parseInt(parts[1])
except:
raise newException(TransportAddressError, "Illegal port number!")
if port < 0 or port >= 65536:
raise newException(TransportAddressError, "Illegal port number!")
try:
var ipaddr: IpAddress
if parts[0][0] == '[' and parts[0][^1] == ']':
@ -227,7 +225,7 @@ proc initTAddress*(address: string): TransportAddress =
result.address_v6 = ipaddr.address_v6
else:
raise newException(TransportAddressError, "Incorrect address family!")
result.port = port
result.port = Port(port)
except:
raise newException(TransportAddressError, getCurrentException().msg)
else:
@ -388,10 +386,12 @@ proc resolveTAddress*(address: string,
try:
port = parseInt(parts[1])
doAssert(port > 0 and port < 65536)
except:
raise newException(TransportAddressError, "Illegal port number!")
if port < 0 or port >= 65536:
raise newException(TransportAddressError, "Illegal port number!")
if parts[0][0] == '[' and parts[0][^1] == ']':
# IPv6 numeric addresses must be enclosed with `[]`.
hostname = parts[0][1..^2]