public Socket Accept()
        {
            byte[] response = new byte[262];

            if (_socket.Receive(response) < 10)
            {
                throw new SocksClientException("The connection was reset by the remote peer.");
            }

            if (response[0] != SocksClient.SOCKS_VERSION)
            {
                throw new SocksClientException("Socks version 5 is not supported by the proxy server.");
            }

            SocksReplyCode reply = (SocksReplyCode)response[1];

            if (reply != SocksReplyCode.Succeeded)
            {
                throw new SocksClientException("Socks proxy server request failed: " + reply.ToString());
            }

            _dstEP = SocksClient.ParseEndpoint(response, 3);

            return(_socket);
        }
Example #2
0
        public int ReceiveFrom(byte[] buffer, int offset, int size, out EndPoint remoteEP)
        {
            byte[]   datagram = new byte[262 + size];
            EndPoint dummyEP  = new IPEndPoint(IPAddress.Any, 0);

            int bytesReceived = _udpSocket.ReceiveFrom(datagram, 0, datagram.Length, SocketFlags.None, ref dummyEP);

            if (bytesReceived < 10)
            {
                throw new SocksClientException("The connection was reset by the remote peer.");
            }

            remoteEP = SocksClient.ParseEndpoint(datagram, 3);

            int addressSize;

            switch (remoteEP.AddressFamily)
            {
            case AddressFamily.InterNetwork:
                addressSize = 4;
                break;

            case AddressFamily.InterNetworkV6:
                addressSize = 16;
                break;

            case AddressFamily.Unspecified:
                addressSize = 1 + (remoteEP as DomainEndPoint).Address.Length;
                break;

            default:
                throw new NotSupportedException("AddressFamily not supported.");
            }

            int dataOffset = 6 + addressSize;
            int dataSize   = bytesReceived - dataOffset;

            if (dataSize > size)
            {
                dataSize = size;
            }

            Buffer.BlockCopy(datagram, dataOffset, buffer, offset, dataSize);

            return(dataSize);
        }