forked from netty/netty
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TcpDnsQueryDecoder and TcpDnsResponseEncoder (netty#11415)
Motivation: There is no decoder and encoder for TCP based DNS. Result: - Added decoder and encoder - Added tests - Added example Result: Be able to decode and encode TCP based dns Co-authored-by: Norman Maurer <norman_maurer@apple.com>
- Loading branch information
1 parent
c83c5b7
commit ccef8fe
Showing
7 changed files
with
490 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
codec-dns/src/main/java/io/netty/handler/codec/dns/TcpDnsQueryDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2021 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.handler.codec.dns; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; | ||
import io.netty.util.internal.ObjectUtil; | ||
|
||
public final class TcpDnsQueryDecoder extends LengthFieldBasedFrameDecoder { | ||
private final DnsRecordDecoder decoder; | ||
|
||
/** | ||
* Creates a new decoder with {@linkplain DnsRecordDecoder#DEFAULT the default record decoder}. | ||
*/ | ||
public TcpDnsQueryDecoder() { | ||
this(DnsRecordDecoder.DEFAULT, 65535); | ||
} | ||
|
||
/** | ||
* Creates a new decoder with the specified {@code decoder}. | ||
*/ | ||
public TcpDnsQueryDecoder(DnsRecordDecoder decoder, int maxFrameLength) { | ||
super(maxFrameLength, 0, 2, 0, 2); | ||
this.decoder = ObjectUtil.checkNotNull(decoder, "decoder"); | ||
} | ||
|
||
@Override | ||
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { | ||
ByteBuf frame = (ByteBuf) super.decode(ctx, in); | ||
if (frame == null) { | ||
return null; | ||
} | ||
|
||
return DnsMessageUtil.decodeDnsQuery(decoder, frame.slice(), new DnsMessageUtil.DnsQueryFactory() { | ||
@Override | ||
public DnsQuery newQuery(int id, DnsOpCode dnsOpCode) { | ||
return new DefaultDnsQuery(id, dnsOpCode); | ||
} | ||
}); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
codec-dns/src/main/java/io/netty/handler/codec/dns/TcpDnsResponseEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2021 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.handler.codec.dns; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToMessageEncoder; | ||
import io.netty.util.internal.ObjectUtil; | ||
|
||
import java.util.List; | ||
|
||
@ChannelHandler.Sharable | ||
public final class TcpDnsResponseEncoder extends MessageToMessageEncoder<DnsResponse> { | ||
private final DnsRecordEncoder encoder; | ||
|
||
/** | ||
* Creates a new encoder with {@linkplain DnsRecordEncoder#DEFAULT the default record encoder}. | ||
*/ | ||
public TcpDnsResponseEncoder() { | ||
this(DnsRecordEncoder.DEFAULT); | ||
} | ||
|
||
/** | ||
* Creates a new encoder with the specified {@code encoder}. | ||
*/ | ||
public TcpDnsResponseEncoder(DnsRecordEncoder encoder) { | ||
this.encoder = ObjectUtil.checkNotNull(encoder, "encoder"); | ||
} | ||
|
||
@Override | ||
protected void encode(ChannelHandlerContext ctx, DnsResponse response, List<Object> out) throws Exception { | ||
ByteBuf buf = ctx.alloc().ioBuffer(1024); | ||
|
||
buf.writerIndex(buf.writerIndex() + 2); | ||
DnsMessageUtil.encodeDnsResponse(encoder, response, buf); | ||
buf.setShort(0, buf.readableBytes() - 2); | ||
|
||
out.add(buf); | ||
} | ||
} |
Oops, something went wrong.