Skip to content

Commit

Permalink
Reduced constructor complexity and fixed some clang tidy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed Jan 9, 2025
1 parent d54efae commit ca6129c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 81 deletions.
106 changes: 42 additions & 64 deletions fineftp-server/include/fineftp/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,84 +43,62 @@ namespace fineftp
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 binding the server to a specific address, the server can
* listen on any interface by providing "0.0.0.0" as address.
*
* 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.
*
* This constructor will accept streams for info and error log output.
*
* @param address: The address to accept incoming connections from. Use "0.0.0.0" to accept connections from any address.
* @param port: The port to start the FTP server on. Use 0 to let the operating system choose a free port. Use 21 for using the default FTP port.
* @param output: Stream for info log output. Defaults to std::cout if constructors without that options are used.
* @param error: Stream for error log output. Defaults to std::cerr if constructors without that options are used.
*/
FINEFTP_EXPORT FtpServer(const std::string& address, uint16_t port = 21);
FINEFTP_EXPORT FtpServer(const std::string& address, uint16_t port, std::ostream& output, std::ostream& error);

/**
* @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 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, 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.
*
* Logs will be printed to std::cout and std::cerr. If you want to use a
* different output stream, use the other constructor.
*
* @param port: The port to start the FTP server on. Defaults to 21.
* @param host: The host to accept incoming connections from.
*/
FINEFTP_EXPORT FtpServer(const std::string& address, std::ostream& output, std::ostream& error);
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.
*
*
* 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().
*
*
* 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.
*
*
* Logs will be printed to std::cout and std::cerr. If you want to use a
* different output stream, use the other constructor.
*
* @param port: The port to start the FTP server on. Defaults to 21.
* @param output: Normal output prints. Defaults to std::cout.
* @param error: Error output prints. Defaults to std::cerr.
*/
FINEFTP_EXPORT FtpServer(uint16_t port, std::ostream& output, std::ostream& error);
FINEFTP_EXPORT explicit FtpServer(uint16_t port = 21);

// Move
FINEFTP_EXPORT FtpServer(FtpServer&&) noexcept;
Expand All @@ -135,18 +113,18 @@ 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
Expand All @@ -158,53 +136,53 @@ namespace fineftp

/**
* @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.
*/
FINEFTP_EXPORT void stop();

/**
* @brief Returns the number of currently open connections
*
*
* @return the number of open connections
*/
FINEFTP_EXPORT int getOpenConnectionCount() const;

/**
* @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
9 changes: 8 additions & 1 deletion fineftp-server/src/ftp_session.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "ftp_session.h"

#include <asio.hpp>

#include <algorithm>
#include <cassert> // assert
#include <cctype> // std::iscntrl, toupper
#include <chrono>
#include <chrono> // IWYU pragma: keep (it is used for special preprocessor defines)
#include <cstddef>
#include <cstdio>
#include <fstream>
Expand All @@ -28,6 +30,11 @@
#include <sys/stat.h>

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include "win_str_convert.h"
#else
#include <unistd.h>
Expand Down
19 changes: 6 additions & 13 deletions fineftp-server/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,29 @@

#include "server_impl.h"

#include <cassert> // assert
#include <cstddef> // size_t
#include <cstdint> // uint16_t
#include <memory>
#include <ostream>
#include <string>
#include <cstdint> // uint16_t
#include <cstddef> // size_t
#include <cassert> // assert

#include <fineftp/permissions.h>

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))
{}

FtpServer::FtpServer(const std::string& address, std::ostream& output, std::ostream& error)
: ftp_server_(std::make_unique<FtpServerImpl>(address, 21, output, error))
FtpServer::FtpServer(const std::string& address, const uint16_t port)
: FtpServer(address, port, std::cout, std::cerr)
{}

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)
{}

// Move
FtpServer::FtpServer(FtpServer&&) noexcept = default;
FtpServer& FtpServer::operator=(FtpServer&&) noexcept = default;
Expand Down
6 changes: 3 additions & 3 deletions fineftp-server/src/server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace fineftp
{

FtpServerImpl::FtpServerImpl(const std::string& address, const uint16_t port, std::ostream& output, std::ostream& error)
: ftp_users_(output, error)
: ftp_users_ (output, error)
, port_ (port)
, address_ (address)
, acceptor_ (io_service_)
, open_connection_count_(0)
, output_(output)
, error_(error)
, output_ (output)
, error_ (error)
{}

FtpServerImpl::~FtpServerImpl()
Expand Down
11 changes: 11 additions & 0 deletions fineftp-server/src/win32/file_man.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

#include "file_man.h"

#include <cstdint>
#include <ios>
#include <map>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <utility>

#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

#include "win_str_convert.h"

Expand Down

0 comments on commit ca6129c

Please sign in to comment.