Code Monkey home page Code Monkey logo

sockpuppet's Introduction

sockpuppet

Minimalistic and easy-to-use C++ socket and address library.

About

So you want to have network communication in your little program but boost seems just too much? And how hard can this socket stuff be anyhow, eh? - After being there more than once, I write this library with the hope that it spares me from future coding of half-assed single-use sockets.

Library features

  • supports Unix, OSX and Windows OS
  • IPv6 and IPv4 address handling (lookup using getaddrinfo and storage using sockaddr_storage)
  • multi-interface-aware (list local host interface addresses before selecting one to bind to)
  • UDP and TCP socket classes
  • UDP broadcast (but not automatically on multiple network interfaces)
  • basic sockets with blocking and non-blocking IO using optional timeout parameter
  • extended sockets with configurable internal resource pool eliminating the need for pre-allocated buffers
  • extended sockets for asynchronous operation using driver thread interface (event handling using poll)
  • TCP sockets can be augmented with TLS encryption
  • scheduled tasks to be run at given point in time by driver thread
  • exceptions with meaningful system-provided error messages
  • library includes do not pull any system or external headers
  • static and dynamic library build targets
  • some functional tests
  • examples and demo project included
  • exhaustive unit tests ๐Ÿ˜ฐ
  • UDP multicast
  • address arithmetic/lookup for network/broadcast addresses

Build Build Status

Configure and build library/examples/demo/tests using CMake.

Quickstart

The CMake install target prepares a CMake-based demo project suitable as a starting point for own applications.

Library interface

The Address class represents localhost or remote UDP/TCP addresses and is used to create local and send/connect to remote sockets.

The socket classes Socket*, Socket*Bufferedand Socket*Async provide different levels of convenience around the raw OS socket representation:

  • SocketUdp and SocketTcp allow basic functions like connect, send and receive, while Acceptor listens for incoming TCP connections
  • SocketUdpBuffered and SocketTcpBuffered add an internal receive buffer pool
  • SocketUdpAsync and SocketTcpAsync as well as AcceptorAsync are run by a Driver (i.e. a thread) providing asynchronous operation to one or multiple sockets

If built with TLS support, all TCP socket classes can be instantiated with an SSL certificate and private key file to run encrypted connections.

The ToDo class is used for scheduling tasks to be run by a Driver at a given point in time, e.g. periodic heartbeat packet transmissions or reconnect attemts.

Design rationale

  • most user-visible classes employ a bridge/PIMPL pattern to avoid forwarding the internally included system headers
  • the address class employs getaddrinfo when created by user input and sockaddr_storage when created by a socket; for users this distinction is transparent
  • while the user-visible socket classes distinguish between UDP/TCP, the socket PIMPL classes provide generic functions that may have redundancies for some use cases
  • poll is used prior to socket IO if a limited timeout is given, to honor the deadline
  • the augmenting sockets consume pre-constructed basic sockets to avoid aggregating all base socket constructor arguments and funnelling all their exceptions
  • threads are not created internally to avoid messy shared library shutdown on Windows

sockpuppet's People

Contributors

mporsch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

linecode

sockpuppet's Issues

Issues with TLS under write load (again)

Seems like ssl3_write_bytes:bad length:ssl is not leaving us anytime soon, see test failure in MSVC with OpenSSL 1.1.1u.

12/14 Test #12: sockpuppet_tls_buffered_test ............***Failed    7.34 sec
generating random reference data
test case #1: unlimited send timeout
server listening at [::1]:8554
client [::1]:50124 connected to server [::1]:8554
sending reference data from [::1]:50124 to [::1]:8554
verifying received against reference data
test case #2: limited send timeout
server listening at [::1]:8554
client [::1]:50125 connected to server [::1]:8554
sending reference data from [::1]:50125 to [::1]:8554
failed to TLS receive/send/handshake: 928:error:1409E10F:SSL routines:ssl3_write_bytes:bad length:ssl\record\rec_layer_s3.c:377:

verifying received against reference data
received only 18876 of 1000000 bytes
test case #3: non-blocking send
server listening at [::1]:8554
client [::1]:50126 connected to server [::1]:8554
sending reference data from [::1]:50126 to [::1]:8554
failed to TLS receive/send/handshake: 2816:error:1409E10F:SSL routines:ssl3_write_bytes:bad length:ssl\record\rec_layer_s3.c:377:

verifying received against reference data
received only 15556 of 1000000 bytes

Maybe this is specific to the OpenSSL v1 library used in this github action job's OS? - job has been disabled for now.

Issues with TLS under write load

