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)
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);
619 static struct Session *
620 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
621 const void *addr, size_t addrlen,
622 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
624 struct Session *peer_session;
625 const struct IPv4UdpAddress *t4;
626 const struct IPv6UdpAddress *t6;
627 struct sockaddr_in *v4;
628 struct sockaddr_in6 *v6;
630 struct GNUNET_ATS_Information ats;
634 case sizeof (struct IPv4UdpAddress):
635 if (NULL == plugin->sockv4)
641 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
642 len = sizeof (struct sockaddr_in);
643 v4 = (struct sockaddr_in *) &peer_session[1];
644 v4->sin_family = AF_INET;
645 #if HAVE_SOCKADDR_IN_SIN_LEN
646 v4->sin_len = sizeof (struct sockaddr_in);
648 v4->sin_port = t4->u4_port;
649 v4->sin_addr.s_addr = t4->ipv4_addr;
650 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
652 case sizeof (struct IPv6UdpAddress):
653 if (NULL == plugin->sockv6)
659 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
660 len = sizeof (struct sockaddr_in6);
661 v6 = (struct sockaddr_in6 *) &peer_session[1];
662 v6->sin6_family = AF_INET6;
663 #if HAVE_SOCKADDR_IN_SIN_LEN
664 v6->sin6_len = sizeof (struct sockaddr_in6);
666 v6->sin6_port = t6->u6_port;
667 v6->sin6_addr = t6->ipv6_addr;
668 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
671 /* Must have a valid address to send to */
676 peer_session->ats_address_network_type = ats.value;
677 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
678 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
679 peer_session->addrlen = len;
680 peer_session->target = *target;
681 peer_session->plugin = plugin;
682 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
683 peer_session->cont = cont;
684 peer_session->cont_cls = cont_cls;
690 static int session_cmp_it (void *cls,
691 const GNUNET_HashCode * key,
696 struct GNUNET_HELLO_Address *address = cls;
697 struct Session *s = value;
699 struct IPv4UdpAddress * u4 = NULL;
700 struct IPv4UdpAddress * u6 = NULL;
704 if ((s->addrlen == address->address_length) &&
705 (0 == memcmp (s->sock_addr, address->address, address->address_length)))
715 * Creates a new outbound session the transport service will use to send data to the
718 * @param cls the plugin
719 * @param address the address
720 * @return the session or NULL of max connections exceeded
723 static struct Session *
724 udp_plugin_get_session (void *cls,
725 const struct GNUNET_HELLO_Address *address)
727 struct Session * s = NULL;
728 struct Plugin * plugin = cls;
730 GNUNET_assert (plugin != NULL);
731 GNUNET_assert (address != NULL);
733 if ((address->address == NULL) ||
734 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
735 (address->address_length != sizeof (struct IPv6UdpAddress))))
741 /* check if session already exists */
742 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, (void *) address);
744 /* otherwise create new */
745 s = create_session (plugin,
748 address->address_length,
755 * Function that can be used by the transport service to transmit
756 * a message using the plugin. Note that in the case of a
757 * peer disconnecting, the continuation MUST be called
758 * prior to the disconnect notification itself. This function
759 * will be called with this peer's HELLO message to initiate
760 * a fresh connection to another peer.
763 * @param session which session must be used
764 * @param msgbuf the message to transmit
765 * @param msgbuf_size number of bytes in 'msgbuf'
766 * @param priority how important is the message (most plugins will
767 * ignore message priority and just FIFO)
768 * @param to how long to wait at most for the transmission (does not
769 * require plugins to discard the message after the timeout,
770 * just advisory for the desired delay; most plugins will ignore
772 * @param cont continuation to call once the message has
773 * been transmitted (or if the transport is ready
774 * for the next transmission call; or if the
775 * peer disconnected...); can be NULL
776 * @param cont_cls closure for cont
777 * @return number of bytes used (on the physical network, with overheads);
778 * -1 on hard errors (i.e. address invalid); 0 is a legal value
779 * and does NOT mean that the message was not transmitted (DV)
782 udp_plugin_send (void *cls,
783 struct Session *session,
784 const char *msgbuf, size_t msgbuf_size,
785 unsigned int priority,
786 struct GNUNET_TIME_Relative to,
787 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "To be implemented\n");
797 * Actually send out the message.
799 * @param plugin the plugin
800 * @param sa the address to send the message to
801 * @param msg message to transmit
802 * @return the number of bytes written
805 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
806 const struct GNUNET_MessageHeader *msg)
811 switch (sa->sa_family)
814 if (NULL == plugin->sockv4)
817 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
818 sa, slen = sizeof (struct sockaddr_in));
821 if (NULL == plugin->sockv6)
824 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
825 sa, slen = sizeof (struct sockaddr_in6));
831 if (GNUNET_SYSERR == sent)
833 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
834 LOG (GNUNET_ERROR_TYPE_ERROR,
835 "UDP transmited %u-byte message to %s (%d: %s)\n",
836 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
837 (sent < 0) ? STRERROR (errno) : "ok");
840 LOG (GNUNET_ERROR_TYPE_DEBUG,
841 "UDP transmited %u-byte message to %s (%d: %s)\n",
842 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
843 (sent < 0) ? STRERROR (errno) : "ok");
849 * Function that is called with messages created by the fragmentation
850 * module. In the case of the 'proc' callback of the
851 * GNUNET_FRAGMENT_context_create function, this function must
852 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
854 * @param cls closure, the 'struct PeerSession'
855 * @param msg the message that was created
858 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
860 struct Session *session = cls;
862 udp_send (session->plugin, session->sock_addr, msg);
863 GNUNET_FRAGMENT_context_transmission_done (session->frag);
868 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
872 udp_call_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 struct Session *s = cls;
875 GNUNET_TRANSPORT_TransmitContinuation cont = s->cont;
877 s->delayed_cont_task = GNUNET_SCHEDULER_NO_TASK;
879 cont (s->cont_cls, &s->target, GNUNET_OK);
884 * Function that can be used by the transport service to transmit
885 * a message using the plugin.
888 * @param target who should receive this message (ignored by UDP)
889 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
890 * @param msgbuf_size the size of the msgbuf to send
891 * @param priority how important is the message (ignored by UDP)
892 * @param timeout when should we time out (give up) if we can not transmit?
893 * @param session identifier used for this session (NULL for UDP)
894 * @param addr the addr to send the message to
895 * @param addrlen the len of addr
896 * @param force_address not used, we had better have an address to send to
897 * because we are stateless!!
898 * @param cont continuation to call once the message has
899 * been transmitted (or if the transport is ready
900 * for the next transmission call; or if the
901 * peer disconnected...)
902 * @param cont_cls closure for cont
904 * @return the number of bytes written (may return 0 and the message can
905 * still be transmitted later!)
908 udp_plugin_send_old (void *cls, const struct GNUNET_PeerIdentity *target,
909 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
910 struct GNUNET_TIME_Relative timeout, struct Session *session,
911 const void *addr, size_t addrlen, int force_address,
912 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
914 struct Plugin *plugin = cls;
915 struct Session *peer_session;
917 const struct IPv4UdpAddress *t4;
918 const struct IPv6UdpAddress *t6;
919 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
921 struct UDPMessage *udp;
922 struct GNUNET_TIME_Relative delta;
924 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
927 return GNUNET_SYSERR;
930 LOG (GNUNET_ERROR_TYPE_DEBUG,
931 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
932 msgbuf_size, GNUNET_i2s (target), udp_address_to_string (NULL, addr,
934 session, force_address);
936 if ((force_address == GNUNET_SYSERR) && (session == NULL))
937 return GNUNET_SYSERR;
940 /* safety check: comparing address to address stored in session */
941 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
944 GNUNET_assert (GNUNET_YES ==
945 GNUNET_CONTAINER_multihashmap_contains_value
946 (plugin->inbound_sessions, &target->hashPubKey, s));
948 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
949 return GNUNET_SYSERR;
952 case sizeof (struct IPv4UdpAddress):
953 if (NULL == plugin->sockv4)
956 cont (cont_cls, target, GNUNET_SYSERR);
957 return GNUNET_SYSERR;
960 if (s->addrlen != (sizeof (struct sockaddr_in)))
961 return GNUNET_SYSERR;
962 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
964 GNUNET_assert (a4->sin_port == t4->u4_port);
966 memcmp (&a4->sin_addr, &t4->ipv4_addr,
967 sizeof (struct in_addr)));
968 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
971 case sizeof (struct IPv6UdpAddress):
972 if (NULL == plugin->sockv6)
975 cont (cont_cls, target, GNUNET_SYSERR);
976 return GNUNET_SYSERR;
979 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
980 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
982 GNUNET_assert (a6->sin6_port == t6->u6_port);
984 memcmp (&a6->sin6_addr, &t6->ipv6_addr,
985 sizeof (struct in6_addr)));
986 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
990 /* Must have a valid address to send to */
995 if ((addr == NULL) || (addrlen == 0))
996 return GNUNET_SYSERR;
997 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
998 if (peer_session == NULL)
1001 cont (cont_cls, target, GNUNET_SYSERR);
1002 return GNUNET_SYSERR;;
1009 udp = (struct UDPMessage *) mbuf;
1010 udp->header.size = htons (mlen);
1011 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1012 udp->reserved = htonl (0);
1013 udp->sender = *plugin->env->my_identity;
1014 memcpy (&udp[1], msgbuf, msgbuf_size);
1017 delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
1019 delta = GNUNET_TIME_UNIT_ZERO;
1020 if (mlen <= UDP_MTU)
1022 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
1025 if ((delta.rel_value > 0) && (mlen > 0))
1028 s->cont_cls = cont_cls;
1029 s->delayed_cont_task =
1030 GNUNET_SCHEDULER_add_delayed (delta, &udp_call_continuation, s);
1033 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
1035 GNUNET_free_non_null (peer_session);
1039 GNUNET_assert (GNUNET_OK ==
1040 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1041 &target->hashPubKey,
1043 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1044 peer_session->frag =
1045 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1047 plugin->last_expected_delay,
1048 &udp->header, &send_fragment,
1056 * Closure for 'process_inbound_tokenized_messages'
1058 struct SourceInformation
1063 struct GNUNET_PeerIdentity sender;
1071 * Number of bytes in source address.
1075 struct Session *session;
1080 * Message tokenizer has broken up an incomming message. Pass it on
1083 * @param cls the 'struct Plugin'
1084 * @param client the 'struct SourceInformation'
1085 * @param hdr the actual message
1088 process_inbound_tokenized_messages (void *cls, void *client,
1089 const struct GNUNET_MessageHeader *hdr)
1091 struct Plugin *plugin = cls;
1092 struct SourceInformation *si = client;
1093 struct GNUNET_ATS_Information atsi[2];
1094 struct GNUNET_TIME_Relative delay;
1097 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1098 atsi[0].value = htonl (1);
1099 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1100 atsi[1].value = si->session->ats_address_network_type;
1101 GNUNET_break (ntohl(si->session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving Session %X %s to transport\n",
1105 si->session, GNUNET_i2s (&si->session->target));
1107 plugin->env->receive (plugin->env->cls, &si->sender, hdr,
1108 (const struct GNUNET_ATS_Information *) &atsi, 2,
1109 si->session, si->arg, si->args);
1110 si->session->flow_delay_for_other_peer = delay;
1114 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1116 struct Session *s = cls;
1118 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %X (`%s') is now invalid\n", s,
1120 GNUNET_a2s (s->sock_addr, s->addrlen));
1122 s->plugin->env->session_end (s->plugin->env->cls, &s->target, s);
1123 GNUNET_assert (GNUNET_YES ==
1124 GNUNET_CONTAINER_multihashmap_remove (s->
1125 plugin->inbound_sessions,
1126 &s->target.hashPubKey,
1133 * We've received a UDP Message. Process it (pass contents to main service).
1135 * @param plugin plugin context
1136 * @param msg the message
1137 * @param sender_addr sender address
1138 * @param sender_addr_len number of bytes in sender_addr
1141 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1142 const struct sockaddr *sender_addr,
1143 socklen_t sender_addr_len)
1145 struct SourceInformation si;
1146 struct IPv4UdpAddress u4;
1147 struct IPv6UdpAddress u6;
1148 struct GNUNET_ATS_Information ats;
1152 if (0 != ntohl (msg->reserved))
1154 GNUNET_break_op (0);
1157 if (ntohs (msg->header.size) <
1158 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1160 GNUNET_break_op (0);
1164 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1165 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1166 /* convert address */
1167 switch (sender_addr->sa_family)
1170 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1171 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1172 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1177 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1178 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1179 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "Received message with %u bytes from peer `%s' at `%s'\n",
1190 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1191 GNUNET_a2s (sender_addr, sender_addr_len));
1194 /* create a session for inbound connections */
1195 const struct UDPMessage *udp_msg = (const struct UDPMessage *) msg;
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
1199 GNUNET_i2s (&udp_msg->sender), udp_address_to_string (NULL, arg, args));
1201 struct Session *s = NULL;
1203 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr,
1208 LOG (GNUNET_ERROR_TYPE_DEBUG,
1209 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
1210 s, GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1214 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
1215 ats = plugin->env->get_address_type (plugin->env->cls, sender_addr, sender_addr_len);
1216 s->ats_address_network_type = ats.value;
1217 LOG (GNUNET_ERROR_TYPE_DEBUG,
1218 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n", s,
1219 GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1221 GNUNET_assert (GNUNET_OK ==
1222 GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
1223 &s->target.hashPubKey, s,
1224 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1227 GNUNET_TIME_relative_to_absolute
1228 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1229 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
1231 GNUNET_SCHEDULER_cancel (s->invalidation_task);
1232 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rescheduling %X' `%s'\n", s,
1234 udp_address_to_string (NULL, arg, args));
1236 s->invalidation_task =
1237 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1238 &invalidation_task, s);
1239 /* iterate over all embedded messages */
1240 si.sender = msg->sender;
1244 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1245 ntohs (msg->header.size) -
1246 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1251 * Process a defragmented message.
1253 * @param cls the 'struct ReceiveContext'
1254 * @param msg the message
1257 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1259 struct ReceiveContext *rc = cls;
1261 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1266 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1271 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1272 rc->src_addr, rc->addr_len);
1277 * Transmit an acknowledgement.
1279 * @param cls the 'struct ReceiveContext'
1280 * @param id message ID (unused)
1281 * @param msg ack to transmit
1284 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1286 struct ReceiveContext *rc = cls;
1288 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1290 struct UDP_ACK_Message *udp_ack;
1295 s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
1298 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1299 delay = s->flow_delay_for_other_peer.rel_value;
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "Sending ACK to `%s' including delay of %u ms\n",
1308 GNUNET_a2s (rc->src_addr,
1309 (rc->src_addr->sa_family ==
1310 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1314 udp_ack = (struct UDP_ACK_Message *) buf;
1315 udp_ack->header.size = htons ((uint16_t) msize);
1316 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1317 udp_ack->delay = htonl (delay);
1318 udp_ack->sender = *rc->plugin->env->my_identity;
1319 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1320 (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
1325 * Closure for 'find_receive_context'.
1327 struct FindReceiveContext
1330 * Where to store the result.
1332 struct ReceiveContext *rc;
1337 const struct sockaddr *addr;
1340 * Number of bytes in 'addr'.
1344 struct Session *session;
1349 * Scan the heap for a receive context with the given address.
1351 * @param cls the 'struct FindReceiveContext'
1352 * @param node internal node of the heap
1353 * @param element value stored at the node (a 'struct ReceiveContext')
1354 * @param cost cost associated with the node
1355 * @return GNUNET_YES if we should continue to iterate,
1359 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1360 void *element, GNUNET_CONTAINER_HeapCostType cost)
1362 struct FindReceiveContext *frc = cls;
1363 struct ReceiveContext *e = element;
1365 if ((frc->addr_len == e->addr_len) &&
1366 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1376 struct Plugin *plugin;
1378 struct IPv4UdpAddress addr;
1380 * ATS network type in NBO
1382 uint32_t ats_address_network_type;
1387 struct Plugin *plugin;
1389 struct IPv6UdpAddress addr;
1391 * ATS network type in NBO
1393 uint32_t ats_address_network_type;
1398 * Read and process a message from the given socket.
1400 * @param plugin the overall plugin
1401 * @param rsock socket to read from
1404 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1410 const struct GNUNET_MessageHeader *msg;
1411 const struct GNUNET_MessageHeader *ack;
1412 struct Session *peer_session;
1413 const struct UDP_ACK_Message *udp_ack;
1414 struct ReceiveContext *rc;
1415 struct GNUNET_TIME_Absolute now;
1416 struct FindReceiveContext frc;
1417 struct Session *s = NULL;
1418 struct GNUNET_TIME_Relative flow_delay;
1419 struct GNUNET_ATS_Information ats;
1421 fromlen = sizeof (addr);
1422 memset (&addr, 0, sizeof (addr));
1424 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1425 (struct sockaddr *) &addr, &fromlen);
1426 if (ret < sizeof (struct GNUNET_MessageHeader))
1428 GNUNET_break_op (0);
1431 msg = (const struct GNUNET_MessageHeader *) buf;
1433 LOG (GNUNET_ERROR_TYPE_DEBUG,
1434 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1435 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1437 if (ret != ntohs (msg->size))
1439 GNUNET_break_op (0);
1442 switch (ntohs (msg->type))
1444 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1446 if (fromlen == sizeof (struct sockaddr_in))
1448 LOG (GNUNET_ERROR_TYPE_DEBUG,
1449 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
1450 ret, GNUNET_a2s ((const struct sockaddr *) &addr, fromlen));
1452 struct Mstv4Context *mc;
1454 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
1455 struct sockaddr_in *av4 = (struct sockaddr_in *) &addr;
1457 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
1458 mc->addr.u4_port = av4->sin_port;
1459 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &addr, fromlen);
1460 mc->ats_address_network_type = ats.value;
1462 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, ret,
1463 GNUNET_NO, GNUNET_NO))
1466 else if (fromlen == sizeof (struct sockaddr_in6))
1468 LOG (GNUNET_ERROR_TYPE_DEBUG,
1469 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
1470 ret, GNUNET_a2s ((const struct sockaddr *) &addr, fromlen));
1472 struct Mstv6Context *mc;
1474 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
1475 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) &addr;
1477 mc->addr.ipv6_addr = av6->sin6_addr;
1478 mc->addr.u6_port = av6->sin6_port;
1479 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &addr, fromlen);
1480 mc->ats_address_network_type = ats.value;
1483 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, ret,
1484 GNUNET_NO, GNUNET_NO))
1489 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1490 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1492 GNUNET_break_op (0);
1495 process_udp_message (plugin, (const struct UDPMessage *) msg,
1496 (const struct sockaddr *) addr, fromlen);
1498 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1500 if (ntohs (msg->size) <
1501 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1503 GNUNET_break_op (0);
1506 udp_ack = (const struct UDP_ACK_Message *) msg;
1507 s = find_inbound_session (plugin, &udp_ack->sender, addr, fromlen);
1510 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1513 flow_delay.rel_value);
1515 s->flow_delay_from_other_peer =
1516 GNUNET_TIME_relative_to_absolute (flow_delay);
1518 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1519 if (ntohs (ack->size) !=
1520 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1522 GNUNET_break_op (0);
1526 LOG (GNUNET_ERROR_TYPE_DEBUG,
1527 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1528 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1529 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1532 peer_session = find_session (plugin, &udp_ack->sender);
1533 if (NULL == peer_session)
1536 LOG (GNUNET_ERROR_TYPE_DEBUG,
1537 "Session for ACK not found, dropping ACK!\n");
1541 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1543 GNUNET_assert (GNUNET_OK ==
1544 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1548 plugin->last_expected_delay =
1549 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1550 if (peer_session->cont != NULL)
1551 peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
1552 GNUNET_free (peer_session);
1554 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1556 frc.addr = (const struct sockaddr *) addr;
1557 frc.addr_len = fromlen;
1558 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1560 now = GNUNET_TIME_absolute_get ();
1564 /* need to create a new RC */
1565 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1566 memcpy (&rc[1], addr, fromlen);
1567 rc->src_addr = (const struct sockaddr *) &rc[1];
1568 rc->addr_len = fromlen;
1569 rc->plugin = plugin;
1571 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1572 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1573 &fragment_msg_proc, &ack_proc);
1575 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1576 (GNUNET_CONTAINER_HeapCostType)
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1581 (unsigned int) ntohs (msg->size),
1582 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1585 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1587 /* keep this 'rc' from expiring */
1588 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1589 (GNUNET_CONTAINER_HeapCostType)
1592 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1593 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1595 /* remove 'rc' that was inactive the longest */
1596 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1597 GNUNET_assert (NULL != rc);
1598 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1603 GNUNET_break_op (0);
1610 * We have been notified that our writeset has something to read. We don't
1611 * know which socket needs to be read, so we have to check each one
1612 * Then reschedule this function to be called again once more is available.
1614 * @param cls the plugin handle
1615 * @param tc the scheduling context (for rescheduling this function again)
1618 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1620 struct Plugin *plugin = cls;
1622 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1623 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1625 if ((NULL != plugin->sockv4) &&
1626 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1627 udp_read (plugin, plugin->sockv4);
1628 if ((NULL != plugin->sockv6) &&
1629 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1630 udp_read (plugin, plugin->sockv6);
1631 plugin->select_task =
1632 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1633 GNUNET_SCHEDULER_NO_TASK,
1634 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1635 NULL, &udp_plugin_select, plugin);
1641 broadcast_ipv4_mst_cb (void *cls, void *client,
1642 const struct GNUNET_MessageHeader *message)
1644 struct Plugin *plugin = cls;
1645 struct Mstv4Context *mc = client;
1646 const struct GNUNET_MessageHeader *hello;
1647 struct UDP_Beacon_Message *msg;
1649 msg = (struct UDP_Beacon_Message *) message;
1651 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1652 ntohs (msg->header.type))
1655 LOG (GNUNET_ERROR_TYPE_DEBUG,
1656 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1657 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1658 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1660 struct GNUNET_ATS_Information atsi[2];
1663 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1664 atsi[0].value = htonl (1);
1665 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1666 atsi[1].value = mc->ats_address_network_type;
1667 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
1669 hello = (struct GNUNET_MessageHeader *) &msg[1];
1670 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
1671 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
1672 (const char *) &mc->addr, sizeof (mc->addr));
1674 GNUNET_STATISTICS_update (plugin->env->stats,
1676 ("# IPv4 broadcast HELLO beacons received via udp"),
1683 broadcast_ipv6_mst_cb (void *cls, void *client,
1684 const struct GNUNET_MessageHeader *message)
1687 struct Plugin *plugin = cls;
1688 struct Mstv6Context *mc = client;
1689 const struct GNUNET_MessageHeader *hello;
1690 struct UDP_Beacon_Message *msg;
1692 msg = (struct UDP_Beacon_Message *) message;
1694 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1695 ntohs (msg->header.type))
1698 LOG (GNUNET_ERROR_TYPE_DEBUG,
1699 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1700 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1701 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1703 struct GNUNET_ATS_Information atsi[2];
1706 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1707 atsi[0].value = htonl (1);
1708 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1709 atsi[1].value = mc->ats_address_network_type;
1710 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
1712 hello = (struct GNUNET_MessageHeader *) &msg[1];
1713 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
1714 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
1715 (const char *) &mc->addr, sizeof (mc->addr));
1717 GNUNET_STATISTICS_update (plugin->env->stats,
1719 ("# IPv6 multicast HELLO beacons received via udp"),
1726 udp_ipv4_broadcast_send (void *cls,
1727 const struct GNUNET_SCHEDULER_TaskContext *tc)
1729 struct Plugin *plugin = cls;
1732 uint16_t hello_size;
1735 const struct GNUNET_MessageHeader *hello;
1736 struct UDP_Beacon_Message *msg;
1737 struct BroadcastAddress *baddr;
1739 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1741 hello = plugin->env->get_our_hello ();
1742 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1743 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1745 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1746 (msg_size > (UDP_MTU)))
1749 msg = (struct UDP_Beacon_Message *) buf;
1750 msg->sender = *(plugin->env->my_identity);
1751 msg->header.size = ntohs (msg_size);
1752 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1753 memcpy (&msg[1], hello, hello_size);
1756 baddr = plugin->ipv4_broadcast_head;
1758 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
1760 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
1762 addr->sin_port = htons (plugin->port);
1765 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, msg_size,
1766 (const struct sockaddr *) addr,
1768 if (sent == GNUNET_SYSERR)
1769 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1771 LOG (GNUNET_ERROR_TYPE_DEBUG,
1772 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
1773 GNUNET_a2s (baddr->addr, baddr->addrlen));
1774 baddr = baddr->next;
1777 plugin->send_ipv4_broadcast_task =
1778 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1779 &udp_ipv4_broadcast_send, plugin);
1783 udp_ipv6_broadcast_send (void *cls,
1784 const struct GNUNET_SCHEDULER_TaskContext *tc)
1786 struct Plugin *plugin = cls;
1789 uint16_t hello_size;
1792 const struct GNUNET_MessageHeader *hello;
1793 struct UDP_Beacon_Message *msg;
1795 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1797 hello = plugin->env->get_our_hello ();
1798 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1799 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1801 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1802 (msg_size > (UDP_MTU)))
1805 msg = (struct UDP_Beacon_Message *) buf;
1806 msg->sender = *(plugin->env->my_identity);
1807 msg->header.size = ntohs (msg_size);
1808 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1809 memcpy (&msg[1], hello, hello_size);
1813 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, msg_size,
1814 (const struct sockaddr *)
1815 &plugin->ipv6_multicast_address,
1816 sizeof (struct sockaddr_in6));
1817 if (sent == GNUNET_SYSERR)
1818 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1820 LOG (GNUNET_ERROR_TYPE_DEBUG,
1821 "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
1823 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
1824 sizeof (struct sockaddr_in6)));
1828 plugin->send_ipv6_broadcast_task =
1829 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1830 &udp_ipv6_broadcast_send, plugin);
1834 * Check if the given port is plausible (must be either our listen
1835 * port or our advertised port). If it is neither, we return
1838 * @param plugin global variables
1839 * @param in_port port number to check
1840 * @return GNUNET_OK if port is either open_port or adv_port
1843 check_port (struct Plugin *plugin, uint16_t in_port)
1845 if ((in_port == plugin->port) || (in_port == plugin->aport))
1847 return GNUNET_SYSERR;
1852 * Function that will be called to check if a binary address for this
1853 * plugin is well-formed and corresponds to an address for THIS peer
1854 * (as per our configuration). Naturally, if absolutely necessary,
1855 * plugins can be a bit conservative in their answer, but in general
1856 * plugins should make sure that the address does not redirect
1857 * traffic to a 3rd party that might try to man-in-the-middle our
1860 * @param cls closure, should be our handle to the Plugin
1861 * @param addr pointer to the address
1862 * @param addrlen length of addr
1863 * @return GNUNET_OK if this is a plausible address for this peer
1864 * and transport, GNUNET_SYSERR if not
1868 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1870 struct Plugin *plugin = cls;
1871 struct IPv4UdpAddress *v4;
1872 struct IPv6UdpAddress *v6;
1874 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1875 (addrlen != sizeof (struct IPv6UdpAddress)))
1877 GNUNET_break_op (0);
1878 return GNUNET_SYSERR;
1880 if (addrlen == sizeof (struct IPv4UdpAddress))
1882 v4 = (struct IPv4UdpAddress *) addr;
1883 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1884 return GNUNET_SYSERR;
1886 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1887 sizeof (struct in_addr)))
1888 return GNUNET_SYSERR;
1892 v6 = (struct IPv6UdpAddress *) addr;
1893 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1895 GNUNET_break_op (0);
1896 return GNUNET_SYSERR;
1898 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1899 return GNUNET_SYSERR;
1901 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1902 sizeof (struct in6_addr)))
1903 return GNUNET_SYSERR;
1910 * Function called for a quick conversion of the binary address to
1911 * a numeric address. Note that the caller must not free the
1912 * address and that the next call to this function is allowed
1913 * to override the address again.
1915 * @param cls closure
1916 * @param addr binary address
1917 * @param addrlen length of the address
1918 * @return string representing the same address
1921 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1923 static char rbuf[INET6_ADDRSTRLEN + 10];
1924 char buf[INET6_ADDRSTRLEN];
1928 const struct IPv4UdpAddress *t4;
1929 const struct IPv6UdpAddress *t6;
1933 if (addrlen == sizeof (struct IPv6UdpAddress))
1937 port = ntohs (t6->u6_port);
1938 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1941 else if (addrlen == sizeof (struct IPv4UdpAddress))
1945 port = ntohs (t4->u4_port);
1946 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1951 GNUNET_break_op (0);
1954 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1955 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1962 * Closure for 'append_port'.
1964 struct PrettyPrinterContext
1967 * Function to call with the result.
1969 GNUNET_TRANSPORT_AddressStringCallback asc;
1972 * Clsoure for 'asc'.
1977 * Port to add after the IP address.
1984 * Append our port and forward the result.
1986 * @param cls a 'struct PrettyPrinterContext'
1987 * @param hostname result from DNS resolver
1990 append_port (void *cls, const char *hostname)
1992 struct PrettyPrinterContext *ppc = cls;
1995 if (hostname == NULL)
1997 ppc->asc (ppc->asc_cls, NULL);
2001 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
2002 ppc->asc (ppc->asc_cls, ret);
2008 * Convert the transports address to a nice, human-readable
2011 * @param cls closure
2012 * @param type name of the transport that generated the address
2013 * @param addr one of the addresses of the host, NULL for the last address
2014 * the specific address format depends on the transport
2015 * @param addrlen length of the address
2016 * @param numeric should (IP) addresses be displayed in numeric form?
2017 * @param timeout after how long should we give up?
2018 * @param asc function to call on each string
2019 * @param asc_cls closure for asc
2022 udp_plugin_address_pretty_printer (void *cls, const char *type,
2023 const void *addr, size_t addrlen,
2025 struct GNUNET_TIME_Relative timeout,
2026 GNUNET_TRANSPORT_AddressStringCallback asc,
2029 struct PrettyPrinterContext *ppc;
2032 struct sockaddr_in a4;
2033 struct sockaddr_in6 a6;
2034 const struct IPv4UdpAddress *u4;
2035 const struct IPv6UdpAddress *u6;
2038 if (addrlen == sizeof (struct IPv6UdpAddress))
2041 memset (&a6, 0, sizeof (a6));
2042 a6.sin6_family = AF_INET6;
2043 #if HAVE_SOCKADDR_IN_SIN_LEN
2044 a6.sin6_len = sizeof (a6);
2046 a6.sin6_port = u6->u6_port;
2047 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
2048 port = ntohs (u6->u6_port);
2052 else if (addrlen == sizeof (struct IPv4UdpAddress))
2055 memset (&a4, 0, sizeof (a4));
2056 a4.sin_family = AF_INET;
2057 #if HAVE_SOCKADDR_IN_SIN_LEN
2058 a4.sin_len = sizeof (a4);
2060 a4.sin_port = u4->u4_port;
2061 a4.sin_addr.s_addr = u4->ipv4_addr;
2062 port = ntohs (u4->u4_port);
2068 /* invalid address */
2069 GNUNET_break_op (0);
2070 asc (asc_cls, NULL);
2073 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
2075 ppc->asc_cls = asc_cls;
2077 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
2082 * Our external IP address/port mapping has changed.
2084 * @param cls closure, the 'struct LocalAddrList'
2085 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2086 * the previous (now invalid) one
2087 * @param addr either the previous or the new public IP address
2088 * @param addrlen actual lenght of the address
2091 udp_nat_port_map_callback (void *cls, int add_remove,
2092 const struct sockaddr *addr, socklen_t addrlen)
2094 struct Plugin *plugin = cls;
2095 struct IPv4UdpAddress u4;
2096 struct IPv6UdpAddress u6;
2100 /* convert 'addr' to our internal format */
2101 switch (addr->sa_family)
2104 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2105 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2106 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
2111 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
2112 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
2113 sizeof (struct in6_addr));
2114 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
2122 /* modify our published address list */
2123 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
2128 iface_proc (void *cls, const char *name, int isDefault,
2129 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
2130 const struct sockaddr *netmask, socklen_t addrlen)
2132 struct Plugin *plugin = cls;
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
2137 GNUNET_a2s (addr, addrlen), name, addr);
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 "broadcast address %s for interface %s %p\n ",
2140 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
2142 GNUNET_a2s (netmask, addrlen), name, netmask);
2145 /* Collecting broadcast addresses */
2146 if (broadcast_addr != NULL)
2148 struct BroadcastAddress *ba =
2149 GNUNET_malloc (sizeof (struct BroadcastAddress));
2150 ba->addr = GNUNET_malloc (addrlen);
2151 memcpy (ba->addr, broadcast_addr, addrlen);
2152 ba->addrlen = addrlen;
2153 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
2154 plugin->ipv4_broadcast_tail, ba);
2162 * The exported method. Makes the core api available via a global and
2163 * returns the udp transport API.
2165 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2166 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2169 libgnunet_plugin_transport_udp_init (void *cls)
2171 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2172 unsigned long long port;
2173 unsigned long long aport;
2174 struct GNUNET_TRANSPORT_PluginFunctions *api;
2175 struct Plugin *plugin;
2176 int sockets_created;
2178 struct GNUNET_TIME_Relative interval;
2179 struct sockaddr_in serverAddrv4;
2180 struct sockaddr_in6 serverAddrv6;
2181 struct sockaddr *serverAddr;
2182 struct sockaddr *addrs[2];
2183 socklen_t addrlens[2];
2186 unsigned long long udp_max_bps;
2189 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2194 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2196 if (broadcast == GNUNET_SYSERR)
2197 broadcast = GNUNET_NO;
2199 if (GNUNET_SYSERR ==
2200 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
2201 "BROADCAST_INTERVAL", &interval))
2202 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2205 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2206 "MAX_BPS", &udp_max_bps))
2207 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2209 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2210 "ADVERTISED_PORT", &aport))
2214 LOG (GNUNET_ERROR_TYPE_WARNING,
2215 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2219 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2220 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2222 plugin = GNUNET_malloc (sizeof (struct Plugin));
2223 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2224 GNUNET_BANDWIDTH_value_init ((uint32_t)
2226 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
2227 plugin->port = port;
2228 plugin->aport = aport;
2230 plugin->broadcast_interval = interval;
2231 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2234 api->send = &udp_plugin_send_old;
2235 api->disconnect = &udp_disconnect;
2236 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2237 api->address_to_string = &udp_address_to_string;
2238 api->check_address = &udp_plugin_check_address;
2240 api->get_session = &udp_plugin_get_session;
2241 api->send_with_session = &udp_plugin_send;
2244 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2245 "BINDTO", &plugin->bind4_address))
2247 LOG (GNUNET_ERROR_TYPE_DEBUG,
2248 "Binding udp plugin to specific address: `%s'\n",
2249 plugin->bind4_address);
2250 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
2252 GNUNET_free (plugin->bind4_address);
2253 GNUNET_free (plugin);
2260 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2261 "BINDTO6", &plugin->bind6_address))
2263 LOG (GNUNET_ERROR_TYPE_DEBUG,
2264 "Binding udp plugin to specific address: `%s'\n",
2265 plugin->bind6_address);
2267 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
2269 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2270 plugin->bind6_address);
2271 GNUNET_free_non_null (plugin->bind4_address);
2272 GNUNET_free (plugin->bind6_address);
2273 GNUNET_free (plugin);
2280 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2282 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2284 plugin->inbound_sessions =
2285 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2287 sockets_created = 0;
2289 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
2292 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2293 if (NULL == plugin->sockv6)
2295 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2299 #if HAVE_SOCKADDR_IN_SIN_LEN
2300 serverAddrv6.sin6_len = sizeof (serverAddrv6);
2302 serverAddrv6.sin6_family = AF_INET6;
2303 serverAddrv6.sin6_addr = in6addr_any;
2304 serverAddrv6.sin6_port = htons (plugin->port);
2305 addrlen = sizeof (serverAddrv6);
2306 serverAddr = (struct sockaddr *) &serverAddrv6;
2308 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2309 ntohs (serverAddrv6.sin6_port));
2312 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2315 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2317 LOG (GNUNET_ERROR_TYPE_DEBUG,
2318 "IPv6 Binding failed, trying new port %d\n",
2319 ntohs (serverAddrv6.sin6_port));
2324 GNUNET_NETWORK_socket_close (plugin->sockv6);
2325 plugin->sockv6 = NULL;
2329 if (plugin->sockv6 != NULL)
2331 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2332 addrlens[sockets_created] = sizeof (serverAddrv6);
2339 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2340 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2341 if (NULL == plugin->sockv4)
2343 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2347 #if HAVE_SOCKADDR_IN_SIN_LEN
2348 serverAddrv4.sin_len = sizeof (serverAddrv4);
2350 serverAddrv4.sin_family = AF_INET;
2351 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2352 serverAddrv4.sin_port = htons (plugin->port);
2353 addrlen = sizeof (serverAddrv4);
2354 serverAddr = (struct sockaddr *) &serverAddrv4;
2356 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2357 ntohs (serverAddrv4.sin_port));
2360 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2363 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2365 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2366 ntohs (serverAddrv4.sin_port));
2371 GNUNET_NETWORK_socket_close (plugin->sockv4);
2372 plugin->sockv4 = NULL;
2376 if (plugin->sockv4 != NULL)
2378 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2379 addrlens[sockets_created] = sizeof (serverAddrv4);
2384 plugin->rs = GNUNET_NETWORK_fdset_create ();
2385 GNUNET_NETWORK_fdset_zero (plugin->rs);
2386 if (NULL != plugin->sockv4)
2387 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
2388 if (NULL != plugin->sockv6)
2389 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
2391 plugin->select_task =
2392 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2393 GNUNET_SCHEDULER_NO_TASK,
2394 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
2395 NULL, &udp_plugin_select, plugin);
2401 /* create IPv4 broadcast socket */
2402 plugin->broadcast_ipv4 = GNUNET_NO;
2403 if (plugin->sockv4 != NULL)
2407 if (GNUNET_NETWORK_socket_setsockopt
2408 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
2409 sizeof (int)) != GNUNET_OK)
2411 LOG (GNUNET_ERROR_TYPE_WARNING,
2413 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
2414 ntohs (serverAddrv4.sin_port));
2418 GNUNET_OS_network_interfaces_list (iface_proc, plugin);
2419 plugin->send_ipv4_broadcast_task =
2420 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
2422 plugin->broadcast_ipv4_mst =
2423 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
2425 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
2426 plugin->broadcast_ipv4 = GNUNET_YES;
2430 plugin->broadcast_ipv6 = GNUNET_NO;
2431 if (plugin->sockv6 != NULL)
2433 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
2435 inet_pton (AF_INET6, "FF05::13B",
2436 &plugin->ipv6_multicast_address.sin6_addr));
2438 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
2439 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
2441 plugin->broadcast_ipv6_mst =
2442 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
2444 /* Create IPv6 multicast request */
2445 struct ipv6_mreq multicastRequest;
2447 multicastRequest.ipv6mr_multiaddr =
2448 plugin->ipv6_multicast_address.sin6_addr;
2449 /* TODO: 0 selects the "best" interface, tweak to use all interfaces
2451 * http://tools.ietf.org/html/rfc2553#section-5.2:
2455 * Join a multicast group on a specified local interface. If the
2456 * interface index is specified as 0, the kernel chooses the local
2457 * interface. For example, some kernels look up the multicast
2458 * group in the normal IPv6 routing table and using the resulting
2461 multicastRequest.ipv6mr_interface = 0;
2463 /* Join the multicast group */
2464 if (GNUNET_NETWORK_socket_setsockopt
2465 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
2466 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
2468 LOG (GNUNET_ERROR_TYPE_WARNING,
2469 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
2474 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
2476 plugin->send_ipv6_broadcast_task =
2477 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
2478 plugin->broadcast_ipv6 = GNUNET_YES;
2483 if (sockets_created == 0)
2484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2486 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
2487 (const struct sockaddr **) addrs, addrlens,
2488 &udp_nat_port_map_callback, NULL, plugin);
2493 * Shutdown the plugin.
2495 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
2499 libgnunet_plugin_transport_udp_done (void *cls)
2501 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2502 struct Plugin *plugin = api->cls;
2503 struct ReceiveContext *rc;
2505 /* FIXME: clean up heap and hashmap */
2506 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
2508 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2509 plugin->sessions = NULL;
2510 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
2511 &destroy_inbound_session, NULL);
2512 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
2513 plugin->inbound_sessions = NULL;
2514 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
2516 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2519 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
2521 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2523 GNUNET_SCHEDULER_cancel (plugin->select_task);
2524 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2527 if (plugin->broadcast_ipv4)
2529 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2531 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
2532 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2535 if (plugin->broadcast_ipv4_mst != NULL)
2536 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
2538 while (plugin->ipv4_broadcast_head != NULL)
2540 struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
2542 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
2543 plugin->ipv4_broadcast_tail, p);
2544 GNUNET_free (p->addr);
2549 if (plugin->broadcast_ipv6)
2551 /* Create IPv6 multicast request */
2552 struct ipv6_mreq multicastRequest;
2554 multicastRequest.ipv6mr_multiaddr =
2555 plugin->ipv6_multicast_address.sin6_addr;
2556 multicastRequest.ipv6mr_interface = 0;
2558 /* Join the multicast address */
2559 if (GNUNET_NETWORK_socket_setsockopt
2560 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
2561 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
2563 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
2568 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
2572 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2574 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
2575 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2577 if (plugin->broadcast_ipv6_mst != NULL)
2578 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
2582 if (plugin->sockv4 != NULL)
2584 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2585 plugin->sockv4 = NULL;
2587 if (plugin->sockv6 != NULL)
2589 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2590 plugin->sockv6 = NULL;
2593 GNUNET_SERVER_mst_destroy (plugin->mst);
2594 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2596 GNUNET_NAT_unregister (plugin->nat);
2598 GNUNET_free (plugin);
2603 /* end of plugin_transport_udp.c */