Jump to content

is there no TCPIPtoName() ?


piccaso
 Share

Recommended Posts

i miss the opposite of TCPNameToIp()

is there any?

Many names can resolve to a single IP address. For example, if I'm running a web server, ftp server and email server, they can all have the same IP address. You can query ARIN and get information about who owns the IP if you want.
Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

like this...

TCPStartup() ; WSAStartup  inside...
 
 $sIp = TCPNameToIP("www.hiddensoft.com")
 $vIp = DllCall("Ws2_32.dll","long","inet_addr","str",$sIp)
 $vIp = $vIp[0]
 
 $vHostent = DllCall("Ws2_32.dll","ptr","gethostbyaddr","long_ptr",$vIp,"int",4,"int",2) ; 2= AF_INET
 If @error Then ConsoleWrite(@error & @CR)
 $vHostent = $vHostent[0]
 
 $vzStrptr = DllStructCreate("ptr",$vHostent)
 $vzStr = DllStructCreate("char[256]",DllStructGetData($vzStrptr,1))
 ConsoleWrite(DllStructGetData($vzStr,1) & @LF)

hiddensoft.com -> (some numeric ip) -> autoitscript.com

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

like this...

TCPStartup() ; WSAStartup  inside...
 
 $sIp = TCPNameToIP("www.hiddensoft.com")
 $vIp = DllCall("Ws2_32.dll","long","inet_addr","str",$sIp)
 $vIp = $vIp[0]
 
 $vHostent = DllCall("Ws2_32.dll","ptr","gethostbyaddr","long_ptr",$vIp,"int",4,"int",2) ; 2= AF_INET
 If @error Then ConsoleWrite(@error & @CR)
 $vHostent = $vHostent[0]
 
 $vzStrptr = DllStructCreate("ptr",$vHostent)
 $vzStr = DllStructCreate("char[256]",DllStructGetData($vzStrptr,1))
 ConsoleWrite(DllStructGetData($vzStr,1) & @LF)

hiddensoft.com -> (some numeric ip) -> autoitscript.com

You started off with one name, resolved to an address and ended up with a totally different name. See what I mean about IP addresses can resolve to different names? Primary has no meaning when you resolve IP -> name, only from name -> IP.
Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

Really? And in the example above, which one would be the primary? The web server, the ftp server or the mail server?

depends on dns configuration

normaly if you have:

www.host.tld

smtp.host.tld

pop.host.tld

it resolves to host.tld but this realy differs throu the web...

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

if your behind a router you have multiple hostnames yourself but thats a diffrerent story :whistle:

and i have answered my support request myself, how nice ;)

Func _TCPIpToName($sIp,$hDll_Ws2_32 = "Ws2_32.dll")
    Local $vbinIP,$vaDllCall,$vptrHostent,$vzptrName
    Local $INADDR_NONE = 0xffffffff
    $vaDllCall = DllCall($hDll_Ws2_32,"long","inet_addr","str",$sIp)
    If @error Then Return SetError(1,0,"") ; inet_addr DllCall Failed
    $vbinIP = $vaDllCall[0]
    If $vbinIP = $INADDR_NONE Then Return SetError(2,0,"") ; inet_addr Failed
    $vaDllCall = DllCall($hDll_Ws2_32,"ptr","gethostbyaddr","long_ptr",$vbinIP,"int",4,"int",2) ; 2 = AF_INET
    If @error Then Return SetError(3,0,"") ; gethostbyaddr DllCall Failed
    $vptrHostent = $vaDllCall[0]
    If $vptrHostent = 0 Then
        $vaDllCall = DllCall($hDll_Ws2_32,"int","WSAGetLastError")
        If @error Then Return SetError(5,0,"") ; gethostbyaddr Failed, WSAGetLastError Failed
        Return SetError (4,$vaDllCall[0],"") ; gethostbyaddr Failed, WSAGetLastError = @Extended
    EndIf
    $vzptrName = DllStructCreate("ptr",$vptrHostent)
    Return __TCPIpToName_szStringRead(DllStructGetData($vzptrName,1))
EndFunc
; Internal
Func __TCPIpToName_szStringRead($iszPtr, $iLen = -1)
    Local $aStrLen, $vszString
    If $iszPtr < 1 Then Return ""
    If $iLen < 0 Then
        $aStrLen = DllCall("msvcrt.dll", "int", "strlen", "ptr", $iszPtr)
        If @error Then Return SetError(1, 0, "")
        $iLen = $aStrLen[0] + 1
    EndIf
    $vszString = DllStructCreate("char[" & $iLen & "]", $iszPtr)
    If @error Then Return SetError(2, 0, "")
    Return SetError(0, $iLen, DllStructGetData($vszString, 1))
EndFunc 

#include <inet.au3>
ConsoleWrite("Public hostname:" & _TCPIpToName(_GetIP()) & @LF)
ConsoleWrite("Local hostname:" & _TCPIpToName("127.0.0.1") & @LF)
ConsoleWrite("@IPAddress1 hostname:" & _TCPIpToName(@IPAddress1) & @LF)
;~ Public hostname:85-124-175-2.dynamic.xdsl-line.inode.at
;~ Local hostname:localhost
;~ @IPAddress1 hostname:Kunibert
CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

depends on dns configuration

normaly if you have:

www.host.tld

smtp.host.tld

pop.host.tld

it resolves to host.tld but this realy differs throu the web...

Not wanting to pick a fight. Glad that you found something to resolve IP to DNS, but I stand by my statement that there is no such thing as a "primary" name when resolving IP -> Name. :whistle:
Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

Hi,

I was looking for this function for a long time! :lmao:

Until now I was parsing the output of NBTSTAT.EXE.

Thank you for creating it!!!

I have one question:

I need it for resolving the local network names and I tried to remove the first two ConsoleWrite lines from your example, and it stopped working.

It seams that it needs to first call:

ConsoleWrite("Public hostname:" & _TCPIpToName(_GetIP()) & @LF)

and then:

ConsoleWrite("@IPAddress1 hostname:" & _TCPIpToName(@IPAddress1) & @LF)

to work. ;)

But this is a bit slow.

Can you please explain why is that, and is there a way to call only _TCPIpToName(<IPAddress>)?

Link to comment
Share on other sites

Can you please explain why is that, and is there a way to call only _TCPIpToName(<IPAddress>)?

the problem is not the ConsoleWrite, but the missing TCPStartup() call before _TCPIpToName. With that it works for me.

#include <inet.au3>
TCPStartup()
ConsoleWrite("Public hostname:" & _TCPIpToName("64.111.104.70") & @LF)

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

  • Developers

the above version in not complete, it doesent read out the aliases stroed in hostent struct.

i sent a complete version to jos and if he wants to it will be in the next beta...

pn me if you cant wait ;)

I haven't worked much on UDF submissions lately... motivation is down a bit the last week or so doing this stuff, but no worries, it will come back and I will pick up all submissions.

This one does sound useful to put in .......

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...