View Javadoc
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.netty5.handler.codec.dns;
17  
18  import io.netty5.util.internal.UnstableApi;
19  
20  import java.net.InetAddress;
21  import java.net.ProtocolFamily;
22  import java.util.Arrays;
23  
24  import static io.netty5.util.NetUtil.localHost;
25  
26  /**
27   * Default {@link DnsOptEcsRecord} implementation.
28   */
29  @UnstableApi
30  public final class DefaultDnsOptEcsRecord extends AbstractDnsOptPseudoRrRecord implements DnsOptEcsRecord {
31      private final int srcPrefixLength;
32      private final byte[] address;
33  
34      /**
35       * Creates a new instance.
36       *
37       * @param maxPayloadSize the suggested max payload size in bytes
38       * @param extendedRcode the extended rcode
39       * @param version the version
40       * @param srcPrefixLength the prefix length
41       * @param address the bytes of the {@link InetAddress} to use
42       */
43      public DefaultDnsOptEcsRecord(int maxPayloadSize, int extendedRcode, int version,
44                                    int srcPrefixLength, byte[] address) {
45          super(maxPayloadSize, extendedRcode, version);
46          this.srcPrefixLength = srcPrefixLength;
47          this.address = verifyAddress(address).clone();
48      }
49  
50      /**
51       * Creates a new instance.
52       *
53       * @param maxPayloadSize the suggested max payload size in bytes
54       * @param srcPrefixLength the prefix length
55       * @param address the bytes of the {@link InetAddress} to use
56       */
57      public DefaultDnsOptEcsRecord(int maxPayloadSize, int srcPrefixLength, byte[] address) {
58          this(maxPayloadSize, 0, 0, srcPrefixLength, address);
59      }
60  
61      /**
62       * Creates a new instance.
63       *
64       * @param maxPayloadSize the suggested max payload size in bytes
65       * @param protocolFamily the {@link ProtocolFamily} to use. This should be the same as the one used to
66       *                       send the query.
67       */
68      public DefaultDnsOptEcsRecord(int maxPayloadSize, ProtocolFamily protocolFamily) {
69          this(maxPayloadSize, 0, 0, 0, localHost(protocolFamily).getAddress());
70      }
71  
72      private static byte[] verifyAddress(byte[] bytes) {
73          if (bytes.length == 4 || bytes.length == 16) {
74              return bytes;
75          }
76          throw new IllegalArgumentException("bytes.length must either 4 or 16");
77      }
78  
79      @Override
80      public int sourcePrefixLength() {
81          return srcPrefixLength;
82      }
83  
84      @Override
85      public int scopePrefixLength() {
86          return 0;
87      }
88  
89      @Override
90      public byte[] address() {
91          return address.clone();
92      }
93  
94      @Override
95      public DefaultDnsOptEcsRecord copy() {
96          return this;
97      }
98  
99      @Override
100     public String toString() {
101         StringBuilder sb = toStringBuilder();
102         sb.setLength(sb.length() - 1);
103         return sb.append(" address:")
104           .append(Arrays.toString(address))
105           .append(" sourcePrefixLength:")
106           .append(sourcePrefixLength())
107           .append(" scopePrefixLength:")
108           .append(scopePrefixLength())
109           .append(')').toString();
110     }
111 }