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