call GNUNET_SERVER_receive_done() also on internal error paths
[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., 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  * 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  * UDP Message-Packet header (after defragmentation).
125  */
126 struct UDPMessage
127 {
128   /**
129    * Message header.
130    */
131   struct GNUNET_MessageHeader header;
132
133   /**
134    * Always zero for now.
135    */
136   uint32_t reserved;
137
138   /**
139    * What is the identity of the sender
140    */
141   struct GNUNET_PeerIdentity sender;
142
143 };
144
145 struct UDP_MessageWrapper;
146
147 struct PrettyPrinterContext;
148
149
150 /**
151  * Encapsulation of all of the state of the plugin.
152  */
153 struct Plugin
154 {
155
156   /**
157    * Our environment.
158    */
159   struct GNUNET_TRANSPORT_PluginEnvironment *env;
160
161   /**
162    * Session of peers with whom we are currently connected,
163    * map of peer identity to `struct Session *`.
164    */
165   struct GNUNET_CONTAINER_MultiPeerMap *sessions;
166
167   /**
168    * Heap with all of our defragmentation activities.
169    */
170   struct GNUNET_CONTAINER_Heap *defrag_ctxs;
171
172   /**
173    * ID of select task for IPv4
174    */
175   struct GNUNET_SCHEDULER_Task *select_task;
176
177   /**
178    * ID of select task for IPv6
179    */
180   struct GNUNET_SCHEDULER_Task *select_task_v6;
181
182   /**
183    * Tokenizer for inbound messages.
184    */
185   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
186
187   /**
188    * Bandwidth tracker to limit global UDP traffic.
189    */
190   struct GNUNET_BANDWIDTH_Tracker tracker;
191
192   /**
193    * Address we were told to bind to exclusively (IPv4).
194    */
195   char *bind4_address;
196
197   /**
198    * Address we were told to bind to exclusively (IPv6).
199    */
200   char *bind6_address;
201
202   /**
203    * Handle to NAT traversal support.
204    */
205   struct GNUNET_NAT_Handle *nat;
206
207   /**
208    * FD Read set
209    */
210   struct GNUNET_NETWORK_FDSet *rs_v4;
211
212   /**
213    * FD Write set
214    */
215   struct GNUNET_NETWORK_FDSet *ws_v4;
216
217   /**
218    * The read socket for IPv4
219    */
220   struct GNUNET_NETWORK_Handle *sockv4;
221
222   /**
223    * FD Read set
224    */
225   struct GNUNET_NETWORK_FDSet *rs_v6;
226
227   /**
228    * FD Write set
229    */
230   struct GNUNET_NETWORK_FDSet *ws_v6;
231
232   /**
233    * The read socket for IPv6
234    */
235   struct GNUNET_NETWORK_Handle *sockv6;
236
237   /**
238    * Tokenizer for inbound messages.
239    */
240   struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_mst;
241
242   /**
243    * Head of DLL of broadcast addresses
244    */
245   struct BroadcastAddress *broadcast_tail;
246
247   /**
248    * Tail of DLL of broadcast addresses
249    */
250   struct BroadcastAddress *broadcast_head;
251
252   /**
253    * Head of messages in IPv4 queue.
254    */
255   struct UDP_MessageWrapper *ipv4_queue_head;
256
257   /**
258    * Tail of messages in IPv4 queue.
259    */
260   struct UDP_MessageWrapper *ipv4_queue_tail;
261
262   /**
263    * Head of messages in IPv6 queue.
264    */
265   struct UDP_MessageWrapper *ipv6_queue_head;
266
267   /**
268    * Tail of messages in IPv6 queue.
269    */
270   struct UDP_MessageWrapper *ipv6_queue_tail;
271
272   /**
273    * Running pretty printers: head
274    */
275   struct PrettyPrinterContext *ppc_dll_head;
276
277   /**
278    * Running pretty printers: tail
279    */
280   struct PrettyPrinterContext *ppc_dll_tail;
281
282   /**
283    * Function to call about session status changes.
284    */
285   GNUNET_TRANSPORT_SessionInfoCallback sic;
286
287   /**
288    * Closure for @e sic.
289    */
290   void *sic_cls;
291
292   /**
293    * IPv6 multicast address
294    */
295   struct sockaddr_in6 ipv6_multicast_address;
296
297   /**
298    * Broadcast interval
299    */
300   struct GNUNET_TIME_Relative broadcast_interval;
301
302   /**
303    * Bytes currently in buffer
304    */
305   int64_t bytes_in_buffer;
306
307   /**
308    * Address options
309    */
310   uint32_t myoptions;
311
312   /**
313    * Is IPv6 enabled: #GNUNET_YES or #GNUNET_NO
314    */
315   int enable_ipv6;
316
317   /**
318    * Is IPv4 enabled: #GNUNET_YES or #GNUNET_NO
319    */
320   int enable_ipv4;
321
322   /**
323    * Is broadcasting enabled: #GNUNET_YES or #GNUNET_NO
324    */
325   int enable_broadcasting;
326
327   /**
328    * Is receiving broadcasts enabled: #GNUNET_YES or #GNUNET_NO
329    */
330   int enable_broadcasting_receiving;
331
332   /**
333    * Port we broadcasting on.
334    */
335   uint16_t broadcast_port;
336
337   /**
338    * Port we listen on.
339    */
340   uint16_t port;
341
342   /**
343    * Port we advertise on.
344    */
345   uint16_t aport;
346
347 };
348
349
350 const char *
351 udp_address_to_string (void *cls,
352                        const void *addr,
353                        size_t addrlen);
354
355
356 /**
357  * We received a broadcast message.  Process it and all subsequent
358  * messages in the same packet.
359  *
360  * @param plugin the UDP plugin
361  * @param buf the buffer with the message(s)
362  * @param size number of bytes in @a buf
363  * @param udp_addr address of the sender
364  * @param udp_addr_len number of bytes in @a udp_addr
365  * @param network_type network type of the sender's address
366  */
367 void
368 udp_broadcast_receive (struct Plugin *plugin,
369                        const char *buf,
370                        ssize_t size,
371                        const union UdpAddress *udp_addr,
372                        size_t udp_addr_len,
373                        enum GNUNET_ATS_Network_Type network_type);
374
375
376 void
377 setup_broadcast (struct Plugin *plugin,
378                  struct sockaddr_in6 *server_addrv6,
379                  struct sockaddr_in *server_addrv4);
380
381
382 void
383 stop_broadcast (struct Plugin *plugin);
384
385 /*#ifndef PLUGIN_TRANSPORT_UDP_H*/
386 #endif
387 /* end of plugin_transport_udp.h */