-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathDatagramDnsQueryEncoder.cs
More file actions
82 lines (71 loc) · 2.79 KB
/
Copy pathDatagramDnsQueryEncoder.cs
File metadata and controls
82 lines (71 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using DotNetty.Transport.Channels;
using DotNetty.Codecs.DNS.Messages;
using DotNetty.Buffers;
using DotNetty.Codecs.DNS.Records;
using System.Net;
using DotNetty.Transport.Channels.Sockets;
namespace DotNetty.Codecs.DNS
{
public class DatagramDnsQueryEncoder : MessageToMessageEncoder<IAddressedEnvelope<IDnsQuery>>
{
private readonly IDnsRecordEncoder recordEncoder;
public DatagramDnsQueryEncoder() : this(new DefaultDnsRecordEncoder()) { }
public DatagramDnsQueryEncoder(IDnsRecordEncoder recordEncoder)
{
this.recordEncoder = recordEncoder ?? throw new ArgumentNullException(nameof(recordEncoder));
}
protected override void Encode(IChannelHandlerContext context, IAddressedEnvelope<IDnsQuery> message, List<object> output)
{
EndPoint recipient = message.Recipient;
IDnsQuery query = message.Content;
IByteBuffer buffer = AllocateBuffer(context, message);
bool success = false;
try
{
EncodeHeader(query, buffer);
EncodeQuestions(query, buffer);
EncodeRecords(query, DnsSection.ADDITIONAL, buffer);
success = true;
}
finally
{
if (!success)
buffer.Release();
}
output.Add(new DatagramPacket(buffer, recipient, null));
}
private IByteBuffer AllocateBuffer(IChannelHandlerContext ctx,
IAddressedEnvelope<IDnsQuery> message) => ctx.Allocator.Buffer(1024);
private void EncodeHeader(IDnsQuery query, IByteBuffer buffer)
{
buffer.WriteShort(query.Id);
int flags = 0;
flags |= (query.OpCode.ByteValue & 0xFF) << 14;
if (query.IsRecursionDesired)
flags |= 1 << 8;
buffer.WriteShort(flags);
buffer.WriteShort(query.Count(DnsSection.QUESTION));
buffer.WriteShort(0);
buffer.WriteShort(0);
buffer.WriteShort(query.Count(DnsSection.ADDITIONAL));
}
private void EncodeQuestions(IDnsQuery query, IByteBuffer buffer)
{
int count = query.Count(DnsSection.QUESTION);
for (int i = 0; i < count; i++)
{
recordEncoder.EncodeQuestion(query.GetRecord<IDnsQuestion>(DnsSection.QUESTION, i), buffer);
}
}
private void EncodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buffer)
{
int count = query.Count(section);
for (int i = 0; i < count; i++)
{
recordEncoder.EncodeRecord(query.GetRecord<IDnsRecord>(section, i), buffer);
}
}
}
}