// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)

import java.io.*;
import org.xbill.DNS.*;

/** @author Brian Wellington &lt;bwelling@xbill.org&gt; */

public class dig {

static Name name = null;
static int type = Type.A, dclass = DClass.IN;

static void
usage() {
	System.out.println("Usage: dig [@server] name [<type>] [<class>] " +
			   "[options]");
	System.exit(0);
}

static void
doQuery(Message response, long ms) throws IOException {
	System.out.println("; java dig 0.0");
	System.out.println(response);
	System.out.println(";; Query time: " + ms + " ms");
}

static void
doAXFR(Message response) throws IOException {
	System.out.println("; java dig 0.0 <> " + name + " axfr");
	if (response.isSigned()) {
		System.out.print(";; TSIG ");
		if (response.isVerified())
			System.out.println("ok");
		else
			System.out.println("failed");
	}

	if (response.getRcode() != Rcode.NOERROR) {
		System.out.println(response);
		return;
	}

	Record [] records = response.getSectionArray(Section.ANSWER);
	for (int i = 0; i < records.length; i++)
		System.out.println(records[i]);

	System.out.print(";; done (");
	System.out.print(response.getHeader().getCount(Section.ANSWER));
	System.out.print(" records, ");
	System.out.print(response.getHeader().getCount(Section.ADDITIONAL));
	System.out.println(" additional)");
}

public static void
main(String argv[]) throws IOException {
	String server = null;
	int arg;
	Message query, response;
	Record rec;
	Record opt = null;
	Resolver res = null;
	boolean printQuery = false;
	long startTime, endTime;

	if (argv.length < 1) {
		usage();
	}

	try {
		arg = 0;
		if (argv[arg].startsWith("@"))
			server = argv[arg++].substring(1);

		String nameString = argv[arg++];
		if (nameString.equals("-x")) {
			name = ReverseMap.fromAddress(argv[arg++]);
			type = Type.PTR;
			dclass = DClass.IN;
		}
		else {
			name = Name.fromString(nameString, Name.root);
			type = Type.value(argv[arg]);
			if (type < 0)
				type = Type.A;
			else
				arg++;

			dclass = DClass.value(argv[arg]);
			if (dclass < 0)
				dclass = DClass.IN;
			else
				arg++;
		}

		if (server != null)
			res = new SimpleResolver(server);
		else if (type == Type.AXFR)
			res = new SimpleResolver();
		else
			res = new ExtendedResolver();

		while (argv[arg].startsWith("-") && argv[arg].length() > 1) {
			switch (argv[arg].charAt(1)) {
			    case 'p':
				String portStr;
				int port;
				if (argv[arg].length() > 2)
					portStr = argv[arg].substring(2);
				else
					portStr = argv[++arg];
				port = Integer.parseInt(portStr);
				if (port < 0 || port > 65536) {
					System.out.println("Invalid port");
					return;
				}
				res.setPort(port);
				break;

			    case 'k':
				String key;
				if (argv[arg].length() > 2)
					key = argv[arg].substring(2);
				else
					key = argv[++arg];
				int index = key.indexOf('/');
				if (index < 0)
					res.setTSIGKey(key);
				else
					res.setTSIGKey(key.substring(0, index),
						       key.substring(index+1));
				break;

			    case 't':
				res.setTCP(true);
				break;

			    case 'i':
				res.setIgnoreTruncation(true);
				break;

			    case 'e':
				String ednsStr;
				int edns;
				if (argv[arg].length() > 2)
					ednsStr = argv[arg].substring(2);
				else
					ednsStr = argv[++arg];
				edns = Integer.parseInt(ednsStr);
				if (edns < 0 || edns > 1) {
					System.out.println("Unsupported " +
							   "EDNS level: " +
							   edns);
					return;
				}
				res.setEDNS(edns);
				break;

			    case 'd':
			    	opt = new OPTRecord((short)1280, (byte)0,
						    (byte)0, ExtendedFlags.DO);
				break;

			    case 'q':
			    	printQuery = true;
				break;

			    default:
				System.out.print("Invalid option: ");
				System.out.println(argv[arg]);
			}
			arg++;
		}

	}
	catch (ArrayIndexOutOfBoundsException e) {
		if (name == null)
			usage();
	}
	if (res == null)
		res = new ExtendedResolver();

	rec = Record.newRecord(name, type, dclass);
	query = Message.newQuery(rec);
	if (opt != null)
		query.addRecord(opt, Section.ADDITIONAL);
	if (printQuery)
		System.out.println(query);
	startTime = System.currentTimeMillis();
	response = res.send(query);
	endTime = System.currentTimeMillis();

	if (type == Type.AXFR)
		doAXFR(response);
	else
		doQuery(response, endTime - startTime);
}

}

@@ -2833,171 +2833,42 @@
 %09%09%09%09
-int index = key.indexOf('/');%0A%09%09%09%09if (index %3C 0)%0A%09%09%09%09%09res.setTSIGKey(key);%0A%09%09%09%09else%0A%09%09%09%09%09res.setTSIGKey(key.substring(0, index),%0A%09%09%09%09%09%09       key.substring(index+1
+res.setTSIGKey(TSIG.fromString(key
 ));%0A

Use res.setTSIGKey(TSIG).