    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[] var1 = par0Str.split(":");
    040
    041            if (par0Str.startsWith("["))
    042            {
    043                int var2 = par0Str.indexOf("]");
    044
    045                if (var2 > 0)
    046                {
    047                    String var3 = par0Str.substring(1, var2);
    048                    String var4 = par0Str.substring(var2 + 1).trim();
    049
    050                    if (var4.startsWith(":") && var4.length() > 0)
    051                    {
    052                        var4 = var4.substring(1);
    053                        var1 = new String[] {var3, var4};
    054                    }
    055                    else
    056                    {
    057                        var1 = new String[] {var3};
    058                    }
    059                }
    060            }
    061
    062            if (var1.length > 2)
    063            {
    064                var1 = new String[] {par0Str};
    065            }
    066
    067            String var5 = var1[0];
    068            int var6 = var1.length > 1 ? parseIntWithDefault(var1[1], 25565) : 25565;
    069
    070            if (var6 == 25565)
    071            {
    072                String[] var7 = getServerAddress(var5);
    073                var5 = var7[0];
    074                var6 = parseIntWithDefault(var7[1], 25565);
    075            }
    076
    077            return new ServerAddress(var5, var6);
    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 var2 = new Hashtable();
    090            var2.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    091            var2.put("java.naming.provider.url", "dns:");
    092            InitialDirContext var3 = new InitialDirContext(var2);
    093            Attributes var4 = var3.getAttributes("_minecraft._tcp." + par0Str, new String[] {"SRV"});
    094            String[] var5 = var4.get("srv").get().toString().split(" ", 4);
    095            return new String[] {var5[3], var5[2]};
    096        }
    097        catch (Throwable var6)
    098        {
    099            return new String[] {par0Str, Integer.toString(25565)};
    100        }
    101    }
    102
    103    private static int parseIntWithDefault(String par0Str, int par1)
    104    {
    105        try
    106        {
    107            return Integer.parseInt(par0Str.trim());
    108        }
    109        catch (Exception var3)
    110        {
    111            return par1;
    112        }
    113    }
    114}