/// <summary> /// PASV Command - RFC 959 - Section 4.1.2 /// <para>进入被动模式(请求服务器等待数据连接)</para> /// </summary> /// <returns></returns> private Response Passive() { _dataConnectionType = DataConnectionType.Passive; IPAddress localIp = ((IPEndPoint)ControlClient.Client.LocalEndPoint).Address; _passiveListener = PassiveListenerPool.GetListener(localIp); try { _passiveListener.Start(); } catch { _log.Error("No more ports available"); return(GetResponse(FtpResponses.UNABLE_TO_OPEN_DATA_CONNECTION)); } IPEndPoint passiveListenerEndpoint = (IPEndPoint)_passiveListener.LocalEndpoint; byte[] address = passiveListenerEndpoint.Address.GetAddressBytes(); ushort port = (ushort)passiveListenerEndpoint.Port; byte[] portArray = BitConverter.GetBytes(port); if (BitConverter.IsLittleEndian) { Array.Reverse(portArray); } return(GetResponse(FtpResponses.ENTERING_PASSIVE_MODE.SetData(address[0], address[1], address[2], address[3], portArray[0], portArray[1]))); }
protected override void Dispose(bool disposing) { PassiveListenerPool.ReleaseAll(); if (_timer != null) { _timer.Dispose(); } base.Dispose(disposing); }
/// <summary> /// EPSV Command - RFC 2428 /// <para>扩展被动模式(IPv6)</para> /// </summary> /// <returns></returns> private Response EPassive() { _dataConnectionType = DataConnectionType.Passive; IPAddress localIp = ((IPEndPoint)ControlClient.Client.LocalEndPoint).Address; _passiveListener = PassiveListenerPool.GetListener(localIp); try { _passiveListener.Start(); } catch { _log.Error("No more ports available"); return(GetResponse(FtpResponses.UNABLE_TO_OPEN_DATA_CONNECTION)); } IPEndPoint passiveListenerEndpoint = (IPEndPoint)_passiveListener.LocalEndpoint; return(GetResponse(FtpResponses.ENTERING_EXTENDED_PASSIVE_MODE.SetData(passiveListenerEndpoint.Port))); }
private void DoDataConnectionOperation(IAsyncResult result) { _performanceCounter.IncrementTotalConnectionAttempts(); _performanceCounter.IncrementCurrentConnections(); //由BeginConnect异步调用,因此需要EndConnect,EndConnect需要使用【对应的】IAsyncResult作为参数 HandleAsyncResult(result); //取出我们传入的真正需要的DataConnectionOperation结构,内含本次要执行的方法与参数 DataConnectionOperation op = result.AsyncState as DataConnectionOperation; Response response; try { if (_dataClient == null) { throw new SocketException(); } //通过上述异步过程,此时的_dataClient已经连接 if (_protected) { using (GnuSslStream dataStream = new GnuSslStream(_dataClient.GetStream(), false)) { try { dataStream.AuthenticateAsServer(_cert, false, SslProtocols.Tls, true); response = op.Operation(dataStream, op.Arguments); } catch (Exception) { response = GetResponse(FtpResponses.UNABLE_TO_OPEN_DATA_CONNECTION); //throw; } dataStream.Close(); //response = op.Operation(dataStream, op.Arguments); } } else { using (NetworkStream dataStream = _dataClient.GetStream()) { response = op.Operation(dataStream, op.Arguments); } } } catch (Exception ex) { _log.Error(ex); response = GetResponse(FtpResponses.TRANSFER_ABORTED); } if (_dataClient != null) { _dataClient.Close(); _dataClient = null; } _performanceCounter.DecrementCurrentConnections(); if (_dataConnectionType == DataConnectionType.Passive) { PassiveListenerPool.FreeListener(_passiveListener); } Write(response.ToString()); }