$search
00001 00004 package javax.jmdns.impl; 00005 00006 import java.util.ArrayList; 00007 import java.util.Collection; 00008 import java.util.Collections; 00009 import java.util.LinkedList; 00010 import java.util.List; 00011 00012 import javax.jmdns.impl.constants.DNSConstants; 00013 00019 public abstract class DNSMessage { 00020 00024 public static final boolean MULTICAST = true; 00025 00029 public static final boolean UNICAST = false; 00030 00031 // protected DatagramPacket _packet; 00032 // protected int _off; 00033 // protected int _len; 00034 // protected byte[] _data; 00035 00036 private int _id; 00037 00038 boolean _multicast; 00039 00040 private int _flags; 00041 00042 protected final List<DNSQuestion> _questions; 00043 00044 protected final List<DNSRecord> _answers; 00045 00046 protected final List<DNSRecord> _authoritativeAnswers; 00047 00048 protected final List<DNSRecord> _additionals; 00049 00055 protected DNSMessage(int flags, int id, boolean multicast) { 00056 super(); 00057 _flags = flags; 00058 _id = id; 00059 _multicast = multicast; 00060 _questions = Collections.synchronizedList(new LinkedList<DNSQuestion>()); 00061 _answers = Collections.synchronizedList(new LinkedList<DNSRecord>()); 00062 _authoritativeAnswers = Collections.synchronizedList(new LinkedList<DNSRecord>()); 00063 _additionals = Collections.synchronizedList(new LinkedList<DNSRecord>()); 00064 } 00065 00066 // public DatagramPacket getPacket() { 00067 // return _packet; 00068 // } 00069 // 00070 // public int getOffset() { 00071 // return _off; 00072 // } 00073 // 00074 // public int getLength() { 00075 // return _len; 00076 // } 00077 // 00078 // public byte[] getData() { 00079 // if ( _data == null ) _data = new byte[DNSConstants.MAX_MSG_TYPICAL]; 00080 // return _data; 00081 // } 00082 00086 public int getId() { 00087 return (_multicast ? 0 : _id); 00088 } 00089 00094 public void setId(int id) { 00095 this._id = id; 00096 } 00097 00101 public int getFlags() { 00102 return _flags; 00103 } 00104 00109 public void setFlags(int flags) { 00110 this._flags = flags; 00111 } 00112 00116 public boolean isMulticast() { 00117 return _multicast; 00118 } 00119 00123 public Collection<? extends DNSQuestion> getQuestions() { 00124 return _questions; 00125 } 00126 00130 public int getNumberOfQuestions() { 00131 return this.getQuestions().size(); 00132 } 00133 00134 public Collection<? extends DNSRecord> getAllAnswers() { 00135 List<DNSRecord> aList = new ArrayList<DNSRecord>(_answers.size() + _authoritativeAnswers.size() + _additionals.size()); 00136 aList.addAll(_answers); 00137 aList.addAll(_authoritativeAnswers); 00138 aList.addAll(_additionals); 00139 return aList; 00140 } 00141 00145 public Collection<? extends DNSRecord> getAnswers() { 00146 return _answers; 00147 } 00148 00152 public int getNumberOfAnswers() { 00153 return this.getAnswers().size(); 00154 } 00155 00159 public Collection<? extends DNSRecord> getAuthorities() { 00160 return _authoritativeAnswers; 00161 } 00162 00166 public int getNumberOfAuthorities() { 00167 return this.getAuthorities().size(); 00168 } 00169 00173 public Collection<? extends DNSRecord> getAdditionals() { 00174 return _additionals; 00175 } 00176 00180 public int getNumberOfAdditionals() { 00181 return this.getAdditionals().size(); 00182 } 00183 00189 public boolean isTruncated() { 00190 return (_flags & DNSConstants.FLAGS_TC) != 0; 00191 } 00192 00198 public boolean isQuery() { 00199 return (_flags & DNSConstants.FLAGS_QR_MASK) == DNSConstants.FLAGS_QR_QUERY; 00200 } 00201 00207 public boolean isResponse() { 00208 return (_flags & DNSConstants.FLAGS_QR_MASK) == DNSConstants.FLAGS_QR_RESPONSE; 00209 } 00210 00216 public boolean isEmpty() { 00217 return (this.getNumberOfQuestions() + this.getNumberOfAnswers() + this.getNumberOfAuthorities() + this.getNumberOfAdditionals()) == 0; 00218 } 00219 00223 String print() { 00224 StringBuffer buf = new StringBuffer(200); 00225 buf.append(this.toString()); 00226 buf.append("\n"); 00227 for (DNSQuestion question : _questions) { 00228 buf.append("\tquestion: "); 00229 buf.append(question); 00230 buf.append("\n"); 00231 } 00232 for (DNSRecord answer : _answers) { 00233 buf.append("\tanswer: "); 00234 buf.append(answer); 00235 buf.append("\n"); 00236 } 00237 for (DNSRecord answer : _authoritativeAnswers) { 00238 buf.append("\tauthoritative: "); 00239 buf.append(answer); 00240 buf.append("\n"); 00241 } 00242 for (DNSRecord answer : _additionals) { 00243 buf.append("\tadditional: "); 00244 buf.append(answer); 00245 buf.append("\n"); 00246 } 00247 return buf.toString(); 00248 } 00249 00256 protected String print(byte[] data) { 00257 StringBuilder buf = new StringBuilder(4000); 00258 for (int off = 0, len = data.length; off < len; off += 32) { 00259 int n = Math.min(32, len - off); 00260 if (off < 0x10) { 00261 buf.append(' '); 00262 } 00263 if (off < 0x100) { 00264 buf.append(' '); 00265 } 00266 if (off < 0x1000) { 00267 buf.append(' '); 00268 } 00269 buf.append(Integer.toHexString(off)); 00270 buf.append(':'); 00271 int index = 0; 00272 for (index = 0; index < n; index++) { 00273 if ((index % 8) == 0) { 00274 buf.append(' '); 00275 } 00276 buf.append(Integer.toHexString((data[off + index] & 0xF0) >> 4)); 00277 buf.append(Integer.toHexString((data[off + index] & 0x0F) >> 0)); 00278 } 00279 // for incomplete lines 00280 if (index < 32) { 00281 for (int i = index; i < 32; i++) { 00282 if ((i % 8) == 0) { 00283 buf.append(' '); 00284 } 00285 buf.append(" "); 00286 } 00287 } 00288 buf.append(" "); 00289 for (index = 0; index < n; index++) { 00290 if ((index % 8) == 0) { 00291 buf.append(' '); 00292 } 00293 int ch = data[off + index] & 0xFF; 00294 buf.append(((ch > ' ') && (ch < 127)) ? (char) ch : '.'); 00295 } 00296 buf.append("\n"); 00297 00298 // limit message size 00299 if (off + 32 >= 2048) { 00300 buf.append("....\n"); 00301 break; 00302 } 00303 } 00304 return buf.toString(); 00305 } 00306 00307 }