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