lookup IP by netbios name on LAN?

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

lookup IP by netbios name on LAN?

Post by jassing »

I have some code for doing a dns lookup -- which works fine; however, if I drop to a console window and do:
ping dslmodem
It does a proper lookup and fetches the IP.

However, the code I am using fails to find the local IP address -- Any ideas?

Code: Select all

#DNS_TYPE_A          = $0001      ; //  1
#DNS_TYPE_NS         = $0002      ; //  2
#DNS_TYPE_MD         = $0003      ; //  3
#DNS_TYPE_MF         = $0004      ; //  4
#DNS_TYPE_CNAME      = $0005      ; //  5
#DNS_TYPE_SOA        = $0006      ; //  6
#DNS_TYPE_MB         = $0007      ; //  7
#DNS_TYPE_MG         = $0008      ; //  8
#DNS_TYPE_MR         = $0009      ; //  9
#DNS_TYPE_NULL       = $000a      ; //  10
#DNS_TYPE_WKS        = $000b      ; //  11
#DNS_TYPE_PTR        = $000c      ; //  12
#DNS_TYPE_HINFO      = $000d      ; //  13
#DNS_TYPE_MINFO      = $000e      ; //  14
#DNS_TYPE_MX         = $000f      ; //  15
#DNS_TYPE_TEXT       = $0010      ; //  16

#DNS_QUERY_STANDARD                  = $00000000
#DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = $00000001
#DNS_QUERY_USE_TCP_ONLY              = $00000002
#DNS_QUERY_NO_RECURSION              = $00000004
#DNS_QUERY_BYPASS_CACHE              = $00000008

#DNS_QUERY_NO_WIRE_QUERY             = $00000010
#DNS_QUERY_NO_LOCAL_NAME             = $00000020
#DNS_QUERY_NO_HOSTS_FILE             = $00000040
#DNS_QUERY_NO_NETBT                  = $00000080

#DNS_QUERY_WIRE_ONLY                 = $00000100
#DNS_QUERY_RETURN_MESSAGE            = $00000200

#DNS_QUERY_TREAT_AS_FQDN             = $00001000
#DNS_QUERY_DONT_RESET_TTL_VALUES     = $00100000
#DNS_QUERY_RESERVED                  = $ff000000

Enumeration ; DNS_FREE_TYPE
  #DnsFreeFlat = 0
  #DnsFreeRecordList
  #DnsFreeParsedMessageFields
EndEnumeration 

Structure DNS_A_DATA
  IpAddress.l
EndStructure

Structure DNS_PTR_DATAA
  *pNameHost
EndStructure

Structure DNS_RECORD 
  *pNext.DNS_RECORD;  
  pName.s
  wType.w
  wDataLength.w
  StructureUnion
    DW.l 
    S.l
  EndStructureUnion
  dwTtl.l
  dwReserved.l
  
  ; Note: The Union def is incomplete. see DNS_RECORD in the psdk for more fields
  StructureUnion
    A.DNS_A_DATA 
    PTR.DNS_PTR_DATAA
    CNAME.DNS_PTR_DATAA
  EndStructureUnion 
EndStructure

Prototype DnsQuery_(Name.s, wType.w, fOptions.l, *aopServers, *ppQueryResultSet, *pReserved)
Prototype DnsRecordListFree(*RecordList, FreeType)
Global DnsQuery_.DnsQuery_, DnsRecordListFree.DnsRecordListFree

; =========================================================

; Load the Dnsapi.dll. Use it just like OpenLibrary()
; Library is the #Library number for the new lib (can be #PB_Any)
;
Procedure LoadDnsApi(Library)
  Protected Result

  Result = OpenLibrary(Library, "Dnsapi.dll")
  If Result
    If Library = #PB_Any
      Library = Result
    EndIf
    
    CompilerIf #PB_Compiler_Unicode
      DnsQuery_ = GetFunction(Library, "DnsQuery_W")
    CompilerElse
       DnsQuery_ = GetFunction(Library, "DnsQuery_A")
    CompilerEndIf
    
    DnsRecordListFree = GetFunction(Library, "DnsRecordListFree")    
    
    If DnsQuery_ = 0 Or  DnsRecordListFree = 0
      CloseLibrary(Library)
      Result = 0
    EndIf      
  EndIf

  ProcedureReturn Result
EndProcedure

