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 * Network format for IPv4 addresses.
102 * IPv4 address, in network byte order.
104 uint32_t ipv4_addr GNUNET_PACKED;
107 * Port number, in network byte order.
109 uint16_t u4_port GNUNET_PACKED;
114 * Network format for IPv6 addresses.
116 struct IPv6UdpAddress
122 struct in6_addr ipv6_addr GNUNET_PACKED;
125 * Port number, in network byte order.
127 uint16_t u6_port GNUNET_PACKED;
131 /* Forward definition */
136 * Session with another peer. FIXME: why not make this into
137 * a regular 'struct Session' and pass it around!?
143 * Which peer is this session for?
145 struct GNUNET_PeerIdentity target;
148 * Pointer to the global plugin struct.
150 struct Plugin *plugin;
153 * Address of the other peer
155 const struct sockaddr *sock_addr;
160 * Function to call upon completion of the transmission.
162 GNUNET_TRANSPORT_TransmitContinuation cont;
165 * Closure for 'cont'.
170 * Current outgoing message to this peer.
172 struct GNUNET_FRAGMENT_Context *frag;
174 struct GNUNET_TIME_Absolute valid_until;
176 GNUNET_SCHEDULER_TaskIdentifier invalidation_task;
181 * Data structure to track defragmentation contexts based
182 * on the source of the UDP traffic.
184 struct ReceiveContext
188 * Defragmentation context.
190 struct GNUNET_DEFRAGMENT_Context *defrag;
193 * Source address this receive context is for (allocated at the
194 * end of the struct).
196 const struct sockaddr *src_addr;
199 * Reference to master plugin struct.
201 struct Plugin *plugin;
204 * Node in the defrag heap.
206 struct GNUNET_CONTAINER_HeapNode *hnode;
209 * Length of 'src_addr'
217 * Encapsulation of all of the state of the plugin.
225 struct GNUNET_TRANSPORT_PluginEnvironment *env;
228 * Session of peers with whom we are currently connected,
229 * map of peer identity to 'struct PeerSession'.
231 struct GNUNET_CONTAINER_MultiHashMap *sessions;
234 * Session of peers with whom we are currently connected,
235 * map of peer identity to 'struct PeerSession'.
237 struct GNUNET_CONTAINER_MultiHashMap *inbound_sessions;
240 * Heap with all of our defragmentation activities.
242 struct GNUNET_CONTAINER_Heap *defrags;
247 GNUNET_SCHEDULER_TaskIdentifier select_task;
250 * Tokenizer for inbound messages.
252 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
255 * Bandwidth tracker to limit global UDP traffic.
257 struct GNUNET_BANDWIDTH_Tracker tracker;
260 * Address we were told to bind to exclusively (IPv4).
265 * Address we were told to bind to exclusively (IPv6).
270 * Handle to NAT traversal support.
272 struct GNUNET_NAT_Handle *nat;
277 struct GNUNET_NETWORK_FDSet *rs;
280 * The read socket for IPv4
282 struct GNUNET_NETWORK_Handle *sockv4;
285 * The read socket for IPv6
287 struct GNUNET_NETWORK_Handle *sockv6;
290 * expected delay for ACKs
292 struct GNUNET_TIME_Relative last_expected_delay;
300 * Port we advertise on.
306 struct PeerSessionIteratorContext
308 struct Session * result;
315 * Lookup the session for the given peer.
317 * @param plugin the plugin
318 * @param peer peer's identity
319 * @return NULL if we have no session
322 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
324 return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
328 int inbound_session_iterator (void *cls,
329 const GNUNET_HashCode * key,
332 struct PeerSessionIteratorContext *psc = cls;
333 struct Session *s = value;
334 if (s->addrlen == psc->addrlen)
336 if (0 == memcmp (&s[1], psc->addr, s->addrlen))
339 if (psc->result != NULL)
346 * Lookup the session for the given peer.
348 * @param plugin the plugin
349 * @param peer peer's identity
350 * @return NULL if we have no session
353 find_inbound_session (struct Plugin *plugin,
354 const struct GNUNET_PeerIdentity *peer,
355 const void * addr, size_t addrlen)
357 struct PeerSessionIteratorContext psc;
359 psc.addrlen = addrlen;
362 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->inbound_sessions, &peer->hashPubKey, &inbound_session_iterator, &psc);
368 * Destroy a session, plugin is being unloaded.
371 * @param key hash of public key of target peer
372 * @param value a 'struct PeerSession*' to clean up
373 * @return GNUNET_OK (continue to iterate)
376 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
378 struct Session *peer_session = value;
380 if (peer_session->frag != NULL)
381 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
382 GNUNET_free (peer_session);
387 * Destroy a session, plugin is being unloaded.
390 * @param key hash of public key of target peer
391 * @param value a 'struct PeerSession*' to clean up
392 * @return GNUNET_OK (continue to iterate)
395 destroy_inbound_session (void *cls, const GNUNET_HashCode * key, void *value)
397 struct Session *s = value;
399 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
400 GNUNET_SCHEDULER_cancel(s->invalidation_task);
406 * Disconnect from a remote node. Clean up session if we have one for this peer
408 * @param cls closure for this call (should be handle to Plugin)
409 * @param target the peeridentity of the peer to disconnect
410 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
413 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
415 struct Plugin *plugin = cls;
416 struct Session *session;
418 session = find_session (plugin, target);
421 GNUNET_assert (GNUNET_OK ==
422 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
426 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
428 &destroy_inbound_session, NULL);
429 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
430 LOG (GNUNET_ERROR_TYPE_ERROR,
433 plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
434 if (session->cont != NULL)
435 session->cont (session->cont_cls, target, GNUNET_SYSERR);
436 GNUNET_free (session);
441 * Actually send out the message.
443 * @param plugin the plugin
444 * @param sa the address to send the message to
445 * @param msg message to transmit
446 * @return the number of bytes written
449 udp_send (struct Plugin *plugin, const struct sockaddr *sa,
450 const struct GNUNET_MessageHeader *msg)
455 switch (sa->sa_family)
458 if (NULL == plugin->sockv4)
461 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, ntohs (msg->size),
462 sa, slen = sizeof (struct sockaddr_in));
465 if (NULL == plugin->sockv6)
468 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, ntohs (msg->size),
469 sa, slen = sizeof (struct sockaddr_in6));
475 if (GNUNET_SYSERR == sent)
477 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
478 LOG (GNUNET_ERROR_TYPE_ERROR,
479 "UDP transmited %u-byte message to %s (%d: %s)\n",
480 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
481 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
484 LOG (GNUNET_ERROR_TYPE_DEBUG,
485 "UDP transmited %u-byte message to %s (%d: %s)\n",
486 (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
487 (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
493 * Function that is called with messages created by the fragmentation
494 * module. In the case of the 'proc' callback of the
495 * GNUNET_FRAGMENT_context_create function, this function must
496 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
498 * @param cls closure, the 'struct PeerSession'
499 * @param msg the message that was created
502 send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
504 struct Session *session = cls;
506 udp_send (session->plugin, session->sock_addr, msg);
507 GNUNET_FRAGMENT_context_transmission_done (session->frag);
510 static struct Session *
511 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
512 const void *addr, size_t addrlen,
513 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
515 struct Session * peer_session;
516 const struct IPv4UdpAddress *t4;
517 const struct IPv6UdpAddress *t6;
518 struct sockaddr_in *v4;
519 struct sockaddr_in6 *v6;
524 case sizeof (struct IPv4UdpAddress):
525 if (NULL == plugin->sockv4)
531 GNUNET_malloc (sizeof (struct Session) +
532 sizeof (struct sockaddr_in));
533 len = sizeof (struct sockaddr_in);
534 v4 = (struct sockaddr_in *) &peer_session[1];
535 v4->sin_family = AF_INET;
536 #if HAVE_SOCKADDR_IN_SIN_LEN
537 v4->sin_len = sizeof (struct sockaddr_in);
539 v4->sin_port = t4->u4_port;
540 v4->sin_addr.s_addr = t4->ipv4_addr;
542 case sizeof (struct IPv6UdpAddress):
543 if (NULL == plugin->sockv6)
549 GNUNET_malloc (sizeof (struct Session) +
550 sizeof (struct sockaddr_in6));
551 len = sizeof (struct sockaddr_in6);
552 v6 = (struct sockaddr_in6 *) &peer_session[1];
553 v6->sin6_family = AF_INET6;
554 #if HAVE_SOCKADDR_IN_SIN_LEN
555 v6->sin6_len = sizeof (struct sockaddr_in6);
557 v6->sin6_port = t6->u6_port;
558 v6->sin6_addr = t6->ipv6_addr;
561 /* Must have a valid address to send to */
566 peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
567 peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
568 peer_session->addrlen = len;
569 peer_session->target = *target;
570 peer_session->plugin = plugin;
571 peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
572 peer_session->cont = cont;
573 peer_session->cont_cls = cont_cls;
579 udp_address_to_string (void *cls, const void *addr, size_t addrlen);
582 * Function that can be used by the transport service to transmit
583 * a message using the plugin.
586 * @param target who should receive this message (ignored by UDP)
587 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
588 * @param msgbuf_size the size of the msgbuf to send
589 * @param priority how important is the message (ignored by UDP)
590 * @param timeout when should we time out (give up) if we can not transmit?
591 * @param session identifier used for this session (NULL for UDP)
592 * @param addr the addr to send the message to
593 * @param addrlen the len of addr
594 * @param force_address not used, we had better have an address to send to
595 * because we are stateless!!
596 * @param cont continuation to call once the message has
597 * been transmitted (or if the transport is ready
598 * for the next transmission call; or if the
599 * peer disconnected...)
600 * @param cont_cls closure for cont
602 * @return the number of bytes written (may return 0 and the message can
603 * still be transmitted later!)
606 udp_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
607 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
608 struct GNUNET_TIME_Relative timeout, struct Session *session,
609 const void *addr, size_t addrlen, int force_address,
610 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
612 struct Plugin *plugin = cls;
613 struct Session *peer_session;
615 const struct IPv4UdpAddress *t4;
616 const struct IPv6UdpAddress *t6;
617 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
619 struct UDPMessage *udp;
621 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
624 return GNUNET_SYSERR;
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
629 msgbuf_size, GNUNET_i2s (target), udp_address_to_string (NULL, addr, addrlen), session, force_address);
631 if ((force_address == GNUNET_SYSERR) && (session == NULL))
632 return GNUNET_SYSERR;
634 /* safety check: comparing address to address stored in session */
635 if ((session != NULL) && (addr != NULL) && (addrlen != 0))
638 /* session timed out */
640 if (GNUNET_TIME_absolute_get().abs_value > s->valid_until.abs_value)
642 LOG (GNUNET_ERROR_TYPE_ERROR,
643 "UDP Session %X is invalid %u\n", session, force_address);
645 plugin->env->session_end (plugin->env->cls, &s->target, s);
646 GNUNET_CONTAINER_multihashmap_remove (plugin->inbound_sessions, &s->target.hashPubKey, s);
648 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
650 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
651 GNUNET_SCHEDULER_cancel(s->invalidation_task);
655 if ((force_address != GNUNET_SYSERR) && (addr != NULL) && (addrlen != 0))
657 LOG (GNUNET_ERROR_TYPE_ERROR,
658 "goto session_invalid: %X %u %s\n", addr, addrlen, udp_address_to_string(NULL, addr, addrlen));
659 goto session_invalid;
661 LOG (GNUNET_ERROR_TYPE_ERROR,
662 "return GNUNET_SYSERR;\n");
663 return GNUNET_SYSERR;
665 if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
666 return GNUNET_SYSERR;
669 case sizeof (struct IPv4UdpAddress):
670 if (NULL == plugin->sockv4)
673 cont (cont_cls, target, GNUNET_SYSERR);
674 return GNUNET_SYSERR;
677 if (s->addrlen != (sizeof (struct sockaddr_in)))
678 return GNUNET_SYSERR;
679 struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
680 GNUNET_assert (a4->sin_port == t4->u4_port);
681 GNUNET_assert (0 == memcmp(&a4->sin_addr, &t4->ipv4_addr, sizeof (struct in_addr)));
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 "Session 0x%X successfully checked!\n", session);
685 case sizeof (struct IPv6UdpAddress):
686 if (NULL == plugin->sockv6)
689 cont (cont_cls, target, GNUNET_SYSERR);
690 return GNUNET_SYSERR;
693 GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
694 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
695 GNUNET_assert (a6->sin6_port == t6->u6_port);
696 GNUNET_assert (0 == memcmp(&a6->sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr)));
697 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 "Session 0x%X successfully checked!\n", session);
701 /* Must have a valid address to send to */
706 peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
707 if (peer_session == NULL)
710 cont (cont_cls, target, GNUNET_SYSERR);
714 udp = (struct UDPMessage *) mbuf;
715 udp->header.size = htons (mlen);
716 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
717 udp->reserved = htonl (0);
718 udp->sender = *plugin->env->my_identity;
719 memcpy (&udp[1], msgbuf, msgbuf_size);
723 mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
725 cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
726 GNUNET_free_non_null (peer_session);
730 GNUNET_assert (GNUNET_OK ==
731 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
736 GNUNET_FRAGMENT_context_create (plugin->env->stats, UDP_MTU,
738 plugin->last_expected_delay,
739 &udp->header, &send_fragment,
747 * Closure for 'process_inbound_tokenized_messages'
749 struct SourceInformation
754 struct GNUNET_PeerIdentity sender;
762 * Number of bytes in source address.
766 struct Session * session;
771 * Message tokenizer has broken up an incomming message. Pass it on
774 * @param cls the 'struct Plugin'
775 * @param client the 'struct SourceInformation'
776 * @param hdr the actual message
779 process_inbound_tokenized_messages (void *cls, void *client,
780 const struct GNUNET_MessageHeader *hdr)
782 struct Plugin *plugin = cls;
783 struct SourceInformation *si = client;
784 struct GNUNET_TRANSPORT_ATS_Information distance[2];
787 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
788 distance[0].value = htonl (1);
789 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
790 distance[1].value = htonl (0);
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "Giving Session %X %s to transport\n", si->session, GNUNET_i2s(&si->session->target));
794 plugin->env->receive (plugin->env->cls, &si->sender, hdr, distance, 2, si->session,
799 invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
801 struct Session * s = cls;
802 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
803 LOG (GNUNET_ERROR_TYPE_ERROR,
804 "Session %X (`%s') is now invalid\n", s, GNUNET_a2s (s->sock_addr,s->addrlen));
806 // s->plugin->env->session_end(s->plugin->env->cls, &s->target, s);
807 // GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(s->plugin->inbound_sessions, &s->target.hashPubKey, s));
813 * We've received a UDP Message. Process it (pass contents to main service).
815 * @param plugin plugin context
816 * @param msg the message
817 * @param sender_addr sender address
818 * @param sender_addr_len number of bytes in sender_addr
821 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
822 const struct sockaddr *sender_addr,
823 socklen_t sender_addr_len)
825 struct SourceInformation si;
826 struct IPv4UdpAddress u4;
827 struct IPv6UdpAddress u6;
831 if (0 != ntohl (msg->reserved))
836 if (ntohs (msg->header.size) <
837 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
843 /* convert address */
844 switch (sender_addr->sa_family)
847 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
848 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
849 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
854 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
855 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
856 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
865 LOG (GNUNET_ERROR_TYPE_DEBUG,
866 "Received message with %u bytes from peer `%s' at `%s'\n",
867 (unsigned int) ntohs (msg->header.size),
868 GNUNET_i2s (&msg->sender), GNUNET_a2s (sender_addr,
872 /* create a session for inbound connections */
873 const struct UDPMessage * udp_msg = (const struct UDPMessage *) msg;
874 LOG (GNUNET_ERROR_TYPE_DEBUG,
875 "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
876 GNUNET_i2s (&udp_msg->sender),
877 udp_address_to_string(NULL, arg, args));
879 struct Session * s = NULL;
880 s = find_inbound_session (plugin, &udp_msg->sender, sender_addr, sender_addr_len);
884 LOG (GNUNET_ERROR_TYPE_DEBUG,
885 "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
887 GNUNET_i2s (&s->target),
888 udp_address_to_string(NULL, arg, args));
892 s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
893 LOG (GNUNET_ERROR_TYPE_DEBUG,
894 "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
896 GNUNET_i2s (&s->target),
897 udp_address_to_string(NULL, arg, args));
899 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
900 &s->target.hashPubKey,
902 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
904 //s->valid_until = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
905 struct GNUNET_TIME_Relative delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10);
906 s->valid_until = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
907 if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
909 GNUNET_SCHEDULER_cancel(s->invalidation_task);
910 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
911 LOG (GNUNET_ERROR_TYPE_ERROR,
912 "Rescheduling %X' `%s'\n",
913 s, udp_address_to_string(NULL, arg, args));
915 s->invalidation_task = GNUNET_SCHEDULER_add_delayed(delay, &invalidation_task, s);
916 /* iterate over all embedded messages */
917 si.sender = msg->sender;
921 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
922 ntohs (msg->header.size) -
923 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
928 * Process a defragmented message.
930 * @param cls the 'struct ReceiveContext'
931 * @param msg the message
934 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
936 struct ReceiveContext *rc = cls;
938 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
943 if (ntohs (msg->size) < sizeof (struct UDPMessage))
948 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
949 rc->src_addr, rc->addr_len);
954 * Transmit an acknowledgement.
956 * @param cls the 'struct ReceiveContext'
957 * @param id message ID (unused)
958 * @param msg ack to transmit
961 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
963 struct ReceiveContext *rc = cls;
964 size_t msize = sizeof (struct UDPMessage) + ntohs (msg->size);
966 struct UDPMessage *udp;
969 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK to `%s'\n",
970 GNUNET_a2s (rc->src_addr,
971 (rc->src_addr->sa_family ==
972 AF_INET) ? sizeof (struct sockaddr_in) :
973 sizeof (struct sockaddr_in6)));
975 udp = (struct UDPMessage *) buf;
976 udp->header.size = htons ((uint16_t) msize);
977 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
978 udp->reserved = htonl (0);
979 udp->sender = *rc->plugin->env->my_identity;
980 memcpy (&udp[1], msg, ntohs (msg->size));
981 (void) udp_send (rc->plugin, rc->src_addr, &udp->header);
986 * Closure for 'find_receive_context'.
988 struct FindReceiveContext
991 * Where to store the result.
993 struct ReceiveContext *rc;
998 const struct sockaddr *addr;
1001 * Number of bytes in 'addr'.
1008 * Scan the heap for a receive context with the given address.
1010 * @param cls the 'struct FindReceiveContext'
1011 * @param node internal node of the heap
1012 * @param element value stored at the node (a 'struct ReceiveContext')
1013 * @param cost cost associated with the node
1014 * @return GNUNET_YES if we should continue to iterate,
1018 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1019 void *element, GNUNET_CONTAINER_HeapCostType cost)
1021 struct FindReceiveContext *frc = cls;
1022 struct ReceiveContext *e = element;
1024 if ((frc->addr_len == e->addr_len) &&
1025 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1035 * Read and process a message from the given socket.
1037 * @param plugin the overall plugin
1038 * @param rsock socket to read from
1041 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1047 const struct GNUNET_MessageHeader *msg;
1048 const struct GNUNET_MessageHeader *ack;
1049 struct Session *peer_session;
1050 const struct UDPMessage *udp;
1051 struct ReceiveContext *rc;
1052 struct GNUNET_TIME_Absolute now;
1053 struct FindReceiveContext frc;
1055 fromlen = sizeof (addr);
1056 memset (&addr, 0, sizeof (addr));
1058 GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1059 (struct sockaddr *) &addr, &fromlen);
1060 if (ret < sizeof (struct GNUNET_MessageHeader))
1062 GNUNET_break_op (0);
1065 msg = (const struct GNUNET_MessageHeader *) buf;
1067 LOG (GNUNET_ERROR_TYPE_DEBUG,
1068 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
1069 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs(msg->type));
1071 if (ret != ntohs (msg->size))
1073 GNUNET_break_op (0);
1076 switch (ntohs (msg->type))
1078 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1079 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1081 GNUNET_break_op (0);
1084 process_udp_message (plugin, (const struct UDPMessage *) msg,
1085 (const struct sockaddr *) addr, fromlen);
1087 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1088 if (ntohs (msg->size) <
1089 sizeof (struct UDPMessage) + sizeof (struct GNUNET_MessageHeader))
1091 GNUNET_break_op (0);
1094 udp = (const struct UDPMessage *) msg;
1095 if (ntohl (udp->reserved) != 0)
1097 GNUNET_break_op (0);
1100 ack = (const struct GNUNET_MessageHeader *) &udp[1];
1101 if (ntohs (ack->size) != ntohs (msg->size) - sizeof (struct UDPMessage))
1103 GNUNET_break_op (0);
1107 LOG (GNUNET_ERROR_TYPE_DEBUG,
1108 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1109 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp->sender),
1110 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1113 peer_session = find_session (plugin, &udp->sender);
1114 if (NULL == peer_session)
1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "Session for ACK not found, dropping ACK!\n");
1122 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (peer_session->frag, ack))
1124 GNUNET_assert (GNUNET_OK ==
1125 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1129 plugin->last_expected_delay =
1130 GNUNET_FRAGMENT_context_destroy (peer_session->frag);
1131 if (peer_session->cont != NULL)
1132 peer_session->cont (peer_session->cont_cls, &udp->sender, GNUNET_OK);
1133 GNUNET_free (peer_session);
1135 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1137 frc.addr = (const struct sockaddr *) addr;
1138 frc.addr_len = fromlen;
1139 GNUNET_CONTAINER_heap_iterate (plugin->defrags, &find_receive_context,
1141 now = GNUNET_TIME_absolute_get ();
1145 /* need to create a new RC */
1146 rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen);
1147 memcpy (&rc[1], addr, fromlen);
1148 rc->src_addr = (const struct sockaddr *) &rc[1];
1149 rc->addr_len = fromlen;
1150 rc->plugin = plugin;
1152 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1153 UDP_MAX_MESSAGES_IN_DEFRAG, rc,
1154 &fragment_msg_proc, &ack_proc);
1156 GNUNET_CONTAINER_heap_insert (plugin->defrags, rc,
1157 (GNUNET_CONTAINER_HeapCostType)
1161 LOG (GNUNET_ERROR_TYPE_DEBUG,
1162 "UDP processes %u-byte fragment from `%s'\n",
1163 (unsigned int) ntohs (msg->size),
1164 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1167 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (rc->defrag, msg))
1169 /* keep this 'rc' from expiring */
1170 GNUNET_CONTAINER_heap_update_cost (plugin->defrags, rc->hnode,
1171 (GNUNET_CONTAINER_HeapCostType)
1174 if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) >
1175 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1177 /* remove 'rc' that was inactive the longest */
1178 rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags);
1179 GNUNET_assert (NULL != rc);
1180 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1185 GNUNET_break_op (0);
1192 * We have been notified that our writeset has something to read. We don't
1193 * know which socket needs to be read, so we have to check each one
1194 * Then reschedule this function to be called again once more is available.
1196 * @param cls the plugin handle
1197 * @param tc the scheduling context (for rescheduling this function again)
1200 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1202 struct Plugin *plugin = cls;
1204 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1205 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1207 if ((NULL != plugin->sockv4) &&
1208 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1209 udp_read (plugin, plugin->sockv4);
1210 if ((NULL != plugin->sockv6) &&
1211 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1212 udp_read (plugin, plugin->sockv6);
1213 plugin->select_task =
1214 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1215 GNUNET_SCHEDULER_NO_TASK,
1216 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1217 NULL, &udp_plugin_select, plugin);
1223 * Check if the given port is plausible (must be either our listen
1224 * port or our advertised port). If it is neither, we return
1227 * @param plugin global variables
1228 * @param in_port port number to check
1229 * @return GNUNET_OK if port is either open_port or adv_port
1232 check_port (struct Plugin *plugin, uint16_t in_port)
1234 if ((in_port == plugin->port) || (in_port == plugin->aport))
1236 return GNUNET_SYSERR;
1241 * Function that will be called to check if a binary address for this
1242 * plugin is well-formed and corresponds to an address for THIS peer
1243 * (as per our configuration). Naturally, if absolutely necessary,
1244 * plugins can be a bit conservative in their answer, but in general
1245 * plugins should make sure that the address does not redirect
1246 * traffic to a 3rd party that might try to man-in-the-middle our
1249 * @param cls closure, should be our handle to the Plugin
1250 * @param addr pointer to the address
1251 * @param addrlen length of addr
1252 * @return GNUNET_OK if this is a plausible address for this peer
1253 * and transport, GNUNET_SYSERR if not
1257 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1259 struct Plugin *plugin = cls;
1260 struct IPv4UdpAddress *v4;
1261 struct IPv6UdpAddress *v6;
1263 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1264 (addrlen != sizeof (struct IPv6UdpAddress)))
1266 GNUNET_break_op (0);
1267 return GNUNET_SYSERR;
1269 if (addrlen == sizeof (struct IPv4UdpAddress))
1271 v4 = (struct IPv4UdpAddress *) addr;
1272 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1273 return GNUNET_SYSERR;
1275 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1276 sizeof (struct in_addr)))
1277 return GNUNET_SYSERR;
1281 v6 = (struct IPv6UdpAddress *) addr;
1282 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1284 GNUNET_break_op (0);
1285 return GNUNET_SYSERR;
1287 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1288 return GNUNET_SYSERR;
1290 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1291 sizeof (struct in6_addr)))
1292 return GNUNET_SYSERR;
1299 * Function called for a quick conversion of the binary address to
1300 * a numeric address. Note that the caller must not free the
1301 * address and that the next call to this function is allowed
1302 * to override the address again.
1304 * @param cls closure
1305 * @param addr binary address
1306 * @param addrlen length of the address
1307 * @return string representing the same address
1310 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
1312 static char rbuf[INET6_ADDRSTRLEN + 10];
1313 char buf[INET6_ADDRSTRLEN];
1317 const struct IPv4UdpAddress *t4;
1318 const struct IPv6UdpAddress *t6;
1322 if (addrlen == sizeof (struct IPv6UdpAddress))
1326 port = ntohs (t6->u6_port);
1327 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1330 else if (addrlen == sizeof (struct IPv4UdpAddress))
1334 port = ntohs (t4->u4_port);
1335 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1340 GNUNET_break_op (0);
1343 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1344 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
1351 * Closure for 'append_port'.
1353 struct PrettyPrinterContext
1356 * Function to call with the result.
1358 GNUNET_TRANSPORT_AddressStringCallback asc;
1361 * Clsoure for 'asc'.
1366 * Port to add after the IP address.
1373 * Append our port and forward the result.
1375 * @param cls a 'struct PrettyPrinterContext'
1376 * @param hostname result from DNS resolver
1379 append_port (void *cls, const char *hostname)
1381 struct PrettyPrinterContext *ppc = cls;
1384 if (hostname == NULL)
1386 ppc->asc (ppc->asc_cls, NULL);
1390 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1391 ppc->asc (ppc->asc_cls, ret);
1397 * Convert the transports address to a nice, human-readable
1400 * @param cls closure
1401 * @param type name of the transport that generated the address
1402 * @param addr one of the addresses of the host, NULL for the last address
1403 * the specific address format depends on the transport
1404 * @param addrlen length of the address
1405 * @param numeric should (IP) addresses be displayed in numeric form?
1406 * @param timeout after how long should we give up?
1407 * @param asc function to call on each string
1408 * @param asc_cls closure for asc
1411 udp_plugin_address_pretty_printer (void *cls, const char *type,
1412 const void *addr, size_t addrlen,
1414 struct GNUNET_TIME_Relative timeout,
1415 GNUNET_TRANSPORT_AddressStringCallback asc,
1418 struct PrettyPrinterContext *ppc;
1421 struct sockaddr_in a4;
1422 struct sockaddr_in6 a6;
1423 const struct IPv4UdpAddress *u4;
1424 const struct IPv6UdpAddress *u6;
1427 if (addrlen == sizeof (struct IPv6UdpAddress))
1430 memset (&a6, 0, sizeof (a6));
1431 a6.sin6_family = AF_INET6;
1432 #if HAVE_SOCKADDR_IN_SIN_LEN
1433 a6.sin6_len = sizeof (a6);
1435 a6.sin6_port = u6->u6_port;
1436 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
1437 port = ntohs (u6->u6_port);
1441 else if (addrlen == sizeof (struct IPv4UdpAddress))
1444 memset (&a4, 0, sizeof (a4));
1445 a4.sin_family = AF_INET;
1446 #if HAVE_SOCKADDR_IN_SIN_LEN
1447 a4.sin_len = sizeof (a4);
1449 a4.sin_port = u4->u4_port;
1450 a4.sin_addr.s_addr = u4->ipv4_addr;
1451 port = ntohs (u4->u4_port);
1457 /* invalid address */
1458 GNUNET_break_op (0);
1459 asc (asc_cls, NULL);
1462 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1464 ppc->asc_cls = asc_cls;
1466 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1471 * Our external IP address/port mapping has changed.
1473 * @param cls closure, the 'struct LocalAddrList'
1474 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1475 * the previous (now invalid) one
1476 * @param addr either the previous or the new public IP address
1477 * @param addrlen actual lenght of the address
1480 udp_nat_port_map_callback (void *cls, int add_remove,
1481 const struct sockaddr *addr, socklen_t addrlen)
1483 struct Plugin *plugin = cls;
1484 struct IPv4UdpAddress u4;
1485 struct IPv6UdpAddress u6;
1489 /* convert 'addr' to our internal format */
1490 switch (addr->sa_family)
1493 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1494 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1495 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1500 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1501 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1502 sizeof (struct in6_addr));
1503 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1511 /* modify our published address list */
1512 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1517 * The exported method. Makes the core api available via a global and
1518 * returns the udp transport API.
1520 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1521 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1524 libgnunet_plugin_transport_udp_init (void *cls)
1526 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1527 unsigned long long port;
1528 unsigned long long aport;
1529 struct GNUNET_TRANSPORT_PluginFunctions *api;
1530 struct Plugin *plugin;
1531 int sockets_created;
1532 struct sockaddr_in serverAddrv4;
1533 struct sockaddr_in6 serverAddrv6;
1534 struct sockaddr *serverAddr;
1535 struct sockaddr *addrs[2];
1536 socklen_t addrlens[2];
1539 unsigned long long udp_max_bps;
1542 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1546 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1547 "MAX_BPS", &udp_max_bps))
1548 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1550 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1551 "ADVERTISED_PORT", &aport))
1555 LOG (GNUNET_ERROR_TYPE_WARNING,
1556 _("Given `%s' option is out of range: %llu > %u\n"), "PORT",
1560 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1561 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1563 plugin = GNUNET_malloc (sizeof (struct Plugin));
1564 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1565 GNUNET_BANDWIDTH_value_init ((uint32_t)
1567 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1568 plugin->port = port;
1569 plugin->aport = aport;
1571 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1574 api->send = &udp_plugin_send;
1575 api->disconnect = &udp_disconnect;
1576 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1577 api->address_to_string = &udp_address_to_string;
1578 api->check_address = &udp_plugin_check_address;
1581 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1582 "BINDTO", &plugin->bind4_address))
1584 LOG (GNUNET_ERROR_TYPE_DEBUG,
1585 "Binding udp plugin to specific address: `%s'\n",
1586 plugin->bind4_address);
1587 if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
1589 GNUNET_free (plugin->bind4_address);
1590 GNUNET_free (plugin);
1597 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1598 "BINDTO6", &plugin->bind6_address))
1600 LOG (GNUNET_ERROR_TYPE_DEBUG,
1601 "Binding udp plugin to specific address: `%s'\n",
1602 plugin->bind6_address);
1604 inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1606 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1607 plugin->bind6_address);
1608 GNUNET_free_non_null (plugin->bind4_address);
1609 GNUNET_free (plugin->bind6_address);
1610 GNUNET_free (plugin);
1617 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1619 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1621 plugin->inbound_sessions =
1622 GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
1624 sockets_created = 0;
1626 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
1629 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1630 if (NULL == plugin->sockv6)
1632 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1636 #if HAVE_SOCKADDR_IN_SIN_LEN
1637 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1639 serverAddrv6.sin6_family = AF_INET6;
1640 serverAddrv6.sin6_addr = in6addr_any;
1641 serverAddrv6.sin6_port = htons (plugin->port);
1642 addrlen = sizeof (serverAddrv6);
1643 serverAddr = (struct sockaddr *) &serverAddrv6;
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1646 ntohs (serverAddrv6.sin6_port));
1649 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1652 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1654 LOG (GNUNET_ERROR_TYPE_DEBUG,
1655 "IPv6 Binding failed, trying new port %d\n",
1656 ntohs (serverAddrv6.sin6_port));
1661 GNUNET_NETWORK_socket_close (plugin->sockv6);
1662 plugin->sockv6 = NULL;
1666 if (plugin->sockv6 != NULL)
1668 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
1669 addrlens[sockets_created] = sizeof (serverAddrv6);
1676 GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1677 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1678 if (NULL == plugin->sockv4)
1680 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1684 #if HAVE_SOCKADDR_IN_SIN_LEN
1685 serverAddrv4.sin_len = sizeof (serverAddrv4);
1687 serverAddrv4.sin_family = AF_INET;
1688 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1689 serverAddrv4.sin_port = htons (plugin->port);
1690 addrlen = sizeof (serverAddrv4);
1691 serverAddr = (struct sockaddr *) &serverAddrv4;
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1694 ntohs (serverAddrv4.sin_port));
1697 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1700 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1702 LOG (GNUNET_ERROR_TYPE_DEBUG,
1703 "IPv4 Binding failed, trying new port %d\n",
1704 ntohs (serverAddrv4.sin_port));
1709 GNUNET_NETWORK_socket_close (plugin->sockv4);
1710 plugin->sockv4 = NULL;
1714 if (plugin->sockv4 != NULL)
1716 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
1717 addrlens[sockets_created] = sizeof (serverAddrv4);
1722 plugin->rs = GNUNET_NETWORK_fdset_create ();
1723 GNUNET_NETWORK_fdset_zero (plugin->rs);
1724 if (NULL != plugin->sockv4)
1725 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1726 if (NULL != plugin->sockv6)
1727 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1729 plugin->select_task =
1730 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1731 GNUNET_SCHEDULER_NO_TASK,
1732 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1733 NULL, &udp_plugin_select, plugin);
1734 if (sockets_created == 0)
1735 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1737 GNUNET_NAT_register (env->cfg, GNUNET_NO, port, sockets_created,
1738 (const struct sockaddr **) addrs, addrlens,
1739 &udp_nat_port_map_callback, NULL, plugin);
1745 static void invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1747 struct Session * s = cls;
1748 struct Plugin * plugin = s->plugin;
1750 s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
1752 GNUNET_CONTAINER_multihashmap_remove (plugin->inbound_sessions, &s->target.hashPubKey, s);
1755 plugin->env->session_end (plugin->env->cls, &s->target, s);
1756 LOG (GNUNET_ERROR_TYPE_ERROR,
1757 "Session %X is now invalid\n", s);
1758 destroy_session(s, &s->target.hashPubKey, s);
1765 * Shutdown the plugin.
1767 * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions'
1771 libgnunet_plugin_transport_udp_done (void *cls)
1773 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1774 struct Plugin *plugin = api->cls;
1775 struct ReceiveContext *rc;
1777 /* FIXME: clean up heap and hashmap */
1778 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &destroy_session,
1780 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1781 plugin->sessions = NULL;
1782 GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions, &destroy_inbound_session,
1784 GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
1785 plugin->inbound_sessions = NULL;
1786 while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
1788 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
1791 GNUNET_CONTAINER_heap_destroy (plugin->defrags);
1793 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1795 GNUNET_SCHEDULER_cancel (plugin->select_task);
1796 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1798 if (plugin->sockv4 != NULL)
1800 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1801 plugin->sockv4 = NULL;
1803 if (plugin->sockv6 != NULL)
1805 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1806 plugin->sockv6 = NULL;
1808 GNUNET_SERVER_mst_destroy (plugin->mst);
1809 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1810 GNUNET_NAT_unregister (plugin->nat);
1812 GNUNET_free (plugin);
1817 /* end of plugin_transport_udp.c */