slow reverse dns lookup

Just starting out? Need help? Post your questions and find answers here.
fluent
User
User
Posts: 68
Joined: Sun Jan 24, 2021 10:57 am

slow reverse dns lookup

Post by fluent »

On Windows, the following nslookup command returns in less than 200ms, and correctly states that 162.125.65.13 has no corresponding DNS name:

Code: Select all

nslookup 162.125.65.13
However, for the same IP address, gethostbyaddr_ takes more than 4 seconds to respond. How to speed it up?

Code: Select all

procedure.s IPtoHostName(IPaddress$)
#AF_INET = 2

Define *HOST.HOSTENT
Define LocalIPv4Address.L

InitNetwork()
LocalIPv4Address = MakeIPaddress (Val(StringField(IPaddress$, 1, ".")),
                                  Val(StringField(IPaddress$, 2, ".")),
                                  Val(StringField(IPaddress$, 3, ".")),
                                  Val(StringField(IPaddress$, 4, ".")))
*HOST = gethostbyaddr_(@LocalIPv4Address, 4, #AF_INET)

if *HOST
  HostName$ = PeekS(*HOST\h_name, -1, #PB_Ascii)
endif

ProcedureReturn Hostname$
EndProcedure


debug IPtoHostName("162.125.65.13")     

Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: slow reverse dns lookup

Post by Marc56us »

Hi fluent,

Not sure this will help, but I have seen this:

gethostbyaddr function (winsock2.h)

[gethostbyaddr is no longer recommended for use as of Windows Sockets 2. Instead, use getnameinfo.]

https://docs.microsoft.com/en-us/window ... hostbyaddr

https://docs.microsoft.com/en-us/window ... etnameinfo

(not tested)

:wink:
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: slow reverse dns lookup

Post by infratec »

Code: Select all

EnableExplicit


Import "Ws2_32.lib"
  getnameinfo(*pSockAddr, SockaddrLength.i, *pNodeBuffer, NodeBufferSize.l, Flags.i)
EndImport

;Debug @getnameinfo()

Define IPaddress$, *NodeBuffer, dwRetval.l
Define WSAData_.WSAData
Define saGNI.sockaddr_in

IPaddress$ = "8.8.8.8"

*NodeBuffer = AllocateMemory(1024)
If *NodeBuffer
  WSAStartup_(1, @WSAData_)
  
  saGNI\sin_family = #AF_INET
  saGNI\sin_addr = MakeIPAddress (Val(StringField(IPaddress$, 1, ".")), Val(StringField(IPaddress$, 2, ".")), Val(StringField(IPaddress$, 3, ".")), Val(StringField(IPaddress$, 4, ".")))
  saGNI\sin_port = 27015
  
  dwRetval = getnameinfo(@saGNI, SizeOf(sockaddr_in), *NodeBuffer, MemorySize(*NodeBuffer), 0)
  If dwRetval = 0
    Debug PeekS(*NodeBuffer, -1, #PB_Ascii)
  EndIf
  
  WSACleanup_()
  
  FreeMemory(*NodeBuffer)
EndIf
But it's not faster the first time.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: slow reverse dns lookup

Post by Lunasole »

infratec wrote: But it's not faster the first time.
It also didn't worked correctly in 32 bit version (returned string "dns" instead of "dns.google.com" in that example with 8.8.8.8 ), didn't get why.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
HeX0R
Addict
Addict
Posts: 979
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: slow reverse dns lookup

Post by HeX0R »

Shouldn't that be:

Code: Select all

Import "Ws2_32.lib"
  getnameinfo(*pSockAddr, SockaddrLength.i, *pNodeBuffer, NodeBufferSize.l, *ServiceBuffer, ServiceBufferSize.l, Flags.i)
EndImport
and then

Code: Select all

dwRetval = getnameinfo(@saGNI, SizeOf(sockaddr_in), *NodeBuffer, MemorySize(*NodeBuffer), 0, 0, 0)
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Re: slow reverse dns lookup

Post by ZX80 »

Hi, 2 all

I get "dns.google" string with infratec's code (on my 32-bit W7).
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: slow reverse dns lookup

Post by BarryG »

Does this help?

Code: Select all

p=RunProgram("nslookup", "162.125.65.13", "", #PB_Program_Open | #PB_Program_Error | #PB_Program_Read | #PB_Program_Hide)
While ProgramRunning(p)
  If AvailableProgramOutput(p)
    o$+ReadProgramString(p)+#CR$
  EndIf
Wend
e$=ReadProgramError(p)
If e$
  o$+e$+#CR$
EndIf
CloseProgram(p)

Debug o$
fluent
User
User
Posts: 68
Joined: Sun Jan 24, 2021 10:57 am

Re: slow reverse dns lookup

Post by fluent »

BarryG wrote:Does this help?

Code: Select all

p=RunProgram("nslookup", "162.125.65.13", "", #PB_Program_Open | #PB_Program_Error | #PB_Program_Read | #PB_Program_Hide)
While ProgramRunning(p)
  If AvailableProgramOutput(p)
    o$+ReadProgramString(p)+#CR$
  EndIf
Wend
e$=ReadProgramError(p)
If e$
  o$+e$+#CR$
EndIf
CloseProgram(p)

Debug o$
This is the exact method I have been using so far. I am looking for a pure API approach, but unfortunately all the ones I tried were much slower than nslookup for some reason (specifically when no match exists, as for 162.125.65.13)
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: slow reverse dns lookup

Post by Lunasole »

HeX0R wrote:Shouldn't that be:

Code: Select all

Import "Ws2_32.lib"
  getnameinfo(*pSockAddr, SockaddrLength.i, *pNodeBuffer, NodeBufferSize.l, *ServiceBuffer, ServiceBufferSize.l, Flags.i)
EndImport
and then

Code: Select all

dwRetval = getnameinfo(@saGNI, SizeOf(sockaddr_in), *NodeBuffer, MemorySize(*NodeBuffer), 0, 0, 0)
Seems fine, after your patch returns what expected both in x86 and x64 (at least Win7).
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply