/*
* @(#)DNSUtils.java 7/09/2004
*
* Copyright (c) 2004, 2005 jASEN.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* 3. The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 4. Any modification or additions to the software must be contributed back
* to the project.
*
* 5. Any investigation or reverse engineering of source code or binary to
* enable emails to bypass the filters, and hence inflict spam and or viruses
* onto users who use or do not use jASEN could subject the perpetrator to
* criminal and or civil liability.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JASEN.ORG,
* OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.jasen.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import org.jasen.core.parsers.URLParser;
import org.jasen.error.DNSException;
import org.jasen.error.ErrorHandlerBroker;
import org.jasen.interfaces.DNSResolver;
import org.jasen.net.MXRecord;
/**
* <P>
* General DNS utilities.
* </P>
* @author Jason Polites
*/
public final class DNSUtils
{
/**
*
*/
public DNSUtils() {
super ();
}
/**
* The current list of generic top level domains<br/>
* This will be overwritten by a propertied file if one is found
*/
public static String[] TLD = { "aero", "arpa", "biz", "com", "coop", "info", "museum", "name", "net", "org", "pro", "gov", "edu", "mil", "int", "travel", "post" };
static
{
Properties props = new Properties();
try
{
InputStream in = DNSUtils.class.getClassLoader().getResourceAsStream("dns.properties");
if(in == null) {
// try the local file
in = DNSUtils.class.getClassLoader().getResourceAsStream("org/jasen/util/dns.properties");
}
if(in != null) {
props.load(in);
String tlds = props.getProperty("tlds");
if(tlds != null) {
TLD = tlds.split(",");
}
}
}
catch (IOException ignore)
{
// Use the hard coded list above
ErrorHandlerBroker.getInstance().getErrorHandler().handleException(ignore);
}
finally {
Arrays.sort (TLD);
}
}
/**
* Lists the MX records for the given canonical domain
*
* @param canonical The canonical domain. Eg., microsoft.com, NOT www.microsoft.com
* @return The MX (mail exchanger) records for the given domain
* @throws DNSException
*/
public static MXRecord[] getMXRecords(DNSResolver resolver, String canonical) throws UnknownHostException, DNSException {
MXRecord[] records = null;
Attributes atrs = null;
try
{
atrs = resolver.lookup(canonical, "MX");
if (atrs != null && atrs.size () > 0)
{
Attribute at = atrs.get ("MX");
if (at != null && at.size () > 0)
{
// The MX records returned will be in the format:
// <preference> domain.
// eg.
// 10 maila.microsoft.com.
String record = null;
MXRecord mxRecord = null;
String[] parsed = null;
records = new MXRecord[at.size ()];
for (int i = 0; i < at.size (); i++)
{
record = at.get (i).toString ();
parsed = record.split (" ");
if (parsed.length > 1)
{
mxRecord = new MXRecord ();
mxRecord.setPreference (Integer.parseInt (parsed[0]));
if (parsed[1].endsWith ("."))
{
parsed[1] = parsed[1].substring (0, parsed[1].length () - 1);
}
mxRecord.setAddress (InetAddress.getByName (parsed[1]));
records[i] = mxRecord;
}
else
{
throw new DNSException ("Error parsing MX record");
}
}
}
}
}
catch (NamingException e)
{
throw new DNSException (e);
}
return records;
}
/**
* Returns true if the given address is a valid IPv4 or IPv6 address
* @param address
* @return True if the String passed is a valid IP address, false otherwise
*/
public static boolean isIPAddress(String address) {
return (isIPv4Address(address) || isIPv6Address(address));
}
/**
* Returns true if the given string is a correctly formed IPv4 address.
* <br/>
* The long regex stores each of the 4 numbers of the IP address into a
* capturing group. <br/>Taken from: <a
* href="http://www.regular-expressions.info/examples.html">http://www.regular-expressions.info/examples.html
* </a>
*
* @param address
* @return True if the String passed is a valid IP address, false otherwise
*/
public static boolean isIPv4Address(String address) {
String pattern = "\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
return address.matches (pattern);
}
/**
* Returns true if the given string is a correctly formed IPv6 address.
* <br>
* <pre>
There are three conventional forms for representing IPv6 addresses as
text strings:
1. The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the
hexadecimal values of the eight 16-bit pieces of the address.
Examples:
FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
1080:0:0:0:8:800:200C:417A
Note that it is not necessary to write the leading zeros in an
individual field, but there must be at least one numeral in every
field (except for the case described in 2.).
2. Due to some methods of allocating certain styles of IPv6
addresses, it will be common for addresses to contain long strings
of zero bits. In order to make writing addresses containing zero
bits easier a special syntax is available to compress the zeros.
The use of "::" indicates multiple groups of 16-bits of zeros.
The "::" can only appear once in an address. The "::" can also be
used to compress the leading and/or trailing zeros in an address.
For example the following addresses:
1080:0:0:0:8:800:200C:417A a unicast address
FF01:0:0:0:0:0:0:101 a multicast address
0:0:0:0:0:0:0:1 the loopback address
0:0:0:0:0:0:0:0 the unspecified addresses
may be represented as:
1080::8:800:200C:417A a unicast address
FF01::101 a multicast address
::1 the loopback address
:: the unspecified addresses
3. An alternative form that is sometimes more convenient when dealing
with a mixed environment of IPv4 and IPv6 nodes is
x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values of
the six high-order 16-bit pieces of the address, and the 'd's are
the decimal values of the four low-order 8-bit pieces of the
address (standard IPv4 representation).
Examples:
0:0:0:0:0:0:13.1.68.3
0:0:0:0:0:FFFF:129.144.52.38
or in compressed form:
::13.1.68.3
::FFFF:129.144.52.38
* </pre>
* @param address
* @return True if the String passed is a valid IP address, false otherwise
* @see <a href="http://www.ietf.org/rfc/rfc2373.txt">http://www.ietf.org/rfc/rfc2373.txt</a>
*/
public static boolean isIPv6Address(String address) {
// Define the 3 variant regular expressions:
String variant1 = "[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}";
String variant2 = "(?:[0-9A-F]{0,4}:){0,6}:(?:[0-9A-F]{0,4}:){0,5}[0-9A-F]{0,4}";
String variant3 = "[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}:[0-9A-F]{1,4}";
Pattern p1 = Pattern.compile(variant1, Pattern.CASE_INSENSITIVE);
Pattern p2 = Pattern.compile(variant2, Pattern.CASE_INSENSITIVE);
Pattern p3 = Pattern.compile(variant3, Pattern.CASE_INSENSITIVE);
if(p1.matcher(address).matches() || p2.matcher(address).matches()) {
return true;
}
else {
// Test variant 3
int lastColon = address.lastIndexOf(':');
String IPv4 = address.substring(lastColon + 1, address.length());
String IPv6 = address.substring(0, lastColon + 1);
if(IPv6.length() > 2) {
// Test for compressed address
if(IPv6.endsWith(":")) {
if(IPv6.charAt(IPv6.length() - 2) != ':') {
IPv6 = IPv6.substring(0, IPv6.length() -1);
}
}
if(isIPv4Address(IPv4) && (p3.matcher(IPv6).matches() || p2.matcher(IPv6).matches())) {
return true;
}
}
}
return false;
}
/**
* Gets the root domain from a fully qualified domain
* <BR>
* Eg, gets microsoft.com from a.b.c.microsoft.com
* @param domain
* @return
*/
public static String getRootDomain(String domain) {
if(domain != null) {
String suffix = domain.substring (domain.lastIndexOf (".") + 1, domain.length ());
int segments;
if (Arrays.binarySearch (TLD, suffix.toLowerCase ()) >= 0)
{
// This uses a generic top level domain
segments = 2;
}
else
{
// We have a country specific domain
segments = 3;
}
String[] segmentArray = domain.split ("\\.");
if (segmentArray.length > segments)
{
StringBuffer buffer = new StringBuffer ();
int index = segmentArray.length - segments;
for (int i = index; i < segmentArray.length; i++)
{
if (i > index)
{
buffer.append (".");
}
buffer.append (segmentArray[i]);
}
domain = buffer.toString ();
}
}
return domain;
}
/**
* Inverts the IP address to match the requirements of DNS services.
* <br>
*
* @param originalIPAddress the IP address to invert
* @return the inverted form of the passed IP address
*/
public static String invertIPAddress(String originalIPAddress) {
StringTokenizer t = new StringTokenizer(originalIPAddress, ".");
String inverted = t.nextToken();
while (t.hasMoreTokens()) {
inverted = t.nextToken() + "." + inverted;
}
return inverted;
}
/**
* Test harness only
* @param args
*/
public static void main(String[] args) {
try
{
String[] tests = {
"fedc:BA98:7654:3210:FEDC:BA98:7654:3210",
"1080:0:0:0:8:800:200C:417A",
"FF01:0:0:0:0:0:0:101",
"0:0:0:0:0:0:0:1",
"0:0:0:0:0:0:0:0",
"1080::8:800:200C:417A",
"FF01:1080::8:800:200C:417A",
"FF01::101",
"::1",
"::",
"0:0:0:0:0:0:13.1.68.3",
"0:0:0:0:0:FFFF:129.144.52.38",
"::13.1.68.3",
"::FFFF:129.144.52.38"};
for (int i = 0; i < tests.length; i++)
{
System.out.println (tests[i] + " -> " + isIPv6Address(tests[i]));
}
}
catch (Exception e)
{
e.printStackTrace ();
}
}
/**
* Gets the valid domain part of a URL.
* @param host
* @return Returns null if there is no valid domain
*/
public static String getValidDomainOnly(String host) {
String[] split = null;
if(!isIPAddress(host)) {
split = host.split("\\.");
if(split.length > 2) {
if(Arrays.binarySearch(URLParser.URL_WORDS, split[0]) > -1) {
host = split[1] + "." + split[2];
}
}
else {
// The host is invalid
host = null;
}
}
return host;
}
/**
* Determines if the string passed "appears" to be a valid domain
* @param str The string to test
* @return True if the string matches a standard domain sequence.
* That is, a sequence of alphanumeric characters delimited by dots.
* False otherwise
*/
public static boolean isDomain(String str) {
return str.matches("[a-z0-9][-a-z0-9]*(\\.[-a-z0-9]+)*\\.[a-z]{2,6}");
}
}