    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.Hashtable;
    006    import javax.naming.directory.Attributes;
    007    import javax.naming.directory.InitialDirContext;
    008
    009    @SideOnly(Side.CLIENT)
    010    public 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 = func_78863_b(var5);
    073                    var5 = var7[0];
    074                    var6 = parseIntWithDefault(var7[1], 25565);
    075                }
    076
    077                return new ServerAddress(var5, var6);
    078            }
    079        }
    080
    081        private static String[] func_78863_b(String par0Str)
    082        {
    083            try
    084            {
    085                Hashtable var1 = new Hashtable();
    086                var1.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    087                var1.put("java.naming.provider.url", "dns:");
    088                InitialDirContext var2 = new InitialDirContext(var1);
    089                Attributes var3 = var2.getAttributes("_minecraft._tcp." + par0Str, new String[] {"SRV"});
    090                String[] var4 = var3.get("srv").get().toString().split(" ", 4);
    091                return new String[] {var4[3], var4[2]};
    092            }
    093            catch (Throwable var5)
    094            {
    095                return new String[] {par0Str, Integer.toString(25565)};
    096            }
    097        }
    098
    099        private static int parseIntWithDefault(String par0Str, int par1)
    100        {
    101            try
    102            {
    103                return Integer.parseInt(par0Str.trim());
    104            }
    105            catch (Exception var3)
    106            {
    107                return par1;
    108            }
    109        }
    110    }