Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

没有改动过你的代码,只是做了以下操作 #331

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
68 changes: 60 additions & 8 deletions src/DotNetty.Codecs/DotNetty.Codecs.DNS/DatagramDnsQueryDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Collections.Generic;
using DotNetty.Transport.Channels;
using DotNetty.Buffers;
using DotNetty.Codecs.DNS.Messages;
using DotNetty.Buffers;
using DotNetty.Codecs.DNS.Records;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Collections.Generic;

namespace DotNetty.Codecs.DNS
{
/// <summary>
/// Defines the <see cref="DatagramDnsQueryDecoder" />
/// </summary>
public class DatagramDnsQueryDecoder : MessageToMessageDecoder<DatagramPacket>
{
#region 字段

/// <summary>
/// Defines the recordDecoder
/// </summary>
private readonly IDnsRecordDecoder recordDecoder;

public DatagramDnsQueryDecoder() : this(new DefaultDnsRecordDecoder()) { }
#endregion 字段

#region 构造函数

/// <summary>
/// Initializes a new instance of the <see cref="DatagramDnsQueryDecoder"/> class.
/// </summary>
public DatagramDnsQueryDecoder() : this(new DefaultDnsRecordDecoder())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DatagramDnsQueryDecoder"/> class.
/// </summary>
/// <param name="recordDecoder">The recordDecoder<see cref="IDnsRecordDecoder"/></param>
public DatagramDnsQueryDecoder(IDnsRecordDecoder recordDecoder)
{
this.recordDecoder = recordDecoder ?? throw new ArgumentNullException(nameof(recordDecoder));
}

#endregion 构造函数

#region 方法

/// <summary>
/// The Decode
/// </summary>
/// <param name="context">The context<see cref="IChannelHandlerContext"/></param>
/// <param name="message">The message<see cref="DatagramPacket"/></param>
/// <param name="output">The output<see cref="List{object}"/></param>
protected override void Decode(IChannelHandlerContext context, DatagramPacket message, List<object> output)
{
IByteBuffer buffer = message.Content;
Expand Down Expand Up @@ -47,6 +78,12 @@ protected override void Decode(IChannelHandlerContext context, DatagramPacket me
}
}

/// <summary>
/// The NewQuery
/// </summary>
/// <param name="packet">The packet<see cref="DatagramPacket"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
/// <returns>The <see cref="IDnsQuery"/></returns>
private static IDnsQuery NewQuery(DatagramPacket packet, IByteBuffer buffer)
{
int id = buffer.ReadUnsignedShort();
Expand All @@ -55,14 +92,20 @@ private static IDnsQuery NewQuery(DatagramPacket packet, IByteBuffer buffer)
throw new CorruptedFrameException("not a query");

IDnsQuery query = new DatagramDnsQuery(
packet.Sender, packet.Recipient, id,
packet.Sender, packet.Recipient, id,
DnsOpCode.From((byte)(flags >> 11 & 0xf)));

query.IsRecursionDesired = (flags >> 8 & 1) == 1;
query.Z = flags >> 4 & 0x7;
return query;
}

/// <summary>
/// The DecodeQuestions
/// </summary>
/// <param name="query">The query<see cref="IDnsQuery"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
/// <param name="questionCount">The questionCount<see cref="int"/></param>
private void DecodeQuestions(IDnsQuery query, IByteBuffer buffer, int questionCount)
{
for (int i = questionCount; i > 0; i--)
Expand All @@ -71,6 +114,13 @@ private void DecodeQuestions(IDnsQuery query, IByteBuffer buffer, int questionCo
}
}

/// <summary>
/// The DecodeRecords
/// </summary>
/// <param name="query">The query<see cref="IDnsQuery"/></param>
/// <param name="section">The section<see cref="DnsSection"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
/// <param name="count">The count<see cref="int"/></param>
private void DecodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buffer, int count)
{
for (int i = count; i > 0; i--)
Expand All @@ -82,5 +132,7 @@ private void DecodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buff
query.AddRecord(section, r);
}
}

