Skip to content

Commit

Permalink
Implement Tls server
Browse files Browse the repository at this point in the history
  • Loading branch information
Iandiehard committed Mar 26, 2024
1 parent e212840 commit 00fd0ca
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Diagnostic Client library
* Copyright (C) 2024 Avijit Dey
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_SERVER_TLS_TLS_ACCEPTOR_H_
#define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_SERVER_TLS_TLS_ACCEPTOR_H_

#include "boost-support/server/tls/tls_server.h"

namespace boost_support {
namespace server {
namespace tls {

/**
* @brief The acceptor to create new tcp servers
*/
class TlsAcceptor final {
public:
/**
* @brief Constructs an instance of Acceptor
* @details Tcp connection shall be accepted on this ip address and port
* @param[in] local_ip_address
* The local ip address
* @param[in] local_port_num
* The local port number
* @param[in] maximum_connection
* The maximum number of accepted connection allowed
*/
TlsAcceptor(std::string_view local_ip_address, std::uint16_t local_port_num,
std::uint8_t maximum_connection) noexcept;

/**
* @brief Destruct an instance of TcpAcceptor
*/
~TlsAcceptor() noexcept;

/**
* @brief Get a tls server ready to communicate
* @details This blocks until new server is created
* @return Tls server object on success, else nothing
*/
std::optional<TlsServer> GetTlsServer() noexcept;

private:
/**
* @brief Forward declaration of tls acceptor implementation
*/
class TlsAcceptorImpl;

/**
* @brief Unique pointer to tls acceptor implementation
*/
std::unique_ptr<TlsAcceptorImpl> tls_acceptor_impl_;
};
} // namespace tls
} // namespace server
} // namespace boost_support
#endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_SERVER_TLS_TLS_ACCEPTOR_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* Diagnostic Client library
* Copyright (C) 2024 Avijit Dey
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_SERVER_TLS_TLS_SERVER_H_
#define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_SERVER_TLS_TLS_SERVER_H_

#include <functional>
#include <memory>

#include "boost-support/message/tcp/tcp_message.h"
#include "core/include/result.h"

namespace boost_support {
namespace socket {
namespace tls {
class TlsSocket;
} // namespace tls
} // namespace socket

namespace server {
namespace tls {

/**
* @brief Server that manages unsecured/ secured tcp connection
*/
class TlsServer final {
public:
/**
* @brief Type alias for tcp unsecured socket
*/
using TlsSocket = socket::tls::TlsSocket;

/**
* @brief Type alias for Tcp message
*/
using Message = boost_support::message::tcp::TcpMessage;

/**
* @brief Type alias for Tcp message pointer
*/
using MessagePtr = boost_support::message::tcp::TcpMessagePtr;

/**
* @brief Type alias for Tcp message const pointer
*/
using MessageConstPtr = boost_support::message::tcp::TcpMessageConstPtr;

/**
* @brief Tcp function template used for reception
*/
using HandlerRead = std::function<void(MessagePtr)>;

public:
/**
* @brief Constructs an instance of TlsServer
* @param[in] tls_socket
* The underlying tls socket required for communication
*/
explicit TlsServer(TlsSocket tls_socket) noexcept;

/**
* @brief Deleted copy assignment and copy constructor
*/
TlsServer(const TlsServer &other) noexcept = delete;
TlsServer &operator=(const TlsServer &other) noexcept = delete;

/**
* @brief Move assignment and move constructor
*/
TlsServer(TlsServer &&other) noexcept;
TlsServer &operator=(TlsServer &&other) noexcept;

/**
* @brief Destruct an instance of TcpServer
*/
~TlsServer() noexcept;

/**
* @brief Initialize the server
*/
void Initialize() noexcept;

/**
* @brief De-initialize the server
*/
void DeInitialize() noexcept;

/**
* @brief Function to set the read handler that is invoked when message is received
* @details The ownership of provided read handler is moved
* @param[in] read_handler
* The handler to be set
*/
void SetReadHandler(HandlerRead read_handler) noexcept;

/**
* @brief Function to transmit the provided tcp message
* @param[in] tcp_message
* The tcp message
* @return Empty void on success, otherwise error is returned
*/
core_type::Result<void> Transmit(MessageConstPtr tcp_message);

private:
/**
* @brief Forward declaration of tcp server implementation
*/
class TlsServerImpl;

/**
* @brief Unique pointer to tcp server implementation
*/
std::unique_ptr<TlsServerImpl> tls_server_impl_;
};

} // namespace tls
} // namespace server
} // namespace boost_support
#endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_SERVER_TLS_TLS_SERVER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ class TcpSocket final {

/**
* @brief Constructs an instance of TcpSocket
* @param[in] local_ip_address
* The local ip address
* @param[in] local_port_num
* The local port number
* @param[in] socket
* The socket
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "boost-support/socket/tcp/tls_server.h"
#include "boost-support/socket/tcp/tls_server_.h"

#include <utility>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ TlsSocket::TlsSocket(std::string_view local_ip_address, std::uint16_t local_port
local_port_num_{local_port_num},
tls_socket_{io_context.GetContext(), tls_context.GetContext()} {}

TlsSocket::TlsSocket(TlsSocket::TcpSocket tcp_socket, TlsContext &tls_context) noexcept {}

TlsSocket::TlsSocket(TlsSocket &&other) noexcept
: local_ip_address_{std::move(other.local_ip_address_)},
local_port_num_{other.local_port_num_},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class TlsSocket final {
*/
using TcpMessageConstPtr = boost_support::message::tcp::TcpMessageConstPtr;

/**
* @brief Type alias for tcp protocol
*/
using Tcp = boost::asio::ip::tcp;

/**
* @brief Type alias for tcp socket
*/
using TcpSocket = Tcp::socket;
public:
/**
* @brief Constructs an instance of TcpSocket
Expand All @@ -63,6 +72,13 @@ class TlsSocket final {
TlsSocket(std::string_view local_ip_address, std::uint16_t local_port_num, TlsContext &tls_context,
IoContext &io_context) noexcept;

/**
* @brief Constructs an instance of TcpSocket
* @param[in] socket
* The socket
*/
TlsSocket(TcpSocket tcp_socket, TlsContext &tls_context) noexcept;

/**
* @brief Deleted copy assignment and copy constructor
*/
Expand Down
2 changes: 1 addition & 1 deletion test/test_case/tls_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <optional>

#include "boost-support/socket/tcp/tls_client_.h"
#include "boost-support/socket/tcp/tls_server.h"
#include "boost-support/socket/tcp/tls_server_.h"

namespace doip_client {
namespace {
Expand Down

0 comments on commit 00fd0ca

Please sign in to comment.