aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2012-02-13 17:15:52 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2012-02-13 17:15:52 +0000
commitcee04ec80653f8fecf198d8fe14dc73f84b53524 (patch)
treeaeca1509c9ccd350fc25eb998f25a3c5427107b0
parent3b2be11385b8b4626b86d4438008eb0508f856fa (diff)
downloadgnunet-cee04ec80653f8fecf198d8fe14dc73f84b53524.tar.gz
gnunet-cee04ec80653f8fecf198d8fe14dc73f84b53524.zip
- coverity errors
-rw-r--r--src/ats/gnunet-service-ats_addresses_mlp.c2
-rw-r--r--src/include/gnunet_transport_service.h2
-rw-r--r--src/transport/gnunet-service-transport_neighbours.c4
-rw-r--r--src/transport/gnunet-service-transport_neighbours.h6
-rw-r--r--src/transport/plugin_transport_tcp.c4
-rw-r--r--src/transport/plugin_transport_udp.c2
-rw-r--r--src/transport/plugin_transport_udp_new.c2054
-rw-r--r--src/transport/plugin_transport_unix.c1
-rw-r--r--src/transport/transport-testing.c23
-rw-r--r--src/transport/transport-testing.h8
10 files changed, 24 insertions, 2082 deletions
diff --git a/src/ats/gnunet-service-ats_addresses_mlp.c b/src/ats/gnunet-service-ats_addresses_mlp.c
index cdda9d790..9850bc2a3 100644
--- a/src/ats/gnunet-service-ats_addresses_mlp.c
+++ b/src/ats/gnunet-service-ats_addresses_mlp.c
@@ -1404,7 +1404,7 @@ void
1404GAS_mlp_address_change_preference (struct GAS_MLP_Handle *mlp, 1404GAS_mlp_address_change_preference (struct GAS_MLP_Handle *mlp,
1405 const struct GNUNET_PeerIdentity *peer, 1405 const struct GNUNET_PeerIdentity *peer,
1406 enum GNUNET_ATS_PreferenceKind kind, 1406 enum GNUNET_ATS_PreferenceKind kind,
1407 float ) 1407 float score)
1408{ 1408{
1409 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO); 1409 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1410 1410
diff --git a/src/include/gnunet_transport_service.h b/src/include/gnunet_transport_service.h
index 5889b4a72..5c939a009 100644
--- a/src/include/gnunet_transport_service.h
+++ b/src/include/gnunet_transport_service.h
@@ -285,10 +285,8 @@ struct GNUNET_TRANSPORT_AddressToStringContext;
285 * 285 *
286 * @param cfg configuration to use 286 * @param cfg configuration to use
287 * @param address address to convert (binary format) 287 * @param address address to convert (binary format)
288 * @param addressLen number of bytes in address
289 * @param numeric should (IP) addresses be displayed in numeric form 288 * @param numeric should (IP) addresses be displayed in numeric form
290 * (otherwise do reverse DNS lookup) 289 * (otherwise do reverse DNS lookup)
291 * @param nameTrans name of the transport to which the address belongs
292 * @param timeout how long is the lookup allowed to take at most 290 * @param timeout how long is the lookup allowed to take at most
293 * @param aluc function to call with the results 291 * @param aluc function to call with the results
294 * @param aluc_cls closure for aluc 292 * @param aluc_cls closure for aluc
diff --git a/src/transport/gnunet-service-transport_neighbours.c b/src/transport/gnunet-service-transport_neighbours.c
index 6c072130b..c9af3e36b 100644
--- a/src/transport/gnunet-service-transport_neighbours.c
+++ b/src/transport/gnunet-service-transport_neighbours.c
@@ -1353,7 +1353,7 @@ send_connect_ack_continuation (void *cls,
1353 1353
1354/** 1354/**
1355 * For an existing neighbour record, set the active connection to 1355 * For an existing neighbour record, set the active connection to
1356 * the given address. 1356 * use the given address.
1357 * 1357 *
1358 * @param peer identity of the peer to switch the address for 1358 * @param peer identity of the peer to switch the address for
1359 * @param address address of the other peer, NULL if other peer 1359 * @param address address of the other peer, NULL if other peer
@@ -1361,6 +1361,8 @@ send_connect_ack_continuation (void *cls,
1361 * @param session session to use (or NULL) 1361 * @param session session to use (or NULL)
1362 * @param ats performance data 1362 * @param ats performance data
1363 * @param ats_count number of entries in ats 1363 * @param ats_count number of entries in ats
1364 * @param bandwidth_in inbound quota to be used when connection is up
1365 * @param bandwidth_out outbound quota to be used when connection is up
1364 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the 1366 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1365 * connection is not up (yet) 1367 * connection is not up (yet)
1366 */ 1368 */
diff --git a/src/transport/gnunet-service-transport_neighbours.h b/src/transport/gnunet-service-transport_neighbours.h
index f6e599db1..33fa1dac5 100644
--- a/src/transport/gnunet-service-transport_neighbours.h
+++ b/src/transport/gnunet-service-transport_neighbours.h
@@ -221,10 +221,8 @@ GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
221 *address, struct Session *session, 221 *address, struct Session *session,
222 const struct GNUNET_ATS_Information *ats, 222 const struct GNUNET_ATS_Information *ats,
223 uint32_t ats_count, 223 uint32_t ats_count,
224 struct GNUNET_BANDWIDTH_Value32NBO 224 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
225 bandwidth_in, 225 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out);
226 struct GNUNET_BANDWIDTH_Value32NBO
227 bandwidth_out);
228 226
229 227
230/** 228/**
diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c
index 4157024c9..325ebaf8b 100644
--- a/src/transport/plugin_transport_tcp.c
+++ b/src/transport/plugin_transport_tcp.c
@@ -961,9 +961,7 @@ int session_it (void *cls,
961 * notify us by calling the env->session_end function 961 * notify us by calling the env->session_end function
962 * 962 *
963 * @param cls closure 963 * @param cls closure
964 * @param target the neighbour id 964 * @param address pointer to the GNUNET_HELLO_Address
965 * @param addr pointer to the address
966 * @param addrlen length of addr
967 * @return the session if the address is valid, NULL otherwise 965 * @return the session if the address is valid, NULL otherwise
968 */ 966 */
969static struct Session * 967static struct Session *
diff --git a/src/transport/plugin_transport_udp.c b/src/transport/plugin_transport_udp.c
index 0e5e9037f..a83dc5c1b 100644
--- a/src/transport/plugin_transport_udp.c
+++ b/src/transport/plugin_transport_udp.c
@@ -823,7 +823,7 @@ enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
823 * a fresh connection to another peer. 823 * a fresh connection to another peer.
824 * 824 *
825 * @param cls closure 825 * @param cls closure
826 * @param session which session must be used 826 * @param s which session must be used
827 * @param msgbuf the message to transmit 827 * @param msgbuf the message to transmit
828 * @param msgbuf_size number of bytes in 'msgbuf' 828 * @param msgbuf_size number of bytes in 'msgbuf'
829 * @param priority how important is the message (most plugins will 829 * @param priority how important is the message (most plugins will
diff --git a/src/transport/plugin_transport_udp_new.c b/src/transport/plugin_transport_udp_new.c
deleted file mode 100644
index db5d14ed1..000000000
--- a/src/transport/plugin_transport_udp_new.c
+++ /dev/null
@@ -1,2054 +0,0 @@
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.c
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 "plugin_transport_udp_new.h"
30#include "gnunet_hello_lib.h"
31#include "gnunet_util_lib.h"
32#include "gnunet_fragmentation_lib.h"
33#include "gnunet_nat_lib.h"
34#include "gnunet_protocols.h"
35#include "gnunet_resolver_service.h"
36#include "gnunet_signatures.h"
37#include "gnunet_constants.h"
38#include "gnunet_statistics_service.h"
39#include "gnunet_transport_service.h"
40#include "gnunet_transport_plugin.h"
41#include "transport.h"
42
43#define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
44
45
46/**
47 * Number of messages we can defragment in parallel. We only really
48 * defragment 1 message at a time, but if messages get re-ordered, we
49 * may want to keep knowledge about the previous message to avoid
50 * discarding the current message in favor of a single fragment of a
51 * previous message. 3 should be good since we don't expect massive
52 * message reorderings with UDP.
53 */
54#define UDP_MAX_MESSAGES_IN_DEFRAG 3
55
56/**
57 * We keep a defragmentation queue per sender address. How many
58 * sender addresses do we support at the same time? Memory consumption
59 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
60 * value. (So 128 corresponds to 12 MB and should suffice for
61 * connecting to roughly 128 peers via UDP).
62 */
63#define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
64
65
66
67/**
68 * Closure for 'append_port'.
69 */
70struct PrettyPrinterContext
71{
72 /**
73 * Function to call with the result.
74 */
75 GNUNET_TRANSPORT_AddressStringCallback asc;
76
77 /**
78 * Clsoure for 'asc'.
79 */
80 void *asc_cls;
81
82 /**
83 * Port to add after the IP address.
84 */
85 uint16_t port;
86};
87
88struct Session
89{
90 /**
91 * Which peer is this session for?
92 */
93 struct GNUNET_PeerIdentity target;
94
95 /**
96 * Address of the other peer
97 */
98 const struct sockaddr *sock_addr;
99
100 size_t addrlen;
101
102 /**
103 * Desired delay for next sending we send to other peer
104 */
105 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
106
107 /**
108 * Desired delay for next sending we received from other peer
109 */
110 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
111
112 /**
113 * expected delay for ACKs
114 */
115 struct GNUNET_TIME_Relative last_expected_delay;
116
117
118 struct GNUNET_ATS_Information ats;
119
120 struct FragmentationContext * frag_ctx;
121};
122
123
124struct SessionCompareContext
125{
126 struct Session *res;
127 const struct GNUNET_HELLO_Address *addr;
128};
129
130
131/**
132 * Closure for 'process_inbound_tokenized_messages'
133 */
134struct SourceInformation
135{
136 /**
137 * Sender identity.
138 */
139 struct GNUNET_PeerIdentity sender;
140
141 /**
142 * Source address.
143 */
144 const void *arg;
145
146 /**
147 * Number of bytes in source address.
148 */
149 size_t args;
150
151 struct Session *session;
152};
153
154
155/**
156 * Closure for 'find_receive_context'.
157 */
158struct FindReceiveContext
159{
160 /**
161 * Where to store the result.
162 */
163 struct DefragContext *rc;
164
165 /**
166 * Address to find.
167 */
168 const struct sockaddr *addr;
169
170 /**
171 * Number of bytes in 'addr'.
172 */
173 socklen_t addr_len;
174
175 struct Session *session;
176};
177
178
179
180/**
181 * Data structure to track defragmentation contexts based
182 * on the source of the UDP traffic.
183 */
184struct DefragContext
185{
186
187 /**
188 * Defragmentation context.
189 */
190 struct GNUNET_DEFRAGMENT_Context *defrag;
191
192 /**
193 * Source address this receive context is for (allocated at the
194 * end of the struct).
195 */
196 const struct sockaddr *src_addr;
197
198 /**
199 * Reference to master plugin struct.
200 */
201 struct Plugin *plugin;
202
203 /**
204 * Node in the defrag heap.
205 */
206 struct GNUNET_CONTAINER_HeapNode *hnode;
207
208 /**
209 * Length of 'src_addr'
210 */
211 size_t addr_len;
212};
213
214
215
216/**
217 * Closure for 'process_inbound_tokenized_messages'
218 */
219struct FragmentationContext
220{
221 struct FragmentationContext * next;
222 struct FragmentationContext * prev;
223
224 struct Plugin * plugin;
225 struct GNUNET_FRAGMENT_Context * frag;
226 struct Session * session;
227
228 struct GNUNET_TIME_Absolute timeout;
229
230
231 /**
232 * Function to call upon completion of the transmission.
233 */
234 GNUNET_TRANSPORT_TransmitContinuation cont;
235
236 /**
237 * Closure for 'cont'.
238 */
239 void *cont_cls;
240
241 size_t bytes_to_send;
242};
243
244
245struct UDPMessageWrapper
246{
247 struct Session *session;
248 struct UDPMessageWrapper *prev;
249 struct UDPMessageWrapper *next;
250 char *udp;
251 size_t msg_size;
252
253 struct GNUNET_TIME_Absolute timeout;
254
255 /**
256 * Function to call upon completion of the transmission.
257 */
258 GNUNET_TRANSPORT_TransmitContinuation cont;
259
260 /**
261 * Closure for 'cont'.
262 */
263 void *cont_cls;
264
265 struct FragmentationContext *frag_ctx;
266
267};
268
269
270/**
271 * UDP ACK Message-Packet header (after defragmentation).
272 */
273struct UDP_ACK_Message
274{
275 /**
276 * Message header.
277 */
278 struct GNUNET_MessageHeader header;
279
280 /**
281 * Desired delay for flow control
282 */
283 uint32_t delay;
284
285 /**
286 * What is the identity of the sender
287 */
288 struct GNUNET_PeerIdentity sender;
289
290};
291
292
293
294/**
295 * Function called for a quick conversion of the binary address to
296 * a numeric address. Note that the caller must not free the
297 * address and that the next call to this function is allowed
298 * to override the address again.
299 *
300 * @param cls closure
301 * @param addr binary address
302 * @param addrlen length of the address
303 * @return string representing the same address
304 */
305const char *
306udp_address_to_string (void *cls, const void *addr, size_t addrlen)
307{
308 static char rbuf[INET6_ADDRSTRLEN + 10];
309 char buf[INET6_ADDRSTRLEN];
310 const void *sb;
311 struct in_addr a4;
312 struct in6_addr a6;
313 const struct IPv4UdpAddress *t4;
314 const struct IPv6UdpAddress *t6;
315 int af;
316 uint16_t port;
317
318 if (addrlen == sizeof (struct IPv6UdpAddress))
319 {
320 t6 = addr;
321 af = AF_INET6;
322 port = ntohs (t6->u6_port);
323 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
324 sb = &a6;
325 }
326 else if (addrlen == sizeof (struct IPv4UdpAddress))
327 {
328 t4 = addr;
329 af = AF_INET;
330 port = ntohs (t4->u4_port);
331 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
332 sb = &a4;
333 }
334 else
335 {
336 GNUNET_break_op (0);
337 return NULL;
338 }
339 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
340 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
341 buf, port);
342 return rbuf;
343}
344
345
346/**
347 * Append our port and forward the result.
348 *
349 * @param cls a 'struct PrettyPrinterContext'
350 * @param hostname result from DNS resolver
351 */
352static void
353append_port (void *cls, const char *hostname)
354{
355 struct PrettyPrinterContext *ppc = cls;
356 char *ret;
357
358 if (hostname == NULL)
359 {
360 ppc->asc (ppc->asc_cls, NULL);
361 GNUNET_free (ppc);
362 return;
363 }
364 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
365 ppc->asc (ppc->asc_cls, ret);
366 GNUNET_free (ret);
367}
368
369
370/**
371 * Convert the transports address to a nice, human-readable
372 * format.
373 *
374 * @param cls closure
375 * @param type name of the transport that generated the address
376 * @param addr one of the addresses of the host, NULL for the last address
377 * the specific address format depends on the transport
378 * @param addrlen length of the address
379 * @param numeric should (IP) addresses be displayed in numeric form?
380 * @param timeout after how long should we give up?
381 * @param asc function to call on each string
382 * @param asc_cls closure for asc
383 */
384static void
385udp_plugin_address_pretty_printer (void *cls, const char *type,
386 const void *addr, size_t addrlen,
387 int numeric,
388 struct GNUNET_TIME_Relative timeout,
389 GNUNET_TRANSPORT_AddressStringCallback asc,
390 void *asc_cls)
391{
392 struct PrettyPrinterContext *ppc;
393 const void *sb;
394 size_t sbs;
395 struct sockaddr_in a4;
396 struct sockaddr_in6 a6;
397 const struct IPv4UdpAddress *u4;
398 const struct IPv6UdpAddress *u6;
399 uint16_t port;
400
401 if (addrlen == sizeof (struct IPv6UdpAddress))
402 {
403 u6 = addr;
404 memset (&a6, 0, sizeof (a6));
405 a6.sin6_family = AF_INET6;
406#if HAVE_SOCKADDR_IN_SIN_LEN
407 a6.sin6_len = sizeof (a6);
408#endif
409 a6.sin6_port = u6->u6_port;
410 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
411 port = ntohs (u6->u6_port);
412 sb = &a6;
413 sbs = sizeof (a6);
414 }
415 else if (addrlen == sizeof (struct IPv4UdpAddress))
416 {
417 u4 = addr;
418 memset (&a4, 0, sizeof (a4));
419 a4.sin_family = AF_INET;
420#if HAVE_SOCKADDR_IN_SIN_LEN
421 a4.sin_len = sizeof (a4);
422#endif
423 a4.sin_port = u4->u4_port;
424 a4.sin_addr.s_addr = u4->ipv4_addr;
425 port = ntohs (u4->u4_port);
426 sb = &a4;
427 sbs = sizeof (a4);
428 }
429 else
430 {
431 /* invalid address */
432 GNUNET_break_op (0);
433 asc (asc_cls, NULL);
434 return;
435 }
436 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
437 ppc->asc = asc;
438 ppc->asc_cls = asc_cls;
439 ppc->port = port;
440 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
441}
442
443
444/**
445 * Check if the given port is plausible (must be either our listen
446 * port or our advertised port). If it is neither, we return
447 * GNUNET_SYSERR.
448 *
449 * @param plugin global variables
450 * @param in_port port number to check
451 * @return GNUNET_OK if port is either open_port or adv_port
452 */
453static int
454check_port (struct Plugin *plugin, uint16_t in_port)
455{
456 if ((in_port == plugin->port) || (in_port == plugin->aport))
457 return GNUNET_OK;
458 return GNUNET_SYSERR;
459}
460
461
462
463/**
464 * Function that will be called to check if a binary address for this
465 * plugin is well-formed and corresponds to an address for THIS peer
466 * (as per our configuration). Naturally, if absolutely necessary,
467 * plugins can be a bit conservative in their answer, but in general
468 * plugins should make sure that the address does not redirect
469 * traffic to a 3rd party that might try to man-in-the-middle our
470 * traffic.
471 *
472 * @param cls closure, should be our handle to the Plugin
473 * @param addr pointer to the address
474 * @param addrlen length of addr
475 * @return GNUNET_OK if this is a plausible address for this peer
476 * and transport, GNUNET_SYSERR if not
477 *
478 */
479static int
480udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
481{
482 struct Plugin *plugin = cls;
483 struct IPv4UdpAddress *v4;
484 struct IPv6UdpAddress *v6;
485
486 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
487 (addrlen != sizeof (struct IPv6UdpAddress)))
488 {
489 GNUNET_break_op (0);
490 return GNUNET_SYSERR;
491 }
492 if (addrlen == sizeof (struct IPv4UdpAddress))
493 {
494 v4 = (struct IPv4UdpAddress *) addr;
495 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
496 return GNUNET_SYSERR;
497 if (GNUNET_OK !=
498 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
499 sizeof (struct in_addr)))
500 return GNUNET_SYSERR;
501 }
502 else
503 {
504 v6 = (struct IPv6UdpAddress *) addr;
505 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
506 {
507 GNUNET_break_op (0);
508 return GNUNET_SYSERR;
509 }
510 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
511 return GNUNET_SYSERR;
512 if (GNUNET_OK !=
513 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
514 sizeof (struct in6_addr)))
515 return GNUNET_SYSERR;
516 }
517 return GNUNET_OK;
518}
519
520
521/**
522 * Destroy a session, plugin is being unloaded.
523 *
524 * @param cls unused
525 * @param key hash of public key of target peer
526 * @param value a 'struct PeerSession*' to clean up
527 * @return GNUNET_OK (continue to iterate)
528 */
529static int
530disconnect_and_free_it (void *cls, const GNUNET_HashCode * key, void *value)
531{
532 struct Plugin *plugin = cls;
533 struct Session *s = value;
534 struct UDPMessageWrapper *udpw;
535
536#if DEBUG_UDP
537 LOG (GNUNET_ERROR_TYPE_DEBUG,
538 "Session %p to peer `%s' address ended \n",
539 s,
540 GNUNET_i2s (&s->target),
541 GNUNET_a2s (s->sock_addr, s->addrlen));
542#endif
543 plugin->env->session_end (plugin->env->cls, &s->target, s);
544
545 if (s->frag_ctx != NULL)
546 {
547 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag);
548 GNUNET_free (s->frag_ctx);
549 s->frag_ctx = NULL;
550 }
551
552 udpw = plugin->msg_head;
553 while (udpw != NULL)
554 {
555 if (udpw->session == s)
556 {
557 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
558
559 if (udpw->cont != NULL)
560 udpw->cont (udpw->cont_cls, &s->target, GNUNET_SYSERR);
561 GNUNET_free (udpw);
562 }
563 udpw = plugin->msg_head;
564 }
565
566 GNUNET_assert (GNUNET_YES ==
567 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
568 &s->target.hashPubKey,
569 s));
570
571
572 GNUNET_free (s);
573 return GNUNET_OK;
574}
575
576
577/**
578 * Disconnect from a remote node. Clean up session if we have one for this peer
579 *
580 * @param cls closure for this call (should be handle to Plugin)
581 * @param target the peeridentity of the peer to disconnect
582 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
583 */
584static void
585udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
586{
587 struct Plugin *plugin = cls;
588 GNUNET_assert (plugin != NULL);
589
590 GNUNET_assert (target != NULL);
591#if DEBUG_UDP
592 LOG (GNUNET_ERROR_TYPE_DEBUG,
593 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
594#endif
595 /* Clean up sessions */
596 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
597
598 LOG (GNUNET_ERROR_TYPE_DEBUG,
599 "FREEED SESSIONS from peer `%s'\n", GNUNET_i2s (target));
600}
601
602static struct Session *
603create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
604 const void *addr, size_t addrlen,
605 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
606{
607 struct Session *s;
608 const struct IPv4UdpAddress *t4;
609 const struct IPv6UdpAddress *t6;
610 struct sockaddr_in *v4;
611 struct sockaddr_in6 *v6;
612 size_t len;
613
614 switch (addrlen)
615 {
616 case sizeof (struct IPv4UdpAddress):
617 if (NULL == plugin->sockv4)
618 {
619 return NULL;
620 }
621 t4 = addr;
622 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
623 len = sizeof (struct sockaddr_in);
624 v4 = (struct sockaddr_in *) &s[1];
625 v4->sin_family = AF_INET;
626#if HAVE_SOCKADDR_IN_SIN_LEN
627 v4->sin_len = sizeof (struct sockaddr_in);
628#endif
629 v4->sin_port = t4->u4_port;
630 v4->sin_addr.s_addr = t4->ipv4_addr;
631 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
632 break;
633 case sizeof (struct IPv6UdpAddress):
634 if (NULL == plugin->sockv6)
635 {
636 return NULL;
637 }
638 t6 = addr;
639 s =
640 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
641 len = sizeof (struct sockaddr_in6);
642 v6 = (struct sockaddr_in6 *) &s[1];
643 v6->sin6_family = AF_INET6;
644#if HAVE_SOCKADDR_IN_SIN_LEN
645 v6->sin6_len = sizeof (struct sockaddr_in6);
646#endif
647 v6->sin6_port = t6->u6_port;
648 v6->sin6_addr = t6->ipv6_addr;
649 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
650 break;
651 default:
652 /* Must have a valid address to send to */
653 GNUNET_break_op (0);
654 return NULL;
655 }
656
657 s->addrlen = len;
658 s->target = *target;
659 s->sock_addr = (const struct sockaddr *) &s[1];
660 s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero();
661 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero();
662 s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
663
664 return s;
665}
666
667static int session_cmp_it (void *cls,
668 const GNUNET_HashCode * key,
669 void *value)
670{
671 struct SessionCompareContext * cctx = cls;
672 const struct GNUNET_HELLO_Address *address = cctx->addr;
673 struct Session *s = value;
674
675 socklen_t s_addrlen = s->addrlen;
676
677#if VERBOSE_UDP
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
679 udp_address_to_string (NULL, (void *) address->address, address->address_length),
680 GNUNET_a2s (s->sock_addr, s->addrlen));
681#endif
682
683 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
684 (s_addrlen == sizeof (struct sockaddr_in)))
685 {
686 struct IPv4UdpAddress * u4 = NULL;
687 u4 = (struct IPv4UdpAddress *) address->address;
688 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
689 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
690 (u4->u4_port == s4->sin_port))
691 {
692 cctx->res = s;
693 return GNUNET_NO;
694 }
695
696 }
697 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
698 (s_addrlen == sizeof (struct sockaddr_in6)))
699 {
700 struct IPv6UdpAddress * u6 = NULL;
701 u6 = (struct IPv6UdpAddress *) address->address;
702 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
703 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
704 (u6->u6_port == s6->sin6_port))
705 {
706 cctx->res = s;
707 return GNUNET_NO;
708 }
709 }
710
711
712 return GNUNET_YES;
713}
714
715
716/**
717 * Creates a new outbound session the transport service will use to send data to the
718 * peer
719 *
720 * @param cls the plugin
721 * @param address the address
722 * @return the session or NULL of max connections exceeded
723 */
724static struct Session *
725udp_plugin_get_session (void *cls,
726 const struct GNUNET_HELLO_Address *address)
727{
728 struct Session * s = NULL;
729 struct Plugin * plugin = cls;
730
731 GNUNET_assert (plugin != NULL);
732 GNUNET_assert (address != NULL);
733
734 if ((address->address == NULL) ||
735 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
736 (address->address_length != sizeof (struct IPv6UdpAddress))))
737 {
738 GNUNET_break (0);
739 return NULL;
740 }
741
742 /* check if session already exists */
743 struct SessionCompareContext cctx;
744 cctx.addr = address;
745 cctx.res = NULL;
746#if VERBOSE_UDP
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for existing session for peer `%s' `%s' \n", GNUNET_i2s (&address->peer), udp_address_to_string(NULL, address->address, address->address_length));
748#endif
749 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
750 if (cctx.res != NULL)
751 {
752#if VERBOSE_UDP
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
754#endif
755 return cctx.res;
756 }
757
758 /* otherwise create new */
759 s = create_session (plugin,
760 &address->peer,
761 address->address,
762 address->address_length,
763 NULL, NULL);
764#if VERBOSE
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Creating new session %p for peer `%s' address `%s'\n",
767 s,
768 GNUNET_i2s(&address->peer),
769 udp_address_to_string(NULL,address->address,address->address_length));
770#endif
771 GNUNET_assert (GNUNET_OK ==
772 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
773 &s->target.hashPubKey,
774 s,
775 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
776
777 return s;
778}
779
780/**
781 * Function that is called with messages created by the fragmentation
782 * module. In the case of the 'proc' callback of the
783 * GNUNET_FRAGMENT_context_create function, this function must
784 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
785 *
786 * @param cls closure, the 'struct FragmentationContext'
787 * @param msg the message that was created
788 */
789static void
790enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
791{
792 struct FragmentationContext *frag_ctx = cls;
793 struct Plugin *plugin = frag_ctx->plugin;
794 struct UDPMessageWrapper * udpw;
795
796 size_t msg_len = ntohs (msg->size);
797
798#if VERBOSE_UDP
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueuing fragment with %u bytes %u\n", msg_len , sizeof (struct UDPMessageWrapper));
800#endif
801
802 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msg_len);
803 udpw->session = frag_ctx->session;
804 udpw->udp = (char *) &udpw[1];
805
806 udpw->msg_size = msg_len;
807 udpw->cont = frag_ctx->cont;
808 udpw->cont_cls = frag_ctx->cont_cls;
809 udpw->timeout = frag_ctx->timeout;
810 udpw->frag_ctx = frag_ctx;
811 memcpy (udpw->udp, msg, msg_len);
812
813 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, udpw);
814}
815
816
817/**
818 * Function that can be used by the transport service to transmit
819 * a message using the plugin. Note that in the case of a
820 * peer disconnecting, the continuation MUST be called
821 * prior to the disconnect notification itself. This function
822 * will be called with this peer's HELLO message to initiate
823 * a fresh connection to another peer.
824 *
825 * @param cls closure
826 * @param session which session must be used
827 * @param msgbuf the message to transmit
828 * @param msgbuf_size number of bytes in 'msgbuf'
829 * @param priority how important is the message (most plugins will
830 * ignore message priority and just FIFO)
831 * @param to how long to wait at most for the transmission (does not
832 * require plugins to discard the message after the timeout,
833 * just advisory for the desired delay; most plugins will ignore
834 * this as well)
835 * @param cont continuation to call once the message has
836 * been transmitted (or if the transport is ready
837 * for the next transmission call; or if the
838 * peer disconnected...); can be NULL
839 * @param cont_cls closure for cont
840 * @return number of bytes used (on the physical network, with overheads);
841 * -1 on hard errors (i.e. address invalid); 0 is a legal value
842 * and does NOT mean that the message was not transmitted (DV)
843 */
844static ssize_t
845udp_plugin_send (void *cls,
846 struct Session *s,
847 const char *msgbuf, size_t msgbuf_size,
848 unsigned int priority,
849 struct GNUNET_TIME_Relative to,
850 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
851{
852 struct Plugin *plugin = cls;
853 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
854
855 struct UDPMessageWrapper * udpw;
856 struct UDPMessage *udp;
857 char mbuf[mlen];
858 GNUNET_assert (plugin != NULL);
859 GNUNET_assert (s != NULL);
860
861 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
862 {
863 GNUNET_break (0);
864 return GNUNET_SYSERR;
865 }
866
867 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
868 {
869 GNUNET_break (0);
870 return GNUNET_SYSERR;
871 }
872
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "UDP transmits %u-byte message to `%s' using address `%s'\n",
875 msgbuf_size,
876 GNUNET_i2s (&s->target),
877 GNUNET_a2s(s->sock_addr, s->addrlen));
878
879 /* Message */
880 udp = (struct UDPMessage *) mbuf;
881 udp->header.size = htons (mlen);
882 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
883 udp->reserved = htonl (0);
884 udp->sender = *plugin->env->my_identity;
885
886 if (mlen <= UDP_MTU)
887 {
888 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + mlen);
889 udpw->session = s;
890 udpw->udp = (char *) &udpw[1];
891 udpw->msg_size = mlen;
892 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
893 udpw->cont = cont;
894 udpw->cont_cls = cont_cls;
895 udpw->frag_ctx = NULL;
896
897 memcpy (udpw->udp, udp, sizeof (struct UDPMessage));
898 memcpy (&udpw->udp[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
899
900 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, udpw);
901 }
902 else
903 {
904 LOG (GNUNET_ERROR_TYPE_DEBUG,
905 "UDP has to fragment message \n");
906 if (s->frag_ctx != NULL)
907 return GNUNET_SYSERR;
908 memcpy (&udp[1], msgbuf, msgbuf_size);
909 struct FragmentationContext * frag_ctx = GNUNET_malloc(sizeof (struct FragmentationContext));
910
911 frag_ctx->plugin = plugin;
912 frag_ctx->session = s;
913 frag_ctx->cont = cont;
914 frag_ctx->cont_cls = cont_cls;
915 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
916 frag_ctx->bytes_to_send = mlen;
917 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
918 UDP_MTU,
919 &plugin->tracker,
920 s->last_expected_delay,
921 &udp->header,
922 &enqueue_fragment,
923 frag_ctx);
924
925 s->frag_ctx = frag_ctx;
926
927 }
928 return mlen;
929}
930
931static ssize_t udp_plugin_send_wrapper (void *cls,
932 const struct
933 GNUNET_PeerIdentity *
934 target,
935 const char *msgbuf,
936 size_t msgbuf_size,
937 uint32_t priority,
938 struct
939 GNUNET_TIME_Relative
940 timeout,
941 struct Session * session,
942 const void *addr,
943 size_t addrlen,
944 int force_address,
945 GNUNET_TRANSPORT_TransmitContinuation
946 cont, void *cont_cls)
947{
948 int ret;
949 struct Session * s = NULL;
950 struct GNUNET_HELLO_Address * ha = NULL;
951
952 ha = GNUNET_HELLO_address_allocate(target, "", addr,addrlen);
953 GNUNET_assert (ha != NULL);
954 s = udp_plugin_get_session(cls, ha);
955 GNUNET_assert (s != NULL);
956 GNUNET_free (ha);
957 ret = udp_plugin_send (cls, s, msgbuf, msgbuf_size, priority, timeout, cont, cont_cls);
958
959 return ret;
960}
961
962
963/**
964 * Our external IP address/port mapping has changed.
965 *
966 * @param cls closure, the 'struct LocalAddrList'
967 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
968 * the previous (now invalid) one
969 * @param addr either the previous or the new public IP address
970 * @param addrlen actual lenght of the address
971 */
972static void
973udp_nat_port_map_callback (void *cls, int add_remove,
974 const struct sockaddr *addr, socklen_t addrlen)
975{
976 struct Plugin *plugin = cls;
977 struct IPv4UdpAddress u4;
978 struct IPv6UdpAddress u6;
979 void *arg;
980 size_t args;
981
982 /* convert 'addr' to our internal format */
983 switch (addr->sa_family)
984 {
985 case AF_INET:
986 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
987 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
988 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
989 arg = &u4;
990 args = sizeof (u4);
991 break;
992 case AF_INET6:
993 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
994 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
995 sizeof (struct in6_addr));
996 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
997 arg = &u6;
998 args = sizeof (u6);
999 break;
1000 default:
1001 GNUNET_break (0);
1002 return;
1003 }
1004 /* modify our published address list */
1005 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1006}
1007
1008
1009
1010/**
1011 * Message tokenizer has broken up an incomming message. Pass it on
1012 * to the service.
1013 *
1014 * @param cls the 'struct Plugin'
1015 * @param client the 'struct SourceInformation'
1016 * @param hdr the actual message
1017 */
1018static void
1019process_inbound_tokenized_messages (void *cls, void *client,
1020 const struct GNUNET_MessageHeader *hdr)
1021{
1022 struct Plugin *plugin = cls;
1023 struct SourceInformation *si = client;
1024 struct GNUNET_ATS_Information ats[2];
1025 struct GNUNET_TIME_Relative delay;
1026
1027 GNUNET_assert (si->session != NULL);
1028 /* setup ATS */
1029 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1030 ats[0].value = htonl (1);
1031 ats[1] = si->session->ats;
1032 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1033
1034 delay = plugin->env->receive (plugin->env->cls,
1035 &si->sender,
1036 hdr,
1037 (const struct GNUNET_ATS_Information *) &ats, 2,
1038 NULL,
1039 si->arg,
1040 si->args);
1041 si->session->flow_delay_for_other_peer = delay;
1042}
1043
1044
1045/**
1046 * We've received a UDP Message. Process it (pass contents to main service).
1047 *
1048 * @param plugin plugin context
1049 * @param msg the message
1050 * @param sender_addr sender address
1051 * @param sender_addr_len number of bytes in sender_addr
1052 */
1053static void
1054process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1055 const struct sockaddr *sender_addr,
1056 socklen_t sender_addr_len)
1057{
1058 struct SourceInformation si;
1059 struct Session * s = NULL;
1060 struct IPv4UdpAddress u4;
1061 struct IPv6UdpAddress u6;
1062 struct GNUNET_ATS_Information ats;
1063 const void *arg;
1064 size_t args;
1065
1066 if (0 != ntohl (msg->reserved))
1067 {
1068 GNUNET_break_op (0);
1069 return;
1070 }
1071 if (ntohs (msg->header.size) <
1072 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1073 {
1074 GNUNET_break_op (0);
1075 return;
1076 }
1077
1078 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1079 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1080 /* convert address */
1081 switch (sender_addr->sa_family)
1082 {
1083 case AF_INET:
1084 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1085 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1086 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1087 arg = &u4;
1088 args = sizeof (u4);
1089 break;
1090 case AF_INET6:
1091 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1092 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1093 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1094 arg = &u6;
1095 args = sizeof (u6);
1096 break;
1097 default:
1098 GNUNET_break (0);
1099 return;
1100 }
1101#if DEBUG_UDP
1102 LOG (GNUNET_ERROR_TYPE_DEBUG,
1103 "Received message with %u bytes from peer `%s' at `%s'\n",
1104 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1105 GNUNET_a2s (sender_addr, sender_addr_len));
1106#endif
1107
1108 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1109 s = udp_plugin_get_session(plugin, address);
1110 GNUNET_free (address);
1111
1112 /* iterate over all embedded messages */
1113 si.session = s;
1114 si.sender = msg->sender;
1115 si.arg = arg;
1116 si.args = args;
1117
1118 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1119 ntohs (msg->header.size) -
1120 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1121}
1122
1123
1124/**
1125 * Scan the heap for a receive context with the given address.
1126 *
1127 * @param cls the 'struct FindReceiveContext'
1128 * @param node internal node of the heap
1129 * @param element value stored at the node (a 'struct ReceiveContext')
1130 * @param cost cost associated with the node
1131 * @return GNUNET_YES if we should continue to iterate,
1132 * GNUNET_NO if not.
1133 */
1134static int
1135find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1136 void *element, GNUNET_CONTAINER_HeapCostType cost)
1137{
1138 struct FindReceiveContext *frc = cls;
1139 struct DefragContext *e = element;
1140
1141 if ((frc->addr_len == e->addr_len) &&
1142 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1143 {
1144 frc->rc = e;
1145 return GNUNET_NO;
1146 }
1147 return GNUNET_YES;
1148}
1149
1150
1151/**
1152 * Process a defragmented message.
1153 *
1154 * @param cls the 'struct ReceiveContext'
1155 * @param msg the message
1156 */
1157static void
1158fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1159{
1160 struct DefragContext *rc = cls;
1161
1162 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1163 {
1164 GNUNET_break (0);
1165 return;
1166 }
1167 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1168 {
1169 GNUNET_break (0);
1170 return;
1171 }
1172 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1173 rc->src_addr, rc->addr_len);
1174}
1175
1176struct LookupContext
1177{
1178 const struct sockaddr * addr;
1179 size_t addrlen;
1180
1181 struct Session *res;
1182};
1183
1184static int
1185lookup_session_by_addr_it (void *cls, const GNUNET_HashCode * key, void *value)
1186{
1187 struct LookupContext *l_ctx = cls;
1188 struct Session * s = value;
1189
1190 if ((s->addrlen == l_ctx->addrlen) &&
1191 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1192 {
1193 l_ctx->res = s;
1194 return GNUNET_NO;
1195 }
1196 return GNUNET_YES;
1197}
1198
1199/**
1200 * Transmit an acknowledgement.
1201 *
1202 * @param cls the 'struct ReceiveContext'
1203 * @param id message ID (unused)
1204 * @param msg ack to transmit
1205 */
1206static void
1207ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1208{
1209 struct DefragContext *rc = cls;
1210
1211 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1212 struct UDP_ACK_Message *udp_ack;
1213 uint32_t delay = 0;
1214 struct UDPMessageWrapper *udpw;
1215 struct Session *s;
1216
1217 struct LookupContext l_ctx;
1218 l_ctx.addr = rc->src_addr;
1219 l_ctx.addrlen = rc->addr_len;
1220 l_ctx.res = NULL;
1221 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1222 &lookup_session_by_addr_it,
1223 &l_ctx);
1224 s = l_ctx.res;
1225
1226 GNUNET_assert (s != NULL);
1227
1228 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1229 delay = s->flow_delay_for_other_peer.rel_value;
1230
1231#if DEBUG_UDP
1232 LOG (GNUNET_ERROR_TYPE_DEBUG,
1233 "Sending ACK to `%s' including delay of %u ms\n",
1234 GNUNET_a2s (rc->src_addr,
1235 (rc->src_addr->sa_family ==
1236 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1237 sockaddr_in6)),
1238 delay);
1239#endif
1240 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msize);
1241 udpw->cont = NULL;
1242 udpw->cont_cls = NULL;
1243 udpw->frag_ctx = NULL;
1244 udpw->msg_size = msize;
1245 udpw->session = s;
1246 udpw->timeout = GNUNET_TIME_absolute_get_forever();
1247 udpw->udp = (char *)&udpw[1];
1248
1249 udp_ack = (struct UDP_ACK_Message *) udpw->udp;
1250 udp_ack->header.size = htons ((uint16_t) msize);
1251 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1252 udp_ack->delay = htonl (delay);
1253 udp_ack->sender = *rc->plugin->env->my_identity;
1254 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1255
1256 GNUNET_CONTAINER_DLL_insert(rc->plugin->msg_head, rc->plugin->msg_tail, udpw);
1257}
1258
1259
1260static void read_process_msg (struct Plugin *plugin,
1261 const struct GNUNET_MessageHeader *msg,
1262 char *addr,
1263 socklen_t fromlen)
1264{
1265 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1266 {
1267 GNUNET_break_op (0);
1268 return;
1269 }
1270 process_udp_message (plugin, (const struct UDPMessage *) msg,
1271 (const struct sockaddr *) addr, fromlen);
1272 return;
1273}
1274
1275static void read_process_ack (struct Plugin *plugin,
1276 const struct GNUNET_MessageHeader *msg,
1277 char *addr,
1278 socklen_t fromlen)
1279{
1280 const struct GNUNET_MessageHeader *ack;
1281 const struct UDP_ACK_Message *udp_ack;
1282 struct LookupContext l_ctx;
1283 struct Session *s = NULL;
1284 struct GNUNET_TIME_Relative flow_delay;
1285
1286 if (ntohs (msg->size) <
1287 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1288 {
1289 GNUNET_break_op (0);
1290 return;
1291 }
1292
1293 udp_ack = (const struct UDP_ACK_Message *) msg;
1294
1295 l_ctx.addr = (const struct sockaddr *) addr;
1296 l_ctx.addrlen = fromlen;
1297 l_ctx.res = NULL;
1298 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1299 &lookup_session_by_addr_it,
1300 &l_ctx);
1301 s = l_ctx.res;
1302
1303 if ((s == NULL) || (s->frag_ctx == NULL))
1304 return;
1305
1306 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1307 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1308 flow_delay.rel_value);
1309 s->flow_delay_from_other_peer =
1310 GNUNET_TIME_relative_to_absolute (flow_delay);
1311
1312 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1313 if (ntohs (ack->size) !=
1314 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1315 {
1316 GNUNET_break_op (0);
1317 return;
1318 }
1319
1320 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
1321 {
1322#if DEBUG_UDP
1323 LOG (GNUNET_ERROR_TYPE_DEBUG,
1324 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1325 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1326 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1327#endif
1328 return;
1329 }
1330
1331#if DEBUG_UDP
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "FULL MESSAGE ACKed\n",
1334 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1335 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1336#endif
1337 s->last_expected_delay = GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag);
1338
1339 struct UDPMessageWrapper * udpw = plugin->msg_head;
1340 while (udpw!= NULL)
1341 {
1342 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1343 {
1344 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
1345 GNUNET_free (udpw);
1346 }
1347 udpw = udpw->next;
1348 }
1349
1350 if (s->frag_ctx->cont != NULL)
1351 s->frag_ctx->cont
1352 (s->frag_ctx->cont_cls, &udp_ack->sender, GNUNET_OK);
1353 GNUNET_free (s->frag_ctx);
1354 s->frag_ctx = NULL;
1355 return;
1356}
1357
1358static void read_process_fragment (struct Plugin *plugin,
1359 const struct GNUNET_MessageHeader *msg,
1360 char *addr,
1361 socklen_t fromlen)
1362{
1363 struct DefragContext *d_ctx;
1364 struct GNUNET_TIME_Absolute now;
1365 struct FindReceiveContext frc;
1366
1367
1368 frc.rc = NULL;
1369 frc.addr = (const struct sockaddr *) addr;
1370 frc.addr_len = fromlen;
1371
1372#if DEBUG_UDP
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1374 (unsigned int) ntohs (msg->size),
1375 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1376#endif
1377
1378 /* Lookup existing receive context for this address */
1379 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1380 &find_receive_context,
1381 &frc);
1382 now = GNUNET_TIME_absolute_get ();
1383 d_ctx = frc.rc;
1384
1385 if (d_ctx == NULL)
1386 {
1387 /* Create a new defragmentation context */
1388 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
1389 memcpy (&d_ctx[1], addr, fromlen);
1390 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
1391 d_ctx->addr_len = fromlen;
1392 d_ctx->plugin = plugin;
1393 d_ctx->defrag =
1394 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1395 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
1396 &fragment_msg_proc, &ack_proc);
1397 d_ctx->hnode =
1398 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
1399 (GNUNET_CONTAINER_HeapCostType)
1400 now.abs_value);
1401#if DEBUG_UDP
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, "Created new defragmentation context for %u-byte fragment from `%s'\n",
1403 (unsigned int) ntohs (msg->size),
1404 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1405#endif
1406 }
1407 else
1408 {
1409#if DEBUG_UDP
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing defragmentation context for %u-byte fragment from `%s'\n",
1411 (unsigned int) ntohs (msg->size),
1412 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1413#endif
1414 }
1415
1416 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
1417 {
1418 /* keep this 'rc' from expiring */
1419 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
1420 (GNUNET_CONTAINER_HeapCostType)
1421 now.abs_value);
1422 }
1423 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
1424 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1425 {
1426 /* remove 'rc' that was inactive the longest */
1427 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
1428 GNUNET_assert (NULL != d_ctx);
1429 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1430 GNUNET_free (d_ctx);
1431 }
1432}
1433
1434/**
1435 * Read and process a message from the given socket.
1436 *
1437 * @param plugin the overall plugin
1438 * @param rsock socket to read from
1439 */
1440static void
1441udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1442{
1443 socklen_t fromlen;
1444 char addr[32];
1445 char buf[65536];
1446 ssize_t size;
1447 const struct GNUNET_MessageHeader *msg;
1448
1449 fromlen = sizeof (addr);
1450 memset (&addr, 0, sizeof (addr));
1451 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1452 (struct sockaddr *) &addr, &fromlen);
1453
1454 if (size < sizeof (struct GNUNET_MessageHeader))
1455 {
1456 GNUNET_break_op (0);
1457 return;
1458 }
1459 msg = (const struct GNUNET_MessageHeader *) buf;
1460
1461 LOG (GNUNET_ERROR_TYPE_DEBUG,
1462 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
1463 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1464
1465 if (size != ntohs (msg->size))
1466 {
1467 GNUNET_break_op (0);
1468 return;
1469 }
1470
1471 switch (ntohs (msg->type))
1472 {
1473 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1474 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
1475 return;
1476
1477 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1478 read_process_msg (plugin, msg, addr, fromlen);
1479 return;
1480
1481 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1482 read_process_ack (plugin, msg, addr, fromlen);;
1483 return;
1484
1485 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1486 read_process_fragment (plugin, msg, addr, fromlen);
1487 return;
1488
1489 default:
1490 GNUNET_break_op (0);
1491 return;
1492 }
1493}
1494
1495size_t
1496udp_select_send (struct Plugin *plugin)
1497{
1498 ssize_t sent;
1499 size_t slen;
1500 struct GNUNET_TIME_Absolute max;
1501 struct GNUNET_TIME_Absolute ;
1502
1503 struct UDPMessageWrapper *udpw = plugin->msg_head;
1504 const struct sockaddr * sa = udpw->session->sock_addr;
1505
1506 max = GNUNET_TIME_absolute_max(udpw->timeout, GNUNET_TIME_absolute_get());
1507
1508 while (udpw != NULL)
1509 {
1510 if (max.abs_value != udpw->timeout.abs_value)
1511 {
1512 /* Message timed out */
1513
1514 if (udpw->cont != NULL)
1515 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
1516 if (udpw->frag_ctx != NULL)
1517 {
1518#if DEBUG_UDP
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fragmented message for peer `%s' with size %u timed out\n",
1520 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->bytes_to_send);
1521#endif
1522 udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy(udpw->frag_ctx->frag);
1523 GNUNET_free (udpw->frag_ctx);
1524 udpw->session->frag_ctx = NULL;
1525 }
1526 else
1527 {
1528#if DEBUG_UDP
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' with size %u timed out\n",
1530 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1531#endif
1532 }
1533
1534 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
1535 GNUNET_free (udpw);
1536 udpw = plugin->msg_head;
1537 }
1538 else
1539 {
1540 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
1541 if (delta.rel_value == 0)
1542 {
1543 /* this message is not delayed */
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is not delayed \n",
1545 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1546 break;
1547 }
1548 else
1549 {
1550 /* this message is delayed, try next */
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is delayed for %llu \n",
1552 GNUNET_i2s(&udpw->session->target), udpw->msg_size,
1553 delta);
1554 udpw = udpw->next;
1555 }
1556 }
1557
1558 }
1559
1560 if (udpw == NULL)
1561 {
1562 /* No message left */
1563 return 0;
1564 }
1565
1566 switch (sa->sa_family)
1567 {
1568 case AF_INET:
1569 if (NULL == plugin->sockv4)
1570 return 0;
1571 sent =
1572 GNUNET_NETWORK_socket_sendto (plugin->sockv4, udpw->udp, udpw->msg_size,
1573 sa, slen = sizeof (struct sockaddr_in));
1574 break;
1575 case AF_INET6:
1576 if (NULL == plugin->sockv6)
1577 return 0;
1578 sent =
1579 GNUNET_NETWORK_socket_sendto (plugin->sockv6, udpw->udp, udpw->msg_size,
1580 sa, slen = sizeof (struct sockaddr_in6));
1581 break;
1582 default:
1583 GNUNET_break (0);
1584 return 0;
1585 }
1586 if (GNUNET_SYSERR == sent)
1587 {
1588 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1589 LOG (GNUNET_ERROR_TYPE_DEBUG,
1590 "UDP transmitted %u-byte message to %s (%d: %s)\n",
1591 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1592 (sent < 0) ? STRERROR (errno) : "ok");
1593 if (udpw->cont != NULL)
1594 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
1595 }
1596 LOG (GNUNET_ERROR_TYPE_DEBUG,
1597 "UDP transmitted %u-byte message to %s (%d: %s)\n",
1598 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1599 (sent < 0) ? STRERROR (errno) : "ok");
1600
1601 /* This was just a message fragment */
1602 if (udpw->frag_ctx != NULL)
1603 {
1604 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1605 }
1606 /* This was a complete message*/
1607 else
1608 {
1609 if (udpw->cont != NULL)
1610 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_OK);
1611 }
1612
1613 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
1614 GNUNET_free (udpw);
1615
1616 return sent;
1617}
1618
1619/**
1620 * We have been notified that our readset has something to read. We don't
1621 * know which socket needs to be read, so we have to check each one
1622 * Then reschedule this function to be called again once more is available.
1623 *
1624 * @param cls the plugin handle
1625 * @param tc the scheduling context (for rescheduling this function again)
1626 */
1627static void
1628udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1629{
1630 struct Plugin *plugin = cls;
1631
1632 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1633 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1634 return;
1635
1636 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1637 {
1638 if ((NULL != plugin->sockv4) &&
1639 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1640 udp_select_read (plugin, plugin->sockv4);
1641 if ((NULL != plugin->sockv6) &&
1642 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1643 udp_select_read (plugin, plugin->sockv6);
1644 }
1645
1646 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1647 {
1648 if (plugin->msg_head != NULL)
1649 udp_select_send (plugin);
1650 }
1651
1652 plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1653 GNUNET_SCHEDULER_NO_TASK,
1654 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1655 plugin->ws, &udp_plugin_select, plugin);
1656
1657}
1658
1659
1660static int
1661setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
1662{
1663 int tries;
1664 int sockets_created = 0;
1665 struct sockaddr *serverAddr;
1666 struct sockaddr *addrs[2];
1667 socklen_t addrlens[2];
1668 socklen_t addrlen;
1669
1670 /* Create IPv6 socket */
1671 if (plugin->enable_ipv6 == GNUNET_YES)
1672 {
1673 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1674 if (NULL == plugin->sockv6)
1675 {
1676 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1677 }
1678 else
1679 {
1680#if HAVE_SOCKADDR_IN_SIN_LEN
1681 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1682#endif
1683 serverAddrv6->sin6_family = AF_INET6;
1684 serverAddrv6->sin6_addr = in6addr_any;
1685 serverAddrv6->sin6_port = htons (plugin->port);
1686 addrlen = sizeof (struct sockaddr_in6);
1687 serverAddr = (struct sockaddr *) serverAddrv6;
1688#if DEBUG_UDP
1689 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1690 ntohs (serverAddrv6->sin6_port));
1691#endif
1692 tries = 0;
1693 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1694 GNUNET_OK)
1695 {
1696 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1697#if DEBUG_UDP
1698 LOG (GNUNET_ERROR_TYPE_DEBUG,
1699 "IPv6 Binding failed, trying new port %d\n",
1700 ntohs (serverAddrv6->sin6_port));
1701#endif
1702 tries++;
1703 if (tries > 10)
1704 {
1705 GNUNET_NETWORK_socket_close (plugin->sockv6);
1706 plugin->sockv6 = NULL;
1707 break;
1708 }
1709 }
1710 if (plugin->sockv6 != NULL)
1711 {
1712#if DEBUG_UDP
1713 LOG (GNUNET_ERROR_TYPE_DEBUG,
1714 "IPv6 socket created on port %d\n",
1715 ntohs (serverAddrv6->sin6_port));
1716#endif
1717 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
1718 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
1719 sockets_created++;
1720 }
1721 }
1722 }
1723
1724 /* Create IPv4 socket */
1725 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1726 if (NULL == plugin->sockv4)
1727 {
1728 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1729 }
1730 else
1731 {
1732#if HAVE_SOCKADDR_IN_SIN_LEN
1733 serverAddrv4.sin_len = sizeof (serverAddrv4);
1734#endif
1735 serverAddrv4->sin_family = AF_INET;
1736 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
1737 serverAddrv4->sin_port = htons (plugin->port);
1738 addrlen = sizeof (struct sockaddr_in);
1739 serverAddr = (struct sockaddr *) serverAddrv4;
1740
1741#if DEBUG_UDP
1742 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1743 ntohs (serverAddrv4->sin_port));
1744#endif
1745 tries = 0;
1746 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1747 GNUNET_OK)
1748 {
1749 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1750#if DEBUG_UDP
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
1752 ntohs (serverAddrv4->sin_port));
1753#endif
1754 tries++;
1755 if (tries > 10)
1756 {
1757 GNUNET_NETWORK_socket_close (plugin->sockv4);
1758 plugin->sockv4 = NULL;
1759 break;
1760 }
1761 }
1762 if (plugin->sockv4 != NULL)
1763 {
1764 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
1765 addrlens[sockets_created] = sizeof (struct sockaddr_in);
1766 sockets_created++;
1767 }
1768 }
1769
1770 /* Create file descriptors */
1771 plugin->rs = GNUNET_NETWORK_fdset_create ();
1772 plugin->ws = GNUNET_NETWORK_fdset_create ();
1773 GNUNET_NETWORK_fdset_zero (plugin->rs);
1774 GNUNET_NETWORK_fdset_zero (plugin->ws);
1775 if (NULL != plugin->sockv4)
1776 {
1777 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1778 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv4);
1779 }
1780 if (NULL != plugin->sockv6)
1781 {
1782 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1783 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv6);
1784 }
1785
1786 if (sockets_created == 0)
1787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1788
1789 plugin->select_task =
1790 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1791 GNUNET_SCHEDULER_NO_TASK,
1792 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1793 plugin->ws, &udp_plugin_select, plugin);
1794
1795 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
1796 GNUNET_NO, plugin->port,
1797 sockets_created,
1798 (const struct sockaddr **) addrs, addrlens,
1799 &udp_nat_port_map_callback, NULL, plugin);
1800
1801 return sockets_created;
1802}
1803
1804
1805/**
1806 * The exported method. Makes the core api available via a global and
1807 * returns the udp transport API.
1808 *
1809 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1810 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1811 */
1812void *
1813libgnunet_plugin_transport_udp_init (void *cls)
1814{
1815 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1816 struct GNUNET_TRANSPORT_PluginFunctions *api;
1817 struct Plugin *plugin;
1818
1819 unsigned long long port;
1820 unsigned long long aport;
1821 unsigned long long broadcast;
1822 unsigned long long udp_max_bps;
1823 unsigned long long enable_v6;
1824 char * bind4_address;
1825 char * bind6_address;
1826 struct GNUNET_TIME_Relative interval;
1827
1828 struct sockaddr_in serverAddrv4;
1829 struct sockaddr_in6 serverAddrv6;
1830
1831 int res;
1832
1833 /* Get port number */
1834 if (GNUNET_OK !=
1835 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1836 &port))
1837 port = 2086;
1838 if (GNUNET_OK !=
1839 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1840 "ADVERTISED_PORT", &aport))
1841 aport = port;
1842 if (port > 65535)
1843 {
1844 LOG (GNUNET_ERROR_TYPE_WARNING,
1845 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
1846 65535);
1847 return NULL;
1848 }
1849
1850 /* Protocols */
1851 if ((GNUNET_YES ==
1852 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
1853 "DISABLEV6")))
1854 {
1855 enable_v6 = GNUNET_NO;
1856 }
1857 else
1858 enable_v6 = GNUNET_YES;
1859
1860
1861 /* Addresses */
1862 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1863 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1864
1865 if (GNUNET_YES ==
1866 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1867 "BINDTO", &bind4_address))
1868 {
1869 LOG (GNUNET_ERROR_TYPE_DEBUG,
1870 "Binding udp plugin to specific address: `%s'\n",
1871 bind4_address);
1872 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
1873 {
1874 GNUNET_free (bind4_address);
1875 return NULL;
1876 }
1877 }
1878
1879 if (GNUNET_YES ==
1880 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1881 "BINDTO6", &bind6_address))
1882 {
1883 LOG (GNUNET_ERROR_TYPE_DEBUG,
1884 "Binding udp plugin to specific address: `%s'\n",
1885 bind6_address);
1886 if (1 !=
1887 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
1888 {
1889 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1890 bind6_address);
1891 GNUNET_free_non_null (bind4_address);
1892 GNUNET_free (bind6_address);
1893 return NULL;
1894 }
1895 }
1896
1897
1898 /* Enable neighbour discovery */
1899 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
1900 "BROADCAST");
1901 if (broadcast == GNUNET_SYSERR)
1902 broadcast = GNUNET_NO;
1903
1904 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
1905 "BROADCAST_INTERVAL", &interval))
1906 {
1907 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1908 }
1909
1910 /* Maximum datarate */
1911 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1912 "MAX_BPS", &udp_max_bps))
1913 {
1914 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1915 }
1916
1917 plugin = GNUNET_malloc (sizeof (struct Plugin));
1918 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1919
1920 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1921 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
1922
1923
1924 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
1925 plugin->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1926 plugin->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1927 plugin->port = port;
1928 plugin->aport = aport;
1929 plugin->broadcast_interval = interval;
1930 plugin->enable_ipv6 = enable_v6;
1931 plugin->env = env;
1932
1933 api->cls = plugin;
1934 api->send = NULL;
1935 api->disconnect = &udp_disconnect;
1936 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1937 api->address_to_string = &udp_address_to_string;
1938 api->check_address = &udp_plugin_check_address;
1939 api->get_session = &udp_plugin_get_session;
1940 api->send = &udp_plugin_send_wrapper;
1941 api->send_with_session = &udp_plugin_send;
1942
1943 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
1944 res = setup_sockets (plugin, &serverAddrv6, &serverAddrv4);
1945 if ((res == 0) || ((plugin->sockv4 == NULL) && (plugin->sockv6 == NULL)))
1946 {
1947 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
1948 GNUNET_free (plugin);
1949 GNUNET_free (api);
1950 return NULL;
1951 }
1952
1953 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
1954 if (broadcast == GNUNET_YES)
1955 setup_broadcast (plugin, &serverAddrv6, &serverAddrv4);
1956
1957
1958 GNUNET_free_non_null (bind4_address);
1959 GNUNET_free_non_null (bind6_address);
1960 return api;
1961}
1962
1963int heap_cleanup_iterator (void *cls,
1964 struct GNUNET_CONTAINER_HeapNode *
1965 node, void *element,
1966 GNUNET_CONTAINER_HeapCostType
1967 cost)
1968{
1969 struct DefragContext * d_ctx = element;
1970
1971 GNUNET_CONTAINER_heap_remove_node (node);
1972 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
1973 GNUNET_free (d_ctx);
1974
1975 return GNUNET_YES;
1976}
1977
1978
1979/**
1980 * The exported method. Makes the core api available via a global and
1981 * returns the udp transport API.
1982 *
1983 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1984 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1985 */
1986void *
1987libgnunet_plugin_transport_udp_done (void *cls)
1988{
1989 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1990 struct Plugin *plugin = api->cls;
1991 stop_broadcast (plugin);
1992
1993 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1994 {
1995 GNUNET_SCHEDULER_cancel (plugin->select_task);
1996 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1997 }
1998
1999 /* Closing sockets */
2000 if (plugin->sockv4 != NULL)
2001 {
2002 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2003 plugin->sockv4 = NULL;
2004 }
2005 if (plugin->sockv6 != NULL)
2006 {
2007 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2008 plugin->sockv6 = NULL;
2009 }
2010 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2011 GNUNET_NETWORK_fdset_destroy (plugin->ws);
2012 GNUNET_NAT_unregister (plugin->nat);
2013
2014 if (plugin->defrag_ctxs != NULL)
2015 {
2016 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2017 heap_cleanup_iterator, NULL);
2018 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2019 plugin->defrag_ctxs = NULL;
2020 }
2021 if (plugin->mst != NULL)
2022 {
2023 GNUNET_SERVER_mst_destroy(plugin->mst);
2024 plugin->mst = NULL;
2025 }
2026
2027 /* Clean up leftover messages */
2028 struct UDPMessageWrapper *udpw = plugin->msg_head;
2029 while (udpw != NULL)
2030 {
2031 struct UDPMessageWrapper *tmp = udpw->next;
2032 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
2033 if (udpw->cont != NULL)
2034 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
2035 GNUNET_free (udpw);
2036 udpw = tmp;
2037 }
2038
2039 /* Clean up sessions */
2040#if DEBUG_UDP
2041 LOG (GNUNET_ERROR_TYPE_DEBUG,
2042 "Cleaning up sessions\n");
2043#endif
2044 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2045 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2046
2047 plugin->nat = NULL;
2048 GNUNET_free (plugin);
2049 GNUNET_free (api);
2050 return NULL;
2051}
2052
2053
2054/* end of plugin_transport_udp.c */
diff --git a/src/transport/plugin_transport_unix.c b/src/transport/plugin_transport_unix.c
index 9f717f649..c5cdb17d4 100644
--- a/src/transport/plugin_transport_unix.c
+++ b/src/transport/plugin_transport_unix.c
@@ -346,7 +346,6 @@ find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
346 * send_handle squared away! 346 * send_handle squared away!
347 * 347 *
348 * @param cls closure 348 * @param cls closure
349 * @param incoming_retry_context the retry context to use
350 * @param send_handle which handle to send message on 349 * @param send_handle which handle to send message on
351 * @param target who should receive this message (ignored by UNIX) 350 * @param target who should receive this message (ignored by UNIX)
352 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together 351 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
diff --git a/src/transport/transport-testing.c b/src/transport/transport-testing.c
index dcd95053b..01b313a15 100644
--- a/src/transport/transport-testing.c
+++ b/src/transport/transport-testing.c
@@ -518,22 +518,23 @@ GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
518} 518}
519 519
520/** 520/**
521 * Initiate peer p1 to connect to peer p2 521 * Connect the given peers and call the callback when both peers report the
522 * Get peer p2's HELLO and offer it to p1 522 * inbound connection. Remarks: start_peer's notify_connect callback can be called
523 * p1 then tries to connect to p2 523 * before.
524 *
525 * @param tth transport testing handle
524 * @param p1 peer 1 526 * @param p1 peer 1
525 * @param p2 peer 2 527 * @param p2 peer 2
526 * @param cb the callback to call when both peers notified that they are connected 528 * @param cb the callback to call when both peers notified that they are connected
527 * @param cb_cls callback cls (or a pointer to the 529 * @param cls callback cls
528 * GNUNET_TRANSPORT_TESTING_ConnectRequest itself if null) 530 * @return a connect request handle
529 * @return connect context
530 */ 531 */
531GNUNET_TRANSPORT_TESTING_ConnectRequest 532GNUNET_TRANSPORT_TESTING_ConnectRequest
532GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle 533GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
533 *tth, struct PeerContext *p1, 534 struct PeerContext *p1,
534 struct PeerContext *p2, 535 struct PeerContext *p2,
535 GNUNET_TRANSPORT_TESTING_connect_cb cb, 536 GNUNET_TRANSPORT_TESTING_connect_cb cb,
536 void *cb_cls) 537 void *cls)
537{ 538{
538 GNUNET_assert (tth != NULL); 539 GNUNET_assert (tth != NULL);
539 540
@@ -547,8 +548,8 @@ GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
547 cc->p2 = p2; 548 cc->p2 = p2;
548 549
549 cc->cb = cb; 550 cc->cb = cb;
550 if (cb_cls != NULL) 551 if (cls != NULL)
551 cc->cb_cls = cb_cls; 552 cc->cb_cls = cls;
552 else 553 else
553 cc->cb_cls = cc; 554 cc->cb_cls = cc;
554 555
diff --git a/src/transport/transport-testing.h b/src/transport/transport-testing.h
index cc4974ff8..20e802dfa 100644
--- a/src/transport/transport-testing.h
+++ b/src/transport/transport-testing.h
@@ -189,12 +189,12 @@ GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
189 * @param p1 peer 1 189 * @param p1 peer 1
190 * @param p2 peer 2 190 * @param p2 peer 2
191 * @param cb the callback to call when both peers notified that they are connected 191 * @param cb the callback to call when both peers notified that they are connected
192 * @param cb_cls callback cls 192 * @param cls callback cls
193 * @return a connect request handle 193 * @return a connect request handle
194 */ 194 */
195GNUNET_TRANSPORT_TESTING_ConnectRequest 195GNUNET_TRANSPORT_TESTING_ConnectRequest
196GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle 196GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
197 *tth, struct PeerContext *p1, 197 struct PeerContext *p1,
198 struct PeerContext *p2, 198 struct PeerContext *p2,
199 GNUNET_TRANSPORT_TESTING_connect_cb cb, 199 GNUNET_TRANSPORT_TESTING_connect_cb cb,
200 void *cls); 200 void *cls);
@@ -203,7 +203,7 @@ GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
203 * Cancel the request to connect two peers 203 * Cancel the request to connect two peers
204 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully 204 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
205 * @param tth testing 205 * @param tth testing
206 * @param cc a connect request handle 206 * @param ccr a connect request handle
207 */ 207 */
208void 208void
209GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct 209GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct