View Javadoc
1   /*
2    * Copyright 2015 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.AddressedEnvelope;
19  import io.netty.util.internal.UnstableApi;
20  
21  import java.net.InetSocketAddress;
22  import java.net.SocketAddress;
23  
24  /**
25   * A {@link DnsResponse} implementation for UDP/IP.
26   */
27  @UnstableApi
28  public class DatagramDnsResponse extends DefaultDnsResponse
29          implements AddressedEnvelope<DatagramDnsResponse, InetSocketAddress> {
30  
31      private final InetSocketAddress sender;
32      private final InetSocketAddress recipient;
33  
34      /**
35       * Creates a new instance with the {@link DnsOpCode#QUERY} {@code opCode} and
36       * the {@link DnsResponseCode#NOERROR} {@code RCODE}.
37       *
38       * @param sender the address of the sender
39       * @param recipient the address of the recipient
40       * @param id the {@code ID} of the DNS response
41       */
42      public DatagramDnsResponse(InetSocketAddress sender, InetSocketAddress recipient, int id) {
43          this(sender, recipient, id, DnsOpCode.QUERY, DnsResponseCode.NOERROR);
44      }
45  
46      /**
47       * Creates a new instance with the {@link DnsResponseCode#NOERROR} responseCode.
48       *
49       * @param sender the address of the sender
50       * @param recipient the address of the recipient
51       * @param id the {@code ID} of the DNS response
52       * @param opCode the {@code opCode} of the DNS response
53       */
54      public DatagramDnsResponse(InetSocketAddress sender, InetSocketAddress recipient, int id, DnsOpCode opCode) {
55          this(sender, recipient, id, opCode, DnsResponseCode.NOERROR);
56      }
57  
58      /**
59       * Creates a new instance.
60       *
61       * @param sender the address of the sender
62       * @param recipient the address of the recipient
63       * @param id the {@code ID} of the DNS response
64       * @param opCode the {@code opCode} of the DNS response
65       * @param responseCode the {@code RCODE} of the DNS response
66       */
67      public DatagramDnsResponse(
68              InetSocketAddress sender, InetSocketAddress recipient,
69              int id, DnsOpCode opCode, DnsResponseCode responseCode) {
70          super(id, opCode, responseCode);
71  
72          if (recipient == null && sender == null) {
73              throw new NullPointerException("recipient and sender");
74          }
75  
76          this.sender = sender;
77          this.recipient = recipient;
78      }
79  
80      @Override
81      public DatagramDnsResponse content() {
82          return this;
83      }
84  
85      @Override
86      public InetSocketAddress sender() {
87          return sender;
88      }
89  
90      @Override
91      public InetSocketAddress recipient() {
92          return recipient;
93      }
94  
95      @Override
96      public DatagramDnsResponse setAuthoritativeAnswer(boolean authoritativeAnswer) {
97          return (DatagramDnsResponse) super.setAuthoritativeAnswer(authoritativeAnswer);
98      }
99  
100     @Override
101     public DatagramDnsResponse setTruncated(boolean truncated) {
102         return (DatagramDnsResponse) super.setTruncated(truncated);
103     }
104 
105     @Override
106     public DatagramDnsResponse setRecursionAvailable(boolean recursionAvailable) {
107         return (DatagramDnsResponse) super.setRecursionAvailable(recursionAvailable);
108     }
109 
110     @Override
111     public DatagramDnsResponse setCode(DnsResponseCode code) {
112         return (DatagramDnsResponse) super.setCode(code);
113     }
114 
115     @Override
116     public DatagramDnsResponse setId(int id) {
117         return (DatagramDnsResponse) super.setId(id);
118     }
119 
120     @Override
121     public DatagramDnsResponse setOpCode(DnsOpCode opCode) {
122         return (DatagramDnsResponse) super.setOpCode(opCode);
123     }
124 
125     @Override
126     public DatagramDnsResponse setRecursionDesired(boolean recursionDesired) {
127         return (DatagramDnsResponse) super.setRecursionDesired(recursionDesired);
128     }
129 
130     @Override
131     public DatagramDnsResponse setZ(int z) {
132         return (DatagramDnsResponse) super.setZ(z);
133     }
134 
135     @Override
136     public DatagramDnsResponse setRecord(DnsSection section, DnsRecord record) {
137         return (DatagramDnsResponse) super.setRecord(section, record);
138     }
139 
140     @Override
141     public DatagramDnsResponse addRecord(DnsSection section, DnsRecord record) {
142         return (DatagramDnsResponse) super.addRecord(section, record);
143     }
144 
145     @Override
146     public DatagramDnsResponse addRecord(DnsSection section, int index, DnsRecord record) {
147         return (DatagramDnsResponse) super.addRecord(section, index, record);
148     }
149 
150     @Override
151     public DatagramDnsResponse clear(DnsSection section) {
152         return (DatagramDnsResponse) super.clear(section);
153     }
154 
155     @Override
156     public DatagramDnsResponse clear() {
157         return (DatagramDnsResponse) super.clear();
158     }
159 
160     @Override
161     public DatagramDnsResponse touch() {
162         return (DatagramDnsResponse) super.touch();
163     }
164 
165     @Override
166     public DatagramDnsResponse touch(Object hint) {
167         return (DatagramDnsResponse) super.touch(hint);
168     }
169 
170     @Override
171     public DatagramDnsResponse retain() {
172         return (DatagramDnsResponse) super.retain();
173     }
174 
175     @Override
176     public DatagramDnsResponse retain(int increment) {
177         return (DatagramDnsResponse) super.retain(increment);
178     }
179 
180     @Override
181     public boolean equals(Object obj) {
182         if (this == obj) {
183             return true;
184         }
185 
186         if (!super.equals(obj)) {
187             return false;
188         }
189 
190         if (!(obj instanceof AddressedEnvelope)) {
191             return false;
192         }
193 
194         @SuppressWarnings("unchecked")
195         final AddressedEnvelope<?, SocketAddress> that = (AddressedEnvelope<?, SocketAddress>) obj;
196         if (sender() == null) {
197             if (that.sender() != null) {
198                 return false;
199             }
200         } else if (!sender().equals(that.sender())) {
201             return false;
202         }
203 
204         if (recipient() == null) {
205             return that.recipient() == null;
206         } else {
207             return recipient().equals(that.recipient());
208         }
209     }
210 
211     @Override
212     public int hashCode() {
213         int hashCode = super.hashCode();
214         if (sender() != null) {
215             hashCode = hashCode * 31 + sender().hashCode();
216         }
217         if (recipient() != null) {
218             hashCode = hashCode * 31 + recipient().hashCode();
219         }
220         return hashCode;
221     }
222 }