Skip to content

Commit

Permalink
fix cppcheck and update readme (#31)
Browse files Browse the repository at this point in the history
* fix cppcheck and update readme

* clang-tidy fixes

* update cppcheck

* update readme

* fix compilation issue

* fix file name

* update action and fix warning
  • Loading branch information
martelkr authored Dec 25, 2023
1 parent 99dfd60 commit 8f9d55c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ jobs:
- name: Run clang-tidy
run: |
cmake -B ${{github.workspace}}/build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
clang-tidy -p ${{github.workspace}}/build inc/cppsocket.hpp test/TestCppSocket.cpp
clang-tidy -p ${{github.workspace}}/build -header-filter=.* inc/cppsocket.hpp test/Testcppsocket.cpp
32 changes: 11 additions & 21 deletions .github/workflows/cppcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Install cppcheck
run: apt install cppcheck -y


- name: cppcheck
uses: deep5050/cppcheck-action@main
with:
github_token: ${{ secrets.GITHUB_TOKEN}}
enable: all
force_language: c++
std: c++20
platform: unix64
other_options: --bug-hunting -I inc --verbose --suppress=missingIncludeSystem --enable=warning

- name: publish report
uses: mikeal/publish-to-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH_NAME: '--force main'
- uses: actions/checkout@v2
- name: cppcheck
uses: chmorgan/cppcheck-action-jackson@main
with:
github_token: ${{ secrets.GITHUB_TOKEN}}
enable: all
inline_suppression: enable
force_language: c++
platform: unix64
std: c++20
other_options: -I inc --verbose --suppress=missingIncludeSystem
102 changes: 52 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,118 +38,120 @@ Socket::initSocket(domain, type, protocol); // creates socket
// create socket from previously created file descriptor
Socket(int);

// create socket
// create socket of the given type
Socket(domain, type, protocol);


```
### TCP server/client
Create a TCP server object for accepting TCP connections.
```cpp
// default no SSL and not IP/port bound
// default no IP/port bound but TCP socket created
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);
// TCP socket created, IP/port bound, but not listening
TcpServer(const uint16_t port, const std::string& ipAddr = "0.0.0.0");
/// 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);
// TCP socket created, IP/port bound, and listening for clients
TcpServer(const std::string& ipAddr, const uint16_t port, const int backlog);
```

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

```cpp
TcpClient(const std::string& ip, const uint16_t port, const bool ssl = false);
explicit TcpClient(const int fd, SSL_CTX* sslctx = nullptr);
// default TCP socket created but no server connection
TcpClient();

// create TcpClient with given TCP socket file descriptor
TcpClient(const int filedescriptor);

// TcpClient connected to a TcpServer IP/port
TcpClient(const std::string& ipAddr, const uint16_t port);
```
Create a SSL TCP Server for accepting SSL TCP clients.
```cpp
// Create a SSL TCP Server not bound to IP/port
SecureTcpServer(const std::string& keyFile, const std::string& certFile);
// Create a SSL TCP Server bound to a given port and IP or default IP
SecureTcpServer(const std::string& keyFile, const std::string& certFile, const uint16_t port, const std::string& ipAddr = "0.0.0.0");
```

Create a SSL TCP client for connecting to SSL TCP servers.

```cpp
// create a SSL TCP client with a given SSL context - used with SecureTcpServer::accept return
SecureTcpClient(const int filedescriptor, SSL_CTX *sslctx);

// create a SSL TCP client connected to a SSL TCP server
SecureTcpClient(const std::string& ipAddr, const uint16_t port);
```
For a BSD-like approach, the following sequence can be followed:
```cpp
// 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);
// create server socket and bind to all IP and 54321 port
TcpServer server(54321);
// wait for a client connection
TcpClient client = server.accept();
```

```cpp
// 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
TcpClient client("127.0.0.1", 54321);
```
### UDP server/client
Create a UDP server object for accepting UDP connections.
```cpp
// 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");
// default no IP/port bound but UDP socket created
UdpServer();
// 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);
// UDP server socket created, IP/port bound
UdpServer(const uint16_t port, const std::string &ipAddr = "0.0.0.0");
```

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

```cpp
// default UDP socket created but no server connection
UdpClient();

// 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);
// UdpClient connected to a UdpServer IP/port
UdpClient(const std::string& ipAddr, const uint16_t port) noexcept(false)
```
For a BSD-like approach, the following sequence can be followed:
```cpp
// 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();
// create server socket bound to all IP and 54321 port
UdpServer server(54321);
```

```cpp
// 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
UDPClient client("127.0.0.1", 54321);
```
## 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.
Do not share any of the socket objects across threads unless you provide your own thread safety on the send/read and accept calls.
## Installation
Expand Down
Loading

0 comments on commit 8f9d55c

Please sign in to comment.