001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    
021    package org.apache.directory.server.dns.messages;
022    
023    
024    import java.util.List;
025    
026    import org.apache.commons.lang.builder.EqualsBuilder;
027    import org.apache.commons.lang.builder.HashCodeBuilder;
028    import org.apache.commons.lang.builder.ToStringBuilder;
029    
030    
031    /**
032     * All communications inside of the domain protocol are carried in a single
033     * format called a message.  The top level format of message is divided
034     * into 5 sections (some of which are empty in certain cases) shown below:
035     *
036     *     +---------------------+
037     *     |        Header       |
038     *     +---------------------+
039     *     |       Question      | the question for the name server
040     *     +---------------------+
041     *     |        Answer       | ResourceRecords answering the question
042     *     +---------------------+
043     *     |      Authority      | ResourceRecords pointing toward an authority
044     *     +---------------------+
045     *     |      Additional     | ResourceRecords holding additional information
046     *     +---------------------+
047     * 
048     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
049     * @version $Rev: 664295 $, $Date: 2008-06-07 09:48:16 +0200 (Sat, 07 Jun 2008) $
050     */
051    public class DnsMessage
052    {
053        /**
054         * The header section is always present.  The header includes fields that
055         * specify which of the remaining sections are present, and also specify
056         * whether the message is a query or a response, a standard query or some
057         * other opcode, etc.
058         */
059        private int transactionId;
060        private MessageType messageType;
061        private OpCode opCode;
062        private boolean authoritativeAnswer;
063        private boolean truncated;
064        private boolean recursionDesired;
065        private boolean recursionAvailable;
066        private boolean reserved;
067        private boolean acceptNonAuthenticatedData;
068    
069        private ResponseCode responseCode;
070    
071        private List<QuestionRecord> questionRecords;
072        private List<ResourceRecord> answerRecords;
073        private List<ResourceRecord> authorityRecords;
074        private List<ResourceRecord> additionalRecords;
075    
076    
077        /**
078         * Creates a new instance of DnsMessage.
079         *
080         * @param transactionId
081         * @param messageType
082         * @param opCode
083         * @param authoritativeAnswer
084         * @param truncated
085         * @param recursionDesired
086         * @param recursionAvailable
087         * @param reserved
088         * @param acceptNonAuthenticatedData
089         * @param responseCode
090         * @param question
091         * @param answer
092         * @param authority
093         * @param additional
094         */
095        public DnsMessage( int transactionId, MessageType messageType, OpCode opCode, boolean authoritativeAnswer,
096            boolean truncated, boolean recursionDesired, boolean recursionAvailable, boolean reserved,
097            boolean acceptNonAuthenticatedData, ResponseCode responseCode, List<QuestionRecord> question,
098            List<ResourceRecord> answer, List<ResourceRecord> authority, List<ResourceRecord> additional )
099        {
100            this.transactionId = transactionId;
101            this.messageType = messageType;
102            this.opCode = opCode;
103            this.authoritativeAnswer = authoritativeAnswer;
104            this.truncated = truncated;
105            this.recursionDesired = recursionDesired;
106            this.recursionAvailable = recursionAvailable;
107            this.reserved = reserved;
108            this.acceptNonAuthenticatedData = acceptNonAuthenticatedData;
109            this.responseCode = responseCode;
110            this.questionRecords = question;
111            this.answerRecords = answer;
112            this.authorityRecords = authority;
113            this.additionalRecords = additional;
114        }
115    
116    
117        /**
118         * @return Returns the acceptNonAuthenticatedData.
119         */
120        public boolean isAcceptNonAuthenticatedData()
121        {
122            return acceptNonAuthenticatedData;
123        }
124    
125    
126        /**
127         * @return Returns the additional.
128         */
129        public List<ResourceRecord> getAdditionalRecords()
130        {
131            return additionalRecords;
132        }
133    
134    
135        /**
136         * @return Returns the answers.
137         */
138        public List<ResourceRecord> getAnswerRecords()
139        {
140            return answerRecords;
141        }
142    
143    
144        /**
145         * @return Returns the authoritativeAnswer.
146         */
147        public boolean isAuthoritativeAnswer()
148        {
149            return authoritativeAnswer;
150        }
151    
152    
153        /**
154         * @return Returns the authority.
155         */
156        public List<ResourceRecord> getAuthorityRecords()
157        {
158            return authorityRecords;
159        }
160    
161    
162        /**
163         * @return Returns the messageType.
164         */
165        public MessageType getMessageType()
166        {
167            return messageType;
168        }
169    
170    
171        /**
172         * @return Returns the opCode.
173         */
174        public OpCode getOpCode()
175        {
176            return opCode;
177        }
178    
179    
180        /**
181         * @return Returns the question.
182         */
183        public List<QuestionRecord> getQuestionRecords()
184        {
185            return questionRecords;
186        }
187    
188    
189        /**
190         * @return Returns the recursionAvailable.
191         */
192        public boolean isRecursionAvailable()
193        {
194            return recursionAvailable;
195        }
196    
197    
198        /**
199         * @return Returns the recursionDesired.
200         */
201        public boolean isRecursionDesired()
202        {
203            return recursionDesired;
204        }
205    
206    
207        /**
208         * @return Returns the reserved.
209         */
210        public boolean isReserved()
211        {
212            return reserved;
213        }
214    
215    
216        /**
217         * @return Returns the responseCode.
218         */
219        public ResponseCode getResponseCode()
220        {
221            return responseCode;
222        }
223    
224    
225        /**
226         * @return Returns the transactionId.
227         */
228        public int getTransactionId()
229        {
230            return transactionId;
231        }
232    
233    
234        /**
235         * @return Returns the truncated.
236         */
237        public boolean isTruncated()
238        {
239            return truncated;
240        }
241    
242    
243        /**
244         * @see java.lang.Object#equals(Object)
245         */
246        public boolean equals( Object object )
247        {
248            if ( object == this )
249            {
250                return true;
251            }
252            if ( !( object instanceof DnsMessage ) )
253            {
254                return false;
255            }
256            DnsMessage rhs = ( DnsMessage ) object;
257            return new EqualsBuilder().append( this.transactionId, rhs.transactionId ).append( this.answerRecords,
258                rhs.answerRecords ).append( this.opCode, rhs.opCode ).append( this.recursionAvailable,
259                rhs.recursionAvailable ).append( this.messageType, rhs.messageType ).append( this.additionalRecords,
260                rhs.additionalRecords ).append( this.truncated, rhs.truncated ).append( this.recursionDesired,
261                rhs.recursionDesired ).append( this.responseCode, rhs.responseCode ).append( this.authorityRecords,
262                rhs.authorityRecords ).append( this.authoritativeAnswer, rhs.authoritativeAnswer ).append( this.reserved,
263                rhs.reserved ).append( this.acceptNonAuthenticatedData, rhs.acceptNonAuthenticatedData ).append(
264                this.questionRecords, rhs.questionRecords ).isEquals();
265        }
266    
267    
268        /**
269         * @see java.lang.Object#hashCode()
270         * @return the instance's hash code 
271         */
272        public int hashCode()
273        {
274            return new HashCodeBuilder( -1805208585, -276770303 ).append( this.transactionId ).append( this.answerRecords )
275                .append( this.opCode ).append( this.recursionAvailable ).append( this.messageType ).append(
276                    this.additionalRecords ).append( this.truncated ).append( this.recursionDesired ).append(
277                    this.responseCode ).append( this.authorityRecords ).append( this.authoritativeAnswer ).append(
278                    this.reserved ).append( this.acceptNonAuthenticatedData ).append( this.questionRecords ).toHashCode();
279        }
280    
281    
282        /**
283         * @see java.lang.Object#toString()
284         */
285        public String toString()
286        {
287            return new ToStringBuilder( this ).appendSuper( super.toString() ).append( "transactionId", this.transactionId )
288                .append( "opCode", this.opCode ).append( "truncated", this.truncated ).append( "messageType",
289                    this.messageType ).append( "recursionDesired", this.recursionDesired ).append( "additionalRecords",
290                    this.additionalRecords ).append( "responseCode", this.responseCode ).append( "authorityRecords",
291                    this.authorityRecords ).append( "acceptNonAuthenticatedData", this.acceptNonAuthenticatedData ).append(
292                    "recursionAvailable", this.recursionAvailable ).append( "answerRecords", this.answerRecords ).append(
293                    "questionRecords", this.questionRecords ).append( "authoritativeAnswer", this.authoritativeAnswer )
294                .append( "reserved", this.reserved ).toString();
295        }
296    }