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;
267 * Encapsulation of all of the state of the plugin.
275 struct GNUNET_TRANSPORT_PluginEnvironment *env;
278 * Session of peers with whom we are currently connected,
279 * map of peer identity to 'struct PeerSession'.
281 struct GNUNET_CONTAINER_MultiHashMap *sessions;
284 * Session of peers with whom we are currently connected,
285 * map of peer identity to 'struct PeerSession'.
287 struct GNUNET_CONTAINER_MultiHashMap *inbound_sessions;
290 * Heap with all of our defragmentation activities.
292 struct GNUNET_CONTAINER_Heap *defrags;
297 GNUNET_SCHEDULER_TaskIdentifier select_task;
300 * Tokenizer for inbound messages.
302 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
305 * Bandwidth tracker to limit global UDP traffic.
307 struct GNUNET_BANDWIDTH_Tracker tracker;
310 * Address we were told to bind to exclusively (IPv4).
315 * Address we were told to bind to exclusively (IPv6).
320 * Handle to NAT traversal support.
322 struct GNUNET_NAT_Handle *nat;
327 struct GNUNET_NETWORK_FDSet *rs;
330 * The read socket for IPv4
332 struct GNUNET_NETWORK_Handle *sockv4;
335 * The read socket for IPv6
337 struct GNUNET_NETWORK_Handle *sockv6;
346 * Tokenizer for inbound messages.
348 struct GNUNET_SERVER_MessageStreamTokenizer *broadcast_mst;
351 * The read socket for IPv4
353 struct GNUNET_NETWORK_Handle *sockv4_broadcast;
356 * ID of select broadcast task
358 GNUNET_SCHEDULER_TaskIdentifier select_broadcast_task;
361 * ID of select broadcast task
363 GNUNET_SCHEDULER_TaskIdentifier send_broadcast_task;
368 struct GNUNET_NETWORK_FDSet *broadcast_rs;
373 struct GNUNET_TIME_Relative broadcast_interval;
376 * expected delay for ACKs
378 struct GNUNET_TIME_Relative last_expected_delay;
381 * Port we broadcasting on.
383 uint16_t broadcast_port;
391 * Port we advertise on.
397 struct PeerSessionIteratorContext
399 struct Session *result;
406 * Lookup the session for the given peer.
408 * @param plugin the plugin
409 * @param peer peer's identity
410 * @return NULL if we have no session
412 static struct Session *
413 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
415 return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
421 inbound_session_iterator (void *cls, const GNUNET_HashCode * key, void *value)
423 struct PeerSessionIteratorContext *psc = cls;
424 struct Session *s = value;
426 if (s->addrlen == psc->addrlen)
428 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
431 if (psc->result != NULL)
438 * Lookup the session for the given peer.
440 * @param plugin the plugin
441 * @param peer peer's identity
442 * @return NULL if we have no session
444 static struct Session *
445 find_inbound_session (struct Plugin *plugin,
446 const struct GNUNET_PeerIdentity *peer, const void *addr,
449 struct PeerSessionIteratorContext psc;
452 psc.addrlen = addrlen;
455 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
457 &inbound_session_iterator, &psc);
463 inbound_session_by_addr_iterator (void *cls, const GNUNET_HashCode * key,
466 struct PeerSessionIteratorContext *psc = cls;
467 struct Session *s = value;
469 if (s->addrlen == psc->addrlen)
471 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
474 if (psc->result != NULL)
481 * Lookup the session for the given peer just by address.
483 * @param plugin the plugin
484 * @param addr address
485 * @param addrlen address length
486 * @return NULL if we have no session
488 static struct Session *
489 find_inbound_session_by_addr (struct Plugin *plugin, const void *addr,
492 struct PeerSessionIteratorContext psc;
495 psc.addrlen = addrlen;
498 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
499 &inbound_session_by_addr_iterator,
506 * Destroy a session, plugin is being unloaded.
509 * @param key hash of public key of target peer
510 * @param value a 'struct PeerSession*' to clean up
511 * @return GNUNET_OK (continue to iterate)
514 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
516 struct Session *peer_session = value;
518 if (peer_session->frag != NULL)
519 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
520 if (GNUNET_SCHEDULER_NO_TASK != peer_session->delayed_cont_task)
521 GNUNET_SCHEDULER_cancel (peer_session->delayed_cont_task);
522 GNUNET_free (peer_session);
527 * Destroy a session, plugin is being unloaded.
530 * @param key hash of public key of target peer
531 * @param value a 'struct PeerSession*' to clean up
532 * @return GNUNET_OK (continue to iterate)
535 destroy_inbound_session (void *cls, const GNUNET_HashCode * key, void *value)
537 struct Session *s = value;
539 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
540 GNUNET_SCHEDULER_cancel (s->invalidation_task);
541 if (GNUNET_SCHEDULER_NO_TASK != s->delayed_cont_task)
542 GNUNET_SCHEDULER_cancel (s->delayed_cont_task);
548 * Disconnect from a remote node. Clean up session if we have one for this peer
550 * @param cls closure for this call (should be handle to Plugin)
551 * @param target the peeridentity of the peer to disconnect
552 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
555 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
557 struct Plugin *plugin = cls;
558 struct Session *session;
560 session = find_session (plugin, target);
563 GNUNET_assert (GNUNET_OK ==
564 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
568 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
570 &destroy_inbound_session, NULL);
571 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
572 LOG (GNUNET_ERROR_TYPE_ERROR, "UDP DISCONNECT\n");
574 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
575 if (GNUNET_SCHEDULER_NO_TASK != session->delayed_cont_task)
576 GNUNET_SCHEDULER_cancel (session->delayed_cont_task);
577 if (session->cont != NULL)
578 session->cont (session->cont_cls, target, GNUNET_SYSERR);
579 GNUNET_free (session);
584 * Actually send out the message.
586 * @param plugin the plugin
587 * @param sa the address to send the message to
588 * @param msg message to transmit
589 * @return the number of bytes written
592 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
593 const struct GNUNET_MessageHeader *msg)
598 switch (sa->sa_family)
601 if (NULL == plugin->sockv4)
604 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
605 sa, slen = sizeof (struct sockaddr_in));
608 if (NULL == plugin->sockv6)
611 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
612 sa, slen = sizeof (struct sockaddr_in6));
618 if (GNUNET_SYSERR == sent)
620 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
621 LOG (GNUNET_ERROR_TYPE_ERROR,
622 "UDP transmited %u-byte message to %s (%d: %s)\n",
623 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
624 (sent < 0) ? STRERROR (errno) : "ok");
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "UDP transmited %u-byte message to %s (%d: %s)\n",
629 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
630 (sent < 0) ? STRERROR (errno) : "ok");
636 * Function that is called with messages created by the fragmentation
637 * module. In the case of the 'proc' callback of the
638 * GNUNET_FRAGMENT_context_create function, this function must
639 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
641 * @param cls closure, the 'struct PeerSession'
642 * @param msg the message that was created
645 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
647 struct Session *session = cls;
649 udp_send (session->plugin, session->sock_addr, msg);
650 GNUNET_FRAGMENT_context_transmission_done (session->frag);
654 static struct Session *
655 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
656 const void *addr, size_t addrlen,
657 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
659 struct Session *peer_session;
660 const struct IPv4UdpAddress *t4;
661 const struct IPv6UdpAddress *t6;
662 struct sockaddr_in *v4;
663 struct sockaddr_in6 *v6;
668 case sizeof (struct IPv4UdpAddress):
669 if (NULL == plugin->sockv4)
675 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
676 len = sizeof (struct sockaddr_in);
677 v4 = (struct sockaddr_in *) &peer_session[1];
678 v4->sin_family = AF_INET;
679 #if HAVE_SOCKADDR_IN_SIN_LEN
680 v4->sin_len = sizeof (struct sockaddr_in);
682 v4->sin_port = t4->u4_port;
683 v4->sin_addr.s_addr = t4->ipv4_addr;
685 case sizeof (struct IPv6UdpAddress):
686 if (NULL == plugin->sockv6)
692 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
693 len = sizeof (struct sockaddr_in6);
694 v6 = (struct sockaddr_in6 *) &peer_session[1];
695 v6->sin6_family = AF_INET6;
696 #if HAVE_SOCKADDR_IN_SIN_LEN
697 v6->sin6_len = sizeof (struct sockaddr_in6);
699 v6->sin6_port = t6->u6_port;
700 v6->sin6_addr = t6->ipv6_addr;
703 /* Must have a valid address to send to */
708 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
709 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
710 peer_session->addrlen = len;
711 peer_session->target = *target;
712 peer_session->plugin = plugin;
713 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
714 peer_session->cont = cont;
715 peer_session->cont_cls = cont_cls;
721 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
725 udp_call_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
727 struct Session *s = cls;
728 GNUNET_TRANSPORT_TransmitContinuation cont = s->cont;
730 s->delayed_cont_task = GNUNET_SCHEDULER_NO_TASK;
732 cont (s->cont_cls, &s->target, GNUNET_OK);
737 * Function that can be used by the transport service to transmit
738 * a message using the plugin.
741 * @param target who should receive this message (ignored by UDP)
742 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
743 * @param msgbuf_size the size of the msgbuf to send
744 * @param priority how important is the message (ignored by UDP)
745 * @param timeout when should we time out (give up) if we can not transmit?
746 * @param session identifier used for this session (NULL for UDP)
747 * @param addr the addr to send the message to
748 * @param addrlen the len of addr
749 * @param force_address not used, we had better have an address to send to
750 * because we are stateless!!
751 * @param cont continuation to call once the message has
752 * been transmitted (or if the transport is ready
753 * for the next transmission call; or if the
754 * peer disconnected...)
755 * @param cont_cls closure for cont
757 * @return the number of bytes written (may return 0 and the message can
758 * still be transmitted later!)
761 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
762 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
763 struct GNUNET_TIME_Relative timeout, struct Session *session,
764 const void *addr, size_t addrlen, int force_address,
765 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
767 struct Plugin *plugin = cls;
768 struct Session *peer_session;
770 const struct IPv4UdpAddress *t4;
771 const struct IPv6UdpAddress *t6;
772 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
774 struct UDPMessage *udp;
775 struct GNUNET_TIME_Relative delta;
777 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
780 return GNUNET_SYSERR;
783 LOG (GNUNET_ERROR_TYPE_DEBUG,
784 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
785 msgbuf_size, GNUNET_i2s (target), udp_address_to_string (NULL, addr,
787 session, force_address);
789 if ((force_address == GNUNET_SYSERR) && (session == NULL))
790 return GNUNET_SYSERR;
793 /* safety check: comparing address to address stored in session */
794 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
797 GNUNET_assert (GNUNET_YES ==
798 GNUNET_CONTAINER_multihashmap_contains_value
799 (plugin->inbound_sessions, &target->hashPubKey, s));
801 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
802 return GNUNET_SYSERR;
805 case sizeof (struct IPv4UdpAddress):
806 if (NULL == plugin->sockv4)
809 cont (cont_cls, target, GNUNET_SYSERR);
810 return GNUNET_SYSERR;
813 if (s->addrlen != (sizeof (struct sockaddr_in)))
814 return GNUNET_SYSERR;
815 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
817 GNUNET_assert (a4->sin_port == t4->u4_port);
819 memcmp (&a4->sin_addr, &t4->ipv4_addr,
820 sizeof (struct in_addr)));
821 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
824 case sizeof (struct IPv6UdpAddress):
825 if (NULL == plugin->sockv6)
828 cont (cont_cls, target, GNUNET_SYSERR);
829 return GNUNET_SYSERR;
832 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
833 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
835 GNUNET_assert (a6->sin6_port == t6->u6_port);
837 memcmp (&a6->sin6_addr, &t6->ipv6_addr,
838 sizeof (struct in6_addr)));
839 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
843 /* Must have a valid address to send to */
848 if ((addr == NULL) || (addrlen == 0))
849 return GNUNET_SYSERR;
850 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
851 if (peer_session == NULL)
854 cont (cont_cls, target, GNUNET_SYSERR);
855 return GNUNET_SYSERR;;
859 udp = (struct UDPMessage *) mbuf;
860 udp->header.size = htons (mlen);
861 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
862 udp->reserved = htonl (0);
863 udp->sender = *plugin->env->my_identity;
864 memcpy (&udp[1], msgbuf, msgbuf_size);
867 delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
869 delta = GNUNET_TIME_UNIT_ZERO;
872 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
875 if ((delta.rel_value > 0) && (mlen > 0))
878 s->cont_cls = cont_cls;
879 s->delayed_cont_task =
880 GNUNET_SCHEDULER_add_delayed (delta, &udp_call_continuation, s);
883 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
885 GNUNET_free_non_null (peer_session);
889 GNUNET_assert (GNUNET_OK ==
890 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
893 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
895 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
897 plugin->last_expected_delay,
898 &udp->header, &send_fragment,
906 * Closure for 'process_inbound_tokenized_messages'
908 struct SourceInformation
913 struct GNUNET_PeerIdentity sender;
921 * Number of bytes in source address.
925 struct Session *session;
930 * Message tokenizer has broken up an incomming message. Pass it on
933 * @param cls the 'struct Plugin'
934 * @param client the 'struct SourceInformation'
935 * @param hdr the actual message
938 process_inbound_tokenized_messages (void *cls, void *client,
939 const struct GNUNET_MessageHeader *hdr)
941 struct Plugin *plugin = cls;
942 struct SourceInformation *si = client;
943 struct GNUNET_ATS_Information distance;
944 struct GNUNET_TIME_Relative delay;
947 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
948 distance.value = htonl (1);
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving Session %X %s to transport\n",
951 si->session, GNUNET_i2s (&si->session->target));
953 plugin->env->receive (plugin->env->cls, &si->sender, hdr, &distance, 1,
954 si->session, si->arg, si->args);
955 si->session->flow_delay_for_other_peer = delay;
959 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
961 struct Session *s = cls;
963 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
964 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %X (`%s') is now invalid\n", s,
965 GNUNET_a2s (s->sock_addr, s->addrlen));
967 s->plugin->env->session_end (s->plugin->env->cls, &s->target, s);
968 GNUNET_assert (GNUNET_YES ==
969 GNUNET_CONTAINER_multihashmap_remove (s->
970 plugin->inbound_sessions,
971 &s->target.hashPubKey,
978 * We've received a UDP Message. Process it (pass contents to main service).
980 * @param plugin plugin context
981 * @param msg the message
982 * @param sender_addr sender address
983 * @param sender_addr_len number of bytes in sender_addr
986 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
987 const struct sockaddr *sender_addr,
988 socklen_t sender_addr_len)
990 struct SourceInformation si;
991 struct IPv4UdpAddress u4;
992 struct IPv6UdpAddress u6;
996 if (0 != ntohl (msg->reserved))
1001 if (ntohs (msg->header.size) <
1002 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1004 GNUNET_break_op (0);
1008 /* convert address */
1009 switch (sender_addr->sa_family)
1012 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1013 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1014 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1019 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1020 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1021 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1030 LOG (GNUNET_ERROR_TYPE_DEBUG,
1031 "Received message with %u bytes from peer `%s' at `%s'\n",
1032 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1033 GNUNET_a2s (sender_addr, sender_addr_len));
1036 /* create a session for inbound connections */
1037 const struct UDPMessage *udp_msg = (const struct UDPMessage *) msg;
1039 LOG (GNUNET_ERROR_TYPE_DEBUG,
1040 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
1041 GNUNET_i2s (&udp_msg->sender), udp_address_to_string (NULL, arg, args));
1043 struct Session *s = NULL;
1045 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr,
1050 LOG (GNUNET_ERROR_TYPE_DEBUG,
1051 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
1052 s, GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1056 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
1057 LOG (GNUNET_ERROR_TYPE_DEBUG,
1058 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n", s,
1059 GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1061 GNUNET_assert (GNUNET_OK ==
1062 GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
1063 &s->target.hashPubKey, s,
1064 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1067 GNUNET_TIME_relative_to_absolute
1068 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1069 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
1071 GNUNET_SCHEDULER_cancel (s->invalidation_task);
1072 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rescheduling %X' `%s'\n", s,
1074 udp_address_to_string (NULL, arg, args));
1076 s->invalidation_task =
1077 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1078 &invalidation_task, s);
1079 /* iterate over all embedded messages */
1080 si.sender = msg->sender;
1084 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1085 ntohs (msg->header.size) -
1086 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1091 * Process a defragmented message.
1093 * @param cls the 'struct ReceiveContext'
1094 * @param msg the message
1097 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1099 struct ReceiveContext *rc = cls;
1101 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1106 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1111 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1112 rc->src_addr, rc->addr_len);
1117 * Transmit an acknowledgement.
1119 * @param cls the 'struct ReceiveContext'
1120 * @param id message ID (unused)
1121 * @param msg ack to transmit
1124 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1126 struct ReceiveContext *rc = cls;
1128 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1130 struct UDP_ACK_Message *udp_ack;
1135 s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
1138 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1139 delay = s->flow_delay_for_other_peer.rel_value;
1146 LOG (GNUNET_ERROR_TYPE_DEBUG,
1147 "Sending ACK to `%s' including delay of %u ms\n",
1148 GNUNET_a2s (rc->src_addr,
1149 (rc->src_addr->sa_family ==
1150 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1154 udp_ack = (struct UDP_ACK_Message *) buf;
1155 udp_ack->header.size = htons ((uint16_t) msize);
1156 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1157 udp_ack->delay = htonl (delay);
1158 udp_ack->sender = *rc->plugin->env->my_identity;
1159 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1160 (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
1165 * Closure for 'find_receive_context'.
1167 struct FindReceiveContext
1170 * Where to store the result.
1172 struct ReceiveContext *rc;
1177 const struct sockaddr *addr;
1180 * Number of bytes in 'addr'.
1184 struct Session *session;
1189 * Scan the heap for a receive context with the given address.
1191 * @param cls the 'struct FindReceiveContext'
1192 * @param node internal node of the heap
1193 * @param element value stored at the node (a 'struct ReceiveContext')
1194 * @param cost cost associated with the node
1195 * @return GNUNET_YES if we should continue to iterate,
1199 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1200 void *element, GNUNET_CONTAINER_HeapCostType cost)
1202 struct FindReceiveContext *frc = cls;
1203 struct ReceiveContext *e = element;
1205 if ((frc->addr_len == e->addr_len) &&
1206 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1216 * Read and process a message from the given socket.
1218 * @param plugin the overall plugin
1219 * @param rsock socket to read from
1222 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1228 const struct GNUNET_MessageHeader *msg;
1229 const struct GNUNET_MessageHeader *ack;
1230 struct Session *peer_session;
1231 const struct UDP_ACK_Message *udp_ack;
1232 struct ReceiveContext *rc;
1233 struct GNUNET_TIME_Absolute now;
1234 struct FindReceiveContext frc;
1235 struct Session *s = NULL;
1236 struct GNUNET_TIME_Relative flow_delay;
1238 fromlen = sizeof (addr);
1239 memset (&addr, 0, sizeof (addr));
1241 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1242 (struct sockaddr *) &addr, &fromlen);
1243 if (ret < sizeof (struct GNUNET_MessageHeader))
1245 GNUNET_break_op (0);
1248 msg = (const struct GNUNET_MessageHeader *) buf;
1250 LOG (GNUNET_ERROR_TYPE_DEBUG,
1251 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1252 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1254 if (ret != ntohs (msg->size))
1256 GNUNET_break_op (0);
1259 switch (ntohs (msg->type))
1261 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1262 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1264 GNUNET_break_op (0);
1267 process_udp_message (plugin, (const struct UDPMessage *) msg,
1268 (const struct sockaddr *) addr, fromlen);
1270 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1272 if (ntohs (msg->size) <
1273 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1275 GNUNET_break_op (0);
1278 udp_ack = (const struct UDP_ACK_Message *) msg;
1279 s = find_inbound_session (plugin, &udp_ack->sender, addr, fromlen);
1282 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1284 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1285 flow_delay.rel_value);
1287 s->flow_delay_from_other_peer =
1288 GNUNET_TIME_relative_to_absolute (flow_delay);
1290 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1291 if (ntohs (ack->size) !=
1292 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1294 GNUNET_break_op (0);
1298 LOG (GNUNET_ERROR_TYPE_DEBUG,
1299 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1300 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1301 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1304 peer_session = find_session (plugin, &udp_ack->sender);
1305 if (NULL == peer_session)
1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "Session for ACK not found, dropping ACK!\n");
1313 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1315 GNUNET_assert (GNUNET_OK ==
1316 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1320 plugin->last_expected_delay =
1321 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1322 if (peer_session->cont != NULL)
1323 peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
1324 GNUNET_free (peer_session);
1326 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1328 frc.addr = (const struct sockaddr *) addr;
1329 frc.addr_len = fromlen;
1330 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1332 now = GNUNET_TIME_absolute_get ();
1336 /* need to create a new RC */
1337 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1338 memcpy (&rc[1], addr, fromlen);
1339 rc->src_addr = (const struct sockaddr *) &rc[1];
1340 rc->addr_len = fromlen;
1341 rc->plugin = plugin;
1343 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1344 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1345 &fragment_msg_proc, &ack_proc);
1347 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1348 (GNUNET_CONTAINER_HeapCostType)
1352 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1353 (unsigned int) ntohs (msg->size),
1354 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1357 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1359 /* keep this 'rc' from expiring */
1360 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1361 (GNUNET_CONTAINER_HeapCostType)
1364 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1365 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1367 /* remove 'rc' that was inactive the longest */
1368 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1369 GNUNET_assert (NULL != rc);
1370 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1375 GNUNET_break_op (0);
1382 * We have been notified that our writeset has something to read. We don't
1383 * know which socket needs to be read, so we have to check each one
1384 * Then reschedule this function to be called again once more is available.
1386 * @param cls the plugin handle
1387 * @param tc the scheduling context (for rescheduling this function again)
1390 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1392 struct Plugin *plugin = cls;
1394 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1395 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1397 if ((NULL != plugin->sockv4) &&
1398 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1399 udp_read (plugin, plugin->sockv4);
1400 if ((NULL != plugin->sockv6) &&
1401 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1402 udp_read (plugin, plugin->sockv6);
1403 plugin->select_task =
1404 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1405 GNUNET_SCHEDULER_NO_TASK,
1406 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1407 NULL, &udp_plugin_select, plugin);
1414 struct Plugin *plugin;
1416 struct IPv4UdpAddress addr;
1420 udp_broadcast_mst_cb (void *cls, void *client,
1421 const struct GNUNET_MessageHeader *message)
1423 struct Plugin *plugin = cls;
1424 struct MstContext *mc = client;
1425 const struct GNUNET_MessageHeader *hello;
1426 struct UDP_Beacon_Message *msg;
1428 msg = (struct UDP_Beacon_Message *) message;
1430 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1431 ntohs (msg->header.type))
1434 LOG (GNUNET_ERROR_TYPE_ERROR,
1435 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1436 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1437 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1439 struct GNUNET_ATS_Information ats;
1441 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1442 ats.value = htonl (1);
1444 hello = (struct GNUNET_MessageHeader *) &msg[1];
1445 plugin->env->receive (plugin->env->cls, &msg->sender, hello, &ats, 1, NULL,
1446 (const char *) &mc->addr, sizeof (mc->addr));
1448 GNUNET_STATISTICS_update (plugin->env->stats,
1449 _("# HELLO beacons received via udp"), 1,
1455 * Read and process a message from the given socket.
1457 * @param plugin the overall plugin
1458 * @param rsock socket to read from
1461 udp_broadcast_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1467 struct MstContext *mc;
1470 fromlen = sizeof (addr);
1471 memset (&addr, 0, sizeof (addr));
1473 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1474 (struct sockaddr *) &addr, &fromlen);
1475 if (ret < sizeof (struct GNUNET_MessageHeader))
1477 /* malformed beacon, just throw it away */
1481 mc = GNUNET_malloc (sizeof (struct MstContext));
1483 struct sockaddr_in *av4 = (struct sockaddr_in *) &addr;
1485 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
1486 mc->addr.u4_port = av4->sin_port;
1489 GNUNET_SERVER_mst_receive (plugin->broadcast_mst, mc, buf, ret, GNUNET_NO,
1496 udp_broadcast_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1498 struct Plugin *plugin = cls;
1501 uint16_t hello_size;
1505 const struct GNUNET_MessageHeader *hello;
1506 struct UDP_Beacon_Message *msg;
1508 plugin->send_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1510 struct sockaddr_in baddr;
1512 baddr.sin_family = AF_INET;
1513 baddr.sin_port = htons (plugin->broadcast_port);
1514 baddr.sin_addr.s_addr = htonl (-1); /* send message to 255.255.255.255 */
1516 hello = plugin->env->get_our_hello ();
1517 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1518 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1520 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1521 (msg_size > (UDP_MTU)))
1524 msg = (struct UDP_Beacon_Message *) buf;
1525 msg->sender = *(plugin->env->my_identity);
1526 msg->header.size = ntohs (msg_size);
1527 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1528 memcpy (&msg[1], hello, hello_size);
1531 GNUNET_NETWORK_socket_sendto (plugin->sockv4_broadcast, msg, msg_size,
1532 (const struct sockaddr *) &baddr,
1533 sizeof (struct sockaddr_in));
1535 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sent HELLO beacon broadcast with %i bytes\n",
1538 plugin->send_broadcast_task =
1539 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1540 &udp_broadcast_send, plugin);
1545 * We have been notified that our writeset has something to read. We don't
1546 * know which socket needs to be read, so we have to check each one
1547 * Then reschedule this function to be called again once more is available.
1549 * @param cls the plugin handle
1550 * @param tc the scheduling context (for rescheduling this function again)
1553 udp_plugin_broadcast_select (void *cls,
1554 const struct GNUNET_SCHEDULER_TaskContext *tc)
1556 struct Plugin *plugin = cls;
1558 plugin->select_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1559 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1562 if ((NULL != plugin->sockv4_broadcast) &&
1563 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4_broadcast)))
1564 udp_broadcast_read (plugin, plugin->sockv4_broadcast);
1566 plugin->select_broadcast_task =
1567 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1568 GNUNET_SCHEDULER_NO_TASK,
1569 GNUNET_TIME_UNIT_FOREVER_REL,
1570 plugin->broadcast_rs, NULL,
1571 &udp_plugin_broadcast_select, plugin);
1577 * Check if the given port is plausible (must be either our listen
1578 * port or our advertised port). If it is neither, we return
1581 * @param plugin global variables
1582 * @param in_port port number to check
1583 * @return GNUNET_OK if port is either open_port or adv_port
1586 check_port (struct Plugin *plugin, uint16_t in_port)
1588 if ((in_port == plugin->port) || (in_port == plugin->aport))
1590 return GNUNET_SYSERR;
1595 * Function that will be called to check if a binary address for this
1596 * plugin is well-formed and corresponds to an address for THIS peer
1597 * (as per our configuration). Naturally, if absolutely necessary,
1598 * plugins can be a bit conservative in their answer, but in general
1599 * plugins should make sure that the address does not redirect
1600 * traffic to a 3rd party that might try to man-in-the-middle our
1603 * @param cls closure, should be our handle to the Plugin
1604 * @param addr pointer to the address
1605 * @param addrlen length of addr
1606 * @return GNUNET_OK if this is a plausible address for this peer
1607 * and transport, GNUNET_SYSERR if not
1611 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1613 struct Plugin *plugin = cls;
1614 struct IPv4UdpAddress *v4;
1615 struct IPv6UdpAddress *v6;
1617 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1618 (addrlen != sizeof (struct IPv6UdpAddress)))
1620 GNUNET_break_op (0);
1621 return GNUNET_SYSERR;
1623 if (addrlen == sizeof (struct IPv4UdpAddress))
1625 v4 = (struct IPv4UdpAddress *) addr;
1626 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1627 return GNUNET_SYSERR;
1629 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1630 sizeof (struct in_addr)))
1631 return GNUNET_SYSERR;
1635 v6 = (struct IPv6UdpAddress *) addr;
1636 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1638 GNUNET_break_op (0);
1639 return GNUNET_SYSERR;
1641 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1642 return GNUNET_SYSERR;
1644 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1645 sizeof (struct in6_addr)))
1646 return GNUNET_SYSERR;
1653 * Function called for a quick conversion of the binary address to
1654 * a numeric address. Note that the caller must not free the
1655 * address and that the next call to this function is allowed
1656 * to override the address again.
1658 * @param cls closure
1659 * @param addr binary address
1660 * @param addrlen length of the address
1661 * @return string representing the same address
1664 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1666 static char rbuf[INET6_ADDRSTRLEN + 10];
1667 char buf[INET6_ADDRSTRLEN];
1671 const struct IPv4UdpAddress *t4;
1672 const struct IPv6UdpAddress *t6;
1676 if (addrlen == sizeof (struct IPv6UdpAddress))
1680 port = ntohs (t6->u6_port);
1681 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1684 else if (addrlen == sizeof (struct IPv4UdpAddress))
1688 port = ntohs (t4->u4_port);
1689 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1694 GNUNET_break_op (0);
1697 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1698 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1705 * Closure for 'append_port'.
1707 struct PrettyPrinterContext
1710 * Function to call with the result.
1712 GNUNET_TRANSPORT_AddressStringCallback asc;
1715 * Clsoure for 'asc'.
1720 * Port to add after the IP address.
1727 * Append our port and forward the result.
1729 * @param cls a 'struct PrettyPrinterContext'
1730 * @param hostname result from DNS resolver
1733 append_port (void *cls, const char *hostname)
1735 struct PrettyPrinterContext *ppc = cls;
1738 if (hostname == NULL)
1740 ppc->asc (ppc->asc_cls, NULL);
1744 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1745 ppc->asc (ppc->asc_cls, ret);
1751 * Convert the transports address to a nice, human-readable
1754 * @param cls closure
1755 * @param type name of the transport that generated the address
1756 * @param addr one of the addresses of the host, NULL for the last address
1757 * the specific address format depends on the transport
1758 * @param addrlen length of the address
1759 * @param numeric should (IP) addresses be displayed in numeric form?
1760 * @param timeout after how long should we give up?
1761 * @param asc function to call on each string
1762 * @param asc_cls closure for asc
1765 udp_plugin_address_pretty_printer (void *cls, const char *type,
1766 const void *addr, size_t addrlen,
1768 struct GNUNET_TIME_Relative timeout,
1769 GNUNET_TRANSPORT_AddressStringCallback asc,
1772 struct PrettyPrinterContext *ppc;
1775 struct sockaddr_in a4;
1776 struct sockaddr_in6 a6;
1777 const struct IPv4UdpAddress *u4;
1778 const struct IPv6UdpAddress *u6;
1781 if (addrlen == sizeof (struct IPv6UdpAddress))
1784 memset (&a6, 0, sizeof (a6));
1785 a6.sin6_family = AF_INET6;
1786 #if HAVE_SOCKADDR_IN_SIN_LEN
1787 a6.sin6_len = sizeof (a6);
1789 a6.sin6_port = u6->u6_port;
1790 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1791 port = ntohs (u6->u6_port);
1795 else if (addrlen == sizeof (struct IPv4UdpAddress))
1798 memset (&a4, 0, sizeof (a4));
1799 a4.sin_family = AF_INET;
1800 #if HAVE_SOCKADDR_IN_SIN_LEN
1801 a4.sin_len = sizeof (a4);
1803 a4.sin_port = u4->u4_port;
1804 a4.sin_addr.s_addr = u4->ipv4_addr;
1805 port = ntohs (u4->u4_port);
1811 /* invalid address */
1812 GNUNET_break_op (0);
1813 asc (asc_cls, NULL);
1816 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1818 ppc->asc_cls = asc_cls;
1820 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1825 * Our external IP address/port mapping has changed.
1827 * @param cls closure, the 'struct LocalAddrList'
1828 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1829 * the previous (now invalid) one
1830 * @param addr either the previous or the new public IP address
1831 * @param addrlen actual lenght of the address
1834 udp_nat_port_map_callback (void *cls, int add_remove,
1835 const struct sockaddr *addr, socklen_t addrlen)
1837 struct Plugin *plugin = cls;
1838 struct IPv4UdpAddress u4;
1839 struct IPv6UdpAddress u6;
1843 /* convert 'addr' to our internal format */
1844 switch (addr->sa_family)
1847 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1848 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1849 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1854 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1855 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1856 sizeof (struct in6_addr));
1857 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1865 /* modify our published address list */
1866 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1871 * The exported method. Makes the core api available via a global and
1872 * returns the udp transport API.
1874 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1875 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1878 libgnunet_plugin_transport_udp_init (void *cls)
1880 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1881 unsigned long long port;
1882 unsigned long long aport;
1883 unsigned long long bport;
1884 struct GNUNET_TRANSPORT_PluginFunctions *api;
1885 struct Plugin *plugin;
1886 int sockets_created;
1888 struct GNUNET_TIME_Relative interval;
1889 struct sockaddr_in serverAddrv4;
1890 struct sockaddr_in6 serverAddrv6;
1891 struct sockaddr *serverAddr;
1892 struct sockaddr *addrs[2];
1893 socklen_t addrlens[2];
1896 unsigned long long udp_max_bps;
1899 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1904 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
1906 if (broadcast == GNUNET_SYSERR)
1907 broadcast = GNUNET_NO;
1909 if (GNUNET_SYSERR ==
1910 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
1911 "BROADCAST_INTERVAL", &interval))
1912 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1915 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1916 "BROADCAST_PORT", &bport))
1920 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1921 "MAX_BPS", &udp_max_bps))
1922 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1924 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1925 "ADVERTISED_PORT", &aport))
1929 LOG (GNUNET_ERROR_TYPE_WARNING,
1930 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
1934 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1935 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1937 plugin = GNUNET_malloc (sizeof (struct Plugin));
1938 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1939 GNUNET_BANDWIDTH_value_init ((uint32_t)
1941 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1942 plugin->port = port;
1943 plugin->aport = aport;
1944 plugin->broadcast_port = bport;
1945 plugin->broadcast = broadcast;
1947 plugin->broadcast_interval = interval;
1948 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1951 api->send = &udp_plugin_send;
1952 api->disconnect = &udp_disconnect;
1953 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1954 api->address_to_string = &udp_address_to_string;
1955 api->check_address = &udp_plugin_check_address;
1958 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1959 "BINDTO", &plugin->bind4_address))
1961 LOG (GNUNET_ERROR_TYPE_DEBUG,
1962 "Binding udp plugin to specific address: `%s'\n",
1963 plugin->bind4_address);
1964 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
1966 GNUNET_free (plugin->bind4_address);
1967 GNUNET_free (plugin);
1974 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1975 "BINDTO6", &plugin->bind6_address))
1977 LOG (GNUNET_ERROR_TYPE_DEBUG,
1978 "Binding udp plugin to specific address: `%s'\n",
1979 plugin->bind6_address);
1981 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1983 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1984 plugin->bind6_address);
1985 GNUNET_free_non_null (plugin->bind4_address);
1986 GNUNET_free (plugin->bind6_address);
1987 GNUNET_free (plugin);
1994 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1996 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1998 plugin->inbound_sessions =
1999 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2001 sockets_created = 0;
2003 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
2006 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2007 if (NULL == plugin->sockv6)
2009 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2013 #if HAVE_SOCKADDR_IN_SIN_LEN
2014 serverAddrv6.sin6_len = sizeof (serverAddrv6);
2016 serverAddrv6.sin6_family = AF_INET6;
2017 serverAddrv6.sin6_addr = in6addr_any;
2018 serverAddrv6.sin6_port = htons (plugin->port);
2019 addrlen = sizeof (serverAddrv6);
2020 serverAddr = (struct sockaddr *) &serverAddrv6;
2022 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2023 ntohs (serverAddrv6.sin6_port));
2026 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2029 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2031 LOG (GNUNET_ERROR_TYPE_DEBUG,
2032 "IPv6 Binding failed, trying new port %d\n",
2033 ntohs (serverAddrv6.sin6_port));
2038 GNUNET_NETWORK_socket_close (plugin->sockv6);
2039 plugin->sockv6 = NULL;
2043 if (plugin->sockv6 != NULL)
2045 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2046 addrlens[sockets_created] = sizeof (serverAddrv6);
2053 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2054 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2055 if (NULL == plugin->sockv4)
2057 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2061 #if HAVE_SOCKADDR_IN_SIN_LEN
2062 serverAddrv4.sin_len = sizeof (serverAddrv4);
2064 serverAddrv4.sin_family = AF_INET;
2065 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2066 serverAddrv4.sin_port = htons (plugin->port);
2067 addrlen = sizeof (serverAddrv4);
2068 serverAddr = (struct sockaddr *) &serverAddrv4;
2070 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2071 ntohs (serverAddrv4.sin_port));
2074 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2077 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2079 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2080 ntohs (serverAddrv4.sin_port));
2085 GNUNET_NETWORK_socket_close (plugin->sockv4);
2086 plugin->sockv4 = NULL;
2090 if (plugin->sockv4 != NULL)
2092 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2093 addrlens[sockets_created] = sizeof (serverAddrv4);
2098 plugin->rs = GNUNET_NETWORK_fdset_create ();
2099 GNUNET_NETWORK_fdset_zero (plugin->rs);
2100 if (NULL != plugin->sockv4)
2101 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
2102 if (NULL != plugin->sockv6)
2103 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
2105 plugin->select_task =
2106 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2107 GNUNET_SCHEDULER_NO_TASK,
2108 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
2109 NULL, &udp_plugin_select, plugin);
2112 /* create broadcast socket */
2115 plugin->sockv4_broadcast =
2116 GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2117 if (NULL == plugin->sockv4_broadcast)
2119 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2123 #if HAVE_SOCKADDR_IN_SIN_LEN
2124 serverAddrv4.sin_len = sizeof (serverAddrv4);
2126 serverAddrv4.sin_family = AF_INET;
2127 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2128 serverAddrv4.sin_port = htons (plugin->broadcast_port);
2129 addrlen = sizeof (serverAddrv4);
2130 serverAddr = (struct sockaddr *) &serverAddrv4;
2133 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding Broadcast to IPv4 port %d\n",
2134 ntohs (serverAddrv4.sin_port));
2136 if (GNUNET_NETWORK_socket_bind
2137 (plugin->sockv4_broadcast, serverAddr, addrlen) != GNUNET_OK)
2139 LOG (GNUNET_ERROR_TYPE_WARNING,
2140 _("Failed to create IPv4 broadcast socket on port %d\n"),
2141 ntohs (serverAddrv4.sin_port));
2142 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast);
2143 plugin->sockv4_broadcast = NULL;
2145 if (plugin->sockv4_broadcast != NULL)
2149 if (GNUNET_NETWORK_socket_setsockopt
2150 (plugin->sockv4_broadcast, SOL_SOCKET, SO_BROADCAST, &yes,
2151 sizeof (int)) != GNUNET_OK)
2153 LOG (GNUNET_ERROR_TYPE_WARNING,
2155 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
2156 ntohs (serverAddrv4.sin_port));
2157 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast);
2158 plugin->sockv4_broadcast = NULL;
2162 plugin->broadcast_rs = GNUNET_NETWORK_fdset_create ();
2163 GNUNET_NETWORK_fdset_set (plugin->broadcast_rs,
2164 plugin->sockv4_broadcast);
2169 if (plugin->sockv4_broadcast != NULL)
2171 plugin->broadcast = GNUNET_YES;
2172 plugin->broadcast_mst =
2173 GNUNET_SERVER_mst_create (udp_broadcast_mst_cb, plugin);
2174 GNUNET_STATISTICS_update (plugin->env->stats,
2175 _("# HELLO beacons received via udp"), 1,
2177 plugin->select_broadcast_task =
2178 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2179 GNUNET_SCHEDULER_NO_TASK,
2180 GNUNET_TIME_UNIT_FOREVER_REL,
2181 plugin->broadcast_rs, NULL,
2182 &udp_plugin_broadcast_select, plugin);
2184 plugin->send_broadcast_task =
2185 GNUNET_SCHEDULER_add_now (&udp_broadcast_send, plugin);
2188 plugin->broadcast = GNUNET_NO;
2191 if (sockets_created == 0)
2192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2194 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
2195 (const struct sockaddr **) addrs, addrlens,
2196 &udp_nat_port_map_callback, NULL, plugin);
2201 * Shutdown the plugin.
2203 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
2207 libgnunet_plugin_transport_udp_done (void *cls)
2209 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2210 struct Plugin *plugin = api->cls;
2211 struct ReceiveContext *rc;
2213 /* FIXME: clean up heap and hashmap */
2214 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
2216 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2217 plugin->sessions = NULL;
2218 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
2219 &destroy_inbound_session, NULL);
2220 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
2221 plugin->inbound_sessions = NULL;
2222 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
2224 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2227 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
2229 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2231 GNUNET_SCHEDULER_cancel (plugin->select_task);
2232 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2234 if (plugin->sockv4 != NULL)
2236 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2237 plugin->sockv4 = NULL;
2239 if (plugin->sockv6 != NULL)
2241 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2242 plugin->sockv6 = NULL;
2245 if (plugin->broadcast)
2247 if (plugin->select_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2249 GNUNET_SCHEDULER_cancel (plugin->select_broadcast_task);
2250 plugin->select_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2252 if (plugin->send_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2254 GNUNET_SCHEDULER_cancel (plugin->send_broadcast_task);
2255 plugin->send_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2257 if (plugin->broadcast_mst != NULL)
2258 GNUNET_SERVER_mst_destroy (plugin->broadcast_mst);
2259 if (plugin->sockv4_broadcast != NULL)
2261 GNUNET_break (GNUNET_OK ==
2262 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast));
2263 plugin->sockv4_broadcast = NULL;
2265 GNUNET_NETWORK_fdset_destroy (plugin->broadcast_rs);
2268 GNUNET_SERVER_mst_destroy (plugin->mst);
2269 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2271 GNUNET_NAT_unregister (plugin->nat);
2273 GNUNET_free (plugin);
2278 /* end of plugin_transport_udp.c */