/// <summary> /// Deserialize the raw ResourceRecord embedded in this DnsRecord /// </summary> public DnsResourceRecord Deserialize() { if (this.RecordData.IsNullOrEmpty()) { throw new InvalidOperationException("Empty record data found."); } DnsBufferReader bufferReader = new DnsBufferReader(this.RecordData, 0, this.RecordData.Length); return(DnsResourceRecord.Deserialize(ref bufferReader)); }
T Roundtrip <T>(T record) where T : DnsResourceRecord { DnsBuffer buffer = new DnsBuffer(1024); record.Serialize(buffer); DnsBufferReader reader = buffer.CreateReader(); return((T)DnsResourceRecord.Deserialize(ref reader)); }
/// <summary> /// loads and verifies the dnsrecords from the bin associated bin files, ensuring that the types /// match up /// </summary> /// <typeparam name="T">Type of record that is expected</typeparam> /// <param name="path">path to the bin file to be loaded</param> /// <returns>bytes from the bin file</returns> protected byte[] LoadAndVerifyDnsRecordFromBin <T>(string path) { byte[] bytes = null; //---------------------------------------------------------------------------------------------------- //---read the stream from the bytes using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { //Console.WriteLine("checking [{0}]", path); bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length); DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length); DnsResourceRecord rec = DnsResourceRecord.Deserialize(ref rdr); Assert.Equal(rec.GetType(), typeof(T)); } return(bytes); }
/// <summary> /// loads and verifies the dnsrecords from the bin associated bin files, ensuring that the types /// match up /// </summary> /// <typeparam name="T">Type of record that is expected</typeparam> /// <param name="path">path to the bin file to be loaded</param> /// <returns>bytes from the bin file</returns> protected T LoadAndVerifyDnsRecordFromBin <T>(string path) where T : DnsResourceRecord { T record; //---------------------------------------------------------------------------------------------------- //---read the stream from the bytes using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length); DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length); record = DnsResourceRecord.Deserialize(ref rdr) as T; if (record == null) { throw new TypeLoadException("unexpected type encountered in file"); } } return(record); }
public void CreateDnsResourceRecords(string domain) { DnsBuffer buff = new DnsBuffer(); byte[] bytes; AddressRecord arec = new AddressRecord(domain , "127.0.0.1") { TTL = 1000 }; arec.Serialize(buff); string path = Path.Combine(DNSRECORDSEPATH, string.Format("aname.{0}.bin", domain)); Console.WriteLine("Creating {0}", path); using (FileStream s = new FileStream(path, FileMode.OpenOrCreate)) { s.Write(buff.Buffer , 0 , buff.Buffer.Length); s.Close(); } //---------------------------------------------------------------------------------------------------- //---read the stream from the bytes using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { Console.WriteLine("checking [{0}]", path); bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length); DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length); arec = (AddressRecord)DnsResourceRecord.Deserialize(ref rdr); } Console.WriteLine(arec.IPAddress); Console.WriteLine(arec.TTL); Console.WriteLine(arec.Name); //---------------------------------------------------------------------------------------------------------------- SOARecord soa = new SOARecord(domain , domain + ".dom" , "somebody" , 1 , 2 , 3 , 4 , 5) { TTL = 2000 }; buff = new DnsBuffer(); soa.Serialize(buff); path = Path.Combine(DNSRECORDSEPATH, string.Format("soa.{0}.bin", domain)); Console.WriteLine("Creating {0}", path); using (FileStream s = new FileStream(path, FileMode.OpenOrCreate)) { s.Write(buff.Buffer , 0 , buff.Buffer.Length); s.Close(); } //---------------------------------------------------------------------------------------------------- //---read the stream from the bytes using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { Console.WriteLine("checking [{0}]", path); bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length); DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length); soa = (SOARecord)DnsResourceRecord.Deserialize(ref rdr); } Console.WriteLine(soa.ResponsibleName); Console.WriteLine(soa.SerialNumber); Console.WriteLine(soa.Retry); Console.WriteLine(soa.Refresh); Console.WriteLine(soa.Expire); Console.WriteLine(soa.Minimum); Console.WriteLine(soa.TTL); Console.WriteLine(soa.Name); //---------------------------------------------------------------------------------------------------------------- MXRecord mx = new MXRecord(domain , string.Format("mx.{0}", domain) , 1) { TTL = 2000 }; buff = new DnsBuffer(); mx.Serialize(buff); path = Path.Combine(DNSRECORDSEPATH, string.Format("mx.{0}.bin", domain)); Console.WriteLine("Creating {0}", path); using (FileStream s = new FileStream(path, FileMode.OpenOrCreate)) { s.Write(buff.Buffer , 0 , buff.Buffer.Length); s.Close(); } //---------------------------------------------------------------------------------------------------- //---read the stream from the bytes using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { Console.WriteLine("checking [{0}]", path); bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length); DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length); mx = (MXRecord)DnsResourceRecord.Deserialize(ref rdr); } Console.WriteLine(mx.Exchange); Console.WriteLine(mx.Name); Console.WriteLine(mx.Preference); //---------------------------------------------------------------------------------------------------------------- //---create the cert on the fly CertRecord cert = new CertRecord(new DnsX509Cert(CreateNamedKeyCertificate(new CertData(domain , domain , string.Format("CN={0}", domain) , "")))) { TTL = 2000 }; buff = new DnsBuffer(); cert.Serialize(buff); path = Path.Combine(DNSRECORDSEPATH, string.Format("cert.{0}.bin", domain)); Console.WriteLine("Creating {0}", path); using (FileStream s = new FileStream(path, FileMode.OpenOrCreate)) { s.Write(buff.Buffer , 0 , buff.Buffer.Length); s.Close(); } //---------------------------------------------------------------------------------------------------- //---read the stream from the bytes using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { Console.WriteLine("checking [{0}]", path); bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length); DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length); cert = (CertRecord)DnsResourceRecord.Deserialize(ref rdr); } Console.WriteLine(cert.Name); Console.WriteLine(cert.Cert.Certificate.NotBefore); Console.WriteLine(cert.Cert.Certificate.NotAfter); }