public void DnsRecordFactory_MBRecord() { var name = DnsString.Parse("Müsli.de"); using (var pool = new PooledBytes(DnsDatagramWriter.BufferSize)) { var writer = new DnsDatagramWriter(new ArraySegment <byte>(pool.Buffer)); writer.WriteHostName(name.Value); var factory = GetFactory( writer.Data.ToArray(), out var reader); var info = new ResourceRecord( DnsString.Parse("Müsli.de"), MbRecord.ResourceRecordType, QueryClass.In, 0, writer.Data.Count); var result = factory.GetRecord( info, reader) as MbRecord ?? throw new Exception(); Assert.Equal( result.MadName, name); Assert.Equal( "müsli.de.", result.MadName.Original); } }
public DnsResponseMessage Resolve(IPEndPoint server, DnsRequestMessage dnsRequest) { using (communicator) { IUdpAdapter clientSocket = communicator.Client; int timeoutInMillis = 5000; clientSocket.ReceiveTimeout = timeoutInMillis; clientSocket.SendTimeout = timeoutInMillis; // // write the dns query request data on in bytes to send to the DNS server. // using (IDnsDatagramWriter writer = new DnsDatagramWriter()) { ConstructRequestData(dnsRequest, writer); clientSocket.SendTo(writer.Data.Array, 0, writer.Data.Count, SocketFlags.None, server); } // // Read the dns query response from the DNS server. // using (PooledBytes memory = new PooledBytes(ReadSize)) { int received = clientSocket.Receive(memory.Buffer, 0, ReadSize, SocketFlags.None); DnsResponseMessage response = messageProcessor.ProcessResponse(memory.BufferSegment); if (dnsRequest.Header.Identifier != response.Header.Identifier) { throw new DnsResponseException("Header id mismatch."); } return(response); } } }
public void DnsRecordFactory_PTRRecord() { var name = DnsString.Parse("result.example.com"); using (var pool = new PooledBytes(DnsDatagramWriter.BufferSize)) { var writer = new DnsDatagramWriter(new ArraySegment <byte>(pool.Buffer)); writer.WriteHostName(name.Value); var factory = GetFactory( writer.Data.ToArray(), out var reader); var info = new ResourceRecord( DnsString.Parse("query.example.com"), PtrRecord.ResourceRecordType, QueryClass.In, 0, writer.Data.Count); var result = factory.GetRecord( info, reader) as PtrRecord ?? throw new Exception(); Assert.Equal( result.PtrDomainName, name); } }
public void DnsRecordFactory_SRVRecord() { var name = DnsString.Parse("result.example.com"); using (var pool = new PooledBytes(DnsDatagramWriter.BufferSize)) { var writer = new DnsDatagramWriter(new ArraySegment <byte>(pool.Buffer)); writer.WriteBytes( new byte[] { 0, 1, 1, 0, 2, 3 }, 6); writer.WriteHostName(name.Value); var factory = GetFactory( writer.Data.ToArray(), out var reader); var info = new ResourceRecord( DnsString.Parse("query.example.com"), SrvRecord.ResourceRecordType, QueryClass.In, 0, writer.Data.Count); var result = factory.GetRecord( info, reader) as SrvRecord ?? throw new Exception(); Assert.Equal( result.Target, name); Assert.True(result.Priority == 1); Assert.True(result.Weight == 256); Assert.True(result.Port == 515); } }
private static byte[] GetResponseBytes(IDnsQueryResponse message, byte[] answerData) { using (var pool = new PooledBytes(DnsDatagramWriter.BufferSize)) { var writer = new DnsDatagramWriter(new ArraySegment <byte>(pool.Buffer)); writer.WriteUInt16NetworkOrder((ushort)message.Header.Id); writer.WriteUInt16NetworkOrder((ushort)message.Header.HeaderFlags); // lets iterate answers only, makes it easier //writer.WriteUInt16Network((ushort)message.Header.QuestionCount); writer.WriteUInt16NetworkOrder(0); writer.WriteUInt16NetworkOrder(1); //writer.WriteUInt16Network((ushort)message.Header.NameServerCount); writer.WriteUInt16NetworkOrder(0); //writer.WriteUInt16Network((ushort)message.Header.AdditionalCount); writer.WriteUInt16NetworkOrder(0); var answer = message.Answers.First(); writer.WriteHostName(answer.DomainName.Value); writer.WriteUInt16NetworkOrder(answer.RecordType.Value); writer.WriteUInt16NetworkOrder((ushort)answer.RecordClass); writer.WriteUInt32NetworkOrder((uint)answer.TimeToLive); writer.WriteUInt16NetworkOrder((ushort)answerData.Length); //writer.Extend(answerData.Length); // the following data->length writer.WriteBytes(answerData, answerData.Length); return(writer.Data.ToArray()); } }
public void BufferSegmentTest() { // Arrange. int count = 10; PooledBytes pool = new PooledBytes(count); // Act. ArraySegment <byte> segmentedArray = pool.BufferSegment; // Act. Assert.AreEqual(count, segmentedArray.Count); }
public ArraySegment <byte> BufferedWrite() { using (var bytes = new PooledBytes(ShortTestValueByteLength * Ops + Ops)) using (var writer = new DnsDatagramWriter(bytes.BufferSegment)) { for (var i = 0; i < Ops; i++) { writer.WriteHostName(ShortTestValue); } return(writer.Data); } }
public ArraySegment <byte> BufferedWriteInt() { using (var bytes = new PooledBytes(4 * Ops)) using (var writer = new DnsDatagramWriter(bytes.BufferSegment)) { for (var i = 0; i < 1000; i++) { writer.WriteInt32NetworkOrder(i); } return(writer.Data); } }
public void PooledByte_SimpleRent() { using (var b = new PooledBytes(10)) { for (byte i = 0; i < 10; i++) { b.Buffer[i] = i; } Assert.Equal(10, b.BufferSegment.Count); Assert.True(b.Buffer.Length > 10); Assert.Equal(9, b.Buffer[9]); } }
public void BufferTest() { // Arrange. PooledBytes pool = new PooledBytes(3); // Act. byte[] buffer = pool.Buffer; // Assert. Assert.IsNotNull(buffer); Assert.AreEqual(16, buffer.Length); }
public void TestDispose() { // Arrange. PooledBytes pool = new PooledBytes(10); // act. pool.Dispose(); // assert. Assert.Throws <ObjectDisposedException>(() => { ArraySegment <byte> buffer = pool.BufferSegment; }); }
public void PooledByte_Extend() { using (var b = new PooledBytes(10)) { for (byte i = 0; i < 10; i++) { b.Buffer[i] = i; } Assert.Equal(10, b.BufferSegment.Count); b.Extend(100); b.Buffer[109] = 42; Assert.Equal(110, b.BufferSegment.Count); Assert.True(b.Buffer.Length > 110); Assert.Equal(1, b.Buffer[1]); Assert.Equal(9, b.Buffer[9]); Assert.Equal(0, b.Buffer[10]); Assert.Equal(42, b.Buffer[109]); } }