类org.xbill.DNS.ReverseMap源码实例Demo

下面列出了怎么用org.xbill.DNS.ReverseMap的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: openvisualtraceroute   文件: DNSLookupService.java
/**
 * Dns lookup more efficient than the INetAddress.getHostName(ip)
 *
 * @param hostIp
 * @return
 * @throws IOException
 */
public String dnsLookup(final String hostIp) {
	try {
		final Name name = ReverseMap.fromAddress(hostIp);
		final int type = Type.PTR;
		final int dclass = DClass.IN;
		final Record rec = Record.newRecord(name, type, dclass);
		final Message query = Message.newQuery(rec);

		final Message response = _resolver.send(query);

		final Record[] answers = response.getSectionArray(Section.ANSWER);
		if (answers.length > 0) {
			String ret = answers[0].rdataToString();
			if (ret.endsWith(".")) {
				ret = ret.substring(0, ret.length() - 1);
			}
			return ret;
		}
	} catch (final IOException e) {
		LOGGER.warn("Failed to resolve hostname for " + hostIp, e);
	}
	return UNKNOWN_HOST;
}
 
源代码2 项目: james-project   文件: DNSJavaService.java
@Override
public String getHostName(InetAddress addr) {
    TimeMetric timeMetric = metricFactory.timer("getHostName");
    String result;
    Name name = ReverseMap.fromAddress(addr);
    Record[] records = lookupNoException(name.toString(), Type.PTR, "PTR");

    try {
        if (records == null) {
            result = addr.getHostAddress();
        } else {
            PTRRecord ptr = (PTRRecord) records[0];
            result = ptr.getTarget().toString();
        }
        return result;
    } finally {
        timeMetric.stopAndPublish();
    }
}
 
源代码3 项目: open-rmbt   文件: Helperfunctions.java
public static String reverseDNSLookup(final InetAddress adr)
{
    try
    {
        final Name name = ReverseMap.fromAddress(adr);
        
        final Lookup lookup = new Lookup(name, Type.PTR);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof PTRRecord)
                {
                    final PTRRecord ptr = (PTRRecord) record;
                    return ptr.getTarget().toString();
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}
 
源代码4 项目: megatron-java   文件: IpAddressUtil.java
private static String reverseDnsLookupUsingDnsJavaSimpleResolver(long ipAddress) throws IOException {
    String result = null;
    byte[] address = convertLongAddressToBuf(ipAddress);
    Name name = ReverseMap.fromAddress(InetAddress.getByAddress(address));
    Record record = Record.newRecord(name, Type.PTR, DClass.IN);
    Message query = Message.newQuery(record);
    Message response = simpleResolver.send(query);
    Record[] answers = response.getSectionArray(Section.ANSWER);
    if (answers.length != 0) {
        // If PTR-record exists this will be at index 1 or above (more than one PTR-record may exist)
        Record answer = (answers.length > 1) ? answers[1] : answers[0];  
        result = answer.rdataToString();
        // remove trailing "."
        result = result.endsWith(".") ? result.substring(0, result.length() - 1) : result;
    } else {
        throw new IOException("Empty DNS response.");
    }
    return result;
}
 
源代码5 项目: Virtual-Hosts   文件: DNSJavaNameService.java
/**
 * Performs a reverse DNS lookup.
 * @param addr The ip address to lookup.
 * @return The host name found for the ip address.
 */
public String
getHostByAddr(byte [] addr) throws UnknownHostException {
	Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));
	Record [] records = new Lookup(name, Type.PTR).run();
	if (records == null)
		throw new UnknownHostException();
	return ((PTRRecord) records[0]).getTarget().toString();
}
 
源代码6 项目: AndroidPNClient   文件: DNSJavaNameService.java
/**
 * Performs a reverse DNS lookup.
 * @param addr The ip address to lookup.
 * @return The host name found for the ip address.
 */
public String
getHostByAddr(byte [] addr) throws UnknownHostException {
	Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));
	Record [] records = new Lookup(name, Type.PTR).run();
	if (records == null)
		throw new UnknownHostException();
	return ((PTRRecord) records[0]).getTarget().toString();
}
 
源代码7 项目: dnsjava   文件: DNSJavaNameService.java
/**
 * Performs a reverse DNS lookup.
 *
 * @param addr The ip address to lookup.
 * @return The host name found for the ip address.
 */
public String getHostByAddr(byte[] addr) throws UnknownHostException {
  Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));
  Record[] records = new Lookup(name, Type.PTR).run();
  if (records == null) {
    throw new UnknownHostException("Unknown address: " + name);
  }
  return ((PTRRecord) records[0]).getTarget().toString();
}
 
源代码8 项目: megatron-java   文件: IpAddressUtil.java
private static String reverseDnsLookupUsingDnsJavaExtendedResolver(long ipAddress) throws UnknownHostException {
    byte[] address = convertLongAddressToBuf(ipAddress);
    Name name = ReverseMap.fromAddress(InetAddress.getByAddress(address));
    Record[] records = new Lookup(name, Type.PTR).run();
    if (records == null) {
        throw new UnknownHostException();
    }
    String result = ((PTRRecord)records[0]).getTarget().toString();
    // remove trailing "."
    result = result.endsWith(".") ? result.substring(0, result.length() - 1) : result;
    return result;
}