Exemplo n.º 1
0
        public TcpClientBouncy(bool secure)
        {
            if (TcpClientUtil.OSSupportsIPv4())
            {
                _client = new System.Net.Sockets.TcpClient(System.Net.Sockets.AddressFamily.InterNetwork);
            }
            else if (TcpClientUtil.OSSupportsIPv6())
            {
                _client = new System.Net.Sockets.TcpClient(System.Net.Sockets.AddressFamily.InterNetworkV6);
            }
            _client.NoDelay = true;
            _secure         = secure;
            _readBuffer     = new byte[4 * 1024];
            _readOffset     = 0;
            _readCount      = 0;

            ++RefCount;
            //Debug.Log("TcpClients: Construct " + RefCount);
        }
Exemplo n.º 2
0
        public static IPAddress[] Lookup(string hostname)
        {
            IPAddress addr;

            if (IPAddress.TryParse(hostname, out addr))
            {
                return(ParseIpAddress(addr));
            }

            // safe read
            IPAddress[] addres = GetAddresses(hostname);
            if (addres != null)
            {
                return(addres);
            }

            // try to get the host entries
            List <IPAddress> addresses = new List <IPAddress>();

            try
            {
                var async = DnsBase.BeginGetHostAddresses(hostname, null, null);
                if (async.AsyncWaitHandle.WaitOne(System.TimeSpan.FromSeconds(5)) && async.IsCompleted)
                {
                    var tmp = DnsBase.EndGetHostAddresses(async);
                    if (tmp != null)
                    {
                        // shuffle
                        if (tmp.Length > 1)
                        {
                            System.Random rand = new System.Random();
                            for (int i = 0; i < tmp.Length; ++i)
                            {
                                var t = tmp[i];
                                int r = rand.Next(i, tmp.Length);
                                tmp[i] = tmp[r];
                                tmp[r] = t;
                            }
                        }


                        // support ipv6
                        if (TcpClientUtil.OSSupportsIPv6())
                        {
                            foreach (var ip in tmp)
                            {
                                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                                {
                                    addresses.Add(ip);
                                }
                            }
                        }

                        // ipv4
                        if (TcpClientUtil.OSSupportsIPv4())
                        {
                            foreach (var ip in tmp)
                            {
                                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                                {
                                    //Debug.Log("addr " + ip);
                                    addresses.Add(ip);
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                // failure,
                EB.Debug.LogError("failed to lookup " + hostname + " " + ex);
            }

            // fall back
            if (addresses.Count == 0 && s_fallbackDNS.ContainsKey(hostname))
            {
                EB.Debug.LogWarning("failed to lookup {0}, use fall back dns", hostname);
                addresses.AddRange(s_fallbackDNS[hostname]);
                return(addresses.ToArray());
            }

            // not found
            if (addresses.Count == 0)
            {
                EB.Debug.LogError("failed to lookup {0}", hostname);
                return(new IPAddress[0]);
            }

            // safe update
            Entry e = GetEntry(hostname);

            lock (e)
            {
                e.addresses = addresses.ToArray();
                // keep last in first
                if (e.last != null)
                {
                    var index = System.Array.IndexOf(e.addresses, e.last);
                    if (index >= 0)
                    {
                        // move to the front
                        var tmp = e.addresses[0];
                        e.addresses[0]     = e.addresses[index];
                        e.addresses[index] = tmp;
                    }
                }
                else
                {
                    e.last = e.addresses[0];
                }

                return((IPAddress[])e.addresses.Clone());
            }
        }