summary refs log tree commit diff stats
path: root/lib/pure/sockets.nim
diff options
context:
space:
mode:
authordom96 <dominikpicheta@googlemail.com>2011-05-14 20:32:31 +0100
committerdom96 <dominikpicheta@googlemail.com>2011-05-14 20:32:31 +0100
commitd1cd1cea345e82ff1f8b55803b81d68a86cb9d50 (patch)
treeded8f8c61d85178677ac6ed4ff3c84a71624a518 /lib/pure/sockets.nim
parent74b1b28f7e8d832a002502011e800405dedfb6dd (diff)
downloadNim-d1cd1cea345e82ff1f8b55803b81d68a86cb9d50.tar.gz
fixed some redis commands; fixed bindAddr and scgi now doesn't bind to all addresses. copy and delete for json module.
Diffstat (limited to 'lib/pure/sockets.nim')
-rwxr-xr-xlib/pure/sockets.nim28
1 files changed, 18 insertions, 10 deletions
diff --git a/lib/pure/sockets.nim b/lib/pure/sockets.nim
index 45e4da118..73f57395a 100755
--- a/lib/pure/sockets.nim
+++ b/lib/pure/sockets.nim
@@ -179,19 +179,27 @@ proc bindAddr*(socket: TSocket, port = TPort(0), address = "") =
   ## binds an address/port number to a socket.
   ## Use address string in dotted decimal form like "a.b.c.d"
   ## or leave "" for any address.
-  var name: Tsockaddr_in
-  when defined(Windows):
-    name.sin_family = int16(ord(AF_INET))
-  else:
-    name.sin_family = posix.AF_INET
-  name.sin_port = sockets.htons(int16(port))
+
   if address == "":
+    var name: Tsockaddr_in
+    when defined(Windows):
+      name.sin_family = int16(ord(AF_INET))
+    else:
+      name.sin_family = posix.AF_INET
+    name.sin_port = sockets.htons(int16(port))
     name.sin_addr.s_addr = sockets.htonl(INADDR_ANY)
+    if bindSocket(cint(socket), cast[ptr TSockAddr](addr(name)),
+                  sizeof(name)) < 0'i32:
+      OSError()
   else:
-    name.sin_addr.s_addr = parseIp4(address)
-  if bindSocket(cint(socket), cast[ptr TSockAddr](addr(name)),
-                sizeof(name)) < 0'i32:
-    OSError()
+    var hints: TAddrInfo
+    var aiList: ptr TAddrInfo = nil
+    hints.ai_family = toInt(AF_INET)
+    hints.ai_socktype = toInt(SOCK_STREAM)
+    hints.ai_protocol = toInt(IPPROTO_TCP)
+    if getAddrInfo(address, $port, addr(hints), aiList) != 0'i32: OSError()
+    if bindSocket(cint(socket), aiList.ai_addr, aiList.ai_addrLen) < 0'i32:
+      OSError()
 
 when false:
   proc bindAddr*(socket: TSocket, port = TPort(0)) =