-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSocketWrapper.h
1043 lines (846 loc) · 26.5 KB
/
SocketWrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2019 Yuji Hirose. All rights reserved.
// Copyright (c) 2019 Zpalmtree. All rights reserved.
//
// MIT License
//
// Adapted from https://github.com/yhirose/cpp-httplib
#pragma once
#ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif //_CRT_SECURE_NO_WARNINGS
#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif //_CRT_NONSTDC_NO_DEPRECATE
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#include <io.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
typedef SOCKET socket_t;
#else
#include <arpa/inet.h>
#include <cstring>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
typedef int socket_t;
#define INVALID_SOCKET (-1)
#endif //_WIN32
#include <cassert>
#include <fcntl.h>
#include <future>
#include <optional>
#include <string>
#include <thread>
#include <vector>
#ifdef SOCKETWRAPPER_OPENSSL_SUPPORT
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#include <openssl/crypto.h>
inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1)
{
return M_ASN1_STRING_data(asn1);
}
#endif
#endif
/* send() on a closed socket will crash our program. MSG_NOSIGNAL disables this.
However, this is not available on windows (it doesn't crash our program),
so we can set it to 0. If it's not on osx, we can use SO_NOSIGPIPE instead */
#ifndef MSG_NOSIGNAL
#ifdef SO_NOSIGPIPE
#define MSG_NOSIGNAL SO_NOSIGPIPE
#else
#define MSG_NOSIGNAL 0
#endif
#endif
/*
* Configuration
*/
#define SOCKETWRAPPER_READ_TIMEOUT_SECOND 5
#define SOCKETWRAPPER_READ_TIMEOUT_USECOND 0
namespace sockwrapper
{
class Stream
{
public:
virtual ~Stream() {}
virtual int read(char *ptr, size_t size) = 0;
virtual int write(const char *ptr, size_t size1) = 0;
virtual int write(const char *ptr) = 0;
virtual int write(const std::string &s) = 0;
};
class SocketStream : public Stream
{
public:
SocketStream() {};
SocketStream(socket_t sock);
virtual ~SocketStream();
virtual int read(char *ptr, size_t size);
virtual int write(const char *ptr, size_t size);
virtual int write(const char *ptr);
virtual int write(const std::string &s);
private:
socket_t sock_;
};
class BufferStream : public Stream
{
public:
BufferStream() {}
virtual ~BufferStream() {}
virtual int read(char *ptr, size_t size);
virtual int write(const char *ptr, size_t size);
virtual int write(const char *ptr);
virtual int write(const std::string &s);
const std::string &get_buffer() const;
private:
std::string buffer;
};
class SocketWrapper
{
public:
/* CONSTRUCTOR AND DESTRUCTOR */
SocketWrapper(
const char *host,
const int port = 80,
const char messageDelimiter = '\n',
const time_t timeout_sec = 10);
virtual ~SocketWrapper();
/* Deleting the copy constructor to avoid the buffers and socket being
shared */
SocketWrapper(const SocketWrapper&) = delete;
/* PUBLIC MEMBER FUNCTIONS */
virtual bool is_valid() const;
/* Initialize the socket connection and start listening for messages */
virtual bool start();
/* Close the socket connection and stop listening for messages */
virtual void stop();
/* Send a message down the socket */
bool sendMessage(const std::string &message);
/* Send a message down the socket and wait for the next response (Can time out) */
std::optional<std::string> sendMessageAndGetResponse(const std::string &message, const bool timeout = true);
/* Register a function to be called when a message is received */
void onMessage(const std::function<void(const std::string &message)> callback);
/* Register a function to be called when the socket is closed */
void onSocketClosed(const std::function<void(void)> callback);
protected:
/* PRIVATE MEMBER FUNCTIONS */
/* The function which grabs and processes messages from the socket */
void waitForMessages();
/* PRIVATE MEMBER VARIABLES */
/* The host to connect to */
const std::string m_host;
/* The port to connect to */
const int m_port;
/* What character to split incoming messages on */
const char m_messageDelimiter;
/* How long to wait for socket to connect in init() */
time_t timeout_sec_;
/* The socket instance we are wrapping */
socket_t m_socket = INVALID_SOCKET;
/* The socket reader/writer */
std::shared_ptr<Stream> m_socketStream;
/* The function to call upon message receieval */
std::function<void(const std::string &message)> m_messageCallback;
/* The function to call upon socket closed */
std::function<void(void)> m_socketClosedCallback;
/* The thread that listens for messages */
std::thread m_listenThread;
/* This will be set to the next message to arrive when sendMessageAndGetResponse
is set */
std::promise<std::string> m_nextMessagePromise;
/* Should we pass the next message to sendMessageAndGetResponse */
std::atomic<bool> m_giveMeTheNextMessagePlease;
/* Should we stop the listen thread */
std::atomic<bool> m_shouldStop;
/* Has the listen thread been started */
std::atomic<bool> m_started = false;
/* Used to synchronize writes */
std::mutex m_sendMutex;
};
#ifdef SOCKETWRAPPER_OPENSSL_SUPPORT
class SSLSocketStream : public Stream
{
public:
SSLSocketStream() {};
SSLSocketStream(socket_t sock, SSL *ssl);
virtual ~SSLSocketStream();
virtual int read(char *ptr, size_t size);
virtual int write(const char *ptr, size_t size);
virtual int write(const char *ptr);
virtual int write(const std::string &s);
private:
socket_t sock_;
SSL *m_ssl;
};
class SSLSocketWrapper : public SocketWrapper
{
public:
SSLSocketWrapper(
const char *host,
int port = 443,
const char messageDelimiter = '\n',
time_t timeout_sec = 10,
const char *client_cert_path = nullptr,
const char *client_key_path = nullptr);
virtual ~SSLSocketWrapper();
virtual bool is_valid() const;
bool start();
void stop();
private:
void shutdownSSL();
SSL_CTX *ctx_;
std::mutex ctx_mutex_;
std::string ca_cert_file_path_;
std::string ca_cert_dir_path_;
long verify_result_ = 0;
SSL *m_ssl = nullptr;
};
#endif
/*
* Implementation
*/
namespace detail
{
// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer`
// to store data. The call can set memory on stack for performance.
class stream_line_reader
{
public:
stream_line_reader(Stream &strm, char *fixed_buffer, size_t fixed_buffer_size):
strm_(strm),
fixed_buffer_(fixed_buffer),
fixed_buffer_size_(fixed_buffer_size)
{
}
const char *ptr() const
{
if (glowable_buffer_.empty())
{
return fixed_buffer_;
}
else
{
return glowable_buffer_.data();
}
}
size_t size() const
{
if (glowable_buffer_.empty())
{
return fixed_buffer_used_size_;
}
else
{
return glowable_buffer_.size();
}
}
std::optional<std::string> getline(const char delimiter, const std::atomic<bool> &shouldStop)
{
fixed_buffer_used_size_ = 0;
glowable_buffer_.clear();
while (!shouldStop)
{
char byte;
/* Read a byte */
auto n = strm_.read(&byte, 1);
/* Zero bytes read = socket closed */
if (n == 0)
{
return std::nullopt;
}
/* < zero bytes = timeout or error */
if (n < 0)
{
continue;
}
/* Append the byte to the buffer */
append(byte);
/* Message complete */
if (byte == delimiter)
{
return std::string(ptr(), size());
}
}
return std::nullopt;
}
private:
void append(char c)
{
if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1)
{
fixed_buffer_[fixed_buffer_used_size_++] = c;
fixed_buffer_[fixed_buffer_used_size_] = '\0';
}
else
{
if (glowable_buffer_.empty())
{
assert(fixed_buffer_[fixed_buffer_used_size_] == '\0');
glowable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_);
}
glowable_buffer_ += c;
}
}
Stream &strm_;
char *fixed_buffer_;
const size_t fixed_buffer_size_;
size_t fixed_buffer_used_size_;
std::string glowable_buffer_;
};
inline int close_socket(socket_t sock)
{
#ifdef _WIN32
return closesocket(sock);
#else
return close(sock);
#endif
}
inline int select_read(socket_t sock, time_t sec, time_t usec)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
timeval tv;
tv.tv_sec = static_cast<long>(sec);
tv.tv_usec = static_cast<long>(usec);
return select(static_cast<int>(sock + 1), &fds, nullptr, nullptr, &tv);
}
inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec)
{
fd_set fdsr;
FD_ZERO(&fdsr);
FD_SET(sock, &fdsr);
auto fdsw = fdsr;
auto fdse = fdsr;
timeval tv;
tv.tv_sec = static_cast<long>(sec);
tv.tv_usec = static_cast<long>(usec);
if (select(static_cast<int>(sock + 1), &fdsr, &fdsw, &fdse, &tv) < 0)
{
return false;
}
else if (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))
{
int error = 0;
socklen_t len = sizeof(error);
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len) < 0 || error)
{
return false;
}
}
else
{
return false;
}
return true;
}
inline int shutdown_socket(socket_t sock)
{
#ifdef _WIN32
return shutdown(sock, SD_BOTH);
#else
return shutdown(sock, SHUT_RDWR);
#endif
}
template<typename Fn> socket_t create_socket(const char *host, int port, Fn fn, int socket_flags = 0)
{
#ifdef _WIN32
#define SO_SYNCHRONOUS_NONALERT 0x20
#define SO_OPENTYPE 0x7008
int opt = SO_SYNCHRONOUS_NONALERT;
setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&opt, sizeof(opt));
#endif
// Get address info
struct addrinfo hints;
struct addrinfo *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = socket_flags;
hints.ai_protocol = 0;
auto service = std::to_string(port);
if (getaddrinfo(host, service.c_str(), &hints, &result))
{
return INVALID_SOCKET;
}
for (auto rp = result; rp; rp = rp->ai_next)
{
// Create a socket
#ifdef _WIN32
auto sock =
WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT);
#else
auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
#endif
if (sock == INVALID_SOCKET)
{
continue;
}
#ifndef _WIN32
if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1)
{
continue;
}
#endif
// Make 'reuse address' option available
int yes = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes));
#ifdef SO_REUSEPORT
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (char *)&yes, sizeof(yes));
#endif
// bind or connect
if (fn(sock, *rp))
{
freeaddrinfo(result);
return sock;
}
close_socket(sock);
}
freeaddrinfo(result);
return INVALID_SOCKET;
}
inline void set_nonblocking(socket_t sock, bool nonblocking)
{
#ifdef _WIN32
auto flags = nonblocking ? 1UL : 0UL;
ioctlsocket(sock, FIONBIO, &flags);
#else
auto flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK)));
#endif
}
inline bool is_connection_error()
{
#ifdef _WIN32
return WSAGetLastError() != WSAEWOULDBLOCK;
#else
return errno != EINPROGRESS;
#endif
}
#ifdef _WIN32
class WSInit
{
public:
WSInit()
{
WSADATA wsaData;
WSAStartup(0x0002, &wsaData);
}
~WSInit()
{
WSACleanup();
}
};
static WSInit wsinit_;
#endif
} // namespace detail
// Socket stream implementation
inline SocketStream::SocketStream(socket_t sock): sock_(sock) {}
inline SocketStream::~SocketStream() {}
inline int SocketStream::read(char *ptr, size_t size)
{
if (detail::select_read(sock_, SOCKETWRAPPER_READ_TIMEOUT_SECOND, SOCKETWRAPPER_READ_TIMEOUT_USECOND) > 0)
{
return recv(sock_, ptr, static_cast<int>(size), 0);
}
return -1;
}
inline int SocketStream::write(const char *ptr, size_t size)
{
return send(sock_, ptr, static_cast<int>(size), MSG_NOSIGNAL);
}
inline int SocketStream::write(const char *ptr)
{
return write(ptr, strlen(ptr));
}
inline int SocketStream::write(const std::string &s)
{
return write(s.data(), s.size());
}
// Buffer stream implementation
inline int BufferStream::read(char *ptr, size_t size)
{
#if defined(_MSC_VER) && _MSC_VER < 1900
return static_cast<int>(buffer._Copy_s(ptr, size, size));
#else
return static_cast<int>(buffer.copy(ptr, size));
#endif
}
inline int BufferStream::write(const char *ptr, size_t size)
{
buffer.append(ptr, size);
return static_cast<int>(size);
}
inline int BufferStream::write(const char *ptr)
{
return write(ptr, strlen(ptr));
}
inline int BufferStream::write(const std::string &s)
{
return write(s.data(), s.size());
}
inline const std::string &BufferStream::get_buffer() const
{
return buffer;
}
inline SocketWrapper::SocketWrapper(
const char *host,
const int port,
const char messageDelimiter,
const time_t timeout_sec):
m_host(host),
m_port(port),
m_messageDelimiter(messageDelimiter),
timeout_sec_(timeout_sec)
{
}
inline SocketWrapper::~SocketWrapper()
{
stop();
}
inline bool SocketWrapper::is_valid() const
{
return true;
}
inline bool SocketWrapper::start()
{
/* Already started */
if (m_started)
{
return true;
}
/* Create the socket */
m_socket = detail::create_socket(m_host.c_str(), m_port, [=](socket_t sock, struct addrinfo &ai) {
detail::set_nonblocking(sock, true);
auto ret = connect(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen));
if (ret < 0)
{
if (detail::is_connection_error() || !detail::wait_until_socket_is_ready(sock, timeout_sec_, 0))
{
detail::close_socket(sock);
return false;
}
}
detail::set_nonblocking(sock, false);
return true;
});
if (m_socket == INVALID_SOCKET)
{
return false;
}
if (m_listenThread.joinable())
{
m_listenThread.join();
}
m_socketStream = std::make_shared<SocketStream>(m_socket);
m_shouldStop = false;
m_started = true;
/* Start listening for messages */
m_listenThread = std::thread(&SocketWrapper::waitForMessages, this);
return true;
}
inline void SocketWrapper::stop()
{
m_shouldStop = true;
if (m_socket != INVALID_SOCKET)
{
detail::close_socket(m_socket);
m_socket = INVALID_SOCKET;
}
if (m_listenThread.joinable())
{
m_listenThread.join();
}
m_started = false;
}
inline bool SocketWrapper::sendMessage(const std::string &message)
{
if (m_socket == INVALID_SOCKET)
{
return false;
}
std::scoped_lock<std::mutex> lock(m_sendMutex);
m_socketStream->write(message);
return true;
}
inline std::optional<std::string> SocketWrapper::sendMessageAndGetResponse(const std::string &message, const bool timeout)
{
const bool success = sendMessage(message);
if (!success)
{
return std::nullopt;
}
m_nextMessagePromise = std::promise<std::string>();
std::future<std::string> futureMessage = m_nextMessagePromise.get_future();
m_giveMeTheNextMessagePlease = true;
/* Wait forever */
if (!timeout)
{
return futureMessage.get();
}
/* Else wait for the timeout before returning nothing */
else
{
std::future_status status = futureMessage.wait_for(std::chrono::seconds(timeout_sec_));
if (status == std::future_status::ready)
{
return futureMessage.get();
}
else
{
return std::nullopt;
}
}
}
inline void SocketWrapper::onMessage(const std::function<void(const std::string &message)> callback)
{
m_messageCallback = callback;
}
inline void SocketWrapper::onSocketClosed(const std::function<void(void)> callback)
{
m_socketClosedCallback = callback;
}
inline void SocketWrapper::waitForMessages()
{
const auto bufsiz = 4096;
char buf[bufsiz];
while (!m_shouldStop)
{
detail::stream_line_reader reader(*m_socketStream, buf, bufsiz);
const auto message = reader.getline(m_messageDelimiter, m_shouldStop);
if (m_shouldStop)
{
break;
}
if (!message)
{
if (m_socket != INVALID_SOCKET)
{
detail::close_socket(m_socket);
m_socket = INVALID_SOCKET;
}
if (m_socketClosedCallback)
{
m_socketClosedCallback();
}
break;
}
if (m_giveMeTheNextMessagePlease)
{
m_nextMessagePromise.set_value(*message);
m_giveMeTheNextMessagePlease = false;
}
if (m_messageCallback)
{
m_messageCallback(*message);
}
}
}
/*
* SSL Implementation
*/
#ifdef SOCKETWRAPPER_OPENSSL_SUPPORT
namespace detail
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static std::shared_ptr<std::vector<std::mutex>> openSSL_locks_;
class SSLThreadLocks
{
public:
SSLThreadLocks()
{
openSSL_locks_ = std::make_shared<std::vector<std::mutex>>(CRYPTO_num_locks());
CRYPTO_set_locking_callback(locking_callback);
}
~SSLThreadLocks()
{
CRYPTO_set_locking_callback(nullptr);
}
private:
static void locking_callback(int mode, int type, const char * /*file*/, int /*line*/)
{
auto &locks = *openSSL_locks_;
if (mode & CRYPTO_LOCK)
{
locks[type].lock();
}
else
{
locks[type].unlock();
}
}
};
#endif
class SSLInit
{
public:
SSLInit()
{
SSL_load_error_strings();
SSL_library_init();
}
~SSLInit()
{
ERR_free_strings();
}
private:
#if OPENSSL_VERSION_NUMBER < 0x10100000L
SSLThreadLocks thread_init_;
#endif
};
static SSLInit sslinit_;
} // namespace detail
// SSL socket stream implementation
inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl): sock_(sock), m_ssl(ssl) {}
inline SSLSocketStream::~SSLSocketStream() {}
inline int SSLSocketStream::read(char *ptr, size_t size)
{
if (SSL_pending(m_ssl) > 0
|| detail::select_read(sock_, SOCKETWRAPPER_READ_TIMEOUT_SECOND, SOCKETWRAPPER_READ_TIMEOUT_USECOND) > 0)
{
return SSL_read(m_ssl, ptr, size);
}
return -1;
}
inline int SSLSocketStream::write(const char *ptr, size_t size)
{
return SSL_write(m_ssl, ptr, size);
}
inline int SSLSocketStream::write(const char *ptr)
{
return write(ptr, strlen(ptr));
}
inline int SSLSocketStream::write(const std::string &s)
{
return write(s.data(), s.size());
}
// SSL HTTP client implementation
inline SSLSocketWrapper::SSLSocketWrapper(
const char *host,
int port,
const char messageDelimiter,
time_t timeout_sec,
const char *client_cert_path,
const char *client_key_path):
SocketWrapper(host, port, messageDelimiter, timeout_sec)
{
ctx_ = SSL_CTX_new(SSLv23_client_method());
if (client_cert_path && client_key_path)
{
if (SSL_CTX_use_certificate_file(ctx_, client_cert_path, SSL_FILETYPE_PEM) != 1
|| SSL_CTX_use_PrivateKey_file(ctx_, client_key_path, SSL_FILETYPE_PEM) != 1)
{
SSL_CTX_free(ctx_);
ctx_ = nullptr;
}
}
}
inline bool SSLSocketWrapper::start()
{
/* Already started */
if (m_started)
{
return true;
}
/* Create the socket */
m_socket = detail::create_socket(m_host.c_str(), m_port, [=](socket_t sock, struct addrinfo &ai) {
detail::set_nonblocking(sock, true);
auto ret = connect(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen));
if (ret < 0)
{
if (detail::is_connection_error() || !detail::wait_until_socket_is_ready(sock, timeout_sec_, 0))
{
detail::close_socket(sock);
return false;
}
}
detail::set_nonblocking(sock, false);
return true;
});
if (m_socket == INVALID_SOCKET)
{
return false;
}
{
std::lock_guard<std::mutex> guard(ctx_mutex_);
m_ssl = SSL_new(ctx_);
}
if (!m_ssl)
{
detail::close_socket(m_socket);
return false;
}
auto bio = BIO_new_socket(m_socket, BIO_NOCLOSE);
SSL_set_bio(m_ssl, bio, bio);
SSL_set_tlsext_host_name(m_ssl, m_host.c_str());
if (ca_cert_file_path_.empty())
{
SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, nullptr);
}
else
{
if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), nullptr))
{
shutdownSSL();
return false;
}
SSL_CTX_set_verify(ctx_, SSL_VERIFY_PEER, nullptr);
}
if (SSL_connect(m_ssl) != 1)
{
shutdownSSL();
return false;
}
if (m_listenThread.joinable())
{
m_listenThread.join();
}
m_socketStream = std::make_shared<SSLSocketStream>(m_socket, m_ssl);
m_shouldStop = false;
m_started = true;
/* Start listening for messages */
m_listenThread = std::thread(&SSLSocketWrapper::waitForMessages, this);