GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — codec-dns: Report and allow setting full 16-bit EDNS(0) flags field (… · netty/netty@b75f5fd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-dns/src
    • main/java/io/netty/handler/codec/dns
      • AbstractDnsOptPseudoRrRecord.java
      • DnsOptPseudoRecord.java
    • test/java/io/netty/handler/codec/dns
      • AbstractDnsOptPseudoRrRecordTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-dns/src/main/java/io/netty/handler/codec/dns/AbstractDnsOptPseudoRrRecord.java‎

Copy file name to clipboardExpand all lines: codec-dns/src/main/java/io/netty/handler/codec/dns/AbstractDnsOptPseudoRrRecord.java
+20-5Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,32 @@
2626
public abstract class AbstractDnsOptPseudoRrRecord extends AbstractDnsRecord implements DnsOptPseudoRecord {
2727

2828
protected AbstractDnsOptPseudoRrRecord(int maxPayloadSize, int extendedRcode, int version) {
29-
super(StringUtil.EMPTY_STRING, DnsRecordType.OPT, maxPayloadSize, packIntoLong(extendedRcode, version));
29+
this(maxPayloadSize, extendedRcode, version, 0);
30+
}
31+
32+
/**
33+
* Creates a new instance.
34+
*
35+
* @param maxPayloadSize the maximum UDP payload size that can be reassembled and delivered by this
36+
* endpoint's network stack, encoded into the {@code CLASS} field
37+
* @param extendedRcode the upper 8 bits of the extended 12-bit RCODE; the lower 4 bits are carried
38+
* by the DNS message header
39+
* @param version the EDNS version
40+
* @param flags the 16-bit flags field, which holds {@code DO} and {@code Z}; only its
41+
* low 16 bits are used
42+
*/
43+
protected AbstractDnsOptPseudoRrRecord(int maxPayloadSize, int extendedRcode, int version, int flags) {
44+
super(StringUtil.EMPTY_STRING, DnsRecordType.OPT, maxPayloadSize,
45+
packIntoLong(extendedRcode, version, flags));
3046
}
3147

3248
protected AbstractDnsOptPseudoRrRecord(int maxPayloadSize) {
3349
super(StringUtil.EMPTY_STRING, DnsRecordType.OPT, maxPayloadSize, 0);
3450
}
3551

3652
// See https://tools.ietf.org/html/rfc6891#section-6.1.3
37-
private static long packIntoLong(int val, int val2) {
38-
// We are currently not support DO and Z fields, just use 0.
39-
return ((val & 0xffL) << 24 | (val2 & 0xff) << 16) & 0xFFFFFFFFL;
53+
private static long packIntoLong(int extendedRcode, int version, int flags) {
54+
return ((extendedRcode & 0xffL) << 24 | (version & 0xffL) << 16 | (flags & 0xffffL)) & 0xFFFFFFFFL;
4055
}
4156

4257
@Override
@@ -51,7 +66,7 @@ public int version() {
5166

5267
@Override
5368
public int flags() {
54-
return (short) ((short) timeToLive() & 0xff);
69+
return (int) (timeToLive() & 0xffffL);
5570
}
5671

5772
@Override
Collapse file

‎codec-dns/src/main/java/io/netty/handler/codec/dns/DnsOptPseudoRecord.java‎

Copy file name to clipboardExpand all lines: codec-dns/src/main/java/io/netty/handler/codec/dns/DnsOptPseudoRecord.java
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ public interface DnsOptPseudoRecord extends DnsRecord {
3434
int version();
3535

3636
/**
37-
* Returns the {@code flags} which includes {@code DO} and {@code Z} which is encoded
38-
* into {@link DnsOptPseudoRecord#timeToLive()}.
37+
* Returns the 16-bit {@code flags} field which is encoded into the lower 16 bits of
38+
* {@link DnsOptPseudoRecord#timeToLive()}, as laid out by
39+
* <a href="https://tools.ietf.org/html/rfc6891#section-6.1.3">RFC 6891</a>.
40+
* <p>
41+
* {@code DO}, defined by <a href="https://tools.ietf.org/html/rfc3225#section-3">RFC 3225</a>, is the
42+
* most significant bit of that field, so {@code (flags() & 0x8000) != 0} tests for {@code DNSSEC OK}.
43+
* Further bits are assigned by the IANA {@code EDNS Header Flags} registry; the remaining bits are
44+
* {@code Z}, which senders set to zero and receivers ignore.
3945
*/
4046
int flags();
4147
}
Collapse file

‎codec-dns/src/test/java/io/netty/handler/codec/dns/AbstractDnsOptPseudoRrRecordTest.java‎

Copy file name to clipboard
+147Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.dns;
17+
18+
import io.netty.buffer.ByteBuf;
19+
import io.netty.buffer.Unpooled;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
24+
public class AbstractDnsOptPseudoRrRecordTest {
25+
26+
// DNSSEC OK, see https://tools.ietf.org/html/rfc3225#section-3
27+
private static final int DO = 0x8000;
28+
29+
// Compact Answers OK, see https://www.rfc-editor.org/rfc/rfc9824.html#section-5.1
30+
private static final int CO = 0x4000;
31+
32+
private static DnsOptPseudoRecord newRecord(int maxPayloadSize, int extendedRcode, int version, int flags) {
33+
return new AbstractDnsOptPseudoRrRecord(maxPayloadSize, extendedRcode, version, flags) { };
34+
}
35+
36+
@Test
37+
public void testFlagsCanCarryDo() {
38+
DnsOptPseudoRecord record = newRecord(4096, 0, 0, DO);
39+
assertEquals(DO, record.flags());
40+
assertEquals(0, record.extendedRcode());
41+
assertEquals(0, record.version());
42+
}
43+
44+
@Test
45+
public void testFlagsIsNotTruncatedToEightBits() {
46+
// The flags field is 16 bits wide, so bits 8..15 must survive as well.
47+
DnsOptPseudoRecord record = newRecord(4096, 0, 0, 0x7fff);
48+
assertEquals(0x7fff, record.flags());
49+
}
50+
51+
@Test
52+
public void testFlagsCanCarryBitsOtherThanDo() {
53+
// Bits below DO are assigned by the IANA EDNS Header Flags registry, so they must not be masked off.
54+
assertEquals(CO, newRecord(4096, 0, 0, CO).flags());
55+
assertEquals(DO | CO, newRecord(4096, 0, 0, DO | CO).flags());
56+
}
57+
58+
@Test
59+
public void testTtlLayoutIsExtendedRcodeVersionFlags() {
60+
// Asymmetric values so that swapping or widening any of the three fields is detectable.
61+
DnsOptPseudoRecord record = newRecord(4096, 0x12, 0x34, 0x8abc);
62+
assertEquals(0x12348abcL, record.timeToLive());
63+
assertEquals(0x12, record.extendedRcode());
64+
assertEquals(0x34, record.version());
65+
assertEquals(0x8abc, record.flags());
66+
}
67+
68+
@Test
69+
public void testAllFieldsAtMaximum() {
70+
DnsOptPseudoRecord record = newRecord(1232, 0xff, 0xff, 0xffff);
71+
assertEquals(0xff, record.extendedRcode());
72+
assertEquals(0xff, record.version());
73+
assertEquals(0xffff, record.flags());
74+
assertEquals(0xffffffffL, record.timeToLive());
75+
assertEquals(1232, record.dnsClass());
76+
}
77+
78+
@Test
79+
public void testFlagsDoNotLeakIntoExtendedRcodeOrVersion() {
80+
DnsOptPseudoRecord record = newRecord(4096, 0, 0, 0xffff);
81+
assertEquals(0, record.extendedRcode());
82+
assertEquals(0, record.version());
83+
}
84+
85+
@Test
86+
public void testExtendedRcodeAndVersionDoNotLeakIntoFlags() {
87+
DnsOptPseudoRecord record = newRecord(4096, 0xff, 0xff, 0);
88+
assertEquals(0, record.flags());
89+
}
90+
91+
@Test
92+
public void testOutOfRangeValuesAreTruncatedToTheirFieldWidth() {
93+
// 0x01 for the extended RCODE so a bit bleeding out of an over-wide VERSION is visible.
94+
DnsOptPseudoRecord record = newRecord(4096, 0x01, 0x2ff, 0x3ffff);
95+
assertEquals(0x01, record.extendedRcode());
96+
assertEquals(0xff, record.version());
97+
assertEquals(0xffff, record.flags());
98+
assertEquals(0x01ffffffL, record.timeToLive());
99+
100+
// A negative value must never produce a negative TTL, which AbstractDnsRecord rejects.
101+
DnsOptPseudoRecord negative = newRecord(4096, -1, -1, -1);
102+
assertEquals(0xffffffffL, negative.timeToLive());
103+
assertEquals(0xffff, negative.flags());
104+
}
105+
106+
@Test
107+
public void testPreExistingConstructorsKeepTtlUnchanged() {
108+
// The constructors that predate the flags parameter must produce exactly the TTL they did before.
109+
DnsOptPseudoRecord threeArg = new AbstractDnsOptPseudoRrRecord(4096, 0xab, 0xcd) { };
110+
assertEquals(0xabcd0000L, threeArg.timeToLive());
111+
assertEquals(0, threeArg.flags());
112+
113+
DnsOptPseudoRecord oneArg = new AbstractDnsOptPseudoRrRecord(4096) { };
114+
assertEquals(0L, oneArg.timeToLive());
115+
assertEquals(0, oneArg.flags());
116+
}
117+
118+
@Test
119+
public void testDoIsEncodedOnTheWire() throws Exception {
120+
assertEncoded(newRecord(4096, 0, 0, DO), 4096, 0, 0, DO);
121+
}
122+
123+
@Test
124+
public void testExtendedRcodeVersionAndFlagsAreEncodedOnTheWire() throws Exception {
125+
assertEncoded(newRecord(1232, 0x01, 0x02, DO | CO), 1232, 0x01, 0x02, DO | CO);
126+
}
127+
128+
private static void assertEncoded(DnsOptPseudoRecord record, int maxPayloadSize,
129+
int extendedRcode, int version, int flags) throws Exception {
130+
ByteBuf out = Unpooled.buffer();
131+
try {
132+
new DefaultDnsRecordEncoder().encodeRecord(record, out);
133+
134+
// See https://tools.ietf.org/html/rfc6891#section-6.1.2
135+
assertEquals(0, out.readByte()); // NAME must be the root domain.
136+
assertEquals(DnsRecordType.OPT.intValue(), out.readUnsignedShort()); // TYPE
137+
assertEquals(maxPayloadSize, out.readUnsignedShort()); // CLASS holds the UDP payload size.
138+
assertEquals(extendedRcode, out.readUnsignedByte()); // TTL: EXTENDED-RCODE
139+
assertEquals(version, out.readUnsignedByte()); // TTL: VERSION
140+
assertEquals(flags, out.readUnsignedShort()); // TTL: DO + Z
141+
assertEquals(0, out.readUnsignedShort()); // RDLENGTH
142+
assertEquals(0, out.readableBytes());
143+
} finally {
144+
out.release();
145+
}
146+
}
147+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.