Difference between revisions of "Unit DNS"

From Ultibo.org
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
----
 
----
  
'''Ultibo DNS client unit'''
+
'''Ultibo DNS Client unit'''
  
 
Note: DNS Client is IPv4 based, for IPv6 see new Winsock2 functions
 
Note: DNS Client is IPv4 based, for IPv6 see new Winsock2 functions
Line 54: Line 54:
 
<br />  
 
<br />  
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
<div style="font-size: 14px; padding-left: 12px;">'''DNS flag constants''' <code> DNS_FLAG_* </code></div>
+
<div style="font-size: 14px; padding-left: 12px;">'''DNS flag''' <code> DNS_FLAG_* </code></div>
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
Line 77: Line 77:
 
<br />  
 
<br />  
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
<div style="font-size: 14px; padding-left: 12px;">'''DNS field masks constants''' <code> DNS_OPCODE_* </code></div>
+
<div style="font-size: 14px; padding-left: 12px;">'''DNS field masks''' <code> DNS_OPCODE_* </code></div>
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
Line 91: Line 91:
 
<br />  
 
<br />  
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
<div style="font-size: 14px; padding-left: 12px;">'''DNS query code constants''' <code> DNS_QUERY* </code></div>
+
<div style="font-size: 14px; padding-left: 12px;">'''DNS query code''' <code> DNS_QUERY* </code></div>
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
Line 111: Line 111:
 
<br />  
 
<br />  
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
<div style="font-size: 14px; padding-left: 12px;">'''DNS response code constants''' <code> DNS_NO_* </code></div>
+
<div style="font-size: 14px; padding-left: 12px;">'''DNS response code''' <code> DNS_NO_* </code></div>
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
Line 137: Line 137:
 
<br />  
 
<br />  
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
<div style="font-size: 14px; padding-left: 12px;">'''DNS record type constants''' <code> DNS_TYPE_* </code></div>
+
<div style="font-size: 14px; padding-left: 12px;">'''DNS record type''' <code> DNS_TYPE_* </code></div>
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
Line 190: Line 190:
 
<br />
 
<br />
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
<div style="font-size: 14px; padding-left: 12px;">'''DNS address class constants''' <code> DNS_CLASS_* </code></div>
+
<div style="font-size: 14px; padding-left: 12px;">'''DNS address class''' <code> DNS_CLASS_* </code></div>
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
Line 207: Line 207:
 
<br />  
 
<br />  
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
<div style="font-size: 14px; padding-left: 12px;">'''DNS message compression constants''' <code> DNS_POINTER_* </code></div>
+
<div style="font-size: 14px; padding-left: 12px;">'''DNS message compression''' <code> DNS_POINTER_* </code></div>
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
Line 272: Line 272:
 
</div></div>   
 
</div></div>   
 
   
 
   
'''DNA name'''
+
'''DNS name'''
  
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial;">
Line 342: Line 342:
 
</div></div>   
 
</div></div>   
  
