public void Process(Byte[] abData) { string sMessage = this.Encoding.GetString(abData); sMessage = sMessage.Substring(0, sMessage.IndexOf('\r')); FtpServerMessageHandler.SendMessage(Id, sMessage); string sCommand; string sValue; int nSpaceIndex = sMessage.IndexOf(' '); if (nSpaceIndex < 0) { sCommand = sMessage.ToUpper(); sValue = ""; } else { sCommand = sMessage.Substring(0, nSpaceIndex).ToUpper(); sValue = sMessage.Substring(sCommand.Length + 1); } // check whether the client has logged in if (!isLogged) { if (!((sCommand == "USER") || (sCommand == "PASS") || (sCommand == "HELP") || (sCommand == "FEAT") || (sCommand == "QUIT"))) { SocketHelpers.Send(Socket, "530 Not logged in\r\n", this.Encoding); return; } } // check if data connection will be used if ((sCommand == "APPE") || (sCommand == "MLSD") || (sCommand == "LIST") || (sCommand == "RETR") || (sCommand == "STOR")) { m_useDataSocket = true; } var handler = m_theCommandHashTable[sCommand] as FtpCommandHandler; if (handler == null) { FtpServerMessageHandler.SendMessage(Id, string.Format("\"{0}\" : Unknown command", sCommand)); SocketHelpers.Send(Socket, "550 Unknown command\r\n", this.Encoding); } else { handler.Process(sValue); } // reset m_useDataSocket = false; }
private static TcpClient OpenSocket(FtpConnectionObject connectionObject) { switch (connectionObject.DataConnectionType) { case DataConnectionType.Active: return(SocketHelpers.CreateTcpClient(connectionObject.PortCommandSocketAddress, connectionObject.PortCommandSocketPort)); case DataConnectionType.Passive: return(connectionObject.PassiveSocket); default: FtpServerMessageHandler.SendMessage(connectionObject.Id, "Invalid connection type!"); return(null); } }
private void ThreadMonitor() { while (m_theThread.IsAlive) { DateTime currentTime = DateTime.Now; TimeSpan timeSpan = currentTime - m_lastActiveTime; // has been idle for a long time if ((timeSpan.TotalSeconds > m_maxIdleSeconds) && !m_theCommands.DataSocketOpen) { SocketHelpers.Send(m_theSocket, string.Format("426 No operations for {0}+ seconds. Bye!", m_maxIdleSeconds), m_theCommands.Encoding); FtpServerMessageHandler.SendMessage(m_nId, "Connection closed for too long idle time."); if (Closed != null) { Closed(this); } m_theSocket.Close(); this.Stop(); return; } Thread.Sleep(1000 * m_maxIdleSeconds); } return; // only monitor the work thread }
/// <summary> /// The main thread of the ftp server /// Listen and acception clients, create handler threads for each client /// </summary> private void ThreadRun() { FtpServerMessageHandler.Message += TraceMessage; // listen at the port by the "FTP" endpoint setting var ipEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 21); m_socketListen = SocketHelpers.CreateTcpListener(ipEndPoint); if (m_socketListen != null) { Console.WriteLine("Information: FTP Server listened at: " + ipEndPoint); m_socketListen.Start(); Console.WriteLine("Information: FTP Server Started"); bool fContinue = true; while (fContinue) { TcpClient socket = null; try { socket = m_socketListen.AcceptTcpClient(); } catch (SocketException) { fContinue = false; } finally { if (socket == null) { fContinue = false; } else if (m_apConnections.Count >= m_maxClients) { Console.WriteLine( "Warning: Too many clients, won't handle this connection"); SendRejectMessage(socket); socket.Close(); } else { socket.NoDelay = false; m_nId++; FtpServerMessageHandler.SendMessage(m_nId, "New connection"); SendAcceptMessage(socket); InitialiseSocketHandler(socket); } } } } else { FtpServerMessageHandler.SendMessage(0, "Error in starting FTP server"); } }