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