2 This file is part of GNUnet
3 (C) 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_udp.c
23 * @brief Implementation of the UDP NAT punching
25 * @author Christian Grothoff
26 * @author Nathan Evans
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fragmentation_lib.h"
32 #include "gnunet_nat_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_constants.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
45 #define DEBUG_UDP GNUNET_EXTRA_LOGGING
48 * MTU for fragmentation subsystem. Should be conservative since
49 * all communicating peers MUST work with this MTU.
54 * Number of messages we can defragment in parallel. We only really
55 * defragment 1 message at a time, but if messages get re-ordered, we
56 * may want to keep knowledge about the previous message to avoid
57 * discarding the current message in favor of a single fragment of a
58 * previous message. 3 should be good since we don't expect massive
59 * message reorderings with UDP.
61 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
64 * We keep a defragmentation queue per sender address. How many
65 * sender addresses do we support at the same time? Memory consumption
66 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
67 * value. (So 128 corresponds to 12 MB and should suffice for
68 * connecting to roughly 128 peers via UDP).
70 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
74 * UDP Message-Packet header (after defragmentation).
81 struct GNUNET_MessageHeader header;
84 * Always zero for now.
89 * What is the identity of the sender
91 struct GNUNET_PeerIdentity sender;
97 * UDP ACK Message-Packet header (after defragmentation).
99 struct UDP_ACK_Message
104 struct GNUNET_MessageHeader header;
107 * Desired delay for flow control
112 * What is the identity of the sender
114 struct GNUNET_PeerIdentity sender;
119 * Network format for IPv4 addresses.
121 struct IPv4UdpAddress
124 * IPv4 address, in network byte order.
126 uint32_t ipv4_addr GNUNET_PACKED;
129 * Port number, in network byte order.
131 uint16_t u4_port GNUNET_PACKED;
136 * Network format for IPv6 addresses.
138 struct IPv6UdpAddress
144 struct in6_addr ipv6_addr GNUNET_PACKED;
147 * Port number, in network byte order.
149 uint16_t u6_port GNUNET_PACKED;
153 /* Forward definition */
158 * Session with another peer. FIXME: why not make this into
159 * a regular 'struct Session' and pass it around!?
165 * Which peer is this session for or from?
167 struct GNUNET_PeerIdentity target;
170 * Pointer to the global plugin struct.
172 struct Plugin *plugin;
175 * Address of the other peer
177 const struct sockaddr *sock_addr;
182 * Function to call upon completion of the transmission.
184 GNUNET_TRANSPORT_TransmitContinuation cont;
187 * Closure for 'cont'.
192 * Current outgoing message to this peer.
194 struct GNUNET_FRAGMENT_Context *frag;
197 * Is this a reliable, bidirectional connection?
198 * YES if we already received a message with this session
203 * Task invalidating this session if idle
205 GNUNET_SCHEDULER_TaskIdentifier invalidation_task;
208 * Desired delay for next sending we send to other peer
210 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
213 * Desired delay for next sending we received from other peer
215 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
219 * Encapsulation of all of the state of the plugin.
227 struct GNUNET_TRANSPORT_PluginEnvironment *env;
230 * Session of peers with whom we are currently connected,
231 * map of peer identity to 'struct PeerSession'.
233 struct GNUNET_CONTAINER_MultiHashMap *sessions;
236 * Heap with all of our defragmentation activities.
238 struct GNUNET_CONTAINER_Heap *defrags;
243 GNUNET_SCHEDULER_TaskIdentifier select_task;
246 * Tokenizer for inbound messages.
248 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
251 * Bandwidth tracker to limit global UDP traffic.
253 struct GNUNET_BANDWIDTH_Tracker tracker;
256 * Address we were told to bind to exclusively (IPv4).
261 * Address we were told to bind to exclusively (IPv6).
266 * Handle to NAT traversal support.
268 struct GNUNET_NAT_Handle *nat;
273 struct GNUNET_NETWORK_FDSet *rs;
276 * The read socket for IPv4
278 struct GNUNET_NETWORK_Handle *sockv4;
281 * The read socket for IPv6
283 struct GNUNET_NETWORK_Handle *sockv6;
286 * expected delay for ACKs
288 struct GNUNET_TIME_Relative last_expected_delay;
296 * Port we advertise on.
302 int delete_session_iterator (void *cls,
303 const GNUNET_HashCode * key,
306 struct Session *s = (struct Session *) value;
307 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
309 GNUNET_SCHEDULER_cancel(s->invalidation_task);
310 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
312 GNUNET_CONTAINER_multihashmap_remove(s->plugin->sessions, key, s);
318 * Disconnect from a remote node. Clean up session if we have one for this peer
320 * @param cls closure for this call (should be handle to Plugin)
321 * @param target the peeridentity of the peer to disconnect
322 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
325 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
327 struct Plugin *plugin = cls;
329 GNUNET_CONTAINER_multihashmap_get_multiple(plugin, &target->hashPubKey, &delete_session_iterator, NULL);
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)
371 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
372 LOG (GNUNET_ERROR_TYPE_ERROR,
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");
378 LOG (GNUNET_ERROR_TYPE_DEBUG,
379 "UDP transmited %u-byte message to %s (%d: %s)\n",
380 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
381 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
386 static struct Session *
387 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
388 const void *addr, size_t addrlen,
389 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
392 const struct IPv4UdpAddress *t4;
393 const struct IPv6UdpAddress *t6;
394 struct sockaddr_in *v4;
395 struct sockaddr_in6 *v6;
400 case sizeof (struct IPv4UdpAddress):
401 if (NULL == plugin->sockv4)
407 GNUNET_malloc (sizeof (struct Session) +
408 sizeof (struct sockaddr_in));
409 len = sizeof (struct sockaddr_in);
410 v4 = (struct sockaddr_in *) &s[1];
411 v4->sin_family = AF_INET;
412 #if HAVE_SOCKADDR_IN_SIN_LEN
413 v4->sin_len = sizeof (struct sockaddr_in);
415 v4->sin_port = t4->u4_port;
416 v4->sin_addr.s_addr = t4->ipv4_addr;
418 case sizeof (struct IPv6UdpAddress):
419 if (NULL == plugin->sockv6)
425 GNUNET_malloc (sizeof (struct Session) +
426 sizeof (struct sockaddr_in6));
427 len = sizeof (struct sockaddr_in6);
428 v6 = (struct sockaddr_in6 *) &s[1];
429 v6->sin6_family = AF_INET6;
430 #if HAVE_SOCKADDR_IN_SIN_LEN
431 v6->sin6_len = sizeof (struct sockaddr_in6);
433 v6->sin6_port = t6->u6_port;
434 v6->sin6_addr = t6->ipv6_addr;
437 /* Must have a valid address to send to */
442 s->valid_until = GNUNET_TIME_absolute_get_zero ();
443 s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero ();
444 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero ();
445 s->bidirectional = GNUNET_NO;
446 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
450 s->sock_addr = (const struct sockaddr *) &s[1];
452 s->cont_cls = cont_cls;
458 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
460 struct Session * s = cls;
461 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
462 LOG (GNUNET_ERROR_TYPE_ERROR,
463 "Session %X (`%s') is now invalid\n", s, GNUNET_a2s (s->sock_addr,s->addrlen));
465 s->plugin->env->session_end(s->plugin->env->cls, &s->target, s);
466 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(s->plugin->sessions, &s->target.hashPubKey, s));
471 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
474 * Function that is called with messages created by the fragmentation
475 * module. In the case of the 'proc' callback of the
476 * GNUNET_FRAGMENT_context_create function, this function must
477 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
479 * @param cls closure, the 'struct PeerSession'
480 * @param msg the message that was created
483 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
485 struct Session *s = cls;
487 udp_send (session->plugin, session->sock_addr, msg);
488 GNUNET_FRAGMENT_context_transmission_done (session->frag);
492 * Function that can be used by the transport service to transmit
493 * a message using the plugin.
496 * @param target who should receive this message (ignored by UDP)
497 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
498 * @param msgbuf_size the size of the msgbuf to send
499 * @param priority how important is the message (ignored by UDP)
500 * @param timeout when should we time out (give up) if we can not transmit?
501 * @param session identifier used for this session (NULL for UDP)
502 * @param addr the addr to send the message to
503 * @param addrlen the len of addr
504 * @param force_address not used, we had better have an address to send to
505 * because we are stateless!!
506 * @param cont continuation to call once the message has
507 * been transmitted (or if the transport is ready
508 * for the next transmission call; or if the
509 * peer disconnected...)
510 * @param cont_cls closure for cont
512 * @return the number of bytes written (may return 0 and the message can
513 * still be transmitted later!)
516 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
517 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
518 struct GNUNET_TIME_Relative timeout, struct Session *session,
519 const void *addr, size_t addrlen, int force_address,
520 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
522 struct Plugin *plugin = cls;
524 const struct IPv4UdpAddress *t4;
525 const struct IPv6UdpAddress *t6;
526 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
528 struct UDPMessage *udp;
530 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
533 return GNUNET_SYSERR;
536 LOG (GNUNET_ERROR_TYPE_DEBUG,
537 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
538 msgbuf_size, GNUNET_i2s (target),
539 udp_address_to_string (NULL, addr, addrlen),
540 session, force_address);
542 /* no valid address given */
543 if ((addr = NULL) || (addrlen == 0))
546 return GNUNET_SYSERR;
551 s = find_session (target, addr, addrlen);
554 /* found a conflicting session for this peer */
556 return GNUNET_SYSERR;
561 /* use a reliable transmission but no session given */
562 if (force_address == GNUNET_SYSERR)
563 return GNUNET_SYSERR;
564 /* create new session */
565 s = create_session(plugin, target, addr, addrlen, cont, cont_cls);
566 GNUNET_CONTAINER_multihashmap_put(plugin->sessions, &target->hashPubKey, s, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
567 /* schedule invalidation task */
568 s->invalidation_task = GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &invalidation_task, NULL);
572 udp = (struct UDPMessage *) mbuf;
573 udp->header.size = htons (mlen);
574 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
575 udp->reserved = htonl (0);
576 udp->sender = *plugin->env->my_identity;
577 memcpy (&udp[1], msgbuf, msgbuf_size);
579 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
580 if (s->flow_delay_from_other_peer.abs_value > now.abs_value)
582 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(now, s->flow_delay_from_other_peer);
583 LOG (GNUNET_ERROR_TYPE_DEBUG,
584 "We try to send to early! Should in %llu!\n", delta.rel_value);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "We can send!\n");
590 /* send without fragmentation */
593 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
595 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
596 GNUNET_free_non_null (peer_session);
598 /* send with fragmentation */
601 s->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
603 plugin->last_expected_delay,
604 &udp->header, &send_fragment,
612 * Message tokenizer has broken up an incomming message. Pass it on
615 * @param cls the 'struct Plugin'
616 * @param client the Session
617 * @param hdr the actual message
620 process_inbound_tokenized_messages (void *cls, void *client,
621 const struct GNUNET_MessageHeader *hdr)
623 struct Plugin *plugin = cls;
624 struct Session * s = client;
625 struct GNUNET_ATS_Information distance;
626 struct GNUNET_TIME_Relative delay;
629 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
630 distance.value = htonl (1);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "Giving Session %X %s to transport\n", s->session, GNUNET_i2s(&s->target));
634 delay = plugin->env->receive (plugin->env->cls, &s->target, hdr, &distance, 1, s,
635 // FIXME: USE UDP ADDRESSES!!!!
636 s->sock_addr, s->addrlen);
637 s->flow_delay_for_other_peer = delay;
642 * We've received a UDP Message. Process it (pass contents to main service).
644 * @param plugin plugin context
645 * @param msg the message
646 * @param sender_addr sender address
647 * @param sender_addr_len number of bytes in sender_addr
650 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
651 const struct sockaddr *sender_addr,
652 socklen_t sender_addr_len)
654 struct IPv4UdpAddress u4;
655 struct IPv6UdpAddress u6;
659 if (0 != ntohl (msg->reserved))
664 if (ntohs (msg->header.size) <
665 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
671 /* convert address */
672 switch (sender_addr->sa_family)
675 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
676 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
677 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
682 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
683 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
684 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
693 LOG (GNUNET_ERROR_TYPE_DEBUG,
694 "Received message with %u bytes from peer `%s' at `%s'\n",
695 (unsigned int) ntohs (msg->header.size),
696 GNUNET_i2s (&msg->sender), GNUNET_a2s (sender_addr,
700 const struct UDPMessage * udp_msg = (const struct UDPMessage *) msg;
701 LOG (GNUNET_ERROR_TYPE_DEBUG,
702 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
703 GNUNET_i2s (&udp_msg->sender),
704 udp_address_to_string(NULL, arg, args));
706 /* create a session for inbound connections */
707 struct Session * s = NULL;
708 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr, sender_addr_len);
712 LOG (GNUNET_ERROR_TYPE_DEBUG,
713 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
715 GNUNET_i2s (&s->target),
716 udp_address_to_string(NULL, arg, args));
720 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
721 LOG (GNUNET_ERROR_TYPE_DEBUG,
722 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
724 GNUNET_i2s (&s->target),
725 udp_address_to_string(NULL, arg, args));
727 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
728 &s->target.hashPubKey,
730 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
732 s->valid_until = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
733 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
735 GNUNET_SCHEDULER_cancel(s->invalidation_task);
736 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
737 LOG (GNUNET_ERROR_TYPE_DEBUG,
738 "Rescheduling %X' `%s'\n",
739 s, udp_address_to_string(NULL, arg, args));
741 s->invalidation_task = GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &invalidation_task, s);
742 /* we received from peer, so session is bidirectional and reliable */
743 s->bidirectional = GNUNET_YES;
745 GNUNET_SERVER_mst_receive (plugin->mst, &s, (const char *) &msg[1],
746 ntohs (msg->header.size) -
747 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
752 * Read and process a message from the given socket.
754 * @param plugin the overall plugin
755 * @param rsock socket to read from
758 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
764 const struct GNUNET_MessageHeader *msg;
765 const struct GNUNET_MessageHeader *ack;
766 struct Session *peer_session;
767 const struct UDP_ACK_Message *udp_ack;
768 struct ReceiveContext *rc;
769 struct GNUNET_TIME_Absolute now;
770 struct FindReceiveContext frc;
771 struct Session * s = NULL;
772 struct GNUNET_TIME_Relative flow_delay;
774 fromlen = sizeof (addr);
775 memset (&addr, 0, sizeof (addr));
777 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
778 (struct sockaddr *) &addr, &fromlen);
779 if (ret < sizeof (struct GNUNET_MessageHeader))
784 msg = (const struct GNUNET_MessageHeader *) buf;
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
788 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs(msg->type));
790 if (ret != ntohs (msg->size))
796 switch (ntohs (msg->type))
798 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
799 if (ntohs (msg->size) < sizeof (struct UDPMessage))
804 process_udp_message (plugin, (const struct UDPMessage *) msg,
805 (const struct sockaddr *) addr, fromlen);
808 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
809 if (ntohs (msg->size) <
810 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
815 udp_ack = (const struct UDP_ACK_Message *) msg;
816 LOG (GNUNET_ERROR_TYPE_DEBUG,
817 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
818 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp->sender),
819 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
821 // TODO Guess a session from address, give it defragmenter and check in process_udp if guess was right
822 s = guess_session (plugin->sessions, addr, addrlen);
823 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
827 case GNUNET_MESSAGE_TYPE_FRAGMENT:
828 LOG (GNUNET_ERROR_TYPE_DEBUG,
829 "UDP processes %u-byte fragment from `%s'\n",
830 (unsigned int) ntohs (msg->size),
831 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
832 // TODO Guess a session from address, give it defragmenter and check in process_udp if guess was right
833 s = guess_session (plugin->sessions, addr, addrlen);
834 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
845 * We have been notified that our writeset has something to read. We don't
846 * know which socket needs to be read, so we have to check each one
847 * Then reschedule this function to be called again once more is available.
849 * @param cls the plugin handle
850 * @param tc the scheduling context (for rescheduling this function again)
853 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
855 struct Plugin *plugin = cls;
857 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
858 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
860 if ((NULL != plugin->sockv4) &&
861 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
862 udp_read (plugin, plugin->sockv4);
863 if ((NULL != plugin->sockv6) &&
864 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
865 udp_read (plugin, plugin->sockv6);
866 plugin->select_task =
867 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
868 GNUNET_SCHEDULER_NO_TASK,
869 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
870 NULL, &udp_plugin_select, plugin);
876 * Check if the given port is plausible (must be either our listen
877 * port or our advertised port). If it is neither, we return
880 * @param plugin global variables
881 * @param in_port port number to check
882 * @return GNUNET_OK if port is either open_port or adv_port
885 check_port (struct Plugin *plugin, uint16_t in_port)
887 if ((in_port == plugin->port) || (in_port == plugin->aport))
889 return GNUNET_SYSERR;
894 * Function that will be called to check if a binary address for this
895 * plugin is well-formed and corresponds to an address for THIS peer
896 * (as per our configuration). Naturally, if absolutely necessary,
897 * plugins can be a bit conservative in their answer, but in general
898 * plugins should make sure that the address does not redirect
899 * traffic to a 3rd party that might try to man-in-the-middle our
902 * @param cls closure, should be our handle to the Plugin
903 * @param addr pointer to the address
904 * @param addrlen length of addr
905 * @return GNUNET_OK if this is a plausible address for this peer
906 * and transport, GNUNET_SYSERR if not
910 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
912 struct Plugin *plugin = cls;
913 struct IPv4UdpAddress *v4;
914 struct IPv6UdpAddress *v6;
916 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
917 (addrlen != sizeof (struct IPv6UdpAddress)))
920 return GNUNET_SYSERR;
922 if (addrlen == sizeof (struct IPv4UdpAddress))
924 v4 = (struct IPv4UdpAddress *) addr;
925 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
926 return GNUNET_SYSERR;
928 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
929 sizeof (struct in_addr)))
930 return GNUNET_SYSERR;
934 v6 = (struct IPv6UdpAddress *) addr;
935 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
938 return GNUNET_SYSERR;
940 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
941 return GNUNET_SYSERR;
943 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
944 sizeof (struct in6_addr)))
945 return GNUNET_SYSERR;
952 * Function called for a quick conversion of the binary address to
953 * a numeric address. Note that the caller must not free the
954 * address and that the next call to this function is allowed
955 * to override the address again.
958 * @param addr binary address
959 * @param addrlen length of the address
960 * @return string representing the same address
963 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
965 static char rbuf[INET6_ADDRSTRLEN + 10];
966 char buf[INET6_ADDRSTRLEN];
970 const struct IPv4UdpAddress *t4;
971 const struct IPv6UdpAddress *t6;
975 if (addrlen == sizeof (struct IPv6UdpAddress))
979 port = ntohs (t6->u6_port);
980 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
983 else if (addrlen == sizeof (struct IPv4UdpAddress))
987 port = ntohs (t4->u4_port);
988 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
996 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
997 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1004 * Closure for 'append_port'.
1006 struct PrettyPrinterContext
1009 * Function to call with the result.
1011 GNUNET_TRANSPORT_AddressStringCallback asc;
1014 * Clsoure for 'asc'.
1019 * Port to add after the IP address.
1026 * Append our port and forward the result.
1028 * @param cls a 'struct PrettyPrinterContext'
1029 * @param hostname result from DNS resolver
1032 append_port (void *cls, const char *hostname)
1034 struct PrettyPrinterContext *ppc = cls;
1037 if (hostname == NULL)
1039 ppc->asc (ppc->asc_cls, NULL);
1043 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1044 ppc->asc (ppc->asc_cls, ret);
1050 * Convert the transports address to a nice, human-readable
1053 * @param cls closure
1054 * @param type name of the transport that generated the address
1055 * @param addr one of the addresses of the host, NULL for the last address
1056 * the specific address format depends on the transport
1057 * @param addrlen length of the address
1058 * @param numeric should (IP) addresses be displayed in numeric form?
1059 * @param timeout after how long should we give up?
1060 * @param asc function to call on each string
1061 * @param asc_cls closure for asc
1064 udp_plugin_address_pretty_printer (void *cls, const char *type,
1065 const void *addr, size_t addrlen,
1067 struct GNUNET_TIME_Relative timeout,
1068 GNUNET_TRANSPORT_AddressStringCallback asc,
1071 struct PrettyPrinterContext *ppc;
1074 struct sockaddr_in a4;
1075 struct sockaddr_in6 a6;
1076 const struct IPv4UdpAddress *u4;
1077 const struct IPv6UdpAddress *u6;
1080 if (addrlen == sizeof (struct IPv6UdpAddress))
1083 memset (&a6, 0, sizeof (a6));
1084 a6.sin6_family = AF_INET6;
1085 #if HAVE_SOCKADDR_IN_SIN_LEN
1086 a6.sin6_len = sizeof (a6);
1088 a6.sin6_port = u6->u6_port;
1089 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1090 port = ntohs (u6->u6_port);
1094 else if (addrlen == sizeof (struct IPv4UdpAddress))
1097 memset (&a4, 0, sizeof (a4));
1098 a4.sin_family = AF_INET;
1099 #if HAVE_SOCKADDR_IN_SIN_LEN
1100 a4.sin_len = sizeof (a4);
1102 a4.sin_port = u4->u4_port;
1103 a4.sin_addr.s_addr = u4->ipv4_addr;
1104 port = ntohs (u4->u4_port);
1110 /* invalid address */
1111 GNUNET_break_op (0);
1112 asc (asc_cls, NULL);
1115 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1117 ppc->asc_cls = asc_cls;
1119 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1124 * Our external IP address/port mapping has changed.
1126 * @param cls closure, the 'struct LocalAddrList'
1127 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1128 * the previous (now invalid) one
1129 * @param addr either the previous or the new public IP address
1130 * @param addrlen actual lenght of the address
1133 udp_nat_port_map_callback (void *cls, int add_remove,
1134 const struct sockaddr *addr, socklen_t addrlen)
1136 struct Plugin *plugin = cls;
1137 struct IPv4UdpAddress u4;
1138 struct IPv6UdpAddress u6;
1142 /* convert 'addr' to our internal format */
1143 switch (addr->sa_family)
1146 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1147 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1148 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1153 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1154 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1155 sizeof (struct in6_addr));
1156 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1164 /* modify our published address list */
1165 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1170 * The exported method. Makes the core api available via a global and
1171 * returns the udp transport API.
1173 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1174 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1177 libgnunet_plugin_transport_udp_init (void *cls)
1179 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1180 unsigned long long port;
1181 unsigned long long aport;
1182 struct GNUNET_TRANSPORT_PluginFunctions *api;
1183 struct Plugin *plugin;
1184 int sockets_created;
1185 struct sockaddr_in serverAddrv4;
1186 struct sockaddr_in6 serverAddrv6;
1187 struct sockaddr *serverAddr;
1188 struct sockaddr *addrs[2];
1189 socklen_t addrlens[2];
1192 unsigned long long udp_max_bps;
1195 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1199 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1200 "MAX_BPS", &udp_max_bps))
1201 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1203 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1204 "ADVERTISED_PORT", &aport))
1208 LOG (GNUNET_ERROR_TYPE_WARNING,
1209 _("Given `%s' option is out of range: %llu > %u\n"), "PORT",
1213 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1214 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1216 plugin = GNUNET_malloc (sizeof (struct Plugin));
1217 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1218 GNUNET_BANDWIDTH_value_init ((uint32_t)
1220 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1221 plugin->port = port;
1222 plugin->aport = aport;
1224 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1227 api->send = &udp_plugin_send;
1228 api->disconnect = &udp_disconnect;
1229 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1230 api->address_to_string = &udp_address_to_string;
1231 api->check_address = &udp_plugin_check_address;
1234 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1235 "BINDTO", &plugin->bind4_address))
1237 LOG (GNUNET_ERROR_TYPE_DEBUG,
1238 "Binding udp plugin to specific address: `%s'\n",
1239 plugin->bind4_address);
1240 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
1242 GNUNET_free (plugin->bind4_address);
1243 GNUNET_free (plugin);
1250 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1251 "BINDTO6", &plugin->bind6_address))
1253 LOG (GNUNET_ERROR_TYPE_DEBUG,
1254 "Binding udp plugin to specific address: `%s'\n",
1255 plugin->bind6_address);
1257 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1259 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1260 plugin->bind6_address);
1261 GNUNET_free_non_null (plugin->bind4_address);
1262 GNUNET_free (plugin->bind6_address);
1263 GNUNET_free (plugin);
1270 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1272 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1274 plugin->inbound_sessions =
1275 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1277 sockets_created = 0;
1279 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
1282 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1283 if (NULL == plugin->sockv6)
1285 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1289 #if HAVE_SOCKADDR_IN_SIN_LEN
1290 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1292 serverAddrv6.sin6_family = AF_INET6;
1293 serverAddrv6.sin6_addr = in6addr_any;
1294 serverAddrv6.sin6_port = htons (plugin->port);
1295 addrlen = sizeof (serverAddrv6);
1296 serverAddr = (struct sockaddr *) &serverAddrv6;
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1299 ntohs (serverAddrv6.sin6_port));
1302 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1305 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "IPv6 Binding failed, trying new port %d\n",
1309 ntohs (serverAddrv6.sin6_port));
1314 GNUNET_NETWORK_socket_close (plugin->sockv6);
1315 plugin->sockv6 = NULL;
1319 if (plugin->sockv6 != NULL)
1321 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
1322 addrlens[sockets_created] = sizeof (serverAddrv6);
1329 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1330 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1331 if (NULL == plugin->sockv4)
1333 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1337 #if HAVE_SOCKADDR_IN_SIN_LEN
1338 serverAddrv4.sin_len = sizeof (serverAddrv4);
1340 serverAddrv4.sin_family = AF_INET;
1341 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1342 serverAddrv4.sin_port = htons (plugin->port);
1343 addrlen = sizeof (serverAddrv4);
1344 serverAddr = (struct sockaddr *) &serverAddrv4;
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1347 ntohs (serverAddrv4.sin_port));
1350 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1353 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "IPv4 Binding failed, trying new port %d\n",
1357 ntohs (serverAddrv4.sin_port));
1362 GNUNET_NETWORK_socket_close (plugin->sockv4);
1363 plugin->sockv4 = NULL;
1367 if (plugin->sockv4 != NULL)
1369 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
1370 addrlens[sockets_created] = sizeof (serverAddrv4);
1375 plugin->rs = GNUNET_NETWORK_fdset_create ();
1376 GNUNET_NETWORK_fdset_zero (plugin->rs);
1377 if (NULL != plugin->sockv4)
1378 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1379 if (NULL != plugin->sockv6)
1380 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1382 plugin->select_task =
1383 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1384 GNUNET_SCHEDULER_NO_TASK,
1385 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1386 NULL, &udp_plugin_select, plugin);
1387 if (sockets_created == 0)
1388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1390 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
1391 (const struct sockaddr **) addrs, addrlens,
1392 &udp_nat_port_map_callback, NULL, plugin);
1397 * Shutdown the plugin.
1399 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
1403 libgnunet_plugin_transport_udp_done (void *cls)
1405 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1406 struct Plugin *plugin = api->cls;
1407 struct ReceiveContext *rc;
1409 /* FIXME: clean up heap and hashmap */
1410 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
1412 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1413 plugin->sessions = NULL;
1414 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions, &destroy_inbound_session,
1416 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
1417 plugin->inbound_sessions = NULL;
1418 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
1420 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1423 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
1425 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1427 GNUNET_SCHEDULER_cancel (plugin->select_task);
1428 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1430 if (plugin->sockv4 != NULL)
1432 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1433 plugin->sockv4 = NULL;
1435 if (plugin->sockv6 != NULL)
1437 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1438 plugin->sockv6 = NULL;
1440 GNUNET_SERVER_mst_destroy (plugin->mst);
1441 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1442 GNUNET_NAT_unregister (plugin->nat);
1444 GNUNET_free (plugin);
1449 /* end of plugin_transport_udp.c */