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;
349 * Beacon broadcasting
350 * -------------------
356 struct GNUNET_TIME_Relative broadcast_interval;
359 * Broadcast with IPv4
364 * Broadcast with IPv6
370 * Tokenizer for inbound messages.
372 struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv6_mst;
373 struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_ipv4_mst;
376 * ID of select broadcast task
378 GNUNET_SCHEDULER_TaskIdentifier send_ipv4_broadcast_task;
381 * ID of select broadcast task
383 GNUNET_SCHEDULER_TaskIdentifier send_ipv6_broadcast_task;
386 * IPv6 multicast address
388 struct sockaddr_in6 ipv6_multicast_address;
391 * DLL of IPv4 broadcast addresses
393 struct BroadcastAddress *ipv4_broadcast_tail;
394 struct BroadcastAddress *ipv4_broadcast_head;
398 * expected delay for ACKs
400 struct GNUNET_TIME_Relative last_expected_delay;
403 * Port we broadcasting on.
405 uint16_t broadcast_port;
413 * Port we advertise on.
419 struct PeerSessionIteratorContext
421 struct Session *result;
428 * Lookup the session for the given peer.
430 * @param plugin the plugin
431 * @param peer peer's identity
432 * @return NULL if we have no session
434 static struct Session *
435 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
437 return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
443 inbound_session_iterator (void *cls, const GNUNET_HashCode * key, void *value)
445 struct PeerSessionIteratorContext *psc = cls;
446 struct Session *s = value;
448 if (s->addrlen == psc->addrlen)
450 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
453 if (psc->result != NULL)
460 * Lookup the session for the given peer.
462 * @param plugin the plugin
463 * @param peer peer's identity
464 * @return NULL if we have no session
466 static struct Session *
467 find_inbound_session (struct Plugin *plugin,
468 const struct GNUNET_PeerIdentity *peer, const void *addr,
471 struct PeerSessionIteratorContext psc;
474 psc.addrlen = addrlen;
477 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
479 &inbound_session_iterator, &psc);
485 inbound_session_by_addr_iterator (void *cls, const GNUNET_HashCode * key,
488 struct PeerSessionIteratorContext *psc = cls;
489 struct Session *s = value;
491 if (s->addrlen == psc->addrlen)
493 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
496 if (psc->result != NULL)
503 * Lookup the session for the given peer just by address.
505 * @param plugin the plugin
506 * @param addr address
507 * @param addrlen address length
508 * @return NULL if we have no session
510 static struct Session *
511 find_inbound_session_by_addr (struct Plugin *plugin, const void *addr,
514 struct PeerSessionIteratorContext psc;
517 psc.addrlen = addrlen;
520 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
521 &inbound_session_by_addr_iterator,
528 * Destroy a session, plugin is being unloaded.
531 * @param key hash of public key of target peer
532 * @param value a 'struct PeerSession*' to clean up
533 * @return GNUNET_OK (continue to iterate)
536 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
538 struct Session *peer_session = value;
540 GNUNET_assert (GNUNET_YES ==
541 GNUNET_CONTAINER_multihashmap_remove (peer_session->plugin->sessions,
542 &peer_session->target.hashPubKey, peer_session));
543 if (peer_session->frag != NULL)
544 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
545 if (GNUNET_SCHEDULER_NO_TASK != peer_session->delayed_cont_task)
546 GNUNET_SCHEDULER_cancel (peer_session->delayed_cont_task);
547 GNUNET_free (peer_session);
553 * Destroy a session, plugin is being unloaded.
556 * @param key hash of public key of target peer
557 * @param value a 'struct PeerSession*' to clean up
558 * @return GNUNET_OK (continue to iterate)
561 destroy_inbound_session (void *cls, const GNUNET_HashCode * key, void *value)
563 struct Session *s = value;
565 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
566 GNUNET_SCHEDULER_cancel (s->invalidation_task);
567 if (GNUNET_SCHEDULER_NO_TASK != s->delayed_cont_task)
568 GNUNET_SCHEDULER_cancel (s->delayed_cont_task);
569 GNUNET_CONTAINER_multihashmap_remove (s->plugin->inbound_sessions, &s->target.hashPubKey, s);
576 * Disconnect from a remote node. Clean up session if we have one for this peer
578 * @param cls closure for this call (should be handle to Plugin)
579 * @param target the peeridentity of the peer to disconnect
580 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
583 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
585 struct Plugin *plugin = cls;
586 struct Session *session;
588 session = find_session (plugin, target);
591 GNUNET_assert (GNUNET_OK ==
592 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
596 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
598 &destroy_inbound_session, NULL);
599 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
600 if (GNUNET_SCHEDULER_NO_TASK != session->delayed_cont_task)
601 GNUNET_SCHEDULER_cancel (session->delayed_cont_task);
602 if (session->cont != NULL)
603 session->cont (session->cont_cls, target, GNUNET_SYSERR);
604 GNUNET_free (session);
609 * Actually send out the message.
611 * @param plugin the plugin
612 * @param sa the address to send the message to
613 * @param msg message to transmit
614 * @return the number of bytes written
617 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
618 const struct GNUNET_MessageHeader *msg)
623 switch (sa->sa_family)
626 if (NULL == plugin->sockv4)
629 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
630 sa, slen = sizeof (struct sockaddr_in));
633 if (NULL == plugin->sockv6)
636 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
637 sa, slen = sizeof (struct sockaddr_in6));
643 if (GNUNET_SYSERR == sent)
645 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
646 LOG (GNUNET_ERROR_TYPE_ERROR,
647 "UDP transmited %u-byte message to %s (%d: %s)\n",
648 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
649 (sent < 0) ? STRERROR (errno) : "ok");
652 LOG (GNUNET_ERROR_TYPE_DEBUG,
653 "UDP transmited %u-byte message to %s (%d: %s)\n",
654 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
655 (sent < 0) ? STRERROR (errno) : "ok");
661 * Function that is called with messages created by the fragmentation
662 * module. In the case of the 'proc' callback of the
663 * GNUNET_FRAGMENT_context_create function, this function must
664 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
666 * @param cls closure, the 'struct PeerSession'
667 * @param msg the message that was created
670 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
672 struct Session *session = cls;
674 udp_send (session->plugin, session->sock_addr, msg);
675 GNUNET_FRAGMENT_context_transmission_done (session->frag);
679 static struct Session *
680 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
681 const void *addr, size_t addrlen,
682 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
684 struct Session *peer_session;
685 const struct IPv4UdpAddress *t4;
686 const struct IPv6UdpAddress *t6;
687 struct sockaddr_in *v4;
688 struct sockaddr_in6 *v6;
693 case sizeof (struct IPv4UdpAddress):
694 if (NULL == plugin->sockv4)
700 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
701 len = sizeof (struct sockaddr_in);
702 v4 = (struct sockaddr_in *) &peer_session[1];
703 v4->sin_family = AF_INET;
704 #if HAVE_SOCKADDR_IN_SIN_LEN
705 v4->sin_len = sizeof (struct sockaddr_in);
707 v4->sin_port = t4->u4_port;
708 v4->sin_addr.s_addr = t4->ipv4_addr;
710 case sizeof (struct IPv6UdpAddress):
711 if (NULL == plugin->sockv6)
717 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
718 len = sizeof (struct sockaddr_in6);
719 v6 = (struct sockaddr_in6 *) &peer_session[1];
720 v6->sin6_family = AF_INET6;
721 #if HAVE_SOCKADDR_IN_SIN_LEN
722 v6->sin6_len = sizeof (struct sockaddr_in6);
724 v6->sin6_port = t6->u6_port;
725 v6->sin6_addr = t6->ipv6_addr;
728 /* Must have a valid address to send to */
733 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
734 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
735 peer_session->addrlen = len;
736 peer_session->target = *target;
737 peer_session->plugin = plugin;
738 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
739 peer_session->cont = cont;
740 peer_session->cont_cls = cont_cls;
746 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
750 udp_call_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
752 struct Session *s = cls;
753 GNUNET_TRANSPORT_TransmitContinuation cont = s->cont;
755 s->delayed_cont_task = GNUNET_SCHEDULER_NO_TASK;
757 cont (s->cont_cls, &s->target, GNUNET_OK);
762 * Function that can be used by the transport service to transmit
763 * a message using the plugin.
766 * @param target who should receive this message (ignored by UDP)
767 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
768 * @param msgbuf_size the size of the msgbuf to send
769 * @param priority how important is the message (ignored by UDP)
770 * @param timeout when should we time out (give up) if we can not transmit?
771 * @param session identifier used for this session (NULL for UDP)
772 * @param addr the addr to send the message to
773 * @param addrlen the len of addr
774 * @param force_address not used, we had better have an address to send to
775 * because we are stateless!!
776 * @param cont continuation to call once the message has
777 * been transmitted (or if the transport is ready
778 * for the next transmission call; or if the
779 * peer disconnected...)
780 * @param cont_cls closure for cont
782 * @return the number of bytes written (may return 0 and the message can
783 * still be transmitted later!)
786 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
787 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
788 struct GNUNET_TIME_Relative timeout, struct Session *session,
789 const void *addr, size_t addrlen, int force_address,
790 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
792 struct Plugin *plugin = cls;
793 struct Session *peer_session;
795 const struct IPv4UdpAddress *t4;
796 const struct IPv6UdpAddress *t6;
797 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
799 struct UDPMessage *udp;
800 struct GNUNET_TIME_Relative delta;
802 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
805 return GNUNET_SYSERR;
808 LOG (GNUNET_ERROR_TYPE_DEBUG,
809 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
810 msgbuf_size, GNUNET_i2s (target), udp_address_to_string (NULL, addr,
812 session, force_address);
814 if ((force_address == GNUNET_SYSERR) && (session == NULL))
815 return GNUNET_SYSERR;
818 /* safety check: comparing address to address stored in session */
819 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
822 GNUNET_assert (GNUNET_YES ==
823 GNUNET_CONTAINER_multihashmap_contains_value
824 (plugin->inbound_sessions, &target->hashPubKey, s));
826 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
827 return GNUNET_SYSERR;
830 case sizeof (struct IPv4UdpAddress):
831 if (NULL == plugin->sockv4)
834 cont (cont_cls, target, GNUNET_SYSERR);
835 return GNUNET_SYSERR;
838 if (s->addrlen != (sizeof (struct sockaddr_in)))
839 return GNUNET_SYSERR;
840 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
842 GNUNET_assert (a4->sin_port == t4->u4_port);
844 memcmp (&a4->sin_addr, &t4->ipv4_addr,
845 sizeof (struct in_addr)));
846 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
849 case sizeof (struct IPv6UdpAddress):
850 if (NULL == plugin->sockv6)
853 cont (cont_cls, target, GNUNET_SYSERR);
854 return GNUNET_SYSERR;
857 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
858 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
860 GNUNET_assert (a6->sin6_port == t6->u6_port);
862 memcmp (&a6->sin6_addr, &t6->ipv6_addr,
863 sizeof (struct in6_addr)));
864 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
868 /* Must have a valid address to send to */
873 if ((addr == NULL) || (addrlen == 0))
874 return GNUNET_SYSERR;
875 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
876 if (peer_session == NULL)
879 cont (cont_cls, target, GNUNET_SYSERR);
880 return GNUNET_SYSERR;;
884 udp = (struct UDPMessage *) mbuf;
885 udp->header.size = htons (mlen);
886 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
887 udp->reserved = htonl (0);
888 udp->sender = *plugin->env->my_identity;
889 memcpy (&udp[1], msgbuf, msgbuf_size);
892 delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
894 delta = GNUNET_TIME_UNIT_ZERO;
897 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
900 if ((delta.rel_value > 0) && (mlen > 0))
903 s->cont_cls = cont_cls;
904 s->delayed_cont_task =
905 GNUNET_SCHEDULER_add_delayed (delta, &udp_call_continuation, s);
908 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
910 GNUNET_free_non_null (peer_session);
914 GNUNET_assert (GNUNET_OK ==
915 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
918 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
920 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
922 plugin->last_expected_delay,
923 &udp->header, &send_fragment,
931 * Closure for 'process_inbound_tokenized_messages'
933 struct SourceInformation
938 struct GNUNET_PeerIdentity sender;
946 * Number of bytes in source address.
950 struct Session *session;
955 * Message tokenizer has broken up an incomming message. Pass it on
958 * @param cls the 'struct Plugin'
959 * @param client the 'struct SourceInformation'
960 * @param hdr the actual message
963 process_inbound_tokenized_messages (void *cls, void *client,
964 const struct GNUNET_MessageHeader *hdr)
966 struct Plugin *plugin = cls;
967 struct SourceInformation *si = client;
968 struct GNUNET_ATS_Information distance;
969 struct GNUNET_TIME_Relative delay;
972 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
973 distance.value = htonl (1);
975 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving Session %X %s to transport\n",
976 si->session, GNUNET_i2s (&si->session->target));
978 plugin->env->receive (plugin->env->cls, &si->sender, hdr, &distance, 1,
979 si->session, si->arg, si->args);
980 si->session->flow_delay_for_other_peer = delay;
984 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
986 struct Session *s = cls;
988 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
989 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %X (`%s') is now invalid\n", s,
990 GNUNET_a2s (s->sock_addr, s->addrlen));
992 s->plugin->env->session_end (s->plugin->env->cls, &s->target, s);
993 GNUNET_assert (GNUNET_YES ==
994 GNUNET_CONTAINER_multihashmap_remove (s->
995 plugin->inbound_sessions,
996 &s->target.hashPubKey,
1003 * We've received a UDP Message. Process it (pass contents to main service).
1005 * @param plugin plugin context
1006 * @param msg the message
1007 * @param sender_addr sender address
1008 * @param sender_addr_len number of bytes in sender_addr
1011 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1012 const struct sockaddr *sender_addr,
1013 socklen_t sender_addr_len)
1015 struct SourceInformation si;
1016 struct IPv4UdpAddress u4;
1017 struct IPv6UdpAddress u6;
1021 if (0 != ntohl (msg->reserved))
1023 GNUNET_break_op (0);
1026 if (ntohs (msg->header.size) <
1027 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1029 GNUNET_break_op (0);
1033 /* convert address */
1034 switch (sender_addr->sa_family)
1037 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1038 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1039 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1044 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1045 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1046 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1055 LOG (GNUNET_ERROR_TYPE_DEBUG,
1056 "Received message with %u bytes from peer `%s' at `%s'\n",
1057 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1058 GNUNET_a2s (sender_addr, sender_addr_len));
1061 /* create a session for inbound connections */
1062 const struct UDPMessage *udp_msg = (const struct UDPMessage *) msg;
1064 LOG (GNUNET_ERROR_TYPE_DEBUG,
1065 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
1066 GNUNET_i2s (&udp_msg->sender), udp_address_to_string (NULL, arg, args));
1068 struct Session *s = NULL;
1070 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr,
1075 LOG (GNUNET_ERROR_TYPE_DEBUG,
1076 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
1077 s, GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1081 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
1082 LOG (GNUNET_ERROR_TYPE_DEBUG,
1083 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n", s,
1084 GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1086 GNUNET_assert (GNUNET_OK ==
1087 GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
1088 &s->target.hashPubKey, s,
1089 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1092 GNUNET_TIME_relative_to_absolute
1093 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1094 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
1096 GNUNET_SCHEDULER_cancel (s->invalidation_task);
1097 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rescheduling %X' `%s'\n", s,
1099 udp_address_to_string (NULL, arg, args));
1101 s->invalidation_task =
1102 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1103 &invalidation_task, s);
1104 /* iterate over all embedded messages */
1105 si.sender = msg->sender;
1109 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1110 ntohs (msg->header.size) -
1111 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1116 * Process a defragmented message.
1118 * @param cls the 'struct ReceiveContext'
1119 * @param msg the message
1122 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1124 struct ReceiveContext *rc = cls;
1126 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1131 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1136 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1137 rc->src_addr, rc->addr_len);
1142 * Transmit an acknowledgement.
1144 * @param cls the 'struct ReceiveContext'
1145 * @param id message ID (unused)
1146 * @param msg ack to transmit
1149 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1151 struct ReceiveContext *rc = cls;
1153 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1155 struct UDP_ACK_Message *udp_ack;
1160 s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
1163 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1164 delay = s->flow_delay_for_other_peer.rel_value;
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Sending ACK to `%s' including delay of %u ms\n",
1173 GNUNET_a2s (rc->src_addr,
1174 (rc->src_addr->sa_family ==
1175 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1179 udp_ack = (struct UDP_ACK_Message *) buf;
1180 udp_ack->header.size = htons ((uint16_t) msize);
1181 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1182 udp_ack->delay = htonl (delay);
1183 udp_ack->sender = *rc->plugin->env->my_identity;
1184 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1185 (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
1190 * Closure for 'find_receive_context'.
1192 struct FindReceiveContext
1195 * Where to store the result.
1197 struct ReceiveContext *rc;
1202 const struct sockaddr *addr;
1205 * Number of bytes in 'addr'.
1209 struct Session *session;
1214 * Scan the heap for a receive context with the given address.
1216 * @param cls the 'struct FindReceiveContext'
1217 * @param node internal node of the heap
1218 * @param element value stored at the node (a 'struct ReceiveContext')
1219 * @param cost cost associated with the node
1220 * @return GNUNET_YES if we should continue to iterate,
1224 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1225 void *element, GNUNET_CONTAINER_HeapCostType cost)
1227 struct FindReceiveContext *frc = cls;
1228 struct ReceiveContext *e = element;
1230 if ((frc->addr_len == e->addr_len) &&
1231 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1241 struct Plugin *plugin;
1243 struct IPv4UdpAddress addr;
1248 struct Plugin *plugin;
1250 struct IPv6UdpAddress addr;
1255 * Read and process a message from the given socket.
1257 * @param plugin the overall plugin
1258 * @param rsock socket to read from
1261 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1267 const struct GNUNET_MessageHeader *msg;
1268 const struct GNUNET_MessageHeader *ack;
1269 struct Session *peer_session;
1270 const struct UDP_ACK_Message *udp_ack;
1271 struct ReceiveContext *rc;
1272 struct GNUNET_TIME_Absolute now;
1273 struct FindReceiveContext frc;
1274 struct Session *s = NULL;
1275 struct GNUNET_TIME_Relative flow_delay;
1277 fromlen = sizeof (addr);
1278 memset (&addr, 0, sizeof (addr));
1280 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1281 (struct sockaddr *) &addr, &fromlen);
1282 if (ret < sizeof (struct GNUNET_MessageHeader))
1284 GNUNET_break_op (0);
1287 msg = (const struct GNUNET_MessageHeader *) buf;
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1291 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1293 if (ret != ntohs (msg->size))
1295 GNUNET_break_op (0);
1298 switch (ntohs (msg->type))
1300 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1302 if (fromlen == sizeof (struct sockaddr_in))
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
1305 ret, GNUNET_a2s((const struct sockaddr *) &addr, fromlen));
1307 struct Mstv4Context *mc;
1308 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
1309 struct sockaddr_in *av4 = (struct sockaddr_in *) &addr;
1311 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
1312 mc->addr.u4_port = av4->sin_port;
1314 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, ret, GNUNET_NO,
1318 else if (fromlen == sizeof (struct sockaddr_in6))
1320 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
1321 ret, GNUNET_a2s((const struct sockaddr *) &addr, fromlen));
1323 struct Mstv6Context *mc;
1324 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
1325 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) &addr;
1327 mc->addr.ipv6_addr = av6->sin6_addr;
1328 mc->addr.u6_port = av6->sin6_port;
1331 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, ret, GNUNET_NO,
1337 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1338 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1340 GNUNET_break_op (0);
1343 process_udp_message (plugin, (const struct UDPMessage *) msg,
1344 (const struct sockaddr *) addr, fromlen);
1346 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1348 if (ntohs (msg->size) <
1349 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1351 GNUNET_break_op (0);
1354 udp_ack = (const struct UDP_ACK_Message *) msg;
1355 s = find_inbound_session (plugin, &udp_ack->sender, addr, fromlen);
1358 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1360 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1361 flow_delay.rel_value);
1363 s->flow_delay_from_other_peer =
1364 GNUNET_TIME_relative_to_absolute (flow_delay);
1366 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1367 if (ntohs (ack->size) !=
1368 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1370 GNUNET_break_op (0);
1374 LOG (GNUNET_ERROR_TYPE_DEBUG,
1375 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1376 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1377 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1380 peer_session = find_session (plugin, &udp_ack->sender);
1381 if (NULL == peer_session)
1384 LOG (GNUNET_ERROR_TYPE_DEBUG,
1385 "Session for ACK not found, dropping ACK!\n");
1389 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1391 GNUNET_assert (GNUNET_OK ==
1392 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1396 plugin->last_expected_delay =
1397 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1398 if (peer_session->cont != NULL)
1399 peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
1400 GNUNET_free (peer_session);
1402 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1404 frc.addr = (const struct sockaddr *) addr;
1405 frc.addr_len = fromlen;
1406 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1408 now = GNUNET_TIME_absolute_get ();
1412 /* need to create a new RC */
1413 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1414 memcpy (&rc[1], addr, fromlen);
1415 rc->src_addr = (const struct sockaddr *) &rc[1];
1416 rc->addr_len = fromlen;
1417 rc->plugin = plugin;
1419 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1420 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1421 &fragment_msg_proc, &ack_proc);
1423 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1424 (GNUNET_CONTAINER_HeapCostType)
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1429 (unsigned int) ntohs (msg->size),
1430 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1433 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1435 /* keep this 'rc' from expiring */
1436 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1437 (GNUNET_CONTAINER_HeapCostType)
1440 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1441 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1443 /* remove 'rc' that was inactive the longest */
1444 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1445 GNUNET_assert (NULL != rc);
1446 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1451 GNUNET_break_op (0);
1458 * We have been notified that our writeset has something to read. We don't
1459 * know which socket needs to be read, so we have to check each one
1460 * Then reschedule this function to be called again once more is available.
1462 * @param cls the plugin handle
1463 * @param tc the scheduling context (for rescheduling this function again)
1466 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1468 struct Plugin *plugin = cls;
1470 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1471 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1473 if ((NULL != plugin->sockv4) &&
1474 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1475 udp_read (plugin, plugin->sockv4);
1476 if ((NULL != plugin->sockv6) &&
1477 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1478 udp_read (plugin, plugin->sockv6);
1479 plugin->select_task =
1480 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1481 GNUNET_SCHEDULER_NO_TASK,
1482 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1483 NULL, &udp_plugin_select, plugin);
1489 broadcast_ipv4_mst_cb (void *cls, void *client,
1490 const struct GNUNET_MessageHeader *message)
1492 struct Plugin *plugin = cls;
1493 struct Mstv4Context *mc = client;
1494 const struct GNUNET_MessageHeader *hello;
1495 struct UDP_Beacon_Message *msg;
1497 msg = (struct UDP_Beacon_Message *) message;
1499 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1500 ntohs (msg->header.type))
1503 LOG (GNUNET_ERROR_TYPE_DEBUG,
1504 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1505 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1506 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1508 struct GNUNET_ATS_Information ats;
1510 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1511 ats.value = htonl (1);
1513 hello = (struct GNUNET_MessageHeader *) &msg[1];
1514 plugin->env->receive (plugin->env->cls, &msg->sender, hello, &ats, 1, NULL,
1515 (const char *) &mc->addr, sizeof (mc->addr));
1517 GNUNET_STATISTICS_update (plugin->env->stats,
1518 _("# IPv4 broadcast HELLO beacons received via udp"), 1,
1525 broadcast_ipv6_mst_cb (void *cls, void *client,
1526 const struct GNUNET_MessageHeader *message)
1529 struct Plugin *plugin = cls;
1530 struct Mstv6Context *mc = client;
1531 const struct GNUNET_MessageHeader *hello;
1532 struct UDP_Beacon_Message *msg;
1534 msg = (struct UDP_Beacon_Message *) message;
1536 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1537 ntohs (msg->header.type))
1540 LOG (GNUNET_ERROR_TYPE_DEBUG,
1541 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1542 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1543 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1545 struct GNUNET_ATS_Information ats;
1547 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1548 ats.value = htonl (1);
1550 hello = (struct GNUNET_MessageHeader *) &msg[1];
1551 plugin->env->receive (plugin->env->cls, &msg->sender, hello, &ats, 1, NULL,
1552 (const char *) &mc->addr, sizeof (mc->addr));
1554 GNUNET_STATISTICS_update (plugin->env->stats,
1555 _("# IPv6 multicast HELLO beacons received via udp"), 1,
1562 udp_ipv4_broadcast_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1564 struct Plugin *plugin = cls;
1567 uint16_t hello_size;
1570 const struct GNUNET_MessageHeader *hello;
1571 struct UDP_Beacon_Message *msg;
1572 struct BroadcastAddress * baddr;
1574 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1576 hello = plugin->env->get_our_hello ();
1577 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1578 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1580 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1581 (msg_size > (UDP_MTU)))
1584 msg = (struct UDP_Beacon_Message *) buf;
1585 msg->sender = *(plugin->env->my_identity);
1586 msg->header.size = ntohs (msg_size);
1587 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1588 memcpy (&msg[1], hello, hello_size);
1591 baddr = plugin->ipv4_broadcast_head;
1593 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
1595 struct sockaddr_in * addr = (struct sockaddr_in * ) baddr->addr;
1596 addr->sin_port = htons (plugin->port);
1598 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, msg_size,
1599 (const struct sockaddr *) addr,
1601 if (sent == GNUNET_SYSERR)
1602 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "sendto");
1604 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sent HELLO beacon broadcast with %i bytes to address %s\n",
1605 sent, GNUNET_a2s(baddr->addr, baddr->addrlen));
1606 baddr = baddr->next;
1609 plugin->send_ipv4_broadcast_task =
1610 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1611 &udp_ipv4_broadcast_send, plugin);
1615 udp_ipv6_broadcast_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1617 struct Plugin *plugin = cls;
1620 uint16_t hello_size;
1623 const struct GNUNET_MessageHeader *hello;
1624 struct UDP_Beacon_Message *msg;
1626 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1628 hello = plugin->env->get_our_hello ();
1629 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1630 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1632 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1633 (msg_size > (UDP_MTU)))
1636 msg = (struct UDP_Beacon_Message *) buf;
1637 msg->sender = *(plugin->env->my_identity);
1638 msg->header.size = ntohs (msg_size);
1639 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1640 memcpy (&msg[1], hello, hello_size);
1643 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, msg_size,
1644 (const struct sockaddr *) &plugin->ipv6_multicast_address,
1645 sizeof(struct sockaddr_in6));
1646 if (sent == GNUNET_SYSERR)
1647 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "sendto");
1649 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
1650 sent, GNUNET_a2s((const struct sockaddr *) &plugin->ipv6_multicast_address, sizeof(struct sockaddr_in6)));
1654 plugin->send_ipv6_broadcast_task =
1655 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1656 &udp_ipv6_broadcast_send, plugin);
1660 * Check if the given port is plausible (must be either our listen
1661 * port or our advertised port). If it is neither, we return
1664 * @param plugin global variables
1665 * @param in_port port number to check
1666 * @return GNUNET_OK if port is either open_port or adv_port
1669 check_port (struct Plugin *plugin, uint16_t in_port)
1671 if ((in_port == plugin->port) || (in_port == plugin->aport))
1673 return GNUNET_SYSERR;
1678 * Function that will be called to check if a binary address for this
1679 * plugin is well-formed and corresponds to an address for THIS peer
1680 * (as per our configuration). Naturally, if absolutely necessary,
1681 * plugins can be a bit conservative in their answer, but in general
1682 * plugins should make sure that the address does not redirect
1683 * traffic to a 3rd party that might try to man-in-the-middle our
1686 * @param cls closure, should be our handle to the Plugin
1687 * @param addr pointer to the address
1688 * @param addrlen length of addr
1689 * @return GNUNET_OK if this is a plausible address for this peer
1690 * and transport, GNUNET_SYSERR if not
1694 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1696 struct Plugin *plugin = cls;
1697 struct IPv4UdpAddress *v4;
1698 struct IPv6UdpAddress *v6;
1700 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1701 (addrlen != sizeof (struct IPv6UdpAddress)))
1703 GNUNET_break_op (0);
1704 return GNUNET_SYSERR;
1706 if (addrlen == sizeof (struct IPv4UdpAddress))
1708 v4 = (struct IPv4UdpAddress *) addr;
1709 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1710 return GNUNET_SYSERR;
1712 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1713 sizeof (struct in_addr)))
1714 return GNUNET_SYSERR;
1718 v6 = (struct IPv6UdpAddress *) addr;
1719 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1721 GNUNET_break_op (0);
1722 return GNUNET_SYSERR;
1724 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1725 return GNUNET_SYSERR;
1727 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1728 sizeof (struct in6_addr)))
1729 return GNUNET_SYSERR;
1736 * Function called for a quick conversion of the binary address to
1737 * a numeric address. Note that the caller must not free the
1738 * address and that the next call to this function is allowed
1739 * to override the address again.
1741 * @param cls closure
1742 * @param addr binary address
1743 * @param addrlen length of the address
1744 * @return string representing the same address
1747 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1749 static char rbuf[INET6_ADDRSTRLEN + 10];
1750 char buf[INET6_ADDRSTRLEN];
1754 const struct IPv4UdpAddress *t4;
1755 const struct IPv6UdpAddress *t6;
1759 if (addrlen == sizeof (struct IPv6UdpAddress))
1763 port = ntohs (t6->u6_port);
1764 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1767 else if (addrlen == sizeof (struct IPv4UdpAddress))
1771 port = ntohs (t4->u4_port);
1772 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1777 GNUNET_break_op (0);
1780 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1781 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1788 * Closure for 'append_port'.
1790 struct PrettyPrinterContext
1793 * Function to call with the result.
1795 GNUNET_TRANSPORT_AddressStringCallback asc;
1798 * Clsoure for 'asc'.
1803 * Port to add after the IP address.
1810 * Append our port and forward the result.
1812 * @param cls a 'struct PrettyPrinterContext'
1813 * @param hostname result from DNS resolver
1816 append_port (void *cls, const char *hostname)
1818 struct PrettyPrinterContext *ppc = cls;
1821 if (hostname == NULL)
1823 ppc->asc (ppc->asc_cls, NULL);
1827 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1828 ppc->asc (ppc->asc_cls, ret);
1834 * Convert the transports address to a nice, human-readable
1837 * @param cls closure
1838 * @param type name of the transport that generated the address
1839 * @param addr one of the addresses of the host, NULL for the last address
1840 * the specific address format depends on the transport
1841 * @param addrlen length of the address
1842 * @param numeric should (IP) addresses be displayed in numeric form?
1843 * @param timeout after how long should we give up?
1844 * @param asc function to call on each string
1845 * @param asc_cls closure for asc
1848 udp_plugin_address_pretty_printer (void *cls, const char *type,
1849 const void *addr, size_t addrlen,
1851 struct GNUNET_TIME_Relative timeout,
1852 GNUNET_TRANSPORT_AddressStringCallback asc,
1855 struct PrettyPrinterContext *ppc;
1858 struct sockaddr_in a4;
1859 struct sockaddr_in6 a6;
1860 const struct IPv4UdpAddress *u4;
1861 const struct IPv6UdpAddress *u6;
1864 if (addrlen == sizeof (struct IPv6UdpAddress))
1867 memset (&a6, 0, sizeof (a6));
1868 a6.sin6_family = AF_INET6;
1869 #if HAVE_SOCKADDR_IN_SIN_LEN
1870 a6.sin6_len = sizeof (a6);
1872 a6.sin6_port = u6->u6_port;
1873 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1874 port = ntohs (u6->u6_port);
1878 else if (addrlen == sizeof (struct IPv4UdpAddress))
1881 memset (&a4, 0, sizeof (a4));
1882 a4.sin_family = AF_INET;
1883 #if HAVE_SOCKADDR_IN_SIN_LEN
1884 a4.sin_len = sizeof (a4);
1886 a4.sin_port = u4->u4_port;
1887 a4.sin_addr.s_addr = u4->ipv4_addr;
1888 port = ntohs (u4->u4_port);
1894 /* invalid address */
1895 GNUNET_break_op (0);
1896 asc (asc_cls, NULL);
1899 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1901 ppc->asc_cls = asc_cls;
1903 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1908 * Our external IP address/port mapping has changed.
1910 * @param cls closure, the 'struct LocalAddrList'
1911 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1912 * the previous (now invalid) one
1913 * @param addr either the previous or the new public IP address
1914 * @param addrlen actual lenght of the address
1917 udp_nat_port_map_callback (void *cls, int add_remove,
1918 const struct sockaddr *addr, socklen_t addrlen)
1920 struct Plugin *plugin = cls;
1921 struct IPv4UdpAddress u4;
1922 struct IPv6UdpAddress u6;
1926 /* convert 'addr' to our internal format */
1927 switch (addr->sa_family)
1930 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1931 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1932 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1937 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1938 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1939 sizeof (struct in6_addr));
1940 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1948 /* modify our published address list */
1949 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1954 iface_proc (void *cls, const char *name,
1956 const struct sockaddr * addr,
1957 const struct sockaddr * broadcast_addr,
1958 const struct sockaddr * netmask,
1961 struct Plugin *plugin = cls;
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
1966 GNUNET_a2s(addr,addrlen), name, addr);
1967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "broadcast address %s for interface %s %p\n ",
1968 GNUNET_a2s(broadcast_addr,addrlen), name, broadcast_addr);
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
1970 GNUNET_a2s(netmask,addrlen), name, netmask);
1973 /* Collecting broadcast addresses*/
1974 if (broadcast_addr != NULL)
1976 struct BroadcastAddress * ba = GNUNET_malloc(sizeof (struct BroadcastAddress));
1977 ba->addr = GNUNET_malloc(addrlen);
1978 memcpy(ba->addr, broadcast_addr, addrlen);
1979 ba->addrlen = addrlen;
1980 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_broadcast_head, plugin->ipv4_broadcast_tail, ba);
1988 * The exported method. Makes the core api available via a global and
1989 * returns the udp transport API.
1991 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1992 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1995 libgnunet_plugin_transport_udp_init (void *cls)
1997 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1998 unsigned long long port;
1999 unsigned long long aport;
2000 struct GNUNET_TRANSPORT_PluginFunctions *api;
2001 struct Plugin *plugin;
2002 int sockets_created;
2004 struct GNUNET_TIME_Relative interval;
2005 struct sockaddr_in serverAddrv4;
2006 struct sockaddr_in6 serverAddrv6;
2007 struct sockaddr *serverAddr;
2008 struct sockaddr *addrs[2];
2009 socklen_t addrlens[2];
2012 unsigned long long udp_max_bps;
2015 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2020 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2022 if (broadcast == GNUNET_SYSERR)
2023 broadcast = GNUNET_NO;
2025 if (GNUNET_SYSERR ==
2026 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
2027 "BROADCAST_INTERVAL", &interval))
2028 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2031 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2032 "MAX_BPS", &udp_max_bps))
2033 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2035 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2036 "ADVERTISED_PORT", &aport))
2040 LOG (GNUNET_ERROR_TYPE_WARNING,
2041 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2045 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2046 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2048 plugin = GNUNET_malloc (sizeof (struct Plugin));
2049 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2050 GNUNET_BANDWIDTH_value_init ((uint32_t)
2052 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
2053 plugin->port = port;
2054 plugin->aport = aport;
2056 plugin->broadcast_interval = interval;
2057 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2060 api->send = &udp_plugin_send;
2061 api->disconnect = &udp_disconnect;
2062 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2063 api->address_to_string = &udp_address_to_string;
2064 api->check_address = &udp_plugin_check_address;
2067 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2068 "BINDTO", &plugin->bind4_address))
2070 LOG (GNUNET_ERROR_TYPE_DEBUG,
2071 "Binding udp plugin to specific address: `%s'\n",
2072 plugin->bind4_address);
2073 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
2075 GNUNET_free (plugin->bind4_address);
2076 GNUNET_free (plugin);
2083 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2084 "BINDTO6", &plugin->bind6_address))
2086 LOG (GNUNET_ERROR_TYPE_DEBUG,
2087 "Binding udp plugin to specific address: `%s'\n",
2088 plugin->bind6_address);
2090 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
2092 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2093 plugin->bind6_address);
2094 GNUNET_free_non_null (plugin->bind4_address);
2095 GNUNET_free (plugin->bind6_address);
2096 GNUNET_free (plugin);
2103 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2105 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2107 plugin->inbound_sessions =
2108 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2110 sockets_created = 0;
2112 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
2115 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2116 if (NULL == plugin->sockv6)
2118 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2122 #if HAVE_SOCKADDR_IN_SIN_LEN
2123 serverAddrv6.sin6_len = sizeof (serverAddrv6);
2125 serverAddrv6.sin6_family = AF_INET6;
2126 serverAddrv6.sin6_addr = in6addr_any;
2127 serverAddrv6.sin6_port = htons (plugin->port);
2128 addrlen = sizeof (serverAddrv6);
2129 serverAddr = (struct sockaddr *) &serverAddrv6;
2131 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2132 ntohs (serverAddrv6.sin6_port));
2135 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2138 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2140 LOG (GNUNET_ERROR_TYPE_DEBUG,
2141 "IPv6 Binding failed, trying new port %d\n",
2142 ntohs (serverAddrv6.sin6_port));
2147 GNUNET_NETWORK_socket_close (plugin->sockv6);
2148 plugin->sockv6 = NULL;
2152 if (plugin->sockv6 != NULL)
2154 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2155 addrlens[sockets_created] = sizeof (serverAddrv6);
2162 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2163 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2164 if (NULL == plugin->sockv4)
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->port);
2176 addrlen = sizeof (serverAddrv4);
2177 serverAddr = (struct sockaddr *) &serverAddrv4;
2179 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2180 ntohs (serverAddrv4.sin_port));
2183 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2186 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2188 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2189 ntohs (serverAddrv4.sin_port));
2194 GNUNET_NETWORK_socket_close (plugin->sockv4);
2195 plugin->sockv4 = NULL;
2199 if (plugin->sockv4 != NULL)
2201 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2202 addrlens[sockets_created] = sizeof (serverAddrv4);
2207 plugin->rs = GNUNET_NETWORK_fdset_create ();
2208 GNUNET_NETWORK_fdset_zero (plugin->rs);
2209 if (NULL != plugin->sockv4)
2210 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
2211 if (NULL != plugin->sockv6)
2212 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
2214 plugin->select_task =
2215 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2216 GNUNET_SCHEDULER_NO_TASK,
2217 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
2218 NULL, &udp_plugin_select, plugin);
2224 /* create IPv4 broadcast socket */
2225 plugin->broadcast_ipv4 = GNUNET_NO;
2226 if (plugin->sockv4 != NULL)
2230 if (GNUNET_NETWORK_socket_setsockopt
2231 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes, sizeof (int)) != GNUNET_OK)
2233 LOG (GNUNET_ERROR_TYPE_WARNING,
2235 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
2236 ntohs (serverAddrv4.sin_port));
2240 GNUNET_OS_network_interfaces_list(iface_proc, plugin);
2241 plugin->send_ipv4_broadcast_task =
2242 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
2244 plugin->broadcast_ipv4_mst =
2245 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
2247 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
2248 plugin->broadcast_ipv4 = GNUNET_YES;
2252 plugin->broadcast_ipv6 = GNUNET_NO;
2253 if (plugin->sockv6 != NULL)
2255 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
2256 GNUNET_assert (1 == inet_pton(AF_INET6, "ff02::1", &plugin->ipv6_multicast_address.sin6_addr));
2258 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
2259 plugin->ipv6_multicast_address.sin6_port = htons(plugin->port);
2261 plugin->broadcast_ipv6_mst =
2262 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
2264 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting running\n");
2267 plugin->send_ipv6_broadcast_task =
2268 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
2269 plugin->broadcast_ipv6 = GNUNET_YES;
2273 if (sockets_created == 0)
2274 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2276 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
2277 (const struct sockaddr **) addrs, addrlens,
2278 &udp_nat_port_map_callback, NULL, plugin);
2280 udp_ipv6_broadcast_send(NULL, NULL);
2284 * Shutdown the plugin.
2286 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
2290 libgnunet_plugin_transport_udp_done (void *cls)
2292 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2293 struct Plugin *plugin = api->cls;
2294 struct ReceiveContext *rc;
2296 /* FIXME: clean up heap and hashmap */
2297 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
2299 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2300 plugin->sessions = NULL;
2301 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
2302 &destroy_inbound_session, NULL);
2303 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
2304 plugin->inbound_sessions = NULL;
2305 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
2307 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2310 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
2312 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2314 GNUNET_SCHEDULER_cancel (plugin->select_task);
2315 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2317 if (plugin->sockv4 != NULL)
2319 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2320 plugin->sockv4 = NULL;
2322 if (plugin->sockv6 != NULL)
2324 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2325 plugin->sockv6 = NULL;
2328 if (plugin->broadcast_ipv4)
2330 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2332 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
2333 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2336 if (plugin->broadcast_ipv4_mst != NULL)
2337 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
2339 while (plugin->ipv4_broadcast_head != NULL)
2341 struct BroadcastAddress * p= plugin->ipv4_broadcast_head;
2342 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_broadcast_head, plugin->ipv4_broadcast_tail, p);
2343 GNUNET_free (p->addr);
2348 if (plugin->broadcast_ipv6)
2350 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2352 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
2353 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2356 if (plugin->broadcast_ipv6_mst != NULL)
2357 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
2360 GNUNET_SERVER_mst_destroy (plugin->mst);
2361 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2363 GNUNET_NAT_unregister (plugin->nat);
2365 GNUNET_free (plugin);
2370 /* end of plugin_transport_udp.c */