Sign in to follow this  
Followers 0
rudi

_TCPIPtoName() extremly slow, if no PTR record exists.

4 posts in this topic

Hi.

Reverse lookups with _TCPIPtoName() are extreeemly slow, if there is NO PTR record (NSLOOKUP.EXE resolution returns "non existent domain")

This takes several seconds for each and every unresolvable ip address:

#include <inet.au3>

TCPStartup()

$Range="10.10.10." ; replace this with a net address your DNS server is managing PTR records for.

for $i =1 to  255
    $foo=_TCPIPtoname($Range & $i)
    if @error then $foo = "couldn't be resolved. " & @error
    ConsoleWrite($i & " = " & $foo & @CRLF)
Next

TCPShutdown()

ConsoleWrite("Done." & @CRLF)

nslookup.exe is doing the same very fast (if the DNS Server is servicing the PTR range):

set range=10.10.10.
for /l %a in (1,1,254) do (echo ---------------------- >> result.txt && nslookup %range%%a >>result.txt)

What do I miss?

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Local Const $sRange = "189.108.105."
Local $sName, $sAddress

TCPStartup()

For $i = 100 To 200
    $sAddress = $sRange & $i
    $sName = _ReverseLookup($sAddress)
    ConsoleWrite(StringFormat("\tName:  %s\n\tAddress:  %s\n", $sName, $sAddress))
Next

TCPShutdown()

Func _ReverseLookup($sIP, $iTimeout = 1000)
    Local Const $AF_INET = 2, $SOCK_DGRAM = 2, $IPPROTO_IP = 0
    Local $socket = _socket($AF_INET, $SOCK_DGRAM, $IPPROTO_IP)
    Local $tSockAddr = DllStructCreate("ushort;byte[14]"), $pSockAddr = DllStructGetPtr($tSockAddr), $iSockAddr = DllStructGetSize($tSockAddr)
    Local $sDNSServer = _GetPrimaryDnsServerAddress()

    DllStructSetData($tSockAddr, 1, $AF_INET)
    DllStructSetData($tSockAddr, 2, Binary("0x0035") & Binary(_inet_addr($sDNSServer)))
    _connect($socket, $pSockAddr, $iSockAddr)

    Local $tData = _GeneratePacket($sIP), $pData = DllStructGetPtr($tData), $iData = DllStructGetSize($tData)
    _send($socket, $pData, $iData)

    Local $tReadSockets = DllStructCreate("int;int"), $pReadSockets = DllStructGetPtr($tReadSockets)
    Local $tTimeout = DllStructCreate("int;int"), $pTimeout = DllStructGetPtr($tTimeout)

    DllStructSetData($tReadSockets, 1, 1)
    DllStructSetData($tReadSockets, 2, $socket)
    If $iTimeout < 100 Then $iTimeout = 100
    DllStructSetData($tTimeout, 1, Int($iTimeout/1000))
    DllStructSetData($tTimeout, 2, Mod($iTimeout, 1000))

    Local $iRet = _select($socket, $pReadSockets, 0, 0, $pTimeout)
    If $iRet = 0 Then
        _closesocket($socket)
        Return "DNS request timeout."
    EndIf

    Local $tRecvBuf = DllStructCreate("byte[65536]"), $pRecvBuf = DllStructGetPtr($tRecvBuf), $iRecvBuf = DllStructGetSize($tRecvBuf)
    Local $iReceived = _recv($socket, $pRecvBuf, $iRecvBuf)
    Local $xRecvData = BinaryMid(DllStructGetData($tRecvBuf, 1), 1, $iReceived)
    _closesocket($socket)

    Return _ParsePacket($xRecvData)
EndFunc


Func _socket($iaf, $itype, $iprotocol)
    Local $aResult = DllCall("wsock32.dll", "int", "socket", "int", $iaf, "int", $itype, "int", $iprotocol)

    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] = -1 Then Return SetError(-1, 0, 0)
    Return $aResult[0]
EndFunc

Func _closesocket($socket)
    Local $aResult = DllCall("wsock32.dll", "int", "closesocket", "int", $socket)

    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] = -1 Then Return SetError(-1, 0, 0)
    Return True
EndFunc

Func _connect($socket, $pname, $inamelen)
    Local $aResult = DllCall("wsock32.dll", "int", "connect", "int", $socket, "ptr", $pname, "int", $inamelen)

    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] = -1 Then Return SetError(-1, 0, 0)
    Return True
EndFunc

Func _recv($socket, $pbuffer, $ibuflen, $iflags = 0)
    Local $aResult = DllCall("wsock32.dll", "int", "recv", "int", $socket, "ptr", $pbuffer, "int", $ibuflen, "int", $iflags)

    If $aResult[0] = -1 Then Return SetError(-1, 0, 0)
    Return SetError($aResult[0] = 0, 0, $aResult[0])
