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 sa the address to send the message to
339 * @param msg message to transmit
340 * @return the number of bytes written
343 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
344 const struct GNUNET_MessageHeader *msg)
349 switch (sa->sa_family)
352 if (NULL == plugin->sockv4)
355 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
356 sa, slen = sizeof (struct sockaddr_in));
359 if (NULL == plugin->sockv6)
362 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
363 sa, slen = sizeof (struct sockaddr_in6));
369 if (GNUNET_SYSERR == sent)
370 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "sendto");
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "UDP transmited %u-byte message to %s (%d: %s)\n",
374 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
375 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
382 * Function that is called with messages created by the fragmentation
383 * module. In the case of the 'proc' callback of the
384 * GNUNET_FRAGMENT_context_create function, this function must
385 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
387 * @param cls closure, the 'struct PeerSession'
388 * @param msg the message that was created
391 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
393 struct PeerSession *session = cls;
395 udp_send (session->plugin, session->sock_addr, msg);
396 GNUNET_FRAGMENT_context_transmission_done (session->frag);
400 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
403 * Function that can be used by the transport service to transmit
404 * a message using the plugin.
407 * @param target who should receive this message (ignored by UDP)
408 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
409 * @param msgbuf_size the size of the msgbuf to send
410 * @param priority how important is the message (ignored by UDP)
411 * @param timeout when should we time out (give up) if we can not transmit?
412 * @param session identifier used for this session (NULL for UDP)
413 * @param addr the addr to send the message to
414 * @param addrlen the len of addr
415 * @param force_address not used, we had better have an address to send to
416 * because we are stateless!!
417 * @param cont continuation to call once the message has
418 * been transmitted (or if the transport is ready
419 * for the next transmission call; or if the
420 * peer disconnected...)
421 * @param cont_cls closure for cont
423 * @return the number of bytes written (may return 0 and the message can
424 * still be transmitted later!)
427 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
428 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
429 struct GNUNET_TIME_Relative timeout, struct Session *session,
430 const void *addr, size_t addrlen, int force_address,
431 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
433 struct Plugin *plugin = cls;
434 struct PeerSession *peer_session;
435 const struct IPv4UdpAddress *t4;
436 const struct IPv6UdpAddress *t6;
437 struct sockaddr_in *v4;
438 struct sockaddr_in6 *v6;
439 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
441 struct UDPMessage *udp;
443 if (force_address == GNUNET_SYSERR)
444 return GNUNET_SYSERR;
445 GNUNET_assert (NULL == session);
446 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
449 return GNUNET_SYSERR;
453 case sizeof (struct IPv4UdpAddress):
454 if (NULL == plugin->sockv4)
457 cont (cont_cls, target, GNUNET_SYSERR);
462 GNUNET_malloc (sizeof (struct PeerSession) +
463 sizeof (struct sockaddr_in));
464 v4 = (struct sockaddr_in *) &peer_session[1];
465 v4->sin_family = AF_INET;
466 #if HAVE_SOCKADDR_IN_SIN_LEN
467 v4->sin_len = sizeof (struct sockaddr_in);
469 v4->sin_port = t4->u4_port;
470 v4->sin_addr.s_addr = t4->ipv4_addr;
472 case sizeof (struct IPv6UdpAddress):
473 if (NULL == plugin->sockv6)
476 cont (cont_cls, target, GNUNET_SYSERR);
481 GNUNET_malloc (sizeof (struct PeerSession) +
482 sizeof (struct sockaddr_in6));
483 v6 = (struct sockaddr_in6 *) &peer_session[1];
484 v6->sin6_family = AF_INET6;
485 #if HAVE_SOCKADDR_IN_SIN_LEN
486 v6->sin6_len = sizeof (struct sockaddr_in6);
488 v6->sin6_port = t6->u6_port;
489 v6->sin6_addr = t6->ipv6_addr;
492 /* Must have a valid address to send to */
494 return GNUNET_SYSERR;
496 udp = (struct UDPMessage *) mbuf;
497 udp->header.size = htons (mlen);
498 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
499 udp->reserved = htonl (0);
500 udp->sender = *plugin->env->my_identity;
501 memcpy (&udp[1], msgbuf, msgbuf_size);
502 peer_session->target = *target;
503 peer_session->plugin = plugin;
504 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
505 peer_session->cont = cont;
506 peer_session->cont_cls = cont_cls;
509 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
511 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
512 GNUNET_free_non_null (peer_session);
516 GNUNET_assert (GNUNET_OK ==
517 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
520 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
522 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
524 plugin->last_expected_delay,
525 &udp->header, &send_fragment,
533 * Closure for 'process_inbound_tokenized_messages'
535 struct SourceInformation
540 struct GNUNET_PeerIdentity sender;
548 * Number of bytes in source address.
555 * Message tokenizer has broken up an incomming message. Pass it on
558 * @param cls the 'struct Plugin'
559 * @param client the 'struct SourceInformation'
560 * @param hdr the actual message
563 process_inbound_tokenized_messages (void *cls, void *client,
564 const struct GNUNET_MessageHeader *hdr)
566 struct Plugin *plugin = cls;
567 struct SourceInformation *si = client;
568 struct GNUNET_TRANSPORT_ATS_Information distance[2];
571 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
572 distance[0].value = htonl (1);
573 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
574 distance[1].value = htonl (0);
576 plugin->env->receive (plugin->env->cls, &si->sender, hdr, distance, 2, NULL,
582 * We've received a UDP Message. Process it (pass contents to main service).
584 * @param plugin plugin context
585 * @param msg the message
586 * @param sender_addr sender address
587 * @param sender_addr_len number of bytes in sender_addr
590 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
591 const struct sockaddr *sender_addr,
592 socklen_t sender_addr_len)
594 struct SourceInformation si;
595 struct IPv4UdpAddress u4;
596 struct IPv6UdpAddress u6;
600 if (0 != ntohl (msg->reserved))
605 if (ntohs (msg->header.size) <
606 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
612 /* convert address */
613 switch (sender_addr->sa_family)
616 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
617 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
618 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
623 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
624 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
625 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
634 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
635 "Received message with %u bytes from peer `%s' at `%s'\n",
636 (unsigned int) ntohs (msg->header.size),
637 GNUNET_i2s (&msg->sender), GNUNET_a2s (sender_addr,
641 /* iterate over all embedded messages */
642 si.sender = msg->sender;
645 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
646 ntohs (msg->header.size) -
647 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
652 * Process a defragmented message.
654 * @param cls the 'struct ReceiveContext'
655 * @param msg the message
658 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
660 struct ReceiveContext *rc = cls;
662 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
667 if (ntohs (msg->size) < sizeof (struct UDPMessage))
672 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
673 rc->src_addr, rc->addr_len);
678 * Transmit an acknowledgement.
680 * @param cls the 'struct ReceiveContext'
681 * @param id message ID (unused)
682 * @param msg ack to transmit
685 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
687 struct ReceiveContext *rc = cls;
688 size_t msize = sizeof (struct UDPMessage) + ntohs (msg->size);
690 struct UDPMessage *udp;
693 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "Sending ACK to `%s'\n",
694 GNUNET_a2s (rc->src_addr,
695 (rc->src_addr->sa_family ==
696 AF_INET) ? sizeof (struct sockaddr_in) :
697 sizeof (struct sockaddr_in6)));
699 udp = (struct UDPMessage *) buf;
700 udp->header.size = htons ((uint16_t) msize);
701 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
702 udp->reserved = htonl (0);
703 udp->sender = *rc->plugin->env->my_identity;
704 memcpy (&udp[1], msg, ntohs (msg->size));
705 (void) udp_send (rc->plugin, rc->src_addr, &udp->header);
710 * Closure for 'find_receive_context'.
712 struct FindReceiveContext
715 * Where to store the result.
717 struct ReceiveContext *rc;
722 const struct sockaddr *addr;
725 * Number of bytes in 'addr'.
732 * Scan the heap for a receive context with the given address.
734 * @param cls the 'struct FindReceiveContext'
735 * @param node internal node of the heap
736 * @param element value stored at the node (a 'struct ReceiveContext')
737 * @param cost cost associated with the node
738 * @return GNUNET_YES if we should continue to iterate,
742 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
743 void *element, GNUNET_CONTAINER_HeapCostType cost)
745 struct FindReceiveContext *frc = cls;
746 struct ReceiveContext *e = element;
748 if ((frc->addr_len == e->addr_len) &&
749 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
759 * Read and process a message from the given socket.
761 * @param plugin the overall plugin
762 * @param rsock socket to read from
765 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
771 const struct GNUNET_MessageHeader *msg;
772 const struct GNUNET_MessageHeader *ack;
773 struct PeerSession *peer_session;
774 const struct UDPMessage *udp;
775 struct ReceiveContext *rc;
776 struct GNUNET_TIME_Absolute now;
777 struct FindReceiveContext frc;
779 fromlen = sizeof (addr);
780 memset (&addr, 0, sizeof (addr));
782 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
783 (struct sockaddr *) &addr, &fromlen);
784 if (ret < sizeof (struct GNUNET_MessageHeader))
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "UDP received %u-byte message from `%s'\n", (unsigned int) ret,
792 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
794 msg = (const struct GNUNET_MessageHeader *) buf;
795 if (ret != ntohs (msg->size))
800 switch (ntohs (msg->type))
802 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
803 if (ntohs (msg->size) < sizeof (struct UDPMessage))
808 process_udp_message (plugin, (const struct UDPMessage *) msg,
809 (const struct sockaddr *) addr, fromlen);
811 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
812 if (ntohs (msg->size) <
813 sizeof (struct UDPMessage) + sizeof (struct GNUNET_MessageHeader))
818 udp = (const struct UDPMessage *) msg;
819 if (ntohl (udp->reserved) != 0)
824 ack = (const struct GNUNET_MessageHeader *) &udp[1];
825 if (ntohs (ack->size) != ntohs (msg->size) - sizeof (struct UDPMessage))
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
833 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp->sender),
834 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
837 peer_session = find_session (plugin, &udp->sender);
838 if (NULL == peer_session)
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Session for ACK not found, dropping ACK!\n");
846 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
848 GNUNET_assert (GNUNET_OK ==
849 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
853 plugin->last_expected_delay =
854 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
855 if (peer_session->cont != NULL)
856 peer_session->cont (peer_session->cont_cls, &udp->sender, GNUNET_OK);
857 GNUNET_free (peer_session);
859 case GNUNET_MESSAGE_TYPE_FRAGMENT:
861 frc.addr = (const struct sockaddr *) addr;
862 frc.addr_len = fromlen;
863 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
865 now = GNUNET_TIME_absolute_get ();
869 /* need to create a new RC */
870 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
871 memcpy (&rc[1], addr, fromlen);
872 rc->src_addr = (const struct sockaddr *) &rc[1];
873 rc->addr_len = fromlen;
876 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
877 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
878 &fragment_msg_proc, &ack_proc);
880 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
881 (GNUNET_CONTAINER_HeapCostType)
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "UDP processes %u-byte fragment from `%s'\n",
887 (unsigned int) ntohs (msg->size),
888 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
891 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
893 /* keep this 'rc' from expiring */
894 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
895 (GNUNET_CONTAINER_HeapCostType)
898 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
899 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
901 /* remove 'rc' that was inactive the longest */
902 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
903 GNUNET_assert (NULL != rc);
904 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
916 * We have been notified that our writeset has something to read. We don't
917 * know which socket needs to be read, so we have to check each one
918 * Then reschedule this function to be called again once more is available.
920 * @param cls the plugin handle
921 * @param tc the scheduling context (for rescheduling this function again)
924 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
926 struct Plugin *plugin = cls;
928 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
929 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
931 if ((NULL != plugin->sockv4) &&
932 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
933 udp_read (plugin, plugin->sockv4);
934 if ((NULL != plugin->sockv6) &&
935 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
936 udp_read (plugin, plugin->sockv6);
937 plugin->select_task =
938 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
939 GNUNET_SCHEDULER_NO_TASK,
940 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
941 NULL, &udp_plugin_select, plugin);
947 * Check if the given port is plausible (must be either our listen
948 * port or our advertised port). If it is neither, we return
951 * @param plugin global variables
952 * @param in_port port number to check
953 * @return GNUNET_OK if port is either open_port or adv_port
956 check_port (struct Plugin *plugin, uint16_t in_port)
958 if ((in_port == plugin->port) || (in_port == plugin->aport))
960 return GNUNET_SYSERR;
965 * Function that will be called to check if a binary address for this
966 * plugin is well-formed and corresponds to an address for THIS peer
967 * (as per our configuration). Naturally, if absolutely necessary,
968 * plugins can be a bit conservative in their answer, but in general
969 * plugins should make sure that the address does not redirect
970 * traffic to a 3rd party that might try to man-in-the-middle our
973 * @param cls closure, should be our handle to the Plugin
974 * @param addr pointer to the address
975 * @param addrlen length of addr
976 * @return GNUNET_OK if this is a plausible address for this peer
977 * and transport, GNUNET_SYSERR if not
981 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
983 struct Plugin *plugin = cls;
984 struct IPv4UdpAddress *v4;
985 struct IPv6UdpAddress *v6;
987 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
988 (addrlen != sizeof (struct IPv6UdpAddress)))
991 return GNUNET_SYSERR;
993 if (addrlen == sizeof (struct IPv4UdpAddress))
995 v4 = (struct IPv4UdpAddress *) addr;
996 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
997 return GNUNET_SYSERR;
999 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1000 sizeof (struct in_addr)))
1001 return GNUNET_SYSERR;
1005 v6 = (struct IPv6UdpAddress *) addr;
1006 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1008 GNUNET_break_op (0);
1009 return GNUNET_SYSERR;
1011 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1012 return GNUNET_SYSERR;
1014 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1015 sizeof (struct in6_addr)))
1016 return GNUNET_SYSERR;
1023 * Function called for a quick conversion of the binary address to
1024 * a numeric address. Note that the caller must not free the
1025 * address and that the next call to this function is allowed
1026 * to override the address again.
1028 * @param cls closure
1029 * @param addr binary address
1030 * @param addrlen length of the address
1031 * @return string representing the same address
1034 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1036 static char rbuf[INET6_ADDRSTRLEN + 10];
1037 char buf[INET6_ADDRSTRLEN];
1041 const struct IPv4UdpAddress *t4;
1042 const struct IPv6UdpAddress *t6;
1046 if (addrlen == sizeof (struct IPv6UdpAddress))
1050 port = ntohs (t6->u6_port);
1051 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1054 else if (addrlen == sizeof (struct IPv4UdpAddress))
1058 port = ntohs (t4->u4_port);
1059 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1064 GNUNET_break_op (0);
1067 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1068 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s:%u", buf, port);
1074 * Closure for 'append_port'.
1076 struct PrettyPrinterContext
1079 * Function to call with the result.
1081 GNUNET_TRANSPORT_AddressStringCallback asc;
1084 * Clsoure for 'asc'.
1089 * Port to add after the IP address.
1096 * Append our port and forward the result.
1098 * @param cls a 'struct PrettyPrinterContext'
1099 * @param hostname result from DNS resolver
1102 append_port (void *cls, const char *hostname)
1104 struct PrettyPrinterContext *ppc = cls;
1107 if (hostname == NULL)
1109 ppc->asc (ppc->asc_cls, NULL);
1113 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1114 ppc->asc (ppc->asc_cls, ret);
1120 * Convert the transports address to a nice, human-readable
1123 * @param cls closure
1124 * @param type name of the transport that generated the address
1125 * @param addr one of the addresses of the host, NULL for the last address
1126 * the specific address format depends on the transport
1127 * @param addrlen length of the address
1128 * @param numeric should (IP) addresses be displayed in numeric form?
1129 * @param timeout after how long should we give up?
1130 * @param asc function to call on each string
1131 * @param asc_cls closure for asc
1134 udp_plugin_address_pretty_printer (void *cls, const char *type,
1135 const void *addr, size_t addrlen,
1137 struct GNUNET_TIME_Relative timeout,
1138 GNUNET_TRANSPORT_AddressStringCallback asc,
1141 struct PrettyPrinterContext *ppc;
1144 struct sockaddr_in a4;
1145 struct sockaddr_in6 a6;
1146 const struct IPv4UdpAddress *u4;
1147 const struct IPv6UdpAddress *u6;
1150 if (addrlen == sizeof (struct IPv6UdpAddress))
1153 memset (&a6, 0, sizeof (a6));
1154 a6.sin6_family = AF_INET6;
1155 #if HAVE_SOCKADDR_IN_SIN_LEN
1156 a6.sin6_len = sizeof (a6);
1158 a6.sin6_port = u6->u6_port;
1159 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1160 port = ntohs (u6->u6_port);
1164 else if (addrlen == sizeof (struct IPv4UdpAddress))
1167 memset (&a4, 0, sizeof (a4));
1168 a4.sin_family = AF_INET;
1169 #if HAVE_SOCKADDR_IN_SIN_LEN
1170 a4.sin_len = sizeof (a4);
1172 a4.sin_port = u4->u4_port;
1173 a4.sin_addr.s_addr = u4->ipv4_addr;
1174 port = ntohs (u4->u4_port);
1180 /* invalid address */
1181 GNUNET_break_op (0);
1182 asc (asc_cls, NULL);
1185 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1187 ppc->asc_cls = asc_cls;
1189 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1194 * Our external IP address/port mapping has changed.
1196 * @param cls closure, the 'struct LocalAddrList'
1197 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1198 * the previous (now invalid) one
1199 * @param addr either the previous or the new public IP address
1200 * @param addrlen actual lenght of the address
1203 udp_nat_port_map_callback (void *cls, int add_remove,
1204 const struct sockaddr *addr, socklen_t addrlen)
1206 struct Plugin *plugin = cls;
1207 struct IPv4UdpAddress u4;
1208 struct IPv6UdpAddress u6;
1212 /* convert 'addr' to our internal format */
1213 switch (addr->sa_family)
1216 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1217 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1218 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1223 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1224 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1225 sizeof (struct in6_addr));
1226 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1234 /* modify our published address list */
1235 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1240 * The exported method. Makes the core api available via a global and
1241 * returns the udp transport API.
1243 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1244 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1247 libgnunet_plugin_transport_udp_init (void *cls)
1249 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1250 unsigned long long port;
1251 unsigned long long aport;
1252 struct GNUNET_TRANSPORT_PluginFunctions *api;
1253 struct Plugin *plugin;
1254 int sockets_created;
1255 struct sockaddr_in serverAddrv4;
1256 struct sockaddr_in6 serverAddrv6;
1257 struct sockaddr *serverAddr;
1258 struct sockaddr *addrs[2];
1259 socklen_t addrlens[2];
1262 unsigned long long udp_max_bps;
1265 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1269 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1270 "MAX_BPS", &udp_max_bps))
1271 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1273 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1274 "ADVERTISED_PORT", &aport))
1278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1279 _("Given `%s' option is out of range: %llu > %u\n"), "PORT",
1283 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1284 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1286 plugin = GNUNET_malloc (sizeof (struct Plugin));
1287 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1288 GNUNET_BANDWIDTH_value_init ((uint32_t)
1290 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1291 plugin->port = port;
1292 plugin->aport = aport;
1294 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1297 api->send = &udp_plugin_send;
1298 api->disconnect = &udp_disconnect;
1299 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1300 api->address_to_string = &udp_address_to_string;
1301 api->check_address = &udp_plugin_check_address;
1304 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1305 "BINDTO", &plugin->bind4_address))
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "Binding udp plugin to specific address: `%s'\n",
1309 plugin->bind4_address);
1310 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
1312 GNUNET_free (plugin->bind4_address);
1313 GNUNET_free (plugin);
1320 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1321 "BINDTO6", &plugin->bind6_address))
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Binding udp plugin to specific address: `%s'\n",
1325 plugin->bind6_address);
1327 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1330 plugin->bind6_address);
1331 GNUNET_free_non_null (plugin->bind4_address);
1332 GNUNET_free (plugin->bind6_address);
1333 GNUNET_free (plugin);
1340 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1342 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1344 sockets_created = 0;
1346 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
1349 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1350 if (NULL == plugin->sockv6)
1352 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1356 #if HAVE_SOCKADDR_IN_SIN_LEN
1357 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1359 serverAddrv6.sin6_family = AF_INET6;
1360 serverAddrv6.sin6_addr = in6addr_any;
1361 serverAddrv6.sin6_port = htons (plugin->port);
1362 addrlen = sizeof (serverAddrv6);
1363 serverAddr = (struct sockaddr *) &serverAddrv6;
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1366 ntohs (serverAddrv6.sin6_port));
1369 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1372 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "IPv6 Binding failed, trying new port %d\n",
1376 ntohs (serverAddrv6.sin6_port));
1381 GNUNET_NETWORK_socket_close (plugin->sockv6);
1382 plugin->sockv6 = NULL;
1386 if (plugin->sockv6 != NULL)
1388 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
1389 addrlens[sockets_created] = sizeof (serverAddrv6);
1396 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1397 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1398 if (NULL == plugin->sockv4)
1400 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1404 #if HAVE_SOCKADDR_IN_SIN_LEN
1405 serverAddrv4.sin_len = sizeof (serverAddrv4);
1407 serverAddrv4.sin_family = AF_INET;
1408 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1409 serverAddrv4.sin_port = htons (plugin->port);
1410 addrlen = sizeof (serverAddrv4);
1411 serverAddr = (struct sockaddr *) &serverAddrv4;
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1414 ntohs (serverAddrv4.sin_port));
1417 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1420 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1423 "IPv4 Binding failed, trying new port %d\n",
1424 ntohs (serverAddrv4.sin_port));
1429 GNUNET_NETWORK_socket_close (plugin->sockv4);
1430 plugin->sockv4 = NULL;
1434 if (plugin->sockv4 != NULL)
1436 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
1437 addrlens[sockets_created] = sizeof (serverAddrv4);
1442 plugin->rs = GNUNET_NETWORK_fdset_create ();
1443 GNUNET_NETWORK_fdset_zero (plugin->rs);
1444 if (NULL != plugin->sockv4)
1445 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1446 if (NULL != plugin->sockv6)
1447 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1449 plugin->select_task =
1450 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1451 GNUNET_SCHEDULER_NO_TASK,
1452 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1453 NULL, &udp_plugin_select, plugin);
1454 if (sockets_created == 0)
1455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1457 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
1458 (const struct sockaddr **) addrs, addrlens,
1459 &udp_nat_port_map_callback, NULL, plugin);
1465 * Destroy a session, plugin is being unloaded.
1468 * @param key hash of public key of target peer
1469 * @param value a 'struct PeerSession*' to clean up
1470 * @return GNUNET_OK (continue to iterate)
1473 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
1475 struct PeerSession *peer_session = value;
1477 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1478 GNUNET_free (peer_session);
1484 * Shutdown the plugin.
1486 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
1490 libgnunet_plugin_transport_udp_done (void *cls)
1492 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1493 struct Plugin *plugin = api->cls;
1494 struct ReceiveContext *rc;
1496 /* FIXME: clean up heap and hashmap */
1497 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
1499 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1500 plugin->sessions = NULL;
1501 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
1503 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1506 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
1508 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1510 GNUNET_SCHEDULER_cancel (plugin->select_task);
1511 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1513 if (plugin->sockv4 != NULL)
1515 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1516 plugin->sockv4 = NULL;
1518 if (plugin->sockv6 != NULL)
1520 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1521 plugin->sockv6 = NULL;
1523 GNUNET_SERVER_mst_destroy (plugin->mst);
1524 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1525 GNUNET_NAT_unregister (plugin->nat);
1527 GNUNET_free (plugin);
1532 /* end of plugin_transport_udp.c */