unit DNS;

interface

uses
	System.SysUtils;

const
	DNSTypeA = $0001;
	DNSTypeAAAA = $001C;
	DNSTypePtr = $000C;
	DNSTypeText = $0010;
	DNSTypeSrv = $0021;

const
	DNS_REQUEST_PENDING = 9506;

type
	TIP4Address = array[0..3] of Byte;
	TIP6Address = array[0..7] of Word;

	TDNSAData = packed record
		IPv4Addr: TIP4Address;
	end;

	TDNSAAAAData = packed record
		IPv6Addr: TIP6Address;
	end;

	TDNSPtrData = packed record
		pNameHost: PWideChar;
	end;

	TDNSTxtData = packed record
		dwStringCount: LongWord;
		pStringArray: array[0..0] of PWideChar;
	end;

	TDNSSrvData = packed record
		pNameTarget: PWideChar;
		wPriority,
		wWeight,
		wPort,
		Pad: Word;
	end;

	TPDNSRecord = ^TDNSRecord;
	TDNSRecord = packed record
		pNext: TPDNSRecord;
		pName: PWideChar;
		wType: Word;
		wDataLength: Word;
		Flags: LongWord;
		dwTtl: LongWord;
		dwReserved: LongWord;
	case Word of
		DNSTypeA: (AData: TDNSAData);
		DNSTypeAAAA: (AAAAData: TDNSAAAAData);
		DNSTypePtr: (PtrData: TDNSPtrData);
		DNSTypeText: (TextData: TDNSTxtData);
		DNSTypeSrv: (SrvData: TDNSSrvData);
		$FFFF: (pDataPtr: PByte);
	end;



	TDNSQueryResult = packed record
		Version: LongWord;
		QueryStatus: LongWord;
		QueryOptions: UInt64;
		pQueryRecords: TPDNSRecord;
		Reserved: Pointer;
	end;
	TPDNSQueryResult = ^TDNSQueryResult;

function DNSRecordListFree(pRecordList: TPDNSRecord; FreeType: LongWord): LongWord; stdcall; external 'DNSAPI.dll' name 'DnsRecordListFree';

function StrFromAddr(const Addr: TIP4Address): AnsiString; overload;
function StrFromAddr(const Addr: TIP6Address): AnsiString; overload;



implementation

function StrFromAddr(const Addr: TIP4Address): AnsiString; overload;
var
	I: Integer;

	function ByteStr: AnsiString;
	begin
		Result := AnsiString(IntToStr(Addr[I]));
	end;

begin
	I := 0;
	Result := ByteStr;
	for I := 1 to High(Addr) do
		Result := Result + '.' + ByteStr
	;
end;

function StrFromAddr(const Addr: TIP6Address): AnsiString; overload;
var
	I: Integer;

	function WordStr: AnsiString;
	begin
		Result := AnsiString(IntToHex(Addr[I], 4));
	end;

begin
	I := 0;
	Result := WordStr;
	for I := 1 to High(Addr) do
		Result := Result + ':' + WordStr
	;
end;

end.
