join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
dtls_wrapper.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CRYPTO_DTLS_WRAPPER_HPP
26#define JOIN_CRYPTO_DTLS_WRAPPER_HPP
27
28// libjoin.
29#include <join/tls.hpp>
30
31namespace join
32{
36 template <class Protocol>
37 class BasicDtlsWrapper : public BasicTls<Protocol>
38 {
39 public:
41
43 using BasicTls<Protocol>::BasicTls;
44
52 ssize_t readFrom (char* buf, size_t len, Endpoint* endpoint = nullptr) noexcept
53 {
54 if (this->_ssl)
55 {
56 int nread = SSL_read (this->_ssl.get (), buf, static_cast<int> (len));
57 if (nread < 1)
58 {
59 return this->handleTlsError (nread);
60 }
61
62 if (endpoint != nullptr)
63 {
64 BIO* rbio = SSL_get_rbio (this->_ssl.get ());
65 if (!rbio)
66 {
68 return -1;
69 }
70
71 struct sockaddr_storage sa;
72 socklen_t sa_len = sizeof (struct sockaddr_storage);
73 if (BIO_dgram_get_peer (rbio, &sa) <= 0)
74 {
76 return -1;
77 }
78
79 *endpoint = Endpoint (reinterpret_cast<struct sockaddr*> (&sa), sa_len);
80 }
81
82 return nread;
83 }
84
85 return this->_socket.readFrom (buf, len, endpoint);
86 }
87
95 ssize_t writeTo (const char* buf, size_t len, const Endpoint& endpoint) noexcept
96 {
97 if (this->_ssl)
98 {
99 BIO* wbio = SSL_get_wbio (this->_ssl.get ());
100 if (!wbio)
101 {
103 return -1;
104 }
105
106 struct sockaddr_storage sa;
107 socklen_t sa_len = sizeof (struct sockaddr_storage);
108 if (BIO_dgram_get_peer (wbio, &sa) <= 0)
109 {
111 return -1;
112 }
113
114 Endpoint remote (reinterpret_cast<struct sockaddr*> (&sa), sa_len);
115 if (remote != endpoint)
116 {
118 return -1;
119 }
120
121 int nwritten = SSL_write (this->_ssl.get (), buf, static_cast<int> (len));
122 if (nwritten < 1)
123 {
124 return this->handleTlsError (nwritten);
125 }
126
127 return nwritten;
128 }
129
130 return this->_socket.writeTo (buf, len, endpoint);
131 }
132
137 int ttl () const noexcept
138 {
139 return this->_socket.ttl ();
140 }
141 };
142
149 template <class Protocol>
150 inline bool operator< (const BasicDtlsWrapper<Protocol>& a, const BasicDtlsWrapper<Protocol>& b) noexcept
151 {
152 return a.handle () < b.handle ();
153 }
154}
155
156#endif
DTLS decorator for datagram sockets.
Definition tls_protocol.hpp:37
ssize_t writeTo(const char *buf, size_t len, const Endpoint &endpoint) noexcept
write data on the socket.
Definition dtls_wrapper.hpp:95
typename BasicTls< Protocol >::Endpoint Endpoint
Definition dtls_wrapper.hpp:40
int ttl() const noexcept
returns the Time-To-Live value.
Definition dtls_wrapper.hpp:137
ssize_t readFrom(char *buf, size_t len, Endpoint *endpoint=nullptr) noexcept
read data on the socket.
Definition dtls_wrapper.hpp:52
basic TLS/DTLS decorator.
Definition tls.hpp:54
BasicTls(TlsContext ctx, Mode mode=Mode::NonBlocking) noexcept
create a TLS decorator with an internally created socket.
Definition tls.hpp:68
int handleTlsError(int result) noexcept
handle TLS error.
Definition tls.hpp:912
UnderlyingSocket _socket
verify certificate revocation using CRL.
Definition tls.hpp:1199
typename Protocol::Endpoint Endpoint
Definition tls.hpp:60
SslPtr _ssl
TLS handle.
Definition tls.hpp:1205
Definition acceptor.hpp:32
bool operator<(const BasicDatagramSocket< Protocol > &a, const BasicDatagramSocket< Protocol > &b) noexcept
compare if socket handle is inferior.
Definition datagram_socket.hpp:394
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195