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
73 GNUNET_NETWORK_STRUCT_BEGIN
76 * UDP Message-Packet header (after defragmentation).
83 struct GNUNET_MessageHeader header;
86 * Always zero for now.
91 * What is the identity of the sender
93 struct GNUNET_PeerIdentity sender;
98 * UDP ACK Message-Packet header (after defragmentation).
100 struct UDP_ACK_Message
105 struct GNUNET_MessageHeader header;
108 * Desired delay for flow control
113 * What is the identity of the sender
115 struct GNUNET_PeerIdentity sender;
119 struct UDP_Beacon_Message
124 struct GNUNET_MessageHeader header;
127 * What is the identity of the sender
129 struct GNUNET_PeerIdentity sender;
134 * Network format for IPv4 addresses.
136 struct IPv4UdpAddress
139 * IPv4 address, in network byte order.
141 uint32_t ipv4_addr GNUNET_PACKED;
144 * Port number, in network byte order.
146 uint16_t u4_port GNUNET_PACKED;
151 * Network format for IPv6 addresses.
153 struct IPv6UdpAddress
159 struct in6_addr ipv6_addr GNUNET_PACKED;
162 * Port number, in network byte order.
164 uint16_t u6_port GNUNET_PACKED;
166 GNUNET_NETWORK_STRUCT_END
168 /* Forward definition */
173 * Session with another peer. FIXME: why not make this into
174 * a regular 'struct Session' and pass it around!?
180 * Which peer is this session for?
182 struct GNUNET_PeerIdentity target;
185 * Pointer to the global plugin struct.
187 struct Plugin *plugin;
190 * Address of the other peer
192 const struct sockaddr *sock_addr;
197 * ATS network type in NBO
199 uint32_t ats_address_network_type;
202 * Function to call upon completion of the transmission.
204 GNUNET_TRANSPORT_TransmitContinuation cont;
207 * Closure for 'cont'.
212 * Current outgoing message to this peer.
214 struct GNUNET_FRAGMENT_Context *frag;
216 struct GNUNET_TIME_Absolute valid_until;
218 GNUNET_SCHEDULER_TaskIdentifier invalidation_task;
220 GNUNET_SCHEDULER_TaskIdentifier delayed_cont_task;
223 * Desired delay for next sending we send to other peer
225 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
228 * Desired delay for next sending we received from other peer
230 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
235 * Data structure to track defragmentation contexts based
236 * on the source of the UDP traffic.
238 struct ReceiveContext
242 * Defragmentation context.
244 struct GNUNET_DEFRAGMENT_Context *defrag;
247 * Source address this receive context is for (allocated at the
248 * end of the struct).
250 const struct sockaddr *src_addr;
253 * Reference to master plugin struct.
255 struct Plugin *plugin;
258 * Node in the defrag heap.
260 struct GNUNET_CONTAINER_HeapNode *hnode;
263 * Length of 'src_addr'
267 struct GNUNET_PeerIdentity id;
271 struct BroadcastAddress
273 struct BroadcastAddress *next;
274 struct BroadcastAddress *prev;
282 * Encapsulation of all of the state of the plugin.
290 struct GNUNET_TRANSPORT_PluginEnvironment *env;
293 * Session of peers with whom we are currently connected,
294 * map of peer identity to 'struct PeerSession'.
296 struct GNUNET_CONTAINER_MultiHashMap *sessions;
299 * Session of peers with whom we are currently connected,
300 * map of peer identity to 'struct PeerSession'.
302 struct GNUNET_CONTAINER_MultiHashMap *inbound_sessions;
305 * Heap with all of our defragmentation activities.
307 struct GNUNET_CONTAINER_Heap *defrags;
312 GNUNET_SCHEDULER_TaskIdentifier select_task;
315 * Tokenizer for inbound messages.
317 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
320 * Bandwidth tracker to limit global UDP traffic.
322 struct GNUNET_BANDWIDTH_Tracker tracker;
325 * Address we were told to bind to exclusively (IPv4).
330 * Address we were told to bind to exclusively (IPv6).
335 * Handle to NAT traversal support.
337 struct GNUNET_NAT_Handle *nat;
342 struct GNUNET_NETWORK_FDSet *rs;
345 * The read socket for IPv4
347 struct GNUNET_NETWORK_Handle *sockv4;
350 * The read socket for IPv6
352 struct GNUNET_NETWORK_Handle *sockv6;
355 * Beacon broadcasting
356 * -------------------
362 struct GNUNET_TIME_Relative broadcast_interval;
365 * Broadcast with IPv4
370 * Broadcast with IPv6
376 * Tokenizer for inbound messages.
378 struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv6_mst;
379 struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv4_mst;
382 * ID of select broadcast task
384 GNUNET_SCHEDULER_TaskIdentifier send_ipv4_broadcast_task;
387 * ID of select broadcast task
389 GNUNET_SCHEDULER_TaskIdentifier send_ipv6_broadcast_task;
392 * IPv6 multicast address
394 struct sockaddr_in6 ipv6_multicast_address;
397 * DLL of IPv4 broadcast addresses
399 struct BroadcastAddress *ipv4_broadcast_tail;
400 struct BroadcastAddress *ipv4_broadcast_head;
404 * expected delay for ACKs
406 struct GNUNET_TIME_Relative last_expected_delay;
409 * Port we broadcasting on.
411 uint16_t broadcast_port;
419 * Port we advertise on.
425 struct PeerSessionIteratorContext
427 struct Session *result;
434 * Lookup the session for the given peer.
436 * @param plugin the plugin
437 * @param peer peer's identity
438 * @return NULL if we have no session
440 static struct Session *
441 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
443 return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
449 inbound_session_iterator (void *cls, const GNUNET_HashCode * key, void *value)
451 struct PeerSessionIteratorContext *psc = cls;
452 struct Session *s = value;
454 if (s->addrlen == psc->addrlen)
456 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
459 if (psc->result != NULL)
466 * Lookup the session for the given peer.
468 * @param plugin the plugin
469 * @param peer peer's identity
470 * @param addr address
471 * @param addrlen address length
472 * @return NULL if we have no session
474 static struct Session *
475 find_inbound_session (struct Plugin *plugin,
476 const struct GNUNET_PeerIdentity *peer, const void *addr,
479 struct PeerSessionIteratorContext psc;
482 psc.addrlen = addrlen;
485 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
487 &inbound_session_iterator, &psc);
493 inbound_session_by_addr_iterator (void *cls, const GNUNET_HashCode * key,
496 struct PeerSessionIteratorContext *psc = cls;
497 struct Session *s = value;
499 if (s->addrlen == psc->addrlen)
501 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
504 if (psc->result != NULL)
511 * Lookup the session for the given peer just by address.
513 * @param plugin the plugin
514 * @param addr address
515 * @param addrlen address length
516 * @return NULL if we have no session
518 static struct Session *
519 find_inbound_session_by_addr (struct Plugin *plugin, const void *addr,
522 struct PeerSessionIteratorContext psc;
525 psc.addrlen = addrlen;
528 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
529 &inbound_session_by_addr_iterator,
536 * Destroy a session, plugin is being unloaded.
539 * @param key hash of public key of target peer
540 * @param value a 'struct PeerSession*' to clean up
541 * @return GNUNET_OK (continue to iterate)
544 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
546 struct Session *peer_session = value;
548 GNUNET_assert (GNUNET_YES ==
549 GNUNET_CONTAINER_multihashmap_remove (peer_session->
554 if (peer_session->frag != NULL)
555 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
556 if (GNUNET_SCHEDULER_NO_TASK != peer_session->delayed_cont_task)
557 GNUNET_SCHEDULER_cancel (peer_session->delayed_cont_task);
558 GNUNET_free (peer_session);
564 * Destroy a session, plugin is being unloaded.
567 * @param key hash of public key of target peer
568 * @param value a 'struct PeerSession*' to clean up
569 * @return GNUNET_OK (continue to iterate)
572 destroy_inbound_session (void *cls, const GNUNET_HashCode * key, void *value)
574 struct Session *s = value;
576 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
577 GNUNET_SCHEDULER_cancel (s->invalidation_task);
578 if (GNUNET_SCHEDULER_NO_TASK != s->delayed_cont_task)
579 GNUNET_SCHEDULER_cancel (s->delayed_cont_task);
580 GNUNET_CONTAINER_multihashmap_remove (s->plugin->inbound_sessions,
581 &s->target.hashPubKey, s);
588 * Disconnect from a remote node. Clean up session if we have one for this peer
590 * @param cls closure for this call (should be handle to Plugin)
591 * @param target the peeridentity of the peer to disconnect
592 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
595 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
597 struct Plugin *plugin = cls;
598 struct Session *session;
600 session = find_session (plugin, target);
603 GNUNET_assert (GNUNET_OK ==
604 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
608 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
610 &destroy_inbound_session, NULL);
611 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
612 if (GNUNET_SCHEDULER_NO_TASK != session->delayed_cont_task)
613 GNUNET_SCHEDULER_cancel (session->delayed_cont_task);
614 if (session->cont != NULL)
615 session->cont (session->cont_cls, target, GNUNET_SYSERR);
616 GNUNET_free (session);
621 * Actually send out the message.
623 * @param plugin the plugin
624 * @param sa the address to send the message to
625 * @param msg message to transmit
626 * @return the number of bytes written
629 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
630 const struct GNUNET_MessageHeader *msg)
635 switch (sa->sa_family)
638 if (NULL == plugin->sockv4)
641 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
642 sa, slen = sizeof (struct sockaddr_in));
645 if (NULL == plugin->sockv6)
648 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
649 sa, slen = sizeof (struct sockaddr_in6));
655 if (GNUNET_SYSERR == sent)
657 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
658 LOG (GNUNET_ERROR_TYPE_ERROR,
659 "UDP transmited %u-byte message to %s (%d: %s)\n",
660 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
661 (sent < 0) ? STRERROR (errno) : "ok");
664 LOG (GNUNET_ERROR_TYPE_DEBUG,
665 "UDP transmited %u-byte message to %s (%d: %s)\n",
666 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
667 (sent < 0) ? STRERROR (errno) : "ok");
673 * Function that is called with messages created by the fragmentation
674 * module. In the case of the 'proc' callback of the
675 * GNUNET_FRAGMENT_context_create function, this function must
676 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
678 * @param cls closure, the 'struct PeerSession'
679 * @param msg the message that was created
682 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
684 struct Session *session = cls;
686 udp_send (session->plugin, session->sock_addr, msg);
687 GNUNET_FRAGMENT_context_transmission_done (session->frag);
691 static struct Session *
692 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
693 const void *addr, size_t addrlen,
694 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
696 struct Session *peer_session;
697 const struct IPv4UdpAddress *t4;
698 const struct IPv6UdpAddress *t6;
699 struct sockaddr_in *v4;
700 struct sockaddr_in6 *v6;
702 struct GNUNET_ATS_Information ats;
706 case sizeof (struct IPv4UdpAddress):
707 if (NULL == plugin->sockv4)
713 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
714 len = sizeof (struct sockaddr_in);
715 v4 = (struct sockaddr_in *) &peer_session[1];
716 v4->sin_family = AF_INET;
717 #if HAVE_SOCKADDR_IN_SIN_LEN
718 v4->sin_len = sizeof (struct sockaddr_in);
720 v4->sin_port = t4->u4_port;
721 v4->sin_addr.s_addr = t4->ipv4_addr;
722 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
724 case sizeof (struct IPv6UdpAddress):
725 if (NULL == plugin->sockv6)
731 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
732 len = sizeof (struct sockaddr_in6);
733 v6 = (struct sockaddr_in6 *) &peer_session[1];
734 v6->sin6_family = AF_INET6;
735 #if HAVE_SOCKADDR_IN_SIN_LEN
736 v6->sin6_len = sizeof (struct sockaddr_in6);
738 v6->sin6_port = t6->u6_port;
739 v6->sin6_addr = t6->ipv6_addr;
740 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
743 /* Must have a valid address to send to */
748 peer_session->ats_address_network_type = ats.value;
749 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
750 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
751 peer_session->addrlen = len;
752 peer_session->target = *target;
753 peer_session->plugin = plugin;
754 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
755 peer_session->cont = cont;
756 peer_session->cont_cls = cont_cls;
762 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
766 udp_call_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
768 struct Session *s = cls;
769 GNUNET_TRANSPORT_TransmitContinuation cont = s->cont;
771 s->delayed_cont_task = GNUNET_SCHEDULER_NO_TASK;
773 cont (s->cont_cls, &s->target, GNUNET_OK);
778 * Function that can be used by the transport service to transmit
779 * a message using the plugin.
782 * @param target who should receive this message (ignored by UDP)
783 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
784 * @param msgbuf_size the size of the msgbuf to send
785 * @param priority how important is the message (ignored by UDP)
786 * @param timeout when should we time out (give up) if we can not transmit?
787 * @param session identifier used for this session (NULL for UDP)
788 * @param addr the addr to send the message to
789 * @param addrlen the len of addr
790 * @param force_address not used, we had better have an address to send to
791 * because we are stateless!!
792 * @param cont continuation to call once the message has
793 * been transmitted (or if the transport is ready
794 * for the next transmission call; or if the
795 * peer disconnected...)
796 * @param cont_cls closure for cont
798 * @return the number of bytes written (may return 0 and the message can
799 * still be transmitted later!)
802 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
803 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
804 struct GNUNET_TIME_Relative timeout, struct Session *session,
805 const void *addr, size_t addrlen, int force_address,
806 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
808 struct Plugin *plugin = cls;
809 struct Session *peer_session;
811 const struct IPv4UdpAddress *t4;
812 const struct IPv6UdpAddress *t6;
813 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
815 struct UDPMessage *udp;
816 struct GNUNET_TIME_Relative delta;
818 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
821 return GNUNET_SYSERR;
824 LOG (GNUNET_ERROR_TYPE_DEBUG,
825 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
826 msgbuf_size, GNUNET_i2s (target), udp_address_to_string (NULL, addr,
828 session, force_address);
830 if ((force_address == GNUNET_SYSERR) && (session == NULL))
831 return GNUNET_SYSERR;
834 /* safety check: comparing address to address stored in session */
835 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
838 GNUNET_assert (GNUNET_YES ==
839 GNUNET_CONTAINER_multihashmap_contains_value
840 (plugin->inbound_sessions, &target->hashPubKey, s));
842 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
843 return GNUNET_SYSERR;
846 case sizeof (struct IPv4UdpAddress):
847 if (NULL == plugin->sockv4)
850 cont (cont_cls, target, GNUNET_SYSERR);
851 return GNUNET_SYSERR;
854 if (s->addrlen != (sizeof (struct sockaddr_in)))
855 return GNUNET_SYSERR;
856 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
858 GNUNET_assert (a4->sin_port == t4->u4_port);
860 memcmp (&a4->sin_addr, &t4->ipv4_addr,
861 sizeof (struct in_addr)));
862 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
865 case sizeof (struct IPv6UdpAddress):
866 if (NULL == plugin->sockv6)
869 cont (cont_cls, target, GNUNET_SYSERR);
870 return GNUNET_SYSERR;
873 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
874 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
876 GNUNET_assert (a6->sin6_port == t6->u6_port);
878 memcmp (&a6->sin6_addr, &t6->ipv6_addr,
879 sizeof (struct in6_addr)));
880 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
884 /* Must have a valid address to send to */
889 if ((addr == NULL) || (addrlen == 0))
890 return GNUNET_SYSERR;
891 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
892 if (peer_session == NULL)
895 cont (cont_cls, target, GNUNET_SYSERR);
896 return GNUNET_SYSERR;;
900 udp = (struct UDPMessage *) mbuf;
901 udp->header.size = htons (mlen);
902 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
903 udp->reserved = htonl (0);
904 udp->sender = *plugin->env->my_identity;
905 memcpy (&udp[1], msgbuf, msgbuf_size);
908 delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
910 delta = GNUNET_TIME_UNIT_ZERO;
913 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
916 if ((delta.rel_value > 0) && (mlen > 0))
919 s->cont_cls = cont_cls;
920 s->delayed_cont_task =
921 GNUNET_SCHEDULER_add_delayed (delta, &udp_call_continuation, s);
924 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
926 GNUNET_free_non_null (peer_session);
930 GNUNET_assert (GNUNET_OK ==
931 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
934 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
936 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
938 plugin->last_expected_delay,
939 &udp->header, &send_fragment,
947 * Closure for 'process_inbound_tokenized_messages'
949 struct SourceInformation
954 struct GNUNET_PeerIdentity sender;
962 * Number of bytes in source address.
966 struct Session *session;
971 * Message tokenizer has broken up an incomming message. Pass it on
974 * @param cls the 'struct Plugin'
975 * @param client the 'struct SourceInformation'
976 * @param hdr the actual message
979 process_inbound_tokenized_messages (void *cls, void *client,
980 const struct GNUNET_MessageHeader *hdr)
982 struct Plugin *plugin = cls;
983 struct SourceInformation *si = client;
984 struct GNUNET_ATS_Information atsi[2];
985 struct GNUNET_TIME_Relative delay;
988 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
989 atsi[0].value = htonl (1);
990 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
991 atsi[1].value = si->session->ats_address_network_type;
992 GNUNET_break (ntohl(si->session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
995 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving Session %X %s to transport\n",
996 si->session, GNUNET_i2s (&si->session->target));
998 plugin->env->receive (plugin->env->cls, &si->sender, hdr,
999 (const struct GNUNET_ATS_Information *) &atsi, 2,
1000 si->session, si->arg, si->args);
1001 si->session->flow_delay_for_other_peer = delay;
1005 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1007 struct Session *s = cls;
1009 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1010 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %X (`%s') is now invalid\n", s,
1011 GNUNET_a2s (s->sock_addr, s->addrlen));
1013 s->plugin->env->session_end (s->plugin->env->cls, &s->target, s);
1014 GNUNET_assert (GNUNET_YES ==
1015 GNUNET_CONTAINER_multihashmap_remove (s->
1016 plugin->inbound_sessions,
1017 &s->target.hashPubKey,
1024 * We've received a UDP Message. Process it (pass contents to main service).
1026 * @param plugin plugin context
1027 * @param msg the message
1028 * @param sender_addr sender address
1029 * @param sender_addr_len number of bytes in sender_addr
1032 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1033 const struct sockaddr *sender_addr,
1034 socklen_t sender_addr_len)
1036 struct SourceInformation si;
1037 struct IPv4UdpAddress u4;
1038 struct IPv6UdpAddress u6;
1039 struct GNUNET_ATS_Information ats;
1043 if (0 != ntohl (msg->reserved))
1045 GNUNET_break_op (0);
1048 if (ntohs (msg->header.size) <
1049 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1051 GNUNET_break_op (0);
1055 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1056 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1057 /* convert address */
1058 switch (sender_addr->sa_family)
1061 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1062 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1063 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1068 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1069 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1070 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1079 LOG (GNUNET_ERROR_TYPE_DEBUG,
1080 "Received message with %u bytes from peer `%s' at `%s'\n",
1081 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1082 GNUNET_a2s (sender_addr, sender_addr_len));
1085 /* create a session for inbound connections */
1086 const struct UDPMessage *udp_msg = (const struct UDPMessage *) msg;
1088 LOG (GNUNET_ERROR_TYPE_DEBUG,
1089 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
1090 GNUNET_i2s (&udp_msg->sender), udp_address_to_string (NULL, arg, args));
1092 struct Session *s = NULL;
1094 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr,
1099 LOG (GNUNET_ERROR_TYPE_DEBUG,
1100 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
1101 s, GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1105 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
1106 ats = plugin->env->get_address_type (plugin->env->cls, sender_addr, sender_addr_len);
1107 s->ats_address_network_type = ats.value;
1108 LOG (GNUNET_ERROR_TYPE_DEBUG,
1109 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n", s,
1110 GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1112 GNUNET_assert (GNUNET_OK ==
1113 GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
1114 &s->target.hashPubKey, s,
1115 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1118 GNUNET_TIME_relative_to_absolute
1119 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1120 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
1122 GNUNET_SCHEDULER_cancel (s->invalidation_task);
1123 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rescheduling %X' `%s'\n", s,
1125 udp_address_to_string (NULL, arg, args));
1127 s->invalidation_task =
1128 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1129 &invalidation_task, s);
1130 /* iterate over all embedded messages */
1131 si.sender = msg->sender;
1135 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1136 ntohs (msg->header.size) -
1137 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1142 * Process a defragmented message.
1144 * @param cls the 'struct ReceiveContext'
1145 * @param msg the message
1148 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1150 struct ReceiveContext *rc = cls;
1152 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1157 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1162 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1163 rc->src_addr, rc->addr_len);
1168 * Transmit an acknowledgement.
1170 * @param cls the 'struct ReceiveContext'
1171 * @param id message ID (unused)
1172 * @param msg ack to transmit
1175 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1177 struct ReceiveContext *rc = cls;
1179 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1181 struct UDP_ACK_Message *udp_ack;
1186 s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
1189 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1190 delay = s->flow_delay_for_other_peer.rel_value;
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "Sending ACK to `%s' including delay of %u ms\n",
1199 GNUNET_a2s (rc->src_addr,
1200 (rc->src_addr->sa_family ==
1201 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1205 udp_ack = (struct UDP_ACK_Message *) buf;
1206 udp_ack->header.size = htons ((uint16_t) msize);
1207 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1208 udp_ack->delay = htonl (delay);
1209 udp_ack->sender = *rc->plugin->env->my_identity;
1210 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1211 (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
1216 * Closure for 'find_receive_context'.
1218 struct FindReceiveContext
1221 * Where to store the result.
1223 struct ReceiveContext *rc;
1228 const struct sockaddr *addr;
1231 * Number of bytes in 'addr'.
1235 struct Session *session;
1240 * Scan the heap for a receive context with the given address.
1242 * @param cls the 'struct FindReceiveContext'
1243 * @param node internal node of the heap
1244 * @param element value stored at the node (a 'struct ReceiveContext')
1245 * @param cost cost associated with the node
1246 * @return GNUNET_YES if we should continue to iterate,
1250 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1251 void *element, GNUNET_CONTAINER_HeapCostType cost)
1253 struct FindReceiveContext *frc = cls;
1254 struct ReceiveContext *e = element;
1256 if ((frc->addr_len == e->addr_len) &&
1257 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1267 struct Plugin *plugin;
1269 struct IPv4UdpAddress addr;
1271 * ATS network type in NBO
1273 uint32_t ats_address_network_type;
1278 struct Plugin *plugin;
1280 struct IPv6UdpAddress addr;
1282 * ATS network type in NBO
1284 uint32_t ats_address_network_type;
1289 * Read and process a message from the given socket.
1291 * @param plugin the overall plugin
1292 * @param rsock socket to read from
1295 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1301 const struct GNUNET_MessageHeader *msg;
1302 const struct GNUNET_MessageHeader *ack;
1303 struct Session *peer_session;
1304 const struct UDP_ACK_Message *udp_ack;
1305 struct ReceiveContext *rc;
1306 struct GNUNET_TIME_Absolute now;
1307 struct FindReceiveContext frc;
1308 struct Session *s = NULL;
1309 struct GNUNET_TIME_Relative flow_delay;
1310 struct GNUNET_ATS_Information ats;
1312 fromlen = sizeof (addr);
1313 memset (&addr, 0, sizeof (addr));
1315 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1316 (struct sockaddr *) &addr, &fromlen);
1317 if (ret < sizeof (struct GNUNET_MessageHeader))
1319 GNUNET_break_op (0);
1322 msg = (const struct GNUNET_MessageHeader *) buf;
1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1325 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1326 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1328 if (ret != ntohs (msg->size))
1330 GNUNET_break_op (0);
1333 switch (ntohs (msg->type))
1335 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1337 if (fromlen == sizeof (struct sockaddr_in))
1339 LOG (GNUNET_ERROR_TYPE_DEBUG,
1340 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
1341 ret, GNUNET_a2s ((const struct sockaddr *) &addr, fromlen));
1343 struct Mstv4Context *mc;
1345 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
1346 struct sockaddr_in *av4 = (struct sockaddr_in *) &addr;
1348 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
1349 mc->addr.u4_port = av4->sin_port;
1350 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &addr, fromlen);
1351 mc->ats_address_network_type = ats.value;
1353 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, ret,
1354 GNUNET_NO, GNUNET_NO))
1357 else if (fromlen == sizeof (struct sockaddr_in6))
1359 LOG (GNUNET_ERROR_TYPE_DEBUG,
1360 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
1361 ret, GNUNET_a2s ((const struct sockaddr *) &addr, fromlen));
1363 struct Mstv6Context *mc;
1365 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
1366 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) &addr;
1368 mc->addr.ipv6_addr = av6->sin6_addr;
1369 mc->addr.u6_port = av6->sin6_port;
1370 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &addr, fromlen);
1371 mc->ats_address_network_type = ats.value;
1374 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, ret,
1375 GNUNET_NO, GNUNET_NO))
1380 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1381 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1383 GNUNET_break_op (0);
1386 process_udp_message (plugin, (const struct UDPMessage *) msg,
1387 (const struct sockaddr *) addr, fromlen);
1389 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1391 if (ntohs (msg->size) <
1392 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1394 GNUNET_break_op (0);
1397 udp_ack = (const struct UDP_ACK_Message *) msg;
1398 s = find_inbound_session (plugin, &udp_ack->sender, addr, fromlen);
1401 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1404 flow_delay.rel_value);
1406 s->flow_delay_from_other_peer =
1407 GNUNET_TIME_relative_to_absolute (flow_delay);
1409 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1410 if (ntohs (ack->size) !=
1411 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1413 GNUNET_break_op (0);
1417 LOG (GNUNET_ERROR_TYPE_DEBUG,
1418 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1419 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1420 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1423 peer_session = find_session (plugin, &udp_ack->sender);
1424 if (NULL == peer_session)
1427 LOG (GNUNET_ERROR_TYPE_DEBUG,
1428 "Session for ACK not found, dropping ACK!\n");
1432 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1434 GNUNET_assert (GNUNET_OK ==
1435 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1439 plugin->last_expected_delay =
1440 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1441 if (peer_session->cont != NULL)
1442 peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
1443 GNUNET_free (peer_session);
1445 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1447 frc.addr = (const struct sockaddr *) addr;
1448 frc.addr_len = fromlen;
1449 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1451 now = GNUNET_TIME_absolute_get ();
1455 /* need to create a new RC */
1456 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1457 memcpy (&rc[1], addr, fromlen);
1458 rc->src_addr = (const struct sockaddr *) &rc[1];
1459 rc->addr_len = fromlen;
1460 rc->plugin = plugin;
1462 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1463 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1464 &fragment_msg_proc, &ack_proc);
1466 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1467 (GNUNET_CONTAINER_HeapCostType)
1471 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1472 (unsigned int) ntohs (msg->size),
1473 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1476 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1478 /* keep this 'rc' from expiring */
1479 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1480 (GNUNET_CONTAINER_HeapCostType)
1483 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1484 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1486 /* remove 'rc' that was inactive the longest */
1487 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1488 GNUNET_assert (NULL != rc);
1489 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1494 GNUNET_break_op (0);
1501 * We have been notified that our writeset has something to read. We don't
1502 * know which socket needs to be read, so we have to check each one
1503 * Then reschedule this function to be called again once more is available.
1505 * @param cls the plugin handle
1506 * @param tc the scheduling context (for rescheduling this function again)
1509 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1511 struct Plugin *plugin = cls;
1513 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1514 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1516 if ((NULL != plugin->sockv4) &&
1517 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1518 udp_read (plugin, plugin->sockv4);
1519 if ((NULL != plugin->sockv6) &&
1520 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1521 udp_read (plugin, plugin->sockv6);
1522 plugin->select_task =
1523 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1524 GNUNET_SCHEDULER_NO_TASK,
1525 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1526 NULL, &udp_plugin_select, plugin);
1532 broadcast_ipv4_mst_cb (void *cls, void *client,
1533 const struct GNUNET_MessageHeader *message)
1535 struct Plugin *plugin = cls;
1536 struct Mstv4Context *mc = client;
1537 const struct GNUNET_MessageHeader *hello;
1538 struct UDP_Beacon_Message *msg;
1540 msg = (struct UDP_Beacon_Message *) message;
1542 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1543 ntohs (msg->header.type))
1546 LOG (GNUNET_ERROR_TYPE_DEBUG,
1547 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1548 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1549 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1551 struct GNUNET_ATS_Information atsi[2];
1554 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1555 atsi[0].value = htonl (1);
1556 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1557 atsi[1].value = mc->ats_address_network_type;
1558 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
1560 hello = (struct GNUNET_MessageHeader *) &msg[1];
1561 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
1562 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
1563 (const char *) &mc->addr, sizeof (mc->addr));
1565 GNUNET_STATISTICS_update (plugin->env->stats,
1567 ("# IPv4 broadcast HELLO beacons received via udp"),
1574 broadcast_ipv6_mst_cb (void *cls, void *client,
1575 const struct GNUNET_MessageHeader *message)
1578 struct Plugin *plugin = cls;
1579 struct Mstv6Context *mc = client;
1580 const struct GNUNET_MessageHeader *hello;
1581 struct UDP_Beacon_Message *msg;
1583 msg = (struct UDP_Beacon_Message *) message;
1585 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1586 ntohs (msg->header.type))
1589 LOG (GNUNET_ERROR_TYPE_DEBUG,
1590 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1591 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1592 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1594 struct GNUNET_ATS_Information atsi[2];
1597 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1598 atsi[0].value = htonl (1);
1599 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1600 atsi[1].value = mc->ats_address_network_type;
1601 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
1603 hello = (struct GNUNET_MessageHeader *) &msg[1];
1604 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
1605 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
1606 (const char *) &mc->addr, sizeof (mc->addr));
1608 GNUNET_STATISTICS_update (plugin->env->stats,
1610 ("# IPv6 multicast HELLO beacons received via udp"),
1617 udp_ipv4_broadcast_send (void *cls,
1618 const struct GNUNET_SCHEDULER_TaskContext *tc)
1620 struct Plugin *plugin = cls;
1623 uint16_t hello_size;
1626 const struct GNUNET_MessageHeader *hello;
1627 struct UDP_Beacon_Message *msg;
1628 struct BroadcastAddress *baddr;
1630 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1632 hello = plugin->env->get_our_hello ();
1633 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1634 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1636 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1637 (msg_size > (UDP_MTU)))
1640 msg = (struct UDP_Beacon_Message *) buf;
1641 msg->sender = *(plugin->env->my_identity);
1642 msg->header.size = ntohs (msg_size);
1643 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1644 memcpy (&msg[1], hello, hello_size);
1647 baddr = plugin->ipv4_broadcast_head;
1649 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
1651 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
1653 addr->sin_port = htons (plugin->port);
1656 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, msg_size,
1657 (const struct sockaddr *) addr,
1659 if (sent == GNUNET_SYSERR)
1660 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1662 LOG (GNUNET_ERROR_TYPE_DEBUG,
1663 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
1664 GNUNET_a2s (baddr->addr, baddr->addrlen));
1665 baddr = baddr->next;
1668 plugin->send_ipv4_broadcast_task =
1669 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1670 &udp_ipv4_broadcast_send, plugin);
1674 udp_ipv6_broadcast_send (void *cls,
1675 const struct GNUNET_SCHEDULER_TaskContext *tc)
1677 struct Plugin *plugin = cls;
1680 uint16_t hello_size;
1683 const struct GNUNET_MessageHeader *hello;
1684 struct UDP_Beacon_Message *msg;
1686 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1688 hello = plugin->env->get_our_hello ();
1689 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1690 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1692 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1693 (msg_size > (UDP_MTU)))
1696 msg = (struct UDP_Beacon_Message *) buf;
1697 msg->sender = *(plugin->env->my_identity);
1698 msg->header.size = ntohs (msg_size);
1699 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1700 memcpy (&msg[1], hello, hello_size);
1704 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, msg_size,
1705 (const struct sockaddr *)
1706 &plugin->ipv6_multicast_address,
1707 sizeof (struct sockaddr_in6));
1708 if (sent == GNUNET_SYSERR)
1709 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1711 LOG (GNUNET_ERROR_TYPE_DEBUG,
1712 "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
1714 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
1715 sizeof (struct sockaddr_in6)));
1719 plugin->send_ipv6_broadcast_task =
1720 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1721 &udp_ipv6_broadcast_send, plugin);
1725 * Check if the given port is plausible (must be either our listen
1726 * port or our advertised port). If it is neither, we return
1729 * @param plugin global variables
1730 * @param in_port port number to check
1731 * @return GNUNET_OK if port is either open_port or adv_port
1734 check_port (struct Plugin *plugin, uint16_t in_port)
1736 if ((in_port == plugin->port) || (in_port == plugin->aport))
1738 return GNUNET_SYSERR;
1743 * Function that will be called to check if a binary address for this
1744 * plugin is well-formed and corresponds to an address for THIS peer
1745 * (as per our configuration). Naturally, if absolutely necessary,
1746 * plugins can be a bit conservative in their answer, but in general
1747 * plugins should make sure that the address does not redirect
1748 * traffic to a 3rd party that might try to man-in-the-middle our
1751 * @param cls closure, should be our handle to the Plugin
1752 * @param addr pointer to the address
1753 * @param addrlen length of addr
1754 * @return GNUNET_OK if this is a plausible address for this peer
1755 * and transport, GNUNET_SYSERR if not
1759 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1761 struct Plugin *plugin = cls;
1762 struct IPv4UdpAddress *v4;
1763 struct IPv6UdpAddress *v6;
1765 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1766 (addrlen != sizeof (struct IPv6UdpAddress)))
1768 GNUNET_break_op (0);
1769 return GNUNET_SYSERR;
1771 if (addrlen == sizeof (struct IPv4UdpAddress))
1773 v4 = (struct IPv4UdpAddress *) addr;
1774 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1775 return GNUNET_SYSERR;
1777 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1778 sizeof (struct in_addr)))
1779 return GNUNET_SYSERR;
1783 v6 = (struct IPv6UdpAddress *) addr;
1784 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1786 GNUNET_break_op (0);
1787 return GNUNET_SYSERR;
1789 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1790 return GNUNET_SYSERR;
1792 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1793 sizeof (struct in6_addr)))
1794 return GNUNET_SYSERR;
1801 * Function called for a quick conversion of the binary address to
1802 * a numeric address. Note that the caller must not free the
1803 * address and that the next call to this function is allowed
1804 * to override the address again.
1806 * @param cls closure
1807 * @param addr binary address
1808 * @param addrlen length of the address
1809 * @return string representing the same address
1812 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1814 static char rbuf[INET6_ADDRSTRLEN + 10];
1815 char buf[INET6_ADDRSTRLEN];
1819 const struct IPv4UdpAddress *t4;
1820 const struct IPv6UdpAddress *t6;
1824 if (addrlen == sizeof (struct IPv6UdpAddress))
1828 port = ntohs (t6->u6_port);
1829 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1832 else if (addrlen == sizeof (struct IPv4UdpAddress))
1836 port = ntohs (t4->u4_port);
1837 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1842 GNUNET_break_op (0);
1845 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1846 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1853 * Closure for 'append_port'.
1855 struct PrettyPrinterContext
1858 * Function to call with the result.
1860 GNUNET_TRANSPORT_AddressStringCallback asc;
1863 * Clsoure for 'asc'.
1868 * Port to add after the IP address.
1875 * Append our port and forward the result.
1877 * @param cls a 'struct PrettyPrinterContext'
1878 * @param hostname result from DNS resolver
1881 append_port (void *cls, const char *hostname)
1883 struct PrettyPrinterContext *ppc = cls;
1886 if (hostname == NULL)
1888 ppc->asc (ppc->asc_cls, NULL);
1892 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1893 ppc->asc (ppc->asc_cls, ret);
1899 * Convert the transports address to a nice, human-readable
1902 * @param cls closure
1903 * @param type name of the transport that generated the address
1904 * @param addr one of the addresses of the host, NULL for the last address
1905 * the specific address format depends on the transport
1906 * @param addrlen length of the address
1907 * @param numeric should (IP) addresses be displayed in numeric form?
1908 * @param timeout after how long should we give up?
1909 * @param asc function to call on each string
1910 * @param asc_cls closure for asc
1913 udp_plugin_address_pretty_printer (void *cls, const char *type,
1914 const void *addr, size_t addrlen,
1916 struct GNUNET_TIME_Relative timeout,
1917 GNUNET_TRANSPORT_AddressStringCallback asc,
1920 struct PrettyPrinterContext *ppc;
1923 struct sockaddr_in a4;
1924 struct sockaddr_in6 a6;
1925 const struct IPv4UdpAddress *u4;
1926 const struct IPv6UdpAddress *u6;
1929 if (addrlen == sizeof (struct IPv6UdpAddress))
1932 memset (&a6, 0, sizeof (a6));
1933 a6.sin6_family = AF_INET6;
1934 #if HAVE_SOCKADDR_IN_SIN_LEN
1935 a6.sin6_len = sizeof (a6);
1937 a6.sin6_port = u6->u6_port;
1938 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1939 port = ntohs (u6->u6_port);
1943 else if (addrlen == sizeof (struct IPv4UdpAddress))
1946 memset (&a4, 0, sizeof (a4));
1947 a4.sin_family = AF_INET;
1948 #if HAVE_SOCKADDR_IN_SIN_LEN
1949 a4.sin_len = sizeof (a4);
1951 a4.sin_port = u4->u4_port;
1952 a4.sin_addr.s_addr = u4->ipv4_addr;
1953 port = ntohs (u4->u4_port);
1959 /* invalid address */
1960 GNUNET_break_op (0);
1961 asc (asc_cls, NULL);
1964 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1966 ppc->asc_cls = asc_cls;
1968 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1973 * Our external IP address/port mapping has changed.
1975 * @param cls closure, the 'struct LocalAddrList'
1976 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1977 * the previous (now invalid) one
1978 * @param addr either the previous or the new public IP address
1979 * @param addrlen actual lenght of the address
1982 udp_nat_port_map_callback (void *cls, int add_remove,
1983 const struct sockaddr *addr, socklen_t addrlen)
1985 struct Plugin *plugin = cls;
1986 struct IPv4UdpAddress u4;
1987 struct IPv6UdpAddress u6;
1991 /* convert 'addr' to our internal format */
1992 switch (addr->sa_family)
1995 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1996 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1997 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
2002 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
2003 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
2004 sizeof (struct in6_addr));
2005 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
2013 /* modify our published address list */
2014 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
2019 iface_proc (void *cls, const char *name, int isDefault,
2020 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
2021 const struct sockaddr *netmask, socklen_t addrlen)
2023 struct Plugin *plugin = cls;
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
2028 GNUNET_a2s (addr, addrlen), name, addr);
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2030 "broadcast address %s for interface %s %p\n ",
2031 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
2033 GNUNET_a2s (netmask, addrlen), name, netmask);
2036 /* Collecting broadcast addresses */
2037 if (broadcast_addr != NULL)
2039 struct BroadcastAddress *ba =
2040 GNUNET_malloc (sizeof (struct BroadcastAddress));
2041 ba->addr = GNUNET_malloc (addrlen);
2042 memcpy (ba->addr, broadcast_addr, addrlen);
2043 ba->addrlen = addrlen;
2044 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
2045 plugin->ipv4_broadcast_tail, ba);
2053 * The exported method. Makes the core api available via a global and
2054 * returns the udp transport API.
2056 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2057 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2060 libgnunet_plugin_transport_udp_init (void *cls)
2062 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2063 unsigned long long port;
2064 unsigned long long aport;
2065 struct GNUNET_TRANSPORT_PluginFunctions *api;
2066 struct Plugin *plugin;
2067 int sockets_created;
2069 struct GNUNET_TIME_Relative interval;
2070 struct sockaddr_in serverAddrv4;
2071 struct sockaddr_in6 serverAddrv6;
2072 struct sockaddr *serverAddr;
2073 struct sockaddr *addrs[2];
2074 socklen_t addrlens[2];
2077 unsigned long long udp_max_bps;
2080 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2085 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2087 if (broadcast == GNUNET_SYSERR)
2088 broadcast = GNUNET_NO;
2090 if (GNUNET_SYSERR ==
2091 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
2092 "BROADCAST_INTERVAL", &interval))
2093 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2096 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2097 "MAX_BPS", &udp_max_bps))
2098 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2100 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2101 "ADVERTISED_PORT", &aport))
2105 LOG (GNUNET_ERROR_TYPE_WARNING,
2106 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2110 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2111 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2113 plugin = GNUNET_malloc (sizeof (struct Plugin));
2114 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2115 GNUNET_BANDWIDTH_value_init ((uint32_t)
2117 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
2118 plugin->port = port;
2119 plugin->aport = aport;
2121 plugin->broadcast_interval = interval;
2122 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2125 api->send = &udp_plugin_send;
2126 api->disconnect = &udp_disconnect;
2127 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2128 api->address_to_string = &udp_address_to_string;
2129 api->check_address = &udp_plugin_check_address;
2132 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2133 "BINDTO", &plugin->bind4_address))
2135 LOG (GNUNET_ERROR_TYPE_DEBUG,
2136 "Binding udp plugin to specific address: `%s'\n",
2137 plugin->bind4_address);
2138 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
2140 GNUNET_free (plugin->bind4_address);
2141 GNUNET_free (plugin);
2148 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2149 "BINDTO6", &plugin->bind6_address))
2151 LOG (GNUNET_ERROR_TYPE_DEBUG,
2152 "Binding udp plugin to specific address: `%s'\n",
2153 plugin->bind6_address);
2155 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
2157 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2158 plugin->bind6_address);
2159 GNUNET_free_non_null (plugin->bind4_address);
2160 GNUNET_free (plugin->bind6_address);
2161 GNUNET_free (plugin);
2168 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2170 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2172 plugin->inbound_sessions =
2173 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2175 sockets_created = 0;
2177 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
2180 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2181 if (NULL == plugin->sockv6)
2183 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2187 #if HAVE_SOCKADDR_IN_SIN_LEN
2188 serverAddrv6.sin6_len = sizeof (serverAddrv6);
2190 serverAddrv6.sin6_family = AF_INET6;
2191 serverAddrv6.sin6_addr = in6addr_any;
2192 serverAddrv6.sin6_port = htons (plugin->port);
2193 addrlen = sizeof (serverAddrv6);
2194 serverAddr = (struct sockaddr *) &serverAddrv6;
2196 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2197 ntohs (serverAddrv6.sin6_port));
2200 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2203 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2205 LOG (GNUNET_ERROR_TYPE_DEBUG,
2206 "IPv6 Binding failed, trying new port %d\n",
2207 ntohs (serverAddrv6.sin6_port));
2212 GNUNET_NETWORK_socket_close (plugin->sockv6);
2213 plugin->sockv6 = NULL;
2217 if (plugin->sockv6 != NULL)
2219 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2220 addrlens[sockets_created] = sizeof (serverAddrv6);
2227 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2228 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2229 if (NULL == plugin->sockv4)
2231 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2235 #if HAVE_SOCKADDR_IN_SIN_LEN
2236 serverAddrv4.sin_len = sizeof (serverAddrv4);
2238 serverAddrv4.sin_family = AF_INET;
2239 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2240 serverAddrv4.sin_port = htons (plugin->port);
2241 addrlen = sizeof (serverAddrv4);
2242 serverAddr = (struct sockaddr *) &serverAddrv4;
2244 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2245 ntohs (serverAddrv4.sin_port));
2248 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2251 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2253 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2254 ntohs (serverAddrv4.sin_port));
2259 GNUNET_NETWORK_socket_close (plugin->sockv4);
2260 plugin->sockv4 = NULL;
2264 if (plugin->sockv4 != NULL)
2266 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2267 addrlens[sockets_created] = sizeof (serverAddrv4);
2272 plugin->rs = GNUNET_NETWORK_fdset_create ();
2273 GNUNET_NETWORK_fdset_zero (plugin->rs);
2274 if (NULL != plugin->sockv4)
2275 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
2276 if (NULL != plugin->sockv6)
2277 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
2279 plugin->select_task =
2280 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2281 GNUNET_SCHEDULER_NO_TASK,
2282 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
2283 NULL, &udp_plugin_select, plugin);
2289 /* create IPv4 broadcast socket */
2290 plugin->broadcast_ipv4 = GNUNET_NO;
2291 if (plugin->sockv4 != NULL)
2295 if (GNUNET_NETWORK_socket_setsockopt
2296 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
2297 sizeof (int)) != GNUNET_OK)
2299 LOG (GNUNET_ERROR_TYPE_WARNING,
2301 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
2302 ntohs (serverAddrv4.sin_port));
2306 GNUNET_OS_network_interfaces_list (iface_proc, plugin);
2307 plugin->send_ipv4_broadcast_task =
2308 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
2310 plugin->broadcast_ipv4_mst =
2311 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
2313 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
2314 plugin->broadcast_ipv4 = GNUNET_YES;
2318 plugin->broadcast_ipv6 = GNUNET_NO;
2319 if (plugin->sockv6 != NULL)
2321 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
2323 inet_pton (AF_INET6, "FF05::13B",
2324 &plugin->ipv6_multicast_address.sin6_addr));
2326 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
2327 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
2329 plugin->broadcast_ipv6_mst =
2330 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
2332 /* Create IPv6 multicast request */
2333 struct ipv6_mreq multicastRequest;
2335 multicastRequest.ipv6mr_multiaddr =
2336 plugin->ipv6_multicast_address.sin6_addr;
2337 /* TODO: 0 selects the "best" interface, tweak to use all interfaces
2339 * http://tools.ietf.org/html/rfc2553#section-5.2:
2343 * Join a multicast group on a specified local interface. If the
2344 * interface index is specified as 0, the kernel chooses the local
2345 * interface. For example, some kernels look up the multicast
2346 * group in the normal IPv6 routing table and using the resulting
2349 multicastRequest.ipv6mr_interface = 0;
2351 /* Join the multicast group */
2352 if (GNUNET_NETWORK_socket_setsockopt
2353 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
2354 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
2356 LOG (GNUNET_ERROR_TYPE_WARNING,
2357 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
2362 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
2364 plugin->send_ipv6_broadcast_task =
2365 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
2366 plugin->broadcast_ipv6 = GNUNET_YES;
2371 if (sockets_created == 0)
2372 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2374 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
2375 (const struct sockaddr **) addrs, addrlens,
2376 &udp_nat_port_map_callback, NULL, plugin);
2381 * Shutdown the plugin.
2383 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
2387 libgnunet_plugin_transport_udp_done (void *cls)
2389 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2390 struct Plugin *plugin = api->cls;
2391 struct ReceiveContext *rc;
2393 /* FIXME: clean up heap and hashmap */
2394 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
2396 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2397 plugin->sessions = NULL;
2398 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
2399 &destroy_inbound_session, NULL);
2400 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
2401 plugin->inbound_sessions = NULL;
2402 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
2404 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2407 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
2409 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2411 GNUNET_SCHEDULER_cancel (plugin->select_task);
2412 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2415 if (plugin->broadcast_ipv4)
2417 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2419 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
2420 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2423 if (plugin->broadcast_ipv4_mst != NULL)
2424 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
2426 while (plugin->ipv4_broadcast_head != NULL)
2428 struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
2430 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
2431 plugin->ipv4_broadcast_tail, p);
2432 GNUNET_free (p->addr);
2437 if (plugin->broadcast_ipv6)
2439 /* Create IPv6 multicast request */
2440 struct ipv6_mreq multicastRequest;
2442 multicastRequest.ipv6mr_multiaddr =
2443 plugin->ipv6_multicast_address.sin6_addr;
2444 multicastRequest.ipv6mr_interface = 0;
2446 /* Join the multicast address */
2447 if (GNUNET_NETWORK_socket_setsockopt
2448 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
2449 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
2451 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
2456 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
2460 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2462 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
2463 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2465 if (plugin->broadcast_ipv6_mst != NULL)
2466 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
2470 if (plugin->sockv4 != NULL)
2472 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2473 plugin->sockv4 = NULL;
2475 if (plugin->sockv6 != NULL)
2477 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2478 plugin->sockv6 = NULL;
2481 GNUNET_SERVER_mst_destroy (plugin->mst);
2482 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2484 GNUNET_NAT_unregister (plugin->nat);
2486 GNUNET_free (plugin);
2491 /* end of plugin_transport_udp.c */