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