log
[oweals/gnunet.git] / src / transport / plugin_transport_udp.h
1 /*
2      This file is part of GNUnet
3      (C) 2010, 2011 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 #define DEBUG_UDP_BROADCASTING GNUNET_NO
51
52 /**
53  * MTU for fragmentation subsystem.  Should be conservative since
54  * all communicating peers MUST work with this MTU.
55  */
56 #define UDP_MTU 1400
57
58
59 GNUNET_NETWORK_STRUCT_BEGIN
60 /**
61  * Network format for IPv4 addresses.
62  */
63 struct IPv4UdpAddress
64 {
65   /**
66    * Optional options and flags for this address
67    */
68   uint32_t options GNUNET_PACKED;
69
70   /**
71    * IPv4 address, in network byte order.
72    */
73   uint32_t ipv4_addr GNUNET_PACKED;
74
75   /**
76    * Port number, in network byte order.
77    */
78   uint16_t u4_port GNUNET_PACKED;
79 };
80
81
82 /**
83  * Network format for IPv6 addresses.
84  */
85 struct IPv6UdpAddress
86 {
87   /**
88    * Optional options and flags for this address
89    */
90   uint32_t options GNUNET_PACKED;
91
92   /**
93    * IPv6 address.
94    */
95   struct in6_addr ipv6_addr GNUNET_PACKED;
96
97   /**
98    * Port number, in network byte order.
99    */
100   uint16_t u6_port GNUNET_PACKED;
101 };
102 GNUNET_NETWORK_STRUCT_END
103
104
105 /**
106  * UDP Message-Packet header (after defragmentation).
107  */
108 struct UDPMessage
109 {
110   /**
111    * Message header.
112    */
113   struct GNUNET_MessageHeader header;
114
115   /**
116    * Always zero for now.
117    */
118   uint32_t reserved;
119
120   /**
121    * What is the identity of the sender
122    */
123   struct GNUNET_PeerIdentity sender;
124
125 };
126
127 struct UDP_MessageWrapper;
128
129
130 /**
131  * Encapsulation of all of the state of the plugin.
132  */
133 struct Plugin
134 {
135
136   /**
137    * Our environment.
138    */
139   struct GNUNET_TRANSPORT_PluginEnvironment *env;
140
141   /**
142    * Session of peers with whom we are currently connected,
143    * map of peer identity to 'struct PeerSession'.
144    */
145   struct GNUNET_CONTAINER_MultiPeerMap *sessions;
146
147   /**
148    * Heap with all of our defragmentation activities.
149    */
150   struct GNUNET_CONTAINER_Heap *defrag_ctxs;
151
152   /**
153    * ID of select task
154    */
155   GNUNET_SCHEDULER_TaskIdentifier select_task;
156   GNUNET_SCHEDULER_TaskIdentifier select_task_v6;
157
158   /**
159    * Tokenizer for inbound messages.
160    */
161   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
162
163   /**
164    * Bandwidth tracker to limit global UDP traffic.
165    */
166   struct GNUNET_BANDWIDTH_Tracker tracker;
167
168   /**
169    * Address we were told to bind to exclusively (IPv4).
170    */
171   char *bind4_address;
172
173   /**
174    * Address we were told to bind to exclusively (IPv6).
175    */
176   char *bind6_address;
177
178
179   /**
180    * Bytes currently in buffer
181    */
182   int64_t bytes_in_buffer;
183
184   /**
185    * Handle to NAT traversal support.
186    */
187   struct GNUNET_NAT_Handle *nat;
188
189   /**
190    * FD Read set
191    */
192   struct GNUNET_NETWORK_FDSet *rs_v4;
193
194   /**
195    * FD Write set
196    */
197   struct GNUNET_NETWORK_FDSet *ws_v4;
198
199
200   /**
201    * The read socket for IPv4
202    */
203   struct GNUNET_NETWORK_Handle *sockv4;
204
205
206   /**
207    * FD Read set
208    */
209   struct GNUNET_NETWORK_FDSet *rs_v6;
210
211   /**
212    * FD Write set
213    */
214   struct GNUNET_NETWORK_FDSet *ws_v6;
215
216   /**
217    * The read socket for IPv6
218    */
219   struct GNUNET_NETWORK_Handle *sockv6;
220
221   /**
222    * Beacon broadcasting
223    * -------------------
224    */
225
226   /**
227    * Broadcast interval
228    */
229   struct GNUNET_TIME_Relative broadcast_interval;
230
231   /**
232    * Tokenizer for inbound messages.
233    */
234   struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv6_mst;
235   struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv4_mst;
236
237   /**
238    * IPv6 multicast address
239    */
240   struct sockaddr_in6 ipv6_multicast_address;
241
242   /**
243    * DLL of broadcast addresses
244    */
245   struct BroadcastAddress *broadcast_tail;
246   struct BroadcastAddress *broadcast_head;
247
248   /**
249    * Is IPv6 enabled: GNUNET_YES or GNUNET_NO
250    */
251   int enable_ipv6;
252
253   /**
254    * Is IPv4 enabled: GNUNET_YES or GNUNET_NO
255    */
256   int enable_ipv4;
257
258   /**
259    * Is broadcasting enabled: GNUNET_YES or GNUNET_NO
260    */
261   int enable_broadcasting;
262
263   /**
264    * Is receiving broadcasts enabled: GNUNET_YES or GNUNET_NO
265    */
266   int enable_broadcasting_receiving;
267
268   /**
269    * Port we broadcasting on.
270    */
271   uint16_t broadcast_port;
272
273   /**
274    * Port we listen on.
275    */
276   uint16_t port;
277
278   /**
279    * Port we advertise on.
280    */
281   uint16_t aport;
282
283   struct UDP_MessageWrapper *ipv4_queue_head;
284   struct UDP_MessageWrapper *ipv4_queue_tail;
285
286   struct UDP_MessageWrapper *ipv6_queue_head;
287   struct UDP_MessageWrapper *ipv6_queue_tail;
288 };
289
290
291 const char *
292 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
293
294 void
295 udp_broadcast_receive (struct Plugin *plugin,
296                        const char * buf,
297                        ssize_t size,
298                        const struct sockaddr *addr,
299                        size_t addrlen);
300
301 void
302 setup_broadcast (struct Plugin *plugin,
303                  struct sockaddr_in6 *server_addrv6,
304                  struct sockaddr_in *server_addrv4);
305
306 void
307 stop_broadcast (struct Plugin *plugin);
308
309 /*#ifndef PLUGIN_TRANSPORT_UDP_H*/
310 #endif
311 /* end of plugin_transport_udp.h */