Line data Source code
1 : // Copyright (c) 2012-2021 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
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 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 : // self
43 : //
44 : #include "eventdispatcher/tcp_server_client_connection.h"
45 :
46 : #include "eventdispatcher/exception.h"
47 :
48 :
49 : // snaplogger lib
50 : //
51 : #include <snaplogger/message.h>
52 :
53 :
54 : // C++ lib
55 : //
56 : #include <cstring>
57 :
58 :
59 : // C lib
60 : //
61 : #include <arpa/inet.h>
62 : #include <netdb.h>
63 :
64 :
65 : // last include
66 : //
67 : #include <snapdev/poison.h>
68 :
69 :
70 :
71 : namespace ed
72 : {
73 :
74 :
75 :
76 : /** \brief Create a client connection created from an accept().
77 : *
78 : * This constructor initializes a client connection from a socket
79 : * that we received from an accept() call.
80 : *
81 : * The destructor will automatically close that socket on destruction.
82 : *
83 : * \param[in] client The client that accept() returned.
84 : */
85 0 : tcp_server_client_connection::tcp_server_client_connection(tcp_bio_client::pointer_t client)
86 0 : : f_client(client)
87 : {
88 0 : }
89 :
90 :
91 : /** \brief Make sure the socket gets released.
92 : *
93 : * This destructor makes sure that the socket gets closed.
94 : */
95 0 : tcp_server_client_connection::~tcp_server_client_connection()
96 : {
97 0 : close();
98 0 : }
99 :
100 :
101 : /** \brief Read data from the TCP server client socket.
102 : *
103 : * This function reads as much data up to the specified amount
104 : * in \p count. The read data is saved in \p buf.
105 : *
106 : * \param[in,out] buf The buffer where the data gets read.
107 : * \param[in] count The maximum number of bytes to read in buf.
108 : *
109 : * \return The number of bytes read or -1 if an error occurred.
110 : */
111 0 : ssize_t tcp_server_client_connection::read(void * buf, size_t count)
112 : {
113 0 : if(!f_client)
114 : {
115 0 : errno = EBADF;
116 0 : return -1;
117 : }
118 0 : return f_client->read(reinterpret_cast<char *>(buf), count);
119 : }
120 :
121 :
122 : /** \brief Write data to this connection's socket.
123 : *
124 : * This function writes up to \p count bytes of data from \p buf
125 : * to this connection's socket.
126 : *
127 : * \warning
128 : * This write function may not always write all the data you are
129 : * trying to send to the remote connection. If you want to make
130 : * sure that all your data is written to the other connection,
131 : * you want to instead use the tcp_server_client_buffer_connection
132 : * which overloads the write() function and saves the data to be
133 : * written to the socket in a buffer. The communicator run
134 : * loop is then responsible for sending all the data.
135 : *
136 : * \param[in] buf The buffer of data to be written to the socket.
137 : * \param[in] count The number of bytes the caller wants to write to the
138 : * connection.
139 : *
140 : * \return The number of bytes written to the socket or -1 if an error occurred.
141 : */
142 0 : ssize_t tcp_server_client_connection::write(void const * buf, size_t count)
143 : {
144 0 : if(!f_client)
145 : {
146 0 : errno = EBADF;
147 0 : return -1;
148 : }
149 0 : return f_client->write(reinterpret_cast<char const *>(buf), count);
150 : }
151 :
152 :
153 : /** \brief Close the socket of this connection.
154 : *
155 : * This function is automatically called whenever the object gets
156 : * destroyed (see destructor) or detects that the client closed
157 : * the network connection.
158 : *
159 : * Connections cannot be reopened.
160 : */
161 0 : void tcp_server_client_connection::close()
162 : {
163 0 : f_client.reset();
164 0 : }
165 :
166 :
167 : /** \brief Retrieve the socket of this connection.
168 : *
169 : * This function returns the socket defined in this connection.
170 : */
171 0 : int tcp_server_client_connection::get_socket() const
172 : {
173 0 : if(f_client == nullptr)
174 : {
175 : // client connection was closed
176 : //
177 0 : return -1;
178 : }
179 0 : return f_client->get_socket();
180 : }
181 :
182 :
183 : /** \brief Tell that we are always a reader.
184 : *
185 : * This function always returns true meaning that the connection is
186 : * always of a reader. In most cases this is safe because if nothing
187 : * is being written to you then poll() never returns so you do not
188 : * waste much time in have a TCP connection always marked as a
189 : * reader.
190 : *
191 : * \return The events to listen to for this connection.
192 : */
193 0 : bool tcp_server_client_connection::is_reader() const
194 : {
195 0 : return true;
196 : }
197 :
198 :
199 : /** \brief Retrieve a copy of the client's address.
200 : *
201 : * This function makes a copy of the address of this client connection
202 : * to the \p address parameter and returns the length.
203 : *
204 : * If the function returns zero, then the \p address buffer is not
205 : * modified and no address is defined in this connection.
206 : *
207 : * \param[out] address The reference to an address variable where the
208 : * client's address gets copied.
209 : *
210 : * \return Return the length of the address which may be smaller than
211 : * sizeof(address). If zero, then no address is defined.
212 : *
213 : * \sa get_addr()
214 : */
215 0 : size_t tcp_server_client_connection::get_client_address(sockaddr_storage & address) const
216 : {
217 : // make sure the address is defined and the socket open
218 : //
219 0 : if(const_cast<tcp_server_client_connection *>(this)->define_address() != 0)
220 : {
221 0 : return 0;
222 : }
223 :
224 0 : address = f_address;
225 0 : return f_length;
226 : }
227 :
228 :
229 : /** \brief Retrieve the address in the form of a string.
230 : *
231 : * Like the get_addr() of the tcp client and server classes, this
232 : * function returns the address in the form of a string which can
233 : * easily be used to log information and other similar tasks.
234 : *
235 : * \todo
236 : * Look at using libaddr for the conversion.
237 : *
238 : * \return The client's address in the form of a string.
239 : */
240 0 : std::string tcp_server_client_connection::get_client_addr() const
241 : {
242 : // make sure the address is defined and the socket open
243 : //
244 0 : if(!const_cast<tcp_server_client_connection *>(this)->define_address())
245 : {
246 0 : return std::string();
247 : }
248 :
249 0 : size_t const max_length(std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1);
250 :
251 : // in release mode this should not be dynamic (although the syntax is so
252 : // the warning would happen), but in debug it is likely an alloca()
253 : #pragma GCC diagnostic push
254 : #pragma GCC diagnostic ignored "-Wvla"
255 0 : char buf[max_length];
256 : #pragma GCC diagnostic pop
257 :
258 0 : char const * r(nullptr);
259 :
260 0 : if(f_address.ss_family == AF_INET)
261 : {
262 0 : r = inet_ntop(AF_INET, &reinterpret_cast<sockaddr_in const &>(f_address).sin_addr, buf, max_length);
263 : }
264 : else
265 : {
266 0 : r = inet_ntop(AF_INET6, &reinterpret_cast<sockaddr_in6 const &>(f_address).sin6_addr, buf, max_length);
267 : }
268 :
269 0 : if(r == nullptr)
270 : {
271 0 : int const e(errno);
272 0 : std::string err("inet_ntop() could not convert IP address (errno: ");
273 0 : err += std::to_string(e);
274 0 : err += " -- ";
275 0 : err += strerror(e);
276 0 : err += ").";
277 0 : SNAP_LOG_FATAL << err << SNAP_LOG_SEND;
278 0 : throw event_dispatcher_runtime_error(err);
279 : }
280 :
281 0 : return buf;
282 : }
283 :
284 :
285 : /** \brief Retrieve the port.
286 : *
287 : * This function returns the port of the socket on our side.
288 : *
289 : * If the port is not available (not connected?), then -1 is returned.
290 : *
291 : * \return The client's port in host order.
292 : */
293 0 : int tcp_server_client_connection::get_client_port() const
294 : {
295 : // make sure the address is defined and the socket open
296 : //
297 0 : if(!const_cast<tcp_server_client_connection *>(this)->define_address())
298 : {
299 0 : return -1;
300 : }
301 :
302 0 : if(f_address.ss_family == AF_INET)
303 : {
304 0 : return ntohs(reinterpret_cast<sockaddr_in const &>(f_address).sin_port);
305 : }
306 : else
307 : {
308 0 : return ntohs(reinterpret_cast<sockaddr_in6 const &>(f_address).sin6_port);
309 : }
310 : }
311 :
312 :
313 : /** \brief Retrieve the address in the form of a string.
314 : *
315 : * Like the get_addr() of the tcp client and server classes, this
316 : * function returns the address in the form of a string which can
317 : * easily be used to log information and other similar tasks.
318 : *
319 : * \todo
320 : * Look at using libaddr for the conversion.
321 : *
322 : * \return The client's address in the form of a string.
323 : */
324 0 : std::string tcp_server_client_connection::get_client_addr_port() const
325 : {
326 : // get the current address and port
327 0 : std::string const addr(get_client_addr());
328 0 : int const port(get_client_port());
329 :
330 : // make sure they are defined
331 0 : if(addr.empty()
332 0 : || port < 0)
333 : {
334 0 : return std::string();
335 : }
336 :
337 : // calculate the result
338 0 : std::string buf;
339 0 : buf.reserve(addr.length() + (3 + 5));
340 0 : if(f_address.ss_family == AF_INET)
341 : {
342 0 : buf += addr;
343 0 : buf += ':';
344 : }
345 : else
346 : {
347 0 : buf += '[';
348 0 : buf += addr;
349 0 : buf += "]:";
350 : }
351 0 : buf += std::to_string(port);
352 :
353 0 : return buf;
354 : }
355 :
356 :
357 : /** \brief Retrieve the socket address if we have not done so yet.
358 : *
359 : * This function make sure that the f_address and f_length parameters are
360 : * defined. This is done by calling the getsockname() function.
361 : *
362 : * If f_length is still zero, then it is expected that address was not
363 : * yet read.
364 : *
365 : * Note that the function returns -1 if the socket is now -1 (i.e. the
366 : * connection is closed) whether or not the function worked before.
367 : *
368 : * \return false if the address cannot be defined, true otherwise
369 : */
370 0 : bool tcp_server_client_connection::define_address()
371 : {
372 0 : int const s(get_socket());
373 0 : if(s == -1)
374 : {
375 0 : return false;
376 : }
377 :
378 0 : if(f_length == 0)
379 : {
380 : // address not defined yet, retrieve with with getsockname()
381 : //
382 0 : f_length = sizeof(f_address);
383 0 : if(getsockname(s, reinterpret_cast<struct sockaddr *>(&f_address), &f_length) != 0)
384 : {
385 0 : int const e(errno);
386 0 : SNAP_LOG_ERROR
387 0 : << "getsockname() failed retrieving IP address (errno: "
388 : << e
389 : << " -- "
390 0 : << strerror(e)
391 : << ")."
392 : << SNAP_LOG_SEND;
393 0 : f_length = 0;
394 0 : return false;
395 : }
396 0 : if(f_address.ss_family != AF_INET
397 0 : && f_address.ss_family != AF_INET6)
398 : {
399 0 : SNAP_LOG_ERROR
400 0 : << "address family ("
401 0 : << f_address.ss_family
402 : << ") returned by getsockname() is not understood, it is neither an IPv4 nor IPv6."
403 : << SNAP_LOG_SEND;
404 0 : f_length = 0;
405 0 : return false;
406 : }
407 0 : if(f_length < sizeof(f_address))
408 : {
409 : // reset the rest of the structure, just in case
410 : //
411 0 : memset(reinterpret_cast<char *>(&f_address) + f_length, 0, sizeof(f_address) - f_length);
412 : }
413 : }
414 :
415 0 : return true;
416 : }
417 :
418 :
419 :
420 : } // namespace ed
421 : // vim: ts=4 sw=4 et
|