You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+80-54Lines changed: 80 additions & 54 deletions
Original file line number
Diff line number
Diff line change
@@ -1,41 +1,43 @@
1
1
# sockpp
2
2
3
-
Simple, modern, C++ socket library.
3
+
Simple, modern, C++ network socket library.
4
4
5
5
This is a fairly low-level C++ wrapper around the Berkeley sockets library using `socket`, `acceptor,` and `connector` classes that are familiar concepts from other languages.
6
6
7
-
The base `socket` class wraps a system socket handle, and maintains its lifetime. When the C++ object goes out of scope, it closes the underlying socket handle. Socket objects are generally _moveable_ but not _copyable_. A socket can be transferred from one scope (or thread) to another using `std::move()`.
7
+
The base `socket` class wraps a system socket handle and maintains its lifetime using the familiar [RAII](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) pattern. When the C++ object goes out of scope, it closes the underlying socket handle. Socket objects are generally _moveable_ but not _copyable_. A socket can be transferred from one scope (or thread) to another using `std::move()`.
8
8
9
-
The library currently supports: IPv4 and IPv6 on Linux, Mac, and Windows. Other *nix and POSIX systems should work with little or no modification.
9
+
The library currently supports: IPv4/6 on Linux, Mac, and Windows. Other *nix and POSIX systems should work with little or no modification.
10
10
11
-
Unix-Domain Sockets are available on *nix systems that have an OS implementation for them.
11
+
Unix-Domain Sockets are available on *nix systems that have an OS implementation for them, and can even vbe
12
12
13
13
Support for secure sockets using either the OpenSSL or MbedTLS libraries was recently added with basic coverage. This will continue to be expanded in the near future.
14
14
15
15
There is also some experimental support for CAN bus programming on Linux using the SocketCAN package. This gives CAN bus adapters a network interface, with limitations dictated by the CAN message protocol.
16
16
17
17
All code in the library lives within the `sockpp` C++ namespace.
18
18
19
-
**The 'master' branch is starting the move toward the v2.0 API, and is particularly unstable at the moment. You're advised to download the latest release for general use.**
19
+
**The 'master' branch is starting the move toward the v2.0 API, and is particularly unstable at the moment. You're advised to download the latest release for production use.**
20
20
21
21
## Latest News
22
22
23
-
Version 1.0 is released!
24
-
25
-
As breaking changes were starting to accumulate in the current development branch, the decision was made to release the API that has been fairly stable for the last few years as 1.0. This is from the latest v0.8.x line. That will make things going forward less confusing and allow us to maintain the v1.x branch.
23
+
Version 2.0 development is underway in this branch.
26
24
27
-
Version 2.0 development is underway.
25
+
It moves the library to C++17, taking advantage of features first available in this version of the C++ language.
28
26
29
-
The idea of having "stateless" I/O operations introduced in [PR #17](https://github.com/fpagliughi/sockpp/pull/17), (which was never fully merged) is coming in the 2.0 API with a `result<T>` class. It will be generic over the "success" return type with errors being represented by a `std::error_code`. This should help to significantly reduce platform issues for tracking and reporting errors.
27
+
The idea of having "stateless" I/O operations introduced in [PR #17](https://github.com/fpagliughi/sockpp/pull/17), (which was never fully merged) is coming in the 2.0 API with a `result<T>` class. It's generic over the "success" return type with errors being represented by a `std::error_code`. This should help to significantly reduce platform issues for tracking and reporting errors.
30
28
31
-
Using a uniform result type removes the need for exceptions in most functions, except maybe constructors. In those cases where the function might throw, a comparable `noexcept` function will also be provided which can set an error code parameter instead of throwing. So the library can be used without any exceptions if so desired by the application.
29
+
Using a uniform result type removes the need for exceptions in most functions, except maybe constructors. In those cases where the function might throw, a comparable `noexcept` function is also provided which sets an error code parameter instead of throwing. So the library can be used without any exceptions if so desired by the application.
32
30
33
31
All functions that might fail due to a system error will return a result. That will eliminate the need for the "last error", and thus the cached last error variable in the `socket` class will disappear. The socket classes will then only wrap the socket handle, making them safer to share across threads in the same way a handle can be shared - typically with one thread for reading and another for writing.
34
32
35
-
Some work has also begun to incorporate Secure Sockets into a 2.x release of the library using either OpenSSL or MbedTLS libraries, or (likely), a build-time choice for one or the other. [PR #17](https://github.com/fpagliughi/sockpp/pull/17), which has been sitting dormant for a few years is being merged and updated, along with new work to do something comparable with OpenSSL. You will be able to chose one secure library or the other when building `sockpp`.
33
+
Some work has also begun to incorporate Secure Sockets into a 2.x release of the library using either OpenSSL or MbedTLS libraries, or (likely), a build-time choice for one or the other. [PR #17](https://github.com/fpagliughi/sockpp/pull/17), which has been sitting dormant for a few years is being merged and updated, along with new work to do something comparable with OpenSSL. You will be able to chose one secure library or the other when building `sockpp`.
36
34
37
35
The 2.0 version will also move up to C++17 and CMake v3.12 or later.
38
36
37
+
Version 1.0 is released!
38
+
39
+
As breaking changes were starting to accumulate in the current development branch, the decision was made to release the API that has been fairly stable for the last few years as 1.0. This is from the latest v0.8.x line. That will make things going forward less confusing and allow us to maintain the v1.x branch.
40
+
39
41
### Get Updates
40
42
41
43
To keep up with the latest announcements for this project, follow me at:
@@ -108,7 +110,7 @@ SOCKPP_BUILD_STATIC | OFF | Whether to build the static library
108
110
SOCKPP_BUILD_DOCUMENTATION | OFF | Create and install the HTML based API documentation (requires _Doxygen)_
109
111
SOCKPP_BUILD_EXAMPLES | OFF | Build example programs
110
112
SOCKPP_BUILD_TESTS | OFF | Build the unit tests (requires _Catch2_)
111
-
SOCKPP_WITH_UNIX_SOCKETS | ON (*nix), OFF (Win) | Include support for UNIX-domain sockets
113
+
SOCKPP_WITH_UNIX_SOCKETS | ON (*nix), OFF (Win) | Include support for UNIX-domain sockets. Windows support it experimental.
112
114
SOCKPP_WITH_CAN | OFF | Include SocketCAN support. (Linux only)
113
115
114
116
Set these using the '-D' switch in the CMake configuration command. For example, to build documentation and example apps:
To build the library with secure socket support, a TLS library needs to be chosen to provide support. Currently _OpenSSL_ or _MbedTLS_ can be used.
125
-
126
-
Chose _one_ of the following when configuring the build:
127
-
128
-
Variable | Default Value | Description
129
-
------------ | ------------- | -------------
130
-
SOCKPP_WITH_MBEDTLS | OFF | Secure Sockets with MbedTLS
131
-
SOCKPP_WITH_OPENSSL | OFF | Secure Sockets with OpenSSL
132
-
133
-
#### MbedTLS
134
-
135
-
The `sockpp` library currently supports MbedTLS v3.3. When building that library, the following configuration options should be defined in the config file, _include/mbedtls/mbedtls_config.h_
136
-
137
-
```
138
-
#define MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK
139
-
```
140
-
To support threading:
141
-
142
-
```
143
-
#define MBEDTLS_THREADING_PTHREAD
144
-
#define MBEDTLS_THREADING_C
145
-
```
146
-
147
-
and set the CMake build option:
148
-
149
-
```
150
-
LINK_WITH_PTHREAD:BOOL=ON
151
-
```
152
-
153
-
Note that the options in the config file should already be present in the file but commented out by default. Simply uncomment them, save, and build.
154
-
155
-
#### OpenSSL
156
-
157
-
The `sockpp` OpenSSL wrapper is currently being built and tested with OpenSSL v3.0
158
-
159
124
## TCP Sockets
160
125
161
126
TCP and other "streaming" network applications are usually set up as either servers or clients. An acceptor is used to create a TCP/streaming server. It binds an address and listens on a known port to accept incoming connections. When a connection is accepted, a new, streaming socket is created. That new socket can be handled directly or moved to a thread (or thread pool) for processing.
@@ -231,6 +196,7 @@ UDP sockets can be used for connectionless communications:
231
196
ssize_t n = sock.recv(buf, sizeof(buf), &srcAddr);
232
197
233
198
See the [udpecho.cpp](https://github.com/fpagliughi/sockpp/blob/master/examples/udp/udpecho.cpp) and [udpechosvr.cpp](https://github.com/fpagliughi/sockpp/blob/master/examples/udp/udpechosvr.cpp) examples.
199
+
234
200
### IPv6
235
201
236
202
The same style of connectors and acceptors can be used for TCP connections over IPv6 using the classes:
@@ -250,16 +216,74 @@ The same is true for local connection on *nix systems that implement Unix Domain
250
216
unix_address
251
217
unix_connector
252
218
unix_acceptor
253
-
unix_socket (unix_stream_socket)
219
+
unix_socket
220
+
unix_stream_socket
254
221
unix_dgram_socket
255
222
256
223
Examples are in the [examples/unix](https://github.com/fpagliughi/sockpp/tree/master/examples/unix) directory.
257
224
258
-
### SocketCAN (CAN bus on Linux)
225
+
#### [Experimental] UNIX Socket Support in Windows!
226
+
227
+
Later versions of Windows 10 (starting with April 2018 update from Insider Build 17063) and all versions of Windows 11 implement support for UNIX sockets. Initial support was added to this library as a CMake option (opt-in), but has not been thoroughly tested, so is still considered experimental in this library.
259
228
260
-
The Controller Area Network (CAN bus) is a relatively simple protocol typically used by microcontrollers to communicate inside an automobile or industrial machine. Linux has the _SocketCAN_ package which allows processes to share access to a physical CAN bus interface using sockets in user space. See: [Linux SocketCAN](https://www.kernel.org/doc/html/latest/networking/can.html)
229
+
More information can be found in these posts from Microsoft:
261
230
262
-
At the lowest level, CAN devices write individual packets, called "frames" to a specific numeric addresses on the bus.
231
+
[AF_UNIX comes to Windows](https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/)
232
+
[Windows/WSL Interop with AF_UNIX](https://devblogs.microsoft.com/commandline/windowswsl-interop-with-af_unix/)
233
+
234
+
Some key points for UNIX sockets on Win32/64:
235
+
236
+
- Only stream sockets are supported, not dgram.
237
+
- socketpair is not supported.
238
+
- Windoes file and directory permissions determine who can create and connect to UNIX sockets (as expected).
239
+
- You can check if UNIX sockets are supported on a target by running the command `sc query afunix` from a Windows admin command prompt.
240
+
241
+
242
+
### [Experimental] Secure Sockets
243
+
244
+
Support for secure sockets is being added using a number of possible TLS libraries, although support is incomplete and the API is still changing.
245
+
246
+
To build the library with secure socket support, a TLS library needs to be chosen to provide support. Currently _OpenSSL_ v3.x or _MbedTLS_ can be used, but only one TLS library can be compiled in at a time. When one is chosen, a `tls_context`
247
+
248
+
Chose _one_ of the following when configuring the build:
249
+
250
+
Variable | Default Value | Description
251
+
------------ | ------------- | -------------
252
+
SOCKPP_WITH_OPENSSL | OFF | Secure Sockets with OpenSSL
253
+
SOCKPP_WITH_MBEDTLS | OFF | Secure Sockets with MbedTLS
254
+
255
+
#### OpenSSL
256
+
257
+
The `sockpp` OpenSSL wrapper is currently being built and tested with OpenSSL v3.x.
258
+
259
+
260
+
#### MbedTLS
261
+
262
+
The `sockpp` library currently supports MbedTLS v3.3. When building that library, the following configuration options should be defined in the config file, _include/mbedtls/mbedtls_config.h_
263
+
264
+
```
265
+
#define MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK
266
+
```
267
+
To support threading:
268
+
269
+
```
270
+
#define MBEDTLS_THREADING_PTHREAD
271
+
#define MBEDTLS_THREADING_C
272
+
```
273
+
274
+
and set the CMake build option:
275
+
276
+
```
277
+
LINK_WITH_PTHREAD:BOOL=ON
278
+
```
279
+
280
+
Note that the options in the config file should already be present in the file but commented out by default. Simply uncomment them, save, and build.
281
+
282
+
### [Experimental] SocketCAN (CANbus on Linux)
283
+
284
+
The Controller Area Network (CAN bus) is a relatively simple protocol typically used by microcontrollers to communicate inside an automobile or industrial machine. Linux has the _SocketCAN_ package which allows processes to share access to a physical CAN bus interface using Raw sockets in user space. See: [Linux SocketCAN](https://www.kernel.org/doc/html/latest/networking/can.html)
285
+
286
+
At the lowest level, CAN devices write individual packets, called "frames" to a specific numeric addresses on the bus.
263
287
264
288
For example a device with a temperature sensor might read the temperature peirodically and write it to the bus as a raw 32-bit integer, like:
265
289
@@ -312,6 +336,8 @@ The `socket::handle()` method exposes the underlying OS handle which can then be
312
336
313
337
A socket object is not thread-safe. Applications that want to have multiple threads reading from a socket or writing to a socket should use some form of serialization, such as a `std::mutex` to protect access.
314
338
339
+
As of Version 2.0 of the library
340
+
315
341
A `socket` can be _moved_ from one thread to another safely. This is a common pattern for a server which uses one thread to accept incoming connections and then passes off the new socket to another thread or thread pool for handling. This can be done like:
0 commit comments