switch getMailExchangerIPs() to DNSUtil
authoradam <adam@megacz.com>
Sat, 1 Mar 2008 23:46:15 +0000 (23:46 +0000)
committeradam <adam@megacz.com>
Sat, 1 Mar 2008 23:46:15 +0000 (23:46 +0000)
darcs-hash:20080301234615-5007d-b098d16fc86ed0b0d4b49cc35b1ec2806bcb5ec9.gz

src/org/ibex/mail/Address.java
src/org/ibex/mail/DNSUtil.java
src/org/ibex/mail/SMTP.java

index 1197a30..100cf53 100644 (file)
@@ -59,7 +59,7 @@ public class Address extends JSReflection implements Serializable {
     public boolean isLocal() {
         synchronized(local) {
             if (local.contains(host)) return true;
     public boolean isLocal() {
         synchronized(local) {
             if (local.contains(host)) return true;
-            InetAddress[] mx = SMTP.getMailExchangerIPs(host);
+            InetAddress[] mx = DNSUtil.getMailExchangerIPs(host);
             for(int i=0; i<mx.length; i++) {
                 try { if (NetworkInterface.getByInetAddress(mx[i]) != null) {
                     local.add(host);
             for(int i=0; i<mx.length; i++) {
                 try { if (NetworkInterface.getByInetAddress(mx[i]) != null) {
                     local.add(host);
index 7ad56c8..3e066c7 100644 (file)
@@ -2,8 +2,12 @@ package org.ibex.mail;
 import java.io.*;
 import java.util.*;
 import java.net.*;
 import java.io.*;
 import java.util.*;
 import java.net.*;
+import org.ibex.util.*;
+import org.ibex.net.*;
 import org.xbill.DNS.*;
 import org.xbill.DNS.Message;
 import org.xbill.DNS.*;
 import org.xbill.DNS.Message;
+import javax.naming.*;
+import javax.naming.directory.*;
 
 public class DNSUtil {
 
 
 public class DNSUtil {
 
@@ -15,4 +19,43 @@ public class DNSUtil {
         return answers.length==0 ? null : answers[0].rdataToString();
     }
 
         return answers.length==0 ? null : answers[0].rdataToString();
     }
 
+    public static InetAddress[] getMailExchangerIPs(String hostName) {
+        InetAddress[] ret;
+        try {
+            Hashtable env = new Hashtable();
+            env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
+            DirContext ictx = new InitialDirContext(env);
+            Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
+            Attribute attr = attrs.get("MX");
+            if (attr == null) {
+                ret = new InetAddress[1];
+                try {
+                    ret[0] = InetAddress.getByName(hostName);
+                    if (ret[0].equals(IP.getIP(127,0,0,1)) || ret[0].isLoopbackAddress()) throw new UnknownHostException();
+                    return ret;
+                } catch (UnknownHostException uhe) {
+                    Log.warn(DNSUtil.class, "no MX hosts or A record for " + hostName);
+                    return new InetAddress[0];
+                }
+            } else {
+                ret = new InetAddress[attr.size()];
+                NamingEnumeration ne = attr.getAll();
+                for(int i=0; ne.hasMore();) {
+                    String mx = (String)ne.next();
+                    // FIXME we should be sorting here
+                    mx = mx.substring(mx.indexOf(" ") + 1);
+                    if (mx.charAt(mx.length() - 1) == '.') mx = mx.substring(0, mx.length() - 1);
+                    InetAddress ia = InetAddress.getByName(mx);
+                    if (ia.equals(IP.getIP(127,0,0,1)) || ia.isLoopbackAddress()) continue;
+                    ret[i++] = ia;
+                }
+            }
+        } catch (Exception e) {
+            Log.warn(DNSUtil.class, "couldn't find MX host for " + hostName + " due to");
+            Log.warn(DNSUtil.class, e);
+            return new InetAddress[0];
+        }
+        return ret;
+    }
+
 }
 }
index df480ac..db5da88 100644 (file)
@@ -11,8 +11,6 @@ import java.net.*;
 import java.io.*;
 import java.util.*;
 import java.text.*;
 import java.io.*;
 import java.util.*;
 import java.text.*;
-import javax.naming.*;
-import javax.naming.directory.*;
 
 // FIXME: inbound throttling/ratelimiting
 
 
 // FIXME: inbound throttling/ratelimiting
 
@@ -335,7 +333,7 @@ public class SMTP {
                 Log.warn(SMTP.Outgoing.class, "aieeee, null envelopeTo: " + m.summary());
                 return false;
             }
                 Log.warn(SMTP.Outgoing.class, "aieeee, null envelopeTo: " + m.summary());
                 return false;
             }
-            InetAddress[] mx = getMailExchangerIPs(m.envelopeTo.host);
+            InetAddress[] mx = DNSUtil.getMailExchangerIPs(m.envelopeTo.host);
             if (mx.length == 0) {
                if (!noBounces) {
                    enqueue(m.bounce("could not resolve " + m.envelopeTo.host));
             if (mx.length == 0) {
                if (!noBounces) {
                    enqueue(m.bounce("could not resolve " + m.envelopeTo.host));
@@ -516,42 +514,4 @@ public class SMTP {
         }
     }
 
         }
     }
 
-    public static InetAddress[] getMailExchangerIPs(String hostName) {
-        InetAddress[] ret;
-        try {
-            Hashtable env = new Hashtable();
-            env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
-            DirContext ictx = new InitialDirContext(env);
-            Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
-            Attribute attr = attrs.get("MX");
-            if (attr == null) {
-                ret = new InetAddress[1];
-                try {
-                    ret[0] = InetAddress.getByName(hostName);
-                    if (ret[0].equals(IP.getIP(127,0,0,1)) || ret[0].isLoopbackAddress()) throw new UnknownHostException();
-                    return ret;
-                } catch (UnknownHostException uhe) {
-                    Log.warn(SMTP.class, "no MX hosts or A record for " + hostName);
-                    return new InetAddress[0];
-                }
-            } else {
-                ret = new InetAddress[attr.size()];
-                NamingEnumeration ne = attr.getAll();
-                for(int i=0; ne.hasMore();) {
-                    String mx = (String)ne.next();
-                    // FIXME we should be sorting here
-                    mx = mx.substring(mx.indexOf(" ") + 1);
-                    if (mx.charAt(mx.length() - 1) == '.') mx = mx.substring(0, mx.length() - 1);
-                    InetAddress ia = InetAddress.getByName(mx);
-                    if (ia.equals(IP.getIP(127,0,0,1)) || ia.isLoopbackAddress()) continue;
-                    ret[i++] = ia;
-                }
-            }
-        } catch (Exception e) {
-            Log.warn(SMTP.class, "couldn't find MX host for " + hostName + " due to");
-            Log.warn(SMTP.class, e);
-            return new InetAddress[0];
-        }
-        return ret;
-    }
 }
 }