/** * <summary>Creates a response from the parameter fields with RData being * a memory chunk. This is for MDns which supports caching</summary> * <param name="Name">The name resolved.</param> * <param name="Type">The query type.</param> * <param name="Class">The network type.</param> * <param name="CacheFlush">Flush the dns cache in the client.</param> * <param name="Ttl">How long to hold the result in the local dns cache.</param> * <param name="RData">RData in String format.</param> */ public Response(string name, DnsPacket.Types type, DnsPacket.Classes class_type, bool cache_flush, int ttl, String rdata) { Name = name; Class = class_type; Ttl = ttl; Type = type; CacheFlush = cache_flush; RData = rdata; if (Type == DnsPacket.Types.A || Type == DnsPacket.Types.AAAA) { NameBlob = DnsPacket.HostnameStringToMemBlock(Name); RDataBlob = DnsPacket.IPStringToMemBlock(RData); } else if (Type == DnsPacket.Types.Ptr) { if (DnsPacket.StringIsIP(Name)) { NameBlob = DnsPacket.PtrStringToMemBlock(Name); } else { NameBlob = DnsPacket.HostnameStringToMemBlock(Name); } RDataBlob = DnsPacket.HostnameStringToMemBlock(RData); } else { throw new Exception("Invalid Query Type: " + Type + "!"); } RdLength = (short)RDataBlob.Length; // 2 for Type + 2 for Class + 4 for Ttl + 2 for RdLength byte[] data = new byte[10]; int idx = 0; data[idx++] = (byte)((((int)Type) >> 8) & 0xFF); data[idx++] = (byte)(((int)Type) & 0xFF); byte cf = 0x80; if (!CacheFlush) { cf = 0x00; } data[idx++] = (byte)(((((int)Class) >> 8) & 0x7F) | cf); data[idx++] = (byte)(((int)Class) & 0xFF); data[idx++] = (byte)((Ttl >> 24) & 0xFF); data[idx++] = (byte)((Ttl >> 16) & 0xFF); data[idx++] = (byte)((Ttl >> 8) & 0xFF); data[idx++] = (byte)(Ttl & 0xFF); data[idx++] = (byte)((RdLength >> 8) & 0xFF); data[idx] = (byte)(RdLength & 0xFF); _icpacket = new CopyList(NameBlob, MemBlock.Reference(data), RDataBlob); }
public void TestIP() { String ip = "208.80.152.3"; MemBlock ipm = MemBlock.Reference(new byte[] { 0xd0, 0x50, 0x98, 0x03 }); Assert.AreEqual(ip, DnsPacket.IPMemBlockToString(ipm), "IPMemBlockToString"); Assert.AreEqual(ipm, DnsPacket.IPStringToMemBlock(ip), "IPStringToMemBlock"); Assert.AreEqual(ip, DnsPacket.IPMemBlockToString( DnsPacket.IPStringToMemBlock(ip)), "IP String dual"); Assert.AreEqual(ipm, DnsPacket.IPStringToMemBlock( DnsPacket.IPMemBlockToString(ipm)), "IP MemBlock dual"); String bad_ip = "Test.Test.Test.123"; MemBlock bad_ipm = null; try { bad_ipm = DnsPacket.IPStringToMemBlock(bad_ip); } catch {} Assert.AreEqual(null, bad_ipm, "Bad IP"); }