1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.codec.dns;
17
18 import io.netty5.util.internal.UnstableApi;
19
20 import java.net.InetAddress;
21 import java.net.ProtocolFamily;
22 import java.util.Arrays;
23
24 import static io.netty5.util.NetUtil.localHost;
25
26
27
28
29 @UnstableApi
30 public final class DefaultDnsOptEcsRecord extends AbstractDnsOptPseudoRrRecord implements DnsOptEcsRecord {
31 private final int srcPrefixLength;
32 private final byte[] address;
33
34
35
36
37
38
39
40
41
42
43 public DefaultDnsOptEcsRecord(int maxPayloadSize, int extendedRcode, int version,
44 int srcPrefixLength, byte[] address) {
45 super(maxPayloadSize, extendedRcode, version);
46 this.srcPrefixLength = srcPrefixLength;
47 this.address = verifyAddress(address).clone();
48 }
49
50
51
52
53
54
55
56
57 public DefaultDnsOptEcsRecord(int maxPayloadSize, int srcPrefixLength, byte[] address) {
58 this(maxPayloadSize, 0, 0, srcPrefixLength, address);
59 }
60
61
62
63
64
65
66
67
68 public DefaultDnsOptEcsRecord(int maxPayloadSize, ProtocolFamily protocolFamily) {
69 this(maxPayloadSize, 0, 0, 0, localHost(protocolFamily).getAddress());
70 }
71
72 private static byte[] verifyAddress(byte[] bytes) {
73 if (bytes.length == 4 || bytes.length == 16) {
74 return bytes;
75 }
76 throw new IllegalArgumentException("bytes.length must either 4 or 16");
77 }
78
79 @Override
80 public int sourcePrefixLength() {
81 return srcPrefixLength;
82 }
83
84 @Override
85 public int scopePrefixLength() {
86 return 0;
87 }
88
89 @Override
90 public byte[] address() {
91 return address.clone();
92 }
93
94 @Override
95 public DefaultDnsOptEcsRecord copy() {
96 return this;
97 }
98
99 @Override
100 public String toString() {
101 StringBuilder sb = toStringBuilder();
102 sb.setLength(sb.length() - 1);
103 return sb.append(" address:")
104 .append(Arrays.toString(address))
105 .append(" sourcePrefixLength:")
106 .append(sourcePrefixLength())
107 .append(" scopePrefixLength:")
108 .append(scopePrefixLength())
109 .append(')').toString();
110 }
111 }