aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_udp_new.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-10-21 21:53:56 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-10-21 21:53:56 +0000
commitddc17ac9517141ce9b342ddbb10c87ed328da01b (patch)
tree9939443d95e9193f807ec5a75b5afcc2a0496450 /src/transport/plugin_transport_udp_new.c
parent23066715ced62f6a813b130911f705566f2322fd (diff)
downloadgnunet-ddc17ac9517141ce9b342ddbb10c87ed328da01b.tar.gz
gnunet-ddc17ac9517141ce9b342ddbb10c87ed328da01b.zip
concept for new completely session based udp plugin
old plugin required too many dirty hacks to pass required data around
Diffstat (limited to 'src/transport/plugin_transport_udp_new.c')
-rw-r--r--src/transport/plugin_transport_udp_new.c1449
1 files changed, 1449 insertions, 0 deletions
diff --git a/src/transport/plugin_transport_udp_new.c b/src/transport/plugin_transport_udp_new.c
new file mode 100644
index 000000000..325c5d893
--- /dev/null
+++ b/src/transport/plugin_transport_udp_new.c
@@ -0,0 +1,1449 @@
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 NAT punching
24 * transport service
25 * @author Christian Grothoff
26 * @author Nathan Evans
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
45#define DEBUG_UDP GNUNET_EXTRA_LOGGING
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 * Number of messages we can defragment in parallel. We only really
55 * defragment 1 message at a time, but if messages get re-ordered, we
56 * may want to keep knowledge about the previous message to avoid
57 * discarding the current message in favor of a single fragment of a
58 * previous message. 3 should be good since we don't expect massive
59 * message reorderings with UDP.
60 */
61#define UDP_MAX_MESSAGES_IN_DEFRAG 3
62
63/**
64 * We keep a defragmentation queue per sender address. How many
65 * sender addresses do we support at the same time? Memory consumption
66 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
67 * value. (So 128 corresponds to 12 MB and should suffice for
68 * connecting to roughly 128 peers via UDP).
69 */
70#define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
71
72
73/**
74 * UDP Message-Packet header (after defragmentation).
75 */
76struct UDPMessage
77{
78 /**
79 * Message header.
80 */
81 struct GNUNET_MessageHeader header;
82
83 /**
84 * Always zero for now.
85 */
86 uint32_t reserved;
87
88 /**
89 * What is the identity of the sender
90 */
91 struct GNUNET_PeerIdentity sender;
92
93};
94
95
96/**
97 * UDP ACK Message-Packet header (after defragmentation).
98 */
99struct UDP_ACK_Message
100{
101 /**
102 * Message header.
103 */
104 struct GNUNET_MessageHeader header;
105
106 /**
107 * Desired delay for flow control
108 */
109 uint32_t delay;
110
111 /**
112 * What is the identity of the sender
113 */
114 struct GNUNET_PeerIdentity sender;
115};
116
117
118/**
119 * Network format for IPv4 addresses.
120 */
121struct IPv4UdpAddress
122{
123 /**
124 * IPv4 address, in network byte order.
125 */
126 uint32_t ipv4_addr GNUNET_PACKED;
127
128 /**
129 * Port number, in network byte order.
130 */
131 uint16_t u4_port GNUNET_PACKED;
132};
133
134
135/**
136 * Network format for IPv6 addresses.
137 */
138struct IPv6UdpAddress
139{
140
141 /**
142 * IPv6 address.
143 */
144 struct in6_addr ipv6_addr GNUNET_PACKED;
145
146 /**
147 * Port number, in network byte order.
148 */
149 uint16_t u6_port GNUNET_PACKED;
150};
151
152
153/* Forward definition */
154struct Plugin;
155
156
157/**
158 * Session with another peer. FIXME: why not make this into
159 * a regular 'struct Session' and pass it around!?
160 */
161struct Session
162{
163
164 /**
165 * Which peer is this session for or from?
166 */
167 struct GNUNET_PeerIdentity target;
168
169 /**
170 * Pointer to the global plugin struct.
171 */
172 struct Plugin *plugin;
173
174 /**
175 * Address of the other peer
176 */
177 const struct sockaddr *sock_addr;
178
179 size_t addrlen;
180
181 /**
182 * Function to call upon completion of the transmission.
183 */
184 GNUNET_TRANSPORT_TransmitContinuation cont;
185
186 /**
187 * Closure for 'cont'.
188 */
189 void *cont_cls;
190
191 /**
192 * Current outgoing message to this peer.
193 */
194 struct GNUNET_FRAGMENT_Context *frag;
195
196 /*
197 * Is this a reliable, bidirectional connection?
198 * YES if we already received a message with this session
199 */
200 int bidirectional;
201
202 /*
203 * Task invalidating this session if idle
204 */
205 GNUNET_SCHEDULER_TaskIdentifier invalidation_task;
206
207 /*
208 * Desired delay for next sending we send to other peer
209 */
210 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
211
212 /*
213 * Desired delay for next sending we received from other peer
214 */
215 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
216};
217
218/**
219 * Encapsulation of all of the state of the plugin.
220 */
221struct Plugin
222{
223
224 /**
225 * Our environment.
226 */
227 struct GNUNET_TRANSPORT_PluginEnvironment *env;
228
229 /**
230 * Session of peers with whom we are currently connected,
231 * map of peer identity to 'struct PeerSession'.
232 */
233 struct GNUNET_CONTAINER_MultiHashMap *sessions;
234
235 /**
236 * Heap with all of our defragmentation activities.
237 */
238 struct GNUNET_CONTAINER_Heap *defrags;
239
240 /**
241 * ID of select task
242 */
243 GNUNET_SCHEDULER_TaskIdentifier select_task;
244
245 /**
246 * Tokenizer for inbound messages.
247 */
248 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
249
250 /**
251 * Bandwidth tracker to limit global UDP traffic.
252 */
253 struct GNUNET_BANDWIDTH_Tracker tracker;
254
255 /**
256 * Address we were told to bind to exclusively (IPv4).
257 */
258 char *bind4_address;
259
260 /**
261 * Address we were told to bind to exclusively (IPv6).
262 */
263 char *bind6_address;
264
265 /**
266 * Handle to NAT traversal support.
267 */
268 struct GNUNET_NAT_Handle *nat;
269
270 /**
271 * FD Read set
272 */
273 struct GNUNET_NETWORK_FDSet *rs;
274
275 /**
276 * The read socket for IPv4
277 */
278 struct GNUNET_NETWORK_Handle *sockv4;
279
280 /**
281 * The read socket for IPv6
282 */
283 struct GNUNET_NETWORK_Handle *sockv6;
284
285 /**
286 * expected delay for ACKs
287 */
288 struct GNUNET_TIME_Relative last_expected_delay;
289
290 /**
291 * Port we listen on.
292 */
293 uint16_t port;
294
295 /**
296 * Port we advertise on.
297 */
298 uint16_t aport;
299
300};
301
302int delete_session_iterator (void *cls,
303 const GNUNET_HashCode * key,
304 void *value)
305{
306 struct Session *s = (struct Session *) value;
307 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
308 {
309 GNUNET_SCHEDULER_cancel(s->invalidation_task);
310 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
311 }
312 GNUNET_CONTAINER_multihashmap_remove(s->plugin->sessions, key, s);
313
314 GNUNET_free (s);
315 return GNUNET_YES;
316}
317/**
318 * Disconnect from a remote node. Clean up session if we have one for this peer
319 *
320 * @param cls closure for this call (should be handle to Plugin)
321 * @param target the peeridentity of the peer to disconnect
322 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
323 */
324static void
325udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
326{
327 struct Plugin *plugin = cls;
328
329 GNUNET_CONTAINER_multihashmap_get_multiple(plugin, &target->hashPubKey, &delete_session_iterator, NULL);
330
331}
332
333
334/**
335 * Actually send out the message.
336 *
337 * @param plugin the plugin
338 * @param sa the address to send the message to
339 * @param msg message to transmit
340 * @return the number of bytes written
341 */
342static ssize_t
343udp_send (struct Plugin *plugin, const struct sockaddr *sa,
344 const struct GNUNET_MessageHeader *msg)
345{
346 ssize_t sent;
347 size_t slen;
348
349 switch (sa->sa_family)
350 {
351 case AF_INET:
352 if (NULL == plugin->sockv4)
353 return 0;
354 sent =
355 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
356 sa, slen = sizeof (struct sockaddr_in));
357 break;
358 case AF_INET6:
359 if (NULL == plugin->sockv6)
360 return 0;
361 sent =
362 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
363 sa, slen = sizeof (struct sockaddr_in6));
364 break;
365 default:
366 GNUNET_break (0);
367 return 0;
368 }
369 if (GNUNET_SYSERR == sent)
370 {
371 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
372 LOG (GNUNET_ERROR_TYPE_ERROR,
373 "UDP transmited %u-byte message to %s (%d: %s)\n",
374 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
375 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
376
377 }
378 LOG (GNUNET_ERROR_TYPE_DEBUG,
379 "UDP transmited %u-byte message to %s (%d: %s)\n",
380 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
381 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
382 return sent;
383}
384
385
386static struct Session *
387create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
388 const void *addr, size_t addrlen,
389 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
390{
391 struct Session * s;
392 const struct IPv4UdpAddress *t4;
393 const struct IPv6UdpAddress *t6;
394 struct sockaddr_in *v4;
395 struct sockaddr_in6 *v6;
396 size_t len;
397
398 switch (addrlen)
399 {
400 case sizeof (struct IPv4UdpAddress):
401 if (NULL == plugin->sockv4)
402 {
403 return NULL;
404 }
405 t4 = addr;
406 s =
407 GNUNET_malloc (sizeof (struct Session) +
408 sizeof (struct sockaddr_in));
409 len = sizeof (struct sockaddr_in);
410 v4 = (struct sockaddr_in *) &s[1];
411 v4->sin_family = AF_INET;
412#if HAVE_SOCKADDR_IN_SIN_LEN
413 v4->sin_len = sizeof (struct sockaddr_in);
414#endif
415 v4->sin_port = t4->u4_port;
416 v4->sin_addr.s_addr = t4->ipv4_addr;
417 break;
418 case sizeof (struct IPv6UdpAddress):
419 if (NULL == plugin->sockv6)
420 {
421 return NULL;
422 }
423 t6 = addr;
424 s =
425 GNUNET_malloc (sizeof (struct Session) +
426 sizeof (struct sockaddr_in6));
427 len = sizeof (struct sockaddr_in6);
428 v6 = (struct sockaddr_in6 *) &s[1];
429 v6->sin6_family = AF_INET6;
430#if HAVE_SOCKADDR_IN_SIN_LEN
431 v6->sin6_len = sizeof (struct sockaddr_in6);
432#endif
433 v6->sin6_port = t6->u6_port;
434 v6->sin6_addr = t6->ipv6_addr;
435 break;
436 default:
437 /* Must have a valid address to send to */
438 GNUNET_break_op (0);
439 return NULL;
440 }
441
442 s->valid_until = GNUNET_TIME_absolute_get_zero ();
443 s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero ();
444 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero ();
445 s->bidirectional = GNUNET_NO;
446 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
447 s->addrlen = len;
448 s->target = *target;
449 s->plugin = plugin;
450 s->sock_addr = (const struct sockaddr *) &s[1];
451 s->cont = cont;
452 s->cont_cls = cont_cls;
453
454 return s;
455}
456
457static void
458invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
459{
460 struct Session * s = cls;
461 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
462 LOG (GNUNET_ERROR_TYPE_ERROR,
463 "Session %X (`%s') is now invalid\n", s, GNUNET_a2s (s->sock_addr,s->addrlen));
464
465 s->plugin->env->session_end(s->plugin->env->cls, &s->target, s);
466 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(s->plugin->sessions, &s->target.hashPubKey, s));
467 GNUNET_free (s);
468}
469
470static const char *
471udp_address_to_string (void *cls, const void *addr, size_t addrlen);
472
473/**
474 * Function that is called with messages created by the fragmentation
475 * module. In the case of the 'proc' callback of the
476 * GNUNET_FRAGMENT_context_create function, this function must
477 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
478 *
479 * @param cls closure, the 'struct PeerSession'
480 * @param msg the message that was created
481 */
482static void
483send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
484{
485 struct Session *s = cls;
486
487 udp_send (session->plugin, session->sock_addr, msg);
488 GNUNET_FRAGMENT_context_transmission_done (session->frag);
489}
490
491/**
492 * Function that can be used by the transport service to transmit
493 * a message using the plugin.
494 *
495 * @param cls closure
496 * @param target who should receive this message (ignored by UDP)
497 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
498 * @param msgbuf_size the size of the msgbuf to send
499 * @param priority how important is the message (ignored by UDP)
500 * @param timeout when should we time out (give up) if we can not transmit?
501 * @param session identifier used for this session (NULL for UDP)
502 * @param addr the addr to send the message to
503 * @param addrlen the len of addr
504 * @param force_address not used, we had better have an address to send to
505 * because we are stateless!!
506 * @param cont continuation to call once the message has
507 * been transmitted (or if the transport is ready
508 * for the next transmission call; or if the
509 * peer disconnected...)
510 * @param cont_cls closure for cont
511 *
512 * @return the number of bytes written (may return 0 and the message can
513 * still be transmitted later!)
514 */
515static ssize_t
516udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
517 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
518 struct GNUNET_TIME_Relative timeout, struct Session *session,
519 const void *addr, size_t addrlen, int force_address,
520 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
521{
522 struct Plugin *plugin = cls;
523 struct Session *s;
524 const struct IPv4UdpAddress *t4;
525 const struct IPv6UdpAddress *t6;
526 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
527 char mbuf[mlen];
528 struct UDPMessage *udp;
529
530 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
531 {
532 GNUNET_break (0);
533 return GNUNET_SYSERR;
534 }
535
536 LOG (GNUNET_ERROR_TYPE_DEBUG,
537 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
538 msgbuf_size, GNUNET_i2s (target),
539 udp_address_to_string (NULL, addr, addrlen),
540 session, force_address);
541
542 /* no valid address given */
543 if ((addr = NULL) || (addrlen == 0))
544 {
545 GNUNET_break (0);
546 return GNUNET_SYSERR;
547 }
548
549 if (session != NULL)
550 {
551 s = find_session (target, addr, addrlen);
552 if (s != session)
553 {
554 /* found a conflicting session for this peer */
555 GNUNET_break (0);
556 return GNUNET_SYSERR;
557 }
558 }
559 else
560 {
561 /* use a reliable transmission but no session given */
562 if (force_address == GNUNET_SYSERR)
563 return GNUNET_SYSERR;
564 /* create new session */
565 s = create_session(plugin, target, addr, addrlen, cont, cont_cls);
566 GNUNET_CONTAINER_multihashmap_put(plugin->sessions, &target->hashPubKey, s, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
567 /* schedule invalidation task */
568 s->invalidation_task = GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &invalidation_task, NULL);
569 }
570
571 /* Message */
572 udp = (struct UDPMessage *) mbuf;
573 udp->header.size = htons (mlen);
574 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
575 udp->reserved = htonl (0);
576 udp->sender = *plugin->env->my_identity;
577 memcpy (&udp[1], msgbuf, msgbuf_size);
578
579 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
580 if (s->flow_delay_from_other_peer.abs_value > now.abs_value)
581 {
582 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(now, s->flow_delay_from_other_peer);
583 LOG (GNUNET_ERROR_TYPE_DEBUG,
584 "We try to send to early! Should in %llu!\n", delta.rel_value);
585 }
586 else
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "We can send!\n");
588
589
590 /* send without fragmentation */
591 if (mlen <= UDP_MTU)
592 {
593 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
594 if (cont != NULL)
595 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
596 GNUNET_free_non_null (peer_session);
597 }
598 /* send with fragmentation */
599 else
600 {
601 s->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
602 &plugin->tracker,
603 plugin->last_expected_delay,
604 &udp->header, &send_fragment,
605 s);
606 }
607 return mlen;
608}
609
610
611/**
612 * Message tokenizer has broken up an incomming message. Pass it on
613 * to the service.
614 *
615 * @param cls the 'struct Plugin'
616 * @param client the Session
617 * @param hdr the actual message
618 */
619static void
620process_inbound_tokenized_messages (void *cls, void *client,
621 const struct GNUNET_MessageHeader *hdr)
622{
623 struct Plugin *plugin = cls;
624 struct Session * s = client;
625 struct GNUNET_ATS_Information distance;
626 struct GNUNET_TIME_Relative delay;
627
628 /* setup ATS */
629 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
630 distance.value = htonl (1);
631
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "Giving Session %X %s to transport\n", s->session, GNUNET_i2s(&s->target));
634 delay = plugin->env->receive (plugin->env->cls, &s->target, hdr, &distance, 1, s,
635 // FIXME: USE UDP ADDRESSES!!!!
636 s->sock_addr, s->addrlen);
637 s->flow_delay_for_other_peer = delay;
638}
639
640
641/**
642 * We've received a UDP Message. Process it (pass contents to main service).
643 *
644 * @param plugin plugin context
645 * @param msg the message
646 * @param sender_addr sender address
647 * @param sender_addr_len number of bytes in sender_addr
648 */
649static void
650process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
651 const struct sockaddr *sender_addr,
652 socklen_t sender_addr_len)
653{
654 struct IPv4UdpAddress u4;
655 struct IPv6UdpAddress u6;
656 const void *arg;
657 size_t args;
658
659 if (0 != ntohl (msg->reserved))
660 {
661 GNUNET_break_op (0);
662 return;
663 }
664 if (ntohs (msg->header.size) <
665 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
666 {
667 GNUNET_break_op (0);
668 return;
669 }
670
671 /* convert address */
672 switch (sender_addr->sa_family)
673 {
674 case AF_INET:
675 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
676 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
677 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
678 arg = &u4;
679 args = sizeof (u4);
680 break;
681 case AF_INET6:
682 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
683 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
684 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
685 arg = &u6;
686 args = sizeof (u6);
687 break;
688 default:
689 GNUNET_break (0);
690 return;
691 }
692#if DEBUG_UDP
693 LOG (GNUNET_ERROR_TYPE_DEBUG,
694 "Received message with %u bytes from peer `%s' at `%s'\n",
695 (unsigned int) ntohs (msg->header.size),
696 GNUNET_i2s (&msg->sender), GNUNET_a2s (sender_addr,
697 sender_addr_len));
698#endif
699
700 const struct UDPMessage * udp_msg = (const struct UDPMessage *) msg;
701 LOG (GNUNET_ERROR_TYPE_DEBUG,
702 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
703 GNUNET_i2s (&udp_msg->sender),
704 udp_address_to_string(NULL, arg, args));
705
706 /* create a session for inbound connections */
707 struct Session * s = NULL;
708 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr, sender_addr_len);
709
710 if (s != NULL)
711 {
712 LOG (GNUNET_ERROR_TYPE_DEBUG,
713 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
714 s,
715 GNUNET_i2s (&s->target),
716 udp_address_to_string(NULL, arg, args));
717 }
718 else
719 {
720 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
721 LOG (GNUNET_ERROR_TYPE_DEBUG,
722 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
723 s,
724 GNUNET_i2s (&s->target),
725 udp_address_to_string(NULL, arg, args));
726
727 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
728 &s->target.hashPubKey,
729 s,
730 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
731 }
732 s->valid_until = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
733 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
734 {
735 GNUNET_SCHEDULER_cancel(s->invalidation_task);
736 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
737 LOG (GNUNET_ERROR_TYPE_DEBUG,
738 "Rescheduling %X' `%s'\n",
739 s, udp_address_to_string(NULL, arg, args));
740 }
741 s->invalidation_task = GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &invalidation_task, s);
742 /* we received from peer, so session is bidirectional and reliable */
743 s->bidirectional = GNUNET_YES;
744
745 GNUNET_SERVER_mst_receive (plugin->mst, &s, (const char *) &msg[1],
746 ntohs (msg->header.size) -
747 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
748}
749
750
751/**
752 * Read and process a message from the given socket.
753 *
754 * @param plugin the overall plugin
755 * @param rsock socket to read from
756 */
757static void
758udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
759{
760 socklen_t fromlen;
761 char addr[32];
762 char buf[65536];
763 ssize_t ret;
764 const struct GNUNET_MessageHeader *msg;
765 const struct GNUNET_MessageHeader *ack;
766 struct Session *peer_session;
767 const struct UDP_ACK_Message *udp_ack;
768 struct ReceiveContext *rc;
769 struct GNUNET_TIME_Absolute now;
770 struct FindReceiveContext frc;
771 struct Session * s = NULL;
772 struct GNUNET_TIME_Relative flow_delay;
773
774 fromlen = sizeof (addr);
775 memset (&addr, 0, sizeof (addr));
776 ret =
777 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
778 (struct sockaddr *) &addr, &fromlen);
779 if (ret < sizeof (struct GNUNET_MessageHeader))
780 {
781 GNUNET_break_op (0);
782 return;
783 }
784 msg = (const struct GNUNET_MessageHeader *) buf;
785
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
788 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs(msg->type));
789
790 if (ret != ntohs (msg->size))
791 {
792 GNUNET_break_op (0);
793 return;
794 }
795
796 switch (ntohs (msg->type))
797 {
798 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
799 if (ntohs (msg->size) < sizeof (struct UDPMessage))
800 {
801 GNUNET_break_op (0);
802 return;
803 }
804 process_udp_message (plugin, (const struct UDPMessage *) msg,
805 (const struct sockaddr *) addr, fromlen);
806 return;
807
808 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
809 if (ntohs (msg->size) <
810 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
811 {
812 GNUNET_break_op (0);
813 return;
814 }
815 udp_ack = (const struct UDP_ACK_Message *) msg;
816 LOG (GNUNET_ERROR_TYPE_DEBUG,
817 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
818 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp->sender),
819 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
820
821 // TODO Guess a session from address, give it defragmenter and check in process_udp if guess was right
822 s = guess_session (plugin->sessions, addr, addrlen);
823 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
824 // ... other stuff
825 return;
826
827 case GNUNET_MESSAGE_TYPE_FRAGMENT:
828 LOG (GNUNET_ERROR_TYPE_DEBUG,
829 "UDP processes %u-byte fragment from `%s'\n",
830 (unsigned int) ntohs (msg->size),
831 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
832 // TODO Guess a session from address, give it defragmenter and check in process_udp if guess was right
833 s = guess_session (plugin->sessions, addr, addrlen);
834 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
835 // other stuff
836 return;
837 default:
838 GNUNET_break_op (0);
839 return;
840 }
841}
842
843
844/**
845 * We have been notified that our writeset has something to read. We don't
846 * know which socket needs to be read, so we have to check each one
847 * Then reschedule this function to be called again once more is available.
848 *
849 * @param cls the plugin handle
850 * @param tc the scheduling context (for rescheduling this function again)
851 */
852static void
853udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
854{
855 struct Plugin *plugin = cls;
856
857 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
858 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
859 return;
860 if ((NULL != plugin->sockv4) &&
861 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
862 udp_read (plugin, plugin->sockv4);
863 if ((NULL != plugin->sockv6) &&
864 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
865 udp_read (plugin, plugin->sockv6);
866 plugin->select_task =
867 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
868 GNUNET_SCHEDULER_NO_TASK,
869 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
870 NULL, &udp_plugin_select, plugin);
871
872}
873
874
875/**
876 * Check if the given port is plausible (must be either our listen
877 * port or our advertised port). If it is neither, we return
878 * GNUNET_SYSERR.
879 *
880 * @param plugin global variables
881 * @param in_port port number to check
882 * @return GNUNET_OK if port is either open_port or adv_port
883 */
884static int
885check_port (struct Plugin *plugin, uint16_t in_port)
886{
887 if ((in_port == plugin->port) || (in_port == plugin->aport))
888 return GNUNET_OK;
889 return GNUNET_SYSERR;
890}
891
892
893/**
894 * Function that will be called to check if a binary address for this
895 * plugin is well-formed and corresponds to an address for THIS peer
896 * (as per our configuration). Naturally, if absolutely necessary,
897 * plugins can be a bit conservative in their answer, but in general
898 * plugins should make sure that the address does not redirect
899 * traffic to a 3rd party that might try to man-in-the-middle our
900 * traffic.
901 *
902 * @param cls closure, should be our handle to the Plugin
903 * @param addr pointer to the address
904 * @param addrlen length of addr
905 * @return GNUNET_OK if this is a plausible address for this peer
906 * and transport, GNUNET_SYSERR if not
907 *
908 */
909static int
910udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
911{
912 struct Plugin *plugin = cls;
913 struct IPv4UdpAddress *v4;
914 struct IPv6UdpAddress *v6;
915
916 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
917 (addrlen != sizeof (struct IPv6UdpAddress)))
918 {
919 GNUNET_break_op (0);
920 return GNUNET_SYSERR;
921 }
922 if (addrlen == sizeof (struct IPv4UdpAddress))
923 {
924 v4 = (struct IPv4UdpAddress *) addr;
925 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
926 return GNUNET_SYSERR;
927 if (GNUNET_OK !=
928 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
929 sizeof (struct in_addr)))
930 return GNUNET_SYSERR;
931 }
932 else
933 {
934 v6 = (struct IPv6UdpAddress *) addr;
935 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
936 {
937 GNUNET_break_op (0);
938 return GNUNET_SYSERR;
939 }
940 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
941 return GNUNET_SYSERR;
942 if (GNUNET_OK !=
943 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
944 sizeof (struct in6_addr)))
945 return GNUNET_SYSERR;
946 }
947 return GNUNET_OK;
948}
949
950
951/**
952 * Function called for a quick conversion of the binary address to
953 * a numeric address. Note that the caller must not free the
954 * address and that the next call to this function is allowed
955 * to override the address again.
956 *
957 * @param cls closure
958 * @param addr binary address
959 * @param addrlen length of the address
960 * @return string representing the same address
961 */
962static const char *
963udp_address_to_string (void *cls, const void *addr, size_t addrlen)
964{
965 static char rbuf[INET6_ADDRSTRLEN + 10];
966 char buf[INET6_ADDRSTRLEN];
967 const void *sb;
968 struct in_addr a4;
969 struct in6_addr a6;
970 const struct IPv4UdpAddress *t4;
971 const struct IPv6UdpAddress *t6;
972 int af;
973 uint16_t port;
974
975 if (addrlen == sizeof (struct IPv6UdpAddress))
976 {
977 t6 = addr;
978 af = AF_INET6;
979 port = ntohs (t6->u6_port);
980 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
981 sb = &a6;
982 }
983 else if (addrlen == sizeof (struct IPv4UdpAddress))
984 {
985 t4 = addr;
986 af = AF_INET;
987 port = ntohs (t4->u4_port);
988 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
989 sb = &a4;
990 }
991 else
992 {
993 GNUNET_break_op (0);
994 return NULL;
995 }
996 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
997 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
998 buf, port);
999 return rbuf;
1000}
1001
1002
1003/**
1004 * Closure for 'append_port'.
1005 */
1006struct PrettyPrinterContext
1007{
1008 /**
1009 * Function to call with the result.
1010 */
1011 GNUNET_TRANSPORT_AddressStringCallback asc;
1012
1013 /**
1014 * Clsoure for 'asc'.
1015 */
1016 void *asc_cls;
1017
1018 /**
1019 * Port to add after the IP address.
1020 */
1021 uint16_t port;
1022};
1023
1024
1025/**
1026 * Append our port and forward the result.
1027 *
1028 * @param cls a 'struct PrettyPrinterContext'
1029 * @param hostname result from DNS resolver
1030 */
1031static void
1032append_port (void *cls, const char *hostname)
1033{
1034 struct PrettyPrinterContext *ppc = cls;
1035 char *ret;
1036
1037 if (hostname == NULL)
1038 {
1039 ppc->asc (ppc->asc_cls, NULL);
1040 GNUNET_free (ppc);
1041 return;
1042 }
1043 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1044 ppc->asc (ppc->asc_cls, ret);
1045 GNUNET_free (ret);
1046}
1047
1048
1049/**
1050 * Convert the transports address to a nice, human-readable
1051 * format.
1052 *
1053 * @param cls closure
1054 * @param type name of the transport that generated the address
1055 * @param addr one of the addresses of the host, NULL for the last address
1056 * the specific address format depends on the transport
1057 * @param addrlen length of the address
1058 * @param numeric should (IP) addresses be displayed in numeric form?
1059 * @param timeout after how long should we give up?
1060 * @param asc function to call on each string
1061 * @param asc_cls closure for asc
1062 */
1063static void
1064udp_plugin_address_pretty_printer (void *cls, const char *type,
1065 const void *addr, size_t addrlen,
1066 int numeric,
1067 struct GNUNET_TIME_Relative timeout,
1068 GNUNET_TRANSPORT_AddressStringCallback asc,
1069 void *asc_cls)
1070{
1071 struct PrettyPrinterContext *ppc;
1072 const void *sb;
1073 size_t sbs;
1074 struct sockaddr_in a4;
1075 struct sockaddr_in6 a6;
1076 const struct IPv4UdpAddress *u4;
1077 const struct IPv6UdpAddress *u6;
1078 uint16_t port;
1079
1080 if (addrlen == sizeof (struct IPv6UdpAddress))
1081 {
1082 u6 = addr;
1083 memset (&a6, 0, sizeof (a6));
1084 a6.sin6_family = AF_INET6;
1085#if HAVE_SOCKADDR_IN_SIN_LEN
1086 a6.sin6_len = sizeof (a6);
1087#endif
1088 a6.sin6_port = u6->u6_port;
1089 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1090 port = ntohs (u6->u6_port);
1091 sb = &a6;
1092 sbs = sizeof (a6);
1093 }
1094 else if (addrlen == sizeof (struct IPv4UdpAddress))
1095 {
1096 u4 = addr;
1097 memset (&a4, 0, sizeof (a4));
1098 a4.sin_family = AF_INET;
1099#if HAVE_SOCKADDR_IN_SIN_LEN
1100 a4.sin_len = sizeof (a4);
1101#endif
1102 a4.sin_port = u4->u4_port;
1103 a4.sin_addr.s_addr = u4->ipv4_addr;
1104 port = ntohs (u4->u4_port);
1105 sb = &a4;
1106 sbs = sizeof (a4);
1107 }
1108 else
1109 {
1110 /* invalid address */
1111 GNUNET_break_op (0);
1112 asc (asc_cls, NULL);
1113 return;
1114 }
1115 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1116 ppc->asc = asc;
1117 ppc->asc_cls = asc_cls;
1118 ppc->port = port;
1119 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1120}
1121
1122
1123/**
1124 * Our external IP address/port mapping has changed.
1125 *
1126 * @param cls closure, the 'struct LocalAddrList'
1127 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1128 * the previous (now invalid) one
1129 * @param addr either the previous or the new public IP address
1130 * @param addrlen actual lenght of the address
1131 */
1132static void
1133udp_nat_port_map_callback (void *cls, int add_remove,
1134 const struct sockaddr *addr, socklen_t addrlen)
1135{
1136 struct Plugin *plugin = cls;
1137 struct IPv4UdpAddress u4;
1138 struct IPv6UdpAddress u6;
1139 void *arg;
1140 size_t args;
1141
1142 /* convert 'addr' to our internal format */
1143 switch (addr->sa_family)
1144 {
1145 case AF_INET:
1146 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1147 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1148 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1149 arg = &u4;
1150 args = sizeof (u4);
1151 break;
1152 case AF_INET6:
1153 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1154 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1155 sizeof (struct in6_addr));
1156 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1157 arg = &u6;
1158 args = sizeof (u6);
1159 break;
1160 default:
1161 GNUNET_break (0);
1162 return;
1163 }
1164 /* modify our published address list */
1165 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1166}
1167
1168
1169/**
1170 * The exported method. Makes the core api available via a global and
1171 * returns the udp transport API.
1172 *
1173 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1174 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1175 */
1176void *
1177libgnunet_plugin_transport_udp_init (void *cls)
1178{
1179 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1180 unsigned long long port;
1181 unsigned long long aport;
1182 struct GNUNET_TRANSPORT_PluginFunctions *api;
1183 struct Plugin *plugin;
1184 int sockets_created;
1185 struct sockaddr_in serverAddrv4;
1186 struct sockaddr_in6 serverAddrv6;
1187 struct sockaddr *serverAddr;
1188 struct sockaddr *addrs[2];
1189 socklen_t addrlens[2];
1190 socklen_t addrlen;
1191 unsigned int tries;
1192 unsigned long long udp_max_bps;
1193
1194 if (GNUNET_OK !=
1195 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1196 &port))
1197 port = 2086;
1198 if (GNUNET_OK !=
1199 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1200 "MAX_BPS", &udp_max_bps))
1201 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1202 if (GNUNET_OK !=
1203 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1204 "ADVERTISED_PORT", &aport))
1205 aport = port;
1206 if (port > 65535)
1207 {
1208 LOG (GNUNET_ERROR_TYPE_WARNING,
1209 _("Given `%s' option is out of range: %llu > %u\n"), "PORT",
1210 port, 65535);
1211 return NULL;
1212 }
1213 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1214 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1215
1216 plugin = GNUNET_malloc (sizeof (struct Plugin));
1217 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1218 GNUNET_BANDWIDTH_value_init ((uint32_t)
1219 udp_max_bps), 30);
1220 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1221 plugin->port = port;
1222 plugin->aport = aport;
1223 plugin->env = env;
1224 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1225 api->cls = plugin;
1226
1227 api->send = &udp_plugin_send;
1228 api->disconnect = &udp_disconnect;
1229 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1230 api->address_to_string = &udp_address_to_string;
1231 api->check_address = &udp_plugin_check_address;
1232
1233 if (GNUNET_YES ==
1234 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1235 "BINDTO", &plugin->bind4_address))
1236 {
1237 LOG (GNUNET_ERROR_TYPE_DEBUG,
1238 "Binding udp plugin to specific address: `%s'\n",
1239 plugin->bind4_address);
1240 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
1241 {
1242 GNUNET_free (plugin->bind4_address);
1243 GNUNET_free (plugin);
1244 GNUNET_free (api);
1245 return NULL;
1246 }
1247 }
1248
1249 if (GNUNET_YES ==
1250 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1251 "BINDTO6", &plugin->bind6_address))
1252 {
1253 LOG (GNUNET_ERROR_TYPE_DEBUG,
1254 "Binding udp plugin to specific address: `%s'\n",
1255 plugin->bind6_address);
1256 if (1 !=
1257 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1258 {
1259 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1260 plugin->bind6_address);
1261 GNUNET_free_non_null (plugin->bind4_address);
1262 GNUNET_free (plugin->bind6_address);
1263 GNUNET_free (plugin);
1264 GNUNET_free (api);
1265 return NULL;
1266 }
1267 }
1268
1269 plugin->defrags =
1270 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1271 plugin->sessions =
1272 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1273 * 2);
1274 plugin->inbound_sessions =
1275 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1276 * 2);
1277 sockets_created = 0;
1278 if ((GNUNET_YES !=
1279 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
1280 "DISABLEV6")))
1281 {
1282 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1283 if (NULL == plugin->sockv6)
1284 {
1285 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1286 }
1287 else
1288 {
1289#if HAVE_SOCKADDR_IN_SIN_LEN
1290 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1291#endif
1292 serverAddrv6.sin6_family = AF_INET6;
1293 serverAddrv6.sin6_addr = in6addr_any;
1294 serverAddrv6.sin6_port = htons (plugin->port);
1295 addrlen = sizeof (serverAddrv6);
1296 serverAddr = (struct sockaddr *) &serverAddrv6;
1297#if DEBUG_UDP
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1299 ntohs (serverAddrv6.sin6_port));
1300#endif
1301 tries = 0;
1302 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1303 GNUNET_OK)
1304 {
1305 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1306#if DEBUG_UDP
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "IPv6 Binding failed, trying new port %d\n",
1309 ntohs (serverAddrv6.sin6_port));
1310#endif
1311 tries++;
1312 if (tries > 10)
1313 {
1314 GNUNET_NETWORK_socket_close (plugin->sockv6);
1315 plugin->sockv6 = NULL;
1316 break;
1317 }
1318 }
1319 if (plugin->sockv6 != NULL)
1320 {
1321 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
1322 addrlens[sockets_created] = sizeof (serverAddrv6);
1323 sockets_created++;
1324 }
1325 }
1326 }
1327
1328 plugin->mst =
1329 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1330 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1331 if (NULL == plugin->sockv4)
1332 {
1333 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1334 }
1335 else
1336 {
1337#if HAVE_SOCKADDR_IN_SIN_LEN
1338 serverAddrv4.sin_len = sizeof (serverAddrv4);
1339#endif
1340 serverAddrv4.sin_family = AF_INET;
1341 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1342 serverAddrv4.sin_port = htons (plugin->port);
1343 addrlen = sizeof (serverAddrv4);
1344 serverAddr = (struct sockaddr *) &serverAddrv4;
1345#if DEBUG_UDP
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1347 ntohs (serverAddrv4.sin_port));
1348#endif
1349 tries = 0;
1350 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1351 GNUNET_OK)
1352 {
1353 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1354#if DEBUG_UDP
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "IPv4 Binding failed, trying new port %d\n",
1357 ntohs (serverAddrv4.sin_port));
1358#endif
1359 tries++;
1360 if (tries > 10)
1361 {
1362 GNUNET_NETWORK_socket_close (plugin->sockv4);
1363 plugin->sockv4 = NULL;
1364 break;
1365 }
1366 }
1367 if (plugin->sockv4 != NULL)
1368 {
1369 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
1370 addrlens[sockets_created] = sizeof (serverAddrv4);
1371 sockets_created++;
1372 }
1373 }
1374
1375 plugin->rs = GNUNET_NETWORK_fdset_create ();
1376 GNUNET_NETWORK_fdset_zero (plugin->rs);
1377 if (NULL != plugin->sockv4)
1378 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1379 if (NULL != plugin->sockv6)
1380 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1381
1382 plugin->select_task =
1383 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1384 GNUNET_SCHEDULER_NO_TASK,
1385 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1386 NULL, &udp_plugin_select, plugin);
1387 if (sockets_created == 0)
1388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1389 plugin->nat =
1390 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
1391 (const struct sockaddr **) addrs, addrlens,
1392 &udp_nat_port_map_callback, NULL, plugin);
1393 return api;
1394}
1395
1396/**
1397 * Shutdown the plugin.
1398 *
1399 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
1400 * @return NULL
1401 */
1402void *
1403libgnunet_plugin_transport_udp_done (void *cls)
1404{
1405 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1406 struct Plugin *plugin = api->cls;
1407 struct ReceiveContext *rc;
1408
1409 /* FIXME: clean up heap and hashmap */
1410 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
1411 NULL);
1412 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1413 plugin->sessions = NULL;
1414 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions, &destroy_inbound_session,
1415 NULL);
1416 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
1417 plugin->inbound_sessions = NULL;
1418 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
1419 {
1420 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1421 GNUNET_free (rc);
1422 }
1423 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
1424
1425 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1426 {
1427 GNUNET_SCHEDULER_cancel (plugin->select_task);
1428 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1429 }
1430 if (plugin->sockv4 != NULL)
1431 {
1432 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1433 plugin->sockv4 = NULL;
1434 }
1435 if (plugin->sockv6 != NULL)
1436 {
1437 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1438 plugin->sockv6 = NULL;
1439 }
1440 GNUNET_SERVER_mst_destroy (plugin->mst);
1441 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1442 GNUNET_NAT_unregister (plugin->nat);
1443 plugin->nat = NULL;
1444 GNUNET_free (plugin);
1445 GNUNET_free (api);
1446 return NULL;
1447}
1448
1449/* end of plugin_transport_udp.c */