1 /*
2 * Copyright 2016 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.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 * Default {@link DnsOptEcsRecord} implementation.
27 */
28 public final class DefaultDnsOptEcsRecord extends AbstractDnsOptPseudoRrRecord implements DnsOptEcsRecord {
29 private final int srcPrefixLength;
30 private final byte[] address;
31
32 /**
33 * Creates a new instance.
34 *
35 * @param maxPayloadSize the suggested max payload size in bytes
36 * @param extendedRcode the extended rcode
37 * @param version the version
38 * @param srcPrefixLength the prefix length
39 * @param address the bytes of the {@link InetAddress} to use
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 * Creates a new instance.
50 *
51 * @param maxPayloadSize the suggested max payload size in bytes
52 * @param srcPrefixLength the prefix length
53 * @param address the bytes of the {@link InetAddress} to use
54 */
55 public DefaultDnsOptEcsRecord(int maxPayloadSize, int srcPrefixLength, byte[] address) {
56 this(maxPayloadSize, 0, 0, srcPrefixLength, address);
57 }
58
59 /**
60 * Creates a new instance.
61 *
62 * @param maxPayloadSize the suggested max payload size in bytes
63 * @param protocolFamily the {@link InternetProtocolFamily} to use. This should be the same as the one used to
64 * send the query.
65 * @deprecated use {@link DefaultDnsOptEcsRecord#DefaultDnsOptEcsRecord(int, SocketProtocolFamily)}
66 */
67 @Deprecated
68 public DefaultDnsOptEcsRecord(int maxPayloadSize, InternetProtocolFamily protocolFamily) {
69 this(maxPayloadSize, 0, 0, 0, protocolFamily.localhost().getAddress());
70 }
71
72 /**
73 * Creates a new instance.
74 *
75 * @param maxPayloadSize the suggested max payload size in bytes
76 * @param socketProtocolFamily the {@link SocketProtocolFamily} to use. This should be the same as the one used to
77 * send the query.
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 }