EndFunc

Func _send($socket, $pbuffer, $ibuflen, $iflags = 0)
    Local $aResult = DllCall("wsock32.dll", "int", "send", "int", $socket, "ptr", $pbuffer, "int", $ibuflen, "int", $iflags)

    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] = -1 Then Return SetError(-1, 0, 0)
    Return SetError($aResult[0] = 0, 0, $aResult[0])
EndFunc

Func _select($socket, $preadfds, $pwritefds, $pexceptfds, $ptimeout)
    Local $aResult = DllCall("wsock32.dll", "int", "select", "int", $socket, "ptr", $preadfds, "ptr", $pwritefds, "ptr", $pexceptfds, "ptr", $ptimeout)

    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] = -1 Then Return SetError(-1, 0, 0)
    Return SetError($aResult[0] = 0, 0, $aResult[0])
EndFunc

Func _inet_addr($sIP)
    Local $aResult = DllCall("wsock32.dll", "int", "inet_addr", "str", $sIP)

    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] = -1 Then Return SetError(-1, 0, 0)
    Return SetError($aResult[0] = 0, 0, $aResult[0])
EndFunc

Func _GetPrimaryDnsServerAddress()
    Local $aResult, $tBuf, $tDnsServersList

    $aResult = DllCall("iphlpapi.dll", "uint", "GetNetworkParams", "int*", 0, "uint*", 4)

    If $aResult[0] = 111 Then
        $tBuf = DllStructCreate("byte[" & $aResult[2] & "]")
        $aResult = DllCall("iphlpapi.dll", "uint", "GetNetworkParams", "ptr", DllStructGetPtr($tBuf), "uint*", $aResult[2])
        If $aResult[0] <> 0 Then Return SetError($aResult[0], 0, "")
        $tDnsServersList = DllStructCreate("ptr;char[16];char[16];uint;", DllStructGetPtr($tBuf)+268)
        Return DllStructGetData($tDnsServersList, 2)
    Else
        Return SetError(-1, 0, "")
    EndIf
EndFunc

Func _GeneratePacket($sIP)
    Local $aParts = StringSplit($sIP, "."), $xData = Binary("0x001701000001000000000000"), $tRet

    For $i = 4 To 1 Step -1
        $xData &= Binary("0x" & Hex(StringLen($aParts[$i]), 2)) & StringToBinary($aParts[$i])
    Next

    $xData &= Binary("0x07696E2D61646472046172706100000C0001")
    $tRet = DllStructCreate("byte[" & BinaryLen($xData) & "]")
    DllStructSetData($tRet, 1, $xData)
    Return $tRet
EndFunc

Func _ParsePacket($xData)
    Local $sRet, $iErr, $iPos, $iChrs

    $iErr = BitAND(BinaryMid($xData, 4, 1), 0xf)

    If $iErr <> 0 Then
        Switch $iErr
            Case 2
                $sRet = "Server failed"
            Case 3
                $sRet = "Non-existent domain"
            Case 4
                $sRet = "Not implemented"
            Case 5
                $sRet = "Query refused"
            Case 0xf
                $sRet = "No change"
            Case Else
                $sRet = "Contact Microsoft©. Ask them what to do"
        EndSwitch
    Else
        ; Step over the "in-addr.arpa" part
        $iPos = 13
        Do
            $iPos += Number(BinaryMid($xData, $iPos, 1)) + 1
        Until Number(BinaryMid($xData, $iPos, 1)) = 0

        $iPos += 17
        $iChrs = Number(BinaryMid($xData, $iPos, 1))
        $sRet = ""

        While $iChrs
            $sRet &= BinaryToString(BinaryMid($xData, $iPos + 1, $iChrs)) & "."
            $iPos += $iChrs + 1
            $iChrs = Number(BinaryMid($xData, $iPos, 1))
        WEnd

        $sRet = StringTrimRight($sRet, 1)
    EndIf

    Return $sRet
EndFunc

Nslookup sends a packet through dgram socket on port 53, among other things.

Deciphering the sent and received packets requires more, but for now this script should do the trick.

P.S. If sending or receiving packets takes long time you can use the select function.

Edit: Added timeout option.

Edited by Authenticity
1 person likes this

Share this post


Link to post
Share on other sites

Hi.

Nslookup sends a packet through dgram socket on port 53, among other things.

Deciphering the sent and received packets requires more, but for now this script should do the trick.

Thanks ;) , I'll be able to test it later this week, when I'm back in the office. :)

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Thank you Authenticity!

I'm very impressed that you would write this code in response to a question.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0