'''DNA client data'''
+
'''DNS client data'''
  
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial;">
 
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial;">
Line 426: Line 426:
  
  
'''DNS client'''
+
'''DNS specific classes'''
  
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial;">
+
{| class="wikitable" style="font-size: 14px; text-align: left; width: 100%; height: 50px;"
 
+
<code> TDNSClient = class(TNetworkClient)</code>
+
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
+
{| class="wikitable" style="font-size: 14px; background: white;"
+
 
|-
 
|-
| <code>constructor Create(AProtocol:TNetworkProtocol);</code>
+
| [[TDNSClient|<code>TDNSClient = class(TNetworkClient)</code>]]
| style="width: 50%;"|&nbsp;
+
|-
+
| <code>destructor Destroy; override;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|<div style="font-family: monospace,courier;">'''private'''</div>
+
|-
+
| <code>FTlsIndex:LongWord;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function GetClientData:PDNSClientData;</code>
+
| &nbsp;
+
|-
+
| <code>function CreateClientData:Boolean;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function AddressEntryToHostEnt(AddressEntry:TIPAddressEntry; const AName:String; ACount:Integer):PHostEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function HostEntryToHostEnt(HostEntry:TIPHostEntry):PHostEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function NetworkEntryToNetEnt(NetworkEntry:TIPNetworkEntry):PNetEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function ServEntryToServEnt(ServEntry:TIPServEntry):PServEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function ProtoEntryToProtoEnt(ProtoEntry:TIPProtoEntry):PProtoEnt;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function InAddrToName(const AAddress:TInAddr):String;</code>
+
| &nbsp;
+
|-
+
| <code>function NameToInAddr(const AName:String):TInAddr;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function In6AddrToName(const AAddress:TIn6Addr):String;</code>
+
| &nbsp;
+
|-
+
| <code>function NameToIn6Addr(const AName:String):TIn6Addr;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function GetDNSMessageSize(AMessage:PDNSMessage):Integer;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function GetDNSNameSize(AMessage:PDNSMessage; AOffset:Word):Word;</code>
+
| &nbsp;
+
|-
+
| <code>function GetDNSQuestionSize(AMessage:PDNSMessage; AOffset:Word):Word;</code>
+
| &nbsp;
+
|-
+
| <code>function GetDNSResourceSize(AMessage:PDNSMessage; AOffset:Word):Word;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function GetDNSQuestionOffset(AMessage:PDNSMessage; ACount:Word):Word;</code>
+
| &nbsp;
+
|-
+
| <code>function GetDNSAnswerOffset(AMessage:PDNSMessage; ACount:Word):Word;</code>
+
| &nbsp;
+
|-
+
| <code>function GetDNSAuthorityOffset(AMessage:PDNSMessage; ACount:Word):Word;</code>
+
| &nbsp;
+
|-
+
| <code>function GetDNSAdditionalOffset(AMessage:PDNSMessage; ACount:Word):Word;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function CreateDNSQuery(AMessage:PDNSMessage; AIdentifier:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
| <code>function CheckDNSResponse(AMessage:PDNSMessage; AIdentifier:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
| <code>function HandleDNSResponse(AMessage:PDNSMessage; AIdentifier:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function SendDNSQuery(ASocket:TProtocolSocket; const AServer:TInAddr; AData:Pointer; ALength,AType,AClass,AIdentifier:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
| <code>function RecvDNSResponse(ASocket:TProtocolSocket; AType,AClass,AIdentifier:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function InsertDNSName(AMessage:PDNSMessage; AOffset:Word; AName:Pointer; ALength:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
| <code>function ExtractDNSName(AMessage:PDNSMessage; AOffset:Word; AName:Pointer; var ALength:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
| <code>function ExtractDNSRData(AMessage:PDNSMessage; AOffset:Word; AData:Pointer; var ALength:Word; AType,AClass:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function InsertDNSQuestion(AMessage:PDNSMessage; ACount:Word; AData:Pointer; ALength,AType,AClass:Word):Boolean;</code>
+
| &nbsp;
+
|-
+
| <code>function ExtractDNSAnswer(AMessage:PDNSMessage; ACount:Word; AName,AData:Pointer; var ANameLength,ADataLength,AType,AClass:Word; var ATtl:LongWord):Boolean;</code>
+
| &nbsp;
+
|-
+
| <code>function ExtractDNSAuthority(AMessage:PDNSMessage; ACount:Word; AName,AData:Pointer; var ANameLength,ADataLength,AType,AClass:Word; var ATtl:LongWord):Boolean;</code>
+
| &nbsp;
+
|-
+
| <code>function ExtractDNSAdditional(AMessage:PDNSMessage; ACount:Word; AName,AData:Pointer; var ANameLength,ADataLength,AType,AClass:Word; var ATtl:LongWord):Boolean;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|<div style="font-family: monospace,courier;">'''public'''</div>
+
|-
+
| <code>function GetHostByAddr(AAddr:Pointer; ALength,AFamily:Integer):PHostEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function GetHostByName(AName:PChar):PHostEnt;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function GetHostName(AName:PChar; ALength:Integer):Integer;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function GetServByPort(APort:Integer; AProto:PChar):PServEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function GetServByName(AName,AProto:PChar):PServEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function GetProtoByNumber(AProto:Integer):PProtoEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function GetProtoByName(AName:PChar):PProtoEnt;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function GetNetByAddr(AAddr:Pointer; ALength,AStruct:Integer):PNetEnt;</code>
+
| &nbsp;
+
|-
+
| <code>function GetNetByName(AName:PChar):PNetEnt;</code>
+
| &nbsp;
+
|-
+
|colspan="2"|&nbsp;
+
|-
+
| <code>function StartClient:Boolean; override;</code>
+
| &nbsp;
+
|-
+
| <code>function StopClient:Boolean; override;</code>
+
| &nbsp;
+
 
|-
 
|-
 
|}
 
|}
</div></div>
 
 
<br />
 
<br />
  
Line 624: Line 452:
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
|-
 
|-
! '''Note'''
+
! Note
 
| None documented
 
| None documented
 
|-
 
|-
Line 636: Line 464:
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
|-
 
|-
! '''Note'''
+
! Note
 
| None documented
 
| None documented
 
|-
 
|-
Line 648: Line 476:
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
{| class="wikitable" style="font-size: 14px; background: white;"
 
|-
 
|-
! '''Note'''
+
! Note
 
| None documented
 
| None documented
 
|-
 
|-

Latest revision as of 05:21, 31 August 2021

Return to Unit Reference


Description


Ultibo DNS Client unit

Note: DNS Client is IPv4 based, for IPv6 see new Winsock2 functions

See RFC 1035 Section 4 for details

Constants



DNS specific constants DNS_*
DNS_TIMEOUT = 5000; We wait for 5 seconds for a DNS reply
DNS_RETRIES = 4; Try the request 4 times
 
DNS_HEADER_SIZE = 12; SizeOf(TDNSHeader);
 
DNS_QUESTION_SIZE = 4; SizeOf(TDNSQuestion);
DNS_RESOURCE_SIZE = 10; SizeOf(TDNSResource); Not including Record Data
 
MAX_DNS_NAME = 255;  
MAX_DNS_LABEL = 63;  
MAX_DNS_MESSAGE = 512; Maximum Size of DNS Message


DNS flag DNS_FLAG_*
DNS_FLAG_RESPONSE = $8000; query = 0, response = 1
DNS_FLAG_AUTHORITY = $0400; Authoritative answer
DNS_FLAG_TRUNCATED = $0200; Truncation, response was cut off at 512
DNS_FLAG_DO_RECURSE = $0100; Recursion desired
DNS_FLAG_CAN_RECURSE = $0080; Recursion available


DNS field masks DNS_OPCODE_*
DNS_OPCODE_MASK = $7800; Opcode
DNS_RESPONSE_MASK = $000F; Response code


DNS query code DNS_QUERY*
DNS_QUERY = 0; A standard query
DNS_INV_QUERY = 1; An inverse query
DNS_COMP_QUERY_MULTI = 2; A completion query, multiple reply (Obsolete)
DNS_COMP_QUERY_SINGLE = 3; A completion query, single reply (Obsolete)


DNS response code DNS_NO_*
DNS_NO_ERROR = 0;  
DNS_FORMAT_ERROR = 1;  
DNS_SERVER_FAILURE = 2;  
DNS_NAME_ERROR = 3;  
DNS_NOT_IMPLEMENTED = 4;  
DNS_REFUSED = 5;  


DNS record type DNS_TYPE_*
DNS_TYPE_A = 1; Host address resource record (RR)
DNS_TYPE_NS = 2;  
DNS_TYPE_MD = 3;  
DNS_TYPE_MF = 4;  
DNS_TYPE_CNAME = 5;  
DNS_TYPE_SOA = 6;  
DNS_TYPE_MB = 7;  
DNS_TYPE_MG = 8;  
DNS_TYPE_MR = 9;  
DNS_TYPE_RT_NULL = 10;  
DNS_TYPE_WKS = 11;  
DNS_TYPE_PTR = 12; A domain name ptr
DNS_TYPE_HINFO = 13;  
DNS_TYPE_MINFO = 14;  
DNS_TYPE_MX = 15; Mail exchange


DNS address class DNS_CLASS_*
DNS_CLASS_IN = 1; ARPA internet class
DNS_CLASS_CS = 2;  
DNS_CLASS_WILD = 255; Wildcard for several of the classifications


DNS message compression DNS_POINTER_*
DNS_POINTER_MASK = $C0; Mask to indicate pointer to previously used name


Type definitions



DNS specific types

PDNSHeader = ^TDNSHeader;

TDNSHeader = packed record

Note: All Network Order
Identifier:Word; unique identifier
Flags:Word; QD/Opcode/AA/TC/RD/RA/RCODE
QuestionCount:Word; question section, number of entries
AnswerCount:Word; answers, how many
AuthorityCount:Word; count of name server RRs
AdditionalCount:Word; number of "additional" records

DNS message

PDNSMessage = ^TDNSMessage;

TDNSMessage = packed record

DNS:TDNSHeader;  
Data:array[0..(MAX_DNS_MESSAGE - DNS_HEADER_SIZE) - 1] of Byte;  

DNS name

PDNSName = ^TDNSName;

TDNSName = array[0..MAX_DNS_NAME - 1] of Char;

   

DNS question

PDNSQuestion = ^TDNSQuestion;

TDNSQuestion = packed record

Note: All Network Order
Name:TDNSName; variable length question name
QuestionType:Word; question type (eg DNS_TYPE_A)
QuestionClass:Word; question class (eg DNS_CLASS_IN)

DNS resource

PDNSResource = ^TDNSResource;

TDNSResource = packed record

Note: All Network Order
Name:TDNSName; variable length resource name
RecordType:Word; resource record type (eg DNS_TYPE_A)
RecordClass:Word; resource record class (eg DNS_CLASS_IN)
Ttl:LongWord; time-to-live, changed to 32 bits
DataLength:Word; length of data field
RecordData:array[0..MAX_DNS_MESSAGE - 1] of Byte; data field

DNS client data

PDNSClientData = ^TDNSClientData;

TDNSClientData = record

Note: Used for TLS Data
Host Ent
HostEnt:THostEnt;  
HostEntName:array[0..MAX_NAME_SIZE - 1] of Char;  
HostAliasesPtr:PChar;  
HostAliases:array[0..(MAX_NAME_SIZE * MAX_NAME_ALIASES) - 1] of Char;  
HostAddrListPtr:PChar;  
HostAddrList:array[0..MAX_HOST_ALIASES - 1] of TInAddr;  
Net Ent
NetEnt:TNetEnt;  
NetEntName:array[0..MAX_NAME_SIZE - 1] of Char;  
NetAliasesPtr:PChar;  
NetAliases:array[0..(MAX_NAME_SIZE * MAX_NAME_ALIASES) - 1] of Char;  
Serv Ent
ServEnt:TServEnt;  
ServEntName:array[0..MAX_NAME_SIZE - 1] of Char;  
ServAliasesPtr:PChar;  
ServAliases:array[0..(MAX_NAME_SIZE * MAX_NAME_ALIASES) - 1] of Char;  
ServEntProto:array[0..MAX_NAME_SIZE - 1] of Char;  
Proto Ent
ProtoEnt:TProtoEnt;  
ProtoEntName:array[0..MAX_NAME_SIZE - 1] of Char;  
ProtoAliasesPtr:PChar;  
ProtoAliases:array[0..(MAX_NAME_SIZE * MAX_NAME_ALIASES) - 1] of Char;  


Class definitions



DNS specific classes

TDNSClient = class(TNetworkClient)


Public variables


None defined

Function declarations



Initialization functions

procedure DNSInit;
Description: To be documented
Note None documented


function DNSStart:LongWord;
Description: To be documented
Note None documented


function DNSStop:LongWord;
Description: To be documented
Note None documented


Return to Unit Reference