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 local datagram server connection.
22 : *
23 : * This class implements support offering a UDP like service using an
24 : * AF_UNIX socket. The type of datagram can be set to SOCK_DGRAM
25 : * (sequential = false) or SOCK_SEQPACKET (sequential = true).
26 : */
27 :
28 :
29 : // self
30 : //
31 : #include "eventdispatcher/local_dgram_server_connection.h"
32 :
33 :
34 : // last include
35 : //
36 : #include <snapdev/poison.h>
37 :
38 :
39 :
40 : namespace ed
41 : {
42 :
43 :
44 :
45 : /** \brief Initialize a UDP listener.
46 : *
47 : * This function is used to initialize a server connection, a UDP/IP
48 : * listener which wakes up whenever a send() is sent to this listener
49 : * address and port.
50 : *
51 : * \param[in] address The address to listen on. It may be set to "0.0.0.0".
52 : * \param[in] sequential Whether the packets are kept in order.
53 : * \param[in] close_on_exec Whether the socket is closed on execve().
54 : * \param[in] force_reuse_addr Whether caller is okay with an unlink() of
55 : * a file socket if it exists.
56 : */
57 2 : local_dgram_server_connection::local_dgram_server_connection(
58 : addr::unix const & address
59 : , bool sequential
60 : , bool close_on_exec
61 2 : , bool force_reuse_addr)
62 2 : : local_dgram_server(address, sequential, close_on_exec, force_reuse_addr)
63 : {
64 2 : }
65 :
66 :
67 : /** \brief Check to know whether this UDP connection is a reader.
68 : *
69 : * This function returns true to say that this UDP connection is
70 : * indeed a reader.
71 : *
72 : * \return This function already returns true as we are likely to
73 : * always want a UDP socket to be listening for incoming
74 : * packets.
75 : */
76 5 : bool local_dgram_server_connection::is_reader() const
77 : {
78 5 : return true;
79 : }
80 :
81 :
82 : /** \brief Retrieve the socket of this server connection.
83 : *
84 : * This function retrieves the socket this server connection. In this case
85 : * the socket is defined in the local_dgram_server class.
86 : *
87 : * \return The socket of this client connection.
88 : */
89 18 : int local_dgram_server_connection::get_socket() const
90 : {
91 18 : return local_dgram_server::get_socket();
92 : }
93 :
94 :
95 : /** \brief Define a secret code.
96 : *
97 : * When receiving a message through this UDP socket, this secret code must
98 : * be included in the message. If not present, then the message gets
99 : * discarded.
100 : *
101 : * By default this parameter is an empty string. This means no secret
102 : * code is required and UDP communication can be done without it.
103 : *
104 : * \note
105 : * Secret codes are expected to be used only on connections between
106 : * computers. If the IP address is 127.0.0.1, you probably don't need
107 : * to have a secret code.
108 : *
109 : * \warning
110 : * Remember that UDP messages are limited in size. If too long, the
111 : * send_message() function throws an error. So your secret code should
112 : * remain relatively small.
113 : *
114 : * \todo
115 : * The secret_code string must be a valid UTF-8 string. At this point
116 : * this is not enforced.
117 : *
118 : * \param[in] secret_code The secret code that has to be included in the
119 : * incoming messages for those to be accepted.
120 : */
121 0 : void local_dgram_server_connection::set_secret_code(std::string const & secret_code)
122 : {
123 0 : f_secret_code = secret_code;
124 0 : }
125 :
126 :
127 : /** \brief Retrieve the server secret code.
128 : *
129 : * This function returns the server secret code as defined with the
130 : * set_secret_code() function. By default this parameter is set to
131 : * the empty string.
132 : *
133 : * Whenever a message is received, this code is checked. If defined
134 : * in the server and not equal to the code in the message, then the
135 : * message is discarded (hackers?)
136 : *
137 : * The message is also used when sending a message. It gets added
138 : * to the message if it is not the empty string.
139 : *
140 : * \return The secret code.
141 : */
142 3 : std::string const & local_dgram_server_connection::get_secret_code() const
143 : {
144 3 : return f_secret_code;
145 : }
146 :
147 :
148 :
149 6 : } // namespace ed
150 : // vim: ts=4 sw=4 et
|