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    *   http://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 DnsQuery} implementation for UDP/IP.
26   */
27  @UnstableApi
28  public class DatagramDnsQuery extends DefaultDnsQuery
29          implements AddressedEnvelope<DatagramDnsQuery, 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}.
36       *
37       * @param sender the address of the sender
38       * @param recipient the address of the recipient
39       * @param id the {@code ID} of the DNS query
40       */
41      public DatagramDnsQuery(
42              InetSocketAddress sender, InetSocketAddress recipient, int id) {
43          this(sender, recipient, id, DnsOpCode.QUERY);
44      }
45  
46      /**
47       * Creates a new instance.
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 query
52       * @param opCode the {@code opCode} of the DNS query
53       */
54      public DatagramDnsQuery(
55              InetSocketAddress sender, InetSocketAddress recipient, int id, DnsOpCode opCode) {
56          super(id, opCode);
57  
58          if (recipient == null && sender == null) {
59              throw new NullPointerException("recipient and sender");
60          }
61  
62          this.sender = sender;
63          this.recipient = recipient;
64      }
65  
66      @Override
67      public DatagramDnsQuery content() {
68          return this;
69      }
70  
71      @Override
72      public InetSocketAddress sender() {
73          return sender;
74      }
75  
76      @Override
77      public InetSocketAddress recipient() {
78          return recipient;
79      }
80  
81      @Override
82      public DatagramDnsQuery setId(int id) {
83          return (DatagramDnsQuery) super.setId(id);
84      }
85  
86      @Override
87      public DatagramDnsQuery setOpCode(DnsOpCode opCode) {
88          return (DatagramDnsQuery) super.setOpCode(opCode);
89      }
90  
91      @Override
92      public DatagramDnsQuery setRecursionDesired(boolean recursionDesired) {
93          return (DatagramDnsQuery) super.setRecursionDesired(recursionDesired);
94      }
95  
96      @Override
97      public DatagramDnsQuery setZ(int z) {
98          return (DatagramDnsQuery) super.setZ(z);
99      }
100 
101     @Override
102     public DatagramDnsQuery setRecord(DnsSection section, DnsRecord record) {
103         return (DatagramDnsQuery) super.setRecord(section, record);
104     }
105 
106     @Override
107     public DatagramDnsQuery addRecord(DnsSection section, DnsRecord record) {
108         return (DatagramDnsQuery) super.addRecord(section, record);
109     }
110 
111     @Override
112     public DatagramDnsQuery addRecord(DnsSection section, int index, DnsRecord record) {
113         return (DatagramDnsQuery) super.addRecord(section, index, record);
114     }
115 
116     @Override
117     public DatagramDnsQuery clear(DnsSection section) {
118         return (DatagramDnsQuery) super.clear(section);
119     }
120 
121     @Override
122     public DatagramDnsQuery clear() {
123         return (DatagramDnsQuery) super.clear();
124     }
125 
126     @Override
127     public DatagramDnsQuery touch() {
128         return (DatagramDnsQuery) super.touch();
129     }
130 
131     @Override
132     public DatagramDnsQuery touch(Object hint) {
133         return (DatagramDnsQuery) super.touch(hint);
134     }
135 
136     @Override
137     public DatagramDnsQuery retain() {
138         return (DatagramDnsQuery) super.retain();
139     }
140 
141     @Override
142     public DatagramDnsQuery retain(int increment) {
143         return (DatagramDnsQuery) super.retain(increment);
144     }
145 
146     @Override
147     public boolean equals(Object obj) {
148         if (this == obj) {
149             return true;
150         }
151 
152         if (!super.equals(obj)) {
153             return false;
154         }
155 
156         if (!(obj instanceof AddressedEnvelope)) {
157             return false;
158         }
159 
160         @SuppressWarnings("unchecked")
161         final AddressedEnvelope<?, SocketAddress> that = (AddressedEnvelope<?, SocketAddress>) obj;
162         if (sender() == null) {
163             if (that.sender() != null) {
164                 return false;
165             }
166         } else if (!sender().equals(that.sender())) {
167             return false;
168         }
169 
170         if (recipient() == null) {
171             if (that.recipient() != null) {
172                 return false;
173             }
174         } else if (!recipient().equals(that.recipient())) {
175             return false;
176         }
177 
178         return true;
179     }
180 
181     @Override
182     public int hashCode() {
183         int hashCode = super.hashCode();
184         if (sender() != null) {
185             hashCode = hashCode * 31 + sender().hashCode();
186         }
187         if (recipient() != null) {
188             hashCode = hashCode * 31 + recipient().hashCode();
189         }
190         return hashCode;
191     }
192 }