public void Test_Ops()
        {
            SocketWatcher w = new SocketWatcher(20);
            Address a = new Address("127.0.0.1", 7002);
            a.Resolve();
            AsyncSocket one = w.CreateListenSocket(this, a);
            AsyncSocket two = null;
            AsyncSocket three = one;
            AsyncSocket four = two;
            Assert.IsTrue(one == three);
            Assert.IsTrue(two == four);
            Assert.IsTrue(one >= three);
            Assert.IsTrue(two >= four);
            Assert.IsTrue(one <= three);
            Assert.IsTrue(two <= four);
            Assert.IsTrue(one != two);
            Assert.IsTrue(two != one);
            Assert.IsTrue(one > two);
            Assert.IsTrue(one >= two);
            Assert.IsTrue(two < one);
            Assert.IsTrue(two <= one);

            two = w.CreateListenSocket(this, a);
            four = two;
            Assert.IsTrue(one == three);
            Assert.IsTrue(two == four);
            Assert.IsTrue(one >= three);
            Assert.IsTrue(two >= four);
            Assert.IsTrue(one <= three);
            Assert.IsTrue(two <= four);
            Assert.IsTrue(one != two);
            Assert.IsTrue(two != one);

            int c = ((IComparable)one).CompareTo(two);
            Assert.IsTrue(c != 0);
            if (c == -1)
            {
                // one less than two
                Assert.IsTrue(one < two);
                Assert.IsTrue(one <= two);
                Assert.IsTrue(two > one);
                Assert.IsTrue(two >= one);
            }
            else if (c == 1)
            {
                // one greater than two
                Assert.IsTrue(one > two);
                Assert.IsTrue(one >= two);
                Assert.IsTrue(two < one);
                Assert.IsTrue(two <= one);
            }
            else
            {
                Assert.IsTrue(false);
            }
            one.Close();
            two.Close();
        }
        [Test] public void Test_Write()
        {
            SocketWatcher w = new SocketWatcher(20);
            Address a = new Address("127.0.0.1", 7001);
            a.Resolve();

            AsyncSocket listen = w.CreateListenSocket(this, a);
            listen.RequestAccept();

            AsyncSocket connect;
            lock (done)
            {
                connect = w.CreateConnectSocket(this, a);
                bool NoTimeout = Monitor.Wait(done, new TimeSpan(0, 0, 30));

                Assert.IsTrue(NoTimeout, "The read command didn't complete in time.");
                Assert.IsTrue(succeeded, errorMessage);
            }

            Assert.AreEqual("5678901234", success);
            connect.Close();
            listen.Close();
        }
Example #3
0
 /// <summary>
 /// Create an outbound socket.
 /// </summary>
 /// <param name="listener">Where to send notifications</param>
 /// <param name="addr">Address to connect to</param>
 /// <returns>Socket that is in the process of connecting</returns>
 public AsyncSocket CreateConnectSocket(ISocketEventListener listener,
                                        Address              addr)
 {
     return CreateConnectSocket(listener, addr, false, null);
 }
Example #4
0
 /// <summary>
 /// Create a socket that is listening for inbound connections, with no SSL/TLS.
 /// </summary>
 /// <param name="listener">Where to send notifications</param>
 /// <param name="addr">Address to connect to</param>
 /// <param name="backlog">The maximum length of the queue of pending connections</param>
 /// <returns>A socket that is ready for calling RequestAccept()</returns>
 public AsyncSocket CreateListenSocket(ISocketEventListener listener,
                                       Address              addr,
                                       int                  backlog)
 {
     return CreateListenSocket(listener, addr, backlog, false);
 }
Example #5
0
 /// <summary>
 /// Create a socket that is listening for inbound connections, with no SSL/TLS.
 /// </summary>
 /// <param name="listener">Where to send notifications</param>
 /// <param name="addr">Address to connect to</param>
 /// <returns>A socket that is ready for calling RequestAccept()</returns>
 public AsyncSocket CreateListenSocket(ISocketEventListener listener,
     Address              addr)
 {
     return CreateListenSocket(listener, addr, 5, false);
 }
Example #6
0
 /// <summary>
 /// Outbound connection.  Eventually calls Listener.OnConnect() when
 /// the connection comes up.  Don't forget to call RequestRead() in
 /// OnConnect()!
 /// </summary>
 /// <param name="addr"></param>
 public override void Connect(Address addr)
 {
     // Debug.WriteLine("starting connect to " + addr.ToString());
     State = SocketState.Resolving;
     if (m_synch)
     {
         addr.Resolve();
         OnConnectResolved(addr);
     }
     else
     {
         addr.Resolve(new AddressResolved(OnConnectResolved));
     }
 }
Example #7
0
 /// <summary>
 /// Prepare to start accepting inbound requests.  Call
 /// RequestAccept() to start the async process.
 /// </summary>
 /// <param name="addr">Address to listen on</param>
 /// <param name="backlog">The Maximum length of the queue of
 /// pending connections</param>
 public abstract void Accept(Address addr, int backlog);
Example #8
0
 /// <summary>
 /// Outbound connection.  Eventually calls Listener.OnConnect() when
 /// the connection comes up.  Don't forget to call RequestRead() in
 /// OnConnect()!
 /// </summary>
 /// <param name="addr"></param>
 public abstract void Connect(Address addr);
        /// <summary>
        /// Listens for an inbound connection.
        /// </summary>
        public override void Accept()
        {
            if (m_accept == null)
            {
                m_accept = new AsyncSocket(null, this, (bool)m_listener[Options.SSL], false);
                ((AsyncSocket)m_accept).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as
                    System.Security.Cryptography.X509Certificates.X509Certificate2;

                Address addr = new Address((string)m_listener[Options.NETWORK_HOST],
                    (int)m_listener[Options.PORT]);

                m_accept.Accept(addr);
            }
            m_accept.RequestAccept();
        }
Example #10
0
        /// <summary>
        /// Generally should not be used.
        /// </summary>
        /// <param name="uri"></param>
        internal void Connect(Uri uri)
        {
            m_keepRunning = true;

            if (Connected)
                return;

            m_ssl = (uri != null) && (uri.Scheme == "https");
            m_host = uri.Host;
            if (m_proxyURI != null)
            {
                // TODO: add CONNECT support here.  ShttpProxy?
                if (m_ssl)
                    throw new InvalidOperationException("Can't do SSL through proxies yet.");
                uri = m_proxyURI;
            }
            m_addr = new Address(uri.Host, uri.Port);
            Connect();
        }
Example #11
0
        /// <summary>
        /// Start polling
        /// </summary>
        /// <param name="addr">Ignored in this case.  Set URL.</param>
        public override void Connect(Address addr)
        {
            Debug.Assert(m_uri != null);

            m_rid = -1L;
            m_lastSock = null;
            m_running = false;

            // Create new ones each time, in case the URL has changed or something.
            m_sockA = new HttpSocket(this);
            m_sockB = new HttpSocket(this);

            m_sockA.Name = "A";
            m_sockB.Name = "B";

            m_sockA.ProxyURI = m_sockB.ProxyURI = m_proxyURI;
            m_sockA.ProxyCredentials = m_sockB.ProxyCredentials = m_proxyCredentials;

            m_sockA.Connect(m_uri);
            m_sockB.Connect(m_uri);
        }
Example #12
0
 /// <summary>
 /// Accept a socket.  Not implemented.
 /// </summary>
 /// <param name="addr"></param>
 /// <param name="backlog"></param>
 public override void Accept(Address addr, int backlog)
 {
     throw new NotImplementedException("HTTP binding server not implemented");
 }
Example #13
0
 /// <summary>
 /// Saves the address passed in, and really connects to m_host:m_port.
 /// </summary>
 /// <param name="addr"></param>
 public override void Connect(Kixeye.Jabber.Net.Address addr)
 {
     m_remote_addr = addr; // save this till we are ready for it...
     Debug.Assert(m_host != null);
     Debug.Assert(m_port != 0);
     // connect to the proxy.
     Address proxy_addr = new Address(m_host, m_port);
     m_sock.Connect(proxy_addr, m_hostid);
     // we'll end up in OnConnected below.
 }
Example #14
0
        /// <summary>
        /// Address resolution finished.  Try connecting.
        /// </summary>
        /// <param name="addr"></param>
        private void OnConnectResolved(Address addr)
        {
            // Debug.WriteLine("connectresolved: " + addr.ToString());
            lock (this)
            {
                if (State != SocketState.Resolving)
                {
                    // closed in the mean time.   Probably not an error.
                    return;
                }
                if ((addr == null) || (addr.IP == null) || (addr.Endpoint == null))
                {
                    FireError(new AsyncSocketConnectionException("Bad host: " + addr.Hostname));
                    return;
                }


                if (m_watcher != null)
                    m_watcher.RegisterSocket(this);

                m_addr = addr;
                State = SocketState.Connecting;

                if (Socket.OSSupportsIPv6 && (m_addr.Endpoint.AddressFamily == AddressFamily.InterNetworkV6))
                {
                    // Debug.WriteLine("ipv6");
                    m_sock = new Socket(AddressFamily.InterNetworkV6,
                        SocketType.Stream,
                        ProtocolType.Tcp);
                }
                else
                {
                    // Debug.WriteLine("ipv4");
                    m_sock = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream,
                        ProtocolType.Tcp);
                }

                // well, of course this isn't right.
                m_sock.SetSocketOption(SocketOptionLevel.Socket,
                    SocketOptionName.ReceiveBuffer,
                    4 * m_buf.Length);
            }

            if (m_synch)
            {
                try
                {
                    m_sock.Connect(m_addr.Endpoint);
                }
                catch (SocketException ex)
                {
                    FireError(ex);
                    return;
                }

                if (m_sock.Connected)
                {
                    // TODO: check to see if this Mono bug is still valid
#if __MonoCS__
                    m_sock.Blocking = true;
                    m_stream = new NetworkStream(m_sock);
                    m_sock.Blocking = false;
#else
                    m_stream = new NetworkStream(m_sock);
#endif
                    if (m_secureProtocol != SslProtocols.None)
                        StartTLS();

                    lock (this)
                    {
                        State = SocketState.Connected;
                    }
                    m_listener.OnConnect(this);
                }
                else
                {
                    AsyncClose();
                    FireError(new AsyncSocketConnectionException("could not connect"));
                }
            }
            else
            {
#if __MonoCS__
                m_sock.Blocking = false;
#endif
                m_sock.BeginConnect(m_addr.Endpoint, new AsyncCallback(ExecuteConnect), null);
            }
        }
Example #15
0
        /// <summary>
        /// Create an outbound socket.
        /// </summary>
        /// <param name="listener">Where to send notifications</param>
        /// <param name="addr">Address to connect to</param>
        /// <param name="SSL">Do SSL3/TLS1 on startup</param>
        /// <param name="hostId">The logical name of the host to connect to, for SSL/TLS purposes.</param>
        /// <returns>Socket that is in the process of connecting</returns>
        public AsyncSocket CreateConnectSocket(ISocketEventListener listener,
                                               Address              addr,
                                               bool                 SSL,
                                               string               hostId)
        {
            AsyncSocket result;

            // Create the socket:
            result = new AsyncSocket(this, listener, SSL, m_synch);
            if (SSL)
                result.LocalCertificate = m_cert;

            // Start the connect process:
            result.Connect(addr, hostId);
            return result;
        }
Example #16
0
 /// <summary>
 /// Not implemented.
 /// </summary>
 /// <param name="addr"></param>
 /// <param name="backlog"></param>
 public override void Accept(Address addr, int backlog)
 {
     throw new Exception("The method or operation is not implemented.");
 }
        /// <summary>
        /// Connects to the XMPP server.
        /// </summary>
        public override void Connect()
        {
            m_elements = null;
            int port = (int)m_listener[Options.PORT];
            Debug.Assert(port > 0);
            //m_sslOn = m_ssl;

            ProxySocket proxy = null;
            ProxyType pt = (ProxyType)m_listener[Options.PROXY_TYPE];
            switch (pt)
            {
            case ProxyType.Socks4:
                proxy = new Socks4Proxy(this);
                break;

            case ProxyType.Socks5:
                proxy = new Socks5Proxy(this);
                break;

            case ProxyType.HTTP:
                proxy = new ShttpProxy(this);
                break;

            case ProxyType.None:
                m_sock = new AsyncSocket(null, this, (bool)m_listener[Options.SSL], false);

                ((AsyncSocket)m_sock).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as
                    System.Security.Cryptography.X509Certificates.X509Certificate2;

                ((AsyncSocket)m_sock).CertificateGui = (bool)m_listener[Options.CERTIFICATE_GUI];
                break;

            default:
                throw new ArgumentException("no handler for proxy type: " + pt, "ProxyType");
            }

            if (proxy != null)
            {
                proxy.Socket = new AsyncSocket(null, proxy, (bool)m_listener[Options.SSL], false);
                ((AsyncSocket)proxy.Socket).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as
                    System.Security.Cryptography.X509Certificates.X509Certificate2;

                proxy.Host = m_listener[Options.PROXY_HOST] as string;
                proxy.Port = (int)m_listener[Options.PROXY_PORT];
                proxy.Username = m_listener[Options.PROXY_USER] as string;
                proxy.Password = m_listener[Options.PROXY_PW] as string;
                m_sock = proxy;
            }

            string to = (string)m_listener[Options.TO];
            Debug.Assert(to != null);

            string host = (string)m_listener[Options.NETWORK_HOST];
            if ((host == null) || (host == ""))
            {
#if __MonoCS__
                host = to;
#else
                try
                {
                    Address.LookupSRV((string)m_listener[Options.SRV_PREFIX], to, ref host, ref port);
                }
                catch
                {
                    Debug.WriteLine("WARNING: netlib.Dns.dll missing");
                    host = to;
                }
#endif
            }

            Address addr = new Address(host, port);
            m_sock.Connect(addr, (string)m_listener[Options.SERVER_ID]);
        }
Example #18
0
 /// <summary>
 /// Not implemented.
 /// </summary>
 /// <param name="addr"></param>
 public override void Connect(Address addr)
 {
     throw new Exception("The method or operation is not implemented.");
 }
Example #19
0
        public void Test_Full_Response()
        {
            SocketWatcher watcher = new SocketWatcher();
            Address a = new Address("127.0.0.1", 7002);
            a.Resolve();

            ServerListener server = new ServerListener();
            AsyncSocket server_sock = watcher.CreateListenSocket(server, a);
            server_sock.RequestAccept();

            ResponseListener resp = new ResponseListener();
            HttpSocket sock = new HttpSocket(resp);

            Uri u = new Uri("http://localhost:7002/");
            byte[] buf = ENC.GetBytes("11111");
            HttpSocket s = (HttpSocket)sock;
            s.Execute("GET", u, buf, 0, buf.Length, "text/plain");
            resp.Event.WaitOne();
            Assert.AreEqual("1234567890", resp.Last);

            resp.Last = null;
            buf = ENC.GetBytes("22222");
            s.Execute("GET", u, buf, 0, buf.Length, "text/plain");
            resp.Event.WaitOne();
            Assert.AreEqual("1234567890", resp.Last);

            resp.Last = null;
            buf = ENC.GetBytes("33333");
            s.Execute("GET", u, buf, 0, buf.Length, "text/plain");
            resp.Event.WaitOne();
            Assert.AreEqual("12345678901234567890", resp.Last);
        }
Example #20
0
 /// <summary>
 /// Create a socket that is listening for inbound connections.
 /// </summary>
 /// <param name="listener">Where to send notifications</param>
 /// <param name="addr">Address to connect to</param>
 /// <param name="backlog">The maximum length of the queue of pending connections</param>
 /// <param name="SSL">Do SSL3/TLS1 on connect</param>
 /// <returns>A socket that is ready for calling RequestAccept()</returns>
 public AsyncSocket CreateListenSocket(ISocketEventListener listener,
                                       Address              addr,
                                       int                  backlog,
                                       bool                 SSL)
 {
     //Debug.Assert(m_maxSocks > 1);
     AsyncSocket result = new AsyncSocket(this, listener, SSL, m_synch);
     if (SSL)
     {
         result.LocalCertificate = m_cert;
         result.RequireClientCert = m_requireClientCert;
     }
     result.Accept(addr, backlog);
     return result;
 }
Example #21
0
 /// <summary>
 /// Prepare to start accepting inbound requests.  Call
 /// RequestAccept() to start the async process.
 /// Default the listen queue size to 5.
 /// </summary>
 /// <param name="addr">Address to listen on</param>
 public void Accept(Address addr)
 {
     Accept(addr, 5);
 }
Example #22
0
 /// <summary>
 /// Create a socket that is listening for inbound connections.
 /// </summary>
 /// <param name="listener">Where to send notifications</param>
 /// <param name="addr">Address to connect to</param>
 /// <param name="SSL">Do SSL3/TLS1 on connect</param>
 /// <returns>A socket that is ready for calling RequestAccept()</returns>
 public AsyncSocket CreateListenSocket(ISocketEventListener listener,
                                       Address              addr,
                                       bool                 SSL)
 {
     return CreateListenSocket(listener, addr, 5, SSL);
 }
Example #23
0
 /// <summary>
 /// Outbound connection.  Eventually calls Listener.OnConnect() when
 /// the connection comes up.  Don't forget to call RequestRead() in
 /// OnConnect()!
 /// </summary>
 /// <param name="addr">Address/hostname to connect to</param>
 /// <param name="hostIdentity">Identity of the host we're
 /// connecting to.  Used for SSL validation, this is the name
 /// of the SRV we looked up, for example.</param>
 public void Connect(Address addr, string hostIdentity)
 {
     m_hostid = hostIdentity;
     Connect(addr);
 }
Example #24
0
        /// <summary>
        /// Prepare to start accepting inbound requests.  Call
        /// RequestAccept() to start the async process.
        /// </summary>
        /// <param name="addr">Address to listen on</param>
        /// <param name="backlog">The Maximum length of the queue of
        /// pending connections</param>
        public override void Accept(Address addr, int backlog)
        {
            lock (this)
            {
                m_addr = addr;

                m_sock = new Socket(AddressFamily.InterNetwork,
                                    SocketType.Stream,
                                    ProtocolType.Tcp);

                // Always reuse address.
                m_sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                m_sock.Bind(m_addr.Endpoint);
                m_sock.Listen(backlog);
                State = SocketState.Listening;

                if (m_watcher != null)
                    m_watcher.RegisterSocket(this);
            }
        }