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