Home / Class/ DnsCodecUtil Class — netty Architecture

DnsCodecUtil Class — netty Architecture

Architecture documentation for the DnsCodecUtil class in DnsCodecUtil.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  b5deaff8_7855_06da_81ac_dd1ff0412cf2["DnsCodecUtil"]
  40ba4582_d213_edce_6fc4_9933cc635d03["DnsCodecUtil.java"]
  b5deaff8_7855_06da_81ac_dd1ff0412cf2 -->|defined in| 40ba4582_d213_edce_6fc4_9933cc635d03
  8f3840af_a761_0649_4c09_5483146e4232["DnsCodecUtil()"]
  b5deaff8_7855_06da_81ac_dd1ff0412cf2 -->|method| 8f3840af_a761_0649_4c09_5483146e4232
  e934b2cf_575e_e555_ddd2_e343ea873972["encodeDomainName()"]
  b5deaff8_7855_06da_81ac_dd1ff0412cf2 -->|method| e934b2cf_575e_e555_ddd2_e343ea873972
  bf1393c0_29d3_74ac_3a63_fc1091b78bb6["String()"]
  b5deaff8_7855_06da_81ac_dd1ff0412cf2 -->|method| bf1393c0_29d3_74ac_3a63_fc1091b78bb6
  54bc4f71_0cae_4aae_6759_217b6cada96d["ByteBuf()"]
  b5deaff8_7855_06da_81ac_dd1ff0412cf2 -->|method| 54bc4f71_0cae_4aae_6759_217b6cada96d

Relationship Graph

Source Code

codec-dns/src/main/java/io/netty/handler/codec/dns/DnsCodecUtil.java lines 26–131

final class DnsCodecUtil {
    private DnsCodecUtil() {
        // Util class
    }

    static void encodeDomainName(String name, ByteBuf buf) {
        if (ROOT.equals(name)) {
            // Root domain
            buf.writeByte(0);
            return;
        }

        final String[] labels = name.split("\\.");
        for (String label : labels) {
            final int labelLen = label.length();
            if (labelLen == 0) {
                // zero-length label means the end of the name.
                break;
            }

            buf.writeByte(labelLen);
            ByteBufUtil.writeAscii(buf, label);
        }

        buf.writeByte(0); // marks end of name field
    }

    static String decodeDomainName(ByteBuf in) {
        int position = -1;
        int checked = 0;
        final int end = in.writerIndex();
        final int readable = in.readableBytes();

        // Looking at the spec we should always have at least enough readable bytes to read a byte here but it seems
        // some servers do not respect this for empty names. So just workaround this and return an empty name in this
        // case.
        //
        // See:
        // - https://github.com/netty/netty/issues/5014
        // - https://www.ietf.org/rfc/rfc1035.txt , Section 3.1
        if (readable == 0) {
            return ROOT;
        }

        final StringBuilder name = new StringBuilder(readable << 1);
        while (in.isReadable()) {
            final int len = in.readUnsignedByte();
            final boolean pointer = (len & 0xc0) == 0xc0;
            if (pointer) {
                if (position == -1) {
                    position = in.readerIndex() + 1;
                }

                if (!in.isReadable()) {
                    throw new CorruptedFrameException("truncated pointer in a name");
                }

                final int next = (len & 0x3f) << 8 | in.readUnsignedByte();
                if (next >= end) {
                    throw new CorruptedFrameException("name has an out-of-range pointer");
                }
                in.readerIndex(next);

                // check for loops
                checked += 2;
                if (checked >= end) {
                    throw new CorruptedFrameException("name contains a loop.");
                }
            } else if (len != 0) {
                if (!in.isReadable(len)) {
                    throw new CorruptedFrameException("truncated label in a name");
                }
                name.append(in.toString(in.readerIndex(), len, CharsetUtil.UTF_8)).append('.');
                in.skipBytes(len);
            } else { // len == 0
                break;
            }
        }

        if (position != -1) {
            in.readerIndex(position);

Frequently Asked Questions

What is the DnsCodecUtil class?
DnsCodecUtil is a class in the netty codebase, defined in codec-dns/src/main/java/io/netty/handler/codec/dns/DnsCodecUtil.java.
Where is DnsCodecUtil defined?
DnsCodecUtil is defined in codec-dns/src/main/java/io/netty/handler/codec/dns/DnsCodecUtil.java at line 26.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free