下面列出了怎么用org.xbill.DNS.ReverseMap的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
@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();
}
}
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;
}
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;
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
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;
}