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