Line data Source code
1 : // Copyright (c) 2012-2019 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_message_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 <arpa/inet.h>
57 : #include <sys/socket.h>
58 :
59 :
60 : // last include
61 : //
62 : #include <snapdev/poison.h>
63 :
64 :
65 :
66 : namespace ed
67 : {
68 :
69 :
70 :
71 : /** \brief Initializes a client to read messages from a socket.
72 : *
73 : * This implementation creates a message in/out client.
74 : * This is the most useful client in our Snap! Communicator
75 : * as it directly sends and receives messages.
76 : *
77 : * \todo
78 : * Convert the socket address to string using libaddr.
79 : *
80 : * \param[in] client The client representing the in/out socket.
81 : */
82 0 : tcp_server_client_message_connection::tcp_server_client_message_connection(tcp_bio_client::pointer_t client)
83 0 : : tcp_server_client_buffer_connection(client)
84 : {
85 : // TODO: somehow the port seems wrong (i.e. all connections return the same port)
86 :
87 : // make sure the socket is defined and well
88 : //
89 0 : int const socket(client->get_socket());
90 0 : if(socket < 0)
91 : {
92 : SNAP_LOG_ERROR
93 0 : << "called with a closed client connection.";
94 0 : throw std::runtime_error("tcp_server_client_message_connection() called with a closed client connection.");
95 : }
96 :
97 0 : struct sockaddr_storage address = sockaddr_storage();
98 0 : socklen_t length(sizeof(address));
99 0 : if(getpeername(socket, reinterpret_cast<struct sockaddr *>(&address), &length) != 0)
100 : {
101 0 : int const e(errno);
102 : SNAP_LOG_ERROR
103 0 : << "getpeername() failed retrieving IP address (errno: "
104 0 : << e
105 0 : << " -- "
106 0 : << strerror(e)
107 0 : << ").";
108 0 : throw std::runtime_error("getpeername() failed to retrieve IP address in tcp_server_client_message_connection()");
109 : }
110 0 : if(address.ss_family != AF_INET
111 0 : && address.ss_family != AF_INET6)
112 : {
113 : SNAP_LOG_ERROR
114 0 : << "address family ("
115 0 : << address.ss_family
116 0 : << ") returned by getpeername() is not understood, it is neither an IPv4 nor IPv6.";
117 0 : throw std::runtime_error("getpeername() returned an address which is not understood in tcp_server_client_message_connection()");
118 : }
119 0 : if(length < sizeof(address))
120 : {
121 : // reset the rest of the structure, just in case
122 0 : memset(reinterpret_cast<char *>(&address) + length, 0, sizeof(address) - length);
123 : }
124 :
125 0 : constexpr size_t max_length(std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1);
126 :
127 : // in release mode this should not be dynamic (although the syntax is so
128 : // the warning would happen), but in debug it is likely an alloca()
129 : //
130 : #pragma GCC diagnostic push
131 : #pragma GCC diagnostic ignored "-Wvla"
132 : char buf[max_length];
133 : #pragma GCC diagnostic pop
134 :
135 0 : char const * r(nullptr);
136 :
137 0 : if(address.ss_family == AF_INET)
138 : {
139 0 : r = inet_ntop(AF_INET, &reinterpret_cast<struct sockaddr_in const &>(address).sin_addr, buf, max_length);
140 : }
141 : else
142 : {
143 0 : r = inet_ntop(AF_INET6, &reinterpret_cast<struct sockaddr_in6 const &>(address).sin6_addr, buf, max_length);
144 : }
145 :
146 0 : if(r == nullptr)
147 : {
148 0 : int const e(errno);
149 0 : std::string err("inet_ntop() could not convert IP address (errno: ");
150 0 : err += std::to_string(e);
151 0 : err += " -- ";
152 0 : err += strerror(e);
153 0 : err += ").";
154 0 : SNAP_LOG_FATAL << err;
155 0 : throw event_dispatcher_runtime_error(err);
156 : }
157 :
158 0 : if(address.ss_family == AF_INET)
159 : {
160 0 : f_remote_address = buf;
161 0 : f_remote_address += ':';
162 0 : f_remote_address += std::to_string(static_cast<int>(ntohs(reinterpret_cast<sockaddr_in const &>(address).sin_port)));
163 : }
164 : else
165 : {
166 0 : f_remote_address = "[";
167 0 : f_remote_address += buf;
168 0 : f_remote_address += "]:";
169 0 : f_remote_address += std::to_string(static_cast<int>(ntohs(reinterpret_cast<sockaddr_in6 const &>(address).sin6_port)));
170 : }
171 0 : }
172 :
173 :
174 : /** \brief Process a line (string) just received.
175 : *
176 : * The function parses the line as a message (snap_communicator_message)
177 : * and then calls the process_message() function if the line was valid.
178 : *
179 : * \param[in] line The line of text that was just read.
180 : */
181 0 : void tcp_server_client_message_connection::process_line(std::string const & line)
182 : {
183 : // empty lines should not occur, but just in case, just ignore
184 0 : if(line.empty())
185 : {
186 0 : return;
187 : }
188 :
189 0 : message msg;
190 0 : if(msg.from_message(line))
191 : {
192 0 : dispatch_message(msg);
193 : }
194 : else
195 : {
196 : // TODO: what to do here? This could because the version changed
197 : // and the messages are not compatible anymore.
198 : //
199 : SNAP_LOG_ERROR
200 0 : << "process_line() was asked to process an invalid message ("
201 0 : << line
202 0 : << ")";
203 : }
204 : }
205 :
206 :
207 : /** \brief Send a message.
208 : *
209 : * This function sends a message to the client on the other side
210 : * of this connection.
211 : *
212 : * \exception event_dispatcher_runtime_error
213 : * This function throws this exception if the write() to the pipe
214 : * fails to write the entire message. This should only happen if
215 : * the pipe gets severed.
216 : *
217 : * \param[in] message The message to be processed.
218 : * \param[in] cache Whether to cache the message if there is no connection.
219 : * (Ignore because a client socket has to be there until
220 : * closed and then it can't be reopened by the server.)
221 : *
222 : * \return Always true, although if an error occurs the function throws.
223 : */
224 0 : bool tcp_server_client_message_connection::send_message(message const & msg, bool cache)
225 : {
226 0 : snap::NOTUSED(cache);
227 :
228 : // transform the message to a string and write to the socket
229 : // the writing is asynchronous so the message is saved in a cache
230 : // and transferred only later when the run() loop is hit again
231 : //
232 0 : std::string buf(msg.to_message());
233 0 : buf += '\n';
234 0 : return write(buf.c_str(), buf.length()) == static_cast<ssize_t>(buf.length());
235 : }
236 :
237 :
238 : /** \brief Retrieve the remote address information.
239 : *
240 : * This function can be used to retrieve the remove address and port
241 : * information as was specified on the constructor. These can be used
242 : * to find this specific connection at a later time or create another
243 : * connection.
244 : *
245 : * For example, you may get 192.168.2.17:4040.
246 : *
247 : * The function works even after the socket gets closed as we save
248 : * the remote address and port in a string just after the connection
249 : * was established.
250 : *
251 : * \warning
252 : * This function returns BOTH: the address and the port.
253 : *
254 : * \note
255 : * These parameters are the same as what was passed to the constructor,
256 : * only both will have been converted to numbers. So for example when
257 : * you used "localhost", here you get "::1" or "127.0.0.1" for the
258 : * address.
259 : *
260 : * \return The remote host address and connection port.
261 : */
262 0 : std::string const & tcp_server_client_message_connection::get_remote_address() const
263 : {
264 0 : return f_remote_address;
265 : }
266 :
267 :
268 :
269 6 : } // namespace ed
270 : // vim: ts=4 sw=4 et
|