Debian Codegraph @

src/BaseSocket.cpp

raw
1 // Base socket class - inherit this to implement UNIX/INET domain sockets
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8
9 #ifdef HAVE_CONFIG_H
10 #include "e2config.h"
11 #endif
12 #include <csignal>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <sys/time.h>
16 #include <pwd.h>
17 #include <cerrno>
18 #include <unistd.h>
19 #include <stdexcept>
20 #include <sys/select.h>
21
22 #include "BaseSocket.hpp"
23 #include "Logger.hpp"
24
25 // GLOBALS
26 extern bool reloadconfig;
27
28 // DEFINITIONS
29
30 // IMPLEMENTATION
31
32 // This class contains client and server socket init and handling
33 // code as well as functions for testing and working with the socket FDs.
34
35 // constructor - override this if desired to create an actual socket at startup
36 BaseSocket::BaseSocket()
37 {
38 infds[0].fd = -1;
39 outfds[0].fd = -1;
40 infds[0].events = POLLIN;
41 outfds[0].events = POLLOUT;
42 isclosing = false;
43 timedout = false;
44 sockerr = false;
45 ishup = false;
46 s_errno = 0;
47 }
48
49 // create socket from FD - must be overridden to clear the relevant address structs
50 BaseSocket::BaseSocket(int fd)
51 {
52 sck = fd;
53 infds[0].fd = fd;
54 outfds[0].fd = fd;
55 infds[0].events = POLLIN;
56 outfds[0].events = POLLOUT;
57 isclosing = false;
58 timedout = false;
59 sockerr = false;
60 ishup = false;
61 s_errno = 0;
62 }
63
64 // destructor - close socket
65 BaseSocket::~BaseSocket()
66 {
67 // close fd if socket not used
68 if (sck > -1) {
69 ::close(sck);
70 }
71 }
72
73 // reset - close socket & reset timeout.
74 // call this in derived classes' reset() method, which should also clear address structs
75 void BaseSocket::baseReset()
76 {
77 if (sck > -1) {
78 ::close(sck);
79 sck = -1;
80 infds[0].fd = -1;
81 outfds[0].fd = -1;
82 }
83 // timeout = 5000; // commented out so that timeout can be set before a connect call
84 buffstart = 0;
85 bufflen = 0;
86 isclosing = false;
87 timedout = false;
88 sockerr = false;
89 ishup = false;
90 s_errno = 0;
91 }
92
93 // mark a socket as a listening server socket
94 int BaseSocket::listen(int queue)
95 {
96 return ::listen(sck, queue);
97 }
98
99 // "template adaptor" for accept - basically, let G++ do the hard work of
100 // figuring out the type of the third parameter ;)
101 template <typename T>
102 inline int local_accept_adaptor(int (*accept_func)(int, struct sockaddr *, T),
103 int sck, struct sockaddr *acc_adr, socklen_t *acc_adr_length)
104 {
105 return accept_func(sck, acc_adr, (T)acc_adr_length);
106 }
107
108 // receive an incoming connection & return FD
109 // call this in accept methods of derived classes, which should pass in empty sockaddr & socklen_t to be filled out
110 int BaseSocket::baseAccept(struct sockaddr *acc_adr, socklen_t *acc_adr_length)
111 {
112
113 return local_accept_adaptor(::accept, sck, acc_adr, acc_adr_length);
114 }
115
116 // return socket's FD - please use sparingly and DO NOT do manual data transfer using it
117 int BaseSocket::getFD()
118 {
119 return sck;
120 }
121
122 // close the socket
123 void BaseSocket::close()
124 {
125 if (sck > -1) {
126 ::close(sck);
127 sck = -1;
128 infds[0].fd = -1;
129 outfds[0].fd = -1;
130 }
131 buffstart = 0;
132 bufflen = 0;
133 isclosing = false;
134 timedout = false;
135 sockerr = false;
136 ishup = false;
137 }
138
139 // set the socket-wide timeout
140 void BaseSocket::setTimeout(int t)
141 {
142 timeout = t;
143 }
144
145 int BaseSocket::getErrno()
146 {
147 return s_errno;
148 }
149
150 // return timeout
151 int BaseSocket::getTimeout()
152 {
153 return timeout;
154 }
155
156 bool BaseSocket::isOpen()
157 {
158 return (sck > -1);
159 }
160
161 bool BaseSocket::isClosing()
162 {
163 return isclosing;
164 }
165
166 bool BaseSocket::sockError()
167 {
168 return sockerr;
169 }
170
171 bool BaseSocket::isTimedout()
172 {
173 return timedout;
174 }
175
176 bool BaseSocket::isHup()
177 {
178 return ishup;
179 }
180
181 bool BaseSocket::isNoOpp()
182 {
183 return (timedout || sockerr || (ishup && !isclosing) || (sck < 0));
184 }
185
186 bool BaseSocket::isNoRead()
187 {
188 return ( sockerr || (sck < 0));
189 }
190
191 bool BaseSocket::isNoWrite()
192 {
193 return ( sockerr || ishup || (sck < 0));
194 }
195
196
197 // blocking check to see if there is data waiting on socket
198 bool BaseSocket::checkForInput(int timeout)
199 {
200 if ((bufflen - buffstart) > 0)
201 return true; // is data left in buffer
202
203 if (isNoRead())
204 return false;
205
206 if(timeout == 0) { // no poll wanted as done by calling function
207 return false;
208 }
209
210 int rc;
211 s_errno = 0;
212 errno = 0;
213
214 rc = poll(infds, 1, timeout);
215
216 if (rc == 0)
217 {
218 timedout = true;
219 return false; //timeout
220 }
221 timedout = false;
222
223 if (rc < 0)
224 {
225 s_errno = errno;
226 sockerr = true;
227 return false;
228 }
229 if (infds[0].revents & POLLHUP) {
230 ishup = true;
231 }
232 if ((infds[0].revents & (POLLHUP | POLLIN))) {
233 return true;
234 }
235 sockerr = true;
236 return false; // must be POLLERR or POLLNVAL
237 }
238
239
240 bool BaseSocket::readyForOutput(int timeout) {
241 if (isNoWrite())
242 return false;
243 int rc;
244 s_errno = 0;
245 errno = 0;
246 rc = poll(outfds, 1, timeout);
247 if (rc == 0) {
248 timedout = true;
249 return false;
250 }
251 timedout = false;
252 if (rc < 0) {
253 s_errno = errno;
254 sockerr = true;
255 return false;
256 }
257 if (outfds[0].revents & POLLOUT)
258 return true;
259 if (outfds[0].revents & POLLHUP)
260 ishup = true;
261 return false;
262 }
263
264 // read a line from the socket
265 int BaseSocket::getLine(char *buff, int size, int timeout, bool *chopped, bool *truncated)
266 {
267 try {
268 // first, return what's left from the previous buffer read, if anything
269 int i = 0;
270 if ((bufflen - buffstart) > 0) {
271 DEBUG_network("data already in buffer; bufflen: ", bufflen, " buffstart: ", buffstart);
272
273 //work out the maximum size we want to read from our internal buffer
274 int tocopy = size - 1;
275 if ((bufflen - buffstart) < tocopy)
276 tocopy = bufflen - buffstart;
277
278 //copy the data to output buffer
279 char *result = (char *) memccpy(buff, buffer + buffstart, '\n', tocopy);
280
281 //if the result was < max size
282 //if the result WAS null this indicates a full buffer copy
283 if (result != NULL) {
284 // indicate that a newline was chopped off, if desired
285 if (chopped)
286 *chopped = true;
287
288 //make the last char a null
289 *(--result) = '\0';
290 buffstart += (result - buff) + 1;
291 return result - buff;
292 } else {
293 i += tocopy;
294 buffstart += tocopy;
295 }
296 }
297 while (i < (size - 1)) {
298 buffstart = 0;
299 bufflen = 0;
300 s_errno = 0;
301 errno = 0;
302 if (!isNoBlock) {
303 if(!checkForInput(timeout))
304 return -1;
305 }
306 bufflen = recv(sck, buffer, SCK_READ_BUFF_SIZE, 0);
307 if (bufflen < 0) {
308 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
309 if(isNoBlock) {
310 if (checkForInput(timeout)) continue;// now got some input
311 else return -1;// timed out
312 }
313 }
314 s_errno = errno;
315 return -1;
316 }
317 //if socket closed...
318 if (bufflen == 0) {
319 buff[i] = '\0'; // ...terminate string & return what read
320 DEBUG_network("getLine terminate string !SSL: ", i );
321 if (truncated)
322 *truncated = true;
323 return i;
324 }
325 int tocopy = bufflen;
326 if ((i + bufflen) > (size - 1))
327 tocopy = (size - 1) - i;
328 char *result = (char *) memccpy(buff + i, buffer, '\n', tocopy);
329 if (result != NULL) {
330 DEBUG_network("getLine result1 !SSL: ", result, i );
331 // indicate that a newline was chopped off, if desired
332 if (chopped)
333 *chopped = true;
334 *(--result) = '\0';
335 buffstart += (result - (buff + i)) + 1;
336 DEBUG_network("getLine result2 !SSL: ", result );
337 return i + (result - (buff + i));
338 }
339 i += tocopy;
340 }
341 // oh dear - buffer end reached before we found a newline
342 buff[i] = '\0';
343 if (truncated)
344 *truncated = true;
345 if (truncated)
346 DEBUG_network("Getline(SSL) truncated buffer end reached before we found a newline: ", buff );
347
348 return i;
349 } catch (...) {
350 return -1;
351 }
352 }
353
354 // write line to socket
355 bool BaseSocket::writeString(const char *line)
356 {
357 int l = strlen(line);
358 return writeToSocket(line, l, 0, timeout);
359 }
360
361 bool BaseSocket::writeToSocket(const char *buff, int len, unsigned int flags, int timeout)
362 {
363 int actuallysent = 0;
364 int sent;
365 while (actuallysent < len) {
366 sent = 0;
367 s_errno = 0;
368 errno = 0;
369 if(isNoWrite()) return false;
370 if((!isNoBlock) && doCheck) {
371 if( !readyForOutput(timeout)) {
372 s_errno = errno;
373 return false;
374 }
375 }
376 sent = send(sck, buff + actuallysent, len - actuallysent, 0);
377
378 if (sent < 1) {
379 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
380 if(isNoBlock)
381 if (readyForOutput(timeout)) continue; // now able to send
382 }
383 s_errno = errno;
384 return false; // other end is closed
385 }
386 actuallysent += sent;
387 }
388 return true;
389
390 }
391
392 // write data to socket - returns no of bytes written or 0 if would block and -1 on error
393 int BaseSocket::writeToSocketNB(const char *buff, int len, unsigned int flags)
394 {
395 int actuallysent = 0;
396 int sent;
397 while (actuallysent < len) {
398 sent = 0;
399 s_errno = 0;
400 errno = 0;
401 if(isNoWrite()) return -1;
402 if((!isNoBlock) && doCheck) {
403 if (!readyForOutput(0))
404 return 0;
405 }
406 sent = send(sck, buff + actuallysent, len - actuallysent, 0);
407
408 if (sent < 1) {
409 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
410 s_errno = errno;
411 return 0;
412 }
413 s_errno = errno;
414 return -1; // other end is closed
415 }
416 actuallysent += sent;
417 return sent;
418 }
419 return -1; //should never get here
420 }
421
422 // read a specified expected amount and return what actually read
423 int BaseSocket::readFromSocket(char *buff, int len, unsigned int flags, int timeout, bool ret_part)
424 {
425 int cnt, rc;
426 cnt = len;
427
428 // first, return what's left from the previous buffer read, if anything
429 if ((bufflen - buffstart) > 0) {
430 DEBUG_network("data already in buffer; bufflen: ", bufflen, " buffstart: ", buffstart);
431 int tocopy = len;
432 if ((bufflen - buffstart) < len)
433 tocopy = bufflen - buffstart;
434 memcpy(buff, buffer + buffstart, tocopy);
435 cnt -= tocopy;
436 buffstart += tocopy;
437 buff += tocopy;
438 if (ret_part)
439 return tocopy;
440 if (cnt == 0)
441 return len;
442 }
443
444 while (cnt > 0) {
445 s_errno = 0;
446 errno = 0;
447 if((!isNoBlock) && doCheck) {
448 if(!checkForInput(timeout))
449 return -1;
450 }
451 rc = recv(sck, buff, cnt, flags);
452 if (rc < 0) {
453 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
454 if (checkForInput(timeout)) continue;// now got some input
455 else {
456 timedout = true;
457 return -1;// timed out
458 }
459 }
460 s_errno = errno;
461 return -1;
462 }
463 if (rc == 0) { // eof
464 ishup = true;
465 return len - cnt;
466 }
467 cnt -= rc;
468 if(ret_part) return len - cnt;
469 buff += rc;
470 }
471 return len;
472 }
473
474
475 short int BaseSocket::get_wait_flag(bool write_flag) {
476 if (timedout)
477 timedout = false;
478 if (write_flag) {
479 return POLLOUT;
480 }
481 return POLLIN;
482 }