forked from e2guardian/e2guardian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseSocket.cpp
617 lines (559 loc) · 16.3 KB
/
BaseSocket.cpp
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
// Base socket class - inherit this to implement UNIX/INET domain sockets
// For all support, instructions and copyright go to:
// http://e2guardian.org/
// Released under the GPL v2, with the OpenSSL exception described in the README file.
// INCLUDES
#ifdef HAVE_CONFIG_H
#include "e2config.h"
#endif
#include <csignal>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pwd.h>
#include <cerrno>
#include <unistd.h>
#include <stdexcept>
#include <syslog.h>
#include <sys/select.h>
#ifdef NETDEBUG
#include <iostream>
#endif
#include "BaseSocket.hpp"
// GLOBALS
extern bool reloadconfig;
extern thread_local std::string thread_id;
// DEFINITIONS
#define dgtimercmp(a, b, cmp) \
(((a)->tv_sec == (b)->tv_sec) ? ((a)->tv_usec cmp(b)->tv_usec) : ((a)->tv_sec cmp(b)->tv_sec))
#define dgtimersub(a, b, result) \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
if ((result)->tv_usec < 0) { \
(result)->tv_sec--; \
(result)->tv_usec += 1000000; \
}
// IMPLEMENTATION
// This class contains client and server socket init and handling
// code as well as functions for testing and working with the socket FDs.
// constructor - override this if desired to create an actual socket at startup
BaseSocket::BaseSocket()
: timeout(5000), sck(-1), buffstart(0), bufflen(0)
{
infds[0].fd = -1;
outfds[0].fd = -1;
infds[0].events = POLLIN;
outfds[0].events = POLLOUT;
isclosing = false;
timedout = false;
sockerr = false;
ishup = false;
s_errno = 0;
}
// create socket from FD - must be overridden to clear the relevant address structs
BaseSocket::BaseSocket(int fd)
: timeout(5000), buffstart(0), bufflen(0)
{
sck = fd;
infds[0].fd = fd;
outfds[0].fd = fd;
infds[0].events = POLLIN;
outfds[0].events = POLLOUT;
isclosing = false;
timedout = false;
sockerr = false;
ishup = false;
s_errno = 0;
}
// destructor - close socket
BaseSocket::~BaseSocket()
{
// close fd if socket not used
if (sck > -1) {
::close(sck);
}
}
// reset - close socket & reset timeout.
// call this in derived classes' reset() method, which should also clear address structs
void BaseSocket::baseReset()
{
if (sck > -1) {
::close(sck);
sck = -1;
infds[0].fd = -1;
outfds[0].fd = -1;
}
// timeout = 5000; // this line overwrites connecttimeout setting so leave out for now.
buffstart = 0;
bufflen = 0;
isclosing = false;
timedout = false;
sockerr = false;
ishup = false;
s_errno = 0;
}
// mark a socket as a listening server socket
int BaseSocket::listen(int queue)
{
return ::listen(sck, queue);
}
// "template adaptor" for accept - basically, let G++ do the hard work of
// figuring out the type of the third parameter ;)
template <typename T>
inline int local_accept_adaptor(int (*accept_func)(int, struct sockaddr *, T),
int sck, struct sockaddr *acc_adr, socklen_t *acc_adr_length)
{
return accept_func(sck, acc_adr, (T)acc_adr_length);
}
// receive an incoming connection & return FD
// call this in accept methods of derived classes, which should pass in empty sockaddr & socklen_t to be filled out
int BaseSocket::baseAccept(struct sockaddr *acc_adr, socklen_t *acc_adr_length)
{
// OS X defines accept as:
// int accept(int s, struct sockaddr *addr, int *addrlen);
// but everyone else as:
// int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
// NB: except 10.4, which seems to use the more standard definition. grrr.
return local_accept_adaptor(::accept, sck, acc_adr, acc_adr_length);
}
// return socket's FD - please use sparingly and DO NOT do manual data transfer using it
int BaseSocket::getFD()
{
return sck;
}
// close the socket
void BaseSocket::close()
{
if (sck > -1) {
::close(sck);
sck = -1;
infds[0].fd = -1;
outfds[0].fd = -1;
}
buffstart = 0;
bufflen = 0;
isclosing = false;
timedout = false;
sockerr = false;
ishup = false;
}
// set the socket-wide timeout
void BaseSocket::setTimeout(int t)
{
timeout = t;
}
int BaseSocket::getErrno()
{
return s_errno;
}
// return timeout
int BaseSocket::getTimeout()
{
return timeout;
}
bool BaseSocket::isOpen()
{
return (sck > -1);
}
bool BaseSocket::isClosing()
{
return isclosing;
}
bool BaseSocket::sockError()
{
return sockerr;
}
bool BaseSocket::isTimedout()
{
return timedout;
}
bool BaseSocket::isHup()
{
return ishup;
}
bool BaseSocket::isNoOpp()
{
return (timedout || sockerr || (ishup && !isclosing) || (sck < 0));
}
bool BaseSocket::isNoRead()
{
return ( sockerr || (sck < 0));
}
bool BaseSocket::isNoWrite()
{
return ( sockerr || ishup || (sck < 0));
}
// blocking check to see if there is data waiting on socket
bool BaseSocket::bcheckSForInput(int timeout)
{
if (isNoRead())
return false;
int rc;
s_errno = 0;
errno = 0;
rc = poll(infds, 1, timeout);
if (rc == 0)
{
timedout = true;
return false; //timeout
}
timedout = false;
if (rc < 0)
{
s_errno = errno;
sockerr = true;
return false;
}
if (infds[0].revents & POLLHUP) {
ishup = true;
}
if ((infds[0].revents & (POLLHUP | POLLIN))) {
return true;
}
sockerr = true;
return false; // must be POLLERR or POLLNVAL
}
// blocking check to see if there is data waiting on socket
bool BaseSocket::bcheckForInput(int timeout)
{
if ((bufflen - buffstart) > 0)
return true;
if (isNoRead())
return false;
int rc;
s_errno = 0;
errno = 0;
rc = poll(infds, 1, timeout);
if (rc == 0)
{
timedout = true;
return false; //timeout
}
timedout = false;
if (rc < 0)
{
s_errno = errno;
sockerr = true;
return false;
}
if (infds[0].revents & POLLHUP) {
ishup = true;
}
if ((infds[0].revents & (POLLHUP | POLLIN))) {
return true;
}
sockerr = true;
return false; // must be POLLERR or POLLNVAL
}
// blocking check for waiting data - blocks for up to given timeout, can be told to break on signal-triggered config reloads
bool BaseSocket::checkForInput()
{
if ((bufflen - buffstart) > 0)
return true;
if (isNoRead())
return false;
int rc;
s_errno = 0;
errno = 0;
rc = poll(infds, 1, 0);
if (rc == 0)
{
return false; //timeout
}
timedout = false;
if (rc < 0)
{
s_errno = errno;
sockerr = true;
return false;
}
if (infds[0].revents & POLLHUP) {
ishup = true;
}
if (infds[0].revents & (POLLHUP | POLLIN)) {
return true;
}
sockerr = true;
return false; // must be POLLERR or POLLNVAL
}
// non-blocking check to see if a socket is ready to be written //NOT EVER USED - not it is used in Socket.cpp
bool BaseSocket::readyForOutput()
{
if (isNoWrite())
return false;
int rc;
s_errno = 0;
errno = 0;
rc = poll(outfds,1, 0);
if (rc == 0)
{
return false;
}
timedout = false;
if (rc < 0)
{
s_errno = errno;
sockerr = true;
return false;
}
if (outfds[0].revents & POLLOUT)
return true;
if (outfds[0].revents & POLLHUP)
ishup = true;
return false;
}
bool BaseSocket::breadyForOutput(int timeout) {
if (isNoWrite())
return false;
int rc;
s_errno = 0;
errno = 0;
rc = poll(outfds, 1, timeout);
if (rc == 0) {
timedout = true;
return false;
}
timedout = false;
if (rc < 0) {
s_errno = errno;
sockerr = true;
return false;
}
if (outfds[0].revents & POLLOUT)
return true;
if (outfds[0].revents & POLLHUP)
ishup = true;
return false;
}
// read a line from the socket, can be told to break on config reloads
int BaseSocket::getLine(char *buff, int size, int timeout, bool honour_reloadconfig, bool *chopped, bool *truncated) //throw(std::exception)
{
try {
// first, return what's left from the previous buffer read, if anything
int i = 0;
if ((bufflen - buffstart) > 0) {
#ifdef NETDEBUG
std::cout << thread_id << "data already in buffer; bufflen: " << bufflen << " buffstart: " << buffstart << std::endl;
#endif
//work out the maximum size we want to read from our internal buffer
int tocopy = size - 1;
if ((bufflen - buffstart) < tocopy)
tocopy = bufflen - buffstart;
//copy the data to output buffer (up to 8192 chars in loglines case)
char *result = (char *) memccpy(buff, buffer + buffstart, '\n', tocopy);
//if the result was < max size
//if the result WAS null this indicates a full buffer copy
if (result != NULL) {
// indicate that a newline was chopped off, if desired
if (chopped)
*chopped = true;
//make the last char a null
*(--result) = '\0';
buffstart += (result - buff) + 1;
return result - buff;
} else {
i += tocopy;
buffstart += tocopy;
}
}
while (i < (size - 1)) {
buffstart = 0;
bufflen = 0;
// try {
s_errno = 0;
errno = 0;
if (bcheckForInput(timeout))
bufflen = recv(sck, buffer, 1024, 0);
// } catch (std::exception &e) {
// throw std::runtime_error(std::string("Can't read from socket: ") + e.what()); // on error
// }
#ifdef NETDEBUG
std::cout << thread_id << "getLine !SSL read into buffer; bufflen: " << bufflen << std::endl;
#endif
//if there was a socket error
if (bufflen < 0) {
#ifdef NETDEBUG
std::cout << thread_id << "getLine Can't read from socket !SSL: " << std::endl;
#endif
s_errno = errno;
return -1;
// throw std::runtime_error(std::string("Can't read from socket: ") + strerror(errno)); // on error
}
//if socket closed...
if (bufflen == 0) {
buff[i] = '\0'; // ...terminate string & return what read
#ifdef NETDEBUG
std::cout << thread_id << "getLine terminate string !SSL: " << i << std::endl;
#endif
if (truncated)
*truncated = true;
return i;
}
int tocopy = bufflen;
if ((i + bufflen) > (size - 1))
tocopy = (size - 1) - i;
char *result = (char *) memccpy(buff + i, buffer, '\n', tocopy);
if (result != NULL) {
#ifdef NETDEBUG
std::cout << thread_id << "getLine result1 !SSL: " << result << i << std::endl;
#endif
// indicate that a newline was chopped off, if desired
if (chopped)
*chopped = true;
*(--result) = '\0';
buffstart += (result - (buff + i)) + 1;
#ifdef NETDEBUG
std::cout << thread_id << "getLine result2 !SSL: " << result << std::endl;
#endif
return i + (result - (buff + i));
}
i += tocopy;
}
// oh dear - buffer end reached before we found a newline
buff[i] = '\0';
if (truncated)
*truncated = true;
#ifdef NETDEBUG
if (truncated)
std::cout << thread_id << "Getline(SSL) truncated buffer end reached before we found a newline: " << buff << " Line: " << __LINE__ << " Function: " << __func__ << std::endl;;
#endif
return i;
} catch (...) {
return -1;
}
}
// write line to socket
bool BaseSocket::writeString(const char *line) //throw(std::exception)
{
int l = strlen(line);
return writeToSocket(line, l, 0, timeout);
}
// write data to socket - throws exception on failure, can be told to break on config reloads
//void BaseSocket::writeToSockete(const char *buff, int len, unsigned int flags, int timeout, bool honour_reloadconfig) throw(std::exception)
void BaseSocket::writeToSockete(const char *buff, int len, unsigned int flags, int timeout, bool honour_reloadconfig)
{
if (!writeToSocket(buff, len, flags, timeout, honour_reloadconfig)) {
throw std::runtime_error(std::string("Can't write to socket: ") + strerror(errno));
}
}
// write data to socket - can be told not to do an initial readyForOutput, and to break on config reloads
bool BaseSocket::writeToSocket(const char *buff, int len, unsigned int flags, int timeout, bool check_first, bool honour_reloadconfig)
{
int actuallysent = 0;
int sent;
while (actuallysent < len) {
if (check_first) {
// try {
//readyForOutput(timeout, honour_reloadconfig); // throws exception on error or timeoutI/
//} catch (std::exception &e) {
// return false;
//}
if(!breadyForOutput(timeout))
return false;
}
sent = 0;
s_errno = 0;
errno = 0;
if(!isNoWrite()) sent = send(sck, buff + actuallysent, len - actuallysent, 0);
// if (sent == 0)
if (sent < 1)
{
s_errno = errno;
return false; // other end is closed
}
actuallysent += sent;
}
return true;
}
// read a specified expected amount and return what actually read
int BaseSocket::readFromSocketn(char *buff, int len, unsigned int flags, int timeout)
{
int cnt, rc;
cnt = len;
// first, return what's left from the previous buffer read, if anything
if ((bufflen - buffstart) > 0) {
#ifdef NETDEBUG
std::cout << thread_id << "readFromSocketn: data already in buffer; bufflen: " << bufflen << " buffstart: " << buffstart << std::endl;
#endif
int tocopy = len;
if ((bufflen - buffstart) < len)
tocopy = bufflen - buffstart;
memcpy(buff, buffer + buffstart, tocopy);
cnt -= tocopy;
buffstart += tocopy;
buff += tocopy;
if (cnt == 0)
return len;
}
while (cnt > 0) {
// try {
// checkForInput(timeout); // throws exception on error or timeout
// } catch (std::exception &e) {
// return -1;
// }
// if (isNoRead()) return -1;
if (!bcheckForInput(timeout)) return -1;
s_errno = 0;
errno = 0;
rc = recv(sck, buff, cnt, flags);
if (rc < 0) {
//i if (errno == EINTR) {
// continue;
// }
s_errno = errno;
return -1;
}
if (rc == 0) { // eof
return len - cnt;
}
buff += rc;
cnt -= rc;
}
return len;
}
// read what's available and return error status - can be told not to do an initial checkForInput, and to break on reloads
int BaseSocket::readFromSocket(char *buff, int len, unsigned int flags, int timeout, bool check_first, bool honour_reloadconfig)
{
// first, return what's left from the previous buffer read, if anything
int cnt = len;
int tocopy = 0;
if ((bufflen - buffstart) > 0) {
tocopy = len;
#ifdef NETDEBUG
std::cout << thread_id << "readFromSocket: data already in buffer; bufflen: " << bufflen << " buffstart: " << buffstart << std::endl;
#endif
if ((bufflen - buffstart) < len)
tocopy = bufflen - buffstart;
memcpy(buff, buffer + buffstart, tocopy);
cnt -= tocopy;
buffstart += tocopy;
buff += tocopy;
if (cnt == 0)
return len;
}
int rc = 0;
if (check_first) {
// try {
// checkForInput(timeout, honour_reloadconfig);
// } catch (std::exception &e) {
// return -1;
// }
if (! bcheckForInput(timeout))
return -1;
}
while (true) {
if (isNoRead()) return -1;
s_errno = 0;
errno = 0;
rc = recv(sck, buff, cnt, flags);
if (rc < 0) {
// if (errno == EINTR ) {
// .. continue;
// }
s_errno = errno;
return -1;
}
break;
}
return rc + tocopy;
}