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.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   * Default {@link DnsOptEcsRecord} implementation.
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       * 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       */
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 }