Exemplo n.º 1
0
        public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
        {
            this.sock   = sock;

            this.epl    = epl;

            this.secure = secure;

            this.key    = key;

            var networkstream = new NetworkStream(sock, false);

            if (secure)
            {
                var sslstream = new System.Net.Security.SslStream(networkstream);

                sslstream.AuthenticateAsServer(cert);

                stream  = sslstream;
            }
            else
            {
                stream = networkstream;
            }

            timer = new Timer(OnTimeout, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);

            if (buffer == null)
            {
                buffer = new byte[BufferSize];
            }

            Init();
        }
Exemplo n.º 2
0
 public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate cert)
 {
     this.sock   = sock;
     this.epl    = epl;
     this.secure = secure;
     this.cert   = cert;
     if (secure == false)
     {
         stream = new NetworkStream(sock, false);
     }
     else
     {
         ssl_stream = epl.Listener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) => {
             if (c == null)
             {
                 return(true);
             }
             var c2 = c as X509Certificate2;
             if (c2 == null)
             {
                 c2 = new X509Certificate2(c.GetRawCertData());
             }
             client_cert        = c2;
             client_cert_errors = new int[] { (int)e };
             return(true);
         });
         stream = ssl_stream.AuthenticatedStream;
     }
     timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
     Init();
 }
Exemplo n.º 3
0
        static void ProcessAccept(SocketAsyncEventArgs args)
        {
            Socket accepted = null;

            if (args.SocketError == SocketError.Success)
            {
                accepted = args.AcceptSocket;
            }

            EndPointListener epl = (EndPointListener)args.UserToken;


            Accept(epl.sock, args, ref accepted);
            if (accepted == null)
            {
                return;
            }

            if (epl.secure && epl.cert == null)
            {
                accepted.Close();
                return;
            }
            HttpConnection conn = new HttpConnection(accepted, epl, epl.secure, epl.cert);

            lock (epl.unregistered) {
                epl.unregistered [conn] = conn;
            }
            conn.BeginReadRequest();
        }
Exemplo n.º 4
0
 public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
 {
     lock (ip_to_endpoints) {
         Dictionary <int, EndPointListener> p = null;
         p = ip_to_endpoints [ep.Address];
         p.Remove(ep.Port);
         epl.Close();
     }
 }
Exemplo n.º 5
0
        private static void RemovePrefixInternal(string prefix, HttpListener listener)
        {
            ListenerPrefix listenerPrefix = new ListenerPrefix(prefix);

            if (listenerPrefix.Path.IndexOf('%') == -1 && listenerPrefix.Path.IndexOf("//") == -1)
            {
                EndPointListener ePListener = GetEPListener(IPAddress.Any, listenerPrefix.Port, listener, listenerPrefix.Secure);
                ePListener.RemovePrefix(listenerPrefix, listener);
            }
        }
Exemplo n.º 6
0
        static EndPointListener GetEpListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;

            if (host == "*")
            {
                addr = IPAddress.Any;
            }
            else if (IPAddress.TryParse(host, out addr) == false)
            {
                try
                {
                    var iphost = new IPHostEntry
                    {
                        HostName    = host,
                        AddressList = Dns.GetHostAddressesAsync(host).Result
                    };

                    addr = iphost.AddressList[0];
                }
                catch
                {
                    addr = IPAddress.Any;
                }
            }
            Hashtable p = null;  // Dictionary<int, EndPointListener>

            if (_ipToEndpoints.ContainsKey(addr))
            {
                p = (Hashtable)_ipToEndpoints[addr];
            }
            else
            {
                p = new Hashtable();
                _ipToEndpoints[addr] = p;
            }

            EndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p[port];
            }
            else
            {
                epl     = new EndPointListener(listener, addr, port, secure);
                p[port] = epl;
            }

            return(epl);
        }
