DNSResourceRecord.java

back to DNS queries in Java

001 // 
002 // DNSResourceRecord.java
003 // 
004 // Wrapper class for the Internet DNS server Resource Record structure.
005 // 
006 // Copyright 1997  W. Scott Means (http://smeans.com)
007 //
008 // Licensed under the Apache License, Version 2.0 (the "License");
009 // you may not use this file except in compliance with the License.
010 // You may obtain a copy of the License at
011 //
012 //     http://www.apache.org/licenses/LICENSE-2.0
013 //
014 // Unless required by applicable law or agreed to in writing, software
015 // distributed under the License is distributed on an "AS IS" BASIS,
016 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 //  See the License for the specific language governing permissions and
018 // limitations under the License.
019 //
020 
021 import java.io.*;
022 import java.util.Date;
023 
024 public class DNSResourceRecord 
025 {
026   public String m_strDomainName;
027   public int m_iType;
028   public int m_iClass;
029   public Date m_dtExpire = new Date();
030   public int m_cbData;
031   public byte [] m_abData = null;
032 
033   // these fields are interpreted differently depending on m_iType
034   // (can you say union?)
035   public long m_lData;
036   public long m_lData1;
037   public long m_lData2;
038   public long m_lData3;
039   public long m_lData4;
040   public long m_lData5;
041 
042   public String m_strData;
043   public String m_strData1;
044 
045   public boolean readRecord(DataInputStream dis, byte [] abData) {
046     try {
047       m_strDomainName = DNSQuery.ReadLabelList(dis, abData);
048       m_iType = dis.readShort();
049       m_iClass = dis.readShort();
050       
051       // time from DNS is in seconds, need milliseconds
052       long lSecondsToLive = dis.readInt();
053       
054       Date dtNow = new Date();
055 
056       m_dtExpire.setTime(dtNow.getTime() (lSecondsToLive * 1000L));
057 
058       m_cbData = dis.readShort();
059 
060       m_abData = null;
061 
062       switch (m_iType) {
063       case DNSQuery.TYPE_A: {
064         m_lData = dis.readInt();
065       break;
066 
067       case DNSQuery.TYPE_MX: {
068         m_lData = dis.readShort();
069         m_strData = DNSQuery.ReadLabelList(dis, abData);
070       break;
071 
072       case DNSQuery.TYPE_NS: 
073       case DNSQuery.TYPE_MD: 
074       case DNSQuery.TYPE_MF: 
075       case DNSQuery.TYPE_CNAME: 
076       case DNSQuery.TYPE_MB: 
077       case DNSQuery.TYPE_MG: 
078       case DNSQuery.TYPE_MR: 
079       case DNSQuery.TYPE_PTR: {
080         m_strData = DNSQuery.ReadLabelList(dis, abData);
081       break;
082 
083       case DNSQuery.TYPE_SOA: {
084         // !!!LATER!!! I had a real problem getting this code to work.
085         // I think the spec I had was old RFC 883
086         m_strData = DNSQuery.ReadLabelList(dis, abData);
087         m_strData1 = DNSQuery.ReadLabelList(dis, abData);
088 
089         // SERIAL
090         m_lData = dis.readUnsignedShort();
091         // REFRESH
092         m_lData1 = dis.readInt() 0xffffffff;
093         // RETRY
094         m_lData2 = dis.readInt() 0xffffffff;
095         // EXPIRE
096         m_lData3 = dis.readInt() 0xffffffff;
097         // MINIMUM
098         m_lData4 = dis.readUnsignedShort();
099         // UNKNOWN
100         m_lData5 = dis.readInt() 0xffffffff;
101       break;
102 
103       case DNSQuery.TYPE_MINFO:
104       case DNSQuery.TYPE_HINFO: {
105         m_strData = DNSQuery.ReadLabelList(dis, abData);
106         m_strData1 = DNSQuery.ReadLabelList(dis, abData);
107       break;
108 
109       default{
110         m_abData = new byte[m_cbData];
111         dis.read(m_abData);
112       break;
113       }
114     catch (IOException ioe) {
115       System.err.println("exception: " + ioe.getMessage());
116     }
117 
118     return true;
119   }
120 
121   public String getMXServer() {
122     if (m_iType != DNSQuery.TYPE_MX) {
123       return new String();
124     }
125 
126     return new String(m_strData);
127   }
128 
129   public int getMXPref() {
130     if (m_iType != DNSQuery.TYPE_MX) {
131       return -1;
132     }
133 
134     return (int)m_lData;
135   }
136 
137   public void dumpRecord(PrintStream ps) {
138     ps.println("Domain: " + m_strDomainName);
139     ps.println("Type: " + DNSQuery.GetTypeDesc(m_iType));
140     ps.println("Class: " + DNSQuery.GetClassDesc(m_iClass));
141     
142     ps.println("Expires: " + m_dtExpire.toString());
143 
144     switch (m_iType) {
145     case DNSQuery.TYPE_A: {
146       ps.println("IP Address: " + Long.toHexString(m_lData));
147     break;
148 
149     case DNSQuery.TYPE_MX: {
150       ps.println("MX Server: " + m_strData);
151       ps.println("MX Pref: " + Long.toString(m_lData));
152     break;
153 
154     case DNSQuery.TYPE_NS: 
155     case DNSQuery.TYPE_MD: 
156     case DNSQuery.TYPE_MF: 
157     case DNSQuery.TYPE_CNAME: 
158     case DNSQuery.TYPE_MB: 
159     case DNSQuery.TYPE_MG: 
160     case DNSQuery.TYPE_MR: 
161     case DNSQuery.TYPE_PTR: {
162       ps.println("Domain: " + m_strData);
163     break;
164 
165     case DNSQuery.TYPE_HINFO: {
166       ps.println("CPU: " + m_strData);
167       ps.println("OS: " + m_strData1);
168     break;
169 
170     case DNSQuery.TYPE_SOA: {
171       ps.println("MNAME: " + m_strData);
172       ps.println("RNAME: " + m_strData1);
173       ps.println("SERIAL: " + Long.toString(m_lData));
174       ps.println("REFRESH: " + Long.toString(m_lData1));
175       ps.println("RETRY: " + Long.toString(m_lData2));
176       ps.println("EXPIRE: " + Long.toString(m_lData3));
177       ps.println("MINIMUM: " + Long.toString(m_lData4));
178       ps.println("UNKNOWN: " + Long.toString(m_lData4));
179     break;
180 
181     default{
182       ps.println("Data: " new String(m_abData));
183       dumpBytes(ps, m_abData);
184     }
185     }
186   }
187 
188   private void dumpBytes(PrintStream ps, byte [] ab) {
189     int i;
190     String strTemp;
191 
192     for (i = 0; i < ab.length; i++) {
193       strTemp = Integer.toHexString(ab[i]);
194       if (strTemp.length() 2) {
195         strTemp = "0" + strTemp;
196       }
197 
198       ps.print(strTemp + " ");
199 
200       if (i > && ((i % 8== || i == ab.length-1)) {
201         ps.println();
202       }
203     }
204   }
205 }
Java2html