Source code: com/act365/net/dns/Query.java
1 /*
2 * JSocket Wrench
3 *
4 * Copyright (C) act365.com October 2003
5 *
6 * Web site: http://www.act365.com/wrench
7 * E-mail: developers@act365.com
8 *
9 * The JSocket Wrench library adds support for low-level Internet protocols
10 * to the Java programming language.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 * Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 package com.act365.net.dns ;
28
29 import java.util.*;
30
31 /**
32 Represents a DNS Query Message.
33 */
34
35 public class Query {
36
37 byte[] query_name ;
38 short query_type ;
39 short query_class ;
40 String domain_name ;
41
42 /**
43 Trivial Query constructor
44 */
45
46 public Query( byte[] query_name , short query_type , short query_class , String domain_name ) {
47
48 this.query_name = query_name ;
49 this.query_type = query_type ;
50 this.query_class = query_class ;
51 this.domain_name = domain_name ;
52 }
53
54 /**
55 Constructs a Query from a string representation of
56 a single domain name.
57 */
58
59 public Query( String domain_name ) throws Exception {
60
61 this.domain_name = domain_name ;
62
63 query_name = new byte[ domain_name.length() + 2 ];
64
65 parse( domain_name , query_name , 0 );
66
67 query_type = (short) DNSMessage.A ;
68 query_class = (short) 1 ;
69 }
70
71 /**
72 Message length
73 */
74
75 public int length() {
76 return 4 + query_name.length ;
77 }
78
79 /**
80 String representation
81 */
82
83 public String toString() {
84 return domain_name ;
85 }
86
87 /**
88 Converts a string representation of a domain name, e.g. "www.act365.com"
89 into the byte stream expected by the query_name member.
90 */
91
92 static int parse( String name , byte[] buffer , int offset ) throws Exception {
93
94 StringTokenizer parser = new StringTokenizer( name , "." );
95
96 byte[] label ;
97
98 int i ;
99
100 while( parser.hasMoreTokens() ){
101 label = parser.nextToken().getBytes("UTF8");
102 if( label.length > 63 ){
103 throw new Exception("Maximum label length exceeded");
104 }
105 buffer[ offset ++ ] = (byte) label.length ;
106 i = -1 ;
107 while( ++ i < label.length ){
108 buffer[ offset ++ ] = label[ i ];
109 }
110 }
111
112 buffer[ offset ++ ] = (byte) 0 ;
113
114 return offset ;
115 }
116 }
117