Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

211
LINES

< > BotCompany Repo | #1008224 // dig (DNS lookup in Pure Java)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 879K of libraries. Click here for Pure Java version (962L/6K/22K).

1  
!7
2  
3  
lib 1008223
4  
5  
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
6  
7  
import java.io.*;
8  
import java.net.*;
9  
import org.xbill.DNS.*;
10  
import org.xbill.DNS.Type;
11  
12  
/** @author Brian Wellington &lt;bwelling@xbill.org&gt; */
13  
14  
public class main {
15  
16  
static Name name = null;
17  
static int type = Type.A, dclass = DClass.IN;
18  
19  
static void
20  
usage() {
21  
	System.out.println("Usage: dig [@server] name [<type>] [<class>] " +
22  
			   "[options]");
23  
}
24  
25  
static void
26  
doQuery(Message response, long ms) throws IOException {
27  
	System.out.println("; java dig 0.0");
28  
	System.out.println(response);
29  
	System.out.println(";; Query time: " + ms + " ms");
30  
}
31  
32  
static void
33  
doAXFR(Message response) throws IOException {
34  
	System.out.println("; java dig 0.0 <> " + name + " axfr");
35  
	if (response.isSigned()) {
36  
		System.out.print(";; TSIG ");
37  
		if (response.isVerified())
38  
			System.out.println("ok");
39  
		else
40  
			System.out.println("failed");
41  
	}
42  
43  
	if (response.getRcode() != Rcode.NOERROR) {
44  
		System.out.println(response);
45  
		return;
46  
	}
47  
48  
	Record [] records = response.getSectionArray(Section.ANSWER);
49  
	for (int i = 0; i < records.length; i++)
50  
		System.out.println(records[i]);
51  
52  
	System.out.print(";; done (");
53  
	System.out.print(response.getHeader().getCount(Section.ANSWER));
54  
	System.out.print(" records, ");
55  
	System.out.print(response.getHeader().getCount(Section.ADDITIONAL));
56  
	System.out.println(" additional)");
57  
}
58  
59  
public static void
60  
main(String argv[]) throws IOException {
61  
  tt();
62  
	String server = null;
63  
	int arg;
64  
	Message query, response;
65  
	Record rec;
66  
	SimpleResolver res = null;
67  
	boolean printQuery = false;
68  
	long startTime, endTime;
69  
70  
	if (argv.length < 1) {
71  
		usage();
72  
		argv = new S[] {"ai1.lol"};
73  
	}
74  
75  
	try {
76  
		arg = 0;
77  
		if (argv[arg].startsWith("@"))
78  
			server = argv[arg++].substring(1);
79  
80  
		if (server != null)
81  
			res = new SimpleResolver(server);
82  
		else
83  
			res = new SimpleResolver();
84  
85  
		String nameString = argv[arg++];
86  
		if (nameString.equals("-x")) {
87  
			name = ReverseMap.fromAddress(argv[arg++]);
88  
			type = Type.PTR;
89  
			dclass = DClass.IN;
90  
		}
91  
		else {
92  
			name = Name.fromString(nameString, Name.root);
93  
			type = Type.value(argv[arg]);
94  
			if (type < 0)
95  
				type = Type.A;
96  
			else
97  
				arg++;
98  
99  
			dclass = DClass.value(argv[arg]);
100  
			if (dclass < 0)
101  
				dclass = DClass.IN;
102  
			else
103  
				arg++;
104  
		}
105  
106  
		while (argv[arg].startsWith("-") && argv[arg].length() > 1) {
107  
			switch (argv[arg].charAt(1)) {
108  
			case 'p':
109  
				String portStr;
110  
				int port;
111  
				if (argv[arg].length() > 2)
112  
					portStr = argv[arg].substring(2);
113  
				else
114  
					portStr = argv[++arg];
115  
				port = Integer.parseInt(portStr);
116  
				if (port < 0 || port > 65536) {
117  
					System.out.println("Invalid port");
118  
					return;
119  
				}
120  
				res.setPort(port);
121  
				break;
122  
123  
			case 'b':
124  
				String addrStr;
125  
				if (argv[arg].length() > 2)
126  
					addrStr = argv[arg].substring(2);
127  
				else
128  
					addrStr = argv[++arg];
129  
				InetAddress addr;
130  
				try {
131  
					addr = InetAddress.getByName(addrStr);
132  
				}
133  
				catch (Exception e) {
134  
					System.out.println("Invalid address");
135  
					return;
136  
				}
137  
				res.setLocalAddress(addr);
138  
				break;
139  
140  
			case 'k':
141  
				String key;
142  
				if (argv[arg].length() > 2)
143  
					key = argv[arg].substring(2);
144  
				else
145  
					key = argv[++arg];
146  
				res.setTSIGKey(TSIG.fromString(key));
147  
				break;
148  
149  
			case 't':
150  
				res.setTCP(true);
151  
				break;
152  
153  
			case 'i':
154  
				res.setIgnoreTruncation(true);
155  
				break;
156  
157  
			case 'e':
158  
				String ednsStr;
159  
				int edns;
160  
				if (argv[arg].length() > 2)
161  
					ednsStr = argv[arg].substring(2);
162  
				else
163  
					ednsStr = argv[++arg];
164  
				edns = Integer.parseInt(ednsStr);
165  
				if (edns < 0 || edns > 1) {
166  
					System.out.println("Unsupported " +
167  
							   "EDNS level: " +
168  
							   edns);
169  
					return;
170  
				}
171  
				res.setEDNS(edns);
172  
				break;
173  
174  
			case 'd':
175  
				res.setEDNS(0, 0, ExtendedFlags.DO, null);
176  
				break;
177  
178  
			case 'q':
179  
			    	printQuery = true;
180  
				break;
181  
182  
			default:
183  
				System.out.print("Invalid option: ");
184  
				System.out.println(argv[arg]);
185  
			}
186  
			arg++;
187  
		}
188  
189  
	}
190  
	catch (ArrayIndexOutOfBoundsException e) {
191  
		if (name == null)
192  
			usage();
193  
	}
194  
	if (res == null)
195  
		res = new SimpleResolver();
196  
197  
	rec = Record.newRecord(name, type, dclass);
198  
	query = Message.newQuery(rec);
199  
	if (printQuery)
200  
		System.out.println(query);
201  
	startTime = System.currentTimeMillis();
202  
	response = res.send(query);
203  
	endTime = System.currentTimeMillis();
204  
205  
	if (type == Type.AXFR)
206  
		doAXFR(response);
207  
	else
208  
		doQuery(response, endTime - startTime);
209  
}
210  
211  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008224
Snippet name: dig (DNS lookup in Pure Java)
Eternal ID of this version: #1008224/8
Text MD5: 5a8d0956be49f5a40d3286170191afd8
Transpilation MD5: 113e385112a913e16f86db5f1ddd3ba6
Author: stefan
Category: javax / networking
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-01 15:20:39
Source code size: 4617 bytes / 211 lines
Pitched / IR pitched: No / No
Views / Downloads: 766 / 844
Version history: 7 change(s)
Referenced in: [show references]