Allows to get the IP for a name, and the name for an IP:
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_W(Name.p-unicode, wType.w, fOptions.l, *aopServers, *ppQueryResultSet, *pReserved)
Prototype DnsRecordListFree(*RecordList, FreeType)
Global DnsQuery_W.DnsQuery_W, 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
DnsQuery_W = GetFunction(Library, "DnsQuery_W")
DnsRecordListFree = GetFunction(Library, "DnsRecordListFree")
If DnsQuery_W = 0 Or DnsRecordListFree = 0
CloseLibrary(Library)
Result = 0
EndIf
EndIf
ProcedureReturn Result
EndProcedure
; Get the IP for the server name
; returns 0 on failure
;
Procedure.l DnsQuery(ServerName$)
Protected IP = 0, CName$, *Record.DNS_RECORD
If DnsQuery_W And DnsRecordListFree
If DnsQuery_W(ServerName$, #DNS_TYPE_A, #DNS_QUERY_STANDARD, #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 ; redirection
CName$ = PeekS(*Record\CNAME\pNameHost, -1, #PB_Unicode)
If CName$
IP = DnsQuery(CName$)
EndIf
EndIf
DnsRecordListFree(*Record, #DnsFreeRecordList)
EndIf
EndIf
ProcedureReturn IP
EndProcedure
; Get the name for the given IP
; returns "" on failure
;
Procedure.s ReverseDnsQuery(IP.l)
Protected Name$ = "", *Record.DNS_RECORD
Protected Query$ = Str((IP>>24) & $FF)+"."+Str((IP>>16) & $FF)+"."+Str((IP>>8) & $FF)+"."+Str(IP & $FF)+".IN-ADDR.ARPA" ; ip must be reversed!
If DnsQuery_W And DnsRecordListFree
If DnsQuery_W(Query$, #DNS_TYPE_PTR, #DNS_QUERY_STANDARD, #Null, @*Record.DNS_RECORD, #Null) = 0 And *Record
If *Record\wType = #DNS_TYPE_PTR
Name$ = PeekS(*Record\PTR\pNameHost, -1, #PB_Unicode)
EndIf
DnsRecordListFree(*Record, #DnsFreeRecordList)
EndIf
EndIf
ProcedureReturn Name$
EndProcedure
; =========================================================
; Example
;
If LoadDnsApi(0)
ip = DnsQuery("www.google.com")
If ip
Debug "IP lookup: " + IPString(ip)
Name$ = ReverseDnsQuery(ip)
If Name$
Debug "Name lookup: " + Name$
Else
Debug "Reverse lookup failed."
EndIf
Else
Debug "IP lookup failed."
EndIf
CloseLibrary(0)
Else
Debug "loading failed"
EndIf
http://msdn2.microsoft.com/en-us/library/ms682016.aspx
http://support.microsoft.com/kb/831226
http://support.microsoft.com/kb/164213