Unit DNS

From Ultibo.org
Revision as of 05:27, 9 March 2017 by Ultibo (Talk | contribs)

Jump to: navigation, search

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 constants 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 constants DNS_OPCODE_*
DNS_OPCODE_MASK = $7800; Opcode
DNS_RESPONSE_MASK = $000F; Response code


DNS query code constants 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 constants 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 constants 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 constants 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 constants 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;  

DNA 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

DNA 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 client

TDNSClient = class(TNetworkClient)

constructor Create(AProtocol:TNetworkProtocol);  
destructor Destroy; override;  
private
FTlsIndex:LongWord;  
 
function GetClientData:PDNSClientData;  
function CreateClientData:Boolean;  
 
function AddressEntryToHostEnt(AddressEntry:TIPAddressEntry; const AName:String; ACount:Integer):PHostEnt;  
function HostEntryToHostEnt(HostEntry:TIPHostEntry):PHostEnt;  
function NetworkEntryToNetEnt(NetworkEntry:TIPNetworkEntry):PNetEnt;  
function ServEntryToServEnt(ServEntry:TIPServEntry):PServEnt;  
function ProtoEntryToProtoEnt(ProtoEntry:TIPProtoEntry):PProtoEnt;  
 
function InAddrToName(const AAddress:TInAddr):String;  
function NameToInAddr(const AName:String):TInAddr;  
 
function In6AddrToName(const AAddress:TIn6Addr):String;  
function NameToIn6Addr(const AName:String):TIn6Addr;  
 
function GetDNSMessageSize(AMessage:PDNSMessage):Integer;  
 
function GetDNSNameSize(AMessage:PDNSMessage; AOffset:Word):Word;  
function GetDNSQuestionSize(AMessage:PDNSMessage; AOffset:Word):Word;  
function GetDNSResourceSize(AMessage:PDNSMessage; AOffset:Word):Word;  
 
function GetDNSQuestionOffset(AMessage:PDNSMessage; ACount:Word):Word;  
function GetDNSAnswerOffset(AMessage:PDNSMessage; ACount:Word):Word;  
function GetDNSAuthorityOffset(AMessage:PDNSMessage; ACount:Word):Word;  
function GetDNSAdditionalOffset(AMessage:PDNSMessage; ACount:Word):Word;  
 
function CreateDNSQuery(AMessage:PDNSMessage; AIdentifier:Word):Boolean;  
function CheckDNSResponse(AMessage:PDNSMessage; AIdentifier:Word):Boolean;  
function HandleDNSResponse(AMessage:PDNSMessage; AIdentifier:Word):Boolean;  
 
function SendDNSQuery(ASocket:TProtocolSocket; const AServer:TInAddr; AData:Pointer; ALength,AType,AClass,AIdentifier:Word):Boolean;  
function RecvDNSResponse(ASocket:TProtocolSocket; AType,AClass,AIdentifier:Word):Boolean;  
 
function InsertDNSName(AMessage:PDNSMessage; AOffset:Word; AName:Pointer; ALength:Word):Boolean;  
function ExtractDNSName(AMessage:PDNSMessage; AOffset:Word; AName:Pointer; var ALength:Word):Boolean;  
function ExtractDNSRData(AMessage:PDNSMessage; AOffset:Word; AData:Pointer; var ALength:Word; AType,AClass:Word):Boolean;  
 
function InsertDNSQuestion(AMessage:PDNSMessage; ACount:Word; AData:Pointer; ALength,AType,AClass:Word):Boolean;  
function ExtractDNSAnswer(AMessage:PDNSMessage; ACount:Word; AName,AData:Pointer; var ANameLength,ADataLength,AType,AClass:Word; var ATtl:LongWord):Boolean;  
function ExtractDNSAuthority(AMessage:PDNSMessage; ACount:Word; AName,AData:Pointer; var ANameLength,ADataLength,AType,AClass:Word; var ATtl:LongWord):Boolean;  
function ExtractDNSAdditional(AMessage:PDNSMessage; ACount:Word; AName,AData:Pointer; var ANameLength,ADataLength,AType,AClass:Word; var ATtl:LongWord):Boolean;  
public
function GetHostByAddr(AAddr:Pointer; ALength,AFamily:Integer):PHostEnt;  
function GetHostByName(AName:PChar):PHostEnt;  
 
function GetHostName(AName:PChar; ALength:Integer):Integer;  
 
function GetServByPort(APort:Integer; AProto:PChar):PServEnt;  
function GetServByName(AName,AProto:PChar):PServEnt;  
function GetProtoByNumber(AProto:Integer):PProtoEnt;  
function GetProtoByName(AName:PChar):PProtoEnt;  
 
function GetNetByAddr(AAddr:Pointer; ALength,AStruct:Integer):PNetEnt;  
function GetNetByName(AName:PChar):PNetEnt;  
 
function StartClient:Boolean; override;  
function StopClient:Boolean; override;  


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