Exemplo n.º 7
0
 public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
 {
     lock (ip_to_endpoints)
     {
         Hashtable hashtable = null;
         hashtable = (Hashtable)ip_to_endpoints[ep.Address];
         hashtable.Remove(ep.Port);
         if (hashtable.Count == 0)
         {
             ip_to_endpoints.Remove(ep.Address);
         }
         epl.Close();
     }
 }
Exemplo n.º 8
0
        static EndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;

            if (host == "*")
            {
                addr = IPAddress.Any;
            }
            else if (IPAddress.TryParse(host, out addr) == false)
            {
                try {
                    IPHostEntry iphost = Dns.GetHostByName(host);
                    if (iphost != null)
                    {
                        addr = iphost.AddressList[0];
                    }
                    else
                    {
                        addr = IPAddress.Any;
                    }
                } catch {
                    addr = IPAddress.Any;
                }
            }
            Hashtable p = null;              // Dictionary<int, EndPointListener>

            if (ip_to_endpoints.ContainsKey(addr))
            {
                p = (Hashtable)ip_to_endpoints [addr];
            }
            else
            {
                p = new Hashtable();
                ip_to_endpoints [addr] = p;
            }

            EndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p [port];
            }
            else
            {
                epl      = new EndPointListener(addr, port, secure);
                p [port] = epl;
            }

            return(epl);
        }
Exemplo n.º 9
0
 public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
 {
     lock (ip_to_endpoints) {
         // Dictionary<int, EndPointListener> p
         Hashtable p = null;
         p = (Hashtable)ip_to_endpoints [ep.Address];
         p.Remove(ep.Port);
         if (p.Count == 0)
         {
             ip_to_endpoints.Remove(ep.Address);
         }
         epl.Close();
     }
 }
Exemplo n.º 10
0
        public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
        {
            Hashtable obj = EndPointManager.ip_to_endpoints;

            lock (obj)
            {
                Hashtable hashtable = (Hashtable)EndPointManager.ip_to_endpoints[ep.Address];
                hashtable.Remove(ep.Port);
                if (hashtable.Count == 0)
                {
                    EndPointManager.ip_to_endpoints.Remove(ep.Address);
                }
                epl.Close();
            }
        }
