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
20 import java.net.InetAddress;
21 import java.util.Arrays;
22
23 /**
24 * Default {@link DnsOptEcsRecord} implementation.
25 */
26 public final class DefaultDnsOptEcsRecord extends AbstractDnsOptPseudoRrRecord implements DnsOptEcsRecord {
27 private final int srcPrefixLength;
28 private final byte[] address;
29
30 /**
31 * Creates a new instance.
32 *
33 * @param maxPayloadSize the suggested max payload size in bytes
34 * @param extendedRcode the extended rcode
35 * @param version the version
36 * @param srcPrefixLength the prefix length
37 * @param address the bytes of the {@link InetAddress} to use
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 * Creates a new instance.
48 *
49 * @param maxPayloadSize the suggested max payload size in bytes
50 * @param srcPrefixLength the prefix length
51 * @param address the bytes of the {@link InetAddress} to use
52 */
53 public DefaultDnsOptEcsRecord(int maxPayloadSize, int srcPrefixLength, byte[] address) {
54 this(maxPayloadSize, 0, 0, srcPrefixLength, address);
55 }
56
57 /**
58 * Creates a new instance.
59 *
60 * @param maxPayloadSize the suggested max payload size in bytes
61 * @param protocolFamily the {@link InternetProtocolFamily} to use. This should be the same as the one used to
62 * send the query.
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 }