001: /*
002: * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.jndi.dns;
027:
028: import java.util.Vector;
029: import javax.naming.*;
030:
031: /**
032: * The ResourceRecords class represents the resource records in the
033: * four sections of a DNS message.
034: *
035: * The additional records section is currently ignored.
036: *
037: * @author Scott Seligman
038: * @version 1.12 07/05/05
039: */
040:
041: class ResourceRecords {
042:
043: // Four sections: question, answer, authority, additional.
044: // The question section is treated as being made up of (shortened)
045: // resource records, although this isn't technically how it's defined.
046: Vector question = new Vector();
047: Vector answer = new Vector();
048: Vector authority = new Vector();
049: Vector additional = new Vector();
050:
051: /*
052: * True if these resource records are from a zone transfer. In
053: * that case only answer records are read (as per
054: * draft-ietf-dnsext-axfr-clarify-02.txt). Also, the rdata of
055: * those answer records is not decoded (for efficiency) except
056: * for SOA records.
057: */
058: boolean zoneXfer;
059:
060: /*
061: * Returns a representation of the resource records in a DNS message.
062: * Does not modify or store a reference to the msg array.
063: */
064: ResourceRecords(byte[] msg, int msgLen, Header hdr, boolean zoneXfer)
065: throws NamingException {
066: if (zoneXfer) {
067: answer.ensureCapacity(8192); // an arbitrary "large" number
068: }
069: this .zoneXfer = zoneXfer;
070: add(msg, msgLen, hdr);
071: }
072:
073: /*
074: * Returns the type field of the first answer record, or -1 if
075: * there are no answer records.
076: */
077: int getFirstAnsType() {
078: if (answer.size() == 0) {
079: return -1;
080: }
081: return ((ResourceRecord) answer.firstElement()).getType();
082: }
083:
084: /*
085: * Returns the type field of the last answer record, or -1 if
086: * there are no answer records.
087: */
088: int getLastAnsType() {
089: if (answer.size() == 0) {
090: return -1;
091: }
092: return ((ResourceRecord) answer.lastElement()).getType();
093: }
094:
095: /*
096: * Decodes the resource records in a DNS message and adds
097: * them to this object.
098: * Does not modify or store a reference to the msg array.
099: */
100: void add(byte[] msg, int msgLen, Header hdr) throws NamingException {
101:
102: ResourceRecord rr;
103: int pos = Header.HEADER_SIZE; // current offset into msg
104:
105: try {
106: for (int i = 0; i < hdr.numQuestions; i++) {
107: rr = new ResourceRecord(msg, msgLen, pos, true, false);
108: if (!zoneXfer) {
109: question.addElement(rr);
110: }
111: pos += rr.size();
112: }
113:
114: for (int i = 0; i < hdr.numAnswers; i++) {
115: rr = new ResourceRecord(msg, msgLen, pos, false,
116: !zoneXfer);
117: answer.addElement(rr);
118: pos += rr.size();
119: }
120:
121: if (zoneXfer) {
122: return;
123: }
124:
125: for (int i = 0; i < hdr.numAuthorities; i++) {
126: rr = new ResourceRecord(msg, msgLen, pos, false, true);
127: authority.addElement(rr);
128: pos += rr.size();
129: }
130:
131: // The additional records section is currently ignored.
132:
133: } catch (IndexOutOfBoundsException e) {
134: throw new CommunicationException(
135: "DNS error: corrupted message");
136: }
137: }
138: }
|