Line data Source code
1 : // Copyright (c) 2012-2021 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/udp_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 UDP listener.
60 : *
61 : * This function is used to initialize a server connection, a UDP/IP
62 : * listener which wakes up whenever a send() is sent to this listener
63 : * address and port.
64 : *
65 : * \param[in] addr The address to listen on. It may be set to "0.0.0.0".
66 : * \param[in] port The port to listen on.
67 : * \param[in] family The family used to search for 'addr' (IPv4 or IPv6).
68 : * \param[in] multicast_addr A multicast address (224.x.x.x) or nullptr.
69 : */
70 0 : udp_server_connection::udp_server_connection(
71 : std::string const & addr
72 : , int port
73 : , int family
74 0 : , std::string const * multicast_addr)
75 0 : : udp_server(addr, port, family, multicast_addr)
76 : {
77 0 : }
78 :
79 :
80 : /** \brief Check to know whether this UDP connection is a reader.
81 : *
82 : * This function returns true to say that this UDP connection is
83 : * indeed a reader.
84 : *
85 : * \return This function already returns true as we are likely to
86 : * always want a UDP socket to be listening for incoming
87 : * packets.
88 : */
89 0 : bool udp_server_connection::is_reader() const
90 : {
91 0 : return true;
92 : }
93 :
94 :
95 : /** \brief Retrieve the socket of this server connection.
96 : *
97 : * This function retrieves the socket this server connection. In this case
98 : * the socket is defined in the udp_server class.
99 : *
100 : * \return The socket of this client connection.
101 : */
102 0 : int udp_server_connection::get_socket() const
103 : {
104 0 : return udp_server::get_socket();
105 : }
106 :
107 :
108 : /** \brief Define a secret code.
109 : *
110 : * When receiving a message through this UDP socket, this secret code must
111 : * be included in the message. If not present, then the message gets
112 : * discarded.
113 : *
114 : * By default this parameter is an empty string. This means no secret
115 : * code is required and UDP communication can be done without it.
116 : *
117 : * \note
118 : * Secret codes are expected to be used only on connections between
119 : * computers. If the IP address is 127.0.0.1, you probably don't need
120 : * to have a secret code.
121 : *
122 : * \warning
123 : * Remember that UDP messages are limited in size. If too long, the
124 : * send_message() function throws an error. So your secret code should
125 : * remain relatively small.
126 : *
127 : * \todo
128 : * The secret_code string must be a valid UTF-8 string. At this point
129 : * this is not enforced.
130 : *
131 : * \param[in] secret_code The secret code that has to be included in the
132 : * incoming messages for those to be accepted.
133 : */
134 0 : void udp_server_connection::set_secret_code(std::string const & secret_code)
135 : {
136 0 : f_secret_code = secret_code;
137 0 : }
138 :
139 :
140 : /** \brief Retrieve the server secret code.
141 : *
142 : * This function returns the server secret code as defined with the
143 : * set_secret_code() function. By default this parameter is set to
144 : * the empty string.
145 : *
146 : * Whenever a message is received, this code is checked. If defined
147 : * in the server and not equal to the code in the message, then the
148 : * message is discarded (hackers?)
149 : *
150 : * The message is also used when sending a message. It gets added
151 : * to the message if it is not the empty string.
152 : *
153 : * \return The secret code.
154 : */
155 0 : std::string const & udp_server_connection::get_secret_code() const
156 : {
157 0 : return f_secret_code;
158 : }
159 :
160 :
161 :
162 6 : } // namespace ed
163 : // vim: ts=4 sw=4 et
|