Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/act365/net/dns/DNSLookup.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 com.act365.net.* ;
30  import com.act365.net.ip.* ;
31  import com.act365.net.udp.* ;
32  
33  import java.net.*;
34  
35  /**
36   Implements a DNS client. 
37   Usage: <code>DNSLookup -r -p protocol -l localhost DNSServer domain</code>.
38   <code>-r</code> (optional) indicates whether recursion is desired, i.e. 
39   whether a query that cannot be resolved by the named server should be
40   passed on to another server.
41   <p><code>-p protocol</code> (optional) defines the socket protocol to be used.
42   By default, the JDK UDP implementation will be used. The alternatives are
43   UDP or RawUDP. 
44   <p><code>-l localhost</code> (optional) should be specified if the protocol
45   has been set to RawUDP. The information will be used to construct the IP header.
46   <p><code>DNSServer</code> is the IP address of the DNS server.
47   <p><code>domain</code> is the domain name to be resolved.
48  */
49  
50  public class DNSLookup {
51  
52    /**
53     Sends a DNS query to a DNS server.
54    */
55  
56    public static void main( String[] args ) {
57  
58      final String errortext = "Usage: DNSLookup -r -p protocol -l localhost DNSServer domain";
59  
60      if( args.length < 2 ){
61        System.err.println( errortext );
62        System.exit( 1 );
63      }
64      
65      int i = -1 ;
66  
67      boolean recursion_desired = false ;
68  
69      String servername = args[ args.length - 2 ] ,
70             domainname = args[ args.length - 1 ] ,
71             protocollabel = "",
72             localhost = null ;
73  
74      while( ++ i < args.length - 2 ){
75        if( args[ i ].equals("-r") ){
76          recursion_desired = true ;
77        } else if( args[ i ].equals("-p") && i < args.length - 3 ){
78          protocollabel = args[ ++ i ];
79          if( ! protocollabel.equalsIgnoreCase("UDP") && 
80              ! protocollabel.equalsIgnoreCase("RawUDP") ){
81                System.err.println("Unsupported protocol");
82                System.exit( 2 );
83              }
84        } else if( args[ i ].equals("-l") && i < args.length - 3 ){
85          localhost = args[ ++ i ];
86        } else {
87          System.err.println( errortext );
88          System.exit( 1 );
89        }
90      }
91  
92      try {
93        SocketUtils.setProtocol( protocollabel );
94      } catch ( java.io.IOException e ) {
95        System.err.println("Unsupported protocol");
96        System.exit( 2 );
97      }
98      
99      final int protocol = SocketUtils.getProtocol();
100     
101     boolean includeheader = SocketUtils.includeHeader();
102     
103     new SocketWrenchSession();
104     
105     InetAddress server = null ,
106                 source = null ;
107 
108     try {
109       server = InetAddress.getByName( servername );
110       if( localhost instanceof String ){
111         source = InetAddress.getByName( localhost );
112       }
113     } catch( UnknownHostException e ){
114       System.err.println("DNS server " + e.getMessage() + " is unknown");
115       System.exit( 2 );
116     }
117 
118     DatagramSocket socket = null ;
119 
120     try {
121       if( source instanceof InetAddress ){
122         socket = new DatagramSocket( 53 , source );
123       } else {
124         socket = new DatagramSocket( 53 );
125       }
126     } catch ( SocketException e ) {
127       System.err.println( e.getMessage() );
128       System.exit( 3 );
129     }
130 
131     final int maxdatagramlength = 512 ;
132 
133     byte[] sendbuffer = null ,
134            recvbuffer = new byte[ maxdatagramlength ];
135 
136     DatagramPacket packet = null ;
137 
138     DNSMessage message = null ;
139 
140     // A RawUDP header comprises 20 bytes of IP4 info and 8 bytes of UDP.
141     
142     DNSReader reader = new DNSReader( includeheader ? 28 : 0 ); 
143 
144     try {
145       
146       sendbuffer = DNSWriter.write( (short) socket.hashCode() , recursion_desired , domainname );
147       
148       if( includeheader ){
149 
150     sendbuffer = UDPWriter.write( source.getAddress() , (short) 53 , server.getAddress() , (short) 53 , sendbuffer , sendbuffer.length );
151     sendbuffer = IP4Writer.write( IP4.TOS_COMMAND ,
152                    (short) 255 ,
153                    (byte) SocketConstants.IPPROTO_UDP ,
154                    source.getAddress() ,
155                    server.getAddress() ,
156                    sendbuffer );
157       }
158       
159       socket.send( new DatagramPacket( sendbuffer , sendbuffer.length , server , 53 ) );
160 
161       packet = new DatagramPacket( recvbuffer , maxdatagramlength );
162 
163       socket.receive( packet );
164 
165       message = reader.read( packet.getData() );
166 
167       i = -1 ;
168 
169       while( ++ i < message.questions.length ){
170         System.out.println( "QUERY " + (int)( i + 1 ) );
171         System.out.println( message.questions[ i ].toString() );
172       }
173 
174       i = -1 ;
175 
176       while( ++ i < message.answers.length ){
177         System.out.println( "ANSWER " + (int)( i + 1 ) );
178         System.out.println( new String( message.answers[ i ].domain_name , "UTF8" ) + ": " + message.answers[ i ].toString() );
179       }
180 
181       i = -1 ;
182 
183       while( ++ i < message.authority_records.length ){
184         System.out.println( "AUTHORITY RECORD " + (int)( i + 1 ) );
185         System.out.println( new String( message.authority_records[ i ].domain_name , "UTF8" ) + ": " + message.authority_records[ i ].toString() );
186       }
187 
188 
189       i = -1 ;
190 
191       while( ++ i < message.additional_records.length ){
192         System.out.println( "ADDITIONAL RECORD " + (int)( i + 1 ) );
193         System.out.println( new String( message.additional_records[ i ].domain_name , "UTF8" ) + ": " + message.additional_records[ i ].toString() );
194       }
195 
196     } catch ( Exception e ) {
197       System.err.println( e.getMessage() );
198       e.printStackTrace();
199       System.exit( 4 );
200     }
201   }
202 
203 }
204