Exemplo n.º 11
0
		public HttpConnection (Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
		{
			this.sock = sock;
			this.epl = epl;
			this.secure = secure;
			this.key = key;
			if (secure == false) {
				stream = new NetworkStream (sock, false);
			} else {
				SslServerStream ssl_stream = new SslServerStream (new NetworkStream (sock, false), cert, false, false);
				ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
				stream = ssl_stream;
			}
			timer = new Timer (OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
			Init ();
		}
Exemplo n.º 12
0
        private static void RemovePrefixInternal(string prefix, HttpListener listener)
        {
            ListenerPrefix listenerPrefix = new ListenerPrefix(prefix);

            if (listenerPrefix.Path.IndexOf('%') != -1)
            {
                return;
            }
            if (listenerPrefix.Path.IndexOf("//") != -1)
            {
                return;
            }
            EndPointListener eplistener = EndPointManager.GetEPListener(IPAddress.Any, listenerPrefix.Port, listener, listenerPrefix.Secure);

            eplistener.RemovePrefix(listenerPrefix, listener);
        }
Exemplo n.º 13
0
        private static void AddPrefixInternal(string p, HttpListener listener)
        {
            ListenerPrefix listenerPrefix = new ListenerPrefix(p);

            if (listenerPrefix.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }
            if (listenerPrefix.Path.IndexOf("//") != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }
            EndPointListener eplistener = EndPointManager.GetEPListener(IPAddress.Any, listenerPrefix.Port, listener, listenerPrefix.Secure);

            eplistener.AddPrefix(listenerPrefix, listener);
        }
        static void OnAccept(object sender, EventArgs e)
        {
            SocketAsyncEventArgs args = (SocketAsyncEventArgs)e;
            EndPointListener     epl  = (EndPointListener)args.UserToken;
            Socket accepted           = null;

            if (args.SocketError == SocketError.Success)
            {
                accepted          = args.AcceptSocket;
                args.AcceptSocket = null;
            }

            try
            {
                if (epl.sock != null)
                {
                    epl.sock.AcceptAsync(args);
                }
            }
            catch
            {
                if (accepted != null)
                {
                    try
                    {
                        accepted.Close();
                    }
                    catch {}
                    accepted = null;
                }
            }

            if (accepted == null)
            {
                return;
            }

            if (epl.secure && (epl.cert == null || epl.key == null))
            {
                accepted.Close();
                return;
            }
            HttpConnection conn = new HttpConnection(accepted, epl, epl.secure, epl.cert, epl.key);

            epl.unregistered [conn] = conn;
            conn.BeginReadRequest();
        }
Exemplo n.º 15
0
        private static void OnAccept(IAsyncResult ares)
        {
            EndPointListener endPointListener = (EndPointListener)ares.AsyncState;

            System.Net.Sockets.Socket socket = null;
            try
            {
                socket = endPointListener.sock.EndAccept(ares);
            }
            catch
            {
            }
            finally
            {
                try
                {
                    endPointListener.sock.BeginAccept(new AsyncCallback(EndPointListener.OnAccept), endPointListener);
                }
                catch
                {
                    if (socket != null)
                    {
                        try
                        {
                            socket.Close();
                        }
                        catch
                        {
                        }
                        socket = null;
                    }
                }
            }
            if (socket == null)
            {
                return;
            }
            if (endPointListener.secure && (endPointListener.cert == null || endPointListener.key == null))
            {
                socket.Close();
                return;
            }
            HttpConnection httpConnection = new HttpConnection(socket, endPointListener, endPointListener.secure, endPointListener.cert, endPointListener.key);

            httpConnection.BeginReadRequest();
        }
Exemplo n.º 16
0
 public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
 {
     this.sock   = sock;
     this.epl    = epl;
     this.secure = secure;
     this.key    = key;
     if (secure == false)
     {
         stream = new NetworkStream(sock, false);
     }
     else
     {
         SslServerStream ssl_stream = new SslServerStream(new NetworkStream(sock, false), cert, false, false);
         ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
         stream = ssl_stream;
     }
     Init();
 }
Exemplo n.º 17
0
        static void RemovePrefixInternal(string prefix, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(prefix);

            if (lp.Path.IndexOf('%') != -1)
            {
                return;
            }

            if (lp.Path.IndexOf("//") != -1)
            {
                return;
            }

            EndPointListener epl = GetEPListener(IPAddress.Any, lp.Port, listener, lp.Secure);

            epl.RemovePrefix(lp, listener);
        }
Exemplo n.º 18
0
        static void RemovePrefixInternal(string prefix, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(prefix);

            if (lp.Path.IndexOf('%') != -1)
            {
                return;
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                return;
            }

            EndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.RemovePrefix(lp, listener);
        }
Exemplo n.º 19
0
 public HttpConnection(System.Net.Sockets.Socket sock, EndPointListener epl, bool secure, System.Security.Cryptography.X509Certificates.X509Certificate2 cert, AsymmetricAlgorithm key)
 {
     this.sock   = sock;
     this.epl    = epl;
     this.secure = secure;
     this.key    = key;
     if (!secure)
     {
         this.stream = new System.Net.Sockets.NetworkStream(sock, false);
     }
     else
     {
         SslServerStream sslServerStream  = new SslServerStream(new System.Net.Sockets.NetworkStream(sock, false), cert, false, false);
         SslServerStream sslServerStream2 = sslServerStream;
         sslServerStream2.PrivateKeyCertSelectionDelegate = (PrivateKeySelectionCallback)Delegate.Combine(sslServerStream2.PrivateKeyCertSelectionDelegate, new PrivateKeySelectionCallback(this.OnPVKSelection));
         this.stream = sslServerStream;
     }
     this.Init();
 }
Exemplo n.º 20
0
 public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
 {
     this.sock   = sock;
     this.epl    = epl;
     this.secure = secure;
     this.key    = key;
     if (!secure)
     {
         stream = new NetworkStream(sock, owns_socket: false);
     }
     else
     {
         SslServerStream sslServerStream  = new SslServerStream(new NetworkStream(sock, owns_socket: false), cert, clientCertificateRequired: false, ownsStream: false);
         SslServerStream sslServerStream2 = sslServerStream;
         sslServerStream2.PrivateKeyCertSelectionDelegate = (PrivateKeySelectionCallback)Delegate.Combine(sslServerStream2.PrivateKeyCertSelectionDelegate, new PrivateKeySelectionCallback(OnPVKSelection));
         stream = sslServerStream;
     }
     Init();
 }
Exemplo n.º 21
0
		public HttpConnection (Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
		{
			this.sock = sock;
			this.epl = epl;
			this.secure = secure;
			this.key = key;
			if (secure == false) {
				stream = new NetworkStream (sock, false);
			} else {
#if EMBEDDED_IN_1_0
				throw new NotImplementedException ();
#else
				SslServerStream ssl_stream = new SslServerStream (new NetworkStream (sock, false), cert, false, false);
				ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
				stream = ssl_stream;
#endif
			}
			Init ();
		}
Exemplo n.º 22
0
        static void AddPrefixInternal(string p, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)              // TODO: Code?
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            // listens on all the interfaces if host name cannot be parsed by IPAddress.
            EndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.AddPrefix(lp, listener);
        }
Exemplo n.º 23
0
        static void AddPrefixInternal(string p, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            if (lp.Path.IndexOf("//") != -1)              // TODO: Code?
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            // Always listens on all the interfaces, no matter the host name/ip used.
            EndPointListener epl = GetEPListener(IPAddress.Any, lp.Port, listener, lp.Secure);

            epl.AddPrefix(lp, listener);
        }
Exemplo n.º 24
0
 public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
 {
     this.sock   = sock;
     this.epl    = epl;
     this.secure = secure;
     this.key    = key;
     if (secure == false)
     {
         stream = new NetworkStream(sock, false);
     }
     else
     {
         SslServerStream ssl_stream = new SslServerStream(new NetworkStream(sock, false), cert, false, true, false);
         ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
         ssl_stream.ClientCertValidationDelegate    += OnClientCertificateValidation;
         stream = ssl_stream;
     }
     timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
     Init();
 }
Exemplo n.º 25
0
        static void OnAccept(IAsyncResult ares)
        {
            EndPointListener epl      = (EndPointListener)ares.AsyncState;
            Socket           accepted = null;

            try {
                accepted = epl.sock.EndAccept(ares);
            } catch {
                // Anything to do here?
            } finally {
                try {
                    epl.sock.BeginAccept(OnAccept, epl);
                } catch {
                    if (accepted != null)
                    {
                        try {
                            accepted.Close();
                        } catch {}
                        accepted = null;
                    }
                }
            }

            if (accepted == null)
            {
                return;
            }

            if (epl.secure && (epl.cert == null || epl.key == null))
            {
                accepted.Close();
                return;
            }
            HttpConnection conn = new HttpConnection(accepted, epl, epl.secure, epl.cert, epl.key);

            conn.BeginReadRequest();
        }
Exemplo n.º 26
0
		public HttpConnection (Socket sock, EndPointListener epl, bool secure, X509Certificate cert)
		{
			this.sock = sock;
			this.epl = epl;
			this.secure = secure;
			this.cert = cert;
			if (secure == false) {
				stream = new NetworkStream (sock, false);
			} else {
				ssl_stream = epl.Listener.CreateSslStream (new NetworkStream (sock, false), false, (t, c, ch, e) => {
					if (c == null)
						return true;
					var c2 = c as X509Certificate2;
					if (c2 == null)
						c2 = new X509Certificate2 (c.GetRawCertData ());
					client_cert = c2;
					client_cert_errors = new int[] { (int)e };
					return true;
				});
				stream = ssl_stream.AuthenticatedStream;
			}
			timer = new Timer (OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
			Init ();
		}
Exemplo n.º 27
0
        public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate cert)
        {
            _sock    = sock;
            _epl     = epl;
            IsSecure = secure;
            _cert    = cert;
            if (secure == false)
            {
                _stream = new NetworkStream(sock, false);
            }
            else
            {
#if SSL
                ssl_stream = epl.Listener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) =>
                {
                    if (c == null)
                    {
                        return(true);
                    }
                    var c2 = c as X509Certificate2;
                    if (c2 == null)
                    {
                        c2 = new X509Certificate2(c.GetRawCertData());
                    }
                    client_cert        = c2;
                    client_cert_errors = new int[] { (int)e };
                    return(true);
                });
                stream = ssl_stream.AuthenticatedStream;
#else
                throw new Exception("SSL is not supported");
#endif
            }
            _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init();
        }
