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);
543 GNUNET_CONTAINER_multihashmap_remove(s->plugin->inbound_sessions, &s->target.hashPubKey, s);
549 * Disconnect from a remote node. Clean up session if we have one for this peer
551 * @param cls closure for this call (should be handle to Plugin)
552 * @param target the peeridentity of the peer to disconnect
553 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
556 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
558 struct Plugin *plugin = cls;
559 struct Session *session;
561 session = find_session (plugin, target);
564 GNUNET_assert (GNUNET_OK ==
565 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
569 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
571 &destroy_inbound_session, NULL);
572 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
575 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
576 if (GNUNET_SCHEDULER_NO_TASK != session->delayed_cont_task)
577 GNUNET_SCHEDULER_cancel (session->delayed_cont_task);
578 if (session->cont != NULL)
579 session->cont (session->cont_cls, target, GNUNET_SYSERR);
580 GNUNET_free (session);
585 * Actually send out the message.
587 * @param plugin the plugin
588 * @param sa the address to send the message to
589 * @param msg message to transmit
590 * @return the number of bytes written
593 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
594 const struct GNUNET_MessageHeader *msg)
599 switch (sa->sa_family)
602 if (NULL == plugin->sockv4)
605 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
606 sa, slen = sizeof (struct sockaddr_in));
609 if (NULL == plugin->sockv6)
612 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
613 sa, slen = sizeof (struct sockaddr_in6));
619 if (GNUNET_SYSERR == sent)
621 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
622 LOG (GNUNET_ERROR_TYPE_ERROR,
623 "UDP transmited %u-byte message to %s (%d: %s)\n",
624 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
625 (sent < 0) ? STRERROR (errno) : "ok");
628 LOG (GNUNET_ERROR_TYPE_DEBUG,
629 "UDP transmited %u-byte message to %s (%d: %s)\n",
630 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen), (int) sent,
631 (sent < 0) ? STRERROR (errno) : "ok");
637 * Function that is called with messages created by the fragmentation
638 * module. In the case of the 'proc' callback of the
639 * GNUNET_FRAGMENT_context_create function, this function must
640 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
642 * @param cls closure, the 'struct PeerSession'
643 * @param msg the message that was created
646 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
648 struct Session *session = cls;
650 udp_send (session->plugin, session->sock_addr, msg);
651 GNUNET_FRAGMENT_context_transmission_done (session->frag);
655 static struct Session *
656 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
657 const void *addr, size_t addrlen,
658 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
660 struct Session *peer_session;
661 const struct IPv4UdpAddress *t4;
662 const struct IPv6UdpAddress *t6;
663 struct sockaddr_in *v4;
664 struct sockaddr_in6 *v6;
669 case sizeof (struct IPv4UdpAddress):
670 if (NULL == plugin->sockv4)
676 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
677 len = sizeof (struct sockaddr_in);
678 v4 = (struct sockaddr_in *) &peer_session[1];
679 v4->sin_family = AF_INET;
680 #if HAVE_SOCKADDR_IN_SIN_LEN
681 v4->sin_len = sizeof (struct sockaddr_in);
683 v4->sin_port = t4->u4_port;
684 v4->sin_addr.s_addr = t4->ipv4_addr;
686 case sizeof (struct IPv6UdpAddress):
687 if (NULL == plugin->sockv6)
693 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
694 len = sizeof (struct sockaddr_in6);
695 v6 = (struct sockaddr_in6 *) &peer_session[1];
696 v6->sin6_family = AF_INET6;
697 #if HAVE_SOCKADDR_IN_SIN_LEN
698 v6->sin6_len = sizeof (struct sockaddr_in6);
700 v6->sin6_port = t6->u6_port;
701 v6->sin6_addr = t6->ipv6_addr;
704 /* Must have a valid address to send to */
709 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
710 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
711 peer_session->addrlen = len;
712 peer_session->target = *target;
713 peer_session->plugin = plugin;
714 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
715 peer_session->cont = cont;
716 peer_session->cont_cls = cont_cls;
722 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
726 udp_call_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
728 struct Session *s = cls;
729 GNUNET_TRANSPORT_TransmitContinuation cont = s->cont;
731 s->delayed_cont_task = GNUNET_SCHEDULER_NO_TASK;
733 cont (s->cont_cls, &s->target, GNUNET_OK);
738 * Function that can be used by the transport service to transmit
739 * a message using the plugin.
742 * @param target who should receive this message (ignored by UDP)
743 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
744 * @param msgbuf_size the size of the msgbuf to send
745 * @param priority how important is the message (ignored by UDP)
746 * @param timeout when should we time out (give up) if we can not transmit?
747 * @param session identifier used for this session (NULL for UDP)
748 * @param addr the addr to send the message to
749 * @param addrlen the len of addr
750 * @param force_address not used, we had better have an address to send to
751 * because we are stateless!!
752 * @param cont continuation to call once the message has
753 * been transmitted (or if the transport is ready
754 * for the next transmission call; or if the
755 * peer disconnected...)
756 * @param cont_cls closure for cont
758 * @return the number of bytes written (may return 0 and the message can
759 * still be transmitted later!)
762 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
763 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
764 struct GNUNET_TIME_Relative timeout, struct Session *session,
765 const void *addr, size_t addrlen, int force_address,
766 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
768 struct Plugin *plugin = cls;
769 struct Session *peer_session;
771 const struct IPv4UdpAddress *t4;
772 const struct IPv6UdpAddress *t6;
773 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
775 struct UDPMessage *udp;
776 struct GNUNET_TIME_Relative delta;
778 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
781 return GNUNET_SYSERR;
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
786 msgbuf_size, GNUNET_i2s (target), udp_address_to_string (NULL, addr,
788 session, force_address);
790 if ((force_address == GNUNET_SYSERR) && (session == NULL))
791 return GNUNET_SYSERR;
794 /* safety check: comparing address to address stored in session */
795 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
798 GNUNET_assert (GNUNET_YES ==
799 GNUNET_CONTAINER_multihashmap_contains_value
800 (plugin->inbound_sessions, &target->hashPubKey, s));
802 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
803 return GNUNET_SYSERR;
806 case sizeof (struct IPv4UdpAddress):
807 if (NULL == plugin->sockv4)
810 cont (cont_cls, target, GNUNET_SYSERR);
811 return GNUNET_SYSERR;
814 if (s->addrlen != (sizeof (struct sockaddr_in)))
815 return GNUNET_SYSERR;
816 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
818 GNUNET_assert (a4->sin_port == t4->u4_port);
820 memcmp (&a4->sin_addr, &t4->ipv4_addr,
821 sizeof (struct in_addr)));
822 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
825 case sizeof (struct IPv6UdpAddress):
826 if (NULL == plugin->sockv6)
829 cont (cont_cls, target, GNUNET_SYSERR);
830 return GNUNET_SYSERR;
833 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
834 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
836 GNUNET_assert (a6->sin6_port == t6->u6_port);
838 memcmp (&a6->sin6_addr, &t6->ipv6_addr,
839 sizeof (struct in6_addr)));
840 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session 0x%X successfully checked!\n",
844 /* Must have a valid address to send to */
849 if ((addr == NULL) || (addrlen == 0))
850 return GNUNET_SYSERR;
851 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
852 if (peer_session == NULL)
855 cont (cont_cls, target, GNUNET_SYSERR);
856 return GNUNET_SYSERR;;
860 udp = (struct UDPMessage *) mbuf;
861 udp->header.size = htons (mlen);
862 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
863 udp->reserved = htonl (0);
864 udp->sender = *plugin->env->my_identity;
865 memcpy (&udp[1], msgbuf, msgbuf_size);
868 delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
870 delta = GNUNET_TIME_UNIT_ZERO;
873 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
876 if ((delta.rel_value > 0) && (mlen > 0))
879 s->cont_cls = cont_cls;
880 s->delayed_cont_task =
881 GNUNET_SCHEDULER_add_delayed (delta, &udp_call_continuation, s);
884 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
886 GNUNET_free_non_null (peer_session);
890 GNUNET_assert (GNUNET_OK ==
891 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
894 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
896 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
898 plugin->last_expected_delay,
899 &udp->header, &send_fragment,
907 * Closure for 'process_inbound_tokenized_messages'
909 struct SourceInformation
914 struct GNUNET_PeerIdentity sender;
922 * Number of bytes in source address.
926 struct Session *session;
931 * Message tokenizer has broken up an incomming message. Pass it on
934 * @param cls the 'struct Plugin'
935 * @param client the 'struct SourceInformation'
936 * @param hdr the actual message
939 process_inbound_tokenized_messages (void *cls, void *client,
940 const struct GNUNET_MessageHeader *hdr)
942 struct Plugin *plugin = cls;
943 struct SourceInformation *si = client;
944 struct GNUNET_ATS_Information distance;
945 struct GNUNET_TIME_Relative delay;
948 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
949 distance.value = htonl (1);
951 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving Session %X %s to transport\n",
952 si->session, GNUNET_i2s (&si->session->target));
954 plugin->env->receive (plugin->env->cls, &si->sender, hdr, &distance, 1,
955 si->session, si->arg, si->args);
956 si->session->flow_delay_for_other_peer = delay;
960 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
962 struct Session *s = cls;
964 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
965 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %X (`%s') is now invalid\n", s,
966 GNUNET_a2s (s->sock_addr, s->addrlen));
968 s->plugin->env->session_end (s->plugin->env->cls, &s->target, s);
969 GNUNET_assert (GNUNET_YES ==
970 GNUNET_CONTAINER_multihashmap_remove (s->
971 plugin->inbound_sessions,
972 &s->target.hashPubKey,
979 * We've received a UDP Message. Process it (pass contents to main service).
981 * @param plugin plugin context
982 * @param msg the message
983 * @param sender_addr sender address
984 * @param sender_addr_len number of bytes in sender_addr
987 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
988 const struct sockaddr *sender_addr,
989 socklen_t sender_addr_len)
991 struct SourceInformation si;
992 struct IPv4UdpAddress u4;
993 struct IPv6UdpAddress u6;
997 if (0 != ntohl (msg->reserved))
1002 if (ntohs (msg->header.size) <
1003 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1005 GNUNET_break_op (0);
1009 /* convert address */
1010 switch (sender_addr->sa_family)
1013 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1014 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1015 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1020 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1021 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1022 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1031 LOG (GNUNET_ERROR_TYPE_DEBUG,
1032 "Received message with %u bytes from peer `%s' at `%s'\n",
1033 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1034 GNUNET_a2s (sender_addr, sender_addr_len));
1037 /* create a session for inbound connections */
1038 const struct UDPMessage *udp_msg = (const struct UDPMessage *) msg;
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
1042 GNUNET_i2s (&udp_msg->sender), udp_address_to_string (NULL, arg, args));
1044 struct Session *s = NULL;
1046 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr,
1051 LOG (GNUNET_ERROR_TYPE_DEBUG,
1052 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
1053 s, GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1057 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
1058 LOG (GNUNET_ERROR_TYPE_DEBUG,
1059 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n", s,
1060 GNUNET_i2s (&s->target), udp_address_to_string (NULL, arg, args));
1062 GNUNET_assert (GNUNET_OK ==
1063 GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
1064 &s->target.hashPubKey, s,
1065 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1068 GNUNET_TIME_relative_to_absolute
1069 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1070 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
1072 GNUNET_SCHEDULER_cancel (s->invalidation_task);
1073 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1074 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rescheduling %X' `%s'\n", s,
1075 udp_address_to_string (NULL, arg, args));
1077 s->invalidation_task =
1078 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1079 &invalidation_task, s);
1080 /* iterate over all embedded messages */
1081 si.sender = msg->sender;
1085 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1086 ntohs (msg->header.size) -
1087 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1092 * Process a defragmented message.
1094 * @param cls the 'struct ReceiveContext'
1095 * @param msg the message
1098 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1100 struct ReceiveContext *rc = cls;
1102 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1107 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1112 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1113 rc->src_addr, rc->addr_len);
1118 * Transmit an acknowledgement.
1120 * @param cls the 'struct ReceiveContext'
1121 * @param id message ID (unused)
1122 * @param msg ack to transmit
1125 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1127 struct ReceiveContext *rc = cls;
1129 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1131 struct UDP_ACK_Message *udp_ack;
1136 s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
1139 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1140 delay = s->flow_delay_for_other_peer.rel_value;
1147 LOG (GNUNET_ERROR_TYPE_DEBUG,
1148 "Sending ACK to `%s' including delay of %u ms\n",
1149 GNUNET_a2s (rc->src_addr,
1150 (rc->src_addr->sa_family ==
1151 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1155 udp_ack = (struct UDP_ACK_Message *) buf;
1156 udp_ack->header.size = htons ((uint16_t) msize);
1157 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1158 udp_ack->delay = htonl (delay);
1159 udp_ack->sender = *rc->plugin->env->my_identity;
1160 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1161 (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
1166 * Closure for 'find_receive_context'.
1168 struct FindReceiveContext
1171 * Where to store the result.
1173 struct ReceiveContext *rc;
1178 const struct sockaddr *addr;
1181 * Number of bytes in 'addr'.
1185 struct Session *session;
1190 * Scan the heap for a receive context with the given address.
1192 * @param cls the 'struct FindReceiveContext'
1193 * @param node internal node of the heap
1194 * @param element value stored at the node (a 'struct ReceiveContext')
1195 * @param cost cost associated with the node
1196 * @return GNUNET_YES if we should continue to iterate,
1200 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1201 void *element, GNUNET_CONTAINER_HeapCostType cost)
1203 struct FindReceiveContext *frc = cls;
1204 struct ReceiveContext *e = element;
1206 if ((frc->addr_len == e->addr_len) &&
1207 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1217 * Read and process a message from the given socket.
1219 * @param plugin the overall plugin
1220 * @param rsock socket to read from
1223 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1229 const struct GNUNET_MessageHeader *msg;
1230 const struct GNUNET_MessageHeader *ack;
1231 struct Session *peer_session;
1232 const struct UDP_ACK_Message *udp_ack;
1233 struct ReceiveContext *rc;
1234 struct GNUNET_TIME_Absolute now;
1235 struct FindReceiveContext frc;
1236 struct Session *s = NULL;
1237 struct GNUNET_TIME_Relative flow_delay;
1239 fromlen = sizeof (addr);
1240 memset (&addr, 0, sizeof (addr));
1242 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1243 (struct sockaddr *) &addr, &fromlen);
1244 if (ret < sizeof (struct GNUNET_MessageHeader))
1246 GNUNET_break_op (0);
1249 msg = (const struct GNUNET_MessageHeader *) buf;
1251 LOG (GNUNET_ERROR_TYPE_DEBUG,
1252 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1253 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1255 if (ret != ntohs (msg->size))
1257 GNUNET_break_op (0);
1260 switch (ntohs (msg->type))
1262 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1263 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1265 GNUNET_break_op (0);
1268 process_udp_message (plugin, (const struct UDPMessage *) msg,
1269 (const struct sockaddr *) addr, fromlen);
1271 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1273 if (ntohs (msg->size) <
1274 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1276 GNUNET_break_op (0);
1279 udp_ack = (const struct UDP_ACK_Message *) msg;
1280 s = find_inbound_session (plugin, &udp_ack->sender, addr, fromlen);
1283 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1286 flow_delay.rel_value);
1288 s->flow_delay_from_other_peer =
1289 GNUNET_TIME_relative_to_absolute (flow_delay);
1291 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1292 if (ntohs (ack->size) !=
1293 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1295 GNUNET_break_op (0);
1299 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1301 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1302 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1305 peer_session = find_session (plugin, &udp_ack->sender);
1306 if (NULL == peer_session)
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 "Session for ACK not found, dropping ACK!\n");
1314 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1316 GNUNET_assert (GNUNET_OK ==
1317 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1321 plugin->last_expected_delay =
1322 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1323 if (peer_session->cont != NULL)
1324 peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
1325 GNUNET_free (peer_session);
1327 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1329 frc.addr = (const struct sockaddr *) addr;
1330 frc.addr_len = fromlen;
1331 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1333 now = GNUNET_TIME_absolute_get ();
1337 /* need to create a new RC */
1338 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1339 memcpy (&rc[1], addr, fromlen);
1340 rc->src_addr = (const struct sockaddr *) &rc[1];
1341 rc->addr_len = fromlen;
1342 rc->plugin = plugin;
1344 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1345 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1346 &fragment_msg_proc, &ack_proc);
1348 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1349 (GNUNET_CONTAINER_HeapCostType)
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1354 (unsigned int) ntohs (msg->size),
1355 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1358 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1360 /* keep this 'rc' from expiring */
1361 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1362 (GNUNET_CONTAINER_HeapCostType)
1365 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1366 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1368 /* remove 'rc' that was inactive the longest */
1369 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1370 GNUNET_assert (NULL != rc);
1371 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1376 GNUNET_break_op (0);
1383 * We have been notified that our writeset has something to read. We don't
1384 * know which socket needs to be read, so we have to check each one
1385 * Then reschedule this function to be called again once more is available.
1387 * @param cls the plugin handle
1388 * @param tc the scheduling context (for rescheduling this function again)
1391 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1393 struct Plugin *plugin = cls;
1395 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1396 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1398 if ((NULL != plugin->sockv4) &&
1399 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1400 udp_read (plugin, plugin->sockv4);
1401 if ((NULL != plugin->sockv6) &&
1402 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1403 udp_read (plugin, plugin->sockv6);
1404 plugin->select_task =
1405 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1406 GNUNET_SCHEDULER_NO_TASK,
1407 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1408 NULL, &udp_plugin_select, plugin);
1415 struct Plugin *plugin;
1417 struct IPv4UdpAddress addr;
1421 udp_broadcast_mst_cb (void *cls, void *client,
1422 const struct GNUNET_MessageHeader *message)
1424 struct Plugin *plugin = cls;
1425 struct MstContext *mc = client;
1426 const struct GNUNET_MessageHeader *hello;
1427 struct UDP_Beacon_Message *msg;
1429 msg = (struct UDP_Beacon_Message *) message;
1431 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
1432 ntohs (msg->header.type))
1435 LOG (GNUNET_ERROR_TYPE_ERROR,
1436 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
1437 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1438 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
1440 struct GNUNET_ATS_Information ats;
1442 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1443 ats.value = htonl (1);
1445 hello = (struct GNUNET_MessageHeader *) &msg[1];
1446 plugin->env->receive (plugin->env->cls, &msg->sender, hello, &ats, 1, NULL,
1447 (const char *) &mc->addr, sizeof (mc->addr));
1449 GNUNET_STATISTICS_update (plugin->env->stats,
1450 _("# HELLO beacons received via udp"), 1,
1456 * Read and process a message from the given socket.
1458 * @param plugin the overall plugin
1459 * @param rsock socket to read from
1462 udp_broadcast_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1468 struct MstContext *mc;
1471 fromlen = sizeof (addr);
1472 memset (&addr, 0, sizeof (addr));
1474 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1475 (struct sockaddr *) &addr, &fromlen);
1476 if (ret < sizeof (struct GNUNET_MessageHeader))
1478 /* malformed beacon, just throw it away */
1482 mc = GNUNET_malloc (sizeof (struct MstContext));
1484 struct sockaddr_in *av4 = (struct sockaddr_in *) &addr;
1486 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
1487 mc->addr.u4_port = av4->sin_port;
1490 GNUNET_SERVER_mst_receive (plugin->broadcast_mst, mc, buf, ret, GNUNET_NO,
1497 udp_broadcast_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1499 struct Plugin *plugin = cls;
1502 uint16_t hello_size;
1506 const struct GNUNET_MessageHeader *hello;
1507 struct UDP_Beacon_Message *msg;
1509 plugin->send_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1511 struct sockaddr_in baddr;
1513 baddr.sin_family = AF_INET;
1514 baddr.sin_port = htons (plugin->broadcast_port);
1515 baddr.sin_addr.s_addr = htonl (-1); /* send message to 255.255.255.255 */
1517 hello = plugin->env->get_our_hello ();
1518 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1519 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
1521 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
1522 (msg_size > (UDP_MTU)))
1525 msg = (struct UDP_Beacon_Message *) buf;
1526 msg->sender = *(plugin->env->my_identity);
1527 msg->header.size = ntohs (msg_size);
1528 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
1529 memcpy (&msg[1], hello, hello_size);
1532 GNUNET_NETWORK_socket_sendto (plugin->sockv4_broadcast, msg, msg_size,
1533 (const struct sockaddr *) &baddr,
1534 sizeof (struct sockaddr_in));
1536 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sent HELLO beacon broadcast with %i bytes\n",
1539 plugin->send_broadcast_task =
1540 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
1541 &udp_broadcast_send, plugin);
1546 * We have been notified that our writeset has something to read. We don't
1547 * know which socket needs to be read, so we have to check each one
1548 * Then reschedule this function to be called again once more is available.
1550 * @param cls the plugin handle
1551 * @param tc the scheduling context (for rescheduling this function again)
1554 udp_plugin_broadcast_select (void *cls,
1555 const struct GNUNET_SCHEDULER_TaskContext *tc)
1557 struct Plugin *plugin = cls;
1559 plugin->select_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
1560 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1563 if ((NULL != plugin->sockv4_broadcast) &&
1564 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4_broadcast)))
1565 udp_broadcast_read (plugin, plugin->sockv4_broadcast);
1567 plugin->select_broadcast_task =
1568 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1569 GNUNET_SCHEDULER_NO_TASK,
1570 GNUNET_TIME_UNIT_FOREVER_REL,
1571 plugin->broadcast_rs, NULL,
1572 &udp_plugin_broadcast_select, plugin);
1578 * Check if the given port is plausible (must be either our listen
1579 * port or our advertised port). If it is neither, we return
1582 * @param plugin global variables
1583 * @param in_port port number to check
1584 * @return GNUNET_OK if port is either open_port or adv_port
1587 check_port (struct Plugin *plugin, uint16_t in_port)
1589 if ((in_port == plugin->port) || (in_port == plugin->aport))
1591 return GNUNET_SYSERR;
1596 * Function that will be called to check if a binary address for this
1597 * plugin is well-formed and corresponds to an address for THIS peer
1598 * (as per our configuration). Naturally, if absolutely necessary,
1599 * plugins can be a bit conservative in their answer, but in general
1600 * plugins should make sure that the address does not redirect
1601 * traffic to a 3rd party that might try to man-in-the-middle our
1604 * @param cls closure, should be our handle to the Plugin
1605 * @param addr pointer to the address
1606 * @param addrlen length of addr
1607 * @return GNUNET_OK if this is a plausible address for this peer
1608 * and transport, GNUNET_SYSERR if not
1612 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1614 struct Plugin *plugin = cls;
1615 struct IPv4UdpAddress *v4;
1616 struct IPv6UdpAddress *v6;
1618 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1619 (addrlen != sizeof (struct IPv6UdpAddress)))
1621 GNUNET_break_op (0);
1622 return GNUNET_SYSERR;
1624 if (addrlen == sizeof (struct IPv4UdpAddress))
1626 v4 = (struct IPv4UdpAddress *) addr;
1627 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1628 return GNUNET_SYSERR;
1630 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1631 sizeof (struct in_addr)))
1632 return GNUNET_SYSERR;
1636 v6 = (struct IPv6UdpAddress *) addr;
1637 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1639 GNUNET_break_op (0);
1640 return GNUNET_SYSERR;
1642 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1643 return GNUNET_SYSERR;
1645 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1646 sizeof (struct in6_addr)))
1647 return GNUNET_SYSERR;
1654 * Function called for a quick conversion of the binary address to
1655 * a numeric address. Note that the caller must not free the
1656 * address and that the next call to this function is allowed
1657 * to override the address again.
1659 * @param cls closure
1660 * @param addr binary address
1661 * @param addrlen length of the address
1662 * @return string representing the same address
1665 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1667 static char rbuf[INET6_ADDRSTRLEN + 10];
1668 char buf[INET6_ADDRSTRLEN];
1672 const struct IPv4UdpAddress *t4;
1673 const struct IPv6UdpAddress *t6;
1677 if (addrlen == sizeof (struct IPv6UdpAddress))
1681 port = ntohs (t6->u6_port);
1682 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1685 else if (addrlen == sizeof (struct IPv4UdpAddress))
1689 port = ntohs (t4->u4_port);
1690 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1695 GNUNET_break_op (0);
1698 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1699 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1706 * Closure for 'append_port'.
1708 struct PrettyPrinterContext
1711 * Function to call with the result.
1713 GNUNET_TRANSPORT_AddressStringCallback asc;
1716 * Clsoure for 'asc'.
1721 * Port to add after the IP address.
1728 * Append our port and forward the result.
1730 * @param cls a 'struct PrettyPrinterContext'
1731 * @param hostname result from DNS resolver
1734 append_port (void *cls, const char *hostname)
1736 struct PrettyPrinterContext *ppc = cls;
1739 if (hostname == NULL)
1741 ppc->asc (ppc->asc_cls, NULL);
1745 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1746 ppc->asc (ppc->asc_cls, ret);
1752 * Convert the transports address to a nice, human-readable
1755 * @param cls closure
1756 * @param type name of the transport that generated the address
1757 * @param addr one of the addresses of the host, NULL for the last address
1758 * the specific address format depends on the transport
1759 * @param addrlen length of the address
1760 * @param numeric should (IP) addresses be displayed in numeric form?
1761 * @param timeout after how long should we give up?
1762 * @param asc function to call on each string
1763 * @param asc_cls closure for asc
1766 udp_plugin_address_pretty_printer (void *cls, const char *type,
1767 const void *addr, size_t addrlen,
1769 struct GNUNET_TIME_Relative timeout,
1770 GNUNET_TRANSPORT_AddressStringCallback asc,
1773 struct PrettyPrinterContext *ppc;
1776 struct sockaddr_in a4;
1777 struct sockaddr_in6 a6;
1778 const struct IPv4UdpAddress *u4;
1779 const struct IPv6UdpAddress *u6;
1782 if (addrlen == sizeof (struct IPv6UdpAddress))
1785 memset (&a6, 0, sizeof (a6));
1786 a6.sin6_family = AF_INET6;
1787 #if HAVE_SOCKADDR_IN_SIN_LEN
1788 a6.sin6_len = sizeof (a6);
1790 a6.sin6_port = u6->u6_port;
1791 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1792 port = ntohs (u6->u6_port);
1796 else if (addrlen == sizeof (struct IPv4UdpAddress))
1799 memset (&a4, 0, sizeof (a4));
1800 a4.sin_family = AF_INET;
1801 #if HAVE_SOCKADDR_IN_SIN_LEN
1802 a4.sin_len = sizeof (a4);
1804 a4.sin_port = u4->u4_port;
1805 a4.sin_addr.s_addr = u4->ipv4_addr;
1806 port = ntohs (u4->u4_port);
1812 /* invalid address */
1813 GNUNET_break_op (0);
1814 asc (asc_cls, NULL);
1817 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1819 ppc->asc_cls = asc_cls;
1821 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1826 * Our external IP address/port mapping has changed.
1828 * @param cls closure, the 'struct LocalAddrList'
1829 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1830 * the previous (now invalid) one
1831 * @param addr either the previous or the new public IP address
1832 * @param addrlen actual lenght of the address
1835 udp_nat_port_map_callback (void *cls, int add_remove,
1836 const struct sockaddr *addr, socklen_t addrlen)
1838 struct Plugin *plugin = cls;
1839 struct IPv4UdpAddress u4;
1840 struct IPv6UdpAddress u6;
1844 /* convert 'addr' to our internal format */
1845 switch (addr->sa_family)
1848 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1849 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1850 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1855 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1856 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1857 sizeof (struct in6_addr));
1858 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1866 /* modify our published address list */
1867 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1872 * The exported method. Makes the core api available via a global and
1873 * returns the udp transport API.
1875 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1876 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1879 libgnunet_plugin_transport_udp_init (void *cls)
1881 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1882 unsigned long long port;
1883 unsigned long long aport;
1884 unsigned long long bport;
1885 struct GNUNET_TRANSPORT_PluginFunctions *api;
1886 struct Plugin *plugin;
1887 int sockets_created;
1889 struct GNUNET_TIME_Relative interval;
1890 struct sockaddr_in serverAddrv4;
1891 struct sockaddr_in6 serverAddrv6;
1892 struct sockaddr *serverAddr;
1893 struct sockaddr *addrs[2];
1894 socklen_t addrlens[2];
1897 unsigned long long udp_max_bps;
1900 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1905 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
1907 if (broadcast == GNUNET_SYSERR)
1908 broadcast = GNUNET_NO;
1910 if (GNUNET_SYSERR ==
1911 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
1912 "BROADCAST_INTERVAL", &interval))
1913 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1916 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1917 "BROADCAST_PORT", &bport))
1921 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1922 "MAX_BPS", &udp_max_bps))
1923 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1925 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1926 "ADVERTISED_PORT", &aport))
1930 LOG (GNUNET_ERROR_TYPE_WARNING,
1931 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
1935 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1936 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1938 plugin = GNUNET_malloc (sizeof (struct Plugin));
1939 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1940 GNUNET_BANDWIDTH_value_init ((uint32_t)
1942 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1943 plugin->port = port;
1944 plugin->aport = aport;
1945 plugin->broadcast_port = bport;
1946 plugin->broadcast = broadcast;
1948 plugin->broadcast_interval = interval;
1949 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1952 api->send = &udp_plugin_send;
1953 api->disconnect = &udp_disconnect;
1954 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1955 api->address_to_string = &udp_address_to_string;
1956 api->check_address = &udp_plugin_check_address;
1959 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1960 "BINDTO", &plugin->bind4_address))
1962 LOG (GNUNET_ERROR_TYPE_DEBUG,
1963 "Binding udp plugin to specific address: `%s'\n",
1964 plugin->bind4_address);
1965 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
1967 GNUNET_free (plugin->bind4_address);
1968 GNUNET_free (plugin);
1975 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1976 "BINDTO6", &plugin->bind6_address))
1978 LOG (GNUNET_ERROR_TYPE_DEBUG,
1979 "Binding udp plugin to specific address: `%s'\n",
1980 plugin->bind6_address);
1982 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1984 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1985 plugin->bind6_address);
1986 GNUNET_free_non_null (plugin->bind4_address);
1987 GNUNET_free (plugin->bind6_address);
1988 GNUNET_free (plugin);
1995 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1997 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1999 plugin->inbound_sessions =
2000 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
2002 sockets_created = 0;
2004 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
2007 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2008 if (NULL == plugin->sockv6)
2010 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2014 #if HAVE_SOCKADDR_IN_SIN_LEN
2015 serverAddrv6.sin6_len = sizeof (serverAddrv6);
2017 serverAddrv6.sin6_family = AF_INET6;
2018 serverAddrv6.sin6_addr = in6addr_any;
2019 serverAddrv6.sin6_port = htons (plugin->port);
2020 addrlen = sizeof (serverAddrv6);
2021 serverAddr = (struct sockaddr *) &serverAddrv6;
2023 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2024 ntohs (serverAddrv6.sin6_port));
2027 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2030 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2032 LOG (GNUNET_ERROR_TYPE_DEBUG,
2033 "IPv6 Binding failed, trying new port %d\n",
2034 ntohs (serverAddrv6.sin6_port));
2039 GNUNET_NETWORK_socket_close (plugin->sockv6);
2040 plugin->sockv6 = NULL;
2044 if (plugin->sockv6 != NULL)
2046 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2047 addrlens[sockets_created] = sizeof (serverAddrv6);
2054 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2055 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2056 if (NULL == plugin->sockv4)
2058 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2062 #if HAVE_SOCKADDR_IN_SIN_LEN
2063 serverAddrv4.sin_len = sizeof (serverAddrv4);
2065 serverAddrv4.sin_family = AF_INET;
2066 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2067 serverAddrv4.sin_port = htons (plugin->port);
2068 addrlen = sizeof (serverAddrv4);
2069 serverAddr = (struct sockaddr *) &serverAddrv4;
2071 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2072 ntohs (serverAddrv4.sin_port));
2075 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2078 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2080 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2081 ntohs (serverAddrv4.sin_port));
2086 GNUNET_NETWORK_socket_close (plugin->sockv4);
2087 plugin->sockv4 = NULL;
2091 if (plugin->sockv4 != NULL)
2093 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2094 addrlens[sockets_created] = sizeof (serverAddrv4);
2099 plugin->rs = GNUNET_NETWORK_fdset_create ();
2100 GNUNET_NETWORK_fdset_zero (plugin->rs);
2101 if (NULL != plugin->sockv4)
2102 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
2103 if (NULL != plugin->sockv6)
2104 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
2106 plugin->select_task =
2107 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2108 GNUNET_SCHEDULER_NO_TASK,
2109 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
2110 NULL, &udp_plugin_select, plugin);
2113 /* create broadcast socket */
2116 plugin->sockv4_broadcast =
2117 GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2118 if (NULL == plugin->sockv4_broadcast)
2120 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2124 #if HAVE_SOCKADDR_IN_SIN_LEN
2125 serverAddrv4.sin_len = sizeof (serverAddrv4);
2127 serverAddrv4.sin_family = AF_INET;
2128 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2129 serverAddrv4.sin_port = htons (plugin->broadcast_port);
2130 addrlen = sizeof (serverAddrv4);
2131 serverAddr = (struct sockaddr *) &serverAddrv4;
2134 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding Broadcast to IPv4 port %d\n",
2135 ntohs (serverAddrv4.sin_port));
2137 if (GNUNET_NETWORK_socket_bind
2138 (plugin->sockv4_broadcast, serverAddr, addrlen) != GNUNET_OK)
2140 LOG (GNUNET_ERROR_TYPE_WARNING,
2141 _("Failed to create IPv4 broadcast socket on port %d\n"),
2142 ntohs (serverAddrv4.sin_port));
2143 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast);
2144 plugin->sockv4_broadcast = NULL;
2146 if (plugin->sockv4_broadcast != NULL)
2150 if (GNUNET_NETWORK_socket_setsockopt
2151 (plugin->sockv4_broadcast, SOL_SOCKET, SO_BROADCAST, &yes,
2152 sizeof (int)) != GNUNET_OK)
2154 LOG (GNUNET_ERROR_TYPE_WARNING,
2156 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
2157 ntohs (serverAddrv4.sin_port));
2158 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast);
2159 plugin->sockv4_broadcast = NULL;
2163 plugin->broadcast_rs = GNUNET_NETWORK_fdset_create ();
2164 GNUNET_NETWORK_fdset_set (plugin->broadcast_rs,
2165 plugin->sockv4_broadcast);
2170 if (plugin->sockv4_broadcast != NULL)
2172 plugin->broadcast = GNUNET_YES;
2173 plugin->broadcast_mst =
2174 GNUNET_SERVER_mst_create (udp_broadcast_mst_cb, plugin);
2175 GNUNET_STATISTICS_update (plugin->env->stats,
2176 _("# HELLO beacons received via udp"), 1,
2178 plugin->select_broadcast_task =
2179 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2180 GNUNET_SCHEDULER_NO_TASK,
2181 GNUNET_TIME_UNIT_FOREVER_REL,
2182 plugin->broadcast_rs, NULL,
2183 &udp_plugin_broadcast_select, plugin);
2185 plugin->send_broadcast_task =
2186 GNUNET_SCHEDULER_add_now (&udp_broadcast_send, plugin);
2189 plugin->broadcast = GNUNET_NO;
2192 if (sockets_created == 0)
2193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2195 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
2196 (const struct sockaddr **) addrs, addrlens,
2197 &udp_nat_port_map_callback, NULL, plugin);
2202 * Shutdown the plugin.
2204 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
2208 libgnunet_plugin_transport_udp_done (void *cls)
2210 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2211 struct Plugin *plugin = api->cls;
2212 struct ReceiveContext *rc;
2214 /* FIXME: clean up heap and hashmap */
2215 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
2217 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2218 plugin->sessions = NULL;
2219 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions,
2220 &destroy_inbound_session, NULL);
2221 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
2222 plugin->inbound_sessions = NULL;
2223 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
2225 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2228 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
2230 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2232 GNUNET_SCHEDULER_cancel (plugin->select_task);
2233 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2235 if (plugin->sockv4 != NULL)
2237 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2238 plugin->sockv4 = NULL;
2240 if (plugin->sockv6 != NULL)
2242 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2243 plugin->sockv6 = NULL;
2246 if (plugin->broadcast)
2248 if (plugin->select_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2250 GNUNET_SCHEDULER_cancel (plugin->select_broadcast_task);
2251 plugin->select_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2253 if (plugin->send_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
2255 GNUNET_SCHEDULER_cancel (plugin->send_broadcast_task);
2256 plugin->send_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
2258 if (plugin->broadcast_mst != NULL)
2259 GNUNET_SERVER_mst_destroy (plugin->broadcast_mst);
2260 if (plugin->sockv4_broadcast != NULL)
2262 GNUNET_break (GNUNET_OK ==
2263 GNUNET_NETWORK_socket_close (plugin->sockv4_broadcast));
2264 plugin->sockv4_broadcast = NULL;
2266 GNUNET_NETWORK_fdset_destroy (plugin->broadcast_rs);
2269 GNUNET_SERVER_mst_destroy (plugin->mst);
2270 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2272 GNUNET_NAT_unregister (plugin->nat);
2274 GNUNET_free (plugin);
2279 /* end of plugin_transport_udp.c */