Jump to content
Sign in to follow this  
tatane

Need Help/Example to use getnameinfo function (WSAAPI)

Recommended Posts

tatane

Hello,

I'm trying to use getnameinfo instead of gethostbyaddr to get the netbios name of computers in a LAN.

gethostbyaddr (used in _TCPIpToName() function) works but is very slow to respond. It seems getnameinfo works faster.

I'm a newbie with DllCall so don't be too harsh with me ^^'.

Here is what i've tried :

;~ struct sockaddr_in {
;~         short   sin_family;
;~         u_short sin_port;
;~         struct  in_addr sin_addr;
;~         char    sin_zero[8];
;~ };

;~ int WSAAPI getnameinfo(
;~   _In_   const struct sockaddr FAR *sa,
;~   _In_   socklen_t salen,
;~   _Out_  char FAR *host,
;~   _In_   DWORD hostlen,
;~   _Out_  char FAR *serv,
;~   _In_   DWORD servlen,
;~   _In_   int flags
;~ );

Local $host, $service

TCPStartup()

Local $ip = "192.168.0.1"
Local $port = 139
Local $hDll_Ws2_32 = "ws2_32.dll"

$ret_inetaddr = DllCall($hDll_Ws2_32, "ulong", "inet_addr", "str", $ip)

$ret_hton = DllCall($hDll_Ws2_32, "USHORT", "htons", "USHORT", $port)

$sockaddr = DllStructCreate("short sin_family ; USHORT port ; ptr in_adrr ; char sin_zero[8]")
DllStructSetData($sockaddr, "sin_family", 2) ; AF_INET
DllStructSetData($sockaddr, "port", $ret_hton[0])
DllStructSetData($sockaddr, "in_adrr", DllStructGetPtr($ret_inetaddr))

$ret = DllCall($hDll_Ws2_32, "int", "getnameinfo" _
        , "ptr", DllStructGetPtr($sockaddr) _
        , "int", DllStructGetSize($sockaddr) _
        , "str*", $host _
        , "dword", 1025 _
        , "str*", $service _
        , "dword", 0 _
        , "int", 0)

ConsoleWrite("error=" & @error & "   $ret[0]=" & $ret[0] & "   host=" & $host & @CR)

TCPShutdown()

Thank you for your time and sorry for my english.

Share this post


Link to post
Share on other sites
tatane

Ok, I found.

Here is the working code :

TCPStartup()

Local $ip = "192.168.0.1"
Local $port = 139
Local $hDll_Ws2_32 = "ws2_32.dll"

$ret_inetaddr = DllCall($hDll_Ws2_32, "ulong", "inet_addr", "str", $ip) ; INADDR_NONE = 4294967295
$ret_hton = DllCall($hDll_Ws2_32, "USHORT", "htons", "USHORT", $port)

$sockaddr = DllStructCreate("short sin_family ; USHORT port ; ptr in_adrr ; char sin_zero[8]")
DllStructSetData($sockaddr, "sin_family", 2) ; AF_INET
DllStructSetData($sockaddr, "port", $ret_hton[0])
DllStructSetData($sockaddr, "in_adrr", $ret_inetaddr[0])

Local $dll_struct_host = DllStructCreate("char[1025]")
Local $dll_struct_service = DllStructCreate("char[32]")

$ret_getnameinfo = DllCall($hDll_Ws2_32, "int", "getnameinfo" _
        , "ptr", DllStructGetPtr($sockaddr) _
        , "int", DllStructGetSize($sockaddr) _
        , "ptr", DllStructGetPtr($dll_struct_host) _
        , "dword", DllStructGetSize($dll_struct_host) _
        , "ptr", DllStructGetPtr($dll_struct_service) _
        , "dword", DllStructGetSize($dll_struct_service) _
        , "int", 0x0004) ; NI_NOFQDN

ConsoleWrite("host=" & DllStructGetData($dll_struct_host,1) & "  service=" & DllStructGetData($dll_struct_service,1) & @CR)

TCPShutdown()

It seems the responding time is as long as "gethostbyaddr"... :(

Edited by tatane

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  

×