fix warnings
[oweals/gnunet.git] / src / transport / plugin_transport_udp.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010-2014 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file transport/plugin_transport_udp.h
23  * @brief Implementation of the UDP transport protocol
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  * @author Matthias Wachs
27  */
28 #ifndef PLUGIN_TRANSPORT_UDP_H
29 #define PLUGIN_TRANSPORT_UDP_H
30
31 #include "platform.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_fragmentation_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_resolver_service.h"
37 #include "gnunet_signatures.h"
38 #include "gnunet_constants.h"
39 #include "gnunet_statistics_service.h"
40 #include "gnunet_transport_service.h"
41 #include "gnunet_transport_plugin.h"
42 #include "transport.h"
43
44 #define LOG(kind, ...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
45
46 #define PLUGIN_NAME "udp"
47
48 #define DEBUG_UDP GNUNET_NO
49
50 #define DEBUG_UDP_BROADCASTING GNUNET_NO
51
52 /**
53  * MTU for fragmentation subsystem.  Should be conservative since
54  * all communicating peers MUST work with this MTU.
55  */
56 #define UDP_MTU 1400
57
58
59 GNUNET_NETWORK_STRUCT_BEGIN
60 /**
61  * Network format for IPv4 addresses.
62  */
63 struct IPv4UdpAddress
64 {
65   /**
66    * Optional options and flags for this address
67    */
68   uint32_t options GNUNET_PACKED;
69
70   /**
71    * IPv4 address, in network byte order.
72    */
73   uint32_t ipv4_addr GNUNET_PACKED;
74
75   /**
76    * Port number, in network byte order.
77    */
78   uint16_t u4_port GNUNET_PACKED;
79 };
80
81
82 /**
83  * Network format for IPv6 addresses.
84  */
85 struct IPv6UdpAddress
86 {
87   /**
88    * Optional options and flags for this address
89    */
90   uint32_t options GNUNET_PACKED;
91
92   /**
93    * IPv6 address.
94    */
95   struct in6_addr ipv6_addr GNUNET_PACKED;
96
97   /**
98    * Port number, in network byte order.
99    */
100   uint16_t u6_port GNUNET_PACKED;
101 };
102 GNUNET_NETWORK_STRUCT_END
103
104 /**
105  * Either an IPv4 or IPv6 UDP address.  Note that without a "length",
106  * one cannot tell which one of the two types this address represents.
107  */
108 union UdpAddress
109 {
110   /**
111    * IPv4 case.
112    */
113   struct IPv4UdpAddress v4;
114
115   /**
116    * IPv6 case.
117    */
118   struct IPv6UdpAddress v6;
119 };
120
121
122 /**
123  * Information we track for each message in the queue.
124  */
125 struct UDP_MessageWrapper;
126
127
128 /**
129  * Closure for #append_port().
130  */
131 struct PrettyPrinterContext;
132
133
134 /**
135  * Encapsulation of all of the state of the plugin.
136  */
137 struct Plugin
138 {
139   /**
140    * Our environment.
141    */
142   struct GNUNET_TRANSPORT_PluginEnvironment *env;
143
144   /**
145    * Session of peers with whom we are currently connected,
146    * map of peer identity to `struct GNUNET_ATS_Session *`.
147    */
148   struct GNUNET_CONTAINER_MultiPeerMap *sessions;
149
150   /**
151    * Heap with all of our defragmentation activities.
152    */
153   struct GNUNET_CONTAINER_Heap *defrag_ctxs;
154
155   /**
156    * ID of select task for IPv4
157    */
158   struct GNUNET_SCHEDULER_Task *select_task_v4;
159
160   /**
161    * ID of select task for IPv6
162    */
163   struct GNUNET_SCHEDULER_Task *select_task_v6;
164
165   /**
166    * Bandwidth tracker to limit global UDP traffic.
167    */
168   struct GNUNET_BANDWIDTH_Tracker tracker;
169
170   /**
171    * Address we were told to bind to exclusively (IPv4).
172    */
173   char *bind4_address;
174
175   /**
176    * Address we were told to bind to exclusively (IPv6).
177    */
178   char *bind6_address;
179
180   /**
181    * Handle to NAT traversal support.
182    */
183   struct GNUNET_NAT_Handle *nat;
184
185   /**
186    * Handle to NAT traversal support.
187    */
188   struct GNUNET_NAT_STUN_Handle *stun;
189
190   /**
191    * The read socket for IPv4
192    */
193   struct GNUNET_NETWORK_Handle *sockv4;
194
195   /**
196    * The read socket for IPv6
197    */
198   struct GNUNET_NETWORK_Handle *sockv6;
199
200   /**
201    * Head of DLL of broadcast addresses
202    */
203   struct BroadcastAddress *broadcast_tail;
204
205   /**
206    * Tail of DLL of broadcast addresses
207    */
208   struct BroadcastAddress *broadcast_head;
209
210   /**
211    * Head of messages in IPv4 queue.
212    */
213   struct UDP_MessageWrapper *ipv4_queue_head;
214
215   /**
216    * Tail of messages in IPv4 queue.
217    */
218   struct UDP_MessageWrapper *ipv4_queue_tail;
219
220   /**
221    * Head of messages in IPv6 queue.
222    */
223   struct UDP_MessageWrapper *ipv6_queue_head;
224
225   /**
226    * Tail of messages in IPv6 queue.
227    */
228   struct UDP_MessageWrapper *ipv6_queue_tail;
229
230   /**
231    * Running pretty printers: head
232    */
233   struct PrettyPrinterContext *ppc_dll_head;
234
235   /**
236    * Running pretty printers: tail
237    */
238   struct PrettyPrinterContext *ppc_dll_tail;
239
240   /**
241    * Function to call about session status changes.
242    */
243   GNUNET_TRANSPORT_SessionInfoCallback sic;
244
245   /**
246    * Closure for @e sic.
247    */
248   void *sic_cls;
249
250   /**
251    * IPv6 multicast address
252    */
253   struct sockaddr_in6 ipv6_multicast_address;
254
255   /**
256    * Broadcast interval
257    */
258   struct GNUNET_TIME_Relative broadcast_interval;
259
260   /**
261    * Bytes currently in buffer
262    */
263   int64_t bytes_in_buffer;
264
265   /**
266    * Address options
267    */
268   uint32_t myoptions;
269
270   /**
271    * Is IPv6 enabled: #GNUNET_YES or #GNUNET_NO
272    */
273   int enable_ipv6;
274
275   /**
276    * Is IPv4 enabled: #GNUNET_YES or #GNUNET_NO
277    */
278   int enable_ipv4;
279
280   /**
281    * Is broadcasting enabled: #GNUNET_YES or #GNUNET_NO
282    */
283   int enable_broadcasting;
284
285   /**
286    * Is receiving broadcasts enabled: #GNUNET_YES or #GNUNET_NO
287    */
288   int enable_broadcasting_receiving;
289
290   /**
291    * Port we broadcasting on.
292    */
293   uint16_t broadcast_port;
294
295   /**
296    * Port we listen on.
297    */
298   uint16_t port;
299
300   /**
301    * Port we advertise on.
302    */
303   uint16_t aport;
304 };
305
306
307 /**
308  * Function called for a quick conversion of the binary address to
309  * a numeric address.  Note that the caller must not free the
310  * address and that the next call to this function is allowed
311  * to override the address again.
312  *
313  * @param cls closure
314  * @param addr binary address (a `union UdpAddress`)
315  * @param addrlen length of the @a addr
316  * @return string representing the same address
317  */
318 const char *
319 udp_address_to_string (void *cls,
320                        const void *addr,
321                        size_t addrlen);
322
323
324 /**
325  * We received a broadcast message.  Process it and all subsequent
326  * messages in the same packet.
327  *
328  * @param plugin the UDP plugin
329  * @param buf the buffer with the message(s)
330  * @param size number of bytes in @a buf
331  * @param udp_addr address of the sender
332  * @param udp_addr_len number of bytes in @a udp_addr
333  * @param network_type network type of the sender's address
334  */
335 void
336 udp_broadcast_receive (struct Plugin *plugin,
337                        const char *buf,
338                        ssize_t size,
339                        const union UdpAddress *udp_addr,
340                        size_t udp_addr_len,
341                        enum GNUNET_NetworkType network_type);
342
343
344 void
345 setup_broadcast (struct Plugin *plugin,
346                  struct sockaddr_in6 *server_addrv6,
347                  struct sockaddr_in *server_addrv4);
348
349
350 void
351 stop_broadcast (struct Plugin *plugin);
352
353 /*#ifndef PLUGIN_TRANSPORT_UDP_H*/
354 #endif
355 /* end of plugin_transport_udp.h */