📄 dns.java
字号:
/**************************************************************** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * * * Copyright 2008 Jun Li(SiChuan University, the School of * * Software Engineering). All rights reserved. * * * * Licensed to the JMS under one or more contributor license * * agreements. See the LICENCE file distributed with this * * work for additional information regarding copyright * * ownership. The JMS licenses this file you may not use this * * file except in compliance with the License. * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.jpxx.mail.Dns;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.ArrayList;import org.apache.log4j.Logger;import org.jpxx.mail.Factory;import org.xbill.DNS.*;/** * Resolve an mx record from a dns server. * The dnsjava library is used as the resolver this class is a simple wrapper * * @author Jun Li * @version $Revision: 0.0.1 $, $Date: 2008/04/25 8:16:00 $ * */public class DNS { /** * Creates an instance of Logger and initializes it. * It is to write log for <code>DNS</code>. */ private static Logger log = Factory.getSingletonInstance().getLogger(DNS.class); /** * Given a domain will return all the mailservers used to send * mail to the given domain. * Need to order the list by preference listed in the dns server. * This works by using the dnsjava library as the resolver. * * @param domain The destnation server domain(or IP) * @return all the mailservers used to send mail to the given domain. */ public static synchronized ArrayList getMailServer(String domain) { ArrayList servers = new ArrayList(); Record foundServers[]; try { foundServers = new Lookup(domain, Type.MX).run(); /** * Put the servers into the server list */ if (foundServers != null) { for (int i = 0; i < foundServers.length; i++) { MXRecord m = (MXRecord) foundServers[i]; /** * Need to add in order of preference */ servers.add(m.getTarget().toString()); } } } catch (Exception e) { log.error(e.getMessage()); } return servers; } /** * Determines the IP address of a host, given the host's name. * * <p> The host name can either be a machine name, such as * "<code>java.sun.com</code>", or a textual representation of its * IP address. If a literal IP address is supplied, only the * validity of the address format is checked. * * <p> For <code>host</code> specified in literal IPv6 address, * either the form defined in RFC 2732 or the literal IPv6 address * format defined in RFC 2373 is accepted. IPv6 scoped addresses are also * supported. * * <p> If the host is <tt>null</tt> then an <tt>InetAddress</tt> * representing an address of the loopback interface is returned. * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC 3330</a> * section 2 and <a href="http://www.ietf.org/rfc/rfc2373.txt"> * RFC 2373</a> * section 2.5.3. </p> * * @param host the specified host, or <code>null</code>. * @return an IP address for the given host name. * @exception UnknownHostException if no IP address for the * <code>host</code> could be found, or if a scope_id was * specified for a global IPv6 address. * @exception SecurityException if a security manager exists * and its checkConnect method doesn't allow the operation */ public static InetAddress getByName(String host) throws UnknownHostException { if (host == null || host.trim().equals("")) { return null; } if (host.endsWith(".")) { host = host.substring(0, host.length() - 1); } return InetAddress.getByName(host); } /** * * @param host * @return * @throws org.xbill.DNS.TextParseException */ public static String[] getHostIP(String host) throws TextParseException { Record record[] = new Lookup(host, Type.A).run(); if (record == null) { return null; } String ip[] = new String[record.length]; for (int i = 0; i < record.length; i++) { ip[i] = record[i].rdataToString(); } return ip; } /** * * @param host * @return * @throws org.xbill.DNS.TextParseException */ public static String[] getMXHostIP(String host) throws TextParseException { ArrayList result = new ArrayList(); ArrayList list = getMailServer(host); for (int i = 0; i < list.size(); i++) { String hostIP[] = getHostIP(list.get(i).toString()); if (hostIP != null) { for (int j = 0; j < hostIP.length; j++) { result.add(hostIP[j]); } } } if (result.size() == 0) { return null; } else { String ip[] = new String[result.size()]; result.toArray(ip); return ip; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -