001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.util;
018:
019: import java.util.List;
020: import java.util.Hashtable;
021: import java.util.ArrayList;
022:
023: import javax.naming.directory.DirContext;
024: import javax.naming.directory.InitialDirContext;
025: import javax.naming.directory.Attributes;
026: import javax.naming.directory.Attribute;
027: import javax.naming.NamingEnumeration;
028: import javax.naming.NamingException;
029:
030: /**
031: * Utility class to resolve names against DN servers
032: */
033: public class DNSResolver {
034:
035: /**
036: * check DNS.
037: * @param hostname hostname
038: * @return true if valid
039: */
040: public static boolean checkDNS(String hostname) {
041: return checkDNS(hostname, false);
042: }
043:
044: /**
045: * check DNS.
046: * @param hostname hostname
047: * @param mx do MX query or not
048: * @return true if valid
049: */
050: public static boolean checkDNS(String hostname, boolean mx) {
051: List<String> records = resolveDNS(hostname, mx);
052: return records != null && records.size() > 0;
053: }
054:
055: /**
056: * Resolve MX DNS.
057: * @param hostname hostname
058: * @return list of MXs
059: */
060: public static List<String> resolveDNS(String hostname, boolean mx) {
061: List<String> result = new ArrayList<String>();
062: try {
063: Logger.trace("DNS validation: resolving DNS for "
064: + hostname + " " + (mx ? "(MX)" : "(A/CNAME)"));
065: Hashtable env = new Hashtable();
066: env.put("java.naming.factory.initial",
067: "com.sun.jndi.dns.DnsContextFactory");
068: env.put("com.sun.jndi.dns.timeout.initial", "5000"); /* quite short... too short? */
069: env.put("com.sun.jndi.dns.timeout.retries", "1");
070: DirContext ictx = new InitialDirContext(env);
071: String[] ids = (mx ? new String[] { "MX" } : new String[] {
072: "A", "CNAME" });
073: Attributes attrs = ictx.getAttributes(hostname, ids);
074: if (mx) {
075: Attribute attr = attrs.get("MX");
076: if (attr != null && attr.size() > 0) {
077: NamingEnumeration e = attr.getAll();
078: while (e.hasMore()) {
079: String mxs = (String) e.next();
080: String f[] = mxs.split("\\s+");
081: for (int i = 0; i < f.length; i++) {
082: if (f[i].endsWith(".")) {
083: result.add(f[i].substring(0, f[i]
084: .length() - 1));
085: }
086: }
087: }
088: return result;
089: } else {
090: Logger.trace("DNS validation: DNS query of '"
091: + hostname + "' failed");
092: return null;
093: }
094: } else {
095: Attribute attr = attrs.get("A");
096: if (attr != null && attr.size() > 0) {
097: NamingEnumeration e = attr.getAll();
098: while (e.hasMore()) {
099: result.add((String) e.next());
100: }
101: return result;
102: } else {
103: attr = attrs.get("CNAME");
104: if (attr != null && attr.size() > 0) {
105: NamingEnumeration e = attr.getAll();
106: while (e.hasMore()) {
107: String h = (String) e.next();
108: if (h.endsWith(".")) {
109: h = h.substring(0, h.lastIndexOf('.'));
110: }
111: Logger
112: .trace("DNS validation: recursing on CNAME record towards host "
113: + h);
114: result.addAll(resolveDNS(h, false));
115: }
116: return result;
117: } else {
118: Logger.trace("DNS validation: DNS query of '"
119: + hostname + "' failed");
120: return null;
121: }
122: }
123: }
124: } catch (NamingException ne) {
125: Logger.trace("DNS validation: DNS MX query failed: "
126: + ne.getMessage());
127: return null;
128: }
129: }
130: }
|