Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
build/

libcppsockets.so
file-share
file-share-cli
*.so
*.dll
*.exe
compile_commands.json

*.tmp
Expand Down
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Author Francois Michaut
##
## Started on Sun Aug 28 19:26:51 2022 Francois Michaut
## Last update Wed Aug 20 17:05:18 2025 Francois Michaut
## Last update Thu Jul 2 01:56:37 2026 Francois Michaut
##
## CMakeLists.txt : CMake to build the CppSockets library
##
Expand All @@ -31,6 +31,7 @@ add_library(cppsockets
source/Socket.cpp
source/SocketInit.cpp
)

target_include_directories(cppsockets
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
Expand All @@ -39,6 +40,8 @@ target_include_directories(cppsockets
${PROJECT_SOURCE_DIR}/private
)

target_compile_definitions(cppsockets PRIVATE CPPSOCKETS_DLL_EXPORTS=1)

find_package(OpenSSL 3.0 COMPONENTS SSL)
target_link_libraries(cppsockets OpenSSL::SSL)
if(WIN32)
Expand Down
4 changes: 2 additions & 2 deletions include/CppSockets/Address.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
** Project CppSockets, 2022
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sun Feb 13 17:09:05 2022 Francois Michaut
** Last update Sat Jun 20 09:12:58 2026 Francois Michaut
** Last update Wed Jul 1 14:19:51 2026 Francois Michaut
**
** Address.hpp : Interface to represent network addresses
*/
Expand Down
4 changes: 2 additions & 2 deletions include/CppSockets/IPv4.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
** Project CppSockets, 2022
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sun Feb 13 17:05:02 2022 Francois Michaut
** Last update Wed Aug 20 12:57:35 2025 Francois Michaut
** Last update Wed Jul 1 14:19:39 2026 Francois Michaut
**
** IPv4.hpp : Class used to represent and manipulate IPv4 addresses
*/
Expand Down
18 changes: 9 additions & 9 deletions include/CppSockets/OSDetection.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/*
** Project CppSockets, 2023
** Project LibCppSockets, 2023
**
** Author Francois Michaut
**
** Started on Tue May 9 23:20:20 2023 Francois Michaut
** Last update Sun May 14 14:03:20 2023 Francois Michaut
** Last update Wed Jul 1 14:20:01 2026 Francois Michaut
**
** OSDetection.hpp : OS Detection macros
*/

#pragma once

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#define OS_WINDOWS
#define OS_WINDOWS 1
#elif defined (__APPLE__)
#define OS_UNIX
#define OS_APPLE
#define OS_UNIX 1
#define OS_APPLE 1
#elif defined(__linux__)
#define OS_UNIX
#define OS_LINUX
#define OS_UNIX 1
#define OS_LINUX 1
#elif defined(__unix__)
#define OS_UNIX
#define OS_UNIX 1
#else
#error "Unknown compiler, please open a issue or pull request to support your compiler"
#error "Unknown OS, please open a issue or pull request to support your OS"
#endif
4 changes: 2 additions & 2 deletions include/CppSockets/Socket.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
** Project CppSockets, 2022
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sat Jan 15 01:17:42 2022 Francois Michaut
** Last update Wed Aug 20 14:01:21 2025 Francois Michaut
** Last update Wed Jul 1 14:20:12 2026 Francois Michaut
**
** Socket.hpp : Portable C++ socket class
*/
Expand Down
10 changes: 7 additions & 3 deletions include/CppSockets/Tls/Certificate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
** Author Francois Michaut
**
** Started on Fri Aug 1 09:50:33 2025 Francois Michaut
** Last update Wed Aug 20 17:17:44 2025 Francois Michaut
** Last update Thu Jul 2 01:03:49 2026 Francois Michaut
**
** Certificate.hpp : Classes to create and manage Certificates
*/
Expand All @@ -27,6 +27,7 @@ namespace CppSockets {
x509Name();
x509Name(X509_NAME_ptr ptr);
x509Name(X509_NAME *ptr, bool own = true);
x509Name(const X509_NAME *ptr);

x509Name(const x509Name &other) { *this = other; }
x509Name(x509Name &&other) noexcept = default;
Expand Down Expand Up @@ -73,6 +74,7 @@ namespace CppSockets {
x509NameEntry();
x509NameEntry(X509_NAME_ENTRY_ptr ptr);
x509NameEntry(X509_NAME_ENTRY *ptr, bool own = true);
x509NameEntry(const X509_NAME_ENTRY *ptr);
x509NameEntry(const std::string &name, int type, const std::u8string &data);
x509NameEntry(const ASN1_OBJECT *obj, int type, const std::u8string &data);
x509NameEntry(int nid, int type, const std::u8string &data);
Expand Down Expand Up @@ -103,6 +105,7 @@ namespace CppSockets {
x509Extension();
x509Extension(X509_EXTENSION_ptr ptr);
x509Extension(X509_EXTENSION *ptr, bool own = true);
x509Extension(const X509_EXTENSION *ptr);
x509Extension(int nid, int crit, ASN1_OCTET_STRING *data);
x509Extension(const ASN1_OBJECT *obj, int crit, ASN1_OCTET_STRING *data);

Expand All @@ -119,8 +122,8 @@ namespace CppSockets {
void set_object(const ASN1_OBJECT *obj);
void set_critical(bool crit);

[[nodiscard]] auto get_data() const -> ASN1_OCTET_STRING *;
[[nodiscard]] auto get_object() const -> ASN1_OBJECT *;
[[nodiscard]] auto get_data() const -> const ASN1_OCTET_STRING *;
[[nodiscard]] auto get_object() const -> const ASN1_OBJECT *;
[[nodiscard]] auto get_critical() const -> bool;

[[nodiscard]] auto get() const -> X509_EXTENSION * { return m_ptr.get(); }
Expand All @@ -134,6 +137,7 @@ namespace CppSockets {
x509Certificate();
x509Certificate(X509_ptr ptr);
x509Certificate(X509 *ptr, bool own = true);
x509Certificate(const X509 *ptr);
explicit x509Certificate(const std::filesystem::path &pem_file_path);

x509Certificate(const x509Certificate &other) { *this = other; }
Expand Down
2 changes: 1 addition & 1 deletion include/CppSockets/Tls/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
** Author Francois Michaut
**
** Started on Wed Aug 20 14:13:44 2025 Francois Michaut
** Last update Fri Aug 22 21:43:02 2025 Francois Michaut
** Last update Tue Jun 30 08:19:16 2026 Francois Michaut
**
** Context.hpp : Context for TLS sockets
*/
Expand Down
3 changes: 2 additions & 1 deletion include/CppSockets/Tls/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
** Author Francois Michaut
**
** Started on Fri Aug 1 09:54:53 2025 Francois Michaut
** Last update Wed Aug 20 16:49:57 2025 Francois Michaut
** Last update Wed Jul 1 19:48:56 2026 Francois Michaut
**
** Utils.hpp : Tls Utility types
*/
Expand Down Expand Up @@ -58,4 +58,5 @@ namespace CppSockets {

// Don't leak macros
#undef CPP_SOCKETS_TLS_UTILS_DEFINE_DTOR
#undef CPP_SOCKETS_TLS_UTILS_DEFINE_PTR_CMD_PREFIX
#undef CPP_SOCKETS_TLS_UTILS_DEFINE_PTR
4 changes: 2 additions & 2 deletions include/CppSockets/Version.hpp.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
** Project CppSockets, 2022
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sun Aug 28 19:32:30 2022 Francois Michaut
** Last update Wed Sep 14 22:26:40 2022 Francois Michaut
** Last update Wed Jul 1 14:19:47 2026 Francois Michaut
**
** Version.hpp.in : Library version
*/
Expand Down
28 changes: 28 additions & 0 deletions include/CppSockets/internal/DLLExport.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
** Project LibCppSockets, 2026
**
** Author Francois Michaut
**
** Started on Wed Jul 1 12:30:17 2026 Francois Michaut
** Last update Wed Jul 1 18:54:43 2026 Francois Michaut
**
** DLLExport.hpp : Macros for DLL symbol exporting
*/

#pragma once

#include "CppSockets/OSDetection.hpp"

// TODO: Use theses to export only external symbols and avoid just exporting everything
#if OS_WINDOWS
#ifdef CPPSOCKETS_DLL_EXPORTS
// For windows, force the symbol to be exported when building the DLL
#define CPPSOCKETS_DLL_EXPORT __declspec(dllexport)
#else
// For windows, allows client code to know that the symbol must be loaded from the DLL
#define CPPSOCKETS_DLL_EXPORT __declspec(dllimport)
#endif
#else
// For Linux/MacOs force the symbol to be exported
#define CPPSOCKETS_DLL_EXPORT __attribute__ ((visibility("default")))
#endif
20 changes: 13 additions & 7 deletions include/CppSockets/internal/SocketInit.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
/*
** Project CppSockets, 2022
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sat Jan 15 01:17:42 2022 Francois Michaut
** Last update Wed Aug 20 13:04:47 2025 Francois Michaut
** Last update Wed Jul 1 14:20:05 2026 Francois Michaut
**
** SocketInit.hpp : Socket class automatic initialization and teardown
*/

// Inspired from https://stackoverflow.com/questions/64753466/how-do-i-automatically-implicitly-create-a-instance-of-a-class-at-program-launch/64754436#64754436

#pragma once

namespace CppSockets {
class SocketInit {
struct Cleanup {
~Cleanup();
};
public:
SocketInit();

private:
struct Cleanup {
~Cleanup();
};

static bool init;
static Cleanup cleanup;
static const bool init;
static const Cleanup cleanup;
};
}
22 changes: 15 additions & 7 deletions private/CppSockets/SslMacros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@
** Author Francois Michaut
**
** Started on Wed Aug 20 16:54:02 2025 Francois Michaut
** Last update Fri Aug 22 21:46:55 2025 Francois Michaut
** Last update Thu Jul 2 03:01:00 2026 Francois Michaut
**
** SslMacros.hpp : Private Macros to define SSL wrappers
*/

#define REQUIRED_PTR(ptr, name) \
if (!ptr) { \
throw std::runtime_error("Failed to create " name); \
}
} \

#define ASSIGNMENT_OPERATOR(type) \
if (this == &other) { \
return *this; \
} \
\
type *dup = type##_dup(other.m_ptr.get()); \
type *ptr = other.m_ptr.get(); \
type *dup = type##_dup(ptr); \
\
if (dup == nullptr) { \
throw std::runtime_error("Failed to dup " #type); \
if (ptr != nullptr && dup == nullptr) { \
throw std::runtime_error("Failed to dup " #type); \
} \
if (!this->m_own) { \
(void)this->m_ptr.release(); \
Expand All @@ -36,11 +37,18 @@
return *this; \
} \
\
type *ptr = other.m_ptr.get(); \
\
if (ptr) { \
if (!SSL_CTX_up_ref(ptr)) { \
throw std::runtime_error("Failed to up ref " #type); \
} \
} \
if (!this->m_own) { \
(void)this->m_ptr.release(); \
} \
this->m_ptr.reset(other.m_ptr.get()); \
this->m_own = false; \
this->m_ptr.reset(ptr); \
this->m_own = true; \
return *this; \

#define MAKE_DESTRUCTOR(klass) \
Expand Down
4 changes: 2 additions & 2 deletions source/Address.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
** Project CppSocket, 2022
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sun Feb 13 22:03:32 2022 Francois Michaut
** Last update Sat Jun 20 09:13:40 2026 Francois Michaut
** Last update Wed Jul 1 14:19:53 2026 Francois Michaut
**
** Address.cpp : Implementation of generic Address classes & functions
*/
Expand Down
4 changes: 2 additions & 2 deletions source/IPv4.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
** Project CppSocket, 2022
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sun Feb 13 18:52:28 2022 Francois Michaut
** Last update Fri Jun 12 02:17:05 2026 Francois Michaut
** Last update Wed Jul 1 14:19:56 2026 Francois Michaut
**
** IPv4.cpp : Implementation of IPv4 class
*/
Expand Down
7 changes: 4 additions & 3 deletions source/Socket.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
** Project CppSocket, 2022
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sat Jan 15 01:27:40 2022 Francois Michaut
** Last update Wed Aug 20 12:59:14 2025 Francois Michaut
** Last update Thu Jul 2 01:19:37 2026 Francois Michaut
**
** Socket.cpp : Protable C++ socket class implementation
*/
Expand Down Expand Up @@ -62,8 +62,9 @@ namespace CppSockets {
Socket::Socket(int domain, int type, int protocol) :
m_domain(domain), m_type(type), m_protocol(protocol), m_sockfd(::socket(domain, type, protocol))
{
if (m_sockfd == INVALID_SOCKET)
if (m_sockfd == INVALID_SOCKET) {
throw std::runtime_error(std::string("Failed to create socket : ") + std::strerror(errno));
}
}

Socket::~Socket() {
Expand Down
Loading
Loading