This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocket.hpp
430 lines (340 loc) · 13.1 KB
/
socket.hpp
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
#include <cstdio>
#include <stdlib.h>
#include <string>
// #include <tuple>
#include <memory>
#include <errno.h>
#include <openssl/ssl.h>
#ifndef __linux__
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#ifndef __linux__
#pragma comment(lib, "Ws2_32.lib")
typedef int64_t sock_buf_size;
typedef int64_t ssize_t;
#else
typedef size_t sock_buf_size;
#endif
#ifndef _SOCK_CLI_HPP
#define _SOCK_CLI_HPP
#ifdef __linux__
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h> // getaddrinfo()
#include <unistd.h> // close()
#include <netinet/in.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#define PHOSTENT hostent*
#define SOCKET unsigned int
#define INVALID_SOCKET (SOCKET)(~0)
#define h_addr h_addr_list[0]
#define SOCKET_ERROR (-1)
#endif
// ---------------------------- develop print related -----------------
#define ping puts("ping");
#define puts(str) puts(str);
#define putss(str) puts(str.c_str());
// ----------------------- client socket class ------------------------
struct ssl_st;
typedef struct ssl_st SSL;
struct TSocketClient {
public:
bool SockCreateAndConnect ( void ); // create and connect via socket
bool SockWriteChunk ( const void* pBuffer, size_t pChunkSize, int& pBytesWritten ); // write a chunk to socket
bool SockReadChunk ( char* pBuffer, int& pBytesRead, int pChunkSize ); // read a chunk from socket
void SockClose ( void ); // closes the existing open socket if any
TSocketClient ( const char* pServer, int pPort , bool is_ssl_ ); // constructor
~TSocketClient (); // destructor
private:
// data members
SOCKET vSocket; // socket handle
struct sockaddr_in vSockAddr; // sockaddr details
SSL *ssl;
bool is_ssl;
// other data members
char vszErrMsg[256]; // last error msg
// private function members
bool SetErrMsg ( bool flgIncludeWin32Error, const char* pszErrMsg, ... );
ssize_t sock_send(const char *buf, sock_buf_size buf_size);
ssize_t sock_recv(char *buf, sock_buf_size buf_size);
public:
// static data members
static bool g_flgLibReady;
// static member functions
static int SockInitLib (short pMajorVersion = 1, short pMinorVersion = 1);
static bool SockFinalizeLib (void);
};
#endif // of hpp
ssize_t TSocketClient::sock_recv (char *buf, sock_buf_size buf_size) {
return (is_ssl) ? SSL_read(ssl, buf, buf_size) : recv(vSocket, buf, buf_size, 0);
}
ssize_t TSocketClient::sock_send(const char *buf, sock_buf_size buf_size) {
return (is_ssl) ? SSL_write(ssl, buf, buf_size) : send(vSocket, buf, buf_size, 0);
}
// ----------------- static data members initialization ------------------
bool TSocketClient::g_flgLibReady = 0;
// --------------------------------------------------------------------
// does the work of the constructor, initializes addr struct with IP and port
// --------------------------------------------------------------------
#ifndef __linux__
typedef unsigned int in_addr_t;
#endif
// --------------------------------------------------------------------
// constructor, which parepares sock_addr for connection
// --------------------------------------------------------------------
TSocketClient::TSocketClient (const char* pServer, int pPort, bool is_ssl_) : is_ssl(is_ssl_) {
vSocket = INVALID_SOCKET; // handle
memset (&vSockAddr, 0, sizeof(vSockAddr)); // address struct
memset (vszErrMsg, 0, sizeof(vszErrMsg)); // internal error message store
auto error = false;
// Initialize
int addr;
PHOSTENT phostent = NULL;
struct addrinfo *result = NULL;
// precaution
if ( pServer == NULL or pPort <= 0 )
error = true;
// try converting from dotted decimal form
inet_pton(AF_INET, pServer, &addr);
// addr = inet_addr (pServer);
if (addr != (in_addr_t)INADDR_NONE) { // success
// put the addr in SOCKADDR struct
vSockAddr.sin_addr.s_addr = addr;
}
// check if local
else if (strcmp(pServer, "(local)") == 0) {
inet_pton(AF_INET, "127.0.0.1", &addr);
// addr = inet_addr ("127.0.0.1");
if (addr != (in_addr_t)INADDR_NONE) { // success
// put the addr in SOCKADDR struct
vSockAddr.sin_addr.s_addr = addr;
}
else
error = true;
}
// try name resolution
else {
// use gethosbyname function and try resolving
getaddrinfo(pServer, NULL, {}, &result);
// phostent = gethostbyname(pServer);
if ( phostent == NULL )
error = true; // resolution failed
// put the addr in SOCKADDR struct
memcpy (&(vSockAddr.sin_addr), phostent->h_addr, phostent->h_length);
}
// prepare the remaining sock addr struct
vSockAddr.sin_family = AF_INET; // address family
vSockAddr.sin_port = htons ( (uint16_t)pPort ); // port number
#ifdef __linux__
g_flgLibReady = true;
#endif
if (is_ssl) {
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
}
}
TSocketClient::~TSocketClient () {
// will close socket if open and reset corres. member variables
SockClose();
}
// --------------------------------------------------------------------
// STATIC, to initialize the windows socket library ( requirement for winsock )
// --------------------------------------------------------------------
#ifndef __linux__ // SockInitLib, SockFinalizeLib - Winsock init / end
int TSocketClient::SockInitLib (short pMajorVersion, short pMinorVersion) {
short v;
int err;
WSADATA wd;
// check if already initialized
if ( TSocketClient::g_flgLibReady == 1 )
return 1;
// call the Winsock initialization function
// https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup
if (WSAStartup (MAKEWORD(pMinorVersion, pMajorVersion), &wd)) {
// SetErrMsg ( true, "WSAStartup failed" ); // cannot call a non-static member function
return -1; // so failure indicated by return code
}
// Confirm Winsock.dll version
if (LOBYTE(wd.wVersion) != pMinorVersion or HIBYTE(wd.wVersion) != pMajorVersion) {
WSACleanup ();
// SetErrMsg ( false, "Version mismatch. Required: %d.%d, Available: %d.%d", pMajorVersion, pMinorVersion, HIBYTE( wd.wVersion ), LOBYTE( wd.wVersion ));
return -2;
}
// set the static memeber indicator
TSocketClient::g_flgLibReady = 1;
return 0; // success
}
// --------------------------------------------------------------------
// to finalize the windows socket library
// --------------------------------------------------------------------
bool TSocketClient::SockFinalizeLib ( void )
{
// note
// applications must make sure that all sockets are closed
// since this is a static member function and does not
// know about class instances
// lib cleanup for winsock
WSACleanup ();
// set the static memeber indicator
TSocketClient::g_flgLibReady = 0;
return true;
}
#endif // SockInitLib, SockFinalizeLib
bool TSocketClient::SockCreateAndConnect ( void ) {
int iStatus;
SOCKET s;
// PRECAUTION
if (TSocketClient::g_flgLibReady == 0 || vSockAddr.sin_addr.s_addr == 0 ) {
SetErrMsg ( false, "Winsock/SockAddr not initialized" );
puts ("*** inside precaution\n")
return false;
}
// CREATE - create a new stream socket
s = socket (AF_INET, SOCK_STREAM, 0 );
if ( s == INVALID_SOCKET ) {
SetErrMsg ( true, "WSASocket failed" );
return false;
}
// CONNECT - connect using the already prepared address
iStatus = connect ( s, ( struct sockaddr* )&vSockAddr, sizeof(vSockAddr));
if ( iStatus != 0 ) {
puts ("*** inside connect fail\n")
printf ("%d", iStatus);
#ifdef __linux__
close(s);
#else
closesocket(s);
#endif
SetErrMsg ( true, "WSAConnect failed - %d\n ",errno );
return false;
}
// SUCCESS
vSocket = s;
// SSL
if (is_ssl) {
SSL_CTX * sslctx = SSL_CTX_new( SSLv23_client_method());
SSL_CTX_set_options (sslctx, SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv2);
ssl = SSL_new(sslctx);
SSL_set_fd(ssl, (int)vSocket );
if (SSL_connect(ssl) == -1) {
is_ssl = false;
SSL_shutdown(ssl);
SSL_free(ssl);
SetErrMsg ( true, "server doesn't work in ssl mode\n ");
return false;
}
}
return true;
} //TSocketClient::SockCreateAndConnect
// --------------------------------------------------------------------
// to write a chunk to the socket
// --------------------------------------------------------------------
bool TSocketClient::SockWriteChunk (const void* pBuffer, size_t pChunkSize, int& pBytesWritten ) {
int iStatus;
// caller safe
pBytesWritten = 0;
// precaution -------- class level
if ( !(TSocketClient::g_flgLibReady == 1 and vSocket != 0)) {
SetErrMsg ( false, "Winsock/Class not initialized" );
return false;
}
// precaution --------- function level
if ( pBuffer == NULL || pChunkSize < 0 ) {
SetErrMsg ( false, "SockWriteChunk, bad params" ); // zero bytes write is NOT an error
return false;
}
// send
iStatus = (int)sock_send ( (char*)pBuffer, pChunkSize);
if ( iStatus == SOCKET_ERROR ) {
int ret = is_ssl ? SSL_get_error(ssl, iStatus) : errno;
SetErrMsg ( true, "WSASend failed: %d\n ", ret);
return false;
}
// bytes sent
pBytesWritten = iStatus;
return true;
}
// --------------------------------------------------------------------
// to read a chunk from the socket
// --------------------------------------------------------------------
bool TSocketClient::SockReadChunk (char* pBuffer, int& pBytesRead, int pChunkSize) {
int bytes_read;
int total_read = 0;
// caller safe
pBytesRead = 0;
// precaution --------- class level
if (!(TSocketClient::g_flgLibReady == 1 and vSocket != 0)) {
SetErrMsg ( false, "Winsock/Class not initialized" );
return false;
}
// precaution --------- function level
if ( pBuffer == NULL || pChunkSize < 0 ) {
SetErrMsg ( false, "SockReadChunk, bad params" ); // zero bytes read is NOT an error
return false;
}
// recv
while(true) {
bytes_read = (int)sock_recv (&pBuffer[total_read], pChunkSize - total_read);
if ( bytes_read == SOCKET_ERROR ) { // -1 bytes read means error
SetErrMsg (true, "WSARecv failed\n ");
return false;
}
total_read = total_read + bytes_read;
if(total_read == pChunkSize)
break;
if(bytes_read == 0) {
if(total_read != pChunkSize)
return false;
else
break;
}
}
// bytes recd
pBytesRead = total_read;
return true;
}
void TSocketClient::SockClose (void) {
int iStatus;
// check if socket handle is valid
if(vSocket != INVALID_SOCKET) {
// start a graceful shutdown
shutdown (vSocket, 0x02); // SD_BOTH is equal to 0x02
// use closesocket call
#ifdef __linux__
iStatus = close(vSocket);
#else
iStatus = closesocket(vSocket);
if(WSAGetLastError () != WSANOTINITIALISED)
SetErrMsg (true, "closesocket failed.");
else
vSocket = INVALID_SOCKET;
#endif
if(iStatus == SOCKET_ERROR) // considered failed only if previously initialized
SetErrMsg ( true, "closesocket failed." );
else
vSocket = INVALID_SOCKET;
if (is_ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
}
}
}
bool TSocketClient::SetErrMsg (bool flgIncludeWin32Error, const char* pszErrMsg, ...) {
// check if error message specified or clean-up required
if(pszErrMsg) {
va_list args;
va_start (args, pszErrMsg);
vsprintf_s( vszErrMsg,pszErrMsg, args );
va_end (args);
#ifndef __linux__
if(flgIncludeWin32Error)
sprintf_s(vszErrMsg + strlen(vszErrMsg), 256 - strlen(vszErrMsg), " WSA-Win32 status: %d", WSAGetLastError());
#endif
}
else
vszErrMsg[0] = 0;
return false;
}