2 This file is part of GNUnet
3 (C) 2010, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/plugin_transport_udp.c
23 * @brief Implementation of the UDP NAT punching
25 * @author Christian Grothoff
26 * @author Nathan Evans
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"
42 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
45 #define DEBUG_UDP GNUNET_EXTRA_LOGGING
48 * MTU for fragmentation subsystem. Should be conservative since
49 * all communicating peers MUST work with this MTU.
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.
61 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
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).
70 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
74 * UDP Message-Packet header (after defragmentation).
81 struct GNUNET_MessageHeader header;
84 * Always zero for now.
89 * What is the identity of the sender
91 struct GNUNET_PeerIdentity sender;
97 * UDP ACK Message-Packet header (after defragmentation).
99 struct UDP_ACK_Message
104 struct GNUNET_MessageHeader header;
107 * Desired delay for flow control
112 * What is the identity of the sender
114 struct GNUNET_PeerIdentity sender;
118 struct UDP_Beacon_Message
123 struct GNUNET_MessageHeader header;
126 * What is the identity of the sender
128 struct GNUNET_PeerIdentity sender;
133 * Network format for IPv4 addresses.
135 struct IPv4UdpAddress
138 * IPv4 address, in network byte order.
140 uint32_t ipv4_addr GNUNET_PACKED;
143 * Port number, in network byte order.
145 uint16_t u4_port GNUNET_PACKED;
150 * Network format for IPv6 addresses.
152 struct IPv6UdpAddress
158 struct in6_addr ipv6_addr GNUNET_PACKED;
161 * Port number, in network byte order.
163 uint16_t u6_port GNUNET_PACKED;
167 /* Forward definition */
172 * Session with another peer. FIXME: why not make this into
173 * a regular 'struct Session' and pass it around!?
179 * Which peer is this session for?
181 struct GNUNET_PeerIdentity target;
184 * Pointer to the global plugin struct.
186 struct Plugin *plugin;
189 * Address of the other peer
191 const struct sockaddr *sock_addr;
196 * ATS network type in NBO
198 uint32_t ats_address_network_type;
201 * Function to call upon completion of the transmission.
203 GNUNET_TRANSPORT_TransmitContinuation cont;
206 * Closure for 'cont'.
211 * Current outgoing message to this peer.
213 struct GNUNET_FRAGMENT_Context *frag;
215 struct GNUNET_TIME_Absolute valid_until;
217 GNUNET_SCHEDULER_TaskIdentifier invalidation_task;
219 GNUNET_SCHEDULER_TaskIdentifier delayed_cont_task;
222 * Desired delay for next sending we send to other peer
224 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
227 * Desired delay for next sending we received from other peer
229 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
234 * Data structure to track defragmentation contexts based
235 * on the source of the UDP traffic.
237 struct ReceiveContext
241 * Defragmentation context.
243 struct GNUNET_DEFRAGMENT_Context *defrag;
246 * Source address this receive context is for (allocated at the
247 * end of the struct).
249 const struct sockaddr *src_addr;
252 * Reference to master plugin struct.
254 struct Plugin *plugin;
257 * Node in the defrag heap.
259 struct GNUNET_CONTAINER_HeapNode *hnode;
262 * Length of 'src_addr'
266 struct GNUNET_PeerIdentity id;
270 struct BroadcastAddress
272 struct BroadcastAddress *next;
273 struct BroadcastAddress *prev;
281 * Encapsulation of all of the state of the plugin.
289 struct GNUNET_TRANSPORT_PluginEnvironment *env;
292 * Session of peers with whom we are currently connected,
293 * map of peer identity to 'struct PeerSession'.
295 struct GNUNET_CONTAINER_MultiHashMap *sessions;
298 * Session of peers with whom we are currently connected,
299 * map of peer identity to 'struct PeerSession'.
301 struct GNUNET_CONTAINER_MultiHashMap *inbound_sessions;
304 * Heap with all of our defragmentation activities.
306 struct GNUNET_CONTAINER_Heap *defrags;
311 GNUNET_SCHEDULER_TaskIdentifier select_task;
314 * Tokenizer for inbound messages.
316 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
319 * Bandwidth tracker to limit global UDP traffic.
321 struct GNUNET_BANDWIDTH_Tracker tracker;
324 * Address we were told to bind to exclusively (IPv4).
329 * Address we were told to bind to exclusively (IPv6).
334 * Handle to NAT traversal support.
336 struct GNUNET_NAT_Handle *nat;
341 struct GNUNET_NETWORK_FDSet *rs;
344 * The read socket for IPv4
346 struct GNUNET_NETWORK_Handle *sockv4;
349 * The read socket for IPv6
351 struct GNUNET_NETWORK_Handle *sockv6;
354 * Beacon broadcasting
355 * -------------------
361 struct GNUNET_TIME_Relative broadcast_interval;
364 * Broadcast with IPv4
369 * Broadcast with IPv6
375 * Tokenizer for inbound messages.
377 struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv6_mst;
378 struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv4_mst;
381 * ID of select broadcast task
383 GNUNET_SCHEDULER_TaskIdentifier send_ipv4_broadcast_task;
386 * ID of select broadcast task
388 GNUNET_SCHEDULER_TaskIdentifier send_ipv6_broadcast_task;
391 * IPv6 multicast address
393 struct sockaddr_in6 ipv6_multicast_address;
396 * DLL of IPv4 broadcast addresses
398 struct BroadcastAddress *ipv4_broadcast_tail;
399 struct BroadcastAddress *ipv4_broadcast_head;
403 * expected delay for ACKs
405 struct GNUNET_TIME_Relative last_expected_delay;
408 * Port we broadcasting on.
410 uint16_t broadcast_port;
418 * Port we advertise on.
424 struct PeerSessionIteratorContext
426 struct Session *result;
433 * Lookup the session for the given peer.
435 * @param plugin the plugin
436 * @param peer peer's identity
437 * @return NULL if we have no session
439 static struct Session *
440 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
442 return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
448 inbound_session_iterator (void *cls, const GNUNET_HashCode * key, void *value)
450 struct PeerSessionIteratorContext *psc = cls;
451 struct Session *s = value;
453 if (s->addrlen == psc->addrlen)
455 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
458 if (psc->result != NULL)
465 * Lookup the session for the given peer.
467 * @param plugin the plugin
468 * @param peer peer's identity
469 * @param addr address
470 * @param addrlen address length
471 * @return NULL if we have no session
473 static struct Session *
474 find_inbound_session (struct Plugin *plugin,
475 const struct GNUNET_PeerIdentity *peer, const void *addr,
478 struct PeerSessionIteratorContext psc;
481 psc.addrlen = addrlen;
484 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
486 &inbound_session_iterator, &psc);
492 inbound_session_by_addr_iterator (void *cls, const GNUNET_HashCode * key,
495 struct PeerSessionIteratorContext *psc = cls;
496 struct Session *s = value;
498 if (s->addrlen == psc->addrlen)
500 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
503 if (psc->result != NULL)
510 * Lookup the session for the given peer just by address.
512 * @param plugin the plugin
513 * @param addr address
514 * @param addrlen address length
515 * @return NULL if we have no session
517 static struct Session *
518 find_inbound_session_by_addr (struct Plugin *plugin, const void *addr,
521 struct PeerSessionIteratorContext psc;
524 psc.addrlen = addrlen;
527 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
528 &inbound_session_by_addr_iterator,
535 * Destroy a session, plugin is being unloaded.
538 * @param key hash of public key of target peer
539 * @param value a 'struct PeerSession*' to clean up
540 * @return GNUNET_OK (continue to iterate)
543 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
545 struct Session *peer_session = value;
547 GNUNET_assert (GNUNET_YES ==
548 GNUNET_CONTAINER_multihashmap_remove (peer_session->
553 if (peer_session->frag != NULL)
554 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
555 if (GNUNET_SCHEDULER_NO_TASK != peer_session->delayed_cont_task)
556 GNUNET_SCHEDULER_cancel (peer_session->delayed_cont_task);
557 GNUNET_free (peer_session);
563 * Destroy a session, plugin is being unloaded.
566 * @param key hash of public key of target peer
567 * @param value a 'struct PeerSession*' to clean up
568 * @return GNUNET_OK (continue to iterate)
571 destroy_inbound_session (void *cls, const GNUNET_HashCode * key, void *value)
573 struct Session *s = value;
575 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
576 GNUNET_SCHEDULER_cancel (s->invalidation_task);
577 if (GNUNET_SCHEDULER_NO_TASK != s->delayed_cont_task)
578 GNUNET_SCHEDULER_cancel (s->delayed_cont_task);
579 GNUNET_CONTAINER_multihashmap_remove (s->plugin->inbound_sessions,
580 &s->target.hashPubKey, s);
587 * Disconnect from a remote node. Clean up session if we have one for this peer
589 * @param cls closure for this call (should be handle to Plugin)
590 * @param target the peeridentity of the peer to disconnect
591 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
594 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
596 struct Plugin *plugin = cls;
597 struct Session *session;
599 session = find_session (plugin, target);
602 GNUNET_assert (GNUNET_OK ==
603 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
607 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
609 &destroy_inbound_session, NULL);
610 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
611 if (GNUNET_SCHEDULER_NO_TASK != session->delayed_cont_task)
612 GNUNET_SCHEDULER_cancel (session->delayed_cont_task);
613 if (session->cont != NULL)
614 session->cont (session->cont_cls, target, GNUNET_SYSERR);
615 GNUNET_free (session);
620 * Actually send out the message.
622 * @param plugin the plugin
623 * @param sa the address to send the message to
624 * @param msg message to transmit
625 * @return the number of bytes written
628 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
629 const struct GNUNET_MessageHeader *msg)
634 switch (sa->sa_family)
637 if (NULL == plugin->sockv4)
640 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
641 sa, slen = sizeof (struct sockaddr_in));
644 if (NULL == plugin->sockv6)
647 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
648 sa, slen = sizeof (struct sockaddr_in6));
654 if (GNUNET_SYSERR == sent)
656 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
657 LOG (GNUNET_ERROR_TYPE_ERROR,
658 "UDP transmited %u-byte message to %s (%d: %s)\n",
659 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
660 (sent < 0) ? STRERROR (errno) : "ok");
663 LOG (GNUNET_ERROR_TYPE_DEBUG,
664 "UDP transmited %u-byte message to %s (%d: %s)\n",
665 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
666 (sent < 0) ? STRERROR (errno) : "ok");
672 * Function that is called with messages created by the fragmentation
673 * module. In the case of the 'proc' callback of the
674 * GNUNET_FRAGMENT_context_create function, this function must
675 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
677 * @param cls closure, the 'struct PeerSession'
678 * @param msg the message that was created
681 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
683 struct Session *session = cls;
685 udp_send (session->plugin, session->sock_addr, msg);
686 GNUNET_FRAGMENT_context_transmission_done (session->frag);
690 static struct Session *
691 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
692 const void *addr, size_t addrlen,
693 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
695 struct Session *peer_session;
696 const struct IPv4UdpAddress *t4;
697 const struct IPv6UdpAddress *t6;
698 struct sockaddr_in *v4;
699 struct sockaddr_in6 *v6;
701 struct GNUNET_ATS_Information ats;
705 case sizeof (struct IPv4UdpAddress):
706 if (NULL == plugin->sockv4)
712 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
713 len = sizeof (struct sockaddr_in);
714 v4 = (struct sockaddr_in *) &peer_session[1];
715 v4->sin_family = AF_INET;
716 #if HAVE_SOCKADDR_IN_SIN_LEN
717 v4->sin_len = sizeof (struct sockaddr_in);
719 v4->sin_port = t4->u4_port;
720 v4->sin_addr.s_addr = t4->ipv4_addr;
721 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
723 case sizeof (struct IPv6UdpAddress):
724 if (NULL == plugin->sockv6)
730 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
731 len = sizeof (struct sockaddr_in6);
732 v6 = (struct sockaddr_in6 *) &peer_session[1];
733 v6->sin6_family = AF_INET6;
734 #if HAVE_SOCKADDR_IN_SIN_LEN
735 v6->sin6_len = sizeof (struct sockaddr_in6);
737 v6->sin6_port = t6->u6_port;
738 v6->sin6_addr = t6->ipv6_addr;
739 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
742 /* Must have a valid address to send to */
747 peer_session->ats_address_network_type = ats.value;
748 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
749 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
750 peer_session->addrlen = len;
751 peer_session->target = *target;
752 peer_session->plugin = plugin;
753 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
754 peer_session->cont = cont;
755 peer_session->cont_cls = cont_cls;
761 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
765 udp_call_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
767 struct Session *s = cls;
768 GNUNET_TRANSPORT_TransmitContinuation cont = s->cont;
770 s->delayed_cont_task = GNUNET_SCHEDULER_NO_TASK;
772 cont (s->cont_cls, &s->target, GNUNET_OK);
777 * Function that can be used by the transport service to transmit
778 * a message using the plugin.
781 * @param target who should receive this message (ignored by UDP)
782 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
783 * @param msgbuf_size the size of the msgbuf to send
784 * @param priority how important is the message (ignored by UDP)
785 * @param timeout when should we time out (give up) if we can not transmit?
786 * @param session identifier used for this session (NULL for UDP)
787 * @param addr the addr to send the message to
788 * @param addrlen the len of addr
789 * @param force_address not used, we had better have an address to send to
790 * because we are stateless!!
791 * @param cont continuation to call once the message has
792 * been transmitted (or if the transport is ready
793 * for the next transmission call; or if the
794 * peer disconnected...)
795 * @param cont_cls closure for cont
797 * @return the number of bytes written (may return 0 and the message can
798 * still be transmitted later!)
801 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
802 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
803 struct GNUNET_TIME_Relative timeout, struct Session *session,
804 const void *addr, size_t addrlen, int force_address,
805 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
807 struct Plugin *plugin = cls;
808 struct Session *peer_session;
810 const struct IPv4UdpAddress *t4;
811 const struct IPv6UdpAddress *t6;
812 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
814 struct UDPMessage *udp;
815 struct GNUNET_TIME_Relative delta;
817 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
820 return GNUNET_SYSERR;
823 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
825 msgbuf_size, GNUNET_i2s (target), udp_address_to_string (NULL, addr,
827 session, force_address);
829 if ((force_address == GNUNET_SYSERR) && (session == NULL))
830 return GNUNET_SYSERR;
833 /* safety check: comparing address to address stored in session */
834 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
837 GNUNET_assert (GNUNET_YES ==
838 GNUNET_CONTAINER_multihashmap_contains_value
839 (plugin->inbound_sessions, &target->hashPubKey, s));
841 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
842 return GNUNET_SYSERR;
845 case sizeof (struct IPv4UdpAddress):
846 if (NULL == plugin->sockv4)
849 cont (cont_cls, target, GNUNET_SYSERR);
850 return GNUNET_SYSERR;
853 if (s->addrlen != (sizeof (struct sockaddr_in)))
854 return GNUNET_SYSERR;
855 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
857 GNUNET_assert (a4->sin_port == t4->u4_port);
859 memcmp (&a4->sin_addr, &t4->ipv4_addr,
860 sizeof (struct in_addr)));
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
864 case sizeof (struct IPv6UdpAddress):
865 if (NULL == plugin->sockv6)
868 cont (cont_cls, target, GNUNET_SYSERR);
869 return GNUNET_SYSERR;
872 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
873 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
875 GNUNET_assert (a6->sin6_port == t6->u6_port);
877 memcmp (&a6->sin6_addr, &t6->ipv6_addr,
878 sizeof (struct in6_addr)));
879 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
883 /* Must have a valid address to send to */
888 if ((addr == NULL) || (addrlen == 0))
889 return GNUNET_SYSERR;
890 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
891 if (peer_session == NULL)
894 cont (cont_cls, target, GNUNET_SYSERR);
895 return GNUNET_SYSERR;;
899 udp = (struct UDPMessage *) mbuf;
900 udp->header.size = htons (mlen);
901 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
902 udp->reserved = htonl (0);
903 udp->sender = *plugin->env->my_identity;
904 memcpy (&udp[1], msgbuf, msgbuf_size);
907 delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
909 delta = GNUNET_TIME_UNIT_ZERO;
912 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
915 if ((delta.rel_value > 0) && (mlen > 0))
918 s->cont_cls = cont_cls;
919 s->delayed_cont_task =
920 GNUNET_SCHEDULER_add_delayed (delta, &udp_call_continuation, s);
923 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
925 GNUNET_free_non_null (peer_session);
929 GNUNET_assert (GNUNET_OK ==
930 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
933 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
935 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
937 plugin->last_expected_delay,
938 &udp->header, &send_fragment,
946 * Closure for 'process_inbound_tokenized_messages'
948 struct SourceInformation
953 struct GNUNET_PeerIdentity sender;
961 * Number of bytes in source address.
965 struct Session *session;
970 * Message tokenizer has broken up an incomming message. Pass it on
973 * @param cls the 'struct Plugin'
974 * @param client the 'struct SourceInformation'
975 * @param hdr the actual message
978 process_inbound_tokenized_messages (void *cls, void *client,
979 const struct GNUNET_MessageHeader *hdr)
981 struct Plugin *plugin = cls;
982 struct SourceInformation *si = client;
983 struct GNUNET_ATS_Information atsi[2];
984 struct GNUNET_TIME_Relative delay;
987 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
988 atsi[0].value = htonl (1);
989 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
990 atsi[1].value = si->session->ats_address_network_type;
991 GNUNET_break (ntohl(si->session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
994 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving Session %X %s to transport\n",
995 si->session, GNUNET_i2s (&si->session->target));
997 plugin->env->receive (plugin->env->cls, &si->sender, hdr,
998 (const struct GNUNET_ATS_Information *) &atsi, 2,
999 si->session, si->arg, si->args);
1000 si->session->flow_delay_for_other_peer = delay;
1004 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1006 struct Session *s = cls;
1008 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1009 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %X (`%s') is now invalid\n", s,
1010 GNUNET_a2s (s->sock_addr, s->addrlen));
1012 s->plugin->env->session_end (s->plugin->env->cls, &s->target, s);
1013 GNUNET_assert (GNUNET_YES ==
1014 GNUNET_CONTAINER_multihashmap_remove (s->
1015 plugin->inbound_sessions,
1016 &s->target.hashPubKey,
1023 * We've received a UDP Message. Process it (pass contents to main service).
1025 * @param plugin plugin context
1026 * @param msg the message
1027 * @param sender_addr sender address
1028 * @param sender_addr_len number of bytes in sender_addr
1031 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1032 const struct sockaddr *sender_addr,
1033 socklen_t sender_addr_len)
1035 struct SourceInformation si;
1036 struct IPv4UdpAddress u4;
1037 struct IPv6UdpAddress u6;
1038 struct GNUNET_ATS_Information ats;
1042 if (0 != ntohl (msg->reserved))
1044 GNUNET_break_op (0);
1047 if (ntohs (msg->header.size) <
1048 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1050 GNUNET_break_op (0);
1054 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1055 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1056 /* convert address */
1057 switch (sender_addr->sa_family)
1060 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1061 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1062 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1067 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1068 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1069 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1078 LOG (GNUNET_ERROR_TYPE_DEBUG,
1079 "Received message with %u bytes from peer `%s' at `%s'\n",
1080 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1081 GNUNET_a2s (sender_addr, sender_addr_len));
1084 /* create a session for inbound connections */
1085 const struct UDPMessage *udp_msg = (const struct UDPMessage *) msg;
1087 LOG (GNUNET_ERROR_TYPE_DEBUG,
1088 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
1089 GNUNET_i2s (&udp_msg->sender), udp_address_to_string (NULL, arg, args));
1091 struct Session *s = NULL;
1093 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr,
1098 LOG (GNUNET_ERROR_TYPE_DEBUG,
1099 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
1100 s, GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1104 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
1105 ats = plugin->env->get_address_type (plugin->env->cls, sender_addr, sender_addr_len);
1106 s->ats_address_network_type = ats.value;
1107 LOG (GNUNET_ERROR_TYPE_DEBUG,
1108 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n", s,
1109 GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1111 GNUNET_assert (GNUNET_OK ==
1112 GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
1113 &s->target.hashPubKey, s,
1114 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1117 GNUNET_TIME_relative_to_absolute
1118 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1119 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
1121 GNUNET_SCHEDULER_cancel (s->invalidation_task);
1122 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1123 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rescheduling %X' `%s'\n", s,
1124 udp_address_to_string (NULL, arg, args));
1126 s->invalidation_task =
1127 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1128 &invalidation_task, s);
1129 /* iterate over all embedded messages */
1130 si.sender = msg->sender;
1134 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1135 ntohs (msg->header.size) -
1136 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1141 * Process a defragmented message.
1143 * @param cls the 'struct ReceiveContext'
1144 * @param msg the message
1147 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1149 struct ReceiveContext *rc = cls;
1151 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1156 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1161 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1162 rc->src_addr, rc->addr_len);
1167 * Transmit an acknowledgement.
1169 * @param cls the 'struct ReceiveContext'
1170 * @param id message ID (unused)
1171 * @param msg ack to transmit
1174 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1176 struct ReceiveContext *rc = cls;
1178 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1180 struct UDP_ACK_Message *udp_ack;
1185 s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
1188 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1189 delay = s->flow_delay_for_other_peer.rel_value;
1196 LOG (GNUNET_ERROR_TYPE_DEBUG,
1197 "Sending ACK to `%s' including delay of %u ms\n",
1198 GNUNET_a2s (rc->src_addr,
1199 (rc->src_addr->sa_family ==
1200 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1204 udp_ack = (struct UDP_ACK_Message *) buf;
1205 udp_ack->header.size = htons ((uint16_t) msize);
1206 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1207 udp_ack->delay = htonl (delay);
1208 udp_ack->sender = *rc->plugin->env->my_identity;
1209 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1210 (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
1215 * Closure for 'find_receive_context'.
1217 struct FindReceiveContext
1220 * Where to store the result.
1222 struct ReceiveContext *rc;
1227 const struct sockaddr *addr;
1230 * Number of bytes in 'addr'.
1234 struct Session *session;
1239 * Scan the heap for a receive context with the given address.
1241 * @param cls the 'struct FindReceiveContext'
1242 * @param node internal node of the heap
1243 * @param element value stored at the node (a 'struct ReceiveContext')
1244 * @param cost cost associated with the node
1245 * @return GNUNET_YES if we should continue to iterate,
1249 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1250 void *element, GNUNET_CONTAINER_HeapCostType cost)
1252 struct FindReceiveContext *frc = cls;
1253 struct ReceiveContext *e = element;
1255 if ((frc->addr_len == e->addr_len) &&
1256 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1266 struct Plugin *plugin;
1268 struct IPv4UdpAddress addr;
1270 * ATS network type in NBO
1272 uint32_t ats_address_network_type;
1277 struct Plugin *plugin;
1279 struct IPv6UdpAddress addr;
1281 * ATS network type in NBO
1283 uint32_t ats_address_network_type;
1288 * Read and process a message from the given socket.
1290 * @param plugin the overall plugin
1291 * @param rsock socket to read from
1294 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1300 const struct GNUNET_MessageHeader *msg;
1301 const struct GNUNET_MessageHeader *ack;
1302 struct Session *peer_session;
1303 const struct UDP_ACK_Message *udp_ack;
1304 struct ReceiveContext *rc;
1305 struct GNUNET_TIME_Absolute now;
1306 struct FindReceiveContext frc;
1307 struct Session *s = NULL;
1308 struct GNUNET_TIME_Relative flow_delay;
1309 struct GNUNET_ATS_Information ats;
1311 fromlen = sizeof (addr);
1312 memset (&addr, 0, sizeof (addr));
1314 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1315 (struct sockaddr *) &addr, &fromlen);
1316 if (ret < sizeof (struct GNUNET_MessageHeader))
1318 GNUNET_break_op (0);
1321 msg = (const struct GNUNET_MessageHeader *) buf;
1323 LOG (GNUNET_ERROR_TYPE_DEBUG,
1324 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1325 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1327 if (ret != ntohs (msg->size))
1329 GNUNET_break_op (0);
1332 switch (ntohs (msg->type))
1334 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1336 if (fromlen == sizeof (struct sockaddr_in))
1338 LOG (GNUNET_ERROR_TYPE_DEBUG,
1339 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
1340 ret, GNUNET_a2s ((const struct sockaddr *) &addr, fromlen));
1342 struct Mstv4Context *mc;
1344 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
1345 struct sockaddr_in *av4 = (struct sockaddr_in *) &addr;
1347 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
1348 mc->addr.u4_port = av4->sin_port;
1349 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &addr, fromlen);
1350 mc->ats_address_network_type = ats.value;
1352 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, ret,
1353 GNUNET_NO, GNUNET_NO))
1356 else if (fromlen == sizeof (struct sockaddr_in6))
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
1360 ret, GNUNET_a2s ((const struct sockaddr *) &addr, fromlen));
1362 struct Mstv6Context *mc;
1364 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
1365 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) &addr;
1367 mc->addr.ipv6_addr = av6->sin6_addr;
1368 mc->addr.u6_port = av6->sin6_port;
1369 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &addr, fromlen);
1370 mc->ats_address_network_type = ats.value;
1373 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, ret,
1374 GNUNET_NO, GNUNET_NO))
1379 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1380 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1382 GNUNET_break_op (0);
1385 process_udp_message (plugin, (const struct UDPMessage *) msg,
1386 (const struct sockaddr *) addr, fromlen);
1388 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1390 if (ntohs (msg->size) <
1391 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1393 GNUNET_break_op (0);
1396 udp_ack = (const struct UDP_ACK_Message *) msg;
1397 s = find_inbound_session (plugin, &udp_ack->sender, addr, fromlen);
1400 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1403 flow_delay.rel_value);
1405 s->flow_delay_from_other_peer =
1406 GNUNET_TIME_relative_to_absolute (flow_delay);
1408 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1409 if (ntohs (ack->size) !=
1410 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1412 GNUNET_break_op (0);
1416 LOG (GNUNET_ERROR_TYPE_DEBUG,
1417 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1418 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1419 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1422 peer_session = find_session (plugin, &udp_ack->sender);
1423 if (NULL == peer_session)
1426 LOG (GNUNET_ERROR_TYPE_DEBUG,
1427 "Session for ACK not found, dropping ACK!\n");
1431 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1433 GNUNET_assert (GNUNET_OK ==
1434 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1438 plugin->last_expected_delay =
1439 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1440 if (peer_session->cont != NULL)
1441 peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
1442 GNUNET_free (peer_session);
1444 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1446 frc.addr = (const struct sockaddr *) addr;
1447 frc.addr_len = fromlen;
1448 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1450 now = GNUNET_TIME_absolute_get ();
1454 /* need to create a new RC */
1455 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1456 memcpy (&rc[1], addr, fromlen);
1457 rc->src_addr = (const struct sockaddr *) &rc[1];
1458 rc->addr_len = fromlen;
1459 rc->plugin = plugin;
1461 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1462 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1463 &fragment_msg_proc, &ack_proc);
1465 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1466 (GNUNET_CONTAINER_HeapCostType)
1470 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1471 (unsigned int) ntohs (msg->size),
1472 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1475 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1477 /* keep this 'rc' from expiring */
1478 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1479 (GNUNET_CONTAINER_HeapCostType)
1482 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1483 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1485 /* remove 'rc' that was inactive the longest */
1486 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1487 GNUNET_assert (NULL != rc);
1488 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1493 GNUNET_break_op (0);
1500 * We have been notified that our writeset has something to read. We don't
1501 * know which socket needs to be read, so we have to check each one
1502 * Then reschedule this function to be called again once more is available.
1504 * @param cls the plugin handle
1505 * @param tc the scheduling context (for rescheduling this function again)
1508 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1510 struct Plugin *plugin = cls;
1512 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1513 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1515 if ((NULL != plugin->sockv4) &&
1516 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1517 udp_read (plugin, plugin->sockv4);
1518 if ((NULL != plugin->sockv6) &&
1519 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1520 udp_read (plugin, plugin->sockv6);
1521 plugin->select_task =
1522 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1523 GNUNET_SCHEDULER_NO_TASK,
1524 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1525 NULL, &udp_plugin_select, plugin);
1531 broadcast_ipv4_mst_cb (void *cls, void *client,
1532 const struct GNUNET_MessageHeader *message)
1534 struct Plugin *plugin = cls;
1535 struct Mstv4Context *mc = client;
1536 const struct GNUNET_MessageHeader *hello;
1537 struct UDP_Beacon_Message *msg;
1539 msg = (struct UDP_Beacon_Message *) message;
1541 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1542 ntohs (msg->header.type))
1545 LOG (GNUNET_ERROR_TYPE_DEBUG,
1546 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1547 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1548 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1550 struct GNUNET_ATS_Information atsi[2];
1553 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1554 atsi[0].value = htonl (1);
1555 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1556 atsi[1].value = mc->ats_address_network_type;
1557 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
1559 hello = (struct GNUNET_MessageHeader *) &msg[1];
1560 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
1561 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
1562 (const char *) &mc->addr, sizeof (mc->addr));
1564 GNUNET_STATISTICS_update (plugin->env->stats,
1566 ("# IPv4 broadcast HELLO beacons received via udp"),
1573 broadcast_ipv6_mst_cb (void *cls, void *client,
1574 const struct GNUNET_MessageHeader *message)
1577 struct Plugin *plugin = cls;
1578 struct Mstv6Context *mc = client;
1579 const struct GNUNET_MessageHeader *hello;
1580 struct UDP_Beacon_Message *msg;
1582 msg = (struct UDP_Beacon_Message *) message;
1584 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1585 ntohs (msg->header.type))
1588 LOG (GNUNET_ERROR_TYPE_DEBUG,
1589 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1590 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1591 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1593 struct GNUNET_ATS_Information atsi[2];
1596 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1597 atsi[0].value = htonl (1);
1598 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1599 atsi[1].value = mc->ats_address_network_type;
1600 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
1602 hello = (struct GNUNET_MessageHeader *) &msg[1];
1603 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
1604 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
1605 (const char *) &mc->addr, sizeof (mc->addr));
1607 GNUNET_STATISTICS_update (plugin->env->stats,
1609 ("# IPv6 multicast HELLO beacons received via udp"),
1616 udp_ipv4_broadcast_send (void *cls,
1617 const struct GNUNET_SCHEDULER_TaskContext *tc)
1619 struct Plugin *plugin = cls;
1622 uint16_t hello_size;
1625 const struct GNUNET_MessageHeader *hello;
1626 struct UDP_Beacon_Message *msg;
1627 struct BroadcastAddress *baddr;
1629 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1631 hello = plugin->env->get_our_hello ();
1632 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1633 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1635 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1636 (msg_size > (UDP_MTU)))
1639 msg = (struct UDP_Beacon_Message *) buf;
1640 msg->sender = *(plugin->env->my_identity);
1641 msg->header.size = ntohs (msg_size);
1642 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1643 memcpy (&msg[1], hello, hello_size);
1646 baddr = plugin->ipv4_broadcast_head;
1648 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
1650 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
1652 addr->sin_port = htons (plugin->port);
1655 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, msg_size,
1656 (const struct sockaddr *) addr,
1658 if (sent == GNUNET_SYSERR)
1659 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1661 LOG (GNUNET_ERROR_TYPE_DEBUG,
1662 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
1663 GNUNET_a2s (baddr->addr, baddr->addrlen));
1664 baddr = baddr->next;
1667 plugin->send_ipv4_broadcast_task =
1668 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1669 &udp_ipv4_broadcast_send, plugin);
1673 udp_ipv6_broadcast_send (void *cls,
1674 const struct GNUNET_SCHEDULER_TaskContext *tc)
1676 struct Plugin *plugin = cls;
1679 uint16_t hello_size;
1682 const struct GNUNET_MessageHeader *hello;
1683 struct UDP_Beacon_Message *msg;
1685 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1687 hello = plugin->env->get_our_hello ();
1688 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1689 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1691 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1692 (msg_size > (UDP_MTU)))
1695 msg = (struct UDP_Beacon_Message *) buf;
1696 msg->sender = *(plugin->env->my_identity);
1697 msg->header.size = ntohs (msg_size);
1698 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1699 memcpy (&msg[1], hello, hello_size);
1703 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, msg_size,
1704 (const struct sockaddr *)
1705 &plugin->ipv6_multicast_address,
1706 sizeof (struct sockaddr_in6));
1707 if (sent == GNUNET_SYSERR)
1708 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1710 LOG (GNUNET_ERROR_TYPE_DEBUG,
1711 "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
1713 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
1714 sizeof (struct sockaddr_in6)));
1718 plugin->send_ipv6_broadcast_task =
1719 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1720 &udp_ipv6_broadcast_send, plugin);
1724 * Check if the given port is plausible (must be either our listen
1725 * port or our advertised port). If it is neither, we return
1728 * @param plugin global variables
1729 * @param in_port port number to check
1730 * @return GNUNET_OK if port is either open_port or adv_port
1733 check_port (struct Plugin *plugin, uint16_t in_port)
1735 if ((in_port == plugin->port) || (in_port == plugin->aport))
1737 return GNUNET_SYSERR;
1742 * Function that will be called to check if a binary address for this
1743 * plugin is well-formed and corresponds to an address for THIS peer
1744 * (as per our configuration). Naturally, if absolutely necessary,
1745 * plugins can be a bit conservative in their answer, but in general
1746 * plugins should make sure that the address does not redirect
1747 * traffic to a 3rd party that might try to man-in-the-middle our
1750 * @param cls closure, should be our handle to the Plugin
1751 * @param addr pointer to the address
1752 * @param addrlen length of addr
1753 * @return GNUNET_OK if this is a plausible address for this peer
1754 * and transport, GNUNET_SYSERR if not
1758 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1760 struct Plugin *plugin = cls;
1761 struct IPv4UdpAddress *v4;
1762 struct IPv6UdpAddress *v6;
1764 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1765 (addrlen != sizeof (struct IPv6UdpAddress)))
1767 GNUNET_break_op (0);
1768 return GNUNET_SYSERR;
1770 if (addrlen == sizeof (struct IPv4UdpAddress))
1772 v4 = (struct IPv4UdpAddress *) addr;
1773 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1774 return GNUNET_SYSERR;
1776 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1777 sizeof (struct in_addr)))
1778 return GNUNET_SYSERR;
1782 v6 = (struct IPv6UdpAddress *) addr;
1783 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1785 GNUNET_break_op (0);
1786 return GNUNET_SYSERR;
1788 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1789 return GNUNET_SYSERR;
1791 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1792 sizeof (struct in6_addr)))
1793 return GNUNET_SYSERR;
1800 * Function called for a quick conversion of the binary address to
1801 * a numeric address. Note that the caller must not free the
1802 * address and that the next call to this function is allowed
1803 * to override the address again.
1805 * @param cls closure
1806 * @param addr binary address
1807 * @param addrlen length of the address
1808 * @return string representing the same address
1811 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1813 static char rbuf[INET6_ADDRSTRLEN + 10];
1814 char buf[INET6_ADDRSTRLEN];
1818 const struct IPv4UdpAddress *t4;
1819 const struct IPv6UdpAddress *t6;
1823 if (addrlen == sizeof (struct IPv6UdpAddress))
1827 port = ntohs (t6->u6_port);
1828 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1831 else if (addrlen == sizeof (struct IPv4UdpAddress))
1835 port = ntohs (t4->u4_port);
1836 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1841 GNUNET_break_op (0);
1844 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1845 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1852 * Closure for 'append_port'.
1854 struct PrettyPrinterContext
1857 * Function to call with the result.
1859 GNUNET_TRANSPORT_AddressStringCallback asc;
1862 * Clsoure for 'asc'.
1867 * Port to add after the IP address.
1874 * Append our port and forward the result.
1876 * @param cls a 'struct PrettyPrinterContext'
1877 * @param hostname result from DNS resolver
1880 append_port (void *cls, const char *hostname)
1882 struct PrettyPrinterContext *ppc = cls;
1885 if (hostname == NULL)
1887 ppc->asc (ppc->asc_cls, NULL);
1891 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1892 ppc->asc (ppc->asc_cls, ret);
1898 * Convert the transports address to a nice, human-readable
1901 * @param cls closure
1902 * @param type name of the transport that generated the address
1903 * @param addr one of the addresses of the host, NULL for the last address
1904 * the specific address format depends on the transport
1905 * @param addrlen length of the address
1906 * @param numeric should (IP) addresses be displayed in numeric form?
1907 * @param timeout after how long should we give up?
1908 * @param asc function to call on each string
1909 * @param asc_cls closure for asc
1912 udp_plugin_address_pretty_printer (void *cls, const char *type,
1913 const void *addr, size_t addrlen,
1915 struct GNUNET_TIME_Relative timeout,
1916 GNUNET_TRANSPORT_AddressStringCallback asc,
1919 struct PrettyPrinterContext *ppc;
1922 struct sockaddr_in a4;
1923 struct sockaddr_in6 a6;
1924 const struct IPv4UdpAddress *u4;
1925 const struct IPv6UdpAddress *u6;
1928 if (addrlen == sizeof (struct IPv6UdpAddress))
1931 memset (&a6, 0, sizeof (a6));
1932 a6.sin6_family = AF_INET6;
1933 #if HAVE_SOCKADDR_IN_SIN_LEN
1934 a6.sin6_len = sizeof (a6);
1936 a6.sin6_port = u6->u6_port;
1937 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1938 port = ntohs (u6->u6_port);
1942 else if (addrlen == sizeof (struct IPv4UdpAddress))
1945 memset (&a4, 0, sizeof (a4));
1946 a4.sin_family = AF_INET;
1947 #if HAVE_SOCKADDR_IN_SIN_LEN
1948 a4.sin_len = sizeof (a4);
1950 a4.sin_port = u4->u4_port;
1951 a4.sin_addr.s_addr = u4->ipv4_addr;
1952 port = ntohs (u4->u4_port);
1958 /* invalid address */
1959 GNUNET_break_op (0);
1960 asc (asc_cls, NULL);
1963 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1965 ppc->asc_cls = asc_cls;
1967 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1972 * Our external IP address/port mapping has changed.
1974 * @param cls closure, the 'struct LocalAddrList'
1975 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1976 * the previous (now invalid) one
1977 * @param addr either the previous or the new public IP address
1978 * @param addrlen actual lenght of the address
1981 udp_nat_port_map_callback (void *cls, int add_remove,
1982 const struct sockaddr *addr, socklen_t addrlen)
1984 struct Plugin *plugin = cls;
1985 struct IPv4UdpAddress u4;
1986 struct IPv6UdpAddress u6;
1990 /* convert 'addr' to our internal format */
1991 switch (addr->sa_family)
1994 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1995 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1996 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
2001 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
2002 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
2003 sizeof (struct in6_addr));
2004 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
2012 /* modify our published address list */
2013 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
2018 iface_proc (void *cls, const char *name, int isDefault,
2019 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
2020 const struct sockaddr *netmask, socklen_t addrlen)
2022 struct Plugin *plugin = cls;
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
2027 GNUNET_a2s (addr, addrlen), name, addr);
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2029 "broadcast address %s for interface %s %p\n ",
2030 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
2032 GNUNET_a2s (netmask, addrlen), name, netmask);
2035 /* Collecting broadcast addresses */
2036 if (broadcast_addr != NULL)
2038 struct BroadcastAddress *ba =
2039 GNUNET_malloc (sizeof (struct BroadcastAddress));
2040 ba->addr = GNUNET_malloc (addrlen);
2041 memcpy (ba->addr, broadcast_addr, addrlen);
2042 ba->addrlen = addrlen;
2043 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
2044 plugin->ipv4_broadcast_tail, ba);
2052 * The exported method. Makes the core api available via a global and
2053 * returns the udp transport API.
2055 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2056 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2059 libgnunet_plugin_transport_udp_init (void *cls)
2061 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2062 unsigned long long port;
2063 unsigned long long aport;
2064 struct GNUNET_TRANSPORT_PluginFunctions *api;
2065 struct Plugin *plugin;
2066 int sockets_created;
2068 struct GNUNET_TIME_Relative interval;
2069 struct sockaddr_in serverAddrv4;
2070 struct sockaddr_in6 serverAddrv6;
2071 struct sockaddr *serverAddr;
2072 struct sockaddr *addrs[2];
2073 socklen_t addrlens[2];
2076 unsigned long long udp_max_bps;
2079 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2084 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2086 if (broadcast == GNUNET_SYSERR)
2087 broadcast = GNUNET_NO;
2089 if (GNUNET_SYSERR ==
2090 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
2091 "BROADCAST_INTERVAL", &interval))
2092 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2095 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2096 "MAX_BPS", &udp_max_bps))
2097 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2099 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2100 "ADVERTISED_PORT", &aport))
2104 LOG (GNUNET_ERROR_TYPE_WARNING,
2105 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2109 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2110 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2112 plugin = GNUNET_malloc (sizeof (struct Plugin));
2113 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2114 GNUNET_BANDWIDTH_value_init ((uint32_t)
2116 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
2117 plugin->port = port;
2118 plugin->aport = aport;
2120 plugin->broadcast_interval = interval;
2121 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2124 api->send = &udp_plugin_send;
2125 api->disconnect = &udp_disconnect;
2126 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2127 api->address_to_string = &udp_address_to_string;
2128 api->check_address = &udp_plugin_check_address;
2131 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2132 "BINDTO", &plugin->bind4_address))
2134 LOG (GNUNET_ERROR_TYPE_DEBUG,
2135 "Binding udp plugin to specific address: `%s'\n",
2136 plugin->bind4_address);
2137 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
2139 GNUNET_free (plugin->bind4_address);
2140 GNUNET_free (plugin);
2147 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2148 "BINDTO6", &plugin->bind6_address))
2150 LOG (GNUNET_ERROR_TYPE_DEBUG,
2151 "Binding udp plugin to specific address: `%s'\n",
2152 plugin->bind6_address);
2154 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
2156 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2157 plugin->bind6_address);
2158 GNUNET_free_non_null (plugin->bind4_address);
2159 GNUNET_free (plugin->bind6_address);
2160 GNUNET_free (plugin);
2167 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2169 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2171 plugin->inbound_sessions =
2172 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2174 sockets_created = 0;
2176 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
2179 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2180 if (NULL == plugin->sockv6)
2182 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2186 #if HAVE_SOCKADDR_IN_SIN_LEN
2187 serverAddrv6.sin6_len = sizeof (serverAddrv6);
2189 serverAddrv6.sin6_family = AF_INET6;
2190 serverAddrv6.sin6_addr = in6addr_any;
2191 serverAddrv6.sin6_port = htons (plugin->port);
2192 addrlen = sizeof (serverAddrv6);
2193 serverAddr = (struct sockaddr *) &serverAddrv6;
2195 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2196 ntohs (serverAddrv6.sin6_port));
2199 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2202 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2204 LOG (GNUNET_ERROR_TYPE_DEBUG,
2205 "IPv6 Binding failed, trying new port %d\n",
2206 ntohs (serverAddrv6.sin6_port));
2211 GNUNET_NETWORK_socket_close (plugin->sockv6);
2212 plugin->sockv6 = NULL;
2216 if (plugin->sockv6 != NULL)
2218 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2219 addrlens[sockets_created] = sizeof (serverAddrv6);
2226 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2227 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2228 if (NULL == plugin->sockv4)
2230 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2234 #if HAVE_SOCKADDR_IN_SIN_LEN
2235 serverAddrv4.sin_len = sizeof (serverAddrv4);
2237 serverAddrv4.sin_family = AF_INET;
2238 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2239 serverAddrv4.sin_port = htons (plugin->port);
2240 addrlen = sizeof (serverAddrv4);
2241 serverAddr = (struct sockaddr *) &serverAddrv4;
2243 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2244 ntohs (serverAddrv4.sin_port));
2247 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2250 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2252 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2253 ntohs (serverAddrv4.sin_port));
2258 GNUNET_NETWORK_socket_close (plugin->sockv4);
2259 plugin->sockv4 = NULL;
2263 if (plugin->sockv4 != NULL)
2265 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2266 addrlens[sockets_created] = sizeof (serverAddrv4);
2271 plugin->rs = GNUNET_NETWORK_fdset_create ();
2272 GNUNET_NETWORK_fdset_zero (plugin->rs);
2273 if (NULL != plugin->sockv4)
2274 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
2275 if (NULL != plugin->sockv6)
2276 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
2278 plugin->select_task =
2279 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2280 GNUNET_SCHEDULER_NO_TASK,
2281 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
2282 NULL, &udp_plugin_select, plugin);
2288 /* create IPv4 broadcast socket */
2289 plugin->broadcast_ipv4 = GNUNET_NO;
2290 if (plugin->sockv4 != NULL)
2294 if (GNUNET_NETWORK_socket_setsockopt
2295 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
2296 sizeof (int)) != GNUNET_OK)
2298 LOG (GNUNET_ERROR_TYPE_WARNING,
2300 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
2301 ntohs (serverAddrv4.sin_port));
2305 GNUNET_OS_network_interfaces_list (iface_proc, plugin);
2306 plugin->send_ipv4_broadcast_task =
2307 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
2309 plugin->broadcast_ipv4_mst =
2310 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
2312 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
2313 plugin->broadcast_ipv4 = GNUNET_YES;
2317 plugin->broadcast_ipv6 = GNUNET_NO;
2318 if (plugin->sockv6 != NULL)
2320 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
2322 inet_pton (AF_INET6, "FF05::13B",
2323 &plugin->ipv6_multicast_address.sin6_addr));
2325 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
2326 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
2328 plugin->broadcast_ipv6_mst =
2329 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
2331 /* Create IPv6 multicast request */
2332 struct ipv6_mreq multicastRequest;
2334 multicastRequest.ipv6mr_multiaddr =
2335 plugin->ipv6_multicast_address.sin6_addr;
2336 /* TODO: 0 selects the "best" interface, tweak to use all interfaces
2338 * http://tools.ietf.org/html/rfc2553#section-5.2:
2342 * Join a multicast group on a specified local interface. If the
2343 * interface index is specified as 0, the kernel chooses the local
2344 * interface. For example, some kernels look up the multicast
2345 * group in the normal IPv6 routing table and using the resulting
2348 multicastRequest.ipv6mr_interface = 0;
2350 /* Join the multicast group */
2351 if (GNUNET_NETWORK_socket_setsockopt
2352 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
2353 (char *) &multicastRequest, sizeof (multicastRequest)) == GNUNET_OK)
2355 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
2357 plugin->send_ipv6_broadcast_task =
2358 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
2359 plugin->broadcast_ipv6 = GNUNET_YES;
2362 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting not running\n");
2366 if (sockets_created == 0)
2367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2369 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
2370 (const struct sockaddr **) addrs, addrlens,
2371 &udp_nat_port_map_callback, NULL, plugin);
2376 * Shutdown the plugin.
2378 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
2382 libgnunet_plugin_transport_udp_done (void *cls)
2384 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2385 struct Plugin *plugin = api->cls;
2386 struct ReceiveContext *rc;
2388 /* FIXME: clean up heap and hashmap */
2389 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
2391 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2392 plugin->sessions = NULL;
2393 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
2394 &destroy_inbound_session, NULL);
2395 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
2396 plugin->inbound_sessions = NULL;
2397 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
2399 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2402 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
2404 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2406 GNUNET_SCHEDULER_cancel (plugin->select_task);
2407 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2410 if (plugin->broadcast_ipv4)
2412 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2414 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
2415 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2418 if (plugin->broadcast_ipv4_mst != NULL)
2419 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
2421 while (plugin->ipv4_broadcast_head != NULL)
2423 struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
2425 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
2426 plugin->ipv4_broadcast_tail, p);
2427 GNUNET_free (p->addr);
2432 if (plugin->broadcast_ipv6)
2434 /* Create IPv6 multicast request */
2435 struct ipv6_mreq multicastRequest;
2437 multicastRequest.ipv6mr_multiaddr =
2438 plugin->ipv6_multicast_address.sin6_addr;
2439 multicastRequest.ipv6mr_interface = 0;
2441 /* Join the multicast address */
2442 if (GNUNET_NETWORK_socket_setsockopt
2443 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
2444 (char *) &multicastRequest, sizeof (multicastRequest)) == 0)
2446 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
2449 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
2451 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2453 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
2454 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2456 if (plugin->broadcast_ipv6_mst != NULL)
2457 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
2461 if (plugin->sockv4 != NULL)
2463 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2464 plugin->sockv4 = NULL;
2466 if (plugin->sockv6 != NULL)
2468 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2469 plugin->sockv6 = NULL;
2472 GNUNET_SERVER_mst_destroy (plugin->mst);
2473 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2475 GNUNET_NAT_unregister (plugin->nat);
2477 GNUNET_free (plugin);
2482 /* end of plugin_transport_udp.c */