#endregion 方法
}
}
}
71 changes: 63 additions & 8 deletions src/DotNetty.Codecs/DotNetty.Codecs.DNS/DatagramDnsQueryEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
using System;
using System.Collections.Generic;
using DotNetty.Transport.Channels;
using DotNetty.Buffers;
using DotNetty.Codecs.DNS.Messages;
using DotNetty.Buffers;
using DotNetty.Codecs.DNS.Records;
using System.Net;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Collections.Generic;
using System.Net;

namespace DotNetty.Codecs.DNS
{
/// <summary>
/// Defines the <see cref="DatagramDnsQueryEncoder" />
/// </summary>
public class DatagramDnsQueryEncoder : MessageToMessageEncoder<IAddressedEnvelope<IDnsQuery>>
{
#region 字段

/// <summary>
/// Defines the recordEncoder
/// </summary>
private readonly IDnsRecordEncoder recordEncoder;

public DatagramDnsQueryEncoder() : this(new DefaultDnsRecordEncoder()) { }
#endregion 字段

#region 构造函数

/// <summary>
/// Initializes a new instance of the <see cref="DatagramDnsQueryEncoder"/> class.
/// </summary>
public DatagramDnsQueryEncoder() : this(new DefaultDnsRecordEncoder())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DatagramDnsQueryEncoder"/> class.
/// </summary>
/// <param name="recordEncoder">The recordEncoder<see cref="IDnsRecordEncoder"/></param>
public DatagramDnsQueryEncoder(IDnsRecordEncoder recordEncoder)
{
this.recordEncoder = recordEncoder ?? throw new ArgumentNullException(nameof(recordEncoder));
}

#endregion 构造函数

#region 方法

/// <summary>
/// The Encode
/// </summary>
/// <param name="context">The context<see cref="IChannelHandlerContext"/></param>
/// <param name="message">The message<see cref="IAddressedEnvelope{IDnsQuery}"/></param>
/// <param name="output">The output<see cref="List{object}"/></param>
protected override void Encode(IChannelHandlerContext context, IAddressedEnvelope<IDnsQuery> message, List<object> output)
{
EndPoint recipient = message.Recipient;
Expand All @@ -43,9 +74,20 @@ protected override void Encode(IChannelHandlerContext context, IAddressedEnvelop
output.Add(new DatagramPacket(buffer, recipient, null));
}

private IByteBuffer AllocateBuffer(IChannelHandlerContext ctx,
/// <summary>
/// The AllocateBuffer
/// </summary>
/// <param name="ctx">The ctx<see cref="IChannelHandlerContext"/></param>
/// <param name="message">The message<see cref="IAddressedEnvelope{IDnsQuery}"/></param>
/// <returns>The <see cref="IByteBuffer"/></returns>
private IByteBuffer AllocateBuffer(IChannelHandlerContext ctx,
IAddressedEnvelope<IDnsQuery> message) => ctx.Allocator.Buffer(1024);

/// <summary>
/// The EncodeHeader
/// </summary>
/// <param name="query">The query<see cref="IDnsQuery"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
private void EncodeHeader(IDnsQuery query, IByteBuffer buffer)
{
buffer.WriteShort(query.Id);
Expand All @@ -61,6 +103,11 @@ private void EncodeHeader(IDnsQuery query, IByteBuffer buffer)
buffer.WriteShort(query.Count(DnsSection.ADDITIONAL));
}

/// <summary>
/// The EncodeQuestions
/// </summary>
/// <param name="query">The query<see cref="IDnsQuery"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
private void EncodeQuestions(IDnsQuery query, IByteBuffer buffer)
{
int count = query.Count(DnsSection.QUESTION);
Expand All @@ -70,6 +117,12 @@ private void EncodeQuestions(IDnsQuery query, IByteBuffer buffer)
}
}

/// <summary>
/// The EncodeRecords
/// </summary>
/// <param name="query">The query<see cref="IDnsQuery"/></param>
/// <param name="section">The section<see cref="DnsSection"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
private void EncodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buffer)
{
int count = query.Count(section);
Expand All @@ -78,5 +131,7 @@ private void EncodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buff
recordEncoder.EncodeRecord(query.GetRecord<IDnsRecord>(section, i), buffer);
}
}

#endregion 方法
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Collections.Generic;
using DotNetty.Transport.Channels;
using DotNetty.Buffers;
using DotNetty.Codecs.DNS.Messages;
using DotNetty.Buffers;
using DotNetty.Codecs.DNS.Records;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Collections.Generic;

namespace DotNetty.Codecs.DNS
{
/// <summary>
/// Defines the <see cref="DatagramDnsResponseDecoder" />
/// </summary>
public class DatagramDnsResponseDecoder : MessageToMessageDecoder<DatagramPacket>
{
#region 字段

/// <summary>
/// Defines the recordDecoder
/// </summary>
private readonly IDnsRecordDecoder recordDecoder;

public DatagramDnsResponseDecoder() : this(new DefaultDnsRecordDecoder()) { }
#endregion 字段

#region 构造函数

/// <summary>
/// Initializes a new instance of the <see cref="DatagramDnsResponseDecoder"/> class.
/// </summary>
public DatagramDnsResponseDecoder() : this(new DefaultDnsRecordDecoder())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DatagramDnsResponseDecoder"/> class.
/// </summary>
/// <param name="recordDecoder">The recordDecoder<see cref="IDnsRecordDecoder"/></param>
public DatagramDnsResponseDecoder(IDnsRecordDecoder recordDecoder)
{
this.recordDecoder = recordDecoder ?? throw new ArgumentNullException(nameof(recordDecoder));
}

#endregion 构造函数

#region 方法

/// <summary>
/// The Decode
/// </summary>
/// <param name="context">The context<see cref="IChannelHandlerContext"/></param>
/// <param name="message">The message<see cref="DatagramPacket"/></param>
/// <param name="output">The output<see cref="List{object}"/></param>
protected override void Decode(IChannelHandlerContext context, DatagramPacket message, List<object> output)
{
IByteBuffer buffer = message.Content;
Expand Down Expand Up @@ -47,21 +78,33 @@ protected override void Decode(IChannelHandlerContext context, DatagramPacket me
}
}

/// <summary>
/// The NewResponse
/// </summary>
/// <param name="packet">The packet<see cref="DatagramPacket"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
/// <returns>The <see cref="IDnsResponse"/></returns>
private static IDnsResponse NewResponse(DatagramPacket packet, IByteBuffer buffer)
{
int id = buffer.ReadUnsignedShort();
int flags = buffer.ReadUnsignedShort();
if (flags >> 15 == 0) throw new CorruptedFrameException("not a response");

IDnsResponse response = new DatagramDnsResponse(
packet.Sender,
packet.Recipient,
id,
DnsOpCode.From((byte)(flags >> 1 & 0xf)),
packet.Sender,
packet.Recipient,
id,
DnsOpCode.From((byte)(flags >> 1 & 0xf)),
DnsResponseCode.From((byte)(flags & 0xf)));
return response;
}

/// <summary>
/// The DecodeQuestions
/// </summary>
/// <param name="response">The response<see cref="IDnsResponse"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
/// <param name="questionCount">The questionCount<see cref="int"/></param>
private void DecodeQuestions(IDnsResponse response, IByteBuffer buffer, int questionCount)
{
for (int i = questionCount - 1; i > 0; i--)
Expand All @@ -70,6 +113,13 @@ private void DecodeQuestions(IDnsResponse response, IByteBuffer buffer, int ques
}
}

/// <summary>
/// The DecodeRecords
/// </summary>
/// <param name="response">The response<see cref="IDnsResponse"/></param>
/// <param name="section">The section<see cref="DnsSection"/></param>
/// <param name="buffer">The buffer<see cref="IByteBuffer"/></param>
/// <param name="count">The count<see cref="int"/></param>
private void DecodeRecords(IDnsResponse response, DnsSection section, IByteBuffer buffer, int count)
{
for (int i = count - 1; i > 0; i--)
Expand All @@ -81,5 +131,7 @@ private void DecodeRecords(IDnsResponse response, DnsSection section, IByteBuffe
response.AddRecord(section, r);
}
}

#endregion 方法
}
}
}
Loading