Line data Source code
1 : // Copyright (c) 2012-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/eventdispatcher
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : /** \file
21 : * \brief Implementation of the Snap Communicator class.
22 : *
23 : * This class wraps the C poll() interface in a C++ object with many types
24 : * of objects:
25 : *
26 : * \li Server Connections; for software that want to offer a port to
27 : * which clients can connect to; the server will call accept()
28 : * once a new client connection is ready; this results in a
29 : * Server/Client connection object
30 : * \li Client Connections; for software that want to connect to
31 : * a server; these expect the IP address and port to connect to
32 : * \li Server/Client Connections; for the server when it accepts a new
33 : * connection; in this case the server gets a socket from accept()
34 : * and creates one of these objects to handle the connection
35 : *
36 : * Using the poll() function is the easiest and allows us to listen
37 : * on pretty much any number of sockets (on my server it is limited
38 : * at 16,768 and frankly over 1,000 we probably will start to have
39 : * real slowness issues on small VPN servers.)
40 : */
41 :
42 : // to get the POLLRDHUP definition
43 : #ifndef _GNU_SOURCE
44 : #define _GNU_SOURCE
45 : #endif
46 :
47 :
48 : // self
49 : //
50 : #include "eventdispatcher/tcp_blocking_client_message_connection.h"
51 :
52 : #include "eventdispatcher/exception.h"
53 :
54 :
55 : // snaplogger
56 : //
57 : #include <snaplogger/message.h>
58 :
59 :
60 : // snapdev
61 : //
62 : #include <snapdev/not_used.h>
63 :
64 :
65 : // C++
66 : //
67 : #include <cstring>
68 :
69 :
70 : // C
71 : //
72 : #include <poll.h>
73 : #include <sys/resource.h>
74 :
75 :
76 : // last include
77 : //
78 : #include <snapdev/poison.h>
79 :
80 :
81 :
82 : namespace ed
83 : {
84 :
85 :
86 :
87 : /** \brief Blocking client message connection.
88 : *
89 : * This object allows you to create a blocking, generally temporary
90 : * one message connection client. This is specifically used with
91 : * the snaplock daemon, but it can be used for other things too as
92 : * required.
93 : *
94 : * The connection is expected to be used as shown in the following
95 : * example which is how it is used to implement the LOCK through
96 : * our snaplock daemons.
97 : *
98 : * \code
99 : * class my_blocking_connection
100 : * : public ed::tcp_blocking_client_message_connection
101 : * {
102 : * public:
103 : * my_blocking_connection(addr::addr const & address, int port, mode_t mode)
104 : * : tcp_blocking_client_message_connection(address, port, mode)
105 : * {
106 : * // need to register with communicator
107 : * message register_message;
108 : * register_message.set_command("REGISTER");
109 : * ...
110 : * blocking_connection.send_message(register_message);
111 : *
112 : * run();
113 : * }
114 : *
115 : * ~my_blocking_connection()
116 : * {
117 : * // done, send UNLOCK and then make sure to unregister
118 : * message unlock_message;
119 : * unlock_message.set_command("UNLOCK");
120 : * ...
121 : * blocking_connection.send_message(unlock_message);
122 : *
123 : * message unregister_message;
124 : * unregister_message.set_command("UNREGISTER");
125 : * ...
126 : * blocking_connection.send_message(unregister_message);
127 : * }
128 : *
129 : * // now that we have a dispatcher, this would probably use
130 : * // that mechanism instead of a list of if()/else if()
131 : * //
132 : * // Please, consider using the dispatcher instead
133 : * //
134 : * virtual void process_message(message & message)
135 : * {
136 : * QString const command(message.get_command());
137 : * if(command == "LOCKED")
138 : * {
139 : * // the lock worked, release hand back to the user
140 : * done();
141 : * }
142 : * else if(command == "READY")
143 : * {
144 : * // the REGISTER worked
145 : * // send the LOCK now
146 : * message lock_message;
147 : * lock_message.set_command("LOCK");
148 : * ...
149 : * blocking_connection.send_message(lock_message);
150 : * }
151 : * else if(command == "HELP")
152 : * {
153 : * // snapcommunicator wants us to tell it what commands
154 : * // we accept
155 : * message commands_message;
156 : * commands_message.set_command("COMMANDS");
157 : * ...
158 : * blocking_connection.send_message(commands_message);
159 : * }
160 : * }
161 : * };
162 : * my_blocking_connection blocking_connection("127.0.0.1", 4040);
163 : *
164 : * // then we can send a message to the service we are interested in
165 : * my_blocking_connection.send_message(my_message);
166 : *
167 : * // now we call run() waiting for a reply
168 : * my_blocking_connection.run();
169 : * \endcode
170 : *
171 : * \param[in] address The address to connect to.
172 : * \param[in] mode The mode used to connect.
173 : */
174 0 : tcp_blocking_client_message_connection::tcp_blocking_client_message_connection(
175 : addr::addr const & address
176 0 : , mode_t const mode)
177 0 : : tcp_client_message_connection(address, mode, true)
178 : {
179 0 : }
180 :
181 :
182 : /** \brief Blocking run on the connection.
183 : *
184 : * This function reads the incoming messages and calls process_message()
185 : * on each one of them, in a blocking manner.
186 : *
187 : * If you called mark_done() before, the done flag is reset back to false.
188 : * You will have to call mark_done() again if you again receive a message
189 : * that is expected to end the loop.
190 : *
191 : * \note
192 : * Internally, the function actually calls process_line() which transforms
193 : * the line in a message and in turn calls process_message().
194 : */
195 0 : void tcp_blocking_client_message_connection::run()
196 : {
197 0 : mark_not_done();
198 :
199 0 : do
200 : {
201 : for(;;)
202 : {
203 : // TBD: can the socket become -1 within the read() loop?
204 : // (i.e. should not that be just outside of the for(;;)?)
205 : //
206 0 : struct pollfd fd;
207 0 : fd.events = POLLIN | POLLPRI | POLLRDHUP;
208 0 : fd.fd = get_socket();
209 0 : if(fd.fd < 0
210 0 : || !is_enabled())
211 : {
212 : // invalid socket
213 0 : process_error();
214 0 : return;
215 : }
216 :
217 : // at this time, this class is used with the lock and
218 : // the lock has a timeout so we need to block at most
219 : // for that amount of time and not forever (presumably
220 : // the snaplock would send us a LOCKFAILED marked with
221 : // a "timeout" parameter, but we cannot rely on the
222 : // snaplock being there and responding as expected.)
223 : //
224 : // calculate the number of microseconds and then convert
225 : // them to milliseconds for poll()
226 : //
227 0 : std::int64_t const next_timeout_timestamp(save_timeout_timestamp());
228 0 : std::int64_t const now(get_current_date());
229 0 : std::int64_t const timeout((next_timeout_timestamp - now) / 1000);
230 0 : if(timeout <= 0)
231 : {
232 : // timed out
233 : //
234 0 : process_timeout();
235 0 : if(is_done())
236 : {
237 0 : return;
238 : }
239 0 : SNAP_LOG_FATAL
240 : << "blocking connection timed out."
241 : << SNAP_LOG_SEND;
242 : throw runtime_error(
243 : "tcp_blocking_client_message_connection::run(): blocking"
244 0 : " connection timed out.");
245 : }
246 0 : errno = 0;
247 0 : fd.revents = 0; // probably useless... (kernel should clear those)
248 0 : int const r(::poll(&fd, 1, timeout));
249 0 : if(r < 0)
250 : {
251 : // r < 0 means an error occurred
252 : //
253 0 : if(errno == EINTR)
254 : {
255 : // Note: if the user wants to prevent this error, he should
256 : // use the signal with the Unix signals that may
257 : // happen while calling poll().
258 : //
259 : throw runtime_error(
260 : "tcp_blocking_client_message_connection::run():"
261 : " EINTR occurred while in poll() -- interrupts"
262 0 : " are not supported yet though.");
263 : }
264 0 : if(errno == EFAULT)
265 : {
266 : throw runtime_error(
267 : "tcp_blocking_client_message_connection::run():"
268 0 : " buffer was moved out of our address space?");
269 : }
270 0 : if(errno == EINVAL)
271 : {
272 : // if this is really because nfds is too large then it may be
273 : // a "soft" error that can be fixed; that being said, my
274 : // current version is 16K files which frankly when we reach
275 : // that level we have a problem...
276 : //
277 0 : rlimit rl;
278 0 : getrlimit(RLIMIT_NOFILE, &rl);
279 : throw invalid_parameter(
280 : "tcp_blocking_client_message_connection::run():"
281 : " too many file fds for poll, limit is"
282 : " currently "
283 0 : + std::to_string(rl.rlim_cur)
284 0 : + ", your kernel top limit is "
285 0 : + std::to_string(rl.rlim_max)
286 0 : + ".");
287 : }
288 0 : if(errno == ENOMEM)
289 : {
290 : throw runtime_error(
291 : "tcp_blocking_client_message_connection::run():"
292 0 : " poll() failed because of memory.");
293 : }
294 0 : int const e(errno);
295 : throw invalid_parameter(
296 : "tcp_blocking_client_message_connection::run():"
297 : " poll() failed with error "
298 0 : + std::to_string(e)
299 0 : + " -- "
300 0 : + strerror(e));
301 : }
302 :
303 0 : if((fd.revents & (POLLIN | POLLPRI)) != 0)
304 : {
305 : // read one character at a time otherwise we would be
306 : // blocked forever
307 : //
308 0 : char buf[2];
309 0 : int const size(::read(fd.fd, buf, 1));
310 0 : if(size != 1)
311 : {
312 : // invalid read
313 : //
314 0 : process_error();
315 : throw invalid_parameter(
316 : "tcp_blocking_client_message_connection::run():"
317 : " read() failed reading data from socket"
318 : " (return value = "
319 0 : + std::to_string(size)
320 0 : + ").");
321 : }
322 0 : if(buf[0] == '\n')
323 : {
324 : // end of a line, we got a whole message in our buffer
325 : // notice that we do not add the '\n' to line
326 : //
327 0 : break;
328 : }
329 0 : buf[1] = '\0';
330 0 : f_line += buf;
331 : }
332 0 : if((fd.revents & POLLERR) != 0)
333 : {
334 0 : process_error();
335 0 : return;
336 : }
337 0 : if((fd.revents & (POLLHUP | POLLRDHUP)) != 0)
338 : {
339 0 : process_hup();
340 0 : return;
341 : }
342 0 : if((fd.revents & POLLNVAL) != 0)
343 : {
344 0 : process_invalid();
345 0 : return;
346 : }
347 0 : }
348 0 : process_line(f_line);
349 0 : f_line.clear();
350 : }
351 0 : while(!is_done());
352 : }
353 :
354 :
355 : /** \brief Quick peek on the connection.
356 : *
357 : * This function checks for incoming messages and calls process_message()
358 : * on each one of them. If no messages are found on the pipe, then the
359 : * function returns immediately.
360 : *
361 : * \note
362 : * Internally, the function actually calls process_line() which transforms
363 : * the line in a message and in turn calls process_message().
364 : */
365 0 : void tcp_blocking_client_message_connection::peek()
366 : {
367 0 : do
368 : {
369 : for(;;)
370 : {
371 0 : pollfd fd;
372 0 : fd.events = POLLIN | POLLPRI | POLLRDHUP;
373 0 : fd.fd = get_socket();
374 0 : if(fd.fd < 0
375 0 : || !is_enabled())
376 : {
377 : // invalid socket
378 0 : process_error();
379 0 : return;
380 : }
381 :
382 0 : errno = 0;
383 0 : fd.revents = 0; // probably useless... (kernel should clear those)
384 0 : int const r(::poll(&fd, 1, 0));
385 0 : if(r < 0)
386 : {
387 : // r < 0 means an error occurred
388 : //
389 0 : if(errno == EINTR)
390 : {
391 : // Note: if the user wants to prevent this error, he should
392 : // use the signal with the Unix signals that may
393 : // happen while calling poll().
394 : //
395 : throw runtime_error(
396 : "tcp_blocking_client_message_connection::run():"
397 : " EINTR occurred while in poll() -- interrupts"
398 0 : " are not supported yet though");
399 : }
400 0 : if(errno == EFAULT)
401 : {
402 : throw invalid_parameter(
403 : "tcp_blocking_client_message_connection::run():"
404 0 : " buffer was moved out of our address space?");
405 : }
406 0 : if(errno == EINVAL)
407 : {
408 : // if this is really because nfds is too large then it may be
409 : // a "soft" error that can be fixed; that being said, my
410 : // current version is 16K files which frankly when we reach
411 : // that level we have a problem...
412 : //
413 0 : struct rlimit rl;
414 0 : getrlimit(RLIMIT_NOFILE, &rl);
415 : throw invalid_parameter(
416 : "tcp_blocking_client_message_connection::run():"
417 : " too many file fds for poll, limit is currently "
418 0 : + std::to_string(rl.rlim_cur)
419 0 : + ", your kernel top limit is "
420 0 : + std::to_string(rl.rlim_max));
421 : }
422 0 : if(errno == ENOMEM)
423 : {
424 : throw runtime_error(
425 : "tcp_blocking_client_message_connection::run():"
426 0 : " poll() failed because of memory");
427 : }
428 0 : int const e(errno);
429 : throw runtime_error(
430 : "tcp_blocking_client_message_connection::run():"
431 : " poll() failed with error "
432 0 : + std::to_string(e)
433 0 : + " -- "
434 0 : + strerror(e));
435 : }
436 :
437 0 : if(r == 0)
438 : {
439 0 : return;
440 : }
441 :
442 0 : if((fd.revents & (POLLIN | POLLPRI)) != 0)
443 : {
444 : // read one character at a time otherwise we would be
445 : // blocked forever
446 : //
447 0 : char buf[2];
448 0 : int const size(::read(fd.fd, buf, 1));
449 0 : if(size != 1)
450 : {
451 : // invalid read
452 0 : process_error();
453 : throw runtime_error(
454 : "tcp_blocking_client_message_connection::run():"
455 : " read() failed reading data from socket (return"
456 : " value = "
457 0 : + std::to_string(size)
458 0 : + ")");
459 : }
460 0 : if(buf[0] == '\n')
461 : {
462 : // end of a line, we got a whole message in our buffer
463 : // notice that we do not add the '\n' to line
464 0 : break;
465 : }
466 0 : buf[1] = '\0';
467 0 : f_line += buf;
468 : }
469 0 : if((fd.revents & POLLERR) != 0)
470 : {
471 0 : process_error();
472 0 : return;
473 : }
474 0 : if((fd.revents & (POLLHUP | POLLRDHUP)) != 0)
475 : {
476 0 : process_hup();
477 0 : return;
478 : }
479 0 : if((fd.revents & POLLNVAL) != 0)
480 : {
481 0 : process_invalid();
482 0 : return;
483 : }
484 0 : }
485 0 : process_line(f_line);
486 0 : f_line.clear();
487 : }
488 0 : while(!is_done());
489 : }
490 :
491 :
492 : /** \brief Send the specified message to the connection on the other end.
493 : *
494 : * This function sends the specified message to the other side of the
495 : * socket connection. If the write somehow fails, then the function
496 : * returns false.
497 : *
498 : * The function blocks until the entire message was written to the
499 : * socket.
500 : *
501 : * \param[in] msg The message to send to the connection.
502 : * \param[in] cache Whether to cache the message if it cannot be sent
503 : * immediately (ignored at the moment.)
504 : *
505 : * \return true if the message was sent successfully, false otherwise.
506 : */
507 0 : bool tcp_blocking_client_message_connection::send_message(message & msg, bool cache)
508 : {
509 0 : snapdev::NOT_USED(cache);
510 :
511 0 : int const s(get_socket());
512 0 : if(s >= 0)
513 : {
514 : // transform the message to a string and write to the socket
515 : // the writing is blocking and thus fully synchronous so the
516 : // function blocks until the message gets fully sent
517 : //
518 : // WARNING: we cannot use f_connection.write() because that one
519 : // is asynchronous (at least, it writes to a buffer
520 : // and not directly to the socket!)
521 : //
522 0 : std::string buf(msg.to_message());
523 0 : buf += '\n';
524 0 : return ::write(s, buf.c_str(), buf.length()) == static_cast<ssize_t>(buf.length());
525 : }
526 :
527 0 : return false;
528 : }
529 :
530 :
531 : /** \brief Overridden callback.
532 : *
533 : * This function is overriding the lower level process_error() to make
534 : * (mostly) sure that the remove_from_communicator() function does not
535 : * get called because that would generate the creation of a
536 : * communicator object which we do not want with blocking
537 : * clients.
538 : */
539 0 : void tcp_blocking_client_message_connection::process_error()
540 : {
541 0 : }
542 :
543 :
544 :
545 6 : } // namespace ed
546 : // vim: ts=4 sw=4 et
|