Line data Source code
1 : // Copyright (c) 2012-2019 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // This program is free software; you can redistribute it and/or modify
4 : // it under the terms of the GNU General Public License as published by
5 : // the Free Software Foundation; either version 2 of the License, or
6 : // (at your option) any later version.
7 : //
8 : // This program is distributed in the hope that it will be useful,
9 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 : // GNU General Public License for more details.
12 : //
13 : // You should have received a copy of the GNU General Public License
14 : // along with this program; if not, write to the Free Software
15 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 :
17 : /** \file
18 : * \brief Implementation of the Snap Communicator class.
19 : *
20 : * This class wraps the C poll() interface in a C++ object with many types
21 : * of objects:
22 : *
23 : * \li Server Connections; for software that want to offer a port to
24 : * which clients can connect to; the server will call accept()
25 : * once a new client connection is ready; this results in a
26 : * Server/Client connection object
27 : * \li Client Connections; for software that want to connect to
28 : * a server; these expect the IP address and port to connect to
29 : * \li Server/Client Connections; for the server when it accepts a new
30 : * connection; in this case the server gets a socket from accept()
31 : * and creates one of these objects to handle the connection
32 : *
33 : * Using the poll() function is the easiest and allows us to listen
34 : * on pretty much any number of sockets (on my server it is limited
35 : * at 16,768 and frankly over 1,000 we probably will start to have
36 : * real slowness issues on small VPN servers.)
37 : */
38 :
39 :
40 : // self
41 : //
42 : #include "eventdispatcher/tcp_server_connection.h"
43 :
44 :
45 : // libaddr lib
46 : //
47 : #include "libaddr/addr_parser.h"
48 :
49 :
50 : // last include
51 : //
52 : #include <snapdev/poison.h>
53 :
54 :
55 :
56 : namespace ed
57 : {
58 :
59 :
60 :
61 : /** \brief Initialize a server connection.
62 : *
63 : * This function is used to initialize a server connection, a TCP/IP
64 : * listener which can accept() new connections.
65 : *
66 : * The connection uses a \p mode parameter which can be set to MODE_PLAIN,
67 : * in which case the \p certificate and \p private_key parameters are
68 : * ignored, or MODE_SECURE.
69 : *
70 : * This connection supports secure SSL communication using a certificate
71 : * and a private key. These have to be specified as filenames. The
72 : * `snapcommunicator` daemon makes use of files defined under
73 : * "/etc/snapwebsites/ssl/..." by default.
74 : *
75 : * These files are created using this command line:
76 : *
77 : * \code
78 : * openssl req \
79 : * -newkey rsa:2048 -nodes -keyout ssl-test.key \
80 : * -x509 -days 3650 -out ssl-test.crt
81 : * \endcode
82 : *
83 : * Then pass "ssl-test.crt" as the certificate and "ssl-test.key"
84 : * as the private key.
85 : *
86 : * \todo
87 : * Add support for DH connections. Since our snapcommunicator connections
88 : * are mostly private, it should not be a huge need at this point, though.
89 : *
90 : * \todo
91 : * Add support for verified certificates. Right now we do not create
92 : * signed certificates. This does not prevent fully secure transactions,
93 : * it just cannot verify that the computer on the other side is correct.
94 : *
95 : * \warning
96 : * The \p max_connections parameter is currently ignored because the
97 : * BIO implementation does not give you an API to change that parameter.
98 : * That being said, they default to the maximum number that the Linux
99 : * kernel will accept so it should be just fine.
100 : *
101 : * \param[in] addr The address to listen on. It may be set to "0.0.0.0".
102 : * \param[in] port The port to listen on.
103 : * \param[in] certificate The filename to a .pem file.
104 : * \param[in] private_key The filename to a .pem file.
105 : * \param[in] mode The mode to use to open the connection (PLAIN or SECURE.)
106 : * \param[in] max_connections The number of connections to keep in the listen queue.
107 : * \param[in] reuse_addr Whether to mark the socket with the SO_REUSEADDR flag.
108 : */
109 0 : tcp_server_connection::tcp_server_connection(
110 : std::string const & addr
111 : , int port
112 : , std::string const & certificate
113 : , std::string const & private_key
114 : , mode_t mode
115 : , int max_connections
116 : , bool reuse_addr)
117 : : tcp_bio_server(
118 0 : addr::string_to_addr(addr, "", port, "tcp")
119 : , max_connections
120 : , reuse_addr
121 : , certificate
122 : , private_key
123 0 : , mode)
124 : {
125 0 : }
126 :
127 :
128 : /** \brief Reimplement the is_listener() for the snap_tcp_server_connection.
129 : *
130 : * A server connection is a listener socket. The library makes
131 : * use of a completely different callback when a "read" event occurs
132 : * on these connections.
133 : *
134 : * The callback is expected to create the new connection and add
135 : * it the communicator.
136 : *
137 : * \return This version of the function always returns true.
138 : */
139 0 : bool tcp_server_connection::is_listener() const
140 : {
141 0 : return true;
142 : }
143 :
144 :
145 : /** \brief Retrieve the socket of this server connection.
146 : *
147 : * This function retrieves the socket this server connection. In this case
148 : * the socket is defined in the tcp_server class.
149 : *
150 : * \return The socket of this client connection.
151 : */
152 0 : int tcp_server_connection::get_socket() const
153 : {
154 0 : return tcp_bio_server::get_socket();
155 : }
156 :
157 :
158 :
159 6 : } // namespace ed
160 : // vim: ts=4 sw=4 et
|