add a second asyncnet.recvFrom (#14237)

* add a second asyncnet.recvFrom

* fixes

* pre-allocating address

* add a new nativesockets.getAddrString()
This commit is contained in:
rockcavera 2020-06-27 18:59:08 -03:00 • committed by GitHub
commit 252f668829
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 98 additions and 31 deletions

View file

@ -461,6 +461,40 @@ proc getAddrString*(sockAddr: ptr SockAddr): string =
return "unix"
raise newException(IOError, "Unknown socket family in getAddrString")
proc getAddrString*(sockAddr: ptr SockAddr, strAddress: var string) =
## Stores in ``strAddress`` the string representation of the address inside
## ``sockAddr``
##
## **Note**
## * ``strAddress`` must be initialized to 46 in length.
assert(46 == len(strAddress),
"`strAddress` was not initialized correctly. 46 != `len(strAddress)`")
if sockAddr.sa_family.cint == nativeAfInet:
let addr4 = addr cast[ptr Sockaddr_in](sockAddr).sin_addr
when not useWinVersion:
if posix.inet_ntop(posix.AF_INET, addr4, addr strAddress[0],
strAddress.len.int32) == nil:
raiseOSError(osLastError())
else:
if winlean.inet_ntop(winlean.AF_INET, addr4, addr strAddress[0],
strAddress.len.int32) == nil:
raiseOSError(osLastError())
elif sockAddr.sa_family.cint == nativeAfInet6:
let addr6 = addr cast[ptr Sockaddr_in6](sockAddr).sin6_addr
when not useWinVersion:
if posix.inet_ntop(posix.AF_INET6, addr6, addr strAddress[0],
strAddress.len.int32) == nil:
raiseOSError(osLastError())
if posix.IN6_IS_ADDR_V4MAPPED(addr6) != 0:
strAddress = strAddress.substr("::ffff:".len)
else:
if winlean.inet_ntop(winlean.AF_INET6, addr6, addr strAddress[0],
strAddress.len.int32) == nil:
raiseOSError(osLastError())
else:
raise newException(IOError, "Unknown socket family in getAddrString")
setLen(strAddress, len(cstring(strAddress)))
when defined(posix) and not defined(nimdoc):
proc makeUnixAddr*(path: string): Sockaddr_un =
result.sun_family = AF_UNIX.TSa_Family