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?
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;
196 struct GNUNET_TIME_Absolute valid_until;
198 GNUNET_SCHEDULER_TaskIdentifier invalidation_task;
201 * Desired delay for next sending we send to other peer
203 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
206 * Desired delay for next sending we received from other peer
208 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
213 * Data structure to track defragmentation contexts based
214 * on the source of the UDP traffic.
216 struct ReceiveContext
220 * Defragmentation context.
222 struct GNUNET_DEFRAGMENT_Context *defrag;
225 * Source address this receive context is for (allocated at the
226 * end of the struct).
228 const struct sockaddr *src_addr;
231 * Reference to master plugin struct.
233 struct Plugin *plugin;
236 * Node in the defrag heap.
238 struct GNUNET_CONTAINER_HeapNode *hnode;
241 * Length of 'src_addr'
245 struct GNUNET_PeerIdentity id;
251 * Encapsulation of all of the state of the plugin.
259 struct GNUNET_TRANSPORT_PluginEnvironment *env;
262 * Session of peers with whom we are currently connected,
263 * map of peer identity to 'struct PeerSession'.
265 struct GNUNET_CONTAINER_MultiHashMap *sessions;
268 * Session of peers with whom we are currently connected,
269 * map of peer identity to 'struct PeerSession'.
271 struct GNUNET_CONTAINER_MultiHashMap *inbound_sessions;
274 * Heap with all of our defragmentation activities.
276 struct GNUNET_CONTAINER_Heap *defrags;
281 GNUNET_SCHEDULER_TaskIdentifier select_task;
284 * Tokenizer for inbound messages.
286 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
289 * Bandwidth tracker to limit global UDP traffic.
291 struct GNUNET_BANDWIDTH_Tracker tracker;
294 * Address we were told to bind to exclusively (IPv4).
299 * Address we were told to bind to exclusively (IPv6).
304 * Handle to NAT traversal support.
306 struct GNUNET_NAT_Handle *nat;
311 struct GNUNET_NETWORK_FDSet *rs;
314 * The read socket for IPv4
316 struct GNUNET_NETWORK_Handle *sockv4;
319 * The read socket for IPv6
321 struct GNUNET_NETWORK_Handle *sockv6;
324 * expected delay for ACKs
326 struct GNUNET_TIME_Relative last_expected_delay;
334 * Port we advertise on.
340 struct PeerSessionIteratorContext
342 struct Session * result;
349 * Lookup the session for the given peer.
351 * @param plugin the plugin
352 * @param peer peer's identity
353 * @return NULL if we have no session
356 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
358 return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
362 int inbound_session_iterator (void *cls,
363 const GNUNET_HashCode * key,
366 struct PeerSessionIteratorContext *psc = cls;
367 struct Session *s = value;
368 if (s->addrlen == psc->addrlen)
370 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
373 if (psc->result != NULL)
380 * Lookup the session for the given peer.
382 * @param plugin the plugin
383 * @param peer peer's identity
384 * @return NULL if we have no session
387 find_inbound_session (struct Plugin *plugin,
388 const struct GNUNET_PeerIdentity *peer,
389 const void * addr, size_t addrlen)
391 struct PeerSessionIteratorContext psc;
393 psc.addrlen = addrlen;
396 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->inbound_sessions, &peer->hashPubKey, &inbound_session_iterator, &psc);
400 int inbound_session_by_addr_iterator (void *cls,
401 const GNUNET_HashCode * key,
404 struct PeerSessionIteratorContext *psc = cls;
405 struct Session *s = value;
406 if (s->addrlen == psc->addrlen)
408 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
411 if (psc->result != NULL)
418 * Lookup the session for the given peer just by address.
420 * @param plugin the plugin
421 * @param addr address
422 * @param addrlen address length
423 * @return NULL if we have no session
426 find_inbound_session_by_addr (struct Plugin *plugin, const void * addr, size_t addrlen)
428 struct PeerSessionIteratorContext psc;
430 psc.addrlen = addrlen;
433 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions, &inbound_session_by_addr_iterator, &psc);
439 * Destroy a session, plugin is being unloaded.
442 * @param key hash of public key of target peer
443 * @param value a 'struct PeerSession*' to clean up
444 * @return GNUNET_OK (continue to iterate)
447 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
449 struct Session *peer_session = value;
451 if (peer_session->frag != NULL)
452 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
453 GNUNET_free (peer_session);
458 * Destroy a session, plugin is being unloaded.
461 * @param key hash of public key of target peer
462 * @param value a 'struct PeerSession*' to clean up
463 * @return GNUNET_OK (continue to iterate)
466 destroy_inbound_session (void *cls, const GNUNET_HashCode * key, void *value)
468 struct Session *s = value;
470 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
471 GNUNET_SCHEDULER_cancel(s->invalidation_task);
477 * Disconnect from a remote node. Clean up session if we have one for this peer
479 * @param cls closure for this call (should be handle to Plugin)
480 * @param target the peeridentity of the peer to disconnect
481 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
484 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
486 struct Plugin *plugin = cls;
487 struct Session *session;
489 session = find_session (plugin, target);
492 GNUNET_assert (GNUNET_OK ==
493 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
497 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
499 &destroy_inbound_session, NULL);
500 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
501 LOG (GNUNET_ERROR_TYPE_ERROR,
504 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
505 if (session->cont != NULL)
506 session->cont (session->cont_cls, target, GNUNET_SYSERR);
507 GNUNET_free (session);
512 * Actually send out the message.
514 * @param plugin the plugin
515 * @param sa the address to send the message to
516 * @param msg message to transmit
517 * @return the number of bytes written
520 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
521 const struct GNUNET_MessageHeader *msg)
526 switch (sa->sa_family)
529 if (NULL == plugin->sockv4)
532 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
533 sa, slen = sizeof (struct sockaddr_in));
536 if (NULL == plugin->sockv6)
539 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
540 sa, slen = sizeof (struct sockaddr_in6));
546 if (GNUNET_SYSERR == sent)
548 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
549 LOG (GNUNET_ERROR_TYPE_ERROR,
550 "UDP transmited %u-byte message to %s (%d: %s)\n",
551 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
552 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
555 LOG (GNUNET_ERROR_TYPE_DEBUG,
556 "UDP transmited %u-byte message to %s (%d: %s)\n",
557 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
558 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
564 * Function that is called with messages created by the fragmentation
565 * module. In the case of the 'proc' callback of the
566 * GNUNET_FRAGMENT_context_create function, this function must
567 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
569 * @param cls closure, the 'struct PeerSession'
570 * @param msg the message that was created
573 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
575 struct Session *session = cls;
577 udp_send (session->plugin, session->sock_addr, msg);
578 GNUNET_FRAGMENT_context_transmission_done (session->frag);
581 static struct Session *
582 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
583 const void *addr, size_t addrlen,
584 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
586 struct Session * peer_session;
587 const struct IPv4UdpAddress *t4;
588 const struct IPv6UdpAddress *t6;
589 struct sockaddr_in *v4;
590 struct sockaddr_in6 *v6;
595 case sizeof (struct IPv4UdpAddress):
596 if (NULL == plugin->sockv4)
602 GNUNET_malloc (sizeof (struct Session) +
603 sizeof (struct sockaddr_in));
604 len = sizeof (struct sockaddr_in);
605 v4 = (struct sockaddr_in *) &peer_session[1];
606 v4->sin_family = AF_INET;
607 #if HAVE_SOCKADDR_IN_SIN_LEN
608 v4->sin_len = sizeof (struct sockaddr_in);
610 v4->sin_port = t4->u4_port;
611 v4->sin_addr.s_addr = t4->ipv4_addr;
613 case sizeof (struct IPv6UdpAddress):
614 if (NULL == plugin->sockv6)
620 GNUNET_malloc (sizeof (struct Session) +
621 sizeof (struct sockaddr_in6));
622 len = sizeof (struct sockaddr_in6);
623 v6 = (struct sockaddr_in6 *) &peer_session[1];
624 v6->sin6_family = AF_INET6;
625 #if HAVE_SOCKADDR_IN_SIN_LEN
626 v6->sin6_len = sizeof (struct sockaddr_in6);
628 v6->sin6_port = t6->u6_port;
629 v6->sin6_addr = t6->ipv6_addr;
632 /* Must have a valid address to send to */
637 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
638 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
639 peer_session->addrlen = len;
640 peer_session->target = *target;
641 peer_session->plugin = plugin;
642 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
643 peer_session->cont = cont;
644 peer_session->cont_cls = cont_cls;
650 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
653 * Function that can be used by the transport service to transmit
654 * a message using the plugin.
657 * @param target who should receive this message (ignored by UDP)
658 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
659 * @param msgbuf_size the size of the msgbuf to send
660 * @param priority how important is the message (ignored by UDP)
661 * @param timeout when should we time out (give up) if we can not transmit?
662 * @param session identifier used for this session (NULL for UDP)
663 * @param addr the addr to send the message to
664 * @param addrlen the len of addr
665 * @param force_address not used, we had better have an address to send to
666 * because we are stateless!!
667 * @param cont continuation to call once the message has
668 * been transmitted (or if the transport is ready
669 * for the next transmission call; or if the
670 * peer disconnected...)
671 * @param cont_cls closure for cont
673 * @return the number of bytes written (may return 0 and the message can
674 * still be transmitted later!)
677 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
678 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
679 struct GNUNET_TIME_Relative timeout, struct Session *session,
680 const void *addr, size_t addrlen, int force_address,
681 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
683 struct Plugin *plugin = cls;
684 struct Session *peer_session;
686 const struct IPv4UdpAddress *t4;
687 const struct IPv6UdpAddress *t6;
688 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
690 struct UDPMessage *udp;
692 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
695 return GNUNET_SYSERR;
698 LOG (GNUNET_ERROR_TYPE_DEBUG,
699 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
700 msgbuf_size, GNUNET_i2s (target),
701 udp_address_to_string (NULL, addr, addrlen),
702 session, force_address);
704 if ((force_address == GNUNET_SYSERR) && (session == NULL))
705 return GNUNET_SYSERR;
708 /* safety check: comparing address to address stored in session */
709 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
712 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value (
713 plugin->inbound_sessions, &target->hashPubKey, s));
715 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
716 return GNUNET_SYSERR;
719 case sizeof (struct IPv4UdpAddress):
720 if (NULL == plugin->sockv4)
723 cont (cont_cls, target, GNUNET_SYSERR);
724 return GNUNET_SYSERR;
727 if (s->addrlen != (sizeof (struct sockaddr_in)))
728 return GNUNET_SYSERR;
729 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
730 GNUNET_assert (a4->sin_port == t4->u4_port);
731 GNUNET_assert (0 == memcmp(&a4->sin_addr, &t4->ipv4_addr, sizeof (struct in_addr)));
732 LOG (GNUNET_ERROR_TYPE_DEBUG,
733 "Session 0x%X successfully checked!\n", session);
735 case sizeof (struct IPv6UdpAddress):
736 if (NULL == plugin->sockv6)
739 cont (cont_cls, target, GNUNET_SYSERR);
740 return GNUNET_SYSERR;
743 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
744 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
745 GNUNET_assert (a6->sin6_port == t6->u6_port);
746 GNUNET_assert (0 == memcmp(&a6->sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr)));
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Session 0x%X successfully checked!\n", session);
751 /* Must have a valid address to send to */
756 if ((addr == NULL) || (addrlen == 0))
757 return GNUNET_SYSERR;
758 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
759 if (peer_session == NULL)
762 cont (cont_cls, target, GNUNET_SYSERR);
763 return GNUNET_SYSERR;;
767 udp = (struct UDPMessage *) mbuf;
768 udp->header.size = htons (mlen);
769 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
770 udp->reserved = htonl (0);
771 udp->sender = *plugin->env->my_identity;
772 memcpy (&udp[1], msgbuf, msgbuf_size);
776 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
777 if (s->flow_delay_from_other_peer.abs_value > now.abs_value)
779 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(now, s->flow_delay_from_other_peer);
780 LOG (GNUNET_ERROR_TYPE_DEBUG,
781 "We try to send to early! Should in %llu!\n", delta.rel_value);
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "SENDING without session!\n");
792 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
794 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
795 GNUNET_free_non_null (peer_session);
799 GNUNET_assert (GNUNET_OK ==
800 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
803 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
805 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
807 plugin->last_expected_delay,
808 &udp->header, &send_fragment,
816 * Closure for 'process_inbound_tokenized_messages'
818 struct SourceInformation
823 struct GNUNET_PeerIdentity sender;
831 * Number of bytes in source address.
835 struct Session * session;
840 * Message tokenizer has broken up an incomming message. Pass it on
843 * @param cls the 'struct Plugin'
844 * @param client the 'struct SourceInformation'
845 * @param hdr the actual message
848 process_inbound_tokenized_messages (void *cls, void *client,
849 const struct GNUNET_MessageHeader *hdr)
851 struct Plugin *plugin = cls;
852 struct SourceInformation *si = client;
853 struct GNUNET_ATS_Information distance;
854 struct GNUNET_TIME_Relative delay;
857 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
858 distance.value = htonl (1);
860 LOG (GNUNET_ERROR_TYPE_DEBUG,
861 "Giving Session %X %s to transport\n", si->session, GNUNET_i2s(&si->session->target));
862 delay = plugin->env->receive (plugin->env->cls, &si->sender, hdr, &distance, 1, si->session,
864 if (delay.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
866 // FIXME: terminate session!
868 si->session->flow_delay_for_other_peer = delay;
872 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 struct Session * s = cls;
875 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
876 LOG (GNUNET_ERROR_TYPE_ERROR,
877 "Session %X (`%s') is now invalid\n", s, GNUNET_a2s (s->sock_addr,s->addrlen));
879 s->plugin->env->session_end(s->plugin->env->cls, &s->target, s);
880 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(s->plugin->inbound_sessions, &s->target.hashPubKey, s));
886 * We've received a UDP Message. Process it (pass contents to main service).
888 * @param plugin plugin context
889 * @param msg the message
890 * @param sender_addr sender address
891 * @param sender_addr_len number of bytes in sender_addr
894 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
895 const struct sockaddr *sender_addr,
896 socklen_t sender_addr_len)
898 struct SourceInformation si;
899 struct IPv4UdpAddress u4;
900 struct IPv6UdpAddress u6;
904 if (0 != ntohl (msg->reserved))
909 if (ntohs (msg->header.size) <
910 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
916 /* convert address */
917 switch (sender_addr->sa_family)
920 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
921 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
922 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
927 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
928 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
929 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
938 LOG (GNUNET_ERROR_TYPE_DEBUG,
939 "Received message with %u bytes from peer `%s' at `%s'\n",
940 (unsigned int) ntohs (msg->header.size),
941 GNUNET_i2s (&msg->sender), GNUNET_a2s (sender_addr,
945 /* create a session for inbound connections */
946 const struct UDPMessage * udp_msg = (const struct UDPMessage *) msg;
947 LOG (GNUNET_ERROR_TYPE_DEBUG,
948 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
949 GNUNET_i2s (&udp_msg->sender),
950 udp_address_to_string(NULL, arg, args));
952 struct Session * s = NULL;
953 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr, sender_addr_len);
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
960 GNUNET_i2s (&s->target),
961 udp_address_to_string(NULL, arg, args));
965 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
966 LOG (GNUNET_ERROR_TYPE_DEBUG,
967 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
969 GNUNET_i2s (&s->target),
970 udp_address_to_string(NULL, arg, args));
972 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
973 &s->target.hashPubKey,
975 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
977 s->valid_until = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
978 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
980 GNUNET_SCHEDULER_cancel(s->invalidation_task);
981 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
982 LOG (GNUNET_ERROR_TYPE_DEBUG,
983 "Rescheduling %X' `%s'\n",
984 s, udp_address_to_string(NULL, arg, args));
986 s->invalidation_task = GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &invalidation_task, s);
987 /* iterate over all embedded messages */
988 si.sender = msg->sender;
992 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
993 ntohs (msg->header.size) -
994 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
999 * Process a defragmented message.
1001 * @param cls the 'struct ReceiveContext'
1002 * @param msg the message
1005 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1007 struct ReceiveContext *rc = cls;
1009 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1014 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1019 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1020 rc->src_addr, rc->addr_len);
1025 * Transmit an acknowledgement.
1027 * @param cls the 'struct ReceiveContext'
1028 * @param id message ID (unused)
1029 * @param msg ack to transmit
1032 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1034 struct ReceiveContext *rc = cls;
1036 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1038 struct UDP_ACK_Message *udp_ack;
1042 s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
1045 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1046 delay = s->flow_delay_for_other_peer.rel_value;
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK to `%s' including delay of %u ms\n",
1054 GNUNET_a2s (rc->src_addr,
1055 (rc->src_addr->sa_family ==
1056 AF_INET) ? sizeof (struct sockaddr_in) :
1057 sizeof (struct sockaddr_in6)),
1060 udp_ack = (struct UDP_ACK_Message *) buf;
1061 udp_ack->header.size = htons ((uint16_t) msize);
1062 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1063 udp_ack->delay = htonl (delay);
1064 udp_ack->sender = *rc->plugin->env->my_identity;
1065 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1066 (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
1071 * Closure for 'find_receive_context'.
1073 struct FindReceiveContext
1076 * Where to store the result.
1078 struct ReceiveContext *rc;
1083 const struct sockaddr *addr;
1086 * Number of bytes in 'addr'.
1090 struct Session * session;
1095 * Scan the heap for a receive context with the given address.
1097 * @param cls the 'struct FindReceiveContext'
1098 * @param node internal node of the heap
1099 * @param element value stored at the node (a 'struct ReceiveContext')
1100 * @param cost cost associated with the node
1101 * @return GNUNET_YES if we should continue to iterate,
1105 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1106 void *element, GNUNET_CONTAINER_HeapCostType cost)
1108 struct FindReceiveContext *frc = cls;
1109 struct ReceiveContext *e = element;
1111 if ((frc->addr_len == e->addr_len) &&
1112 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1122 * Read and process a message from the given socket.
1124 * @param plugin the overall plugin
1125 * @param rsock socket to read from
1128 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1134 const struct GNUNET_MessageHeader *msg;
1135 const struct GNUNET_MessageHeader *ack;
1136 struct Session *peer_session;
1137 const struct UDP_ACK_Message *udp_ack;
1138 struct ReceiveContext *rc;
1139 struct GNUNET_TIME_Absolute now;
1140 struct FindReceiveContext frc;
1141 struct Session * s = NULL;
1142 struct GNUNET_TIME_Relative flow_delay;
1144 fromlen = sizeof (addr);
1145 memset (&addr, 0, sizeof (addr));
1147 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1148 (struct sockaddr *) &addr, &fromlen);
1149 if (ret < sizeof (struct GNUNET_MessageHeader))
1151 GNUNET_break_op (0);
1154 msg = (const struct GNUNET_MessageHeader *) buf;
1156 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1158 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs(msg->type));
1160 if (ret != ntohs (msg->size))
1162 GNUNET_break_op (0);
1165 switch (ntohs (msg->type))
1167 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1168 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1170 GNUNET_break_op (0);
1173 process_udp_message (plugin, (const struct UDPMessage *) msg,
1174 (const struct sockaddr *) addr, fromlen);
1176 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1178 if (ntohs (msg->size) <
1179 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1181 GNUNET_break_op (0);
1184 udp_ack = (const struct UDP_ACK_Message *) msg;
1185 s = find_inbound_session(plugin, &udp_ack->sender, addr, fromlen);
1188 flow_delay.rel_value = (uint64_t) ntohl(udp_ack->delay);
1190 LOG (GNUNET_ERROR_TYPE_DEBUG,
1191 "We received a sending delay of %llu\n", flow_delay.rel_value);
1193 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), flow_delay);
1195 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1196 if (ntohs (ack->size) != ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1198 GNUNET_break_op (0);
1202 LOG (GNUNET_ERROR_TYPE_DEBUG,
1203 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1204 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp->sender),
1205 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1208 peer_session = find_session (plugin, &udp_ack->sender);
1209 if (NULL == peer_session)
1212 LOG (GNUNET_ERROR_TYPE_DEBUG,
1213 "Session for ACK not found, dropping ACK!\n");
1217 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1219 GNUNET_assert (GNUNET_OK ==
1220 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1224 plugin->last_expected_delay =
1225 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1226 if (peer_session->cont != NULL)
1227 peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
1228 GNUNET_free (peer_session);
1230 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1232 frc.addr = (const struct sockaddr *) addr;
1233 frc.addr_len = fromlen;
1234 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1236 now = GNUNET_TIME_absolute_get ();
1240 /* need to create a new RC */
1241 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1242 memcpy (&rc[1], addr, fromlen);
1243 rc->src_addr = (const struct sockaddr *) &rc[1];
1244 rc->addr_len = fromlen;
1245 rc->plugin = plugin;
1247 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1248 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1249 &fragment_msg_proc, &ack_proc);
1251 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1252 (GNUNET_CONTAINER_HeapCostType)
1256 LOG (GNUNET_ERROR_TYPE_DEBUG,
1257 "UDP processes %u-byte fragment from `%s'\n",
1258 (unsigned int) ntohs (msg->size),
1259 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1262 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1264 /* keep this 'rc' from expiring */
1265 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1266 (GNUNET_CONTAINER_HeapCostType)
1269 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1270 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1272 /* remove 'rc' that was inactive the longest */
1273 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1274 GNUNET_assert (NULL != rc);
1275 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1280 GNUNET_break_op (0);
1287 * We have been notified that our writeset has something to read. We don't
1288 * know which socket needs to be read, so we have to check each one
1289 * Then reschedule this function to be called again once more is available.
1291 * @param cls the plugin handle
1292 * @param tc the scheduling context (for rescheduling this function again)
1295 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1297 struct Plugin *plugin = cls;
1299 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1300 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1302 if ((NULL != plugin->sockv4) &&
1303 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1304 udp_read (plugin, plugin->sockv4);
1305 if ((NULL != plugin->sockv6) &&
1306 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1307 udp_read (plugin, plugin->sockv6);
1308 plugin->select_task =
1309 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1310 GNUNET_SCHEDULER_NO_TASK,
1311 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1312 NULL, &udp_plugin_select, plugin);
1318 * Check if the given port is plausible (must be either our listen
1319 * port or our advertised port). If it is neither, we return
1322 * @param plugin global variables
1323 * @param in_port port number to check
1324 * @return GNUNET_OK if port is either open_port or adv_port
1327 check_port (struct Plugin *plugin, uint16_t in_port)
1329 if ((in_port == plugin->port) || (in_port == plugin->aport))
1331 return GNUNET_SYSERR;
1336 * Function that will be called to check if a binary address for this
1337 * plugin is well-formed and corresponds to an address for THIS peer
1338 * (as per our configuration). Naturally, if absolutely necessary,
1339 * plugins can be a bit conservative in their answer, but in general
1340 * plugins should make sure that the address does not redirect
1341 * traffic to a 3rd party that might try to man-in-the-middle our
1344 * @param cls closure, should be our handle to the Plugin
1345 * @param addr pointer to the address
1346 * @param addrlen length of addr
1347 * @return GNUNET_OK if this is a plausible address for this peer
1348 * and transport, GNUNET_SYSERR if not
1352 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1354 struct Plugin *plugin = cls;
1355 struct IPv4UdpAddress *v4;
1356 struct IPv6UdpAddress *v6;
1358 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1359 (addrlen != sizeof (struct IPv6UdpAddress)))
1361 GNUNET_break_op (0);
1362 return GNUNET_SYSERR;
1364 if (addrlen == sizeof (struct IPv4UdpAddress))
1366 v4 = (struct IPv4UdpAddress *) addr;
1367 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1368 return GNUNET_SYSERR;
1370 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1371 sizeof (struct in_addr)))
1372 return GNUNET_SYSERR;
1376 v6 = (struct IPv6UdpAddress *) addr;
1377 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1379 GNUNET_break_op (0);
1380 return GNUNET_SYSERR;
1382 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1383 return GNUNET_SYSERR;
1385 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1386 sizeof (struct in6_addr)))
1387 return GNUNET_SYSERR;
1394 * Function called for a quick conversion of the binary address to
1395 * a numeric address. Note that the caller must not free the
1396 * address and that the next call to this function is allowed
1397 * to override the address again.
1399 * @param cls closure
1400 * @param addr binary address
1401 * @param addrlen length of the address
1402 * @return string representing the same address
1405 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1407 static char rbuf[INET6_ADDRSTRLEN + 10];
1408 char buf[INET6_ADDRSTRLEN];
1412 const struct IPv4UdpAddress *t4;
1413 const struct IPv6UdpAddress *t6;
1417 if (addrlen == sizeof (struct IPv6UdpAddress))
1421 port = ntohs (t6->u6_port);
1422 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1425 else if (addrlen == sizeof (struct IPv4UdpAddress))
1429 port = ntohs (t4->u4_port);
1430 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1435 GNUNET_break_op (0);
1438 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1439 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1446 * Closure for 'append_port'.
1448 struct PrettyPrinterContext
1451 * Function to call with the result.
1453 GNUNET_TRANSPORT_AddressStringCallback asc;
1456 * Clsoure for 'asc'.
1461 * Port to add after the IP address.
1468 * Append our port and forward the result.
1470 * @param cls a 'struct PrettyPrinterContext'
1471 * @param hostname result from DNS resolver
1474 append_port (void *cls, const char *hostname)
1476 struct PrettyPrinterContext *ppc = cls;
1479 if (hostname == NULL)
1481 ppc->asc (ppc->asc_cls, NULL);
1485 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1486 ppc->asc (ppc->asc_cls, ret);
1492 * Convert the transports address to a nice, human-readable
1495 * @param cls closure
1496 * @param type name of the transport that generated the address
1497 * @param addr one of the addresses of the host, NULL for the last address
1498 * the specific address format depends on the transport
1499 * @param addrlen length of the address
1500 * @param numeric should (IP) addresses be displayed in numeric form?
1501 * @param timeout after how long should we give up?
1502 * @param asc function to call on each string
1503 * @param asc_cls closure for asc
1506 udp_plugin_address_pretty_printer (void *cls, const char *type,
1507 const void *addr, size_t addrlen,
1509 struct GNUNET_TIME_Relative timeout,
1510 GNUNET_TRANSPORT_AddressStringCallback asc,
1513 struct PrettyPrinterContext *ppc;
1516 struct sockaddr_in a4;
1517 struct sockaddr_in6 a6;
1518 const struct IPv4UdpAddress *u4;
1519 const struct IPv6UdpAddress *u6;
1522 if (addrlen == sizeof (struct IPv6UdpAddress))
1525 memset (&a6, 0, sizeof (a6));
1526 a6.sin6_family = AF_INET6;
1527 #if HAVE_SOCKADDR_IN_SIN_LEN
1528 a6.sin6_len = sizeof (a6);
1530 a6.sin6_port = u6->u6_port;
1531 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1532 port = ntohs (u6->u6_port);
1536 else if (addrlen == sizeof (struct IPv4UdpAddress))
1539 memset (&a4, 0, sizeof (a4));
1540 a4.sin_family = AF_INET;
1541 #if HAVE_SOCKADDR_IN_SIN_LEN
1542 a4.sin_len = sizeof (a4);
1544 a4.sin_port = u4->u4_port;
1545 a4.sin_addr.s_addr = u4->ipv4_addr;
1546 port = ntohs (u4->u4_port);
1552 /* invalid address */
1553 GNUNET_break_op (0);
1554 asc (asc_cls, NULL);
1557 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1559 ppc->asc_cls = asc_cls;
1561 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1566 * Our external IP address/port mapping has changed.
1568 * @param cls closure, the 'struct LocalAddrList'
1569 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1570 * the previous (now invalid) one
1571 * @param addr either the previous or the new public IP address
1572 * @param addrlen actual lenght of the address
1575 udp_nat_port_map_callback (void *cls, int add_remove,
1576 const struct sockaddr *addr, socklen_t addrlen)
1578 struct Plugin *plugin = cls;
1579 struct IPv4UdpAddress u4;
1580 struct IPv6UdpAddress u6;
1584 /* convert 'addr' to our internal format */
1585 switch (addr->sa_family)
1588 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1589 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1590 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1595 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1596 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1597 sizeof (struct in6_addr));
1598 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1606 /* modify our published address list */
1607 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1612 * The exported method. Makes the core api available via a global and
1613 * returns the udp transport API.
1615 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1616 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1619 libgnunet_plugin_transport_udp_init (void *cls)
1621 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1622 unsigned long long port;
1623 unsigned long long aport;
1624 struct GNUNET_TRANSPORT_PluginFunctions *api;
1625 struct Plugin *plugin;
1626 int sockets_created;
1627 struct sockaddr_in serverAddrv4;
1628 struct sockaddr_in6 serverAddrv6;
1629 struct sockaddr *serverAddr;
1630 struct sockaddr *addrs[2];
1631 socklen_t addrlens[2];
1634 unsigned long long udp_max_bps;
1637 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1641 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1642 "MAX_BPS", &udp_max_bps))
1643 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1645 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1646 "ADVERTISED_PORT", &aport))
1650 LOG (GNUNET_ERROR_TYPE_WARNING,
1651 _("Given `%s' option is out of range: %llu > %u\n"), "PORT",
1655 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1656 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1658 plugin = GNUNET_malloc (sizeof (struct Plugin));
1659 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1660 GNUNET_BANDWIDTH_value_init ((uint32_t)
1662 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1663 plugin->port = port;
1664 plugin->aport = aport;
1666 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1669 api->send = &udp_plugin_send;
1670 api->disconnect = &udp_disconnect;
1671 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1672 api->address_to_string = &udp_address_to_string;
1673 api->check_address = &udp_plugin_check_address;
1676 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1677 "BINDTO", &plugin->bind4_address))
1679 LOG (GNUNET_ERROR_TYPE_DEBUG,
1680 "Binding udp plugin to specific address: `%s'\n",
1681 plugin->bind4_address);
1682 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
1684 GNUNET_free (plugin->bind4_address);
1685 GNUNET_free (plugin);
1692 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1693 "BINDTO6", &plugin->bind6_address))
1695 LOG (GNUNET_ERROR_TYPE_DEBUG,
1696 "Binding udp plugin to specific address: `%s'\n",
1697 plugin->bind6_address);
1699 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1701 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1702 plugin->bind6_address);
1703 GNUNET_free_non_null (plugin->bind4_address);
1704 GNUNET_free (plugin->bind6_address);
1705 GNUNET_free (plugin);
1712 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1714 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1716 plugin->inbound_sessions =
1717 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1719 sockets_created = 0;
1721 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
1724 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1725 if (NULL == plugin->sockv6)
1727 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1731 #if HAVE_SOCKADDR_IN_SIN_LEN
1732 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1734 serverAddrv6.sin6_family = AF_INET6;
1735 serverAddrv6.sin6_addr = in6addr_any;
1736 serverAddrv6.sin6_port = htons (plugin->port);
1737 addrlen = sizeof (serverAddrv6);
1738 serverAddr = (struct sockaddr *) &serverAddrv6;
1740 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1741 ntohs (serverAddrv6.sin6_port));
1744 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1747 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1749 LOG (GNUNET_ERROR_TYPE_DEBUG,
1750 "IPv6 Binding failed, trying new port %d\n",
1751 ntohs (serverAddrv6.sin6_port));
1756 GNUNET_NETWORK_socket_close (plugin->sockv6);
1757 plugin->sockv6 = NULL;
1761 if (plugin->sockv6 != NULL)
1763 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
1764 addrlens[sockets_created] = sizeof (serverAddrv6);
1771 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1772 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1773 if (NULL == plugin->sockv4)
1775 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1779 #if HAVE_SOCKADDR_IN_SIN_LEN
1780 serverAddrv4.sin_len = sizeof (serverAddrv4);
1782 serverAddrv4.sin_family = AF_INET;
1783 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1784 serverAddrv4.sin_port = htons (plugin->port);
1785 addrlen = sizeof (serverAddrv4);
1786 serverAddr = (struct sockaddr *) &serverAddrv4;
1788 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1789 ntohs (serverAddrv4.sin_port));
1792 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1795 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1797 LOG (GNUNET_ERROR_TYPE_DEBUG,
1798 "IPv4 Binding failed, trying new port %d\n",
1799 ntohs (serverAddrv4.sin_port));
1804 GNUNET_NETWORK_socket_close (plugin->sockv4);
1805 plugin->sockv4 = NULL;
1809 if (plugin->sockv4 != NULL)
1811 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
1812 addrlens[sockets_created] = sizeof (serverAddrv4);
1817 plugin->rs = GNUNET_NETWORK_fdset_create ();
1818 GNUNET_NETWORK_fdset_zero (plugin->rs);
1819 if (NULL != plugin->sockv4)
1820 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1821 if (NULL != plugin->sockv6)
1822 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1824 plugin->select_task =
1825 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1826 GNUNET_SCHEDULER_NO_TASK,
1827 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1828 NULL, &udp_plugin_select, plugin);
1829 if (sockets_created == 0)
1830 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1832 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
1833 (const struct sockaddr **) addrs, addrlens,
1834 &udp_nat_port_map_callback, NULL, plugin);
1839 * Shutdown the plugin.
1841 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
1845 libgnunet_plugin_transport_udp_done (void *cls)
1847 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1848 struct Plugin *plugin = api->cls;
1849 struct ReceiveContext *rc;
1851 /* FIXME: clean up heap and hashmap */
1852 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
1854 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1855 plugin->sessions = NULL;
1856 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions, &destroy_inbound_session,
1858 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
1859 plugin->inbound_sessions = NULL;
1860 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
1862 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1865 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
1867 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1869 GNUNET_SCHEDULER_cancel (plugin->select_task);
1870 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1872 if (plugin->sockv4 != NULL)
1874 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1875 plugin->sockv4 = NULL;
1877 if (plugin->sockv6 != NULL)
1879 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1880 plugin->sockv6 = NULL;
1882 GNUNET_SERVER_mst_destroy (plugin->mst);
1883 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1884 GNUNET_NAT_unregister (plugin->nat);
1886 GNUNET_free (plugin);
1891 /* end of plugin_transport_udp.c */