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 transport protocol
24 * @author Christian Grothoff
25 * @author Nathan Evans
26 * @author Matthias Wachs
29 #include "plugin_transport_udp_new.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_fragmentation_lib.h"
33 #include "gnunet_nat_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_constants.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "gnunet_transport_plugin.h"
41 #include "transport.h"
43 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
47 * Closure for 'append_port'.
49 struct PrettyPrinterContext
52 * Function to call with the result.
54 GNUNET_TRANSPORT_AddressStringCallback asc;
62 * Port to add after the IP address.
70 * Which peer is this session for?
72 struct GNUNET_PeerIdentity target;
75 * Address of the other peer
77 const struct sockaddr *sock_addr;
82 * Desired delay for next sending we received from other peer
84 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
88 struct SessionCompareContext
91 const struct GNUNET_HELLO_Address *addr;
96 * Closure for 'process_inbound_tokenized_messages'
98 struct SourceInformation
103 struct GNUNET_PeerIdentity sender;
111 * Number of bytes in source address.
115 struct Session *session;
120 * Function called for a quick conversion of the binary address to
121 * a numeric address. Note that the caller must not free the
122 * address and that the next call to this function is allowed
123 * to override the address again.
126 * @param addr binary address
127 * @param addrlen length of the address
128 * @return string representing the same address
131 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
133 static char rbuf[INET6_ADDRSTRLEN + 10];
134 char buf[INET6_ADDRSTRLEN];
138 const struct IPv4UdpAddress *t4;
139 const struct IPv6UdpAddress *t6;
143 if (addrlen == sizeof (struct IPv6UdpAddress))
147 port = ntohs (t6->u6_port);
148 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
151 else if (addrlen == sizeof (struct IPv4UdpAddress))
155 port = ntohs (t4->u4_port);
156 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
164 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
165 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
172 * Append our port and forward the result.
174 * @param cls a 'struct PrettyPrinterContext'
175 * @param hostname result from DNS resolver
178 append_port (void *cls, const char *hostname)
180 struct PrettyPrinterContext *ppc = cls;
183 if (hostname == NULL)
185 ppc->asc (ppc->asc_cls, NULL);
189 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
190 ppc->asc (ppc->asc_cls, ret);
196 * Convert the transports address to a nice, human-readable
200 * @param type name of the transport that generated the address
201 * @param addr one of the addresses of the host, NULL for the last address
202 * the specific address format depends on the transport
203 * @param addrlen length of the address
204 * @param numeric should (IP) addresses be displayed in numeric form?
205 * @param timeout after how long should we give up?
206 * @param asc function to call on each string
207 * @param asc_cls closure for asc
210 udp_plugin_address_pretty_printer (void *cls, const char *type,
211 const void *addr, size_t addrlen,
213 struct GNUNET_TIME_Relative timeout,
214 GNUNET_TRANSPORT_AddressStringCallback asc,
217 struct PrettyPrinterContext *ppc;
220 struct sockaddr_in a4;
221 struct sockaddr_in6 a6;
222 const struct IPv4UdpAddress *u4;
223 const struct IPv6UdpAddress *u6;
226 if (addrlen == sizeof (struct IPv6UdpAddress))
229 memset (&a6, 0, sizeof (a6));
230 a6.sin6_family = AF_INET6;
231 #if HAVE_SOCKADDR_IN_SIN_LEN
232 a6.sin6_len = sizeof (a6);
234 a6.sin6_port = u6->u6_port;
235 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
236 port = ntohs (u6->u6_port);
240 else if (addrlen == sizeof (struct IPv4UdpAddress))
243 memset (&a4, 0, sizeof (a4));
244 a4.sin_family = AF_INET;
245 #if HAVE_SOCKADDR_IN_SIN_LEN
246 a4.sin_len = sizeof (a4);
248 a4.sin_port = u4->u4_port;
249 a4.sin_addr.s_addr = u4->ipv4_addr;
250 port = ntohs (u4->u4_port);
256 /* invalid address */
261 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
263 ppc->asc_cls = asc_cls;
265 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
270 * Check if the given port is plausible (must be either our listen
271 * port or our advertised port). If it is neither, we return
274 * @param plugin global variables
275 * @param in_port port number to check
276 * @return GNUNET_OK if port is either open_port or adv_port
279 check_port (struct Plugin *plugin, uint16_t in_port)
281 if ((in_port == plugin->port) || (in_port == plugin->aport))
283 return GNUNET_SYSERR;
289 * Function that will be called to check if a binary address for this
290 * plugin is well-formed and corresponds to an address for THIS peer
291 * (as per our configuration). Naturally, if absolutely necessary,
292 * plugins can be a bit conservative in their answer, but in general
293 * plugins should make sure that the address does not redirect
294 * traffic to a 3rd party that might try to man-in-the-middle our
297 * @param cls closure, should be our handle to the Plugin
298 * @param addr pointer to the address
299 * @param addrlen length of addr
300 * @return GNUNET_OK if this is a plausible address for this peer
301 * and transport, GNUNET_SYSERR if not
305 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
307 struct Plugin *plugin = cls;
308 struct IPv4UdpAddress *v4;
309 struct IPv6UdpAddress *v6;
311 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
312 (addrlen != sizeof (struct IPv6UdpAddress)))
315 return GNUNET_SYSERR;
317 if (addrlen == sizeof (struct IPv4UdpAddress))
319 v4 = (struct IPv4UdpAddress *) addr;
320 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
321 return GNUNET_SYSERR;
323 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
324 sizeof (struct in_addr)))
325 return GNUNET_SYSERR;
329 v6 = (struct IPv6UdpAddress *) addr;
330 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
333 return GNUNET_SYSERR;
335 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
336 return GNUNET_SYSERR;
338 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
339 sizeof (struct in6_addr)))
340 return GNUNET_SYSERR;
347 * Destroy a session, plugin is being unloaded.
350 * @param key hash of public key of target peer
351 * @param value a 'struct PeerSession*' to clean up
352 * @return GNUNET_OK (continue to iterate)
355 disconnect_and_free_it (void *cls, const GNUNET_HashCode * key, void *value)
357 struct Plugin *plugin = cls;
358 struct Session *s = value;
361 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "Session %p to peer `%s' address ended \n",
364 GNUNET_i2s (&s->target),
365 GNUNET_a2s (s->sock_addr, s->addrlen));
368 plugin->env->session_end (plugin->env->cls, &s->target, s);
370 GNUNET_assert (GNUNET_YES ==
371 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
372 &s->target.hashPubKey,
381 * Disconnect from a remote node. Clean up session if we have one for this peer
383 * @param cls closure for this call (should be handle to Plugin)
384 * @param target the peeridentity of the peer to disconnect
385 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
388 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
390 struct Plugin *plugin = cls;
391 GNUNET_assert (plugin != NULL);
393 GNUNET_assert (target != NULL);
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
398 /* Clean up sessions */
399 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
403 static struct Session *
404 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
405 const void *addr, size_t addrlen,
406 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
409 const struct IPv4UdpAddress *t4;
410 const struct IPv6UdpAddress *t6;
411 struct sockaddr_in *v4;
412 struct sockaddr_in6 *v6;
414 struct GNUNET_ATS_Information ats;
418 case sizeof (struct IPv4UdpAddress):
419 if (NULL == plugin->sockv4)
424 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
425 len = sizeof (struct sockaddr_in);
426 v4 = (struct sockaddr_in *) &s[1];
427 v4->sin_family = AF_INET;
428 #if HAVE_SOCKADDR_IN_SIN_LEN
429 v4->sin_len = sizeof (struct sockaddr_in);
431 v4->sin_port = t4->u4_port;
432 v4->sin_addr.s_addr = t4->ipv4_addr;
433 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
435 case sizeof (struct IPv6UdpAddress):
436 if (NULL == plugin->sockv6)
442 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
443 len = sizeof (struct sockaddr_in6);
444 v6 = (struct sockaddr_in6 *) &s[1];
445 v6->sin6_family = AF_INET6;
446 #if HAVE_SOCKADDR_IN_SIN_LEN
447 v6->sin6_len = sizeof (struct sockaddr_in6);
449 v6->sin6_port = t6->u6_port;
450 v6->sin6_addr = t6->ipv6_addr;
451 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
454 /* Must have a valid address to send to */
461 s->sock_addr = (const struct sockaddr *) &s[1];
466 static int session_cmp_it (void *cls,
467 const GNUNET_HashCode * key,
470 struct SessionCompareContext * cctx = cls;
471 const struct GNUNET_HELLO_Address *address = cctx->addr;
472 struct Session *s = value;
474 socklen_t s_addrlen = s->addrlen;
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
478 udp_address_to_string (NULL, (void *) address->address, address->address_length),
479 GNUNET_a2s (s->sock_addr, s->addrlen));
482 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
483 (s_addrlen == sizeof (struct sockaddr_in)))
485 struct IPv4UdpAddress * u4 = NULL;
486 u4 = (struct IPv4UdpAddress *) address->address;
487 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
488 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
489 (u4->u4_port == s4->sin_port))
496 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
497 (s_addrlen == sizeof (struct sockaddr_in6)))
499 struct IPv6UdpAddress * u6 = NULL;
500 u6 = (struct IPv6UdpAddress *) address->address;
501 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
502 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
503 (u6->u6_port == s6->sin6_port))
516 * Creates a new outbound session the transport service will use to send data to the
519 * @param cls the plugin
520 * @param address the address
521 * @return the session or NULL of max connections exceeded
523 static struct Session *
524 udp_plugin_get_session (void *cls,
525 const struct GNUNET_HELLO_Address *address)
527 struct Session * s = NULL;
528 struct Plugin * plugin = cls;
530 GNUNET_assert (plugin != NULL);
531 GNUNET_assert (address != NULL);
533 if ((address->address == NULL) ||
534 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
535 (address->address_length != sizeof (struct IPv6UdpAddress))))
541 /* check if session already exists */
542 struct SessionCompareContext cctx;
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for existing session for peer `%s' `%s' \n", GNUNET_i2s (&address->peer), udp_address_to_string(NULL, address->address, address->address_length));
548 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
549 if (cctx.res != NULL)
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
557 /* otherwise create new */
558 s = create_session (plugin,
561 address->address_length,
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "Creating new session %p for peer `%s' address `%s'\n",
567 GNUNET_i2s(&address->peer),
568 udp_address_to_string(NULL,address->address,address->address_length));
570 GNUNET_assert (GNUNET_OK ==
571 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
572 &s->target.hashPubKey,
574 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
581 * Function that can be used by the transport service to transmit
582 * a message using the plugin. Note that in the case of a
583 * peer disconnecting, the continuation MUST be called
584 * prior to the disconnect notification itself. This function
585 * will be called with this peer's HELLO message to initiate
586 * a fresh connection to another peer.
589 * @param session which session must be used
590 * @param msgbuf the message to transmit
591 * @param msgbuf_size number of bytes in 'msgbuf'
592 * @param priority how important is the message (most plugins will
593 * ignore message priority and just FIFO)
594 * @param to how long to wait at most for the transmission (does not
595 * require plugins to discard the message after the timeout,
596 * just advisory for the desired delay; most plugins will ignore
598 * @param cont continuation to call once the message has
599 * been transmitted (or if the transport is ready
600 * for the next transmission call; or if the
601 * peer disconnected...); can be NULL
602 * @param cont_cls closure for cont
603 * @return number of bytes used (on the physical network, with overheads);
604 * -1 on hard errors (i.e. address invalid); 0 is a legal value
605 * and does NOT mean that the message was not transmitted (DV)
608 udp_plugin_send (void *cls,
610 const char *msgbuf, size_t msgbuf_size,
611 unsigned int priority,
612 struct GNUNET_TIME_Relative to,
613 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
615 struct Plugin *plugin = cls;
616 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);;
618 struct GNUNET_TIME_Relative delta;
619 struct UDPMessageWrapper * udpw;
620 struct UDPMessage *udp;
622 GNUNET_assert (plugin != NULL);
623 GNUNET_assert (s != NULL);
625 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
628 return GNUNET_SYSERR;
631 LOG (GNUNET_ERROR_TYPE_DEBUG,
632 "UDP transmits %u-byte message to `%s' using address `%s'\n",
634 GNUNET_i2s (&s->target),
635 GNUNET_a2s(s->sock_addr, s->addrlen));
637 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
640 return GNUNET_SYSERR;
643 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + sizeof (struct UDPMessage) + msgbuf_size);
645 udp = (struct UDPMessage *) &udpw[1];
647 udpw->msg_size = mlen;
649 udpw->cont_cls = cont_cls;
651 udp->header.size = htons (mlen);
652 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
653 udp->reserved = htonl (0);
654 udp->sender = *plugin->env->my_identity;
655 memcpy (&udp[1], msgbuf, msgbuf_size);
657 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, udpw);
659 delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
663 static ssize_t udp_plugin_send_wrapper (void *cls,
665 GNUNET_PeerIdentity *
673 struct Session * session,
677 GNUNET_TRANSPORT_TransmitContinuation
678 cont, void *cont_cls)
681 struct Session * s = NULL;
682 struct GNUNET_HELLO_Address * ha = NULL;
684 ha = GNUNET_HELLO_address_allocate(target, "", addr,addrlen);
685 GNUNET_assert (ha != NULL);
686 s = udp_plugin_get_session(cls, ha);
687 GNUNET_assert (s != NULL);
689 ret = udp_plugin_send (cls, s, msgbuf, msgbuf_size, priority, timeout, cont, cont_cls);
696 * Our external IP address/port mapping has changed.
698 * @param cls closure, the 'struct LocalAddrList'
699 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
700 * the previous (now invalid) one
701 * @param addr either the previous or the new public IP address
702 * @param addrlen actual lenght of the address
705 udp_nat_port_map_callback (void *cls, int add_remove,
706 const struct sockaddr *addr, socklen_t addrlen)
708 struct Plugin *plugin = cls;
709 struct IPv4UdpAddress u4;
710 struct IPv6UdpAddress u6;
714 /* convert 'addr' to our internal format */
715 switch (addr->sa_family)
718 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
719 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
720 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
725 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
726 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
727 sizeof (struct in6_addr));
728 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
736 /* modify our published address list */
737 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
743 * Message tokenizer has broken up an incomming message. Pass it on
746 * @param cls the 'struct Plugin'
747 * @param client the 'struct SourceInformation'
748 * @param hdr the actual message
751 process_inbound_tokenized_messages (void *cls, void *client,
752 const struct GNUNET_MessageHeader *hdr)
754 struct Plugin *plugin = cls;
755 struct SourceInformation *si = client;
756 struct GNUNET_ATS_Information ats[2];
757 struct GNUNET_TIME_Relative delay;
760 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
761 ats[0].value = htonl (1);
762 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
763 ats[1].value = htonl (GNUNET_ATS_COST_WAN);
764 //GNUNET_break (ntohl(si->session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
765 delay = plugin->env->receive (plugin->env->cls,
768 (const struct GNUNET_ATS_Information *) &ats, 2,
772 //si->session->flow_delay_for_other_peer = delay;
777 * We've received a UDP Message. Process it (pass contents to main service).
779 * @param plugin plugin context
780 * @param msg the message
781 * @param sender_addr sender address
782 * @param sender_addr_len number of bytes in sender_addr
785 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
786 const struct sockaddr *sender_addr,
787 socklen_t sender_addr_len)
789 struct SourceInformation si;
790 struct IPv4UdpAddress u4;
791 struct IPv6UdpAddress u6;
792 struct GNUNET_ATS_Information ats;
796 if (0 != ntohl (msg->reserved))
801 if (ntohs (msg->header.size) <
802 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
808 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
809 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
810 /* convert address */
811 switch (sender_addr->sa_family)
814 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
815 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
816 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
821 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
822 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
823 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
832 LOG (GNUNET_ERROR_TYPE_DEBUG,
833 "Received message with %u bytes from peer `%s' at `%s'\n",
834 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
835 GNUNET_a2s (sender_addr, sender_addr_len));
838 /* iterate over all embedded messages */
839 si.sender = msg->sender;
843 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
844 ntohs (msg->header.size) -
845 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
850 * Read and process a message from the given socket.
852 * @param plugin the overall plugin
853 * @param rsock socket to read from
856 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
862 const struct GNUNET_MessageHeader *msg;
863 //const struct GNUNET_MessageHeader *ack;
864 //struct Session *peer_session;
865 //const struct UDP_ACK_Message *udp_ack;
866 //struct ReceiveContext *rc;
867 //struct GNUNET_TIME_Absolute now;
868 //struct FindReceiveContext frc;
869 //struct Session *s = NULL;
870 //struct GNUNET_TIME_Relative flow_delay;
871 //struct GNUNET_ATS_Information ats;
873 fromlen = sizeof (addr);
874 memset (&addr, 0, sizeof (addr));
875 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
876 (struct sockaddr *) &addr, &fromlen);
877 if (size < sizeof (struct GNUNET_MessageHeader))
882 msg = (const struct GNUNET_MessageHeader *) buf;
884 LOG (GNUNET_ERROR_TYPE_DEBUG,
885 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
886 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
888 if (size != ntohs (msg->size))
893 switch (ntohs (msg->type))
895 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
897 udp_broadcast_receive(plugin, &buf, size, addr, fromlen);
900 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
901 if (ntohs (msg->size) < sizeof (struct UDPMessage))
907 process_udp_message (plugin, (const struct UDPMessage *) msg,
908 (const struct sockaddr *) addr, fromlen);
918 udp_select_send (struct Plugin *plugin)
923 struct UDPMessageWrapper *udpw = plugin->msg_head;
924 const struct sockaddr * sa = udpw->session->sock_addr;
926 switch (sa->sa_family)
929 if (NULL == plugin->sockv4)
932 GNUNET_NETWORK_socket_sendto (plugin->sockv4, udpw->udp, udpw->msg_size,
933 sa, slen = sizeof (struct sockaddr_in));
936 if (NULL == plugin->sockv6)
939 GNUNET_NETWORK_socket_sendto (plugin->sockv6, udpw->udp, udpw->msg_size,
940 sa, slen = sizeof (struct sockaddr_in6));
946 if (GNUNET_SYSERR == sent)
948 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
949 LOG (GNUNET_ERROR_TYPE_ERROR,
950 "UDP transmitted %u-byte message to %s (%d: %s)\n",
951 (unsigned int) ntohs (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
952 (sent < 0) ? STRERROR (errno) : "ok");
953 if (udpw->cont != NULL)
954 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
956 LOG (GNUNET_ERROR_TYPE_DEBUG,
957 "UDP transmitted %u-byte message to %s (%d: %s)\n",
958 (unsigned int) ntohs (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
959 (sent < 0) ? STRERROR (errno) : "ok");
961 if (udpw->cont != NULL)
962 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_OK);
964 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
971 * We have been notified that our readset has something to read. We don't
972 * know which socket needs to be read, so we have to check each one
973 * Then reschedule this function to be called again once more is available.
975 * @param cls the plugin handle
976 * @param tc the scheduling context (for rescheduling this function again)
979 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
981 struct Plugin *plugin = cls;
983 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
984 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
987 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
989 if ((NULL != plugin->sockv4) &&
990 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
991 udp_select_read (plugin, plugin->sockv4);
992 if ((NULL != plugin->sockv6) &&
993 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
994 udp_select_read (plugin, plugin->sockv6);
997 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
999 if (plugin->msg_head != NULL)
1000 udp_select_send (plugin);
1003 plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1004 GNUNET_SCHEDULER_NO_TASK,
1005 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1006 plugin->ws, &udp_plugin_select, plugin);
1012 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
1015 int sockets_created = 0;
1016 struct sockaddr *serverAddr;
1017 struct sockaddr *addrs[2];
1018 socklen_t addrlens[2];
1021 /* Create IPv6 socket */
1022 if (plugin->enable_ipv6 == GNUNET_YES)
1024 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1025 if (NULL == plugin->sockv6)
1027 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1031 #if HAVE_SOCKADDR_IN_SIN_LEN
1032 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1034 serverAddrv6->sin6_family = AF_INET6;
1035 serverAddrv6->sin6_addr = in6addr_any;
1036 serverAddrv6->sin6_port = htons (plugin->port);
1037 addrlen = sizeof (struct sockaddr_in6);
1038 serverAddr = (struct sockaddr *) serverAddrv6;
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1041 ntohs (serverAddrv6->sin6_port));
1044 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1047 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1049 LOG (GNUNET_ERROR_TYPE_DEBUG,
1050 "IPv6 Binding failed, trying new port %d\n",
1051 ntohs (serverAddrv6->sin6_port));
1056 GNUNET_NETWORK_socket_close (plugin->sockv6);
1057 plugin->sockv6 = NULL;
1061 if (plugin->sockv6 != NULL)
1064 LOG (GNUNET_ERROR_TYPE_DEBUG,
1065 "IPv6 socket created on port %d\n",
1066 ntohs (serverAddrv6->sin6_port));
1068 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
1069 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
1075 /* Create IPv4 socket */
1076 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1077 if (NULL == plugin->sockv4)
1079 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1083 #if HAVE_SOCKADDR_IN_SIN_LEN
1084 serverAddrv4.sin_len = sizeof (serverAddrv4);
1086 serverAddrv4->sin_family = AF_INET;
1087 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
1088 serverAddrv4->sin_port = htons (plugin->port);
1089 addrlen = sizeof (struct sockaddr_in);
1090 serverAddr = (struct sockaddr *) serverAddrv4;
1093 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1094 ntohs (serverAddrv4->sin_port));
1097 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1100 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
1103 ntohs (serverAddrv4->sin_port));
1108 GNUNET_NETWORK_socket_close (plugin->sockv4);
1109 plugin->sockv4 = NULL;
1113 if (plugin->sockv4 != NULL)
1115 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
1116 addrlens[sockets_created] = sizeof (struct sockaddr_in);
1121 /* Create file descriptors */
1122 plugin->rs = GNUNET_NETWORK_fdset_create ();
1123 plugin->ws = GNUNET_NETWORK_fdset_create ();
1124 GNUNET_NETWORK_fdset_zero (plugin->rs);
1125 GNUNET_NETWORK_fdset_zero (plugin->ws);
1126 if (NULL != plugin->sockv4)
1128 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1129 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv4);
1131 if (NULL != plugin->sockv6)
1133 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1134 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv6);
1137 if (sockets_created == 0)
1138 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1140 plugin->select_task =
1141 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1142 GNUNET_SCHEDULER_NO_TASK,
1143 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1144 plugin->ws, &udp_plugin_select, plugin);
1146 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
1147 GNUNET_NO, plugin->port,
1149 (const struct sockaddr **) addrs, addrlens,
1150 &udp_nat_port_map_callback, NULL, plugin);
1152 return sockets_created;
1157 * The exported method. Makes the core api available via a global and
1158 * returns the udp transport API.
1160 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1161 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1164 libgnunet_plugin_transport_udp_init (void *cls)
1166 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1167 struct GNUNET_TRANSPORT_PluginFunctions *api;
1168 struct Plugin *plugin;
1170 unsigned long long port;
1171 unsigned long long aport;
1172 unsigned long long broadcast;
1173 unsigned long long udp_max_bps;
1174 unsigned long long enable_v6;
1175 char * bind4_address;
1176 char * bind6_address;
1177 struct GNUNET_TIME_Relative interval;
1179 struct sockaddr_in serverAddrv4;
1180 struct sockaddr_in6 serverAddrv6;
1184 /* Get port number */
1186 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1190 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1191 "ADVERTISED_PORT", &aport))
1195 LOG (GNUNET_ERROR_TYPE_WARNING,
1196 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
1203 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
1206 enable_v6 = GNUNET_NO;
1209 enable_v6 = GNUNET_YES;
1213 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1214 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1217 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1218 "BINDTO", &bind4_address))
1220 LOG (GNUNET_ERROR_TYPE_DEBUG,
1221 "Binding udp plugin to specific address: `%s'\n",
1223 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
1225 GNUNET_free (bind4_address);
1231 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1232 "BINDTO6", &bind6_address))
1234 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 "Binding udp plugin to specific address: `%s'\n",
1238 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
1240 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1242 GNUNET_free_non_null (bind4_address);
1243 GNUNET_free (bind6_address);
1249 /* Enable neighbour discovery */
1250 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
1252 if (broadcast == GNUNET_SYSERR)
1253 broadcast = GNUNET_NO;
1255 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
1256 "BROADCAST_INTERVAL", &interval))
1258 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1261 /* Maximum datarate */
1262 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1263 "MAX_BPS", &udp_max_bps))
1265 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1268 plugin = GNUNET_malloc (sizeof (struct Plugin));
1269 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1271 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1272 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
1275 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
1276 plugin->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1277 plugin->port = port;
1278 plugin->aport = aport;
1279 plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1280 plugin->broadcast_interval = interval;
1281 plugin->enable_ipv6 = enable_v6;
1286 api->disconnect = &udp_disconnect;
1287 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1288 api->address_to_string = &udp_address_to_string;
1289 api->check_address = &udp_plugin_check_address;
1290 api->get_session = &udp_plugin_get_session;
1291 api->send = &udp_plugin_send_wrapper;
1292 api->send_with_session = &udp_plugin_send;
1294 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
1295 res = setup_sockets (plugin, &serverAddrv6, &serverAddrv4);
1296 if ((res == 0) || ((plugin->sockv4 == NULL) && (plugin->sockv6 == NULL)))
1298 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
1299 GNUNET_free (plugin);
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
1305 if (broadcast == GNUNET_YES)
1306 setup_broadcast (plugin, &serverAddrv6, &serverAddrv4);
1309 GNUNET_free_non_null (bind4_address);
1310 GNUNET_free_non_null (bind6_address);
1315 * The exported method. Makes the core api available via a global and
1316 * returns the udp transport API.
1318 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1319 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1322 libgnunet_plugin_transport_udp_done (void *cls)
1324 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1325 struct Plugin *plugin = api->cls;
1327 stop_broadcast (plugin);
1329 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1331 GNUNET_SCHEDULER_cancel (plugin->select_task);
1332 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1335 /* Closing sockets */
1336 if (plugin->sockv4 != NULL)
1338 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1339 plugin->sockv4 = NULL;
1341 if (plugin->sockv6 != NULL)
1343 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1344 plugin->sockv6 = NULL;
1346 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1347 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1348 GNUNET_NAT_unregister (plugin->nat);
1350 if (plugin->mst != NULL)
1352 GNUNET_SERVER_mst_destroy(plugin->mst);
1356 /* Clean up leftover messages */
1357 struct UDPMessageWrapper *udpw = plugin->msg_head;
1358 while (udpw != NULL)
1360 struct UDPMessageWrapper *tmp = udpw->next;
1361 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
1362 if (udpw->cont != NULL)
1363 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
1368 /* Clean up sessions */
1370 LOG (GNUNET_ERROR_TYPE_DEBUG,
1371 "Cleaning up sessions\n");
1373 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
1374 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1377 GNUNET_free (plugin);
1383 /* end of plugin_transport_udp.c */