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_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_transport_plugin.h"
39 #include "transport.h"
41 #define DEBUG_UDP GNUNET_NO
44 * MTU for fragmentation subsystem. Should be conservative since
45 * all communicating peers MUST work with this MTU.
50 * Number of messages we can defragment in parallel. We only really
51 * defragment 1 message at a time, but if messages get re-ordered, we
52 * may want to keep knowledge about the previous message to avoid
53 * discarding the current message in favor of a single fragment of a
54 * previous message. 3 should be good since we don't expect massive
55 * message reorderings with UDP.
57 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
60 * We keep a defragmentation queue per sender address. How many
61 * sender addresses do we support at the same time? Memory consumption
62 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
63 * value. (So 128 corresponds to 12 MB and should suffice for
64 * connecting to roughly 128 peers via UDP).
66 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
70 * UDP Message-Packet header (after defragmentation).
77 struct GNUNET_MessageHeader header;
80 * Always zero for now.
85 * What is the identity of the sender
87 struct GNUNET_PeerIdentity sender;
93 * Network format for IPv4 addresses.
98 * IPv4 address, in network byte order.
100 uint32_t ipv4_addr GNUNET_PACKED;
103 * Port number, in network byte order.
105 uint16_t u4_port GNUNET_PACKED;
110 * Network format for IPv6 addresses.
112 struct IPv6UdpAddress
118 struct in6_addr ipv6_addr GNUNET_PACKED;
121 * Port number, in network byte order.
123 uint16_t u6_port GNUNET_PACKED;
127 /* Forward definition */
132 * Session with another peer.
138 * Which peer is this session for?
140 struct GNUNET_PeerIdentity target;
143 * Pointer to the global plugin struct.
145 struct Plugin *plugin;
148 * Address of the other peer
150 const struct sockaddr *sock_addr;
153 * Function to call upon completion of the transmission.
155 GNUNET_TRANSPORT_TransmitContinuation cont;
158 * Closure for 'cont'.
163 * Current outgoing message to this peer.
165 struct GNUNET_FRAGMENT_Context *frag;
171 * Data structure to track defragmentation contexts based
172 * on the source of the UDP traffic.
174 struct ReceiveContext
178 * Defragmentation context.
180 struct GNUNET_DEFRAGMENT_Context *defrag;
183 * Source address this receive context is for (allocated at the
184 * end of the struct).
186 const struct sockaddr *src_addr;
189 * Reference to master plugin struct.
191 struct Plugin *plugin;
194 * Node in the defrag heap.
196 struct GNUNET_CONTAINER_HeapNode *hnode;
199 * Length of 'src_addr'
207 * Encapsulation of all of the state of the plugin.
215 struct GNUNET_TRANSPORT_PluginEnvironment *env;
218 * Session of peers with whom we are currently connected,
219 * map of peer identity to 'struct PeerSession'.
221 struct GNUNET_CONTAINER_MultiHashMap *sessions;
224 * Heap with all of our defragmentation activities.
226 struct GNUNET_CONTAINER_Heap *defrags;
231 GNUNET_SCHEDULER_TaskIdentifier select_task;
234 * Tokenizer for inbound messages.
236 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
239 * Bandwidth tracker to limit global UDP traffic.
241 struct GNUNET_BANDWIDTH_Tracker tracker;
244 * Address we were told to bind to exclusively (IPv4).
249 * Address we were told to bind to exclusively (IPv6).
254 * Handle to NAT traversal support.
256 struct GNUNET_NAT_Handle *nat;
261 struct GNUNET_NETWORK_FDSet *rs;
264 * The read socket for IPv4
266 struct GNUNET_NETWORK_Handle *sockv4;
269 * The read socket for IPv6
271 struct GNUNET_NETWORK_Handle *sockv6;
274 * expected delay for ACKs
276 struct GNUNET_TIME_Relative last_expected_delay;
284 * Port we advertise on.
292 * Lookup the session for the given peer.
294 * @param plugin the plugin
295 * @param peer peer's identity
296 * @return NULL if we have no session
299 find_session (struct Plugin *plugin,
300 const struct GNUNET_PeerIdentity *peer)
302 return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
308 * Disconnect from a remote node. Clean up session if we have one for this peer
310 * @param cls closure for this call (should be handle to Plugin)
311 * @param target the peeridentity of the peer to disconnect
312 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
315 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
317 struct Plugin *plugin = cls;
318 struct PeerSession *session;
320 session = find_session (plugin, target);
323 GNUNET_assert (GNUNET_OK ==
324 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
327 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
328 session->cont (session->cont_cls, target, GNUNET_SYSERR);
329 GNUNET_free (session);
334 * Actually send out the message.
336 * @param plugin the plugin
337 * @param send_handle which handle to send message on
338 * @param target who should receive this message (ignored by UDP)
339 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
340 * @param msgbuf_size the size of the msgbuf to send
341 * @param priority how important is the message (ignored by UDP)
342 * @param timeout when should we time out (give up) if we can not transmit?
343 * @param addr the addr to send the message to, needs to be a sockaddr for us
344 * @param addrlen the len of addr
345 * @param cont continuation to call once the message has
346 * been transmitted (or if the transport is ready
347 * for the next transmission call; or if the
348 * peer disconnected...)
349 * @param cont_cls closure for cont
350 * @return the number of bytes written
353 udp_send (struct Plugin *plugin,
354 const struct sockaddr *sa,
355 const struct GNUNET_MessageHeader *msg)
360 switch (sa->sa_family)
364 GNUNET_NETWORK_socket_sendto (plugin->sockv4,
368 slen = sizeof (struct sockaddr_in));
372 GNUNET_NETWORK_socket_sendto (plugin->sockv6,
376 slen = sizeof (struct sockaddr_in6));
382 if (GNUNET_SYSERR == sent)
383 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "UDP transmited %u-byte message to %s (%d: %s)\n",
388 (unsigned int) ntohs (msg->size),
389 GNUNET_a2s (sa, slen),
391 (sent < 0) ? STRERROR (errno) : "ok");
398 * Function that is called with messages created by the fragmentation
399 * module. In the case of the 'proc' callback of the
400 * GNUNET_FRAGMENT_context_create function, this function must
401 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
403 * @param cls closure, the 'struct PeerSession'
404 * @param msg the message that was created
407 send_fragment (void *cls,
408 const struct GNUNET_MessageHeader *msg)
410 struct PeerSession *session = cls;
412 udp_send (session->plugin,
415 GNUNET_FRAGMENT_context_transmission_done (session->frag);
420 * Function that can be used by the transport service to transmit
421 * a message using the plugin.
424 * @param target who should receive this message (ignored by UDP)
425 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
426 * @param msgbuf_size the size of the msgbuf to send
427 * @param priority how important is the message (ignored by UDP)
428 * @param timeout when should we time out (give up) if we can not transmit?
429 * @param session identifier used for this session (NULL for UDP)
430 * @param addr the addr to send the message to
431 * @param addrlen the len of addr
432 * @param force_address not used, we had better have an address to send to
433 * because we are stateless!!
434 * @param cont continuation to call once the message has
435 * been transmitted (or if the transport is ready
436 * for the next transmission call; or if the
437 * peer disconnected...)
438 * @param cont_cls closure for cont
440 * @return the number of bytes written (may return 0 and the message can
441 * still be transmitted later!)
444 udp_plugin_send (void *cls,
445 const struct GNUNET_PeerIdentity *target,
448 unsigned int priority,
449 struct GNUNET_TIME_Relative timeout,
450 struct Session *session,
454 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
456 struct Plugin *plugin = cls;
457 struct PeerSession *peer_session;
458 const struct IPv4UdpAddress *t4;
459 const struct IPv6UdpAddress *t6;
460 struct sockaddr_in *v4;
461 struct sockaddr_in6 *v6;
462 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
464 struct UDPMessage *udp;
466 if (force_address == GNUNET_SYSERR)
467 return GNUNET_SYSERR;
468 GNUNET_assert (NULL == session);
469 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
472 return GNUNET_SYSERR;
476 case sizeof(struct IPv4UdpAddress):
478 peer_session = GNUNET_malloc (sizeof (struct PeerSession) + sizeof (struct sockaddr_in));
479 v4 = (struct sockaddr_in*) &peer_session[1];
480 v4->sin_family = AF_INET;
481 #if HAVE_SOCKADDR_IN_SIN_LEN
482 v4->sin_len = sizeof (struct sockaddr_in);
484 v4->sin_port = t4->u4_port;
485 v4->sin_addr.s_addr = t4->ipv4_addr;
487 case sizeof(struct IPv6UdpAddress):
489 peer_session = GNUNET_malloc (sizeof (struct PeerSession) + sizeof (struct sockaddr_in6));
490 v6 = (struct sockaddr_in6*) &peer_session[1];
491 v6->sin6_family = AF_INET6;
492 #if HAVE_SOCKADDR_IN_SIN_LEN
493 v6->sin6_len = sizeof (struct sockaddr_in6);
495 v6->sin6_port = t6->u6_port;
496 v6->sin6_addr = t6->ipv6_addr;
499 /* Must have a valid address to send to */
501 return GNUNET_SYSERR;
503 udp = (struct UDPMessage*) mbuf;
504 udp->header.size = htons (mlen);
505 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
506 udp->reserved = htonl (0);
507 udp->sender = *plugin->env->my_identity;
508 memcpy (&udp[1], msgbuf, msgbuf_size);
509 peer_session->target = *target;
510 peer_session->plugin = plugin;
511 peer_session->sock_addr = (const struct sockaddr*) &peer_session[1];
512 peer_session->cont = cont;
513 peer_session->cont_cls = cont_cls;
516 mlen = udp_send (plugin,
517 peer_session->sock_addr,
519 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
520 GNUNET_free (peer_session);
524 GNUNET_assert (GNUNET_OK ==
525 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
528 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
529 peer_session->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
532 plugin->last_expected_delay,
542 * Closure for 'process_inbound_tokenized_messages'
544 struct SourceInformation
549 struct GNUNET_PeerIdentity sender;
557 * Number of bytes in source address.
564 * Message tokenizer has broken up an incomming message. Pass it on
567 * @param cls the 'struct Plugin'
568 * @param client the 'struct SourceInformation'
569 * @param hdr the actual message
572 process_inbound_tokenized_messages (void *cls,
574 const struct GNUNET_MessageHeader *hdr)
576 struct Plugin *plugin = cls;
577 struct SourceInformation* si = client;
578 struct GNUNET_TRANSPORT_ATS_Information distance[2];
581 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
582 distance[0].value = htonl (1);
583 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
584 distance[1].value = htonl (0);
586 plugin->env->receive (plugin->env->cls,
596 * We've received a UDP Message. Process it (pass contents to main service).
598 * @param plugin plugin context
599 * @param msg the message
600 * @param sender_addr sender address
601 * @param sender_addr_len number of bytes in sender_addr
604 process_udp_message (struct Plugin *plugin,
605 const struct UDPMessage *msg,
606 const struct sockaddr *sender_addr,
607 socklen_t sender_addr_len)
609 struct SourceInformation si;
610 struct IPv4UdpAddress u4;
611 struct IPv6UdpAddress u6;
615 if (0 != ntohl (msg->reserved))
620 if (ntohs (msg->header.size) < sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
626 /* convert address */
627 switch (sender_addr->sa_family)
630 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
631 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
632 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
637 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
638 u6.ipv6_addr = ((struct sockaddr_in6*) sender_addr)->sin6_addr;
639 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
648 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
650 "Received message with %u bytes from peer `%s' at `%s'\n",
651 (unsigned int) ntohs (msg->header.size),
652 GNUNET_i2s (&msg->sender),
653 GNUNET_a2s (sender_addr, sender_addr_len));
656 /* iterate over all embedded messages */
657 si.sender = msg->sender;
660 GNUNET_SERVER_mst_receive (plugin->mst,
662 (const char*) &msg[1],
663 ntohs (msg->header.size) - sizeof (struct UDPMessage),
670 * Process a defragmented message.
672 * @param cls the 'struct ReceiveContext'
673 * @param msg the message
676 fragment_msg_proc (void *cls,
677 const struct GNUNET_MessageHeader *msg)
679 struct ReceiveContext *rc = cls;
681 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
686 if (ntohs (msg->size) < sizeof(struct UDPMessage))
691 process_udp_message (rc->plugin,
692 (const struct UDPMessage*) msg,
699 * Transmit an acknowledgement.
701 * @param cls the 'struct ReceiveContext'
702 * @param id message ID (unused)
703 * @param msg ack to transmit
708 const struct GNUNET_MessageHeader *msg)
710 struct ReceiveContext *rc = cls;
711 size_t msize = sizeof (struct UDPMessage) + ntohs (msg->size);
713 struct UDPMessage *udp;
716 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
718 "Sending ACK to `%s'\n",
719 GNUNET_a2s (rc->src_addr,
720 (rc->src_addr->sa_family == AF_INET)
721 ? sizeof (struct sockaddr_in)
722 : sizeof (struct sockaddr_in6)));
724 udp = (struct UDPMessage*) buf;
725 udp->header.size = htons ((uint16_t) msize);
726 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
727 udp->reserved = htonl (0);
728 udp->sender = *rc->plugin->env->my_identity;
729 memcpy (&udp[1], msg, ntohs (msg->size));
730 (void) udp_send (rc->plugin,
737 * Closure for 'find_receive_context'.
739 struct FindReceiveContext
742 * Where to store the result.
744 struct ReceiveContext *rc;
749 const struct sockaddr *addr;
752 * Number of bytes in 'addr'.
759 * Scan the heap for a receive context with the given address.
761 * @param cls the 'struct FindReceiveContext'
762 * @param node internal node of the heap
763 * @param element value stored at the node (a 'struct ReceiveContext')
764 * @param cost cost associated with the node
765 * @return GNUNET_YES if we should continue to iterate,
769 find_receive_context (void *cls,
770 struct GNUNET_CONTAINER_HeapNode *node,
772 GNUNET_CONTAINER_HeapCostType cost)
774 struct FindReceiveContext *frc = cls;
775 struct ReceiveContext *e = element;
777 if ( (frc->addr_len == e->addr_len) &&
778 (0 == memcmp (frc->addr,
790 * Read and process a message from the given socket.
792 * @param plugin the overall plugin
793 * @param rsock socket to read from
796 udp_read (struct Plugin *plugin,
797 struct GNUNET_NETWORK_Handle *rsock)
803 const struct GNUNET_MessageHeader *msg;
804 const struct GNUNET_MessageHeader *ack;
805 struct PeerSession *peer_session;
806 const struct UDPMessage *udp;
807 struct ReceiveContext *rc;
808 struct GNUNET_TIME_Absolute now;
809 struct FindReceiveContext frc;
811 fromlen = sizeof (addr);
812 memset (&addr, 0, sizeof(addr));
813 ret = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
814 (struct sockaddr *)&addr, &fromlen);
815 if (ret < sizeof (struct GNUNET_MessageHeader))
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "UDP received %u-byte message from `%s'\n",
824 GNUNET_a2s ((const struct sockaddr*) addr, fromlen));
826 msg = (const struct GNUNET_MessageHeader *) buf;
827 if (ret != ntohs (msg->size))
832 switch (ntohs (msg->type))
834 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
835 if (ntohs (msg->size) < sizeof (struct UDPMessage))
840 process_udp_message (plugin,
841 (const struct UDPMessage *) msg,
842 (const struct sockaddr*) addr,
845 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
846 if (ntohs (msg->size) < sizeof (struct UDPMessage) + sizeof (struct GNUNET_MessageHeader))
851 udp = (const struct UDPMessage *) msg;
852 if (ntohl (udp->reserved) != 0)
857 ack = (const struct GNUNET_MessageHeader*) &udp[1];
858 if (ntohs (ack->size) != ntohs (msg->size) - sizeof (struct UDPMessage))
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
866 (unsigned int) ntohs (msg->size),
867 GNUNET_i2s (&udp->sender),
868 GNUNET_a2s ((const struct sockaddr*) addr, fromlen));
871 peer_session = find_session (plugin, &udp->sender);
872 if (NULL == peer_session)
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Session for ACK not found, dropping ACK!\n");
881 GNUNET_FRAGMENT_process_ack (peer_session->frag,
884 GNUNET_assert (GNUNET_OK ==
885 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
886 &udp->sender.hashPubKey,
888 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (peer_session->frag);
889 peer_session->cont (peer_session->cont_cls,
892 GNUNET_free (peer_session);
894 case GNUNET_MESSAGE_TYPE_FRAGMENT:
896 frc.addr = (const struct sockaddr*) addr;
897 frc.addr_len = fromlen;
898 GNUNET_CONTAINER_heap_iterate (plugin->defrags,
899 &find_receive_context,
901 now = GNUNET_TIME_absolute_get ();
905 /* need to create a new RC */
906 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
907 memcpy (&rc[1], addr, fromlen);
908 rc->src_addr = (const struct sockaddr*) &rc[1];
909 rc->addr_len = fromlen;
911 rc->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
913 UDP_MAX_MESSAGES_IN_DEFRAG,
917 rc->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrags,
919 (GNUNET_CONTAINER_HeapCostType) now.abs_value);
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "UDP processes %u-byte fragment from `%s'\n",
924 (unsigned int) ntohs (msg->size),
925 GNUNET_a2s ((const struct sockaddr*) addr, fromlen));
929 GNUNET_DEFRAGMENT_process_fragment (rc->defrag,
932 /* keep this 'rc' from expiring */
933 GNUNET_CONTAINER_heap_update_cost (plugin->defrags,
935 (GNUNET_CONTAINER_HeapCostType) now.abs_value);
937 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) > UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
939 /* remove 'rc' that was inactive the longest */
940 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
941 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
953 * We have been notified that our writeset has something to read. We don't
954 * know which socket needs to be read, so we have to check each one
955 * Then reschedule this function to be called again once more is available.
957 * @param cls the plugin handle
958 * @param tc the scheduling context (for rescheduling this function again)
961 udp_plugin_select (void *cls,
962 const struct GNUNET_SCHEDULER_TaskContext *tc)
964 struct Plugin *plugin = cls;
966 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
967 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
969 if ( (NULL != plugin->sockv4) &&
970 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
972 udp_read (plugin, plugin->sockv4);
973 if ( (NULL != plugin->sockv6) &&
974 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
976 udp_read (plugin, plugin->sockv6);
977 plugin->select_task =
978 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
979 GNUNET_SCHEDULER_NO_TASK,
980 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
981 NULL, &udp_plugin_select, plugin);
987 * Check if the given port is plausible (must be either our listen
988 * port or our advertised port). If it is neither, we return
991 * @param plugin global variables
992 * @param in_port port number to check
993 * @return GNUNET_OK if port is either open_port or adv_port
996 check_port (struct Plugin *plugin, uint16_t in_port)
998 if ( (in_port == plugin->port) ||
999 (in_port == plugin->aport) )
1001 return GNUNET_SYSERR;
1006 * Function that will be called to check if a binary address for this
1007 * plugin is well-formed and corresponds to an address for THIS peer
1008 * (as per our configuration). Naturally, if absolutely necessary,
1009 * plugins can be a bit conservative in their answer, but in general
1010 * plugins should make sure that the address does not redirect
1011 * traffic to a 3rd party that might try to man-in-the-middle our
1014 * @param cls closure, should be our handle to the Plugin
1015 * @param addr pointer to the address
1016 * @param addrlen length of addr
1017 * @return GNUNET_OK if this is a plausible address for this peer
1018 * and transport, GNUNET_SYSERR if not
1022 udp_plugin_check_address (void *cls,
1026 struct Plugin *plugin = cls;
1027 struct IPv4UdpAddress *v4;
1028 struct IPv6UdpAddress *v6;
1030 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1031 (addrlen != sizeof (struct IPv6UdpAddress)))
1033 GNUNET_break_op (0);
1034 return GNUNET_SYSERR;
1036 if (addrlen == sizeof (struct IPv4UdpAddress))
1038 v4 = (struct IPv4UdpAddress *) addr;
1040 check_port (plugin, ntohs (v4->u4_port)))
1041 return GNUNET_SYSERR;
1043 GNUNET_NAT_test_address (plugin->nat,
1044 &v4->ipv4_addr, sizeof (struct in_addr)))
1045 return GNUNET_SYSERR;
1049 v6 = (struct IPv6UdpAddress *) addr;
1050 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1052 GNUNET_break_op (0);
1053 return GNUNET_SYSERR;
1056 check_port (plugin, ntohs (v6->u6_port)))
1057 return GNUNET_SYSERR;
1059 GNUNET_NAT_test_address (plugin->nat,
1060 &v6->ipv6_addr, sizeof (struct in6_addr)))
1061 return GNUNET_SYSERR;
1068 * Function called for a quick conversion of the binary address to
1069 * a numeric address. Note that the caller must not free the
1070 * address and that the next call to this function is allowed
1071 * to override the address again.
1073 * @param cls closure
1074 * @param addr binary address
1075 * @param addrlen length of the address
1076 * @return string representing the same address
1079 udp_address_to_string (void *cls,
1083 static char rbuf[INET6_ADDRSTRLEN + 10];
1084 char buf[INET6_ADDRSTRLEN];
1088 const struct IPv4UdpAddress *t4;
1089 const struct IPv6UdpAddress *t6;
1093 if (addrlen == sizeof (struct IPv6UdpAddress))
1097 port = ntohs (t6->u6_port);
1098 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1101 else if (addrlen == sizeof (struct IPv4UdpAddress))
1105 port = ntohs (t4->u4_port);
1106 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1111 GNUNET_break_op (0);
1114 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1115 GNUNET_snprintf (rbuf,
1125 * Closure for 'append_port'.
1127 struct PrettyPrinterContext
1130 * Function to call with the result.
1132 GNUNET_TRANSPORT_AddressStringCallback asc;
1135 * Clsoure for 'asc'.
1140 * Port to add after the IP address.
1147 * Append our port and forward the result.
1149 * @param cls a 'struct PrettyPrinterContext'
1150 * @param hostname result from DNS resolver
1153 append_port (void *cls, const char *hostname)
1155 struct PrettyPrinterContext *ppc = cls;
1158 if (hostname == NULL)
1160 ppc->asc (ppc->asc_cls, NULL);
1164 GNUNET_asprintf (&ret,
1168 ppc->asc (ppc->asc_cls, ret);
1174 * Convert the transports address to a nice, human-readable
1177 * @param cls closure
1178 * @param type name of the transport that generated the address
1179 * @param addr one of the addresses of the host, NULL for the last address
1180 * the specific address format depends on the transport
1181 * @param addrlen length of the address
1182 * @param numeric should (IP) addresses be displayed in numeric form?
1183 * @param timeout after how long should we give up?
1184 * @param asc function to call on each string
1185 * @param asc_cls closure for asc
1188 udp_plugin_address_pretty_printer (void *cls,
1193 struct GNUNET_TIME_Relative timeout,
1194 GNUNET_TRANSPORT_AddressStringCallback asc,
1197 struct PrettyPrinterContext *ppc;
1200 struct sockaddr_in a4;
1201 struct sockaddr_in6 a6;
1202 const struct IPv4UdpAddress *u4;
1203 const struct IPv6UdpAddress *u6;
1206 if (addrlen == sizeof (struct IPv6UdpAddress))
1209 memset (&a6, 0, sizeof (a6));
1210 a6.sin6_family = AF_INET6;
1211 #if HAVE_SOCKADDR_IN_SIN_LEN
1212 a6.sin6_len = sizeof (a6);
1214 a6.sin6_port = u6->u6_port;
1215 memcpy (&a6.sin6_addr,
1217 sizeof (struct in6_addr));
1218 port = ntohs (u6->u6_port);
1222 else if (addrlen == sizeof (struct IPv4UdpAddress))
1225 memset (&a4, 0, sizeof (a4));
1226 a4.sin_family = AF_INET;
1227 #if HAVE_SOCKADDR_IN_SIN_LEN
1228 a4.sin_len = sizeof (a4);
1230 a4.sin_port = u4->u4_port;
1231 a4.sin_addr.s_addr = u4->ipv4_addr;
1232 port = ntohs (u4->u4_port);
1238 /* invalid address */
1239 GNUNET_break_op (0);
1240 asc (asc_cls, NULL);
1243 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1245 ppc->asc_cls = asc_cls;
1247 GNUNET_RESOLVER_hostname_get (sb,
1255 * Our external IP address/port mapping has changed.
1257 * @param cls closure, the 'struct LocalAddrList'
1258 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1259 * the previous (now invalid) one
1260 * @param addr either the previous or the new public IP address
1261 * @param addrlen actual lenght of the address
1264 udp_nat_port_map_callback (void *cls,
1266 const struct sockaddr *addr,
1269 struct Plugin *plugin = cls;
1270 struct IPv4UdpAddress u4;
1271 struct IPv6UdpAddress u6;
1275 /* convert 'addr' to our internal format */
1276 switch (addr->sa_family)
1279 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1280 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1281 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1286 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1287 memcpy (&u6.ipv6_addr,
1288 &((struct sockaddr_in6 *) addr)->sin6_addr,
1289 sizeof (struct in6_addr));
1290 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1298 /* modify our published address list */
1299 plugin->env->notify_address (plugin->env->cls,
1306 * The exported method. Makes the core api available via a global and
1307 * returns the udp transport API.
1309 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1310 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1313 libgnunet_plugin_transport_udp_init (void *cls)
1315 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1316 unsigned long long port;
1317 unsigned long long aport;
1318 struct GNUNET_TRANSPORT_PluginFunctions *api;
1319 struct Plugin *plugin;
1320 int sockets_created;
1321 struct sockaddr_in serverAddrv4;
1322 struct sockaddr_in6 serverAddrv6;
1323 struct sockaddr *serverAddr;
1324 struct sockaddr *addrs[2];
1325 socklen_t addrlens[2];
1328 unsigned long long udp_max_bps;
1331 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1337 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1341 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1343 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1351 _("Given `%s' option is out of range: %llu > %u\n"),
1357 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1358 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1360 plugin = GNUNET_malloc (sizeof (struct Plugin));
1361 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1362 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
1364 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1365 plugin->port = port;
1366 plugin->aport = aport;
1368 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1371 api->send = &udp_plugin_send;
1372 api->disconnect = &udp_disconnect;
1373 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1374 api->address_to_string = &udp_address_to_string;
1375 api->check_address = &udp_plugin_check_address;
1377 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(env->cfg,
1380 &plugin->bind4_address))
1382 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1383 "Binding udp plugin to specific address: `%s'\n",
1384 plugin->bind4_address);
1385 if (1 != inet_pton(AF_INET,
1386 plugin->bind4_address,
1387 &serverAddrv4.sin_addr))
1389 GNUNET_free (plugin->bind4_address);
1390 GNUNET_free (plugin);
1396 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(env->cfg,
1399 &plugin->bind6_address))
1401 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1402 "Binding udp plugin to specific address: `%s'\n",
1403 plugin->bind6_address);
1404 if (1 != inet_pton(AF_INET6,
1405 plugin->bind6_address,
1406 &serverAddrv6.sin6_addr))
1408 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1409 _("Invalid IPv6 address: `%s'\n"),
1410 plugin->bind6_address);
1411 GNUNET_free_non_null (plugin->bind4_address);
1412 GNUNET_free (plugin->bind6_address);
1413 GNUNET_free (plugin);
1418 plugin->defrags = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1419 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG * 2);
1420 sockets_created = 0;
1422 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
1426 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1427 if (NULL == plugin->sockv6)
1429 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1433 #if HAVE_SOCKADDR_IN_SIN_LEN
1434 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1436 serverAddrv6.sin6_family = AF_INET6;
1437 serverAddrv6.sin6_addr = in6addr_any;
1438 serverAddrv6.sin6_port = htons (plugin->port);
1439 addrlen = sizeof (serverAddrv6);
1440 serverAddr = (struct sockaddr *) &serverAddrv6;
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Binding to IPv6 port %d\n",
1444 ntohs(serverAddrv6.sin6_port));
1447 while (GNUNET_NETWORK_socket_bind (plugin->sockv6,
1448 serverAddr, addrlen) !=
1451 serverAddrv6.sin6_port
1452 = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "IPv6 Binding failed, trying new port %d\n",
1456 ntohs(serverAddrv6.sin6_port));
1461 GNUNET_NETWORK_socket_close (plugin->sockv6);
1462 plugin->sockv6 = NULL;
1466 if (plugin->sockv6 != NULL)
1468 addrs[sockets_created] = (struct sockaddr*) &serverAddrv6;
1469 addrlens[sockets_created] = sizeof (serverAddrv6);
1475 plugin->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
1477 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1478 if (NULL == plugin->sockv4)
1480 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1484 #if HAVE_SOCKADDR_IN_SIN_LEN
1485 serverAddrv4.sin_len = sizeof (serverAddrv4);
1487 serverAddrv4.sin_family = AF_INET;
1488 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1489 serverAddrv4.sin_port = htons (plugin->port);
1490 addrlen = sizeof (serverAddrv4);
1491 serverAddr = (struct sockaddr *) &serverAddrv4;
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 "Binding to IPv4 port %d\n",
1495 ntohs(serverAddrv4.sin_port));
1498 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1501 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "IPv4 Binding failed, trying new port %d\n",
1505 ntohs(serverAddrv4.sin_port));
1510 GNUNET_NETWORK_socket_close (plugin->sockv4);
1511 plugin->sockv4 = NULL;
1515 if (plugin->sockv4 != NULL)
1517 addrs[sockets_created] = (struct sockaddr*) &serverAddrv4;
1518 addrlens[sockets_created] = sizeof (serverAddrv4);
1523 plugin->rs = GNUNET_NETWORK_fdset_create ();
1524 GNUNET_NETWORK_fdset_zero (plugin->rs);
1525 if (NULL != plugin->sockv4)
1526 GNUNET_NETWORK_fdset_set (plugin->rs,
1528 if (NULL != plugin->sockv6)
1529 GNUNET_NETWORK_fdset_set (plugin->rs,
1532 plugin->select_task =
1533 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1534 GNUNET_SCHEDULER_NO_TASK,
1535 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1536 NULL, &udp_plugin_select, plugin);
1537 if (sockets_created == 0)
1538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1539 _("Failed to open UDP sockets\n"));
1540 plugin->nat = GNUNET_NAT_register (env->cfg,
1544 (const struct sockaddr**) addrs, addrlens,
1545 &udp_nat_port_map_callback,
1553 * Destroy a session, plugin is being unloaded.
1556 * @param key hash of public key of target peer
1557 * @param value a 'struct PeerSession*' to clean up
1558 * @return GNUNET_OK (continue to iterate)
1561 destroy_session (void *cls,
1562 const GNUNET_HashCode *key,
1565 struct PeerSession *peer_session = value;
1567 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1568 GNUNET_free (peer_session);
1574 * Shutdown the plugin.
1576 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
1580 libgnunet_plugin_transport_udp_done (void *cls)
1582 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1583 struct Plugin *plugin = api->cls;
1584 struct ReceiveContext *rc;
1586 /* FIXME: clean up heap and hashmap */
1587 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1590 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1591 plugin->sessions = NULL;
1592 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
1594 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1597 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
1599 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1601 GNUNET_SCHEDULER_cancel (plugin->select_task);
1602 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1604 if (plugin->sockv4 != NULL)
1606 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1607 plugin->sockv4 = NULL;
1609 if (plugin->sockv6 != NULL)
1611 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1612 plugin->sockv6 = NULL;
1614 GNUNET_SERVER_mst_destroy (plugin->mst);
1615 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1616 GNUNET_NAT_unregister (plugin->nat);
1618 GNUNET_free (plugin);
1623 /* end of plugin_transport_udp.c */