summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorAndrew Yourtchenko <ayourtch@gmail.com>2015-06-23 23:02:10 +0200
committerAndrew Yourtchenko <ayourtch@gmail.com>2015-06-23 23:02:10 +0200
commit93e2d9f9794c637352a167eba2364e2ee7154954 (patch)
tree0af6816bedf1099c6e9f6e8edd55f414b1ac1cc9 /lib/pure
parentf340f76f45170535effa959543f4a458e1964815 (diff)
downloadNim-93e2d9f9794c637352a167eba2364e2ee7154954.tar.gz
Make asyncnet.bindAddr IPv4+IPv6 compatible.
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/asyncnet.nim27
1 files changed, 12 insertions, 15 deletions
diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim
index 712bba33b..2921de681 100644
--- a/lib/pure/asyncnet.nim
+++ b/lib/pure/asyncnet.nim
@@ -423,24 +423,21 @@ proc bindAddr*(socket: AsyncSocket, port = Port(0), address = "") {.
   ## Binds ``address``:``port`` to the socket.
   ##
   ## If ``address`` is "" then ADDR_ANY will be bound.
+  var sockDomain = getSockDomain(socket.fd)
 
-  if address == "":
-    var name: Sockaddr_in
-    when defined(Windows) or defined(nimdoc):
-      name.sin_family = toInt(AF_INET).int16
+  var realaddr = address
+  if realaddr == "":
+    case sockDomain
+    of AF_INET6: realaddr = "::"
+    of AF_INET:  realaddr = "0.0.0.0"
     else:
-      name.sin_family = toInt(AF_INET)
-    name.sin_port = htons(int16(port))
-    name.sin_addr.s_addr = htonl(INADDR_ANY)
-    if bindAddr(socket.fd, cast[ptr SockAddr](addr(name)),
-                  sizeof(name).Socklen) < 0'i32:
-      raiseOSError(osLastError())
-  else:
-    var aiList = getAddrInfo(address, port, AF_INET)
-    if bindAddr(socket.fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32:
-      dealloc(aiList)
-      raiseOSError(osLastError())
+      raiseOSError("Unknown socket address family and no address specified to bindAddr")
+
+  var aiList = getAddrInfo(realaddr, port, sockDomain)
+  if bindAddr(socket.fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32:
     dealloc(aiList)
+    raiseOSError(osLastError())
+  dealloc(aiList)
 
 proc close*(socket: AsyncSocket) =
   ## Closes the socket.