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