Exemplo n.º 28
0
		static EndPointListener GetEPListener (string host, int port, HttpListener listener, bool secure)
		{
			IPAddress addr;
			if (host == "*")
				addr = IPAddress.Any;
			else if (IPAddress.TryParse(host, out addr) == false){
				try {
					IPHostEntry iphost = Dns.GetHostByName(host);
					if (iphost != null)
						addr = iphost.AddressList[0];
					else
						addr = IPAddress.Any;
				} catch {
					addr = IPAddress.Any;
				} 
			}
			Hashtable p = null;  // Dictionary<int, EndPointListener>
			if (ip_to_endpoints.ContainsKey (addr)) {
				p = (Hashtable) ip_to_endpoints [addr];
			} else {
				p = new Hashtable ();
				ip_to_endpoints [addr] = p;
			}

			EndPointListener epl = null;
			if (p.ContainsKey (port)) {
				epl = (EndPointListener) p [port];
			} else {
				epl = new EndPointListener (listener, addr, port, secure);
				p [port] = epl;
			}

			return epl;
		}
		static EndPointListener GetEPListener (IPAddress addr, int port, HttpListener listener, bool secure)
		{
			Hashtable p = null;  // Dictionary<int, EndPointListener>
			if (ip_to_endpoints.ContainsKey (addr)) {
				p = (Hashtable) ip_to_endpoints [addr];
			} else {
				p = new Hashtable ();
				ip_to_endpoints [addr] = p;
			}

			EndPointListener epl = null;
			if (p.ContainsKey (port)) {
				epl = (EndPointListener) p [port];
			} else {
				epl = new EndPointListener (addr, port, secure);
				p [port] = epl;
			}

			return epl;
		}
Exemplo n.º 30
0
		static EndPointListener GetEPListener (string host, int port, HttpListener listener, bool secure)
		{
			IPAddress addr;
			if (IPAddress.TryParse(host, out addr) == false)
				addr = IPAddress.Any;

			Hashtable p = null;  // Dictionary<int, EndPointListener>
			if (ip_to_endpoints.ContainsKey (addr)) {
				p = (Hashtable) ip_to_endpoints [addr];
			} else {
				p = new Hashtable ();
				ip_to_endpoints [addr] = p;
			}

			EndPointListener epl = null;
			if (p.ContainsKey (port)) {
				epl = (EndPointListener) p [port];
			} else {
				epl = new EndPointListener (addr, port, secure);
				p [port] = epl;
			}

			return epl;
		}
Exemplo n.º 31
0
		public static void RemoveEndPoint (EndPointListener epl, IPEndPoint ep)
		{
			lock (ip_to_endpoints) {
				// Dictionary<int, EndPointListener> p
				Hashtable p = null;
				p = (Hashtable) ip_to_endpoints [ep.Address];
				p.Remove (ep.Port);
				if (p.Count == 0) {
					ip_to_endpoints.Remove (ep.Address);
				}
				epl.Close ();
			}
		}