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