package org.cnhawk;
import org.xbill.DNS.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
public class LookDNSMX{
private Resolver resolver;
private Cache cache;
private int DNSCredibility;
private static LookDNSMX instance=null;
private Collection DNSServers = new Vector();
//构造函数
public LookDNSMX() throws Exception{
this.initialize();
}
public static LookDNSMX getInstance() throws Exception{
if(instance==null)
instance=new LookDNSMX();
return instance;
}
//初始化
public void initialize() throws Exception {
DNSServers.add("10.0.1.1");
final String serversArray[] = (String[])DNSServers.toArray(new String[0]);
System.out.println("Current DNS server is : "+serversArray[0].toString());
try {
resolver = new ExtendedResolver(serversArray );
} catch (UnknownHostException uhe) {
throw uhe;
}
cache = new Cache (DClass.IN);
}
//主方法
public synchronized Collection findMXRecords(String hostname)throws Exception {
Record DNSAnswers[] =null;
Collection MXServers = new Vector();
//调用查看DNS的记录的方法,返回一个Record记录数组
DNSAnswers=this.rawDNSLookup(hostname,false,Type.MX);
if (DNSAnswers == null) {
MXServers.add(hostname);
System.out.println(hostname+" MX result is "+hostname+"\r");
return MXServers;
}
MXRecord MXAnswers[] = new MXRecord[DNSAnswers.length];
for (int i = 0; i < DNSAnswers.length; i++) {
MXAnswers[i] = (MXRecord)DNSAnswers[i];
}
Comparator prioritySort = new Comparator () {
public int compare (Object a, Object b) {
MXRecord ma = (MXRecord)a;
MXRecord mb = (MXRecord)b;
return ma.getPriority () - mb.getPriority ();
}
};
Arrays.sort(MXAnswers, prioritySort);
System.out.println("\n\n"+hostname+ " MX result is: ");
String mxstr="";
for (int i = 0; i < MXAnswers.length; i++) {
mxstr=MXAnswers[i].getTarget ().toString ().trim();
MXServers.add(mxstr.substring(0,mxstr.length()-1));
}
//System.out.println(hostname+ " MX result is \n"+mxstr);
return MXServers;
}
/**
* 根据给定的类型和主机名去查看DNS的记录
* 返回一个Record记录数组
*
* @param hostname 被查看的主机名
* @param querysent 这个查询是否已经被发送到DNS 服务器了
* @param type 是要得到的记录类型
*/
private Record[] rawDNSLookup(String hostname, boolean querysent, int type) {
try{
Name name = Name.fromString(hostname, Name.root);
short dclass = DClass.IN;
Record [] DNSAnswers=null;
SetResponse ResponseCached = cache.lookupRecords(name, type, DNSCredibility);
if (ResponseCached.isSuccessful()) {
return getDNSAnswers(ResponseCached);
}
else if (ResponseCached.isNXDOMAIN() || ResponseCached.isNXRRSET() || querysent) {
return null;
}
else
{
Record question = Record.newRecord(name, type, dclass);
org.xbill.DNS.Message query = org.xbill.DNS.Message.newQuery(question);
org.xbill.DNS.Message DNSresponse;
try {
DNSresponse = resolver.send(query);
}catch (Throwable e) {
e.printStackTrace();
return null;
}
int rcode = DNSresponse.getHeader().getRcode();
if (rcode == Rcode.NOERROR || rcode == Rcode.NXDOMAIN) {
ResponseCached = cache.addMessage(DNSresponse);
return getDNSAnswers(ResponseCached);
}
if (rcode != Rcode.NOERROR) {
return null;
}
}
return DNSAnswers;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
private Record[] getDNSAnswers(SetResponse responseCached)
{
Record [] DNSAnswers=null;
try
{
int answerCount = 0;
int n = 0;
RRset [] rrsets = responseCached.answers();
for (int i = 0; i < rrsets.length; i++) {
answerCount += rrsets[i].size();
}
DNSAnswers = new Record[answerCount];
Iterator itor = null;
for (int i = 0; i < rrsets.length; i++) {
itor = rrsets[i].rrs();
while (itor.hasNext()) {
DNSAnswers[n++]= (Record)itor.next();
}
}
}catch (Exception e){
e.printStackTrace();
return null;
}
return DNSAnswers;
}
//测试方法
public static void main(String args[]){
try{
LookDNSMX client= new LookDNSMX();
Collection col=client.findMXRecords("sina.com");
Iterator it=col.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
col=client.findMXRecords("handuancn.com");
it=col.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}catch(Exception e){
e.printStackTrace();
}
}
}