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
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 :
43 : // self
44 : //
45 : #include "eventdispatcher/tcp_server_connection.h"
46 :
47 :
48 : // last include
49 : //
50 : #include <snapdev/poison.h>
51 :
52 :
53 :
54 : namespace ed
55 : {
56 :
57 :
58 :
59 : /** \brief Initialize a server connection.
60 : *
61 : * This function is used to initialize a server connection, a TCP/IP
62 : * listener which can accept() new connections.
63 : *
64 : * The connection uses a \p mode parameter which can be set to MODE_PLAIN,
65 : * in which case the \p certificate and \p private_key parameters are
66 : * ignored, or MODE_SECURE.
67 : *
68 : * This connection supports secure SSL communication using a certificate
69 : * and a private key. These have to be specified as filenames. The
70 : * `snapcommunicator` daemon makes use of files defined under
71 : * "/etc/snapwebsites/ssl/..." by default.
72 : *
73 : * These files are created using this command line:
74 : *
75 : * \code
76 : * openssl req \
77 : * -newkey rsa:2048 -nodes -keyout ssl-test.key \
78 : * -x509 -days 3650 -out ssl-test.crt
79 : * \endcode
80 : *
81 : * Then pass "ssl-test.crt" as the certificate and "ssl-test.key"
82 : * as the private key.
83 : *
84 : * \todo
85 : * Add support for DH connections. Since our snapcommunicator connections
86 : * are mostly private, it should not be a huge need at this point, though.
87 : *
88 : * \todo
89 : * Add support for verified certificates. Right now we do not create
90 : * signed certificates. This does not prevent fully secure transactions,
91 : * it just cannot verify that the computer on the other side is correct.
92 : *
93 : * \warning
94 : * The \p max_connections parameter is currently ignored because the
95 : * BIO implementation does not give you an API to change that parameter.
96 : * That being said, they default to the maximum number that the Linux
97 : * kernel will accept so it should be just fine.
98 : *
99 : * \param[in] addr The address to listen on. It may be set to "0.0.0.0".
100 : * \param[in] port The port to listen on.
101 : * \param[in] certificate The filename to a .pem file.
102 : * \param[in] private_key The filename to a .pem file.
103 : * \param[in] mode The mode to use to open the connection (PLAIN or SECURE.)
104 : * \param[in] max_connections The number of connections to keep in the listen queue.
105 : * \param[in] reuse_addr Whether to mark the socket with the SO_REUSEADDR flag.
106 : */
107 0 : tcp_server_connection::tcp_server_connection(
108 : addr::addr const & address
109 : , std::string const & certificate
110 : , std::string const & private_key
111 : , mode_t mode
112 : , int max_connections
113 0 : , bool reuse_addr)
114 : : tcp_bio_server(
115 : address
116 : , max_connections
117 : , reuse_addr
118 : , certificate
119 : , private_key
120 0 : , mode)
121 : {
122 0 : }
123 :
124 :
125 : /** \brief Reimplement the is_listener() for the tcp_server_connection.
126 : *
127 : * A server connection is a listener socket. The library makes
128 : * use of a completely different callback when a "read" event occurs
129 : * on these connections.
130 : *
131 : * The callback is expected to create the new connection and add
132 : * it the communicator.
133 : *
134 : * \return This version of the function always returns true.
135 : */
136 0 : bool tcp_server_connection::is_listener() const
137 : {
138 0 : return true;
139 : }
140 :
141 :
142 : /** \brief Retrieve the socket of this server connection.
143 : *
144 : * This function retrieves the socket this server connection. In this case
145 : * the socket is defined in the tcp_server class.
146 : *
147 : * \return The socket of this client connection.
148 : */
149 0 : int tcp_server_connection::get_socket() const
150 : {
151 0 : return tcp_bio_server::get_socket();
152 : }
153 :
154 :
155 :
156 6 : } // namespace ed
157 : // vim: ts=4 sw=4 et
|