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.channel.socket.SocketProtocolFamily;
20 import io.netty.util.NetUtil;
21
22 import java.net.InetAddress;
23 import java.util.Arrays;
24
25
26
27
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
67 @Deprecated
68 public DefaultDnsOptEcsRecord(int maxPayloadSize, InternetProtocolFamily protocolFamily) {
69 this(maxPayloadSize, 0, 0, 0, protocolFamily.localhost().getAddress());
70 }
71
72
73
74
75
76
77
78
79 public DefaultDnsOptEcsRecord(int maxPayloadSize, SocketProtocolFamily socketProtocolFamily) {
80 this(maxPayloadSize, 0, 0, 0, localAddress(socketProtocolFamily));
81 }
82
83 private static byte[] localAddress(SocketProtocolFamily family) {
84 switch (family) {
85 case INET:
86 return NetUtil.LOCALHOST4.getAddress();
87 case INET6:
88 return NetUtil.LOCALHOST6.getAddress();
89 default:
90 return null;
91 }
92 }
93
94 private static byte[] verifyAddress(byte[] bytes) {
95 if (bytes != null && bytes.length == 4 || bytes.length == 16) {
96 return bytes;
97 }
98 throw new IllegalArgumentException("bytes.length must either 4 or 16");
99 }
100
101 @Override
102 public int sourcePrefixLength() {
103 return srcPrefixLength;
104 }
105
106 @Override
107 public int scopePrefixLength() {
108 return 0;
109 }
110
111 @Override
112 public byte[] address() {
113 return address.clone();
114 }
115
116 @Override
117 public String toString() {
118 StringBuilder sb = toStringBuilder();
119 sb.setLength(sb.length() - 1);
120 return sb.append(" address:")
121 .append(Arrays.toString(address))
122 .append(" sourcePrefixLength:")
123 .append(sourcePrefixLength())
124 .append(" scopePrefixLength:")
125 .append(scopePrefixLength())
126 .append(')').toString();
127 }
128 }