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:
parent
bc66788b3f
commit
252f668829
2 changed files with 98 additions and 31 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue