예제 #1
0
        internal TcpAcceptor(
            TcpEndpoint endpoint,
            Communicator communicator,
            string host,
            ushort port,
            string adapterName)
        {
            _adapterName  = adapterName;
            _endpoint     = endpoint;
            _communicator = communicator;
            _backlog      = communicator.GetPropertyAsInt("Ice.TCP.Backlog") ?? 511;

            try
            {
                int ipVersion = _communicator.IPVersion;
                _addr = Network.GetAddressForServerEndpoint(host, port, ipVersion, _communicator.PreferIPv6);
                _fd   = Network.CreateServerSocket(false, _addr.AddressFamily, ipVersion);
                Network.SetBlock(_fd, false);
                Network.SetTcpBufSize(_fd, _communicator);
            }
            catch (Exception)
            {
                _fd = null;
                throw;
            }
        }
예제 #2
0
        public StreamSocket(Communicator communicator, Socket fd)
        {
            _communicator = communicator;
            _fd           = fd;
            _state        = StateConnected;

            try
            {
                _desc = Network.FdToString(_fd);
            }
            catch (Exception)
            {
                Network.CloseSocketNoThrow(_fd);
                throw;
            }

            Network.SetBlock(_fd, false);
            Network.SetTcpBufSize(_fd, _communicator);

            _readEventArgs            = new SocketAsyncEventArgs();
            _readEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            _writeEventArgs            = new SocketAsyncEventArgs();
            _writeEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            //
            // For timeouts to work properly, we need to receive/send
            // the data in several chunks. Otherwise, we would only be
            // notified when all the data is received/written. The
            // connection timeout could easily be triggered when
            // receiving/sending large frames.
            //
            _maxSendPacketSize = Math.Max(512, Network.GetSendBufferSize(_fd));
            _maxRecvPacketSize = Math.Max(512, Network.GetRecvBufferSize(_fd));
        }
예제 #3
0
        public StreamSocket(Communicator communicator, INetworkProxy?proxy, EndPoint addr, IPAddress?sourceAddr)
        {
            _communicator = communicator;
            _proxy        = proxy;
            _addr         = addr;
            _sourceAddr   = sourceAddr;
            _fd           = Network.CreateSocket(false, (_proxy != null ? _proxy.GetAddress() : _addr).AddressFamily);
            _state        = StateNeedConnect;

            Network.SetBlock(_fd, false);
            Network.SetTcpBufSize(_fd, _communicator);

            _readEventArgs            = new SocketAsyncEventArgs();
            _readEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            _writeEventArgs            = new SocketAsyncEventArgs();
            _writeEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            //
            // For timeouts to work properly, we need to receive/send
            // the data in several chunks. Otherwise, we would only be
            // notified when all the data is received/written. The
            // connection timeout could easily be triggered when
            // receiving/sending large frames.
            //
            _maxSendPacketSize = Math.Max(512, Network.GetSendBufferSize(_fd));
            _maxRecvPacketSize = Math.Max(512, Network.GetRecvBufferSize(_fd));
        }
