From 5978625c12c705a23ea4d2975162f55b160074ea Mon Sep 17 00:00:00 2001 From: Nathan Hoad Date: Sat, 7 Mar 2015 12:37:52 +1100 Subject: Make IP address checking more obvious, and add tests for the net module. --- lib/pure/net.nim | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'lib/pure/net.nim') diff --git a/lib/pure/net.nim b/lib/pure/net.nim index f7fcea06d..5361994f8 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -95,6 +95,7 @@ type address_v4*: array[0..3, uint8] ## Contains the IP address in bytes in ## case of IPv4 +proc isIpAddress*(address_str: string): bool proc parseIpAddress*(address_str: string): TIpAddress proc isDisconnectionError*(flags: set[SocketFlag], @@ -527,9 +528,8 @@ proc connect*(socket: Socket, address: string, port = Port(0), when defined(ssl): if socket.isSSL: - try: - discard parseIpAddress(address) - except ValueError: + # RFC3546 for SNI specifies that IP addresses are not allowed. + if not isIpAddress(address): # Discard result in case OpenSSL version doesn't support SNI, or we're # not using TLSv1+ discard SSL_set_tlsext_host_name(socket.sslHandle, address) @@ -1259,3 +1259,13 @@ proc parseIpAddress*(address_str: string): TIpAddress = return parseIPv6Address(address_str) else: return parseIPv4Address(address_str) + + +proc isIpAddress*(address_str: string): bool = + ## Checks if a string is an IP address + ## Returns true if it is, false otherwise + try: + discard parseIpAddress(address_str) + except ValueError: + return false + return true -- cgit 1.4.1-2-gfad0