Skip to content

Commit

Permalink
Add constructors to inforce output and error
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharHD committed Jan 3, 2025
1 parent 981a23a commit d54efae
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 35 deletions.
96 changes: 61 additions & 35 deletions fineftp-server/include/fineftp/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,66 +21,92 @@ namespace fineftp

/**
* @brief The fineftp::FtpServer is a simple FTP server library.
*
*
* Using the FtpServer class is simple:
* 1. Create an instance
* 2. Add a user
* 3. Start the server
*
*
* Then your server is up and running. In code it will look like this:
*
*
* @code{.cpp}
*
*
* fineftp::FtpServer server(2121);
* server.addUserAnonymous("C:\\", fineftp::Permission::All);
* server.start();
*
*
* @endcode
*
*
*/
class FtpServer
{
public:
/**
* @brief Creates an FTP Server instance that will listen on the the given control port and accept connections from the given network interface.
*
*
* If no port is provided, the default FTP Port 21 is used. If you want to
* use that port, make sure that your application runs as root.
*
*
* Instead of using a predefined port, the operating system can choose a
* free port port. Use port=0, if that behaviour is desired. The chosen port
* can be determined by with getPort().
*
*
* @param port: The port to start the FTP server on. Defaults to 21.
* @param host: The host to accept incoming connections from.
* @param output: Normal output prints. Defaults to std::cout.
* @param error: Error output prints. Defaults to std::cerr.
*/
FINEFTP_EXPORT FtpServer(const std::string& address, uint16_t port = 21, std::ostream& output = std::cout, std::ostream& error = std::cerr);
FINEFTP_EXPORT FtpServer(const std::string& address, uint16_t port = 21);

/**
* @brief Creates an FTP Server instance that will listen on the the given control port and accept connections from the given network interface.
*
*
* If no port is provided, the default FTP Port 21 is used. If you want to
* use that port, make sure that your application runs as root.
*
*
* Instead of using a predefined port, the operating system can choose a
* free port port. Use port=0, if that behaviour is desired. The chosen port
* can be determined by with getPort().
*
* @param port: The port to start the FTP server on. Defaults to 21.
* @param host: The host to accept incoming connections from.
*
* @param address: The address to accept incoming connections from.
* @param port: The port to start the FTP server on.
* @param output: Normal output prints. Defaults to std::cout.
* @param error: Error output prints. Defaults to std::cerr.
*/
FINEFTP_EXPORT FtpServer(const std::string& address, std::ostream& output = std::cout, std::ostream& error = std::cerr);
FINEFTP_EXPORT FtpServer(const std::string& address, uint16_t port, std::ostream& output = std::cout, std::ostream& error = std::cerr);

/**
* @brief Creates an FTP Server instance that will listen on the the given control port and accept connections from the given network interface.
*
* If no port is provided, the default FTP Port 21 is used. If you want to
* use that port, make sure that your application runs as root.
*
* @param address: The address to accept incoming connections from.
* @param output: Normal output prints.
* @param error: Error output prints.
*/
FINEFTP_EXPORT FtpServer(const std::string& address, std::ostream& output, std::ostream& error);

/**
* @brief Creates an FTP Server instance that will listen on the the given control port.
*
*
* If no port is provided, the default FTP Port 21 is used. If you want to
* use that port, make sure that your application runs as root.
*
*
* Instead of using a predefined port, the operating system can choose a
* free port port. Use port=0, if that behaviour is desired. The chosen port
* can be determined by with getPort().
*
* This constructor will create an FTP Server binding to IPv4 0.0.0.0 and
* Thus accepting connections from any IPv4 address.
* For security reasons it might be desirable to bind to a specific IP
* address. Use FtpServer(const std::string&, uint16_t) for that purpose.
*
* @param port: The port to start the FTP server on. Defaults to 21.
*/
FINEFTP_EXPORT FtpServer(uint16_t port = 21);

/**
* @brief Creates an FTP Server instance that will listen on the the given control port.
*
* Instead of using a predefined port, the operating system can choose a
* free port port. Use port=0, if that behaviour is desired. The chosen port
* can be determined by with getPort().
Expand All @@ -94,7 +120,7 @@ namespace fineftp
* @param output: Normal output prints. Defaults to std::cout.
* @param error: Error output prints. Defaults to std::cerr.
*/
FINEFTP_EXPORT FtpServer(uint16_t port = 21, std::ostream& output = std::cout, std::ostream& error = std::cerr);
FINEFTP_EXPORT FtpServer(uint16_t port, std::ostream& output, std::ostream& error);

// Move
FINEFTP_EXPORT FtpServer(FtpServer&&) noexcept;
Expand All @@ -109,49 +135,49 @@ namespace fineftp

/**
* @brief Adds a new user
*
*
* Adds a new user with a given username, password, local root path and
* permissions.
*
*
* Note that the username "anonymous" and "ftp" are reserved, as those are
* well-known usernames usually used for accessing FTP servers without a
* password. If adding a user with any of those usernames, the password will
* be ignored, any user will be able to log in with any password!
*
*
* The Permissions are flags that are or-ed bit-wise and control what the
* user will be able to do.
*
*
* @param username: The username for login
* @param password: The user's password
* @param local_root_path: A path to any resource on the local filesystem that will be accessed by the user
* @param permissions: A bit-mask of what the user will be able to do.
*
*
* @return True if adding the user was successful (i.e. it didn't exit already).
*/
FINEFTP_EXPORT bool addUser(const std::string& username, const std::string& password, const std::string& local_root_path, Permission permissions);

/**
* @brief Adds the "anonymous" / "ftp" user that FTP clients use to access FTP servers without password
*
*
* @param local_root_path: A path to any resource on the local filesystem that will be accessed by the user
* @param permissions: A bit-mask of what the user will be able to do.
*
*
* @return True if adding the anonymous user was successful (i.e. it didn't exit already).
*/
FINEFTP_EXPORT bool addUserAnonymous(const std::string& local_root_path, Permission permissions);

/**
* @brief Starts the FTP Server
*
*
* @param thread_count: The size of the thread pool to use. Must not be 0.
*
*
* @return True if the Server has been started successfully.
*/
FINEFTP_EXPORT bool start(size_t thread_count = 1);

/**
* @brief Stops the FTP Server
*
*
* All operations will be cancelled as fast as possible. The clients will
* not be informed about the shutdown.
*/
Expand All @@ -166,19 +192,19 @@ namespace fineftp

/**
* @brief Get the control port that the FTP server is listening on
*
*
* When the server was created with a specific port (not 0), this port will
* be returned.
* If the server however was created with port 0, the operating system will
* choose a free port. This method will return that port.
*
*
* @return The control port the server is listening on
*/
FINEFTP_EXPORT uint16_t getPort() const;

/**
* @brief Get the ip address that the FTP server is listening for.
*
*
* @return The ip address the FTP server is listening for.
*/
FINEFTP_EXPORT std::string getAddress() const;
Expand Down
8 changes: 8 additions & 0 deletions fineftp-server/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

namespace fineftp
{
FtpServer::FtpServer(const std::string& address, const uint16_t port)
: ftp_server_(std::make_unique<FtpServerImpl>(address, port, std::cout, std::cerr))
{}

FtpServer::FtpServer(const std::string& address, const uint16_t port, std::ostream& output, std::ostream& error)
: ftp_server_(std::make_unique<FtpServerImpl>(address, port, output, error))
{}
Expand All @@ -20,6 +24,10 @@ namespace fineftp
: ftp_server_(std::make_unique<FtpServerImpl>(address, 21, output, error))
{}

FtpServer::FtpServer(const uint16_t port)
: FtpServer(std::string("0.0.0.0"), port, std::cout, std::cerr)
{}

FtpServer::FtpServer(const uint16_t port, std::ostream& output, std::ostream& error)
: FtpServer(std::string("0.0.0.0"), port, output, error)
{}
Expand Down

0 comments on commit d54efae

Please sign in to comment.