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 : // to get the POLLRDHUP definition
40 : #ifndef _GNU_SOURCE
41 : #define _GNU_SOURCE
42 : #endif
43 :
44 :
45 : // self
46 : //
47 : #include "eventdispatcher/udp_server_connection.h"
48 :
49 : //#include "eventdispatcher/snap_communicator_dispatcher.h"
50 : //
51 : //
52 : //// snaplogger lib
53 : ////
54 : //#include "snaplogger/message.h"
55 : //
56 : //
57 : //// snapdev lib
58 : ////
59 : //#include "snapdev/not_reached.h"
60 : //#include "snapdev/not_used.h"
61 : //#include "snapdev/string_replace_many.h"
62 : //
63 : //
64 : //// libaddr lib
65 : ////
66 : //#include "libaddr/addr_parser.h"
67 : //
68 : //
69 : //// C++ lib
70 : ////
71 : //#include <sstream>
72 : //#include <limits>
73 : //#include <atomic>
74 : //
75 : //
76 : //// C lib
77 : ////
78 : //#include <fcntl.h>
79 : //#include <poll.h>
80 : //#include <unistd.h>
81 : //#include <sys/eventfd.h>
82 : //#include <sys/inotify.h>
83 : //#include <sys/ioctl.h>
84 : //#include <sys/resource.h>
85 : //#include <sys/syscall.h>
86 : //#include <sys/time.h>
87 :
88 :
89 : // last include
90 : //
91 : #include <snapdev/poison.h>
92 :
93 :
94 :
95 : namespace ed
96 : {
97 :
98 :
99 :
100 :
101 : /** \brief Initialize a UDP listener.
102 : *
103 : * This function is used to initialize a server connection, a UDP/IP
104 : * listener which wakes up whenever a send() is sent to this listener
105 : * address and port.
106 : *
107 : * \param[in] communicator The snap communicator controlling this connection.
108 : * \param[in] addr The address to listen on. It may be set to "0.0.0.0".
109 : * \param[in] port The port to listen on.
110 : */
111 0 : udp_server_connection::udp_server_connection(std::string const & addr, int port)
112 0 : : udp_server(addr, port)
113 : {
114 0 : }
115 :
116 :
117 : /** \brief Check to know whether this UDP connection is a reader.
118 : *
119 : * This function returns true to say that this UDP connection is
120 : * indeed a reader.
121 : *
122 : * \return This function already returns true as we are likely to
123 : * always want a UDP socket to be listening for incoming
124 : * packets.
125 : */
126 0 : bool udp_server_connection::is_reader() const
127 : {
128 0 : return true;
129 : }
130 :
131 :
132 : /** \brief Retrieve the socket of this server connection.
133 : *
134 : * This function retrieves the socket this server connection. In this case
135 : * the socket is defined in the udp_server class.
136 : *
137 : * \return The socket of this client connection.
138 : */
139 0 : int udp_server_connection::get_socket() const
140 : {
141 0 : return udp_server::get_socket();
142 : }
143 :
144 :
145 : /** \brief Define a secret code.
146 : *
147 : * When receiving a message through this UDP socket, this secret code must
148 : * be included in the message. If not present, then the message gets
149 : * discarded.
150 : *
151 : * By default this parameter is an empty string. This means no secret
152 : * code is required and UDP communication can be done without it.
153 : *
154 : * \note
155 : * Secret codes are expected to be used only on connections between
156 : * computers. If the IP address is 127.0.0.1, you probably don't need
157 : * to have a secret code.
158 : *
159 : * \warning
160 : * Remember that UDP messages are limited in size. If too long, the
161 : * send_message() function throws an error. So your secret code should
162 : * remain relatively small.
163 : *
164 : * \todo
165 : * The secret_code string must be a valid UTF-8 string. At this point
166 : * this is not enforced.
167 : *
168 : * \param[in] secret_code The secret code that has to be included in the
169 : * incoming messages for those to be accepted.
170 : */
171 0 : void udp_server_connection::set_secret_code(std::string const & secret_code)
172 : {
173 0 : f_secret_code = secret_code;
174 0 : }
175 :
176 :
177 : /** \brief Retrieve the server secret code.
178 : *
179 : * This function returns the server secret code as defined with the
180 : * set_secret_code() function. By default this parameter is set to
181 : * the empty string.
182 : *
183 : * Whenever a message is received, this code is checked. If defined
184 : * in the server and not equal to the code in the message, then the
185 : * message is discarded (hackers?)
186 : *
187 : * The message is also used when sending a message. It gets added
188 : * to the message if it is not the empty string.
189 : *
190 : * \return The secret code.
191 : */
192 0 : std::string const & udp_server_connection::get_secret_code() const
193 : {
194 0 : return f_secret_code;
195 : }
196 :
197 :
198 :
199 6 : } // namespace ed
200 : // vim: ts=4 sw=4 et
|