public void DnsATest() { String NAME = "www.cnn.com"; DnsPacket.Types TYPE = DnsPacket.Types.A; DnsPacket.Classes CLASS = DnsPacket.Classes.IN; Question qp = new Question(NAME, TYPE, CLASS); MemBlock namem = MemBlock.Reference(new byte[] {0x03, 0x77, 0x77, 0x77, 0x03, 0x63, 0x6e, 0x6e, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01}); Question qm = new Question(namem, 0); Assert.AreEqual(qp.Packet, namem, "Packet"); Assert.AreEqual(qm.QName, NAME, "NAME"); Assert.AreEqual(qm.QType, TYPE, "TYPE"); Assert.AreEqual(qm.QClass, CLASS, "CLASS"); }
public void DnsPtrTest() { String NAME = "64.233.169.104"; DnsPacket.Types TYPE = DnsPacket.Types.Ptr; DnsPacket.Classes CLASS = DnsPacket.Classes.IN; Question qp = new Question(NAME, TYPE, CLASS); MemBlock ptrm = MemBlock.Reference(new byte[] {0x03, 0x31, 0x30, 0x34, 0x03, 0x31, 0x36, 0x39, 0x03, 0x32, 0x33, 0x33, 0x02, 0x36, 0x34, 0x07, 0x69, 0x6e, 0x2d, 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, 0x61, 0x00, 0x00, 0x0c, 0x00, 0x01}); Question qm = new Question(ptrm, 0); Assert.AreEqual(qp.Packet, ptrm, "Packet"); Assert.AreEqual(qm.QName, NAME, "NAME"); Assert.AreEqual(qm.QType, TYPE, "TYPE"); Assert.AreEqual(qm.QClass, CLASS, "CLASS"); }
/** <summary>Parses a MemBlock as a DnsPacket.</summary> <param name="Packet">The payload containing hte Dns Packet in byte format. </param> */ public DnsPacket(MemBlock Packet) { ID = (short) ((Packet[0] << 8) + Packet[1]); Query = (bool) (((Packet[2] & 0x80) >> 7) == 0); Opcode = (byte) ((Packet[2] & 0x78) >> 3); if((Packet[2] & 0x4) == 0x4) { AA = true; } else { AA = false; } if((Packet[2] & 0x1) == 0x1) { RD = true; } else { RD = false; } if((Packet[3] & 0x80) == 0x80) { RA = true; } else { RA = false; } int qdcount = (Packet[4] << 8) + Packet[5]; int ancount = (Packet[6] << 8) + Packet[7]; int nscount = (Packet[8] << 8) + Packet[9]; int arcount = (Packet[10] << 8) + Packet[11]; int idx = 12; Questions = new Question[qdcount]; for(int i = 0; i < qdcount; i++) { Questions[i] = new Question(Packet, idx); idx += Questions[i].Packet.Length; } Answers = new Response[ancount]; for(int i = 0; i < ancount; i++) { Answers[i] = new Response(Packet, idx); idx += Answers[i].Packet.Length; } Authority = new Response[nscount]; for(int i = 0; i < nscount; i++) { Authority[i] = new Response(Packet, idx); idx += Authority[i].Packet.Length; } Additional = new Response[arcount]; for(int i = 0; i < arcount; i++) { Additional[i] = new Response(Packet, idx); idx += Additional[i].Packet.Length; } _icpacket = _packet = Packet; }
public void TestPtrRPacketWithoutCompression() { int id = 55885; short ID = (short) id; bool Query = false; byte Opcode = 0; bool AA = false; String QName = "64.233.169.104"; DnsPacket.Types QType = DnsPacket.Types.Ptr; DnsPacket.Classes QClass = DnsPacket.Classes.IN; Question qp = new Question(QName, QType, QClass); String Name = "64.233.169.104"; DnsPacket.Types Type = DnsPacket.Types.Ptr; DnsPacket.Classes Class = DnsPacket.Classes.IN; int Ttl = 30; String RData = "yo-in-f104.google.com"; Response rp = new Response(Name, Type, Class, Ttl, RData); DnsPacket dp = new DnsPacket(ID, Query, Opcode, AA, false, false, new Question[] {qp}, new Response[] {rp}, null, null); MemBlock ptrm = MemBlock.Reference(new byte[] {0xda, 0x4d, 0x80, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x31, 0x30, 0x34, 0x03, 0x31, 0x36, 0x39, 0x03, 0x32, 0x33, 0x33, 0x02, 0x36, 0x34, 0x07, 0x69, 0x6e, 0x2d, 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, 0x61, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x03, 0x31, 0x30, 0x34, 0x03, 0x31, 0x36, 0x39, 0x03, 0x32, 0x33, 0x33, 0x02, 0x36, 0x34, 0x07, 0x69, 0x6e, 0x2d, 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, 0x61, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x17, 0x0a, 0x79, 0x6f, 0x2d, 0x69, 0x6e, 0x2d, 0x66, 0x31, 0x30, 0x34, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00}); DnsPacket dm = new DnsPacket(ptrm); Assert.AreEqual(dm.ID, ID, "ID"); Assert.AreEqual(dm.Query, Query, "Query"); Assert.AreEqual(dm.Opcode, Opcode, "Opcode"); Assert.AreEqual(dm.AA, AA, "AA"); Assert.AreEqual(dm.Questions.Length, 1, "Questions"); Assert.AreEqual(dm.Answers.Length, 1, "Answers"); Assert.AreEqual(dm.Packet, ptrm, "MemBlock"); Response rm = dm.Answers[0]; Assert.AreEqual(rm.Name, Name, "Name"); Assert.AreEqual(rm.Type, Type, "Type"); Assert.AreEqual(rm.Class, Class, "Class"); Assert.AreEqual(rm.Ttl, Ttl, "Ttl"); Assert.AreEqual(rm.RData, RData, "RData"); Question qm = dm.Questions[0]; Assert.AreEqual(qm.QName, Name, "QName"); Assert.AreEqual(qm.QType, Type, "QType"); Assert.AreEqual(qm.QClass, Class, "QClass"); Assert.AreEqual(dp.Packet, ptrm, "Dns Packet"); }
/** <summary>Creates a Dns packet from the parameters provided.</summary> <param name="ID">A unique ID for the packet, responses should be the same as the query</param> <param name="Query">True if a query, false if a response</param> <param name="Opcode">0 = Query, which is the only supported parsing method </param> <param name="AA">Authoritative Answer, true if there is a resolution for the lookup.</param> <param name="Questions">A list of Questions.</param> <param name="Answers">A list of Answers.</param> */ public DnsPacket(short ID, bool Query, byte Opcode, bool AA, bool RA, bool RD, Question[] Questions, Response[] Answers, Response[] Authority, Response[] Additional) { byte[] header = new byte[12]; this.ID = ID; header[0] = (byte) ((ID >> 8) & 0xFF); header[1] = (byte) (ID & 0xFF); this.Query = Query; if(!Query) { header[2] |= 0x80; } this.Opcode = Opcode; header[2] |= (byte) (Opcode << 3); this.AA = AA; if(AA) { header[2] |= 0x4; } this.RD = RD; if(RD) { header[2] |= 0x1; } this.RA = RA; if(RA) { header[3] |= 0x80; } if(Questions != null) { this.Questions = Questions; header[4] = (byte) ((Questions.Length >> 8) & 0xFF); header[5] = (byte) (Questions.Length & 0xFF); } else { this.Questions = new Question[0]; header[4] = 0; header[5] = 0; } if(Answers != null) { this.Answers = Answers; header[6] = (byte) ((Answers.Length >> 8) & 0xFF); header[7] = (byte) (Answers.Length & 0xFF); } else { this.Answers = new Response[0]; header[6] = 0; header[7] = 0; } if(Authority != null) { this.Authority = Authority; header[8] = (byte) ((Authority.Length >> 8) & 0xFF); header[9] = (byte) (Authority.Length & 0xFF); } else { this.Authority = new Response[0]; header[8] = 0; header[9] = 0; } if(Additional != null) { this.Additional = Additional; header[10] = (byte) ((Additional.Length >> 8) & 0xFF); header[11] = (byte) (Additional.Length & 0xFF); } else { this.Additional = new Response[0]; header[10] = 0; header[11] = 0; } _icpacket = MemBlock.Reference(header); for(int i = 0; i < this.Questions.Length; i++) { _icpacket = new CopyList(_icpacket, Questions[i].ICPacket); } for(int i = 0; i < this.Answers.Length; i++) { _icpacket = new CopyList(_icpacket, Answers[i].ICPacket); } for(int i = 0; i < this.Authority.Length; i++) { _icpacket = new CopyList(_icpacket, Authority[i].ICPacket); } for(int i = 0; i < this.Additional.Length; i++) { _icpacket = new CopyList(_icpacket, Additional[i].ICPacket); } }