; Get the IP for the server name
; returns 0 on failure
;
ProcedureDLL.l DnsQuery(ServerName$, nRecordType = #DNS_TYPE_A)
  Protected IP = 0, CName$, *Record.DNS_RECORD
  
  If DnsQuery_ And DnsRecordListFree
    If DnsQuery_(ServerName$, nRecordType, #DNS_QUERY_STANDARD|#DNS_QUERY_NO_HOSTS_FILE, #Null, @*Record.DNS_RECORD, #Null) = 0 And *Record
      If *Record\wType = #DNS_TYPE_A ; dns record
        IP = *Record\A\IpAddress
      ElseIf *Record\wType = #DNS_TYPE_CNAME Or *Record\wType = #DNS_TYPE_MX; redirection
        CName$ = PeekS(*Record\CNAME\pNameHost)
        If CName$
          IP = DnsQuery(CName$)
        EndIf
      EndIf 
      DnsRecordListFree(*Record, #DnsFreeRecordList)
    EndIf
  EndIf
  
  ProcedureReturn IP
EndProcedure
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: lookup IP by netbios name on LAN?

Post by SFSxOI »

Just a quick guess initially, but maybe something like this?

Code: Select all

Procedure.s IpFromName(name$)
  ProcedureReturn PeekS(inet_ntoa_(inet_addr_(name$)))
EndProcedure

Debug IpFromName("PC-NETBIOS-Name")
Or... you could use the command prompt with nbtstat programatically like this and modify it to return only the IP address:

Code: Select all

Structure CmdP_STARTUPINFO ;->Structure CmdP_STARTUPINFO
  cb.l 
  lpReserved.l 
  lpDesktop.l 
  lpTitle.l 
  dwX.l 
  dwY.l 
  dwXSize.l 
  dwYSize.l 
  dwXCountChars.l 
  dwYCountChars.l 
  dwFillAttribute.l 
  dwFlags.l 
  wShowWindow.w 
  cbReserved2.w 
  lpReserved2.l 
  hStdInput.l 
  hStdOutput.l 
  hStdError.l 
EndStructure

Global proc.PROCESS_INFORMATION, start.CmdP_STARTUPINFO, sa.SECURITY_ATTRIBUTES, hReadP.l, hWriteP.l, lngBytesread.l, strBuff.s=Space(256)

Procedure.s Rd_cmd(comnd_send.s)
sa\nLength =SizeOf(SECURITY_ATTRIBUTES)
sa\bInheritHandle = 1 
sa\lpSecurityDescriptor = 0 
ret = CreatePipe_(@hReadP, @hWriteP, @sa, 0)

  If ret = 0 
    MessageRequester("Information", "CreateP failed. Error: ",0) 
    End 
  EndIf 

start\cb = SizeOf(CmdP_STARTUPINFO) 
start\dwFlags = #STARTF_USESHOWWINDOW | #STARTF_USESTDHANDLES 
start\hStdOutput = hWriteP 
start\hStdError = hWriteP 

ret = CreateProcess_(0, comnd_send, sa, sa, 1, #NORMAL_PRIORITY_CLASS, 0, 0, @start, @proc) 

  If ret <> 1 
    MessageRequester("Information","File Or command not found", 0) 
    End 
  EndIf 

ret = CloseHandle_(hWriteP) 

mOutputs.s = "" 

  While ret<>0 
    ret = ReadFile_(hReadP, strBuff, 255, @lngBytesread, 0) 
      If lngBytesread>0 
        mOutputs = mOutputs + Left(strBuff, lngBytesread) 
      EndIf 
  Wend 

ret = CloseHandle_(proc\hProcess) 
ret = CloseHandle_(proc\hThread) 
ret = CloseHandle_(hReadP) 

ProcedureReturn mOutputs
EndProcedure

Debug Rd_cmd("nbtstat -a PC-NETBIOS-NAME")
Or... if those don't work out for you then you could come up with something using the Name Resolution Functions in windows > http://msdn.microsoft.com/en-us/library ... s.85).aspx
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: lookup IP by netbios name on LAN?

Post by LuCiFeR[SD] »

Jassing, when you say fails... in what sense of the word? Nothing is returned, or you are getting a number returned which doesn't look like an IP address? Cause if it is the latter, then you have two options.

You use PB's network commands to convert the number to an IPstring eg.

Code: Select all

;add this somewhere after the procedures.
InitNetwork()
ServerName$=Hostname()
Debug ServerName$
LoadDnsApi(#PB_Any)

IP=DnsQuery(ServerName$, nRecordType = #DNS_TYPE_A)
Debug IP
Debug IPString(IP)
or you could parse the returned value yourself a byte at a time and convert it into the format you were expecting.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: lookup IP by netbios name on LAN?

Post by jassing »

LuCiFeR[SD] wrote:Jassing, when you say fails... in what sense of the word?
In the sense that it fails to resolve the netbios name to an IP address.
Post Reply