01: /* DNSJavaUtil
02: *
03: * Created on Oct 8, 2004
04: *
05: * Copyright (C) 2004 Internet Archive.
06: *
07: * This file is part of the Heritrix web crawler (crawler.archive.org).
08: *
09: * Heritrix is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU Lesser Public License as published by
11: * the Free Software Foundation; either version 2.1 of the License, or
12: * any later version.
13: *
14: * Heritrix is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * GNU Lesser Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser Public License
20: * along with Heritrix; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: */
23: package org.archive.util;
24:
25: import java.net.InetAddress;
26:
27: import org.xbill.DNS.ARecord;
28: import org.xbill.DNS.DClass;
29: import org.xbill.DNS.Record;
30: import org.xbill.DNS.TextParseException;
31: import org.xbill.DNS.Type;
32: import org.xbill.DNS.Lookup;
33:
34: ;
35:
36: /**
37: * Utility methods based on DNSJava.
38: * Use these utilities to avoid having to use the native InetAddress lookup.
39: * @author stack
40: * @version $Date: 2007-01-06 05:17:35 +0000 (Sat, 06 Jan 2007) $, $Revision: 4837 $
41: */
42: public class DNSJavaUtil {
43: private DNSJavaUtil() {
44: super ();
45: }
46:
47: /**
48: * Return an InetAddress for passed <code>host</code>.
49: *
50: * If passed host is an IPv4 address, we'll not do a DNSJava
51: * lookup.
52: *
53: * @param host Host to lookup in dnsjava.
54: * @return A host address or null if not found.
55: */
56: public static InetAddress getHostAddress(String host) {
57: InetAddress hostAddress = InetAddressUtil
58: .getIPHostAddress(host);
59: if (hostAddress != null) {
60: return hostAddress;
61: }
62:
63: // Ask dnsjava for the inetaddress. Should be in its cache.
64: Record[] rrecordSet;
65: try {
66: rrecordSet = (new Lookup(host, Type.A, DClass.IN)).run();
67: } catch (TextParseException e) {
68: rrecordSet = null;
69: }
70: if (rrecordSet != null) {
71: // Get TTL and IP info from the first A record (there may be
72: // multiple, e.g. www.washington.edu).
73: for (int i = 0; i < rrecordSet.length; i++) {
74: if (rrecordSet[i].getType() != Type.A) {
75: continue;
76: }
77: hostAddress = ((ARecord) rrecordSet[i]).getAddress();
78: break;
79: }
80: }
81: return hostAddress;
82: }
83: }
|