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