- sending and getting sessions
[oweals/gnunet.git] / src / transport / plugin_transport_udp_new.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 #include "platform.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fragmentation_lib.h"
32 #include "gnunet_nat_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 DEBUG_UDP GNUNET_NO
45 #define DEBUG_UDP_BROADCASTING GNUNET_NO
46
47 /**
48  * MTU for fragmentation subsystem.  Should be conservative since
49  * all communicating peers MUST work with this MTU.
50  */
51 #define UDP_MTU 1400
52
53
54 GNUNET_NETWORK_STRUCT_BEGIN
55 /**
56  * Network format for IPv4 addresses.
57  */
58 struct IPv4UdpAddress
59 {
60   /**
61    * IPv4 address, in network byte order.
62    */
63   uint32_t ipv4_addr GNUNET_PACKED;
64
65   /**
66    * Port number, in network byte order.
67    */
68   uint16_t u4_port GNUNET_PACKED;
69 };
70
71
72 /**
73  * Network format for IPv6 addresses.
74  */
75 struct IPv6UdpAddress
76 {
77
78   /**
79    * IPv6 address.
80    */
81   struct in6_addr ipv6_addr GNUNET_PACKED;
82
83   /**
84    * Port number, in network byte order.
85    */
86   uint16_t u6_port GNUNET_PACKED;
87 };
88 GNUNET_NETWORK_STRUCT_END
89
90
91 /**
92  * UDP Message-Packet header (after defragmentation).
93  */
94 struct UDPMessage
95 {
96   /**
97    * Message header.
98    */
99   struct GNUNET_MessageHeader header;
100
101   /**
102    * Always zero for now.
103    */
104   uint32_t reserved;
105
106   /**
107    * What is the identity of the sender
108    */
109   struct GNUNET_PeerIdentity sender;
110
111 };
112
113 struct UDPMessageWrapper
114 {
115   struct Session *session;
116   struct UDPMessageWrapper *prev;
117   struct UDPMessageWrapper *next;
118   struct UDPMessage *udp;
119   size_t msg_size;
120   /**
121    * Function to call upon completion of the transmission.
122    */
123   GNUNET_TRANSPORT_TransmitContinuation cont;
124
125   /**
126    * Closure for 'cont'.
127    */
128   void *cont_cls;
129 };
130
131 /**
132  * Encapsulation of all of the state of the plugin.
133  */
134 struct Plugin
135 {
136
137   /**
138    * Our environment.
139    */
140   struct GNUNET_TRANSPORT_PluginEnvironment *env;
141
142   /**
143    * Session of peers with whom we are currently connected,
144    * map of peer identity to 'struct PeerSession'.
145    */
146   struct GNUNET_CONTAINER_MultiHashMap *sessions;
147
148   /**
149    * Heap with all of our defragmentation activities.
150    */
151   struct GNUNET_CONTAINER_Heap *defrags;
152
153   /**
154    * ID of select task
155    */
156   GNUNET_SCHEDULER_TaskIdentifier select_task;
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    * Handle to NAT traversal support.
180    */
181   struct GNUNET_NAT_Handle *nat;
182
183   /**
184    * FD Read set
185    */
186   struct GNUNET_NETWORK_FDSet *rs;
187
188   /**
189    * FD Write set
190    */
191   struct GNUNET_NETWORK_FDSet *ws;
192
193   /**
194    * The read socket for IPv4
195    */
196   struct GNUNET_NETWORK_Handle *sockv4;
197
198   /**
199    * The read socket for IPv6
200    */
201   struct GNUNET_NETWORK_Handle *sockv6;
202
203   /**
204    * Beacon broadcasting
205    * -------------------
206    */
207
208   /**
209    * Broadcast interval
210    */
211   struct GNUNET_TIME_Relative broadcast_interval;
212
213   /**
214    * Broadcast with IPv4
215    */
216   int broadcast_ipv4;
217
218   /**
219    * Broadcast with IPv6
220    */
221   int broadcast_ipv6;
222
223
224   /**
225    * Tokenizer for inbound messages.
226    */
227   struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv6_mst;
228   struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv4_mst;
229
230   /**
231    * ID of select broadcast task
232    */
233   GNUNET_SCHEDULER_TaskIdentifier send_ipv4_broadcast_task;
234
235   /**
236    * ID of select broadcast task
237    */
238   GNUNET_SCHEDULER_TaskIdentifier send_ipv6_broadcast_task;
239
240   /**
241    * IPv6 multicast address
242    */
243   struct sockaddr_in6 ipv6_multicast_address;
244
245   /**
246    * DLL of IPv4 broadcast addresses
247    */
248   struct BroadcastAddress *ipv4_broadcast_tail;
249   struct BroadcastAddress *ipv4_broadcast_head;
250
251
252   /**
253    * expected delay for ACKs
254    */
255   struct GNUNET_TIME_Relative last_expected_delay;
256
257   /**
258    * Enable IPv6
259    */
260   int enable_ipv6;
261
262   /**
263    * Port we broadcasting on.
264    */
265   uint16_t broadcast_port;
266
267   /**
268    * Port we listen on.
269    */
270   uint16_t port;
271
272   /**
273    * Port we advertise on.
274    */
275   uint16_t aport;
276
277   struct UDPMessageWrapper *msg_head;
278   struct UDPMessageWrapper *msg_tail;
279
280 };
281
282
283 const char *
284 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
285
286 void
287 udp_broadcast_receive ();
288
289 void
290 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4);
291
292 void
293 stop_broadcast (struct Plugin *plugin);
294
295 /* end of plugin_transport_udp.h */