All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Net.DnsCache Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Net;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;

/**
 * Summary description for DnsCache.
 */
public class DnsCache {
    /**
     * map for forward lookups
     */
    private static java.util.HashMap _fwdMap;
    /**
     * map for reverse lookups
     */
    private static java.util.HashMap _bckMap;

    static {
        _fwdMap = new HashMap();
        _bckMap = new HashMap();
    }

    /**
     * Does a DNS lookup on the hostname. updates the reverse cache to optimize reverse lookups.
     *
     * @param hostname
     * @return
     */
    public static InetAddress ResolveName(String hostname) throws UnknownHostException {
//			/// see if it is already an ip-address, i.e. dotted notation.
//			try
//			{
//				return IPAddress.Parse(hostname);
//			}
//			catch (Exception)
//			{
//			}
        hostname = hostname.toLowerCase();
        if (!_fwdMap.containsKey(hostname)) {
            InetAddress[] ie = InetAddress.getAllByName(hostname);

            if (ie != null && ie.length > 0) {

                _fwdMap.put(hostname, ie[0]);
                _bckMap.put(ie[0], hostname);

            }
        }

        return (InetAddress) ((_fwdMap.get(hostname) instanceof InetAddress) ? _fwdMap.get(hostname) : null);
    }


    /**
     * Does a reverse DNS lookup on the address. updates the forward cache to optimize lookups.
     *
     * @param address
     * @return
     */
    public static String ResolveAddress(String address) throws UnknownHostException {
        try {
            return InetAddress.getByName(address).getHostAddress();
        } catch (RuntimeException ex) {
            //Trace.error("DnsCache.ResolveAddress", "exception=" + ex.ToString());
        }
        return null;
    }


    /**
     * Does a reverse DNS lookup on the address. updates the forward cache to optimize lookups.
     *
     * @param address
     * @return
     */
    public static String ResolveAddress(InetAddress address) throws UnknownHostException {
        if (!_bckMap.containsKey(address)) {
            InetAddress ie = InetAddress.getByAddress(address.getAddress());

            String hostname = ie.getHostName();
            hostname = hostname.replace("is~", "");
            if (hostname.indexOf('.') > 0) {
                hostname = hostname.substring(0, hostname.indexOf('.'));
            }


            _bckMap.put(address, hostname); //ie.HostName.ToLower();
            _fwdMap.put(ie.getHostName(), address);


        }

        return (String) ((_bckMap.get(address) instanceof String) ? _bckMap.get(address) : null);
    }


    /**
     * Clears the caches
     */
    public static void FlushCache() {

        _fwdMap.clear();
        _bckMap.clear();

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy