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