public BindHandler(AddressFamily family)
            {
                EndPoint    localEP     = null;
                NetworkInfo networkInfo = null;

                switch (family)
                {
                case AddressFamily.InterNetwork:
                    localEP     = new IPEndPoint(IPAddress.Any, 0);
                    networkInfo = NetUtilities.GetDefaultIPv4NetworkInfo();
                    break;

                case AddressFamily.InterNetworkV6:
                    localEP     = new IPEndPoint(IPAddress.IPv6Any, 0);
                    networkInfo = NetUtilities.GetDefaultIPv6NetworkInfo();
                    break;

                default:
                    _replyCode = SocksProxyReplyCode.AddressTypeNotSupported;
                    _bindEP    = new IPEndPoint(IPAddress.Any, 0);
                    break;
                }

                if (localEP != null)
                {
                    if (networkInfo == null)
                    {
                        _replyCode = SocksProxyReplyCode.NetworkUnreachable;
                        _bindEP    = new IPEndPoint(IPAddress.Any, 0);
                    }
                    else
                    {
                        _socket = new Socket(localEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                        _socket.Bind(localEP);
                        _socket.Listen(1);
                        _socket.NoDelay = true;

                        _replyCode = SocksProxyReplyCode.Succeeded;
                        _bindEP    = new IPEndPoint(networkInfo.LocalIP, (_socket.LocalEndPoint as IPEndPoint).Port);
                    }
                }
            }
Пример #2
0
        public DnsClient(bool preferIPv6 = false)
        {
            _preferIPv6 = preferIPv6;

            NetworkInfo defaultNetworkInfo;

            if (_preferIPv6)
            {
                defaultNetworkInfo = NetUtilities.GetDefaultIPv6NetworkInfo();

                if ((defaultNetworkInfo == null) || (defaultNetworkInfo.Interface.GetIPProperties().DnsAddresses.Count == 0))
                {
                    defaultNetworkInfo = NetUtilities.GetDefaultIPv4NetworkInfo();
                }
            }
            else
            {
                defaultNetworkInfo = NetUtilities.GetDefaultIPv4NetworkInfo();
            }

            if (defaultNetworkInfo == null)
            {
                throw new DnsClientException("No default network connection was found on this computer.");
            }

            IPAddressCollection servers = defaultNetworkInfo.Interface.GetIPProperties().DnsAddresses;

            if (servers.Count == 0)
            {
                throw new DnsClientException("Default network does not have any DNS server configured.");
            }

            _servers = new NameServerAddress[servers.Count];

            for (int i = 0; i < servers.Count; i++)
            {
                _servers[i] = new NameServerAddress(servers[i]);
            }
        }