📄 dnsquery.pas
字号:
{*_* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Description: Component to query DNS records.
Implement a subset of RFC 1035 (A and MX records).
Creation: January 29, 1999
Version: 1.03
EMail: http://www.overbyte.be francois.piette@overbyte.be
http://www.rtfm.be/fpiette francois.piette@rtfm.be
francois.piette@pophost.eunet.be
Support: Use the mailing list twsocket@elists.org
Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1999-2001 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@pophost.eunet.be><francois.piette@overbyte.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
4. You must register this software by sending a picture postcard
to the author. Use a nice stamp and mention your name, street
address, EMail address and any comment you like to say.
History:
Feb 14, 1999 V0.02 Indirectly call winsock functions using wsocket because
wsocket provide runtime dynamic link instead of loadtime link.
This allows a program to use DnsQuery if it discover that winsock
is installed and still run if winsock is not installed.
Feb 24, 1999 V1.00 Added code for reverse lookup (PTR record).
Mar 07, 1999 V1.01 Adapted for Delphi 1
Aug 20, 1999 V1.02 Revise compile time option. Adapted for BCB4
Jul 27, 2001 V1.03 Holger Lembke <holger@hlembke.de> implemented a few new
queries or propreties (QueryAny, LongLatToDMS, Loc2Geo, Loc)
and related data types.
-----------------------------
Jul 27, 2002 V1.80 Holger Lembke <holger@hlembke.de> has rewritten major parts
after error corrections, I suggest a jump to 2.01
changed almost everything. goal was
- to have a more comprehensive componente
- ease to implemente future DNS extentions/missing records
(future in meaning of this component. not really the dns system.)
Users from 1.03 and before should be able to adapt code quickly.
- all the response... and question... things moved into a record, so
just add a point after response
- all array-properties are gone.
instead, you have to use a loop to run throu the desired types
for i:=0 to dnsquery.ResponseCount[DnsQueryNS]-1 do
with dnsquery.ResponseItem[DnsQueryNS,i] do begin
writeln(nsdname);
end;
Look up in TRRRecord, which record parts are defined for a
specific item.
To add new records, follow this guidelines:
1.) define the DNSQuery-Constant
2.) Define the needed record
only one rule: no dynamic memory allocation
3.) add it into the case structure in TRRRecord
4.) add a 'decoder' into GetResponseItem
Aug 30, 2002 V1.81 fixed a minor error in SubLOCgeo Holger Lembke <holger@hlembke.de>
Feb 25, 2003 V?.?? Added "Type PCardinal = ^cardinal;" in GetResponseItem
Mar 30, 2003 V?.?? Holger Lembke <holger@hlembke.de>
Added TCP-Protocol-support (rfc1035, 4.2.2.), this enables zone transfers.
To archive this, FResponseBuf has been changed into a pointer. With
this some parts of the buffer handling for FResponseBuf needed to be
rewritten. And WSocketDataAvailable has to be aware of this.
translated IPNummer to IPNumber, ip6nummer to ip6number
added property CtrlSocket : TWSocket read FWSocket;
changed dyn.array to simple pointer (TDataCacheItemArray)
-----------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit DnsQuery;
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$R-} { Disable range checking }
{$IFNDEF VER80} { Not for Delphi 1 }
{$H+} { Use long strings }
{$J+} { Allow typed constant to be modified }
{$ENDIF}
{$IFDEF VER110} { C++ Builder V3.0 }
{$ObjExportAll On}
{$ENDIF}
{$IFDEF VER125} { C++ Builder V4.0 }
{$ObjExportAll On}
{$ENDIF}
interface
uses
WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Winsock, WSocket;
const
DnsQueryVersion = 102;
CopyRight : String = ' TDnsQuery (c) 1999-2000 F. Piette V1.02 ';
UDPBufSize = 512+10; // !!KAP!! 2003-03-30 max. size plus some security
{ DNS Classes }
DnsClassIN = 1; { The internet }
DnsClassCS = 2; { The CSNET class (obsolete, used only for examples)}
DnsClassCH = 3; { The CHAOS class }
DnsClassHS = 4; { Hesiod name service }
DnsClassALL = 255; { Any class }
{ Type of query/response a DNS can handle }
DnsQueryA = 1; { A HostAddress }
DnsQueryNS = 2; { NS Authoritative name server }
DnsQueryMD = 3; { MD MailDestination, obsolete, use Mail Exchange }
DnsQueryMF = 4; { MF MailForwarder, obsolete, use Mail Exchange }
DnsQueryCNAME = 5; { CNAME CanonicalName }
DnsQuerySOA = 6; { SOA Start of a Zone of Authority }
DnsQueryMB = 7; { MB MailBox, experimental }
DnsQueryMG = 8; { MG MailGroup, experimental }
DnsQueryMR = 9; { MR MailRename, experimental }
DnsQueryNULL = 10; { NULL Experimental }
DnsQueryWKS = 11; { WKS Well Known Service Description }
DnsQueryPTR = 12; { PTR Domain Name Pointer }
DnsQueryHINFO = 13; { HINFO Host Information }
DnsQueryMINFO = 14; { MINFO Mailbox information }
DnsQueryMX = 15; { MX Mail Exchange }
DnsQueryTXT = 16; { TXT Text Strings }
{ !!KAP!! }
DnsQueryRP = 17; // RFC 1183
DnsQueryAFSDB = 18; // RFC 1183
DnsQueryX25 = 19; // RFC 1183
DnsQueryISDN = 20; // RFC 1183
DnsQueryRT = 21; // RFC 1183
DnsQueryNSAP = 22; // RFC 1706
DnsQueryNSAPPTR = 23;
DnsQuerySIG = 24; { see RFC-2065 }
DnsQueryKEY = 25; { see RFC-2065 }
DnsQueryPX = 26; // rfc 1327?
DnsQueryGPOS = 27; { GPOS has the following format:
<owner> <ttl> <class> GPOS <longitude> <latitude> <altitude> }
DnsQueryAAAA = 28; { see IP6 Address }
DnsQueryLOC = 29; (* see RFC-1876 http://rfc.net/rfc1876.html
<owner> <TTL> <class> LOC ( d1 [m1 [s1]] {"N"|"S"} d2 [m2 [s2]]
{"E"|"W"} alt["m"] [siz["m"] [hp["m"]
[vp["m"]]]] )
*)
DnsQueryNXT = 30; { see RFC-2065 }
DnsQuerySRV = 33; { see RFC-2052 }
DnsQueryNAPTR = 35; { see RFC-2168 }
DnsQueryKX = 36;
{ Some additional type only allowed in queries }
DnsQueryAXFR = 252; { Transfer for an entire zone }
DnsQueryMAILB = 253; { Mailbox related records (MB, MG or MR) }
DnsQueryMAILA = 254; { MailAgent, obsolete, use MX instead }
DnsQueryALL = 255; { Request ALL records }
{ Opcode field in query flags }
DnsOpCodeQUERY = 0;
DnsOpCodeIQUERY = 1;
DnsOpCodeSTATUS = 2;
type
TDnsRequestDoneEvent = procedure (Sender : TObject; Error : WORD) of Object;
TDnsRequestHeader = packed record
ID : WORD;
flags : word;
QDCount : WORD;
ANCount : WORD;
NSCount : WORD;
ARCount : WORD;
end;
PDnsRequestHeader = ^TDnsRequestHeader;
// rfc 1035 p.10
tname = string[255];
ttxtstring = string[255];
tadress = cardinal; //32bit
// rfc 1035 p.26
TDnsRequestAnswerHeader = record //
len : integer;
ID : WORD;
qr : boolean;
opcode : byte;
AuthoritativeAnswer : boolean;
Truncation : boolean;
RecursionDesired : boolean;
RecursionAvailable : boolean;
z : byte;
rcode : byte;
(* some rrcodes
RCODE Name Description Reference
Decimal
Hexadecimal
0 NoError No Error [RFC 1035]
1 FormErr Format Error [RFC 1035]
2 ServFail Server Failure [RFC 1035]
3 NXDomain Non-Existent Domain [RFC 1035]
4 NotImp Not Implemented [RFC 1035]
5 Refused Query Refused [RFC 1035]
6 YXDomain Name Exists when it should not [RFC 2136]
7 YXRRSet RR Set Exists when it should not [RFC 2136]
8 NXRRSet RR Set that should exist does not [RFC 2136]
9 NotAuth Server Not Authoritative for zone [RFC 2136]
10 NotZone Name not contained in zone [RFC 2136]
11-15 available for assignment
16 BADVERS Bad OPT Version [RFC 2671]
16 BADSIG TSIG Signature Failure [RFC 2845]
17 BADKEY Key not recognized [RFC 2845]
18 BADTIME Signature out of time window [RFC 2845]
19 BADMODE Bad TKEY Mode [RFC 2930]
20 BADNAME Duplicate key name [RFC 2930]
21 BADALG Algorithm not supported [RFC 2930]
22-3840 available for assignment
0x0016-0x0F00
3841-4095 Private Use
0x0F01-0x0FFF
4096-65535 available for assignment
0x1000-0xFFFF
*)
QDCount : WORD;
ANCount : WORD;
NSCount : WORD;
ARCount : WORD;
end;
// rfc 1035 p.19
TSoaRecord = record
mname : tname;
rname : tname;
serial : Cardinal;
refresh : Cardinal;
retry : Cardinal;
expire : Cardinal;
minimum : Cardinal;
end;
// rfc 1876
TLOCInfo = packed record { need to be 16 bytes }
version : byte;
size : byte;
horizpre : byte;
vertpre : byte;
latitude : longint;
longitude : longint;
altitude : longint;
end;
PLOCInfo = ^TLOCInfo;
// RFC 1886 p.2
TAAAA = array[0..3] of Cardinal; // 128 bit
{ Decoded TLOCInfo }
TLogGeo = record
version : byte;
longsize : integer;
latsize : integer;
horizpre : integer;
vertpre : integer;
{ Latitude, degree, minutes, seconds, milliseconds }
lad, lam, las, lams : integer;
lahem : char;
{ same for Longitude }
lod, lom, los, loms : integer;
lohem : char;
altitude : integer;
end;
// Question Data rfc1035 p.28
TQuestion = record
QuestionType : word;
QuestionClass : word;
QuestionName : tname;
end;
// rfc 1035 p.14
THinfo = packed record
cpu : word;
os : word;
end;
// rfc 1035 p.16
TMinfo = packed record
rmailbx : tname;
remailbx : tname;
end;
// rfc 1035 p.17
TMX = record
preference : word;
exchange : tname;
end;
// rfc 1035 p.10
TRRInternal = packed record
rrtype : word; // r due to token conflict
rrclass : word; // same
rrttl : cardinal; // same
rdlength : word;
end;
pRRInternal = ^TRRInternal;
// Result-Record
TRRRecord = packed record
valid : boolean;
// internal for caching
lastid : integer;
lastindex : integer;
// RR record start rfc 1035 p.29
name : tname;
rrtype : word; // r due to token conflict
rrclass : word; // same
rrttl : cardinal; // same
rdlength : word;
case integer of // depending on rrtype, one of these structures is filled
// more or less following rfc 1035 p.15
DnsQueryMD,
DnsQueryMB,
DnsQueryMF : (madname : tname);
DnsQueryCNAME : (cname : tname);
DnsQueryHINFO : (hinfo : thinfo);
DnsQueryMG : (mgmname : tname);
DnsQueryMINFO : (minfo : tminfo);
DnsQueryMR : (newname : tname);
DnsQueryNS : (nsdname : tname);
DnsQueryPTR : (ptrname : tname);
DnsQuerySOA : (soa : TSoaRecord);
DnsQueryTXT : (txt : ttxtstring);
DnsQueryMX : (mx : TMX);
DnsQueryA : (adress : tadress;
ipnumber : tname); // interpreted
DnsQueryAAAA : (aaaa : TAAAA;
ip6number : tname); // interpreted
DnsQueryLOC : (loc : TLOCInfo;
locdecode : TLogGeo); // interpreted
end;
// for a quicker access
TDataCacheItem = record
rrtype : word;
rrpos : pchar;
end;
// !!KAP!! 2003-03-30
TDataCacheItemArray = array[0..$FFFF div sizeof(TDataCacheItem)] of TDataCacheItem;
PDataCacheItemArray = ^TDataCacheItemArray;
tdatacache = record
count : integer;
items : PDataCacheItemArray;
end;
// !!KAP!! 2003-03-30
TResBuf = array[0..$FFFF] of char;
PResBuf = ^TResBuf;
TDnsQuery = class(TComponent)
private
{ D閏larations priv閑s }
frrcache : TRRRecord;
fdatacache : tdatacache;
fDnsRequestAnswer : TDnsRequestAnswerHeader;
fquestion : TQuestion;
function NewExtractName(var p : pchar):string;
function GetRepsonsecount(nid : integer):integer;
function GetResponseItem(nid : integer; nindex : integer):TRRRecord;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -