C# Class NetworkPackets.Dns.DnsPacket

Supports the parsing of Dns Packets.

This is a very naive implementation and lacks support for services other than address lookup (Type=A) and pointer look up (Type=Ptr). Because I haven't found a service that used inverse querying for name look up, only pointer look up is implemented.

Exceptions will not occur when parsing byte arrays, only when attempting to create from scratch new packets with unsupported Types.

A Dns packet ... 1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ID | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |QR| Opcode |AA|TC|RD|RA| Z | RCODE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QDCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ANCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | NSCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ARCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | QueryS / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | RESPONSES / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ Field Description ID identification - client generated, do not change QR query / reply, client sends 0, server replies 1 Opcode 0 for query, 1 inverse query AA Authoritative answer - True when there is a mapping TC Truncation - ignored - 0 RD Recursion desired RA Recursion availabled ZReserved - must be 0 RCODE ignored, stands for error code - 0 QDCOUNT questions - should be 1 ANCOUNT answers - should be 0 until we answer! NSCOUNT name server records - somewhat supported, but I can't find a reason why it needs to be so I've left in ZoneAuthority code in case it is ever needed! ARCOUNT additional records - unsupported
Inheritance: Brunet.DataPacket
Exibir arquivo Open project: pstjuste/brunet Class Usage Examples

Public Properties

Property Type Description
AA bool
Additional Response[]
Answers Response[]
Authority Response[]
ID short
Opcode byte
Query bool
Questions NetworkPackets.Dns.Question[]
RA bool
RD bool

Public Methods

Method Description
BuildFailedReplyPacket ( DnsPacket Packet, bool refused_implemented ) : MemBlock

Given a DnsPacket, it will generate a failure message so that the local resolver can move on to the next nameserver without timeouting on the this one.

DnsPacket ( MemBlock Packet ) : Brunet

Parses a MemBlock as a DnsPacket.

DnsPacket ( short ID, bool Query, byte Opcode, bool AA, bool RA, bool RD, Question Questions, Response Answers, Response Authority, Response Additional ) : Brunet

Creates a Dns packet from the parameters provided.

HostnameMemBlockToString ( MemBlock name ) : String

Takes in a memblock containing a dns formatted hostname string and converts it into a String.

HostnameStringToMemBlock ( String name ) : MemBlock

Given a Name as a string converts it into bytes given the type of query.

IPMemBlockToString ( MemBlock ip ) : String

Takes in an IP Address in dns format and returns a string. The format is abcd (byte[] {a, b, c, d}.

IPStringToMemBlock ( String ip ) : MemBlock

Takes in an ip string such as a.b.c.d and returns a MemBlock containing the IP [a, b, c, d].

PtrMemBlockToString ( MemBlock ptr ) : String

Takes in a memblock containing dns ptr data ... d.c.b.a.in-addr.arpa ... and returns the IP Address as a string.

PtrStringToMemBlock ( String ptr ) : MemBlock

Takes in an IP Address string and returns the dns ptr formatted memblock containing d.c.b.a.in-addr.arpa.

RetrieveBlob ( MemBlock Data, int Start, int &End ) : MemBlock

A blob is a fully resolved name. Dns uses pointers to reduce memory consumption in packets, this can traverse all pointers and return a complete name. The blob starts a Start and Ends at End. This is used so that the parsing program knows where to continue reading data from.

StringIsIP ( String IP ) : bool

Method Details

BuildFailedReplyPacket() public static method

Given a DnsPacket, it will generate a failure message so that the local resolver can move on to the next nameserver without timeouting on the this one.
public static BuildFailedReplyPacket ( DnsPacket Packet, bool refused_implemented ) : MemBlock
Packet DnsPacket The base packet to translate into a failed response
refused_implemented bool
return MemBlock

DnsPacket() public method

Parses a MemBlock as a DnsPacket.
public DnsPacket ( MemBlock Packet ) : Brunet
Packet MemBlock The payload containing hte Dns Packet in byte format.
return Brunet

DnsPacket() public method

Creates a Dns packet from the parameters provided.
public DnsPacket ( short ID, bool Query, byte Opcode, bool AA, bool RA, bool RD, Question Questions, Response Answers, Response Authority, Response Additional ) : Brunet
ID short A unique ID for the packet, responses should be the same as the query
Query bool True if a query, false if a response
Opcode byte 0 = Query, which is the only supported parsing method
AA bool Authoritative Answer, true if there is a resolution for the lookup.
RA bool
RD bool
Questions Question A list of Questions.
Answers Response A list of Answers.
Authority Response
Additional Response
return Brunet

HostnameMemBlockToString() public static method

Takes in a memblock containing a dns formatted hostname string and converts it into a String.
public static HostnameMemBlockToString ( MemBlock name ) : String
name MemBlock The memblock containing the dns formated hostname.
return String

HostnameStringToMemBlock() public static method

Given a Name as a string converts it into bytes given the type of query.
public static HostnameStringToMemBlock ( String name ) : MemBlock
name String The name to convert (and resolve).
return MemBlock

IPMemBlockToString() public static method

Takes in an IP Address in dns format and returns a string. The format is abcd (byte[] {a, b, c, d}.
public static IPMemBlockToString ( MemBlock ip ) : String
ip MemBlock a memblock containing abcd.
return String

IPStringToMemBlock() public static method

Takes in an ip string such as a.b.c.d and returns a MemBlock containing the IP [a, b, c, d].
public static IPStringToMemBlock ( String ip ) : MemBlock
ip String The IP in a string to convert.
return MemBlock

PtrMemBlockToString() public static method

Takes in a memblock containing dns ptr data ... d.c.b.a.in-addr.arpa ... and returns the IP Address as a string.
public static PtrMemBlockToString ( MemBlock ptr ) : String
ptr MemBlock The block containing the dns ptr data.
return String

PtrStringToMemBlock() public static method

Takes in an IP Address string and returns the dns ptr formatted memblock containing d.c.b.a.in-addr.arpa.
public static PtrStringToMemBlock ( String ptr ) : MemBlock
ptr String An IP Address in the format a.b.c.d.
return MemBlock

RetrieveBlob() public static method

A blob is a fully resolved name. Dns uses pointers to reduce memory consumption in packets, this can traverse all pointers and return a complete name. The blob starts a Start and Ends at End. This is used so that the parsing program knows where to continue reading data from.
public static RetrieveBlob ( MemBlock Data, int Start, int &End ) : MemBlock
Data MemBlock The entire packet to grab the blob from.
Start int The beginning of the blob.
End int Returned to the user and notes where the blob ends.
return MemBlock

StringIsIP() public static method

public static StringIsIP ( String IP ) : bool
IP String
return bool

Property Details

AA public_oe property

Authoritative answer (if you have a resolution, set)
public bool AA
return bool

Additional public_oe property

public Response[] Additional
return Response[]

Answers public_oe property

list of Answers
public Response[] Answers
return Response[]

Authority public_oe property

public Response[] Authority
return Response[]

ID public_oe property

Unique packet ID
public short ID
return short

Opcode public_oe property

0 = Query, 1 = Inverse Query, 2 = Status
public byte Opcode
return byte

Query public_oe property

Query if true, otherwise a response
public bool Query
return bool

Questions public_oe property

list of Questions
public Question[],NetworkPackets.Dns Questions
return NetworkPackets.Dns.Question[]

RA public_oe property

public bool RA
return bool

RD public_oe property

public bool RD
return bool