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. FIXME: why not make this into
133 * a regular 'struct Session' and pass it around!?
139 * Which peer is this session for?
141 struct GNUNET_PeerIdentity target;
144 * Pointer to the global plugin struct.
146 struct Plugin *plugin;
149 * Address of the other peer
151 const struct sockaddr *sock_addr;
154 * Function to call upon completion of the transmission.
156 GNUNET_TRANSPORT_TransmitContinuation cont;
159 * Closure for 'cont'.
164 * Current outgoing message to this peer.
166 struct GNUNET_FRAGMENT_Context *frag;
172 * Data structure to track defragmentation contexts based
173 * on the source of the UDP traffic.
175 struct ReceiveContext
179 * Defragmentation context.
181 struct GNUNET_DEFRAGMENT_Context *defrag;
184 * Source address this receive context is for (allocated at the
185 * end of the struct).
187 const struct sockaddr *src_addr;
190 * Reference to master plugin struct.
192 struct Plugin *plugin;
195 * Node in the defrag heap.
197 struct GNUNET_CONTAINER_HeapNode *hnode;
200 * Length of 'src_addr'
208 * Encapsulation of all of the state of the plugin.
216 struct GNUNET_TRANSPORT_PluginEnvironment *env;
219 * Session of peers with whom we are currently connected,
220 * map of peer identity to 'struct PeerSession'.
222 struct GNUNET_CONTAINER_MultiHashMap *sessions;
225 * Heap with all of our defragmentation activities.
227 struct GNUNET_CONTAINER_Heap *defrags;
232 GNUNET_SCHEDULER_TaskIdentifier select_task;
235 * Tokenizer for inbound messages.
237 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
240 * Bandwidth tracker to limit global UDP traffic.
242 struct GNUNET_BANDWIDTH_Tracker tracker;
245 * Address we were told to bind to exclusively (IPv4).
250 * Address we were told to bind to exclusively (IPv6).
255 * Handle to NAT traversal support.
257 struct GNUNET_NAT_Handle *nat;
262 struct GNUNET_NETWORK_FDSet *rs;
265 * The read socket for IPv4
267 struct GNUNET_NETWORK_Handle *sockv4;
270 * The read socket for IPv6
272 struct GNUNET_NETWORK_Handle *sockv6;
275 * expected delay for ACKs
277 struct GNUNET_TIME_Relative last_expected_delay;
285 * Port we advertise on.
293 * Lookup the session for the given peer.
295 * @param plugin the plugin
296 * @param peer peer's identity
297 * @return NULL if we have no session
300 find_session (struct Plugin *plugin, 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 if (session->cont != NULL)
329 session->cont (session->cont_cls, target, GNUNET_SYSERR);
330 GNUNET_free (session);
335 * Actually send out the message.
337 * @param plugin the plugin
338 * @param send_handle which handle to send message on
339 * @param target who should receive this message (ignored by UDP)
340 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
341 * @param msgbuf_size the size of the msgbuf to send
342 * @param priority how important is the message (ignored by UDP)
343 * @param timeout when should we time out (give up) if we can not transmit?
344 * @param addr the addr to send the message to, needs to be a sockaddr for us
345 * @param addrlen the len of addr
346 * @param cont continuation to call once the message has
347 * been transmitted (or if the transport is ready
348 * for the next transmission call; or if the
349 * peer disconnected...)
350 * @param cont_cls closure for cont
351 * @return the number of bytes written
354 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
355 const struct GNUNET_MessageHeader *msg)
360 switch (sa->sa_family)
363 if (NULL == plugin->sockv4)
366 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
367 sa, slen = sizeof (struct sockaddr_in));
370 if (NULL == plugin->sockv6)
373 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
374 sa, slen = sizeof (struct sockaddr_in6));
380 if (GNUNET_SYSERR == sent)
381 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "sendto");
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "UDP transmited %u-byte message to %s (%d: %s)\n",
385 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
386 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
393 * Function that is called with messages created by the fragmentation
394 * module. In the case of the 'proc' callback of the
395 * GNUNET_FRAGMENT_context_create function, this function must
396 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
398 * @param cls closure, the 'struct PeerSession'
399 * @param msg the message that was created
402 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
404 struct PeerSession *session = cls;
406 udp_send (session->plugin, session->sock_addr, msg);
407 GNUNET_FRAGMENT_context_transmission_done (session->frag);
411 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
414 * Function that can be used by the transport service to transmit
415 * a message using the plugin.
418 * @param target who should receive this message (ignored by UDP)
419 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
420 * @param msgbuf_size the size of the msgbuf to send
421 * @param priority how important is the message (ignored by UDP)
422 * @param timeout when should we time out (give up) if we can not transmit?
423 * @param session identifier used for this session (NULL for UDP)
424 * @param addr the addr to send the message to
425 * @param addrlen the len of addr
426 * @param force_address not used, we had better have an address to send to
427 * because we are stateless!!
428 * @param cont continuation to call once the message has
429 * been transmitted (or if the transport is ready
430 * for the next transmission call; or if the
431 * peer disconnected...)
432 * @param cont_cls closure for cont
434 * @return the number of bytes written (may return 0 and the message can
435 * still be transmitted later!)
438 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
439 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
440 struct GNUNET_TIME_Relative timeout, struct Session *session,
441 const void *addr, size_t addrlen, int force_address,
442 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
444 struct Plugin *plugin = cls;
445 struct PeerSession *peer_session;
446 const struct IPv4UdpAddress *t4;
447 const struct IPv6UdpAddress *t6;
448 struct sockaddr_in *v4;
449 struct sockaddr_in6 *v6;
450 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
452 struct UDPMessage *udp;
454 if (force_address == GNUNET_SYSERR)
455 return GNUNET_SYSERR;
456 GNUNET_assert (NULL == session);
457 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
460 return GNUNET_SYSERR;
464 case sizeof (struct IPv4UdpAddress):
465 if (NULL == plugin->sockv4)
468 cont (cont_cls, target, GNUNET_SYSERR);
473 GNUNET_malloc (sizeof (struct PeerSession) +
474 sizeof (struct sockaddr_in));
475 v4 = (struct sockaddr_in *) &peer_session[1];
476 v4->sin_family = AF_INET;
477 #if HAVE_SOCKADDR_IN_SIN_LEN
478 v4->sin_len = sizeof (struct sockaddr_in);
480 v4->sin_port = t4->u4_port;
481 v4->sin_addr.s_addr = t4->ipv4_addr;
483 case sizeof (struct IPv6UdpAddress):
484 if (NULL == plugin->sockv6)
487 cont (cont_cls, target, GNUNET_SYSERR);
492 GNUNET_malloc (sizeof (struct PeerSession) +
493 sizeof (struct sockaddr_in6));
494 v6 = (struct sockaddr_in6 *) &peer_session[1];
495 v6->sin6_family = AF_INET6;
496 #if HAVE_SOCKADDR_IN_SIN_LEN
497 v6->sin6_len = sizeof (struct sockaddr_in6);
499 v6->sin6_port = t6->u6_port;
500 v6->sin6_addr = t6->ipv6_addr;
503 /* Must have a valid address to send to */
505 return GNUNET_SYSERR;
507 udp = (struct UDPMessage *) mbuf;
508 udp->header.size = htons (mlen);
509 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
510 udp->reserved = htonl (0);
511 udp->sender = *plugin->env->my_identity;
512 memcpy (&udp[1], msgbuf, msgbuf_size);
513 peer_session->target = *target;
514 peer_session->plugin = plugin;
515 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
516 peer_session->cont = cont;
517 peer_session->cont_cls = cont_cls;
520 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
522 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
523 GNUNET_free_non_null (peer_session);
527 GNUNET_assert (GNUNET_OK ==
528 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
531 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
533 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
535 plugin->last_expected_delay,
536 &udp->header, &send_fragment,
544 * Closure for 'process_inbound_tokenized_messages'
546 struct SourceInformation
551 struct GNUNET_PeerIdentity sender;
559 * Number of bytes in source address.
566 * Message tokenizer has broken up an incomming message. Pass it on
569 * @param cls the 'struct Plugin'
570 * @param client the 'struct SourceInformation'
571 * @param hdr the actual message
574 process_inbound_tokenized_messages (void *cls, void *client,
575 const struct GNUNET_MessageHeader *hdr)
577 struct Plugin *plugin = cls;
578 struct SourceInformation *si = client;
579 struct GNUNET_TRANSPORT_ATS_Information distance[2];
582 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
583 distance[0].value = htonl (1);
584 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
585 distance[1].value = htonl (0);
587 plugin->env->receive (plugin->env->cls, &si->sender, hdr, distance, 2, NULL,
593 * We've received a UDP Message. Process it (pass contents to main service).
595 * @param plugin plugin context
596 * @param msg the message
597 * @param sender_addr sender address
598 * @param sender_addr_len number of bytes in sender_addr
601 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
602 const struct sockaddr *sender_addr,
603 socklen_t sender_addr_len)
605 struct SourceInformation si;
606 struct IPv4UdpAddress u4;
607 struct IPv6UdpAddress u6;
611 if (0 != ntohl (msg->reserved))
616 if (ntohs (msg->header.size) <
617 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
623 /* convert address */
624 switch (sender_addr->sa_family)
627 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
628 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
629 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
634 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
635 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
636 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
645 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
646 "Received message with %u bytes from peer `%s' at `%s'\n",
647 (unsigned int) ntohs (msg->header.size),
648 GNUNET_i2s (&msg->sender), GNUNET_a2s (sender_addr,
652 /* iterate over all embedded messages */
653 si.sender = msg->sender;
656 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
657 ntohs (msg->header.size) -
658 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
663 * Process a defragmented message.
665 * @param cls the 'struct ReceiveContext'
666 * @param msg the message
669 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
671 struct ReceiveContext *rc = cls;
673 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
678 if (ntohs (msg->size) < sizeof (struct UDPMessage))
683 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
684 rc->src_addr, rc->addr_len);
689 * Transmit an acknowledgement.
691 * @param cls the 'struct ReceiveContext'
692 * @param id message ID (unused)
693 * @param msg ack to transmit
696 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
698 struct ReceiveContext *rc = cls;
699 size_t msize = sizeof (struct UDPMessage) + ntohs (msg->size);
701 struct UDPMessage *udp;
704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "Sending ACK to `%s'\n",
705 GNUNET_a2s (rc->src_addr,
706 (rc->src_addr->sa_family ==
707 AF_INET) ? sizeof (struct sockaddr_in) :
708 sizeof (struct sockaddr_in6)));
710 udp = (struct UDPMessage *) buf;
711 udp->header.size = htons ((uint16_t) msize);
712 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
713 udp->reserved = htonl (0);
714 udp->sender = *rc->plugin->env->my_identity;
715 memcpy (&udp[1], msg, ntohs (msg->size));
716 (void) udp_send (rc->plugin, rc->src_addr, &udp->header);
721 * Closure for 'find_receive_context'.
723 struct FindReceiveContext
726 * Where to store the result.
728 struct ReceiveContext *rc;
733 const struct sockaddr *addr;
736 * Number of bytes in 'addr'.
743 * Scan the heap for a receive context with the given address.
745 * @param cls the 'struct FindReceiveContext'
746 * @param node internal node of the heap
747 * @param element value stored at the node (a 'struct ReceiveContext')
748 * @param cost cost associated with the node
749 * @return GNUNET_YES if we should continue to iterate,
753 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
754 void *element, GNUNET_CONTAINER_HeapCostType cost)
756 struct FindReceiveContext *frc = cls;
757 struct ReceiveContext *e = element;
759 if ((frc->addr_len == e->addr_len) &&
760 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
770 * Read and process a message from the given socket.
772 * @param plugin the overall plugin
773 * @param rsock socket to read from
776 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
782 const struct GNUNET_MessageHeader *msg;
783 const struct GNUNET_MessageHeader *ack;
784 struct PeerSession *peer_session;
785 const struct UDPMessage *udp;
786 struct ReceiveContext *rc;
787 struct GNUNET_TIME_Absolute now;
788 struct FindReceiveContext frc;
790 fromlen = sizeof (addr);
791 memset (&addr, 0, sizeof (addr));
793 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
794 (struct sockaddr *) &addr, &fromlen);
795 if (ret < sizeof (struct GNUNET_MessageHeader))
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "UDP received %u-byte message from `%s'\n", (unsigned int) ret,
803 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
805 msg = (const struct GNUNET_MessageHeader *) buf;
806 if (ret != ntohs (msg->size))
811 switch (ntohs (msg->type))
813 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
814 if (ntohs (msg->size) < sizeof (struct UDPMessage))
819 process_udp_message (plugin, (const struct UDPMessage *) msg,
820 (const struct sockaddr *) addr, fromlen);
822 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
823 if (ntohs (msg->size) <
824 sizeof (struct UDPMessage) + sizeof (struct GNUNET_MessageHeader))
829 udp = (const struct UDPMessage *) msg;
830 if (ntohl (udp->reserved) != 0)
835 ack = (const struct GNUNET_MessageHeader *) &udp[1];
836 if (ntohs (ack->size) != ntohs (msg->size) - sizeof (struct UDPMessage))
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
844 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp->sender),
845 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
848 peer_session = find_session (plugin, &udp->sender);
849 if (NULL == peer_session)
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "Session for ACK not found, dropping ACK!\n");
857 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
859 GNUNET_assert (GNUNET_OK ==
860 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
864 plugin->last_expected_delay =
865 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
866 if (peer_session->cont != NULL)
867 peer_session->cont (peer_session->cont_cls, &udp->sender, GNUNET_OK);
868 GNUNET_free (peer_session);
870 case GNUNET_MESSAGE_TYPE_FRAGMENT:
872 frc.addr = (const struct sockaddr *) addr;
873 frc.addr_len = fromlen;
874 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
876 now = GNUNET_TIME_absolute_get ();
880 /* need to create a new RC */
881 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
882 memcpy (&rc[1], addr, fromlen);
883 rc->src_addr = (const struct sockaddr *) &rc[1];
884 rc->addr_len = fromlen;
887 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
888 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
889 &fragment_msg_proc, &ack_proc);
891 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
892 (GNUNET_CONTAINER_HeapCostType)
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
897 "UDP processes %u-byte fragment from `%s'\n",
898 (unsigned int) ntohs (msg->size),
899 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
902 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
904 /* keep this 'rc' from expiring */
905 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
906 (GNUNET_CONTAINER_HeapCostType)
909 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
910 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
912 /* remove 'rc' that was inactive the longest */
913 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
914 GNUNET_assert (NULL != rc);
915 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
927 * We have been notified that our writeset has something to read. We don't
928 * know which socket needs to be read, so we have to check each one
929 * Then reschedule this function to be called again once more is available.
931 * @param cls the plugin handle
932 * @param tc the scheduling context (for rescheduling this function again)
935 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
937 struct Plugin *plugin = cls;
939 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
940 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
942 if ((NULL != plugin->sockv4) &&
943 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
944 udp_read (plugin, plugin->sockv4);
945 if ((NULL != plugin->sockv6) &&
946 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
947 udp_read (plugin, plugin->sockv6);
948 plugin->select_task =
949 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
950 GNUNET_SCHEDULER_NO_TASK,
951 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
952 NULL, &udp_plugin_select, plugin);
958 * Check if the given port is plausible (must be either our listen
959 * port or our advertised port). If it is neither, we return
962 * @param plugin global variables
963 * @param in_port port number to check
964 * @return GNUNET_OK if port is either open_port or adv_port
967 check_port (struct Plugin *plugin, uint16_t in_port)
969 if ((in_port == plugin->port) || (in_port == plugin->aport))
971 return GNUNET_SYSERR;
976 * Function that will be called to check if a binary address for this
977 * plugin is well-formed and corresponds to an address for THIS peer
978 * (as per our configuration). Naturally, if absolutely necessary,
979 * plugins can be a bit conservative in their answer, but in general
980 * plugins should make sure that the address does not redirect
981 * traffic to a 3rd party that might try to man-in-the-middle our
984 * @param cls closure, should be our handle to the Plugin
985 * @param addr pointer to the address
986 * @param addrlen length of addr
987 * @return GNUNET_OK if this is a plausible address for this peer
988 * and transport, GNUNET_SYSERR if not
992 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
994 struct Plugin *plugin = cls;
995 struct IPv4UdpAddress *v4;
996 struct IPv6UdpAddress *v6;
998 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
999 (addrlen != sizeof (struct IPv6UdpAddress)))
1001 GNUNET_break_op (0);
1002 return GNUNET_SYSERR;
1004 if (addrlen == sizeof (struct IPv4UdpAddress))
1006 v4 = (struct IPv4UdpAddress *) addr;
1007 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1008 return GNUNET_SYSERR;
1010 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1011 sizeof (struct in_addr)))
1012 return GNUNET_SYSERR;
1016 v6 = (struct IPv6UdpAddress *) addr;
1017 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1019 GNUNET_break_op (0);
1020 return GNUNET_SYSERR;
1022 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1023 return GNUNET_SYSERR;
1025 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1026 sizeof (struct in6_addr)))
1027 return GNUNET_SYSERR;
1034 * Function called for a quick conversion of the binary address to
1035 * a numeric address. Note that the caller must not free the
1036 * address and that the next call to this function is allowed
1037 * to override the address again.
1039 * @param cls closure
1040 * @param addr binary address
1041 * @param addrlen length of the address
1042 * @return string representing the same address
1045 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1047 static char rbuf[INET6_ADDRSTRLEN + 10];
1048 char buf[INET6_ADDRSTRLEN];
1052 const struct IPv4UdpAddress *t4;
1053 const struct IPv6UdpAddress *t6;
1057 if (addrlen == sizeof (struct IPv6UdpAddress))
1061 port = ntohs (t6->u6_port);
1062 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1065 else if (addrlen == sizeof (struct IPv4UdpAddress))
1069 port = ntohs (t4->u4_port);
1070 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1075 GNUNET_break_op (0);
1078 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1079 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s:%u", buf, port);
1085 * Closure for 'append_port'.
1087 struct PrettyPrinterContext
1090 * Function to call with the result.
1092 GNUNET_TRANSPORT_AddressStringCallback asc;
1095 * Clsoure for 'asc'.
1100 * Port to add after the IP address.
1107 * Append our port and forward the result.
1109 * @param cls a 'struct PrettyPrinterContext'
1110 * @param hostname result from DNS resolver
1113 append_port (void *cls, const char *hostname)
1115 struct PrettyPrinterContext *ppc = cls;
1118 if (hostname == NULL)
1120 ppc->asc (ppc->asc_cls, NULL);
1124 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1125 ppc->asc (ppc->asc_cls, ret);
1131 * Convert the transports address to a nice, human-readable
1134 * @param cls closure
1135 * @param type name of the transport that generated the address
1136 * @param addr one of the addresses of the host, NULL for the last address
1137 * the specific address format depends on the transport
1138 * @param addrlen length of the address
1139 * @param numeric should (IP) addresses be displayed in numeric form?
1140 * @param timeout after how long should we give up?
1141 * @param asc function to call on each string
1142 * @param asc_cls closure for asc
1145 udp_plugin_address_pretty_printer (void *cls, const char *type,
1146 const void *addr, size_t addrlen,
1148 struct GNUNET_TIME_Relative timeout,
1149 GNUNET_TRANSPORT_AddressStringCallback asc,
1152 struct PrettyPrinterContext *ppc;
1155 struct sockaddr_in a4;
1156 struct sockaddr_in6 a6;
1157 const struct IPv4UdpAddress *u4;
1158 const struct IPv6UdpAddress *u6;
1161 if (addrlen == sizeof (struct IPv6UdpAddress))
1164 memset (&a6, 0, sizeof (a6));
1165 a6.sin6_family = AF_INET6;
1166 #if HAVE_SOCKADDR_IN_SIN_LEN
1167 a6.sin6_len = sizeof (a6);
1169 a6.sin6_port = u6->u6_port;
1170 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1171 port = ntohs (u6->u6_port);
1175 else if (addrlen == sizeof (struct IPv4UdpAddress))
1178 memset (&a4, 0, sizeof (a4));
1179 a4.sin_family = AF_INET;
1180 #if HAVE_SOCKADDR_IN_SIN_LEN
1181 a4.sin_len = sizeof (a4);
1183 a4.sin_port = u4->u4_port;
1184 a4.sin_addr.s_addr = u4->ipv4_addr;
1185 port = ntohs (u4->u4_port);
1191 /* invalid address */
1192 GNUNET_break_op (0);
1193 asc (asc_cls, NULL);
1196 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1198 ppc->asc_cls = asc_cls;
1200 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1205 * Our external IP address/port mapping has changed.
1207 * @param cls closure, the 'struct LocalAddrList'
1208 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1209 * the previous (now invalid) one
1210 * @param addr either the previous or the new public IP address
1211 * @param addrlen actual lenght of the address
1214 udp_nat_port_map_callback (void *cls, int add_remove,
1215 const struct sockaddr *addr, socklen_t addrlen)
1217 struct Plugin *plugin = cls;
1218 struct IPv4UdpAddress u4;
1219 struct IPv6UdpAddress u6;
1223 /* convert 'addr' to our internal format */
1224 switch (addr->sa_family)
1227 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1228 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1229 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1234 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1235 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1236 sizeof (struct in6_addr));
1237 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1245 /* modify our published address list */
1246 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1251 * The exported method. Makes the core api available via a global and
1252 * returns the udp transport API.
1254 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1255 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1258 libgnunet_plugin_transport_udp_init (void *cls)
1260 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1261 unsigned long long port;
1262 unsigned long long aport;
1263 struct GNUNET_TRANSPORT_PluginFunctions *api;
1264 struct Plugin *plugin;
1265 int sockets_created;
1266 struct sockaddr_in serverAddrv4;
1267 struct sockaddr_in6 serverAddrv6;
1268 struct sockaddr *serverAddr;
1269 struct sockaddr *addrs[2];
1270 socklen_t addrlens[2];
1273 unsigned long long udp_max_bps;
1276 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1280 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1281 "MAX_BPS", &udp_max_bps))
1282 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1284 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1285 "ADVERTISED_PORT", &aport))
1289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1290 _("Given `%s' option is out of range: %llu > %u\n"), "PORT",
1294 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1295 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1297 plugin = GNUNET_malloc (sizeof (struct Plugin));
1298 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1299 GNUNET_BANDWIDTH_value_init ((uint32_t)
1301 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1302 plugin->port = port;
1303 plugin->aport = aport;
1305 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1308 api->send = &udp_plugin_send;
1309 api->disconnect = &udp_disconnect;
1310 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1311 api->address_to_string = &udp_address_to_string;
1312 api->check_address = &udp_plugin_check_address;
1315 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1316 "BINDTO", &plugin->bind4_address))
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "Binding udp plugin to specific address: `%s'\n",
1320 plugin->bind4_address);
1321 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
1323 GNUNET_free (plugin->bind4_address);
1324 GNUNET_free (plugin);
1331 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1332 "BINDTO6", &plugin->bind6_address))
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Binding udp plugin to specific address: `%s'\n",
1336 plugin->bind6_address);
1338 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1341 plugin->bind6_address);
1342 GNUNET_free_non_null (plugin->bind4_address);
1343 GNUNET_free (plugin->bind6_address);
1344 GNUNET_free (plugin);
1351 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1353 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1355 sockets_created = 0;
1357 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
1360 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1361 if (NULL == plugin->sockv6)
1363 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1367 #if HAVE_SOCKADDR_IN_SIN_LEN
1368 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1370 serverAddrv6.sin6_family = AF_INET6;
1371 serverAddrv6.sin6_addr = in6addr_any;
1372 serverAddrv6.sin6_port = htons (plugin->port);
1373 addrlen = sizeof (serverAddrv6);
1374 serverAddr = (struct sockaddr *) &serverAddrv6;
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1377 ntohs (serverAddrv6.sin6_port));
1380 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1383 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "IPv6 Binding failed, trying new port %d\n",
1387 ntohs (serverAddrv6.sin6_port));
1392 GNUNET_NETWORK_socket_close (plugin->sockv6);
1393 plugin->sockv6 = NULL;
1397 if (plugin->sockv6 != NULL)
1399 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
1400 addrlens[sockets_created] = sizeof (serverAddrv6);
1407 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1408 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1409 if (NULL == plugin->sockv4)
1411 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1415 #if HAVE_SOCKADDR_IN_SIN_LEN
1416 serverAddrv4.sin_len = sizeof (serverAddrv4);
1418 serverAddrv4.sin_family = AF_INET;
1419 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1420 serverAddrv4.sin_port = htons (plugin->port);
1421 addrlen = sizeof (serverAddrv4);
1422 serverAddr = (struct sockaddr *) &serverAddrv4;
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1425 ntohs (serverAddrv4.sin_port));
1428 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1431 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1434 "IPv4 Binding failed, trying new port %d\n",
1435 ntohs (serverAddrv4.sin_port));
1440 GNUNET_NETWORK_socket_close (plugin->sockv4);
1441 plugin->sockv4 = NULL;
1445 if (plugin->sockv4 != NULL)
1447 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
1448 addrlens[sockets_created] = sizeof (serverAddrv4);
1453 plugin->rs = GNUNET_NETWORK_fdset_create ();
1454 GNUNET_NETWORK_fdset_zero (plugin->rs);
1455 if (NULL != plugin->sockv4)
1456 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1457 if (NULL != plugin->sockv6)
1458 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1460 plugin->select_task =
1461 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1462 GNUNET_SCHEDULER_NO_TASK,
1463 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1464 NULL, &udp_plugin_select, plugin);
1465 if (sockets_created == 0)
1466 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1468 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
1469 (const struct sockaddr **) addrs, addrlens,
1470 &udp_nat_port_map_callback, NULL, plugin);
1476 * Destroy a session, plugin is being unloaded.
1479 * @param key hash of public key of target peer
1480 * @param value a 'struct PeerSession*' to clean up
1481 * @return GNUNET_OK (continue to iterate)
1484 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
1486 struct PeerSession *peer_session = value;
1488 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1489 GNUNET_free (peer_session);
1495 * Shutdown the plugin.
1497 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
1501 libgnunet_plugin_transport_udp_done (void *cls)
1503 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1504 struct Plugin *plugin = api->cls;
1505 struct ReceiveContext *rc;
1507 /* FIXME: clean up heap and hashmap */
1508 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
1510 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1511 plugin->sessions = NULL;
1512 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
1514 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1517 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
1519 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1521 GNUNET_SCHEDULER_cancel (plugin->select_task);
1522 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1524 if (plugin->sockv4 != NULL)
1526 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1527 plugin->sockv4 = NULL;
1529 if (plugin->sockv6 != NULL)
1531 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1532 plugin->sockv6 = NULL;
1534 GNUNET_SERVER_mst_destroy (plugin->mst);
1535 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1536 GNUNET_NAT_unregister (plugin->nat);
1538 GNUNET_free (plugin);
1543 /* end of plugin_transport_udp.c */