Skip to content

Commit

Permalink
fix #22834
Browse files Browse the repository at this point in the history
  • Loading branch information
rockcavera authored Oct 19, 2023
1 parent 05a7c0f commit d6c6baf
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/pure/nativesockets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -387,21 +387,37 @@ when not useNimNetLite:

proc getHostByAddr*(ip: string): Hostent {.tags: [ReadIOEffect].} =
## This function will lookup the hostname of an IP Address.
var myaddr: InAddr
myaddr.s_addr = inet_addr(ip)
var
addrInfo = getAddrInfo(ip, Port(0), AF_UNSPEC)
myAddr: pointer
addrLen = 0
family = 0

if addrInfo.ai_addr.sa_family == nativeAfInet:
family = nativeAfInet
myAddr = addr cast[ptr Sockaddr_in](addrInfo.ai_addr).sin_addr
addrLen = 4
elif addrInfo.ai_addr.sa_family == nativeAfInet6:
family = nativeAfInet6
myAddr = addr cast[ptr Sockaddr_in6](addrInfo.ai_addr).sin6_addr
addrLen = 16
else:
raise newException(IOError, "Unknown socket family in `getHostByAddr()`")

freeAddrInfo(addrInfo)

when useWinVersion:
var s = winlean.gethostbyaddr(addr(myaddr), sizeof(myaddr).cuint,
cint(AF_INET))
var s = winlean.gethostbyaddr(cast[ptr InAddr](myAddr), addrLen.cuint,
cint(family))
if s == nil: raiseOSError(osLastError())
else:
var s =
when defined(android4):
posix.gethostbyaddr(cast[cstring](addr(myaddr)), sizeof(myaddr).cint,
cint(posix.AF_INET))
posix.gethostbyaddr(cast[cstring](myAddr), addrLen.cint,
cint(family))
else:
posix.gethostbyaddr(addr(myaddr), sizeof(myaddr).SockLen,
cint(posix.AF_INET))
posix.gethostbyaddr(myAddr, addrLen.SockLen,
cint(family))
if s == nil:
raiseOSError(osLastError(), $hstrerror(h_errno))

Expand Down

0 comments on commit d6c6baf

Please sign in to comment.