Skip to content

Commit 561686f

Browse files
committed
Detect peer closed connection
- update IAddress port from int to the right type (uint16) - allow for defaulted Socket + TLSSocket - other improvements - some bugfixes
1 parent f10671a commit 561686f

File tree

7 files changed

+195
-95
lines changed

7 files changed

+195
-95
lines changed

Diff for: include/CppSockets/Address.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Sun Feb 13 17:09:05 2022 Francois Michaut
7-
** Last update Tue Jul 18 13:34:19 2023 Francois Michaut
7+
** Last update Thu Jul 20 21:17:13 2023 Francois Michaut
88
**
99
** Address.hpp : Interface to represent network addresses
1010
*/
@@ -30,7 +30,7 @@ namespace CppSockets {
3030
class IEndpoint {
3131
public:
3232
[[nodiscard]]
33-
virtual int getPort() const = 0;
33+
virtual std::uint16_t getPort() const = 0;
3434
[[nodiscard]]
3535
virtual const IAddress &getAddr() const = 0;
3636
[[nodiscard]]
@@ -47,12 +47,12 @@ namespace CppSockets {
4747
"Endpoint address must derive from IAddress"
4848
);
4949
public:
50-
Endpoint(T addr, int port) :
50+
Endpoint(T addr, std::uint16_t port) :
5151
addr(std::move(addr)), port(port), str(makeString())
5252
{};
5353

5454
[[nodiscard]]
55-
int getPort() const override {
55+
std::uint16_t getPort() const override {
5656
return port;
5757
}
5858

@@ -66,7 +66,7 @@ namespace CppSockets {
6666
return str;
6767
}
6868
private:
69-
int port;
69+
std::uint16_t port;
7070
T addr;
7171
std::string str;
7272
};

Diff for: include/CppSockets/IPv4.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Sun Feb 13 17:05:02 2022 Francois Michaut
7-
** Last update Thu Feb 9 08:55:24 2023 Francois Michaut
7+
** Last update Thu Jul 20 20:23:05 2023 Francois Michaut
88
**
99
** IPv4.hpp : Class used to represent and manipulate IPv4 addresses
1010
*/
@@ -17,7 +17,7 @@ namespace CppSockets {
1717
class IPv4 : public IAddress {
1818
public:
1919
explicit IPv4(std::uint32_t addr);
20-
explicit IPv4(const char *addr);
20+
IPv4(const char *addr);
2121

2222
[[nodiscard]]
2323
std::uint32_t getAddress() const override;

Diff for: include/CppSockets/Socket.hpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Sat Jan 15 01:17:42 2022 Francois Michaut
7-
** Last update Tue May 9 23:31:24 2023 Francois Michaut
7+
** Last update Sat Jul 22 19:43:35 2023 Francois Michaut
88
**
99
** Socket.hpp : Portable C++ socket class
1010
*/
@@ -13,6 +13,7 @@
1313

1414
#include "CppSockets/OSDetection.hpp"
1515

16+
// TODO: move the RawSocketType in CppSockets namespace
1617
#ifdef OS_WINDOWS
1718
#define NOMINMAX
1819
#include <winsock2.h>
@@ -33,7 +34,9 @@ namespace CppSockets {
3334

3435
class Socket {
3536
public:
37+
Socket();
3638
Socket(int domain, int type, int protocol);
39+
Socket(RawSocketType fd, bool connected);
3740
~Socket();
3841

3942
// TODO Maybe enable copy with dup(2) ?
@@ -47,17 +50,18 @@ namespace CppSockets {
4750
std::size_t write(const std::string &buff);
4851
std::size_t write(const char *buff, std::size_t len);
4952

53+
int set_reuseaddr(bool value);
5054
int getsockopt(int level, int optname, void *optval, socklen_t *optlen);
5155
int setsockopt(int level, int optname, const void *optval, socklen_t optlen);
5256

57+
void close();
5358
int connect(const IEndpoint &endpoint);
54-
int connect(const std::string &addr, int port);
59+
int connect(const std::string &addr, uint16_t port);
5560

56-
int bind(std::uint32_t addr, int port); // TODO remove ?
5761
int bind(const IEndpoint &endpoint);
58-
int bind(const std::string &addr, int port);
62+
int bind(const std::string &addr, uint16_t port);
5963
int listen(int backlog);
60-
std::shared_ptr<Socket> accept(void *addr_out);
64+
std::shared_ptr<Socket> accept(void *addr_out = nullptr);
6165

6266
void set_blocking(bool val);
6367

@@ -79,14 +83,13 @@ namespace CppSockets {
7983
static char *strerror(int err);
8084
static char *strerror();
8185

82-
private:
83-
Socket(int domain, int type, int protocol, int sockfd);
84-
86+
protected:
8587
static int getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen);
88+
int bind(std::uint32_t addr, uint16_t port);
8689

87-
int domain;
88-
int type;
89-
int protocol;
90+
int domain = 0;
91+
int type = 0;
92+
int protocol = 0;
9093
RawSocketType sockfd;
9194
bool is_connected = false;
9295
};

Diff for: include/CppSockets/TlsSocket.hpp

+20-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Wed Sep 14 20:51:23 2022 Francois Michaut
7-
** Last update Tue May 9 23:29:53 2023 Francois Michaut
7+
** Last update Sat Jul 22 22:45:28 2023 Francois Michaut
88
**
99
** SecureSocket.hpp : TLS socket wrapper using openssl
1010
*/
@@ -23,6 +23,7 @@
2323
#include <functional>
2424

2525
// TODO: find a better way do to this
26+
using BIO = struct bio_st;
2627
using SSL = struct ssl_st;
2728
using SSL_METHOD = struct ssl_method_st;
2829
using SSL_CTX = struct ssl_ctx_st;
@@ -33,6 +34,7 @@ using EVP_MD = struct evp_md_st;
3334
using EVP_MD_CTX = struct evp_md_ctx_st;
3435

3536
namespace CppSockets {
37+
using BIO_ptr=std::unique_ptr<BIO, std::function<void(BIO *)>>;
3638
using SSL_CTX_ptr=std::unique_ptr<SSL_CTX, std::function<void(SSL_CTX *)>>;
3739
using SSL_ptr=std::unique_ptr<SSL, std::function<void(SSL *)>>;
3840
using X509_ptr=std::unique_ptr<X509, std::function<void(X509 *)>>;
@@ -44,8 +46,10 @@ namespace CppSockets {
4446
// TODO add more TLS-related functions
4547
class TlsSocket : public Socket {
4648
public:
47-
explicit TlsSocket(Socket &&other);
49+
TlsSocket() = default;
4850
TlsSocket(int domain, int type, int protocol);
51+
TlsSocket(Socket &&other, SSL_ptr ssl = nullptr);
52+
TlsSocket(RawSocketType fd, SSL_ptr ssl = nullptr);
4953
~TlsSocket();
5054

5155
TlsSocket(const TlsSocket &other) = delete;
@@ -58,9 +62,10 @@ namespace CppSockets {
5862
std::size_t write(const std::string &buff);
5963
std::size_t write(const char *buff, std::size_t len);
6064

65+
void set_certificate(std::string cert_path, std::string pkey_path);
6166
int connect(const IEndpoint &endpoint);
6267

63-
std::shared_ptr<TlsSocket> accept(void *addr_out);
68+
std::shared_ptr<TlsSocket> accept(void *addr_out = nullptr);
6469

6570
[[nodiscard]]
6671
const SSL_CTX_ptr &get_ssl_ctx() const;
@@ -72,26 +77,30 @@ namespace CppSockets {
7277
[[nodiscard]]
7378
const std::string tls_strerror(int ret);
7479
private:
75-
SSL_CTX_ptr ctx;
76-
SSL_ptr ssl;
77-
X509_ptr peer_cert;
78-
bool do_shutdown = true;
80+
SSL_CTX_ptr m_ctx;
81+
SSL_ptr m_ssl;
82+
X509_ptr m_peer_cert;
83+
X509_ptr m_cert;
84+
EVP_PKEY_ptr m_pkey;
85+
bool m_do_shutdown = true;
86+
87+
void check_for_error(std::string error_msg, int ret);
7988
};
8089

8190
inline std::size_t TlsSocket::write(const std::string &buff) {
82-
return write(buff.data(), buff.size());
91+
return write(buff.c_str(), buff.size());
8392
}
8493

8594
inline const SSL_CTX_ptr &TlsSocket::get_ssl_ctx() const {
86-
return ctx;
95+
return m_ctx;
8796
}
8897

8998
inline const SSL_ptr &TlsSocket::get_ssl() const {
90-
return ssl;
99+
return m_ssl;
91100
}
92101

93102
inline const X509_ptr &TlsSocket::get_client_cert() const {
94-
return peer_cert;
103+
return m_peer_cert;
95104
}
96105
}
97106
#endif

Diff for: source/IPv4.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
** Author Francois Michaut
55
**
66
** Started on Sun Feb 13 18:52:28 2022 Francois Michaut
7-
** Last update Mon Aug 29 20:45:54 2022 Francois Michaut
7+
** Last update Thu Jul 20 23:08:49 2023 Francois Michaut
88
**
99
** IPv4.cpp : Implementation of IPv4 class
1010
*/
1111

1212
#include "CppSockets/IPv4.hpp"
1313
#include "CppSockets/Socket.hpp"
1414

15+
#include <stdexcept>
16+
1517
#include <arpa/inet.h>
1618

1719
namespace CppSockets {
@@ -24,8 +26,14 @@ namespace CppSockets {
2426
}
2527

2628
IPv4::IPv4(const char *addr) :
27-
addr(inet_addr(addr))
28-
{}
29+
str(addr)
30+
{
31+
struct in_addr in;
32+
33+
if (inet_aton(addr, &in) == 0)
34+
throw std::runtime_error("Invalid IPv4 address");
35+
this->addr = in.s_addr;
36+
}
2937

3038
std::uint32_t IPv4::getAddress() const {
3139
return addr;

0 commit comments

Comments
 (0)