Example usage for java.net InetAddress getAddress

List of usage examples for java.net InetAddress getAddress

Introduction

In this page you can find the example usage for java.net InetAddress getAddress.

Prototype

public byte[] getAddress() 

Source Link

Document

Returns the raw IP address of this InetAddress object.

Usage

From source file:org.commoncrawl.io.internal.NIODNSLocalResolver.java

public NIODNSQueryResult doDNSQuery(NIODNSQueryClient client, String hostName, boolean useTCP, boolean noCache,
        int timeoutValue) {

    // check cache first ...
    NIODNSQueryResult result = null;/*  ww w . ja v a2s  .co  m*/
    int retryCount = 0;
    boolean retry = false;
    boolean resultViaCache = false;

    do {

        // Ask dnsjava for the inetaddress. Should be in its cache.
        Message response = null;
        InetAddress address = null;
        String cname = null;
        long expireTime = -1;
        Exception resolverException = null;

        _queryCount++;

        try {
            // LOG.info("Checking Cache for Host:" + hostName);
            if (!noCache)
                result = checkCache(client, hostName);

            if (result != null) {
                // increment stats ...
                _cacheHits++;
                resultViaCache = true;
            }
        } catch (UnknownHostException e) {
            if (_logger != null)
                _logger.logDNSException(hostName, StringUtils.stringifyException(e));
        }

        // if not found in cache ... then do full lookup ...
        if (result == null) {

            _cacheMisses++;

            try {

                // allocate a simple resolver object ...
                NIODNSSimpleResolverImpl resolver = new NIODNSSimpleResolverImpl(this, _dnsServerAddress);

                // use tcp if requested ...
                if (useTCP)
                    resolver.setTCP(true);

                // set the timeout ...
                resolver.setTimeout(timeoutValue);

                // create appropriate data structures ...
                Name name = Name.fromString(hostName, Name.root);
                Record rec = Record.newRecord(name, Type.A, DClass.IN);
                Message query = Message.newQuery(rec);

                // send it off ...
                try {
                    response = resolver.send(query);
                } catch (IOException e) {
                    if (_logger != null)
                        _logger.logDNSException(hostName, StringUtils.stringifyException(e));

                    resolverException = e;
                    if (retryCount++ != MAX_DNS_RETRIES) {
                        LOG.info("Waiting to Retry Failed DNS Query for:" + hostName);
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e1) {
                        }
                        LOG.info("Retrying Failed DNS Query for:" + hostName);
                        retry = true;
                    }
                }

                if (response != null && response.getRcode() == Rcode.NOERROR) {

                    // get answer
                    Record records[] = response.getSectionArray(Section.ANSWER);

                    if (records != null) {

                        // walk records ...
                        for (Record record : records) {

                            // store CName for later use ...
                            if (record.getType() == Type.CNAME) {
                                cname = ((CNAMERecord) record).getAlias().toString();
                                if (cname != null && cname.endsWith(".")) {
                                    cname = cname.substring(0, cname.length() - 1);
                                }
                            }
                            // otherwise look for A record
                            else if (record.getType() == Type.A && address == null) {
                                address = ((ARecord) record).getAddress();
                                expireTime = Math.max(
                                        (System.currentTimeMillis() + (((ARecord) record).getTTL() * 1000)),
                                        System.currentTimeMillis() + MIN_TTL_VALUE);
                            }
                        }
                    }

                    if (address != null) {
                        // LOG.info("Caching DNS Entry for Host:" + hostName);
                        // update dns cache ...
                        if (!noCache)
                            _dnsCache.cacheIPAddressForHost(hostName,
                                    IPAddressUtils.IPV4AddressToInteger(address.getAddress()), expireTime,
                                    cname);
                    }
                }
            } catch (TextParseException e) {
                resolverException = e;
            } catch (UnknownHostException e) {
                resolverException = e;
            }

            if (!retry) {
                // create result object ...
                result = new NIODNSQueryResult(this, client, hostName);

                if (response == null) {

                    if (resolverException != null && (resolverException instanceof TextParseException
                            || resolverException instanceof WireParseException)) {
                        result.setStatus(Status.SERVER_FAILURE);
                        result.setErrorDesc(StringUtils.stringifyException(resolverException));
                    } else {
                        result.setStatus(Status.RESOLVER_FAILURE);
                        if (resolverException != null) {
                            if (_logger != null)
                                _logger.logDNSException(hostName,
                                        StringUtils.stringifyException(resolverException));
                            result.setErrorDesc(StringUtils.stringifyException(resolverException));
                        } else {
                            if (_logger != null)
                                _logger.logDNSException(hostName, "Response was NULL");
                        }
                    }
                } else if (response.getRcode() != Rcode.NOERROR) {
                    result.setStatus(Status.SERVER_FAILURE);
                    result.setErrorDesc(Rcode.string(response.getRcode()));
                    if (_logger != null)
                        _logger.logDNSFailure(hostName, Rcode.string(response.getRcode()));
                } else if (response.getRcode() == Rcode.NOERROR) {

                    if (address != null) {

                        result.setStatus(Status.SUCCESS);
                        result.setAddress(address);
                        result.setCName(cname);
                        result.setTTL(expireTime);

                        if (_logger != null) {
                            _logger.logDNSQuery(hostName, address, expireTime, cname);
                        }

                    } else {
                        result.setStatus(Status.SERVER_FAILURE);
                        result.setErrorDesc("UNKNOWN-NO A RECORD");
                        if (_logger != null)
                            _logger.logDNSFailure(hostName, "NOERROR");
                    }
                }
            }
        }
    } while (result == null);

    // if result is server failure ... and not via bad host cache ...
    if (!resultViaCache && result.getStatus() == Status.SERVER_FAILURE) {
        if (result.getErrorDescription().equals("NXDOMAIN")) {
            _badHostCache.cacheIPAddressForHost(hostName, 0,
                    System.currentTimeMillis() + NXDOMAIN_FAIL_BAD_HOST_LIFETIME, null);
        }
    }

    // return result ... will be added to completion queue (to be retrieved via
    // poll method)...
    return result;
}

