Check that you are not present in trail twice
[oweals/gnunet.git] / src / transport / plugin_transport_udp.h
1 /*
2      This file is part of GNUnet
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 /**
107  * UDP Message-Packet header (after defragmentation).
108  */
109 struct UDPMessage
110 {
111   /**
112    * Message header.
113    */
114   struct GNUNET_MessageHeader header;
115
116   /**
117    * Always zero for now.
118    */
119   uint32_t reserved;
120
121   /**
122    * What is the identity of the sender
123    */
124   struct GNUNET_PeerIdentity sender;
125
126 };
127
128 struct UDP_MessageWrapper;
129
130 struct PrettyPrinterContext;
131
132
133 /**
134  * Encapsulation of all of the state of the plugin.
135  */
136 struct Plugin
137 {
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 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   GNUNET_SCHEDULER_TaskIdentifier select_task;
159
160   /**
161    * ID of select task for IPv6
162    */
163   GNUNET_SCHEDULER_TaskIdentifier select_task_v6;
164
165   /**
166    * Tokenizer for inbound messages.
167    */
168   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
169
170   /**
171    * Bandwidth tracker to limit global UDP traffic.
172    */
173   struct GNUNET_BANDWIDTH_Tracker tracker;
174
175   /**
176    * Address we were told to bind to exclusively (IPv4).
177    */
178   char *bind4_address;
179
180   /**
181    * Address we were told to bind to exclusively (IPv6).
182    */
183   char *bind6_address;
184
185   /**
186    * Handle to NAT traversal support.
187    */
188   struct GNUNET_NAT_Handle *nat;
189
190   /**
191    * FD Read set
192    */
193   struct GNUNET_NETWORK_FDSet *rs_v4;
194
195   /**
196    * FD Write set
197    */
198   struct GNUNET_NETWORK_FDSet *ws_v4;
199
200   /**
201    * The read socket for IPv4
202    */
203   struct GNUNET_NETWORK_Handle *sockv4;
204
205   /**
206    * FD Read set
207    */
208   struct GNUNET_NETWORK_FDSet *rs_v6;
209
210   /**
211    * FD Write set
212    */
213   struct GNUNET_NETWORK_FDSet *ws_v6;
214
215   /**
216    * The read socket for IPv6
217    */
218   struct GNUNET_NETWORK_Handle *sockv6;
219
220   /**
221    * Tokenizer for inbound IPv6 messages.
222    */
223   struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv6_mst;
224
225   /**
226    * Tokenizer for inbound IPv4 messages.
227    */
228   struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv4_mst;
229
230   /**
231    * Head of DLL of broadcast addresses
232    */
233   struct BroadcastAddress *broadcast_tail;
234
235   /**
236    * Tail of DLL of broadcast addresses
237    */
238   struct BroadcastAddress *broadcast_head;
239
240   /**
241    * Head of messages in IPv4 queue.
242    */
243   struct UDP_MessageWrapper *ipv4_queue_head;
244
245   /**
246    * Tail of messages in IPv4 queue.
247    */
248   struct UDP_MessageWrapper *ipv4_queue_tail;
249
250   /**
251    * Head of messages in IPv6 queue.
252    */
253   struct UDP_MessageWrapper *ipv6_queue_head;
254
255   /**
256    * Tail of messages in IPv6 queue.
257    */
258   struct UDP_MessageWrapper *ipv6_queue_tail;
259
260   /**
261    * Running pretty printers: head
262    */
263   struct PrettyPrinterContext *ppc_dll_head;
264
265   /**
266    * Running pretty printers: tail
267    */
268   struct PrettyPrinterContext *ppc_dll_tail;
269
270   /**
271    * Function to call about session status changes.
272    */
273   GNUNET_TRANSPORT_SessionInfoCallback sic;
274
275   /**
276    * Closure for @e sic.
277    */
278   void *sic_cls;
279
280   /**
281    * IPv6 multicast address
282    */
283   struct sockaddr_in6 ipv6_multicast_address;
284
285   /**
286    * Broadcast interval
287    */
288   struct GNUNET_TIME_Relative broadcast_interval;
289
290   /**
291    * Bytes currently in buffer
292    */
293   int64_t bytes_in_buffer;
294
295   /**
296    * Address options
297    */
298   uint32_t myoptions;
299
300   /**
301    * Is IPv6 enabled: #GNUNET_YES or #GNUNET_NO
302    */
303   int enable_ipv6;
304
305   /**
306    * Is IPv4 enabled: #GNUNET_YES or #GNUNET_NO
307    */
308   int enable_ipv4;
309
310   /**
311    * Is broadcasting enabled: #GNUNET_YES or #GNUNET_NO
312    */
313   int enable_broadcasting;
314
315   /**
316    * Is receiving broadcasts enabled: #GNUNET_YES or #GNUNET_NO
317    */
318   int enable_broadcasting_receiving;
319
320   /**
321    * Port we broadcasting on.
322    */
323   uint16_t broadcast_port;
324
325   /**
326    * Port we listen on.
327    */
328   uint16_t port;
329
330   /**
331    * Port we advertise on.
332    */
333   uint16_t aport;
334
335 };
336
337
338 const char *
339 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
340
341 void
342 udp_broadcast_receive (struct Plugin *plugin,
343                        const char * buf,
344                        ssize_t size,
345                        const struct sockaddr *addr,
346                        size_t addrlen);
347
348 void
349 setup_broadcast (struct Plugin *plugin,
350                  struct sockaddr_in6 *server_addrv6,
351                  struct sockaddr_in *server_addrv4);
352
353 void
354 stop_broadcast (struct Plugin *plugin);
355
356 /*#ifndef PLUGIN_TRANSPORT_UDP_H*/
357 #endif
358 /* end of plugin_transport_udp.h */