See test failure in MSVC [1] and test failure in OSX [2].

  12: Test command: D:\a\sockpuppet\sockpuppet\build\Debug\sockpuppet_tls_buffered_test.exe
  12: Working Directory: D:/a/sockpuppet/sockpuppet/build
  12: Test timeout computed to be: 10000000
  12: generating random reference data
  12: test case #1: unlimited send timeout
  12: server listening at [::1]:8554
  12: client [::1]:61089 connected to server [::1]:8554
  12: sending reference data from [::1]:61089 to [::1]:8554
  12: verifying received against reference data
  12: test case #2: limited send timeout
  12: server listening at [::1]:8554
  12: client [::1]:61092 connected to server [::1]:8554
  12: sending reference data from [::1]:61092 to [::1]:8554
  12: failed to wait for TLS socket readable/writable: 6956:error:1409E10F:SSL routines:ssl3_write_bytes:bad length:ssl\record\rec_layer_s3.c:377:
  12: 
  12: verifying received against reference data
  12: received only 8166 of 1000000 bytes
  12: test case #3: non-blocking send
  12: server listening at [::1]:8554
  12: client [::1]:61093 connected to server [::1]:8554
  12: sending reference data from [::1]:61093 to [::1]:8554
  12: failed to wait for TLS socket readable/writable: 4156:error:1409E10F:SSL routines:ssl3_write_bytes:bad length:ssl\record\rec_layer_s3.c:377:
  12: 
  12: verifying received against reference data
  12: received only 9602 of 1000000 bytes
  12/14 Test #12: sockpuppet_tls_buffered_test ............***Failed    5.00 sec
14/14 Test #14: sockpuppet_tls_async_performance_test ...Subprocess aborted***Exception:  62.37 sec
generating random reference data
server listening at [::1]:8554
client [::1]:49191 connected to server
client [::1]:49191 connecting to server
sending reference data from [::1]:49191 to [::1]:8554
client [::1]:49192 connecting to server
sending reference data from [::1]:49192 to [::1]:8554
client [::1]:49193 connecting to server
sending reference data from [::1]:49193 to [::1]:8554
client [::1]:49192 connected to server
client [::1]:49193 connected to server
verifying received against reference data
client [::1]:49191 closing connection to server
client [::1]:49191 closed connection to server
verifying received against reference data
client [::1]:49192 closing connection to server
client [::1]:49192 closed connection to server
clients did not receive echoed reference data on time
Assertion failed: (i < handshakeStepsMax), function Shutdown, file socket_tls_impl.cpp, line 240.
client [::1]:49193 closed connection to server

TLS connect issues in Windows with OpenSSL 1.1.1v

  11/14 Test #11: sockpuppet_tls_test .....................***Failed   15.39 sec
  server listening at 127.0.0.1:57972
  Bad optional access
  failed to connect to 127.0.0.1:57972: No connection could be made because the target machine actively refused it.
  failed to connect to 127.0.0.1:57972: No connection could be made because the target machine actively refused it.
  failed to connect to 127.0.0.1:57972: No connection could be made because the target machine actively refused it.
11/14 Test #11: sockpuppet_tls_test .....................***Failed    7.25 sec
  server listening at 127.0.0.1:63399
  client 127.0.0.1:63401 connected to server 127.0.0.1:63399
  client 127.0.0.1:63402 connected to server 127.0.0.1:63399
  client 127.0.0.1:63403 connected to server 127.0.0.1:63399
  server sending to client 127.0.0.1:63401
  server sending to client 127.0.0.1:63402
  server sending to client 127.0.0.1:63403
  client failed to receive
  client failed to receive
  client failed to receive

TLS shutdown assertion hit

Sporadic assertion hits in shutdown procedure (see test failure in Ubuntu with OpenSSL 3.0.2).

14/14 Test #14: sockpuppet_tls_async_performance_test ...Subprocess aborted***Exception:   1.68 sec
generating random reference data
server listening at [::1]:8554
client [::1]:38418 connecting to server
client [::1]:38418 connected to server
sending reference data from [::1]:38418 to [::1]:8554
client [::1]:38430 connected to server
client [::1]:38430 connecting to server
Errors while running CTest
sending reference data from client [::1]:38446 connected to server
client [::1]:38446 connecting to server
[::1]:38430 to [::1]:8554
sending reference data from [::1]:38446 to [::1]:8554
client [::1]:38418 closed connection to server
client [::1]:8554 closing connection to server
sockpuppet_tls_async_performance_test: /home/runner/work/sockpuppet/sockpuppet/src/socket_tls_impl.cpp:329: void sockpuppet::SocketTlsImpl::Shutdown(): Assertion `i < handshakeStepsMax' failed.

Maybe just give the peer more time to process its pending receipts?

Async TLS gets stuck in OSX

14/14 Test #14: sockpuppet_tls_async_performance_test ...Subprocess aborted***Exception:  62.69 sec
...
clients did not receive echoed reference data on time
The associated promise has been destructed prior to the associated state becoming ready.
Assertion failed: (i < handshakeStepsMax), function Shutdown, file socket_tls_impl.cpp, line 262.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.