Make asyncnet.bindAddr IPv4+IPv6 compatible.

This commit is contained in:
Andrew Yourtchenko 2015-06-23 23:02:10 +02:00
commit 93e2d9f979

View file

@ -423,20 +423,17 @@ proc bindAddr*(socket: AsyncSocket, port = Port(0), address = "") {.
## Binds ``address``:``port`` to the socket. ## Binds ``address``:``port`` to the socket.
## ##
## If ``address`` is "" then ADDR_ANY will be bound. ## If ``address`` is "" then ADDR_ANY will be bound.
var sockDomain = getSockDomain(socket.fd)
if address == "": var realaddr = address
var name: Sockaddr_in if realaddr == "":
when defined(Windows) or defined(nimdoc): case sockDomain
name.sin_family = toInt(AF_INET).int16 of AF_INET6: realaddr = "::"
of AF_INET: realaddr = "0.0.0.0"
else: else:
name.sin_family = toInt(AF_INET) raiseOSError("Unknown socket address family and no address specified to bindAddr")
name.sin_port = htons(int16(port))
name.sin_addr.s_addr = htonl(INADDR_ANY) var aiList = getAddrInfo(realaddr, port, sockDomain)
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: if bindAddr(socket.fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32:
dealloc(aiList) dealloc(aiList)
raiseOSError(osLastError()) raiseOSError(osLastError())