From source file:org.commoncrawl.io.NIODNSLocalResolver.java

public DNSQueryResult doDNSQuery(NIODNSQueryClient client, String hostName, boolean useTCP, boolean noCache,
        int timeoutValue) {

    // check cache first ...
    DNSQueryResult result = null;//from   ww w. ja v  a2s.  co  m
    int retryCount = 0;
    boolean retry = false;
    boolean resultViaCache = false;

    do {

        // Ask dnsjava for the inetaddress. Should be in its cache.
        Message response = null;
        InetAddress address = null;
        String cname = null;
        long expireTime = -1;
        Exception resolverException = null;

        _queryCount++;

        try {
            // LOG.info("Checking Cache for Host:" + hostName);
            if (!noCache)
                result = checkCache(client, hostName);

            if (result != null) {
                // increment stats ...
                _cacheHits++;
                resultViaCache = true;
            }
        } catch (UnknownHostException e) {
            if (_logger != null)
                _logger.logDNSException(hostName, StringUtils.stringifyException(e));
        }

        // if not found in cache ... then do full lookup ...
        if (result == null) {

            _cacheMisses++;

            try {

                // allocate a simple resolver object ...
                NIODNSSimpleResolverImpl resolver = new NIODNSSimpleResolverImpl(this, _dnsServerAddress);

                // use tcp if requested ...
                if (useTCP)
                    resolver.setTCP(true);

                // set the timeout ...
                resolver.setTimeout(timeoutValue);

                // create appropriate data structures ...
                Name name = Name.fromString(hostName, Name.root);
                Record rec = Record.newRecord(name, Type.A, DClass.IN);
                Message query = Message.newQuery(rec);

                // send it off ...
                try {
                    response = resolver.send(query);
                } catch (IOException e) {
                    if (_logger != null)
                        _logger.logDNSException(hostName, StringUtils.stringifyException(e));

                    resolverException = e;
                    if (retryCount++ != MAX_DNS_RETRIES) {
                        LOG.info("Waiting to Retry Failed DNS Query for:" + hostName);
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e1) {
                        }
                        LOG.info("Retrying Failed DNS Query for:" + hostName);
                        retry = true;
                    }
                }

                if (response != null && response.getRcode() == Rcode.NOERROR) {

                    // get answer
                    Record records[] = response.getSectionArray(Section.ANSWER);

                    if (records != null) {

                        // walk records ...
                        for (Record record : records) {

                            // store CName for later use ...
                            if (record.getType() == Type.CNAME) {
                                cname = ((CNAMERecord) record).getAlias().toString();
                                if (cname != null && cname.endsWith(".")) {
                                    cname = cname.substring(0, cname.length() - 1);
                                }
                            }
                            // otherwise look for A record
                            else if (record.getType() == Type.A && address == null) {
                                address = ((ARecord) record).getAddress();
                                expireTime = Math.max(
                                        (System.currentTimeMillis() + (((ARecord) record).getTTL() * 1000)),
                                        System.currentTimeMillis() + MIN_TTL_VALUE);
                            }
                        }
                    }

                    if (address != null) {
                        // LOG.info("Caching DNS Entry for Host:" + hostName);
                        // update dns cache ...
                        if (!noCache)
                            _dnsCache.cacheIPAddressForHost(hostName,
                                    IPAddressUtils.IPV4AddressToInteger(address.getAddress()), expireTime,
                                    cname);
                    }
                }
            } catch (TextParseException e) {
                resolverException = e;
            } catch (UnknownHostException e) {
                resolverException = e;
            }

            if (!retry) {
                // create result object ...
                result = new DNSQueryResult(this, client, hostName);

                if (response == null) {

                    if (resolverException != null && (resolverException instanceof TextParseException
                            || resolverException instanceof WireParseException)) {
                        result.setStatus(Status.SERVER_FAILURE);
                        result.setErrorDesc(StringUtils.stringifyException(resolverException));
                    } else {
                        result.setStatus(Status.RESOLVER_FAILURE);
                        if (resolverException != null) {
                            if (_logger != null)
                                _logger.logDNSException(hostName,
                                        StringUtils.stringifyException(resolverException));
                            result.setErrorDesc(StringUtils.stringifyException(resolverException));
                        } else {
                            if (_logger != null)
                                _logger.logDNSException(hostName, "Response was NULL");
                        }
                    }
                } else if (response.getRcode() != Rcode.NOERROR) {
                    result.setStatus(Status.SERVER_FAILURE);
                    result.setErrorDesc(Rcode.string(response.getRcode()));
                    if (_logger != null)
                        _logger.logDNSFailure(hostName, Rcode.string(response.getRcode()));
                } else if (response.getRcode() == Rcode.NOERROR) {

                    if (address != null) {

                        result.setStatus(Status.SUCCESS);
                        result.setAddress(address);
                        result.setCName(cname);
                        result.setTTL(expireTime);

                        if (_logger != null) {
                            _logger.logDNSQuery(hostName, address, expireTime, cname);
                        }

                    } else {
                        result.setStatus(Status.SERVER_FAILURE);
                        result.setErrorDesc("UNKNOWN-NO A RECORD");
                        if (_logger != null)
                            _logger.logDNSFailure(hostName, "NOERROR");
                    }
                }
            }
        }
    } while (result == null);

    // if result is server failure ... and not via bad host cache ...
    if (!resultViaCache && result.getStatus() == Status.SERVER_FAILURE) {
        if (result.getErrorDescription().equals("NXDOMAIN")) {
            _badHostCache.cacheIPAddressForHost(hostName, 0,
                    System.currentTimeMillis() + NXDOMAIN_FAIL_BAD_HOST_LIFETIME, null);
        }
    }

    // return result ... will be added to completion queue (to be retrieved via
    // poll method)...
    return result;
}