예제 #4
0
        public int Initialize(ref ArraySegment <byte> readBuffer, IList <ArraySegment <byte> > writeBuffer)
        {
            if (!_isConnected)
            {
                int status = _delegate.Initialize(ref readBuffer, writeBuffer);
                if (status != SocketOperation.None)
                {
                    return(status);
                }
                _isConnected = true;
            }

            if (SslStream == null)
            {
                try
                {
                    Socket?fd = _delegate.Fd();
                    Debug.Assert(fd != null);

                    Network.SetBlock(fd, true); // SSL requires a blocking socket

                    // For timeouts to work properly, we need to receive/send the data in several chunks. Otherwise,
                    // we would only be notified when all the data is received/written. The connection timeout could
                    // easily be triggered when receiving/sending large frames.
                    _maxSendPacketSize = Math.Max(512, Network.GetSendBufferSize(fd));
                    _maxRecvPacketSize = Math.Max(512, Network.GetRecvBufferSize(fd));

                    if (_incoming)
                    {
                        SslStream = new SslStream(
                            new NetworkStream(fd, false),
                            false,
                            _engine.TlsServerOptions.ClientCertificateValidationCallback ??
                            RemoteCertificateValidationCallback);
                    }
                    else
                    {
                        SslStream = new SslStream(
                            new NetworkStream(fd, false),
                            false,
                            _engine.TlsClientOptions.ServerCertificateValidationCallback ??
                            RemoteCertificateValidationCallback,
                            _engine.TlsClientOptions.ClientCertificateSelectionCallback ??
                            CertificateSelectionCallback);
                    }
                }
                catch (Exception ex)
                {
                    if (ex is IOException ioException && Network.ConnectionLost(ioException))
                    {
                        throw new ConnectionLostException(ex);
                    }
                    else
                    {
                        throw new TransportException(ex);
                    }
                }
예제 #5
0
        //
        // Only for use by UdpConnector.
        //
        internal UdpTransceiver(Communicator communicator, string transport, EndPoint addr, IPAddress?sourceAddr,
                                string mcastInterface, int mcastTtl)
        {
            _communicator = communicator;
            Transport     = transport;
            _addr         = addr;
            if (sourceAddr != null)
            {
                _sourceAddr = new IPEndPoint(sourceAddr, 0);
            }

            _readEventArgs = new SocketAsyncEventArgs();
            _readEventArgs.RemoteEndPoint = _addr;
            _readEventArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            _writeEventArgs = new SocketAsyncEventArgs();
            _writeEventArgs.RemoteEndPoint = _addr;
            _writeEventArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            _mcastInterface = mcastInterface;
            _state          = StateNeedConnect;
            _incoming       = false;

            try
            {
                _fd = Network.CreateSocket(true, _addr.AddressFamily);
                SetBufSize(-1, -1);
                Network.SetBlock(_fd, false);
                if (Network.IsMulticast((IPEndPoint)_addr))
                {
                    if (_mcastInterface.Length > 0)
                    {
                        Network.SetMcastInterface(_fd, _mcastInterface, _addr.AddressFamily);
                    }
                    if (mcastTtl != -1)
                    {
                        Network.SetMcastTtl(_fd, mcastTtl, _addr.AddressFamily);
                    }
                }
            }
            catch (System.Exception)
            {
                _fd = null;
                throw;
            }
        }
예제 #6
0
        internal TcpAcceptor(TcpEndpoint endpoint, ObjectAdapter adapter)
        {
            _adapter = adapter;

            _addr = Network.GetAddressForServerEndpoint(endpoint.Host,
                                                        endpoint.Port,
                                                        endpoint.Communicator.IPVersion,
                                                        endpoint.Communicator.PreferIPv6);

            _fd = Network.CreateServerSocket(false, _addr.AddressFamily, endpoint.Communicator.IPVersion);
            Network.SetBlock(_fd, false);
            Network.SetTcpBufSize(_fd, endpoint.Communicator);

            _addr = Network.DoBind(_fd, _addr);
            Network.DoListen(_fd, endpoint.Communicator.GetPropertyAsInt("Ice.TCP.Backlog") ?? 511);

            Endpoint = endpoint.NewPort((ushort)_addr.Port);
        }
예제 #7
0
        //
        // Only for use by UdpEndpoint.
        //
        internal UdpTransceiver(UdpEndpoint endpoint, Communicator communicator, string transport, string host,
                                int port, string mcastInterface, bool connect)
        {
            _endpoint       = endpoint;
            _communicator   = communicator;
            Transport       = transport;
            _state          = connect ? StateNeedConnect : StateNotConnected;
            _mcastInterface = mcastInterface;
            _incoming       = true;
            _port           = port;

            try
            {
                _addr = Network.GetAddressForServerEndpoint(host, port, communicator.IPVersion, communicator.PreferIPv6);

                _readEventArgs = new SocketAsyncEventArgs();
                _readEventArgs.RemoteEndPoint = _addr;
                _readEventArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

                _writeEventArgs = new SocketAsyncEventArgs();
                _writeEventArgs.RemoteEndPoint = _addr;
                _writeEventArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

                _fd = Network.CreateServerSocket(true, _addr.AddressFamily, communicator.IPVersion);
                SetBufSize(-1, -1);
                Network.SetBlock(_fd, false);
            }
            catch (System.Exception)
            {
                if (_readEventArgs != null)
                {
                    _readEventArgs.Dispose();
                }
                if (_writeEventArgs != null)
                {
                    _writeEventArgs.Dispose();
                }
                _fd = null;
                throw;
            }
        }
예제 #8
0
        public int Initialize(ref ArraySegment <byte> readBuffer, IList <ArraySegment <byte> > writeBuffer)
        {
            if (!_isConnected)
            {
                int status = _delegate.Initialize(ref readBuffer, writeBuffer);
                if (status != SocketOperation.None)
                {
                    return(status);
                }
                _isConnected = true;
            }

            Socket?fd = _delegate.Fd();

            Debug.Assert(fd != null);

            Network.SetBlock(fd, true); // SSL requires a blocking socket

            //
            // For timeouts to work properly, we need to receive/send
            // the data in several chunks. Otherwise, we would only be
            // notified when all the data is received/written. The
            // connection timeout could easily be triggered when
            // receiving/sending large messages.
            //
            _maxSendPacketSize = Math.Max(512, Network.GetSendBufferSize(fd));
            _maxRecvPacketSize = Math.Max(512, Network.GetRecvBufferSize(fd));

            if (_sslStream == null)
            {
                try
                {
                    _sslStream = new SslStream(new NetworkStream(fd, false),
                                               false,
                                               new RemoteCertificateValidationCallback(ValidationCallback),
                                               new LocalCertificateSelectionCallback(SelectCertificate));
                }
                catch (IOException ex)
                {
                    if (Network.ConnectionLost(ex))
                    {
                        throw new ConnectionLostException(ex);
                    }
                    else
                    {
                        throw new TransportException(ex);
                    }
                }
                return(SocketOperation.Connect);
            }

            Debug.Assert(_sslStream.IsAuthenticated);
            _authenticated = true;

            _cipher = _sslStream.CipherAlgorithm.ToString();
            _engine.VerifyPeer((SslConnectionInfo)GetInfo(), ToString());

            if (_engine.SecurityTraceLevel >= 1)
            {
                _engine.TraceStream(_sslStream, ToString());
            }
            return(SocketOperation.None);
        }
예제 #9
0
        public int Initialize(ref ArraySegment <byte> readBuffer, IList <ArraySegment <byte> > writeBuffer)
        {
            if (!_isConnected)
            {
                int status = _delegate.Initialize(ref readBuffer, writeBuffer);
                if (status != SocketOperation.None)
                {
                    return(status);
                }
                _isConnected = true;
            }

            Socket?fd = _delegate.Fd();

            Debug.Assert(fd != null);

            Network.SetBlock(fd, true); // SSL requires a blocking socket

            //
            // For timeouts to work properly, we need to receive/send
            // the data in several chunks. Otherwise, we would only be
            // notified when all the data is received/written. The
            // connection timeout could easily be triggered when
            // receiving/sending large frames.
            //
            _maxSendPacketSize = Math.Max(512, Network.GetSendBufferSize(fd));
            _maxRecvPacketSize = Math.Max(512, Network.GetRecvBufferSize(fd));

            if (SslStream == null)
            {
                try
                {
                    SslStream = new SslStream(
                        new NetworkStream(fd, false),
                        false,
                        _engine.RemoteCertificateValidationCallback ?? RemoteCertificateValidationCallback,
                        _engine.CertificateSelectionCallback ?? CertificateSelectionCallback);
                }
                catch (IOException ex)
                {
                    if (Network.ConnectionLost(ex))
                    {
                        throw new ConnectionLostException(ex);
                    }
                    else
                    {
                        throw new TransportException(ex);
                    }
                }
                return(SocketOperation.Connect);
            }

            Debug.Assert(SslStream.IsAuthenticated);
            _authenticated = true;

            string description = ToString();

            if (!_engine.TrustManager.Verify(_incoming,
                                             SslStream.RemoteCertificate as X509Certificate2,
                                             _adapterName ?? "",
                                             description))
            {
                string msg = string.Format("{0} connection rejected by trust manager\n{1}",
                                           _incoming ? "incoming" : "outgoing",
                                           description);
                if (_engine.SecurityTraceLevel >= 1)
                {
                    _communicator.Logger.Trace(_engine.SecurityTraceCategory, msg);
                }

                throw new TransportException(msg);
            }

            if (_engine.SecurityTraceLevel >= 1)
            {
                _engine.TraceStream(SslStream, ToString());
            }
            return(SocketOperation.None);
        }
예제 #10
0
 public void SetBlock(bool block)
 {
     Debug.Assert(_fd != null);
     Network.SetBlock(_fd, block);
 }