-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCSocket.cpp
More file actions
619 lines (588 loc) · 20 KB
/
CSocket.cpp
File metadata and controls
619 lines (588 loc) · 20 KB
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
#pragma once
#include "CSocket.h"
CThread* g_pThread;
extern CSocket* g_pSocket;
extern logprintf_t logprintf;
CSocket::CSocket()
{
// c'tor
g_pThread = new CThread();
std::fill(m_pSocket, m_pSocket+(sizeof(m_pSocket)/4), -1);
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
int err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0 || LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
logprintf("Winsock failed to initialize: %d\n", WSAGetLastError());
this->~CSocket();
}
#endif
}
CSocket::~CSocket()
{
// d'tor
#ifdef WIN32
WSACleanup();
#endif
for(int i = 0;i < MAX_SOCK;i++) {
if(m_pSocket[i] != (-1)) {
destroy_socket(i);
}
}
//g_pThread->Kill(m_ThreadHandle[]);
}
int CSocket::create_socket(int type)
{
int iSlot = find_free_slot(m_pSocket, (sizeof(m_pSocket)/4));
if(type == SOCK_STREAM)
m_pSocketInfo[iSlot].tcp = true;
else
m_pSocketInfo[iSlot].tcp = false;
m_pSocket[iSlot] = socket(AF_INET, type, 0);
if(m_pSocket[iSlot] == -1) {
logprintf("socket_create(): Failed.");
return (-1);
}
m_pSocketInfo[iSlot].success = true; // initiates that the socket has been successfully created
m_pSocketInfo[iSlot].ssl = false;
strcpy(m_pSocketInfo[iSlot].bind_ip, "0.0.0.0");
return iSlot;
}
int CSocket::find_free_slot(int* arr, int size)
{
for(int i = 0;i < size;i++) {
if(arr[i] == (-1)) {
return i;
}
}
return 0xFFFF;
}
int CSocket::set_max_connections(int socketid, int maxcon)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("set_max_connections(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
//m_pSocketInfo[socketid].max_clients = new int[maxcon];
m_pSocketInfo[socketid].connected_clients = (int*)malloc(sizeof(int)*maxcon);
std::fill(m_pSocketInfo[socketid].connected_clients, m_pSocketInfo[socketid].connected_clients+maxcon, INVALID_CLIENT_ID);
m_pSocketInfo[socketid].max_clients = maxcon;
if(m_pSocketInfo[socketid].ssl) m_pSocketInfo[socketid].ssl_clients = (SSL**)malloc(sizeof(SSL*)*maxcon);
return 1;
}
int CSocket::connect_socket(int socketid, char* address, int port)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("socket_connect(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
struct sockaddr_in addr;
//struct hostent *host;
//host = gethostbyname(address);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(address);
addr.sin_port = htons(port);
//addr.sin_addr = *((struct in_addr *) host->h_addr);
if(strcmp(m_pSocketInfo[socketid].bind_ip, "0.0.0.0") != 0) {
sockaddr_in bind_addr;
bind_addr.sin_addr.s_addr = inet_addr(m_pSocketInfo[socketid].bind_ip);
bind_addr.sin_family = AF_INET;
if(bind(m_pSocket[socketid], (struct sockaddr *)&bind_addr, sizeof(bind_addr)) == SOCKET_ERROR) {
logprintf("socket_connect(): Socket ID %d has failed to bind. (IP %s, Port %s).", socketid, m_pSocketInfo[socketid].bind_ip);
return 0;
}
}
if(connect(m_pSocket[socketid], (struct sockaddr *)&addr, sizeof(sockaddr)) == SOCKET_ERROR) {
logprintf("socket_connect(): Socket ID %d has failed to connect.", socketid);
return 0;
}
//set_nonblocking_socket(m_pSocket[socketid]);
m_pSocketInfo[socketid].is_client = true; // that way the thread knows the socket will act as a client
m_pSocketInfo[socketid].active_thread = true;
if(!m_pSocketInfo[socketid].ssl) {
set_nonblocking_socket(m_pSocket[socketid]);
g_pThread->Start(socket_receive_thread, (void*)socketid);
}
return 1;
}
int CSocket::listen_socket(int socketid, int port)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("socket_listen(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
struct sockaddr_in addr;
memset((char *)&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if(strcmp(m_pSocketInfo[socketid].bind_ip, "0.0.0.0") != 0)
addr.sin_addr.s_addr = htonl(INADDR_ANY);
else
addr.sin_addr.s_addr = inet_addr(m_pSocketInfo[socketid].bind_ip);
if(bind(m_pSocket[socketid], (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) {
logprintf("socket_listen(): Socket has failed to bind. (IP %s, Port %d)", m_pSocketInfo[socketid].bind_ip, port);
logprintf(" The port might be already in use.");
return 0;
}
if(m_pSocketInfo[socketid].tcp) {
if(listen(m_pSocket[socketid], 10) == SOCKET_ERROR) {
logprintf("socket_listen(): Socket has failed to listen on port %d.", port);
logprintf(" The port might be already in use.");
return 0;
}
}
m_pSocketInfo[socketid].is_client = false; // that way the thread knows the socket will act as a server
m_pSocketInfo[socketid].listen = true;
m_pSocketInfo[socketid].active_thread = true;
if(m_pSocketInfo[socketid].tcp)
g_pThread->Start(socket_connection_thread, (void*)socketid);
g_pThread->Start(socket_receive_thread, (void*)socketid);
return 1;
}
int CSocket::stop_listen_socket(int socketid)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("stop_listen_socket(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
close_socket(m_pSocket[socketid]);
m_pSocketInfo[socketid].listen = false;
m_pSocketInfo[socketid].active_thread = false;
return 1;
}
int CSocket::bind_socket(int socketid, char* ip)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("socket_bind(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
strcpy(m_pSocketInfo[socketid].bind_ip, ip);
return 1;
}
int CSocket::destroy_socket(int socketid)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("socket_destroy(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
m_pSocketInfo[socketid].active_thread = false;
m_pSocketInfo[socketid].listen = false;
if(!m_pSocketInfo[socketid].is_client) {
for(int i = 0;i < m_pSocketInfo[socketid].max_clients;i++) {
if(m_pSocketInfo[socketid].connected_clients[i] != INVALID_CLIENT_ID) {
if(m_pSocketInfo[socketid].ssl) SSL_free(m_pSocketInfo[socketid].ssl_clients[i]);
close_socket(m_pSocketInfo[socketid].connected_clients[i]);
m_pSocketInfo[socketid].connected_clients[i] = INVALID_CLIENT_ID;
}
}
if(m_pSocketInfo[socketid].ssl) free(m_pSocketInfo[socketid].ssl_clients);
free(m_pSocketInfo[socketid].connected_clients);
}
close_socket(m_pSocket[socketid]);
if(m_pSocketInfo[socketid].ssl)
SSL_CTX_free(m_pSocketInfo[socketid].ssl_context);
//if(m_pSocketInfo[socketid].ssl && m_pSocketInfo[socketid].is_client) SSL_free(m_pSocketInfo[socketid].ssl_handle);
m_pSocketInfo[socketid].max_clients = 0;
m_pSocket[socketid] = (-1);
//g_pThread->Kill(m_pSocketInfo[socketid].con);
return 1;
}
int CSocket::close_remote_connection(int socketid, int remote_client_id)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("close_remote_connection(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
int *rem_client = &m_pSocketInfo[socketid].connected_clients[remote_client_id];
if(*rem_client != (-1)) {
if(m_pSocketInfo[socketid].ssl) SSL_free(m_pSocketInfo[socketid].ssl_clients[remote_client_id]);
//shutdown(*rem_client, SD_BOTH);
close_socket(*rem_client);
*rem_client = (-1);
}
return 1;
}
int CSocket::send_socket(int socketid, char* data, int len)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("socket_send(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
if(!m_pSocketInfo[socketid].ssl)
return send(m_pSocket[socketid], data, len, 0);
else
return SSL_write(m_pSocketInfo[socketid].ssl_handle, data, len);
}
int CSocket::sendto_socket(int socketid, char* ip, int port, char* data, int len)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("socket_send(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(ip);
return sendto(m_pSocket[socketid], data, len, 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
}
//return sendto(m_pSocket[socketid], data, len, 0, (struct sockaddr *)&addr, sizeof(addr));
int CSocket::sendto_remote_client(int socketid, int remote_clientid, char* data)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("sendto_remote_client(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
if(m_pSocketInfo[socketid].connected_clients[remote_clientid] != (INVALID_CLIENT_ID))
if(!m_pSocketInfo[socketid].ssl)
return send(m_pSocketInfo[socketid].connected_clients[remote_clientid], data, strlen(data), 0);
else
return SSL_write(m_pSocketInfo[socketid].ssl_clients[remote_clientid], data, strlen(data));
return 0;
}
int CSocket::socket_send_array(int socketid, cell* aData, int size)
{
if(m_pSocket[socketid] == -1) {
logprintf("socket_send_array(): Socket ID %d is invalid", socketid);
return 0;
}
char buf[1024];
memset(buf, '\0', sizeof(buf));
memcpy(buf, aData, size*4);
if(!m_pSocketInfo[socketid].ssl)
return send(m_pSocket[socketid], buf, size, 0);
else
return SSL_write(m_pSocketInfo[socketid].ssl_handle, buf, size);
}
int CSocket::is_remote_client_connected(int socketid, int remote_clientid)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("is_remote_client_connected(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
char tempbuf[1024];
if(remote_clientid <= m_pSocketInfo[socketid].max_clients && m_pSocketInfo[socketid].connected_clients[remote_clientid] != (-1)) {
if(m_pSocketInfo[socketid].ssl) {
if(recv(m_pSocketInfo[socketid].connected_clients[remote_clientid], tempbuf, sizeof(tempbuf), NULL) != 0)
return 1;
} else {
if(SSL_read(m_pSocketInfo[socketid].ssl_clients[remote_clientid], tempbuf, 1024) != 0)
return 1;
}
}
return 0;
}
// (c) Bjorn R1eese
int CSocket::set_nonblocking_socket(int fd)
{
DWORD flags;
#ifdef WIN32
flags = 1;
return ioctlsocket(fd, FIONBIO, &flags);
#else
/* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
flags = 0;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
fcntl(fd, F_SETFL, O_NONBLOCK);
#endif
}
int CSocket::is_socket_valid(int socketid)
{
if(socketid > MAX_SOCK || socketid < 0)
return 0;
return ((m_pSocket[socketid] != -1) ? 1 : 0);
}
char* CSocket::get_remote_client_ip(int socketid, int remote_clientid)
{
if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) {
logprintf("get_remote_client_ip(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
struct sockaddr_in peer_addr;
#ifdef WIN32
int cLen = sizeof(peer_addr);
#else
size_t cLen = sizeof(peer_addr);
#endif
getpeername(m_pSocketInfo[socketid].connected_clients[remote_clientid], (struct sockaddr *)&peer_addr, &cLen);
return inet_ntoa(peer_addr.sin_addr);
}
void CSocket::close_socket(int socket)
{
#ifdef WIN32
closesocket(socket);
#else
close(socket);
#endif
}
int CSocket::ssl_create(int socketid, int method)
{
if(m_pSocket[socketid] == -1) {
logprintf("ssl_create(): Socket ID %d does not exist.", socketid);
return 0;
}
#if (defined(WIN32) || defined(_WIN32) || defined(_WIN64))
const SSL_METHOD *ssl_method;
#else
SSL_METHOD **ssl_method;
#endif
ssl_method = ((method) ? SSLv23_server_method() : SSLv23_client_method());
m_pSocketInfo[socketid].ssl_context = SSL_CTX_new(ssl_method);
if (m_pSocketInfo[socketid].ssl_context == NULL) {
ERR_print_errors_fp(stderr);
return 0;
}
if(!method) {
m_pSocketInfo[socketid].ssl_handle = SSL_new(m_pSocketInfo[socketid].ssl_context);
if(m_pSocketInfo[socketid].ssl_handle == NULL) {
ERR_print_errors_fp(stderr);
return 0;
}
}
m_pSocketInfo[socketid].ssl = true;
return 1;
}
int CSocket::ssl_load_cert(int socketid, char* szCert, char* szKey)
{
if(m_pSocket[socketid] == -1) {
logprintf("ssl_load_certificate(): Socket ID %d does not exist.", socketid);
return 0;
}
int cert_ret, key_ret;
cert_ret = SSL_CTX_use_certificate_file(m_pSocketInfo[socketid].ssl_context, szCert, SSL_FILETYPE_PEM);
key_ret = SSL_CTX_use_PrivateKey_file(m_pSocketInfo[socketid].ssl_context, szKey, SSL_FILETYPE_PEM);
if((cert_ret && key_ret) && SSL_CTX_check_private_key(m_pSocketInfo[socketid].ssl_context))
return 1;
else
return 0;
}
int CSocket::ssl_connect(int socketid)
{
if(m_pSocket[socketid] == -1) {
logprintf("ssl_connect(): Socket ID %d does not exist.", socketid);
return 0;
}
if (!SSL_set_fd(m_pSocketInfo[socketid].ssl_handle, m_pSocket[socketid])) {
ERR_print_errors_fp(stderr);
return 0;
}
int a = SSL_connect(m_pSocketInfo[socketid].ssl_handle);
if (a != 1) {
logprintf("ssl_connect(): Something has gone wrong %d (Error: %d).", a, SSL_get_error(m_pSocketInfo[socketid].ssl_handle, a));
ERR_print_errors_fp(stderr);
return 0;
}
g_pThread->Start(socket_receive_thread, (void*)socketid);
return 1;
}
int CSocket::ssl_set_mode(int socketid, int mode)
{
if(m_pSocket[socketid] == -1) {
logprintf("ssl_set_mode(): Socket ID %d does not exist.", socketid);
return 0;
}
return SSL_CTX_set_mode(m_pSocketInfo[socketid].ssl_context, mode);
}
int CSocket::ssl_set_timeout(int socketid, DWORD dwInterval)
{
if(m_pSocket[socketid] == -1) {
logprintf("ssl_set_accept_timeout(): Socket ID %d doesn't exist or hasn't been created yet.", socketid);
return 0;
}
if (setsockopt(m_pSocket[socketid], SOL_SOCKET, SO_RCVTIMEO, (char *)&dwInterval, sizeof(dwInterval)) == -1)
return 0;
else
return 1;
}
int CSocket::ssl_get_peer_certificate(int socketid, int method, int remote_clientid, char* szIssuer, char* szSubject)
{
X509 *cert;
if(!method)
cert = SSL_get_peer_certificate(m_pSocketInfo[socketid].ssl_handle);
else
cert = SSL_get_peer_certificate(m_pSocketInfo[socketid].ssl_clients[remote_clientid]);
if(cert != NULL) {
strcpy(szIssuer, X509_NAME_oneline(X509_get_subject_name(cert), 0, 0));
strcpy(szSubject, X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0));
X509_free(cert);
return 1;
}
return 0;
}
void CSocket::ssl_init()
{
OpenSSL_add_all_algorithms();
SSL_library_init();
SSL_load_error_strings();
}
#ifdef WIN32
DWORD socket_connection_thread(void* lpParam)
#else
void* socket_connection_thread(void* lpParam)
#endif
{
int sockID = (int)lpParam;
sockaddr_in remote_client;
#ifdef WIN32
int cLen = sizeof(remote_client);
#else
size_t cLen = sizeof(remote_client);
#endif
do {
if(g_pSocket->m_pSocketInfo[sockID].listen) {
int client = accept(g_pSocket->m_pSocket[sockID], (sockaddr*)&remote_client, &cLen);
int slot = g_pSocket->find_free_slot(g_pSocket->m_pSocketInfo[sockID].connected_clients, g_pSocket->m_pSocketInfo[sockID].max_clients);
if(client != NULL && client != SOCKET_ERROR && slot != 0xFFFF) {
if(g_pSocket->m_pSocketInfo[sockID].ssl) {
//logprintf("SSL client detected (Slot %d)", slot);
g_pSocket->m_pSocketInfo[sockID].ssl_clients[slot] = SSL_new(g_pSocket->m_pSocketInfo[sockID].ssl_context);
int ret_fd = SSL_set_fd(g_pSocket->m_pSocketInfo[sockID].ssl_clients[slot], client);
if(SSL_accept(g_pSocket->m_pSocketInfo[sockID].ssl_clients[slot]) != 1) {
SSL_free(g_pSocket->m_pSocketInfo[sockID].ssl_clients[slot]);
g_pSocket->close_socket(client);
continue;
}
}
g_pSocket->set_nonblocking_socket(client);
g_pSocket->m_pSocketInfo[sockID].connected_clients[slot] = client;
remoteConnect pData;
pData.remote_client = (char*)malloc(sizeof(char*)*15); // yeh, no IPv6 support /o
strcpy(pData.remote_client, inet_ntoa(remote_client.sin_addr));
pData.remote_clientid = slot;
pData.socketid = sockID;
onRemoteConnect.push(pData);
} else {
if(slot == 0xFFFF) {
send(client, "-- Server is full", 17, 0);
if(m_pSocket[sockID] != (-1)) {
logprintf("onSocketRemoteFail(Socket: %d)\r\n -- Maximum connection limit exceeded. (Limit is %d)\r\n -- Consider using 'socket_set_max_connections()' to increase the limit.", sockID, g_pSocket->m_pSocketInfo[sockID].max_clients);
}
}
g_pSocket->close_socket(client);
/*
onSocketRemoteFail(Socket:id, remote_client[], remote_clientid)
*/
}
}
SLEEP(500);
} while(g_pSocket->m_pSocketInfo[sockID].active_thread);
#ifdef WIN32
return 1;
#endif
}
#ifdef WIN32
DWORD socket_receive_thread(void* lpParam)
#else
void* socket_receive_thread(void* lpParam)
#endif
{
int sockID = (int)lpParam,
iHandle,
maxClients;
bool sockType = g_pSocket->m_pSocketInfo[sockID].is_client;
bool tcpProtocol = g_pSocket->m_pSocketInfo[sockID].tcp;
char szBuffer[2048];
iHandle = g_pSocket->m_pSocket[sockID];
// logprintf("Thread started with socket id %d", sockID);
memset(szBuffer, '\0', sizeof(szBuffer));
if(!sockType)
maxClients = g_pSocket->m_pSocketInfo[sockID].max_clients;
do {
if(!tcpProtocol) {
// udp
struct sockaddr_in remote_client;
#ifdef WIN32
int client_len = sizeof(remote_client);
#else
size_t client_len = sizeof(remote_client);
#endif
int byte_len = recvfrom(iHandle, szBuffer, 2048, 0, (struct sockaddr*)&remote_client, &client_len);
if(byte_len > 0) {
socketUDP pData;
szBuffer[byte_len] = '\0';
if(strlen(szBuffer) != byte_len) {
memset(pData.arr, '\0', byte_len+1);
for(int i = 0;i < byte_len;i++) {
pData.arr[i] = szBuffer[i];
}
pData.is_arr = true;
} else {
pData.data = (char*)malloc(byte_len);
strcpy(pData.data, szBuffer);
pData.is_arr = false;
}
pData.data_len = byte_len;
pData.remote_ip = (char*)malloc(sizeof(char*)*15);
strcpy(pData.remote_ip, inet_ntoa(remote_client.sin_addr));
pData.remote_port = ntohs(remote_client.sin_port);
pData.socketid = sockID;
onUDPReceiveData.push(pData);
}
SLEEP(200); // 200
continue; // skip further processing
}
if(sockType) {
// tcp client
int byte_len;
if(g_pSocket->m_pSocketInfo[sockID].ssl)
byte_len = SSL_read(g_pSocket->m_pSocketInfo[sockID].ssl_handle, szBuffer, 1024);
else
byte_len = recv(iHandle, szBuffer, sizeof(szBuffer), NULL);
if(byte_len > 0) {
//remove_newline(szBuffer);
szBuffer[byte_len] = '\0';
socketAnswer pData;
pData.socketid = sockID;
pData.data_len = byte_len;
pData.data = (char*)malloc(sizeof(char*)*byte_len);
strcpy(pData.data, szBuffer);
onSocketAnswer.push(pData);
}
if(!byte_len) {
socketClose pData;
pData.socketid = sockID;
if(g_pSocket->m_pSocketInfo[sockID].ssl) SSL_free(g_pSocket->m_pSocketInfo[sockID].ssl_handle);
onSocketClose.push(pData);
g_pSocket->destroy_socket(sockID);
}
} else {
// tcp server
for(int i = 0;i < maxClients;i++) { // loop through all connected clients
iHandle = g_pSocket->m_pSocketInfo[sockID].connected_clients[i];
if(iHandle != (-1)) {
int byte_len;
if(g_pSocket->m_pSocketInfo[sockID].ssl)
byte_len = SSL_read(g_pSocket->m_pSocketInfo[sockID].ssl_clients[i], szBuffer, 1024);
else
byte_len = recv(iHandle, szBuffer, sizeof(szBuffer), NULL);
if(byte_len > 0) {
//remove_newline(szBuffer);
szBuffer[byte_len] = '\0';
receiveData pData;
pData.data = (char*)malloc(sizeof(char*)*byte_len);
strcpy(pData.data, szBuffer);
pData.socketid = sockID;
pData.remote_clientid = i;
pData.data_len = byte_len;
onSocketReceiveData.push(pData);
}
if(!byte_len) {
remoteDisconnect pData;
pData.remote_clientid = i;
pData.socketid = sockID;
if(g_pSocket->m_pSocketInfo[sockID].ssl) SSL_free(g_pSocket->m_pSocketInfo[sockID].ssl_clients[i]);
g_pSocket->close_socket(iHandle);
g_pSocket->m_pSocketInfo[sockID].connected_clients[i] = INVALID_CLIENT_ID; // connection has dropped
onRemoteDisconnect.push(pData);
}
}
}
}
SLEEP(200);
} while(g_pSocket->m_pSocketInfo[sockID].active_thread);
#ifdef WIN32
return 1;
#endif
}