martelkr / cppsocket

C++ socket for SSL and non-SSL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C++ client/server with SSL/TLS support (header file only)

MIT license Linux Build Windows Build clang Build profile Build cppcheck Build Coverage Status cpp-linter Address Sanitization Code QL Clang Tidy

About

This is a header file only implementation of a C++ client/server with or without SSL/TLS/DTLS. The implementation uses OpenSSL and BSD API to implement the underlying socket interfaces.

Compilation has been tested with:

  • GCC 11.3.0 (GNU/Linux Ubuntu 22.04.1 LTS)
    • cmake 3.22.1
    • googletest 1.11.0-3
    • clang 14.0.0-1ubuntu1
  • Visual Studio Community 2022 17.4.4 (64-bit) (Windows 11)
    • cmake 3.26.0-rc1
    • googletest 1.13.0

Usage

Socket

Base sockect class for BSD API class methods.

// default constructor - no socket created because type is unknown
Socket();
Socket::initSocket(domain, type, protocol); // creates socket

// create socket from previously created file descriptor
Socket(int);

// create socket
Socket(domain, type, protocol);

TCP server/client

Create a TCP server object for accepting TCP connections.

// default no SSL and not IP/port bound
TcpServer(); 

// default SSL and not IP/port bound
TcpServer(const std::string& keyFile, const std::string& certFile); 

// No SSL and IP/port bound
explicit TcpServer(const uint16_t port, const std::string& ip = "0.0.0.0", const int backlog = 3); 

/// SSL and IP/port bound
TcpServer(const uint16_t port, const std::string& ip, const std::string& keyFile, const std::string& certFile, const int backlog = 3);

Create a TCP client object to connect to a known TCP server.

TcpClient(const std::string& ip, const uint16_t port, const bool ssl = false);
explicit TcpClient(const int fd, SSL_CTX* sslctx = nullptr);

For a BSD-like approach, the following sequence can be followed:

// Server

// create server socket
TcpServer server; // add key file and cert file here for secure connection

// bind to port 54321 on IP 0.0.0.0
server.bindAndListen(54321); 

TcpClient client = server.accept();
// Client

// Connect to TCP server on IP 127.0.0.1 and port 54321
TcpClient client("127.0.0.1", 54321); // add key file and cert file here for secure connection

UDP server/client

Create a UDP server object for accepting UDP connections.

// default constructor creates unbound unsecure UDP server socket
UdpServer();

// default DTLS constructor create unbound UDP server socket ready for DTLS
// NOTE: UdpServer s("", ""); results in unbound unsecure UDP server socket
UdpServer(const std::string& keyFile, const std::string& certFile);

// creates unsecure UDP server socket bound to specific port and IP address (default all host IP)
explicit UdpServer(const uint16_t port, const std::string& ip = "0.0.0.0");

// creates bound UDP server socket ready for DTLS
// NOTE: UdpServer s("", ""); results in unbound unsecure UDP server socket
UdpServer(const uint16_t port, const std::string& ip, const std::string& keyFile, const std::string& certFile);

Create a UDP client object to connect to a known UDP server.

// default constructor creates unconnected UDP client socket
UDPClient();

// creates UDP client socket connected to UDP server
UDPClient(const std::string& remoteIp, const uint16_t remotePort);

// creates unconnected UDP client socket for DTLS communication
UDPClient(const std::string& keyFile, const std::string& certFile);

// created UDP client socket connected to UDP server using DTLS
UDPClient(const std::string& remoteIp, const uint16_t remotePort, const std::string& keyFile, const std::string& certFile);

For a BSD-like approach, the following sequence can be followed:

// Server

// create server socket
UdpServer server; // add key file and cert file here for secure connection

// bind to port 54321 on IP 0.0.0.0
server.bind(54321); 

// following not needed for unsecure connection but is needed for DTLS connection
server.accept();
// Client

// Connect to UDP server on IP 127.0.0.1 and port 54321
UDPClient client("127.0.0.1", 54321); // add key file and cert file here for secure connection

Thread Safety

Do not share TcpServer, TcpClient, UDPClient or UdpServer objects across threads unless you provide your own thread safety on the send/read and accept calls.

Installation

Use the cppsocket.hpp file in your source tree and include it in the file that need to use it.

Run Unit Tests

Unit tests run with ctest:

ctest -C debug

Contribute

All contributions are highly appreciated.

About

C++ socket for SSL and non-SSL

License:MIT License


Languages

Language:C++ 93.5%Language:CMake 6.5%