    001package net.minecraft.client.multiplayer;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.Hashtable;
    006import javax.naming.directory.Attributes;
    007import javax.naming.directory.InitialDirContext;
    008
    009@SideOnly(Side.CLIENT)
    010public class ServerAddress
    011{
    012    private final String ipAddress;
    013    private final int serverPort;
    014
    015    private ServerAddress(String par1Str, int par2)
    016    {
    017        this.ipAddress = par1Str;
    018        this.serverPort = par2;
    019    }
    020
    021    public String getIP()
    022    {
    023        return this.ipAddress;
    024    }
    025
    026    public int getPort()
    027    {
    028        return this.serverPort;
    029    }
    030
    031    public static ServerAddress func_78860_a(String par0Str)
    032    {
    033        if (par0Str == null)
    034        {
    035            return null;
    036        }
    037        else
    038        {
    039            String[] astring = par0Str.split(":");
    040
    041            if (par0Str.startsWith("["))
    042            {
    043                int i = par0Str.indexOf("]");
    044
    045                if (i > 0)
    046                {
    047                    String s1 = par0Str.substring(1, i);
    048                    String s2 = par0Str.substring(i + 1).trim();
    049
    050                    if (s2.startsWith(":") && s2.length() > 0)
    051                    {
    052                        s2 = s2.substring(1);
    053                        astring = new String[] {s1, s2};
    054                    }
    055                    else
    056                    {
    057                        astring = new String[] {s1};
    058                    }
    059                }
    060            }
    061
    062            if (astring.length > 2)
    063            {
    064                astring = new String[] {par0Str};
    065            }
    066
    067            String s3 = astring[0];
    068            int j = astring.length > 1 ? parseIntWithDefault(astring[1], 25565) : 25565;
    069
    070            if (j == 25565)
    071            {
    072                String[] astring1 = getServerAddress(s3);
    073                s3 = astring1[0];
    074                j = parseIntWithDefault(astring1[1], 25565);
    075            }
    076
    077            return new ServerAddress(s3, j);
    078        }
    079    }
    080
    081    /**
    082     * Returns a server's address and port for the specified hostname, looking up the SRV record if possible
    083     */
    084    private static String[] getServerAddress(String par0Str)
    085    {
    086        try
    087        {
    088            Class.forName("com.sun.jndi.dns.DnsContextFactory");
    089            Hashtable hashtable = new Hashtable();
    090            hashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    091            hashtable.put("java.naming.provider.url", "dns:");
    092            hashtable.put("com.sun.jndi.dns.timeout.retries", "1");
    093            InitialDirContext initialdircontext = new InitialDirContext(hashtable);
    094            Attributes attributes = initialdircontext.getAttributes("_minecraft._tcp." + par0Str, new String[] {"SRV"});
    095            String[] astring = attributes.get("srv").get().toString().split(" ", 4);
    096            return new String[] {astring[3], astring[2]};
    097        }
    098        catch (Throwable throwable)
    099        {
    100            return new String[] {par0Str, Integer.toString(25565)};
    101        }
    102    }
    103
    104    private static int parseIntWithDefault(String par0Str, int par1)
    105    {
    106        try
    107        {
    108            return Integer.parseInt(par0Str.trim());
    109        }
    110        catch (Exception exception)
    111        {
    112            return par1;
    113        }
    114    }
    115}