I'm developing a function that queries a list of DNS servers using DNSJava library to check if a specific domain is blacklisted. Here you have the portion of code that make the check. At the end you will find the output of the function; for the test i used a domain that seems to be blacklisten on black.uribl.com: buyapprove.com
In this for i make one query for each blacklist
String mailBlacklistServers[]={"dnsbl.sorbs.net","multi.uribl.com","dbl.spamhaus.org", "multi.surbl.com","bl.spamcop.net"};
boolean blacklisted;
boolean blacklistedFinalResult=false;;
String tempBlacklistedOn="";
for(int i=0;i<mailBlacklistServers.length;i++)
{
blacklisted=checkMailBlacklist(thisWhoAPIRequest.getWebsite().getWebsiteURLstr(),mailBlacklistServers[i]);
if(blacklisted==true)
{
blacklistedFinalResult=true;
if(tempBlacklistedOn.isEmpty())
tempBlacklistedOn=mailBlacklistServers[i];
else
tempBlacklistedOn=tempBlacklistedOn+" "+mailBlacklistServers[i];
}
}
This function performs the check (buyapprove.com is hardcoded)
private boolean checkMailBlacklist(String url, String servAddr)
{
String res=new String("buyapprove.com");
res=res.replace("http://", "");
res=res.replace("www.", "");
String dnsblDomain = servAddr;
Lookup lookup;
try
{
System.out.println("checkMailBlacklist, Lookup Parameters: "+res+"."+servAddr);
lookup = new Lookup(res+"."+servAddr, Type.ANY);
Resolver resolver = new SimpleResolver();
lookup.setResolver(resolver);
lookup.setCache(null);
Record[] records = lookup.run();
if(lookup.getResult() == Lookup.SUCCESSFUL)
{
String responseMessage = null;
String listingType = null;
for (int i = 0; i < records.length; i++)
{
if(records[i] instanceof TXTRecord)
{
TXTRecord txt = (TXTRecord) records[i];
for(Iterator j = txt.getStrings().iterator(); j.hasNext();)
{
responseMessage += (String)j.next();
}
}
else if(records[i] instanceof ARecord)
{
listingType = ((ARecord)records[i]).getAddress().getHostAddress();
}
}
System.out.println("checkMailBlacklist, lookup done: \n"+listingType+"\n"+responseMessage+" fonte: "+servAddr+" sito:"+res);
if(listingType==null)
return false;
else
return true;
}
else if(lookup.getResult() == Lookup.HOST_NOT_FOUND)
{
System.out.println("checkMailBlacklist, lookup bad: HOST_NOT_FOUND");
return false;
}
else
{
System.out.println("checkMailBlacklist, lookup bad: error Lookup="+lookup.getResult());
return false;
}
} catch (TextParseException e) {
System.out.println("Exc TextParseException in checkMailBlacklist");
e.printStackTrace();
} catch (UnknownHostException e) {
System.out.println("Exc UnknownHostException in checkMailBlacklist");
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
buyapprove.com is blacklisten on spamhaus but not in multi.uribl.com (setting black.uribl.com changes nothing). The output for multi.uribl.com is
checkMailBlacklist, lookup done: null null fonte: multi.uribl.com sito:buyapprove.com
i don't get any exception, so the query is done correctly. For spamhaus i get a positive
checkMailBlacklist, lookup done: 127.0.1.2 null http://www.spamhaus.org/query/dbl domain=buyapprove.com fonte: dbl.spamhaus.org sito:buyapprove.com
Am i doing something wrong?
EDIT: if record[i] if not an instance of TXTRecord or ARecord, i've added a else record[i].toString(). That's the output for uribl
checkMailBlacklist, Lookup Parameters: buyapprove.com.multi.uribl.com
Stampa extra del record: buyapprove.com.multi.uribl.com.fastwebnet.it. 28635 IN MX 10 mx2.fastwebnet.it.
Stampa extra del record: buyapprove.com.multi.uribl.com.fastwebnet.it. 28635 IN MX 10 mx4.fastwebnet.it.
Stampa extra del record: buyapprove.com.multi.uribl.com.fastwebnet.it. 28635 IN MX 10 mx3.fastwebnet.it.
Stampa extra del record: buyapprove.com.multi.uribl.com.fastwebnet.it. 28635 IN MX 10 mx1.fastwebnet.it.
checkMailBlacklist, lookup done: null null fonte: multi.uribl.com sito:buyapprove.com
Your codes look right. It could be that your
if/else if
statement didn't account for the other sub-classes ofRecord
. Try replacewith