2 This file is part of GNUnet
3 (C) 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_udp.c
23 * @brief Implementation of the UDP NAT punching
25 * @author Christian Grothoff
26 * @author Nathan Evans
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fragmentation_lib.h"
32 #include "gnunet_nat_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_constants.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
45 #define DEBUG_UDP GNUNET_EXTRA_LOGGING
48 * MTU for fragmentation subsystem. Should be conservative since
49 * all communicating peers MUST work with this MTU.
54 * Number of messages we can defragment in parallel. We only really
55 * defragment 1 message at a time, but if messages get re-ordered, we
56 * may want to keep knowledge about the previous message to avoid
57 * discarding the current message in favor of a single fragment of a
58 * previous message. 3 should be good since we don't expect massive
59 * message reorderings with UDP.
61 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
64 * We keep a defragmentation queue per sender address. How many
65 * sender addresses do we support at the same time? Memory consumption
66 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
67 * value. (So 128 corresponds to 12 MB and should suffice for
68 * connecting to roughly 128 peers via UDP).
70 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
74 * UDP Message-Packet header (after defragmentation).
81 struct GNUNET_MessageHeader header;
84 * Always zero for now.
89 * What is the identity of the sender
91 struct GNUNET_PeerIdentity sender;
97 * UDP ACK Message-Packet header (after defragmentation).
99 struct UDP_ACK_Message
104 struct GNUNET_MessageHeader header;
107 * Desired delay for flow control
112 * What is the identity of the sender
114 struct GNUNET_PeerIdentity sender;
118 struct UDP_Beacon_Message
123 struct GNUNET_MessageHeader header;
126 * What is the identity of the sender
128 struct GNUNET_PeerIdentity sender;
133 * Network format for IPv4 addresses.
135 struct IPv4UdpAddress
138 * IPv4 address, in network byte order.
140 uint32_t ipv4_addr GNUNET_PACKED;
143 * Port number, in network byte order.
145 uint16_t u4_port GNUNET_PACKED;
150 * Network format for IPv6 addresses.
152 struct IPv6UdpAddress
158 struct in6_addr ipv6_addr GNUNET_PACKED;
161 * Port number, in network byte order.
163 uint16_t u6_port GNUNET_PACKED;
167 /* Forward definition */
172 * Session with another peer. FIXME: why not make this into
173 * a regular 'struct Session' and pass it around!?
179 * Which peer is this session for?
181 struct GNUNET_PeerIdentity target;
184 * Pointer to the global plugin struct.
186 struct Plugin *plugin;
189 * Address of the other peer
191 const struct sockaddr *sock_addr;
196 * Function to call upon completion of the transmission.
198 GNUNET_TRANSPORT_TransmitContinuation cont;
201 * Closure for 'cont'.
206 * Current outgoing message to this peer.
208 struct GNUNET_FRAGMENT_Context *frag;
210 struct GNUNET_TIME_Absolute valid_until;
212 GNUNET_SCHEDULER_TaskIdentifier invalidation_task;
214 GNUNET_SCHEDULER_TaskIdentifier delayed_cont_task;
217 * Desired delay for next sending we send to other peer
219 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
222 * Desired delay for next sending we received from other peer
224 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
229 * Data structure to track defragmentation contexts based
230 * on the source of the UDP traffic.
232 struct ReceiveContext
236 * Defragmentation context.
238 struct GNUNET_DEFRAGMENT_Context *defrag;
241 * Source address this receive context is for (allocated at the
242 * end of the struct).
244 const struct sockaddr *src_addr;
247 * Reference to master plugin struct.
249 struct Plugin *plugin;
252 * Node in the defrag heap.
254 struct GNUNET_CONTAINER_HeapNode *hnode;
257 * Length of 'src_addr'
261 struct GNUNET_PeerIdentity id;
265 struct BroadcastAddress
267 struct BroadcastAddress *next;
268 struct BroadcastAddress *prev;
276 * Encapsulation of all of the state of the plugin.
284 struct GNUNET_TRANSPORT_PluginEnvironment *env;
287 * Session of peers with whom we are currently connected,
288 * map of peer identity to 'struct PeerSession'.
290 struct GNUNET_CONTAINER_MultiHashMap *sessions;
293 * Session of peers with whom we are currently connected,
294 * map of peer identity to 'struct PeerSession'.
296 struct GNUNET_CONTAINER_MultiHashMap *inbound_sessions;
299 * Heap with all of our defragmentation activities.
301 struct GNUNET_CONTAINER_Heap *defrags;
306 GNUNET_SCHEDULER_TaskIdentifier select_task;
309 * Tokenizer for inbound messages.
311 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
314 * Bandwidth tracker to limit global UDP traffic.
316 struct GNUNET_BANDWIDTH_Tracker tracker;
319 * Address we were told to bind to exclusively (IPv4).
324 * Address we were told to bind to exclusively (IPv6).
329 * Handle to NAT traversal support.
331 struct GNUNET_NAT_Handle *nat;
336 struct GNUNET_NETWORK_FDSet *rs;
339 * The read socket for IPv4
341 struct GNUNET_NETWORK_Handle *sockv4;
344 * The read socket for IPv6
346 struct GNUNET_NETWORK_Handle *sockv6;
354 * Tokenizer for inbound messages.
356 struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_mst;
359 * The read socket for IPv4
361 struct GNUNET_NETWORK_Handle *sockv4_broadcast;
363 struct BroadcastAddress *head;
364 struct BroadcastAddress *tail;
367 * ID of select broadcast task
369 GNUNET_SCHEDULER_TaskIdentifier select_broadcast_task;
372 * ID of select broadcast task
374 GNUNET_SCHEDULER_TaskIdentifier send_broadcast_task;
379 struct GNUNET_NETWORK_FDSet *broadcast_rs;
384 struct GNUNET_TIME_Relative broadcast_interval;
387 * expected delay for ACKs
389 struct GNUNET_TIME_Relative last_expected_delay;
392 * Port we broadcasting on.
394 uint16_t broadcast_port;
402 * Port we advertise on.
408 struct PeerSessionIteratorContext
410 struct Session *result;
417 * Lookup the session for the given peer.
419 * @param plugin the plugin
420 * @param peer peer's identity
421 * @return NULL if we have no session
423 static struct Session *
424 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
426 return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
432 inbound_session_iterator (void *cls, const GNUNET_HashCode * key, void *value)
434 struct PeerSessionIteratorContext *psc = cls;
435 struct Session *s = value;
437 if (s->addrlen == psc->addrlen)
439 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
442 if (psc->result != NULL)
449 * Lookup the session for the given peer.
451 * @param plugin the plugin
452 * @param peer peer's identity
453 * @return NULL if we have no session
455 static struct Session *
456 find_inbound_session (struct Plugin *plugin,
457 const struct GNUNET_PeerIdentity *peer, const void *addr,
460 struct PeerSessionIteratorContext psc;
463 psc.addrlen = addrlen;
466 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
468 &inbound_session_iterator, &psc);
474 inbound_session_by_addr_iterator (void *cls, const GNUNET_HashCode * key,
477 struct PeerSessionIteratorContext *psc = cls;
478 struct Session *s = value;
480 if (s->addrlen == psc->addrlen)
482 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
485 if (psc->result != NULL)
492 * Lookup the session for the given peer just by address.
494 * @param plugin the plugin
495 * @param addr address
496 * @param addrlen address length
497 * @return NULL if we have no session
499 static struct Session *
500 find_inbound_session_by_addr (struct Plugin *plugin, const void *addr,
503 struct PeerSessionIteratorContext psc;
506 psc.addrlen = addrlen;
509 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
510 &inbound_session_by_addr_iterator,
517 * Destroy a session, plugin is being unloaded.
520 * @param key hash of public key of target peer
521 * @param value a 'struct PeerSession*' to clean up
522 * @return GNUNET_OK (continue to iterate)
525 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
527 struct Session *peer_session = value;
529 GNUNET_assert (GNUNET_YES ==
530 GNUNET_CONTAINER_multihashmap_remove (peer_session->plugin->sessions,
531 &peer_session->target.hashPubKey, peer_session));
532 if (peer_session->frag != NULL)
533 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
534 if (GNUNET_SCHEDULER_NO_TASK != peer_session->delayed_cont_task)
535 GNUNET_SCHEDULER_cancel (peer_session->delayed_cont_task);
536 GNUNET_free (peer_session);
542 * Destroy a session, plugin is being unloaded.
545 * @param key hash of public key of target peer
546 * @param value a 'struct PeerSession*' to clean up
547 * @return GNUNET_OK (continue to iterate)
550 destroy_inbound_session (void *cls, const GNUNET_HashCode * key, void *value)
552 struct Session *s = value;
554 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
555 GNUNET_SCHEDULER_cancel (s->invalidation_task);
556 if (GNUNET_SCHEDULER_NO_TASK != s->delayed_cont_task)
557 GNUNET_SCHEDULER_cancel (s->delayed_cont_task);
558 GNUNET_CONTAINER_multihashmap_remove (s->plugin->inbound_sessions, &s->target.hashPubKey, s);
565 * Disconnect from a remote node. Clean up session if we have one for this peer
567 * @param cls closure for this call (should be handle to Plugin)
568 * @param target the peeridentity of the peer to disconnect
569 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
572 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
574 struct Plugin *plugin = cls;
575 struct Session *session;
577 session = find_session (plugin, target);
580 GNUNET_assert (GNUNET_OK ==
581 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
585 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
587 &destroy_inbound_session, NULL);
588 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
589 if (GNUNET_SCHEDULER_NO_TASK != session->delayed_cont_task)
590 GNUNET_SCHEDULER_cancel (session->delayed_cont_task);
591 if (session->cont != NULL)
592 session->cont (session->cont_cls, target, GNUNET_SYSERR);
593 GNUNET_free (session);
598 * Actually send out the message.
600 * @param plugin the plugin
601 * @param sa the address to send the message to
602 * @param msg message to transmit
603 * @return the number of bytes written
606 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
607 const struct GNUNET_MessageHeader *msg)
612 switch (sa->sa_family)
615 if (NULL == plugin->sockv4)
618 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
619 sa, slen = sizeof (struct sockaddr_in));
622 if (NULL == plugin->sockv6)
625 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
626 sa, slen = sizeof (struct sockaddr_in6));
632 if (GNUNET_SYSERR == sent)
634 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
635 LOG (GNUNET_ERROR_TYPE_ERROR,
636 "UDP transmited %u-byte message to %s (%d: %s)\n",
637 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
638 (sent < 0) ? STRERROR (errno) : "ok");
641 LOG (GNUNET_ERROR_TYPE_DEBUG,
642 "UDP transmited %u-byte message to %s (%d: %s)\n",
643 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
644 (sent < 0) ? STRERROR (errno) : "ok");
650 * Function that is called with messages created by the fragmentation
651 * module. In the case of the 'proc' callback of the
652 * GNUNET_FRAGMENT_context_create function, this function must
653 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
655 * @param cls closure, the 'struct PeerSession'
656 * @param msg the message that was created
659 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
661 struct Session *session = cls;
663 udp_send (session->plugin, session->sock_addr, msg);
664 GNUNET_FRAGMENT_context_transmission_done (session->frag);
668 static struct Session *
669 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
670 const void *addr, size_t addrlen,
671 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
673 struct Session *peer_session;
674 const struct IPv4UdpAddress *t4;
675 const struct IPv6UdpAddress *t6;
676 struct sockaddr_in *v4;
677 struct sockaddr_in6 *v6;
682 case sizeof (struct IPv4UdpAddress):
683 if (NULL == plugin->sockv4)
689 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
690 len = sizeof (struct sockaddr_in);
691 v4 = (struct sockaddr_in *) &peer_session[1];
692 v4->sin_family = AF_INET;
693 #if HAVE_SOCKADDR_IN_SIN_LEN
694 v4->sin_len = sizeof (struct sockaddr_in);
696 v4->sin_port = t4->u4_port;
697 v4->sin_addr.s_addr = t4->ipv4_addr;
699 case sizeof (struct IPv6UdpAddress):
700 if (NULL == plugin->sockv6)
706 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
707 len = sizeof (struct sockaddr_in6);
708 v6 = (struct sockaddr_in6 *) &peer_session[1];
709 v6->sin6_family = AF_INET6;
710 #if HAVE_SOCKADDR_IN_SIN_LEN
711 v6->sin6_len = sizeof (struct sockaddr_in6);
713 v6->sin6_port = t6->u6_port;
714 v6->sin6_addr = t6->ipv6_addr;
717 /* Must have a valid address to send to */
722 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
723 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
724 peer_session->addrlen = len;
725 peer_session->target = *target;
726 peer_session->plugin = plugin;
727 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
728 peer_session->cont = cont;
729 peer_session->cont_cls = cont_cls;
735 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
739 udp_call_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
741 struct Session *s = cls;
742 GNUNET_TRANSPORT_TransmitContinuation cont = s->cont;
744 s->delayed_cont_task = GNUNET_SCHEDULER_NO_TASK;
746 cont (s->cont_cls, &s->target, GNUNET_OK);
751 * Function that can be used by the transport service to transmit
752 * a message using the plugin.
755 * @param target who should receive this message (ignored by UDP)
756 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
757 * @param msgbuf_size the size of the msgbuf to send
758 * @param priority how important is the message (ignored by UDP)
759 * @param timeout when should we time out (give up) if we can not transmit?
760 * @param session identifier used for this session (NULL for UDP)
761 * @param addr the addr to send the message to
762 * @param addrlen the len of addr
763 * @param force_address not used, we had better have an address to send to
764 * because we are stateless!!
765 * @param cont continuation to call once the message has
766 * been transmitted (or if the transport is ready
767 * for the next transmission call; or if the
768 * peer disconnected...)
769 * @param cont_cls closure for cont
771 * @return the number of bytes written (may return 0 and the message can
772 * still be transmitted later!)
775 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
776 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
777 struct GNUNET_TIME_Relative timeout, struct Session *session,
778 const void *addr, size_t addrlen, int force_address,
779 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
781 struct Plugin *plugin = cls;
782 struct Session *peer_session;
784 const struct IPv4UdpAddress *t4;
785 const struct IPv6UdpAddress *t6;
786 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
788 struct UDPMessage *udp;
789 struct GNUNET_TIME_Relative delta;
791 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
794 return GNUNET_SYSERR;
797 LOG (GNUNET_ERROR_TYPE_DEBUG,
798 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
799 msgbuf_size, GNUNET_i2s (target), udp_address_to_string (NULL, addr,
801 session, force_address);
803 if ((force_address == GNUNET_SYSERR) && (session == NULL))
804 return GNUNET_SYSERR;
807 /* safety check: comparing address to address stored in session */
808 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
811 GNUNET_assert (GNUNET_YES ==
812 GNUNET_CONTAINER_multihashmap_contains_value
813 (plugin->inbound_sessions, &target->hashPubKey, s));
815 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
816 return GNUNET_SYSERR;
819 case sizeof (struct IPv4UdpAddress):
820 if (NULL == plugin->sockv4)
823 cont (cont_cls, target, GNUNET_SYSERR);
824 return GNUNET_SYSERR;
827 if (s->addrlen != (sizeof (struct sockaddr_in)))
828 return GNUNET_SYSERR;
829 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
831 GNUNET_assert (a4->sin_port == t4->u4_port);
833 memcmp (&a4->sin_addr, &t4->ipv4_addr,
834 sizeof (struct in_addr)));
835 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
838 case sizeof (struct IPv6UdpAddress):
839 if (NULL == plugin->sockv6)
842 cont (cont_cls, target, GNUNET_SYSERR);
843 return GNUNET_SYSERR;
846 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
847 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
849 GNUNET_assert (a6->sin6_port == t6->u6_port);
851 memcmp (&a6->sin6_addr, &t6->ipv6_addr,
852 sizeof (struct in6_addr)));
853 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
857 /* Must have a valid address to send to */
862 if ((addr == NULL) || (addrlen == 0))
863 return GNUNET_SYSERR;
864 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
865 if (peer_session == NULL)
868 cont (cont_cls, target, GNUNET_SYSERR);
869 return GNUNET_SYSERR;;
873 udp = (struct UDPMessage *) mbuf;
874 udp->header.size = htons (mlen);
875 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
876 udp->reserved = htonl (0);
877 udp->sender = *plugin->env->my_identity;
878 memcpy (&udp[1], msgbuf, msgbuf_size);
881 delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
883 delta = GNUNET_TIME_UNIT_ZERO;
886 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
889 if ((delta.rel_value > 0) && (mlen > 0))
892 s->cont_cls = cont_cls;
893 s->delayed_cont_task =
894 GNUNET_SCHEDULER_add_delayed (delta, &udp_call_continuation, s);
897 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
899 GNUNET_free_non_null (peer_session);
903 GNUNET_assert (GNUNET_OK ==
904 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
907 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
909 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
911 plugin->last_expected_delay,
912 &udp->header, &send_fragment,
920 * Closure for 'process_inbound_tokenized_messages'
922 struct SourceInformation
927 struct GNUNET_PeerIdentity sender;
935 * Number of bytes in source address.
939 struct Session *session;
944 * Message tokenizer has broken up an incomming message. Pass it on
947 * @param cls the 'struct Plugin'
948 * @param client the 'struct SourceInformation'
949 * @param hdr the actual message
952 process_inbound_tokenized_messages (void *cls, void *client,
953 const struct GNUNET_MessageHeader *hdr)
955 struct Plugin *plugin = cls;
956 struct SourceInformation *si = client;
957 struct GNUNET_ATS_Information distance;
958 struct GNUNET_TIME_Relative delay;
961 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
962 distance.value = htonl (1);
964 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving Session %X %s to transport\n",
965 si->session, GNUNET_i2s (&si->session->target));
967 plugin->env->receive (plugin->env->cls, &si->sender, hdr, &distance, 1,
968 si->session, si->arg, si->args);
969 si->session->flow_delay_for_other_peer = delay;
973 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
975 struct Session *s = cls;
977 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
978 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %X (`%s') is now invalid\n", s,
979 GNUNET_a2s (s->sock_addr, s->addrlen));
981 s->plugin->env->session_end (s->plugin->env->cls, &s->target, s);
982 GNUNET_assert (GNUNET_YES ==
983 GNUNET_CONTAINER_multihashmap_remove (s->
984 plugin->inbound_sessions,
985 &s->target.hashPubKey,
992 * We've received a UDP Message. Process it (pass contents to main service).
994 * @param plugin plugin context
995 * @param msg the message
996 * @param sender_addr sender address
997 * @param sender_addr_len number of bytes in sender_addr
1000 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1001 const struct sockaddr *sender_addr,
1002 socklen_t sender_addr_len)
1004 struct SourceInformation si;
1005 struct IPv4UdpAddress u4;
1006 struct IPv6UdpAddress u6;
1010 if (0 != ntohl (msg->reserved))
1012 GNUNET_break_op (0);
1015 if (ntohs (msg->header.size) <
1016 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1018 GNUNET_break_op (0);
1022 /* convert address */
1023 switch (sender_addr->sa_family)
1026 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1027 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1028 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1033 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1034 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1035 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 "Received message with %u bytes from peer `%s' at `%s'\n",
1046 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1047 GNUNET_a2s (sender_addr, sender_addr_len));
1050 /* create a session for inbound connections */
1051 const struct UDPMessage *udp_msg = (const struct UDPMessage *) msg;
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
1055 GNUNET_i2s (&udp_msg->sender), udp_address_to_string (NULL, arg, args));
1057 struct Session *s = NULL;
1059 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr,
1064 LOG (GNUNET_ERROR_TYPE_DEBUG,
1065 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
1066 s, GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1070 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
1071 LOG (GNUNET_ERROR_TYPE_DEBUG,
1072 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n", s,
1073 GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1075 GNUNET_assert (GNUNET_OK ==
1076 GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
1077 &s->target.hashPubKey, s,
1078 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1081 GNUNET_TIME_relative_to_absolute
1082 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1083 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
1085 GNUNET_SCHEDULER_cancel (s->invalidation_task);
1086 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1087 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rescheduling %X' `%s'\n", s,
1088 udp_address_to_string (NULL, arg, args));
1090 s->invalidation_task =
1091 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1092 &invalidation_task, s);
1093 /* iterate over all embedded messages */
1094 si.sender = msg->sender;
1098 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1099 ntohs (msg->header.size) -
1100 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1105 * Process a defragmented message.
1107 * @param cls the 'struct ReceiveContext'
1108 * @param msg the message
1111 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1113 struct ReceiveContext *rc = cls;
1115 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1120 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1125 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1126 rc->src_addr, rc->addr_len);
1131 * Transmit an acknowledgement.
1133 * @param cls the 'struct ReceiveContext'
1134 * @param id message ID (unused)
1135 * @param msg ack to transmit
1138 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1140 struct ReceiveContext *rc = cls;
1142 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1144 struct UDP_ACK_Message *udp_ack;
1149 s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
1152 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1153 delay = s->flow_delay_for_other_peer.rel_value;
1160 LOG (GNUNET_ERROR_TYPE_DEBUG,
1161 "Sending ACK to `%s' including delay of %u ms\n",
1162 GNUNET_a2s (rc->src_addr,
1163 (rc->src_addr->sa_family ==
1164 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1168 udp_ack = (struct UDP_ACK_Message *) buf;
1169 udp_ack->header.size = htons ((uint16_t) msize);
1170 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1171 udp_ack->delay = htonl (delay);
1172 udp_ack->sender = *rc->plugin->env->my_identity;
1173 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1174 (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
1179 * Closure for 'find_receive_context'.
1181 struct FindReceiveContext
1184 * Where to store the result.
1186 struct ReceiveContext *rc;
1191 const struct sockaddr *addr;
1194 * Number of bytes in 'addr'.
1198 struct Session *session;
1203 * Scan the heap for a receive context with the given address.
1205 * @param cls the 'struct FindReceiveContext'
1206 * @param node internal node of the heap
1207 * @param element value stored at the node (a 'struct ReceiveContext')
1208 * @param cost cost associated with the node
1209 * @return GNUNET_YES if we should continue to iterate,
1213 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1214 void *element, GNUNET_CONTAINER_HeapCostType cost)
1216 struct FindReceiveContext *frc = cls;
1217 struct ReceiveContext *e = element;
1219 if ((frc->addr_len == e->addr_len) &&
1220 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1230 * Read and process a message from the given socket.
1232 * @param plugin the overall plugin
1233 * @param rsock socket to read from
1236 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1242 const struct GNUNET_MessageHeader *msg;
1243 const struct GNUNET_MessageHeader *ack;
1244 struct Session *peer_session;
1245 const struct UDP_ACK_Message *udp_ack;
1246 struct ReceiveContext *rc;
1247 struct GNUNET_TIME_Absolute now;
1248 struct FindReceiveContext frc;
1249 struct Session *s = NULL;
1250 struct GNUNET_TIME_Relative flow_delay;
1252 fromlen = sizeof (addr);
1253 memset (&addr, 0, sizeof (addr));
1255 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1256 (struct sockaddr *) &addr, &fromlen);
1257 if (ret < sizeof (struct GNUNET_MessageHeader))
1259 GNUNET_break_op (0);
1262 msg = (const struct GNUNET_MessageHeader *) buf;
1264 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1266 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1268 if (ret != ntohs (msg->size))
1270 GNUNET_break_op (0);
1273 switch (ntohs (msg->type))
1275 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1276 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1278 GNUNET_break_op (0);
1281 process_udp_message (plugin, (const struct UDPMessage *) msg,
1282 (const struct sockaddr *) addr, fromlen);
1284 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1286 if (ntohs (msg->size) <
1287 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1289 GNUNET_break_op (0);
1292 udp_ack = (const struct UDP_ACK_Message *) msg;
1293 s = find_inbound_session (plugin, &udp_ack->sender, addr, fromlen);
1296 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1299 flow_delay.rel_value);
1301 s->flow_delay_from_other_peer =
1302 GNUNET_TIME_relative_to_absolute (flow_delay);
1304 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1305 if (ntohs (ack->size) !=
1306 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1308 GNUNET_break_op (0);
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1314 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1315 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1318 peer_session = find_session (plugin, &udp_ack->sender);
1319 if (NULL == peer_session)
1322 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 "Session for ACK not found, dropping ACK!\n");
1327 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1329 GNUNET_assert (GNUNET_OK ==
1330 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1334 plugin->last_expected_delay =
1335 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1336 if (peer_session->cont != NULL)
1337 peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
1338 GNUNET_free (peer_session);
1340 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1342 frc.addr = (const struct sockaddr *) addr;
1343 frc.addr_len = fromlen;
1344 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1346 now = GNUNET_TIME_absolute_get ();
1350 /* need to create a new RC */
1351 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1352 memcpy (&rc[1], addr, fromlen);
1353 rc->src_addr = (const struct sockaddr *) &rc[1];
1354 rc->addr_len = fromlen;
1355 rc->plugin = plugin;
1357 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1358 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1359 &fragment_msg_proc, &ack_proc);
1361 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1362 (GNUNET_CONTAINER_HeapCostType)
1366 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1367 (unsigned int) ntohs (msg->size),
1368 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1371 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1373 /* keep this 'rc' from expiring */
1374 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1375 (GNUNET_CONTAINER_HeapCostType)
1378 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1379 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1381 /* remove 'rc' that was inactive the longest */
1382 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1383 GNUNET_assert (NULL != rc);
1384 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1389 GNUNET_break_op (0);
1396 * We have been notified that our writeset has something to read. We don't
1397 * know which socket needs to be read, so we have to check each one
1398 * Then reschedule this function to be called again once more is available.
1400 * @param cls the plugin handle
1401 * @param tc the scheduling context (for rescheduling this function again)
1404 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1406 struct Plugin *plugin = cls;
1408 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1409 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1411 if ((NULL != plugin->sockv4) &&
1412 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1413 udp_read (plugin, plugin->sockv4);
1414 if ((NULL != plugin->sockv6) &&
1415 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1416 udp_read (plugin, plugin->sockv6);
1417 plugin->select_task =
1418 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1419 GNUNET_SCHEDULER_NO_TASK,
1420 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1421 NULL, &udp_plugin_select, plugin);
1428 struct Plugin *plugin;
1430 struct IPv4UdpAddress addr;
1434 udp_broadcast_mst_cb (void *cls, void *client,
1435 const struct GNUNET_MessageHeader *message)
1437 struct Plugin *plugin = cls;
1438 struct MstContext *mc = client;
1439 const struct GNUNET_MessageHeader *hello;
1440 struct UDP_Beacon_Message *msg;
1442 msg = (struct UDP_Beacon_Message *) message;
1444 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1445 ntohs (msg->header.type))
1448 LOG (GNUNET_ERROR_TYPE_ERROR,
1449 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1450 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1451 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1453 struct GNUNET_ATS_Information ats;
1455 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1456 ats.value = htonl (1);
1458 hello = (struct GNUNET_MessageHeader *) &msg[1];
1459 plugin->env->receive (plugin->env->cls, &msg->sender, hello, &ats, 1, NULL,
1460 (const char *) &mc->addr, sizeof (mc->addr));
1462 GNUNET_STATISTICS_update (plugin->env->stats,
1463 _("# HELLO beacons received via udp"), 1,
1469 * Read and process a message from the given socket.
1471 * @param plugin the overall plugin
1472 * @param rsock socket to read from
1475 udp_broadcast_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1481 struct MstContext *mc;
1484 fromlen = sizeof (addr);
1485 memset (&addr, 0, sizeof (addr));
1487 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1488 (struct sockaddr *) &addr, &fromlen);
1489 if (ret < sizeof (struct GNUNET_MessageHeader))
1491 /* malformed beacon, just throw it away */
1495 mc = GNUNET_malloc (sizeof (struct MstContext));
1497 struct sockaddr_in *av4 = (struct sockaddr_in *) &addr;
1499 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
1500 mc->addr.u4_port = av4->sin_port;
1503 GNUNET_SERVER_mst_receive (plugin->broadcast_mst, mc, buf, ret, GNUNET_NO,
1510 udp_broadcast_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1512 struct Plugin *plugin = cls;
1515 uint16_t hello_size;
1519 const struct GNUNET_MessageHeader *hello;
1520 struct UDP_Beacon_Message *msg;
1521 struct BroadcastAddress * baddr;
1523 plugin->send_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1526 hello = plugin->env->get_our_hello ();
1527 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1528 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1530 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1531 (msg_size > (UDP_MTU)))
1534 msg = (struct UDP_Beacon_Message *) buf;
1535 msg->sender = *(plugin->env->my_identity);
1536 msg->header.size = ntohs (msg_size);
1537 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1538 memcpy (&msg[1], hello, hello_size);
1541 baddr = plugin->head;
1543 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
1545 struct sockaddr_in * addr = (struct sockaddr_in * ) baddr->addr;
1546 addr->sin_port = htons (plugin->broadcast_port);
1548 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4_broadcast, msg, msg_size,
1549 (const struct sockaddr *) addr,
1551 if (sent == GNUNET_SYSERR)
1552 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "sendto");
1554 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sent HELLO beacon broadcast with %i bytes to address %s\n",
1555 sent, GNUNET_a2s(baddr->addr, baddr->addrlen));
1556 baddr = baddr->next;
1559 plugin->send_broadcast_task =
1560 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1561 &udp_broadcast_send, plugin);
1566 * We have been notified that our writeset has something to read. We don't
1567 * know which socket needs to be read, so we have to check each one
1568 * Then reschedule this function to be called again once more is available.
1570 * @param cls the plugin handle
1571 * @param tc the scheduling context (for rescheduling this function again)
1574 udp_plugin_broadcast_select (void *cls,
1575 const struct GNUNET_SCHEDULER_TaskContext *tc)
1577 struct Plugin *plugin = cls;
1579 plugin->select_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1580 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1583 if ((NULL != plugin->sockv4_broadcast) &&
1584 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4_broadcast)))
1585 udp_broadcast_read (plugin, plugin->sockv4_broadcast);
1587 plugin->select_broadcast_task =
1588 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1589 GNUNET_SCHEDULER_NO_TASK,
1590 GNUNET_TIME_UNIT_FOREVER_REL,
1591 plugin->broadcast_rs, NULL,
1592 &udp_plugin_broadcast_select, plugin);
1598 * Check if the given port is plausible (must be either our listen
1599 * port or our advertised port). If it is neither, we return
1602 * @param plugin global variables
1603 * @param in_port port number to check
1604 * @return GNUNET_OK if port is either open_port or adv_port
1607 check_port (struct Plugin *plugin, uint16_t in_port)
1609 if ((in_port == plugin->port) || (in_port == plugin->aport))
1611 return GNUNET_SYSERR;
1616 * Function that will be called to check if a binary address for this
1617 * plugin is well-formed and corresponds to an address for THIS peer
1618 * (as per our configuration). Naturally, if absolutely necessary,
1619 * plugins can be a bit conservative in their answer, but in general
1620 * plugins should make sure that the address does not redirect
1621 * traffic to a 3rd party that might try to man-in-the-middle our
1624 * @param cls closure, should be our handle to the Plugin
1625 * @param addr pointer to the address
1626 * @param addrlen length of addr
1627 * @return GNUNET_OK if this is a plausible address for this peer
1628 * and transport, GNUNET_SYSERR if not
1632 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1634 struct Plugin *plugin = cls;
1635 struct IPv4UdpAddress *v4;
1636 struct IPv6UdpAddress *v6;
1638 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1639 (addrlen != sizeof (struct IPv6UdpAddress)))
1641 GNUNET_break_op (0);
1642 return GNUNET_SYSERR;
1644 if (addrlen == sizeof (struct IPv4UdpAddress))
1646 v4 = (struct IPv4UdpAddress *) addr;
1647 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1648 return GNUNET_SYSERR;
1650 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1651 sizeof (struct in_addr)))
1652 return GNUNET_SYSERR;
1656 v6 = (struct IPv6UdpAddress *) addr;
1657 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1659 GNUNET_break_op (0);
1660 return GNUNET_SYSERR;
1662 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1663 return GNUNET_SYSERR;
1665 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1666 sizeof (struct in6_addr)))
1667 return GNUNET_SYSERR;
1674 * Function called for a quick conversion of the binary address to
1675 * a numeric address. Note that the caller must not free the
1676 * address and that the next call to this function is allowed
1677 * to override the address again.
1679 * @param cls closure
1680 * @param addr binary address
1681 * @param addrlen length of the address
1682 * @return string representing the same address
1685 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1687 static char rbuf[INET6_ADDRSTRLEN + 10];
1688 char buf[INET6_ADDRSTRLEN];
1692 const struct IPv4UdpAddress *t4;
1693 const struct IPv6UdpAddress *t6;
1697 if (addrlen == sizeof (struct IPv6UdpAddress))
1701 port = ntohs (t6->u6_port);
1702 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1705 else if (addrlen == sizeof (struct IPv4UdpAddress))
1709 port = ntohs (t4->u4_port);
1710 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1715 GNUNET_break_op (0);
1718 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1719 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1726 * Closure for 'append_port'.
1728 struct PrettyPrinterContext
1731 * Function to call with the result.
1733 GNUNET_TRANSPORT_AddressStringCallback asc;
1736 * Clsoure for 'asc'.
1741 * Port to add after the IP address.
1748 * Append our port and forward the result.
1750 * @param cls a 'struct PrettyPrinterContext'
1751 * @param hostname result from DNS resolver
1754 append_port (void *cls, const char *hostname)
1756 struct PrettyPrinterContext *ppc = cls;
1759 if (hostname == NULL)
1761 ppc->asc (ppc->asc_cls, NULL);
1765 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1766 ppc->asc (ppc->asc_cls, ret);
1772 * Convert the transports address to a nice, human-readable
1775 * @param cls closure
1776 * @param type name of the transport that generated the address
1777 * @param addr one of the addresses of the host, NULL for the last address
1778 * the specific address format depends on the transport
1779 * @param addrlen length of the address
1780 * @param numeric should (IP) addresses be displayed in numeric form?
1781 * @param timeout after how long should we give up?
1782 * @param asc function to call on each string
1783 * @param asc_cls closure for asc
1786 udp_plugin_address_pretty_printer (void *cls, const char *type,
1787 const void *addr, size_t addrlen,
1789 struct GNUNET_TIME_Relative timeout,
1790 GNUNET_TRANSPORT_AddressStringCallback asc,
1793 struct PrettyPrinterContext *ppc;
1796 struct sockaddr_in a4;
1797 struct sockaddr_in6 a6;
1798 const struct IPv4UdpAddress *u4;
1799 const struct IPv6UdpAddress *u6;
1802 if (addrlen == sizeof (struct IPv6UdpAddress))
1805 memset (&a6, 0, sizeof (a6));
1806 a6.sin6_family = AF_INET6;
1807 #if HAVE_SOCKADDR_IN_SIN_LEN
1808 a6.sin6_len = sizeof (a6);
1810 a6.sin6_port = u6->u6_port;
1811 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1812 port = ntohs (u6->u6_port);
1816 else if (addrlen == sizeof (struct IPv4UdpAddress))
1819 memset (&a4, 0, sizeof (a4));
1820 a4.sin_family = AF_INET;
1821 #if HAVE_SOCKADDR_IN_SIN_LEN
1822 a4.sin_len = sizeof (a4);
1824 a4.sin_port = u4->u4_port;
1825 a4.sin_addr.s_addr = u4->ipv4_addr;
1826 port = ntohs (u4->u4_port);
1832 /* invalid address */
1833 GNUNET_break_op (0);
1834 asc (asc_cls, NULL);
1837 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1839 ppc->asc_cls = asc_cls;
1841 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1846 * Our external IP address/port mapping has changed.
1848 * @param cls closure, the 'struct LocalAddrList'
1849 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1850 * the previous (now invalid) one
1851 * @param addr either the previous or the new public IP address
1852 * @param addrlen actual lenght of the address
1855 udp_nat_port_map_callback (void *cls, int add_remove,
1856 const struct sockaddr *addr, socklen_t addrlen)
1858 struct Plugin *plugin = cls;
1859 struct IPv4UdpAddress u4;
1860 struct IPv6UdpAddress u6;
1864 /* convert 'addr' to our internal format */
1865 switch (addr->sa_family)
1868 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1869 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1870 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1875 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1876 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1877 sizeof (struct in6_addr));
1878 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1886 /* modify our published address list */
1887 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1892 iface_proc (void *cls, const char *name,
1894 const struct sockaddr * addr,
1895 const struct sockaddr * broadcast_addr,
1896 const struct sockaddr * netmask,
1899 struct Plugin *plugin = cls;
1901 if (broadcast_addr != NULL)
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding broadcast address %s for interface %s %p\n ",
1904 GNUNET_a2s(broadcast_addr,addrlen), name, broadcast_addr);
1906 struct BroadcastAddress * ba = GNUNET_malloc(sizeof (struct BroadcastAddress));
1907 ba->addr = GNUNET_malloc(addrlen);
1908 memcpy(ba->addr, broadcast_addr, addrlen);
1909 ba->addrlen = addrlen;
1911 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, ba);
1918 * The exported method. Makes the core api available via a global and
1919 * returns the udp transport API.
1921 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1922 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1925 libgnunet_plugin_transport_udp_init (void *cls)
1927 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1928 unsigned long long port;
1929 unsigned long long aport;
1930 unsigned long long bport;
1931 struct GNUNET_TRANSPORT_PluginFunctions *api;
1932 struct Plugin *plugin;
1933 int sockets_created;
1935 struct GNUNET_TIME_Relative interval;
1936 struct sockaddr_in serverAddrv4;
1937 struct sockaddr_in6 serverAddrv6;
1938 struct sockaddr *serverAddr;
1939 struct sockaddr *addrs[2];
1940 socklen_t addrlens[2];
1943 unsigned long long udp_max_bps;
1946 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1951 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
1953 if (broadcast == GNUNET_SYSERR)
1954 broadcast = GNUNET_NO;
1956 if (GNUNET_SYSERR ==
1957 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
1958 "BROADCAST_INTERVAL", &interval))
1959 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1962 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1963 "BROADCAST_PORT", &bport))
1967 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1968 "MAX_BPS", &udp_max_bps))
1969 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1971 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1972 "ADVERTISED_PORT", &aport))
1976 LOG (GNUNET_ERROR_TYPE_WARNING,
1977 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
1981 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1982 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1984 plugin = GNUNET_malloc (sizeof (struct Plugin));
1985 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1986 GNUNET_BANDWIDTH_value_init ((uint32_t)
1988 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1989 plugin->port = port;
1990 plugin->aport = aport;
1991 plugin->broadcast_port = bport;
1992 plugin->broadcast = broadcast;
1994 plugin->broadcast_interval = interval;
1995 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1998 api->send = &udp_plugin_send;
1999 api->disconnect = &udp_disconnect;
2000 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2001 api->address_to_string = &udp_address_to_string;
2002 api->check_address = &udp_plugin_check_address;
2005 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2006 "BINDTO", &plugin->bind4_address))
2008 LOG (GNUNET_ERROR_TYPE_DEBUG,
2009 "Binding udp plugin to specific address: `%s'\n",
2010 plugin->bind4_address);
2011 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
2013 GNUNET_free (plugin->bind4_address);
2014 GNUNET_free (plugin);
2021 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2022 "BINDTO6", &plugin->bind6_address))
2024 LOG (GNUNET_ERROR_TYPE_DEBUG,
2025 "Binding udp plugin to specific address: `%s'\n",
2026 plugin->bind6_address);
2028 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
2030 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2031 plugin->bind6_address);
2032 GNUNET_free_non_null (plugin->bind4_address);
2033 GNUNET_free (plugin->bind6_address);
2034 GNUNET_free (plugin);
2041 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2043 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2045 plugin->inbound_sessions =
2046 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2048 sockets_created = 0;
2050 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
2053 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2054 if (NULL == plugin->sockv6)
2056 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2060 #if HAVE_SOCKADDR_IN_SIN_LEN
2061 serverAddrv6.sin6_len = sizeof (serverAddrv6);
2063 serverAddrv6.sin6_family = AF_INET6;
2064 serverAddrv6.sin6_addr = in6addr_any;
2065 serverAddrv6.sin6_port = htons (plugin->port);
2066 addrlen = sizeof (serverAddrv6);
2067 serverAddr = (struct sockaddr *) &serverAddrv6;
2069 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2070 ntohs (serverAddrv6.sin6_port));
2073 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2076 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2078 LOG (GNUNET_ERROR_TYPE_DEBUG,
2079 "IPv6 Binding failed, trying new port %d\n",
2080 ntohs (serverAddrv6.sin6_port));
2085 GNUNET_NETWORK_socket_close (plugin->sockv6);
2086 plugin->sockv6 = NULL;
2090 if (plugin->sockv6 != NULL)
2092 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2093 addrlens[sockets_created] = sizeof (serverAddrv6);
2100 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2101 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2102 if (NULL == plugin->sockv4)
2104 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2108 #if HAVE_SOCKADDR_IN_SIN_LEN
2109 serverAddrv4.sin_len = sizeof (serverAddrv4);
2111 serverAddrv4.sin_family = AF_INET;
2112 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2113 serverAddrv4.sin_port = htons (plugin->port);
2114 addrlen = sizeof (serverAddrv4);
2115 serverAddr = (struct sockaddr *) &serverAddrv4;
2117 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2118 ntohs (serverAddrv4.sin_port));
2121 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2124 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2126 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2127 ntohs (serverAddrv4.sin_port));
2132 GNUNET_NETWORK_socket_close (plugin->sockv4);
2133 plugin->sockv4 = NULL;
2137 if (plugin->sockv4 != NULL)
2139 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2140 addrlens[sockets_created] = sizeof (serverAddrv4);
2145 plugin->rs = GNUNET_NETWORK_fdset_create ();
2146 GNUNET_NETWORK_fdset_zero (plugin->rs);
2147 if (NULL != plugin->sockv4)
2148 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
2149 if (NULL != plugin->sockv6)
2150 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
2152 plugin->select_task =
2153 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2154 GNUNET_SCHEDULER_NO_TASK,
2155 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
2156 NULL, &udp_plugin_select, plugin);
2159 /* create broadcast socket */
2162 plugin->sockv4_broadcast =
2163 GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2164 if (NULL == plugin->sockv4_broadcast)
2166 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2170 #if HAVE_SOCKADDR_IN_SIN_LEN
2171 serverAddrv4.sin_len = sizeof (serverAddrv4);
2173 serverAddrv4.sin_family = AF_INET;
2174 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2175 serverAddrv4.sin_port = htons (plugin->broadcast_port);
2176 addrlen = sizeof (serverAddrv4);
2177 serverAddr = (struct sockaddr *) &serverAddrv4;
2180 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding Broadcast to IPv4 port %d\n",
2181 ntohs (serverAddrv4.sin_port));
2183 if (GNUNET_NETWORK_socket_bind
2184 (plugin->sockv4_broadcast, serverAddr, addrlen) != GNUNET_OK)
2186 LOG (GNUNET_ERROR_TYPE_WARNING,
2187 _("Failed to create IPv4 broadcast socket on port %d\n"),
2188 ntohs (serverAddrv4.sin_port));
2189 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast);
2190 plugin->sockv4_broadcast = NULL;
2192 if (plugin->sockv4_broadcast != NULL)
2196 if (GNUNET_NETWORK_socket_setsockopt
2197 (plugin->sockv4_broadcast, SOL_SOCKET, SO_BROADCAST, &yes,
2198 sizeof (int)) != GNUNET_OK)
2200 LOG (GNUNET_ERROR_TYPE_WARNING,
2202 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
2203 ntohs (serverAddrv4.sin_port));
2204 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast);
2205 plugin->sockv4_broadcast = NULL;
2209 plugin->broadcast_rs = GNUNET_NETWORK_fdset_create ();
2210 GNUNET_NETWORK_fdset_set (plugin->broadcast_rs,
2211 plugin->sockv4_broadcast);
2216 if (plugin->sockv4_broadcast != NULL)
2218 plugin->broadcast = GNUNET_YES;
2219 plugin->broadcast_mst =
2220 GNUNET_SERVER_mst_create (udp_broadcast_mst_cb, plugin);
2221 GNUNET_STATISTICS_update (plugin->env->stats,
2222 _("# HELLO beacons received via udp"), 1,
2224 plugin->select_broadcast_task =
2225 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2226 GNUNET_SCHEDULER_NO_TASK,
2227 GNUNET_TIME_UNIT_FOREVER_REL,
2228 plugin->broadcast_rs, NULL,
2229 &udp_plugin_broadcast_select, plugin);
2231 GNUNET_OS_network_interfaces_list(iface_proc, plugin);
2232 plugin->send_broadcast_task =
2233 GNUNET_SCHEDULER_add_now (&udp_broadcast_send, plugin);
2237 plugin->broadcast = GNUNET_NO;
2240 if (sockets_created == 0)
2241 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2243 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
2244 (const struct sockaddr **) addrs, addrlens,
2245 &udp_nat_port_map_callback, NULL, plugin);
2250 * Shutdown the plugin.
2252 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
2256 libgnunet_plugin_transport_udp_done (void *cls)
2258 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2259 struct Plugin *plugin = api->cls;
2260 struct ReceiveContext *rc;
2262 /* FIXME: clean up heap and hashmap */
2263 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
2265 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2266 plugin->sessions = NULL;
2267 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
2268 &destroy_inbound_session, NULL);
2269 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
2270 plugin->inbound_sessions = NULL;
2271 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
2273 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2276 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
2278 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2280 GNUNET_SCHEDULER_cancel (plugin->select_task);
2281 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2283 if (plugin->sockv4 != NULL)
2285 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2286 plugin->sockv4 = NULL;
2288 if (plugin->sockv6 != NULL)
2290 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2291 plugin->sockv6 = NULL;
2294 if (plugin->broadcast)
2296 if (plugin->select_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2298 GNUNET_SCHEDULER_cancel (plugin->select_broadcast_task);
2299 plugin->select_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2301 if (plugin->send_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2303 GNUNET_SCHEDULER_cancel (plugin->send_broadcast_task);
2304 plugin->send_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2306 if (plugin->broadcast_mst != NULL)
2307 GNUNET_SERVER_mst_destroy (plugin->broadcast_mst);
2308 if (plugin->sockv4_broadcast != NULL)
2310 GNUNET_break (GNUNET_OK ==
2311 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast));
2312 plugin->sockv4_broadcast = NULL;
2314 GNUNET_NETWORK_fdset_destroy (plugin->broadcast_rs);
2316 while (plugin->head != NULL)
2318 struct BroadcastAddress * p= plugin->head;
2319 GNUNET_CONTAINER_DLL_remove(plugin->head, plugin->tail, p);
2320 GNUNET_free (p->addr);
2325 GNUNET_SERVER_mst_destroy (plugin->mst);
2326 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2328 GNUNET_NAT_unregister (plugin->nat);
2330 GNUNET_free (plugin);
2335 /* end of plugin_transport_udp.c */