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)); }
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)); }
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); } }
public ConnectionInfo GetInfo() { var info = new TCPConnectionInfo(); Socket?fd = _stream.Fd(); if (fd != null) { EndPoint localEndpoint = Network.GetLocalAddress(fd); info.LocalAddress = Network.EndpointAddressToString(localEndpoint); info.LocalPort = Network.EndpointPort(localEndpoint); EndPoint?remoteEndpoint = Network.GetRemoteAddress(fd); info.RemoteAddress = Network.EndpointAddressToString(remoteEndpoint); info.RemotePort = Network.EndpointPort(remoteEndpoint); info.RcvSize = Network.GetRecvBufferSize(fd); info.SndSize = Network.GetSendBufferSize(fd); } return(info); }
public ConnectionInfo GetInfo() { var info = new UDPConnectionInfo(); if (_fd != null) { EndPoint localEndpoint = Network.GetLocalAddress(_fd); info.LocalAddress = Network.EndpointAddressToString(localEndpoint); info.LocalPort = Network.EndpointPort(localEndpoint); if (_state == StateNotConnected) { if (_peerAddr != null) { info.RemoteAddress = Network.EndpointAddressToString(_peerAddr); info.RemotePort = Network.EndpointPort(_peerAddr); } } else { EndPoint?remoteEndpoint = Network.GetRemoteAddress(_fd); if (remoteEndpoint != null) { info.RemoteAddress = Network.EndpointAddressToString(remoteEndpoint); info.RemotePort = Network.EndpointPort(remoteEndpoint); } } info.RcvSize = Network.GetRecvBufferSize(_fd); info.SndSize = Network.GetSendBufferSize(_fd); } if (_mcastAddr != null) { info.McastAddress = Network.EndpointAddressToString(_mcastAddr); info.McastPort = Network.EndpointPort(_mcastAddr); } return(info); }
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); }
private void SetBufSize(int rcvSize, int sndSize) { Debug.Assert(_fd != null); for (int i = 0; i < 2; ++i) { bool isSnd; string direction; string prop; int dfltSize; int sizeRequested; if (i == 0) { isSnd = false; direction = "receive"; prop = "Ice.UDP.RcvSize"; dfltSize = Network.GetRecvBufferSize(_fd); sizeRequested = rcvSize; _rcvSize = dfltSize; } else { isSnd = true; direction = "send"; prop = "Ice.UDP.SndSize"; dfltSize = Network.GetSendBufferSize(_fd); sizeRequested = sndSize; _sndSize = dfltSize; } // // Get property for buffer size if size not passed in. // if (sizeRequested == -1) { sizeRequested = _communicator.GetPropertyAsInt(prop) ?? dfltSize; } // // Check for sanity. // if (sizeRequested < (UdpOverhead + Ice1Definitions.HeaderSize)) { _communicator.Logger.Warning($"Invalid {prop} value of {sizeRequested} adjusted to {dfltSize}"); sizeRequested = dfltSize; } if (sizeRequested != dfltSize) { // // Try to set the buffer size. The kernel will silently adjust // the size to an acceptable value. Then read the size back to // get the size that was actually set. // int sizeSet; if (i == 0) { Network.SetRecvBufferSize(_fd, sizeRequested); _rcvSize = Network.GetRecvBufferSize(_fd); sizeSet = _rcvSize; } else { Network.SetSendBufferSize(_fd, sizeRequested); _sndSize = Network.GetSendBufferSize(_fd); sizeSet = _sndSize; } // // Warn if the size that was set is less than the requested size // and we have not already warned // if (sizeSet < sizeRequested) { BufSizeWarnInfo winfo = _communicator.GetBufSizeWarn(EndpointType.UDP); if ((isSnd && (!winfo.SndWarn || winfo.SndSize != sizeRequested)) || (!isSnd && (!winfo.RcvWarn || winfo.RcvSize != sizeRequested))) { _communicator.Logger.Warning( $"UDP {direction} buffer size: requested size of {sizeRequested} adjusted to {sizeSet}"); if (isSnd) { _communicator.SetSndBufSizeWarn(EndpointType.UDP, sizeRequested); } else { _communicator.SetRcvBufSizeWarn(EndpointType.UDP, sizeRequested); } } } } } }
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); }