2 This file is part of GNUnet
3 (C) 2010 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
28 * The idea with this transport is to connect gnunet peers to each other
29 * when ONE is behind a NAT. This is based on pwnat (http://samy.pl/pwnat)
30 * created by Samy Kamkar. When configured with the PWNAT options, this
31 * transport will start a server daemon which sends dummy ICMP and UDP
32 * messages out to a predefined address (typically 1.2.3.4).
34 * When a non-NAT'd peer (the client) learns of the NAT'd peer (the server)
35 * address, it will send ICMP RESPONSES to the NAT'd peers external address.
36 * The NAT box should forward these faked responses to the server, which
37 * can then connect directly to the non-NAT'd peer.
41 #include "gnunet_hello_lib.h"
42 #include "gnunet_connection_lib.h"
43 #include "gnunet_container_lib.h"
44 #include "gnunet_nat_lib.h"
45 #include "gnunet_os_lib.h"
46 #include "gnunet_peerinfo_service.h"
47 #include "gnunet_protocols.h"
48 #include "gnunet_resolver_service.h"
49 #include "gnunet_server_lib.h"
50 #include "gnunet_signatures.h"
51 #include "gnunet_statistics_service.h"
52 #include "gnunet_transport_service.h"
53 #include "gnunet_transport_plugin.h"
54 #include "transport.h"
56 #define DEBUG_UDP GNUNET_NO
61 * Transport cost to peer, always 1 for UDP (direct connection)
63 #define UDP_DIRECT_DISTANCE 1
65 #define DEFAULT_NAT_PORT 0
68 * How long until we give up on transmitting the welcome message?
70 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
73 * Starting port for listening and sending, eventually a config value
75 #define UDP_NAT_DEFAULT_PORT 22086
78 * UDP Message-Packet header.
85 struct GNUNET_MessageHeader header;
88 * What is the identity of the sender (GNUNET_hash of public key)
90 struct GNUNET_PeerIdentity sender;
95 * Network format for IPv4 addresses.
100 * IPv4 address, in network byte order.
102 uint32_t ipv4_addr GNUNET_PACKED;
105 * Port number, in network byte order.
107 uint16_t u4_port GNUNET_PACKED;
112 * Network format for IPv6 addresses.
114 struct IPv6UdpAddress
119 struct in6_addr ipv6_addr GNUNET_PACKED;
122 * Port number, in network byte order.
124 uint16_t u6_port GNUNET_PACKED;
127 /* Forward definition */
130 struct PrettyPrinterContext
132 GNUNET_TRANSPORT_AddressStringCallback asc;
142 struct MessageQueue *next;
145 * Session this message belongs to
147 struct PeerSession *session;
150 * Actual message to be sent
155 * Size of message buffer to be sent
160 * When to discard this message
162 struct GNUNET_TIME_Absolute timeout;
165 * Continuation to call when this message goes out
167 GNUNET_TRANSPORT_TransmitContinuation cont;
170 * closure for continuation
177 * UDP NAT Probe message definition
179 struct UDP_NAT_ProbeMessage
184 struct GNUNET_MessageHeader header;
189 * UDP NAT Probe message reply definition
191 struct UDP_NAT_ProbeMessageReply
196 struct GNUNET_MessageHeader header;
202 * UDP NAT Probe message confirm definition
204 struct UDP_NAT_ProbeMessageConfirmation
209 struct GNUNET_MessageHeader header;
221 * Stored in a linked list.
223 struct PeerSession *next;
226 * Pointer to the global plugin struct.
228 struct Plugin *plugin;
231 * To whom are we talking to (set to our identity
232 * if we are still waiting for the welcome message)
234 struct GNUNET_PeerIdentity target;
237 * Address of the other peer (either based on our 'connect'
238 * call or on our 'accept' call).
243 * Length of connect_addr.
248 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
250 int expecting_welcome;
253 * From which socket do we need to send to this peer?
255 struct GNUNET_NETWORK_Handle *sock;
258 * Queue of messages for this peer, in the case that
259 * we have to await a connection...
261 struct MessageQueue *messages;
265 struct UDP_NAT_Probes
271 struct UDP_NAT_Probes *next;
274 * Timeout for this set of probes
276 struct GNUNET_TIME_Absolute timeout;
279 * Count of how many probes we've attempted
284 * The plugin this probe belongs to
286 struct Plugin *plugin;
289 * The task used to send these probes
291 GNUNET_SCHEDULER_TaskIdentifier task;
294 * Network address (always ipv4!)
296 struct IPv4UdpAddress addr;
302 * Information we keep for each of our listen sockets.
309 struct GNUNET_NETWORK_Handle *desc;
312 * The port we bound to
319 * Encapsulation of all of the state of the plugin.
326 struct GNUNET_TRANSPORT_PluginEnvironment *env;
329 * Session of peers with whom we are currently connected
331 struct PeerSession *sessions;
336 GNUNET_SCHEDULER_TaskIdentifier select_task;
344 * Address we were told to bind to exclusively (IPv4).
349 * Address we were told to bind to exclusively (IPv6).
354 * Handle to NAT traversal support.
356 struct GNUNET_NAT_Handle *nat;
361 struct GNUNET_NETWORK_FDSet *rs;
366 struct UDP_NAT_Probes *probes;
369 * socket that we transmit all IPv4 data with
371 struct UDP_Sock_Info udp_sockv4;
374 * socket that we transmit all IPv6 data with
376 struct UDP_Sock_Info udp_sockv6;
382 * Forward declaration.
385 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result);
389 * Disconnect from a remote node. Clean up session if we have one for this peer
391 * @param cls closure for this call (should be handle to Plugin)
392 * @param target the peeridentity of the peer to disconnect
393 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
396 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
398 /** TODO: Implement! */
404 find_session (struct Plugin *plugin,
405 const struct GNUNET_PeerIdentity *peer)
407 struct PeerSession *pos;
409 pos = plugin->sessions;
412 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
422 * Actually send out the message, assume we've got the address and
423 * send_handle squared away!
426 * @param send_handle which handle to send message on
427 * @param target who should receive this message (ignored by UDP)
428 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
429 * @param msgbuf_size the size of the msgbuf to send
430 * @param priority how important is the message (ignored by UDP)
431 * @param timeout when should we time out (give up) if we can not transmit?
432 * @param addr the addr to send the message to, needs to be a sockaddr for us
433 * @param addrlen the len of addr
434 * @param cont continuation to call once the message has
435 * been transmitted (or if the transport is ready
436 * for the next transmission call; or if the
437 * peer disconnected...)
438 * @param cont_cls closure for cont
439 * @return the number of bytes written
442 udp_real_send (void *cls,
443 struct GNUNET_NETWORK_Handle *send_handle,
444 const struct GNUNET_PeerIdentity *target,
447 unsigned int priority,
448 struct GNUNET_TIME_Relative timeout,
451 GNUNET_TRANSPORT_TransmitContinuation cont,
454 struct Plugin *plugin = cls;
455 struct UDPMessage *message;
458 struct sockaddr_in a4;
459 struct sockaddr_in6 a6;
460 const struct IPv4UdpAddress *t4;
461 const struct IPv6UdpAddress *t6;
465 if (send_handle == NULL)
467 /* failed to open send socket for AF */
469 cont (cont_cls, target, GNUNET_SYSERR);
472 if ((addr == NULL) || (addrlen == 0))
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "udp_real_send called without address, returning!\n");
479 cont (cont_cls, target, GNUNET_SYSERR);
480 return 0; /* Can never send if we don't have an address!! */
483 /* Build the message to be sent */
484 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
485 ssize = sizeof (struct UDPMessage) + msgbuf_size;
487 message->header.size = htons (ssize);
488 message->header.type = htons (0);
489 memcpy (&message->sender, plugin->env->my_identity,
490 sizeof (struct GNUNET_PeerIdentity));
491 memcpy (&message[1], msgbuf, msgbuf_size);
493 if (addrlen == sizeof (struct IPv6UdpAddress))
496 memset (&a6, 0, sizeof (a6));
497 #if HAVE_SOCKADDR_IN_SIN_LEN
498 a6.sin6_len = sizeof (a6);
500 a6.sin6_family = AF_INET6;
501 a6.sin6_port = t6->u6_port;
502 memcpy (&a6.sin6_addr,
504 sizeof (struct in6_addr));
508 else if (addrlen == sizeof (struct IPv4UdpAddress))
511 memset (&a4, 0, sizeof (a4));
512 #if HAVE_SOCKADDR_IN_SIN_LEN
513 a4.sin_len = sizeof (a4);
515 a4.sin_family = AF_INET;
516 a4.sin_port = t4->u4_port;
517 a4.sin_addr.s_addr = t4->ipv4_addr;
524 GNUNET_free (message);
528 /* Actually send the message */
530 GNUNET_NETWORK_socket_sendto (send_handle, message, ssize,
533 if (GNUNET_SYSERR == sent)
534 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "sendto");
535 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
536 "UDP transmit %u-byte message to %s (%d: %s)\n",
537 (unsigned int) ssize,
538 GNUNET_a2s (sb, sbs),
540 (sent < 0) ? STRERROR (errno) : "ok");
543 if (sent == GNUNET_SYSERR)
544 cont (cont_cls, target, GNUNET_SYSERR);
547 cont (cont_cls, target, GNUNET_OK);
551 GNUNET_free (message);
557 * Function that can be used by the transport service to transmit
558 * a message using the plugin.
561 * @param target who should receive this message (ignored by UDP)
562 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
563 * @param msgbuf_size the size of the msgbuf to send
564 * @param priority how important is the message (ignored by UDP)
565 * @param timeout when should we time out (give up) if we can not transmit?
566 * @param session identifier used for this session (can be NULL)
567 * @param addr the addr to send the message to, needs to be a sockaddr for us
568 * @param addrlen the len of addr
569 * @param force_address not used, we had better have an address to send to
570 * because we are stateless!!
571 * @param cont continuation to call once the message has
572 * been transmitted (or if the transport is ready
573 * for the next transmission call; or if the
574 * peer disconnected...)
575 * @param cont_cls closure for cont
577 * @return the number of bytes written (may return 0 and the message can
578 * still be transmitted later!)
581 udp_plugin_send (void *cls,
582 const struct GNUNET_PeerIdentity *target,
585 unsigned int priority,
586 struct GNUNET_TIME_Relative timeout,
587 struct Session *session,
591 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
593 struct Plugin *plugin = cls;
595 struct MessageQueue *temp_message;
596 struct PeerSession *peer_session;
598 const struct IPv4UdpAddress *t4;
599 struct sockaddr_in sin4;
601 if (force_address == GNUNET_SYSERR)
602 return GNUNET_SYSERR;
603 GNUNET_assert (NULL == session);
605 other_peer_natd = GNUNET_NO;
606 if (addrlen == sizeof(struct IPv4UdpAddress))
609 if (ntohs(t4->u4_port) == 0)
610 other_peer_natd = GNUNET_YES;
612 else if (addrlen != sizeof(struct IPv6UdpAddress))
615 return -1; /* Must have an address to send to */
619 if ( (other_peer_natd == GNUNET_YES) &&
620 (addrlen == sizeof(struct IPv4UdpAddress)) )
622 peer_session = find_session(plugin, target);
623 if (peer_session == NULL) /* We have a new peer to add */
626 * The first time, we can assume we have no knowledge of a
627 * working port for this peer, call the ICMP/UDP message sender
630 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
631 peer_session->connect_addr = GNUNET_malloc(addrlen);
632 memcpy(peer_session->connect_addr, addr, addrlen);
633 peer_session->connect_alen = addrlen;
634 peer_session->plugin = plugin;
635 peer_session->sock = NULL;
636 memcpy(&peer_session->target, target, sizeof(struct GNUNET_PeerIdentity));
637 peer_session->expecting_welcome = GNUNET_YES;
639 peer_session->next = plugin->sessions;
640 plugin->sessions = peer_session;
642 peer_session->messages = GNUNET_malloc(sizeof(struct MessageQueue));
643 peer_session->messages->msgbuf = GNUNET_malloc(msgbuf_size);
644 memcpy(peer_session->messages->msgbuf, msgbuf, msgbuf_size);
645 peer_session->messages->msgbuf_size = msgbuf_size;
646 peer_session->messages->timeout = GNUNET_TIME_relative_to_absolute(timeout);
647 peer_session->messages->cont = cont;
648 peer_session->messages->cont_cls = cont_cls;
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target));
653 memset (&sin4, 0, sizeof (sin4));
654 sin4.sin_family = AF_INET;
655 #if HAVE_SOCKADDR_IN_SIN_LEN
656 sin4.sin_len = sizeof (sin4);
658 sin4.sin_port = t4->u4_port;
659 sin4.sin_addr.s_addr = t4->ipv4_addr;
660 GNUNET_NAT_run_client (plugin->nat, &sin4);
664 if (peer_session->expecting_welcome == GNUNET_NO) /* We are "connected" */
666 sent = udp_real_send(cls,
671 peer_session->connect_addr, peer_session->connect_alen,
674 else /* Haven't gotten a response from this peer, queue message */
676 temp_message = GNUNET_malloc(sizeof(struct MessageQueue));
677 temp_message->msgbuf = GNUNET_malloc(msgbuf_size);
678 memcpy(temp_message->msgbuf, msgbuf, msgbuf_size);
679 temp_message->msgbuf_size = msgbuf_size;
680 temp_message->timeout = GNUNET_TIME_relative_to_absolute(timeout);
681 temp_message->cont = cont;
682 temp_message->cont_cls = cont_cls;
683 temp_message->next = peer_session->messages;
684 peer_session->messages = temp_message;
688 else if (other_peer_natd == GNUNET_NO) /* Other peer not behind a NAT, so we can just send the message as is */
690 sent = udp_real_send(cls,
691 (addrlen == sizeof (struct IPv4UdpAddress)) ? plugin->udp_sockv4.desc : plugin->udp_sockv6.desc,
694 priority, timeout, addr, addrlen,
697 else /* Other peer is NAT'd, but we don't want to play with them (or can't!) */
699 return GNUNET_SYSERR;
702 /* When GNUNET_SYSERR is returned from udp_real_send, we will still call
703 * the callback so must not return GNUNET_SYSERR!
704 * If we did, then transport context would get freed twice. */
705 if (sent == GNUNET_SYSERR)
712 * Send UDP probe messages or UDP keepalive messages, depending on the
713 * state of the connection.
715 * @param cls closure for this call (should be the main Plugin)
716 * @param tc task context for running this
719 send_udp_probe_message (void *cls,
720 const struct GNUNET_SCHEDULER_TaskContext *tc)
722 struct UDP_NAT_Probes *probe = cls;
723 struct UDP_NAT_ProbeMessage message;
724 struct Plugin *plugin = probe->plugin;
726 memset (&message, 0, sizeof (message));
727 message.header.size = htons(sizeof(struct UDP_NAT_ProbeMessage));
728 message.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE);
729 /* If they gave us a port, use that. If not, try our port. */
730 if (ntohs(probe->addr.u4_port) == 0)
731 probe->addr.u4_port = htons(plugin->port);
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 _("Sending a probe to port %d\n"), ntohs(probe->addr.u4_port));
738 udp_real_send(plugin,
739 plugin->udp_sockv4.desc,
741 (char *)&message, ntohs(message.header.size), 0,
742 GNUNET_TIME_relative_get_unit(),
743 &probe->addr, sizeof(struct IPv4UdpAddress),
744 &udp_probe_continuation, probe);
749 * Continuation for probe sends. If the last probe was sent
750 * "successfully", schedule sending of another one. If not,
754 udp_probe_continuation (void *cls,
755 const struct GNUNET_PeerIdentity *target,
758 struct UDP_NAT_Probes *probe = cls;
759 /*struct Plugin *plugin = probe->plugin;*/
761 if ((result == GNUNET_OK) && (probe->count < MAX_PROBES))
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 _("Scheduling next probe for 10000 milliseconds\n"));
767 probe->task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10),
768 &send_udp_probe_message, probe);
770 else /* Destroy the probe context. */
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 _("Sending probe didn't go well...\n"));
784 udp_plugin_reversal_callback (void *cls,
785 const struct sockaddr *addr,
788 struct Plugin *plugin = cls;
789 struct UDP_NAT_Probes *temp_probe;
790 const struct sockaddr_in *inaddr;
792 if (sizeof (struct sockaddr_in) != addrlen)
797 inaddr = (const struct sockaddr_in *) addr;
798 temp_probe = GNUNET_malloc(sizeof(struct UDP_NAT_Probes));
799 temp_probe->addr.ipv4_addr = inaddr->sin_addr.s_addr;
800 temp_probe->addr.u4_port = inaddr->sin_port;
801 temp_probe->next = plugin->probes;
802 temp_probe->plugin = plugin;
803 temp_probe->task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500),
804 &send_udp_probe_message,
806 plugin->probes = temp_probe;
811 * Demultiplexer for UDP NAT messages
813 * @param plugin the main plugin for this transport
814 * @param sender from which peer the message was received
815 * @param currhdr pointer to the header of the message
816 * @param sender_addr the address from which the message was received
817 * @param fromlen the length of the address
818 * @param sockinfo which socket did we receive the message on
821 udp_demultiplexer(struct Plugin *plugin,
822 struct GNUNET_PeerIdentity *sender,
823 const struct GNUNET_MessageHeader *currhdr,
824 const void *sender_addr,
825 size_t fromlen, struct UDP_Sock_Info *sockinfo)
827 struct UDP_NAT_ProbeMessageReply *outgoing_probe_reply;
828 struct PeerSession *peer_session;
829 struct MessageQueue *pending_message;
830 struct MessageQueue *pending_message_temp;
831 uint16_t incoming_port;
832 struct GNUNET_TRANSPORT_ATS_Information distance[2];
833 if (memcmp(sender, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
836 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
837 _("Received a message from myself, dropping!!!\n"));
843 GNUNET_assert(sender_addr != NULL); /* Can recvfrom have a NULL address? */
844 if (fromlen == sizeof(struct IPv4UdpAddress))
846 incoming_port = ntohs(((struct IPv4UdpAddress *)sender_addr)->u4_port);
848 else if (fromlen == sizeof(struct IPv6UdpAddress))
850 incoming_port = ntohs(((struct IPv6UdpAddress *)sender_addr)->u6_port);
853 switch (ntohs(currhdr->type))
855 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE:
856 /* Send probe reply */
857 outgoing_probe_reply = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageReply));
858 outgoing_probe_reply->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageReply));
859 outgoing_probe_reply->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY);
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 _("Received a probe on listen port %d, sent_from port %d\n"),
864 sockinfo->port, incoming_port);
867 udp_real_send(plugin, sockinfo->desc, NULL,
868 (char *)outgoing_probe_reply,
869 ntohs(outgoing_probe_reply->header.size), 0,
870 GNUNET_TIME_relative_get_unit(),
871 sender_addr, fromlen,
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 _("Sent PROBE REPLY to port %d on outgoing port %d\n"),
877 incoming_port, sockinfo->port);
879 GNUNET_free(outgoing_probe_reply);
881 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY:
882 /* Check for existing probe, check ports returned, send confirmation if all is well */
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 _("Received PROBE REPLY from port %d on incoming port %d\n"), incoming_port, sockinfo->port);
887 if (fromlen == sizeof(struct IPv4UdpAddress))
891 struct UDP_NAT_ProbeMessageConfirmation *outgoing_probe_confirmation;
892 struct UDP_NAT_Probes *outgoing_probe;
893 outgoing_probe = find_probe(plugin, &addr_buf[0]);
894 if (outgoing_probe != NULL)
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 _("Sending confirmation that we were reached!\n"));
900 outgoing_probe_confirmation = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
901 outgoing_probe_confirmation->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
902 outgoing_probe_confirmation->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM);
903 udp_real_send(plugin, sockinfo->desc, NULL,
904 (char *)outgoing_probe_confirmation,
905 ntohs(outgoing_probe_confirmation->header.size), 0,
906 GNUNET_TIME_relative_get_unit(),
907 sender_addr, fromlen, NULL, NULL);
909 if (outgoing_probe->task != GNUNET_SCHEDULER_NO_TASK)
911 GNUNET_SCHEDULER_cancel(outgoing_probe->task);
912 outgoing_probe->task = GNUNET_SCHEDULER_NO_TASK;
913 /* Schedule task to timeout and remove probe if confirmation not received */
915 GNUNET_free(outgoing_probe_confirmation);
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 _("Received a probe reply, but have no record of a sent probe!\n"));
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 _("Received a probe reply, but sender address size is WRONG (should be %d, is %d)!\n"), sizeof(struct IPv4UdpAddress), fromlen);
934 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM:
935 peer_session = find_session(plugin, sender);
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 _("Looking up peer session for peer %s\n"), GNUNET_i2s(sender));
940 if (peer_session == NULL) /* Shouldn't this NOT happen? */
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 _("Peer not in list, adding (THIS MAY BE A MISTAKE) %s\n"), GNUNET_i2s(sender));
946 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
947 peer_session->connect_addr = GNUNET_malloc(fromlen);
948 memcpy(peer_session->connect_addr, sender_addr, fromlen);
949 peer_session->connect_alen = fromlen;
950 peer_session->plugin = plugin;
951 peer_session->sock = sockinfo->desc;
952 memcpy(&peer_session->target, sender, sizeof(struct GNUNET_PeerIdentity));
953 peer_session->expecting_welcome = GNUNET_NO;
955 peer_session->next = plugin->sessions;
956 plugin->sessions = peer_session;
958 peer_session->messages = NULL;
960 else if (peer_session->expecting_welcome == GNUNET_YES)
962 peer_session->expecting_welcome = GNUNET_NO;
963 peer_session->sock = sockinfo->desc;
964 if (peer_session->connect_alen == sizeof(struct IPv4UdpAddress))
966 ((struct IPv4UdpAddress *)peer_session->connect_addr)->u4_port = htons(incoming_port);
968 else if (peer_session->connect_alen == sizeof(struct IPv4UdpAddress))
970 ((struct IPv6UdpAddress *)peer_session->connect_addr)->u6_port = htons(incoming_port);
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
975 _("Received a probe confirmation, will send to peer on port %d\n"), incoming_port);
977 if (peer_session->messages != NULL)
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 _("Received a probe confirmation, sending queued messages.\n"));
983 pending_message = peer_session->messages;
985 while (pending_message != NULL)
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 _("sending queued message %d\n"), count);
991 udp_real_send(plugin,
993 &peer_session->target,
994 pending_message->msgbuf,
995 pending_message->msgbuf_size, 0,
996 GNUNET_TIME_relative_get_unit(),
997 peer_session->connect_addr,
998 peer_session->connect_alen,
999 pending_message->cont,
1000 pending_message->cont_cls);
1002 pending_message_temp = pending_message;
1003 pending_message = pending_message->next;
1004 GNUNET_free(pending_message_temp->msgbuf);
1005 GNUNET_free(pending_message_temp);
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 _("finished sending queued message %d\n"), count);
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 _("Received probe confirmation for already confirmed peer!\n"));
1022 /* Received confirmation, add peer with address/port specified */
1024 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_KEEPALIVE:
1025 /* Once we've sent NAT_PROBE_CONFIRM change to sending keepalives */
1026 /* If we receive these just ignore! */
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Sending message type %d to transport!\n",
1033 ntohs(currhdr->type));
1036 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1037 distance[0].value = htonl (UDP_DIRECT_DISTANCE);
1038 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1039 distance[1].value = htonl (0);
1041 plugin->env->receive (plugin->env->cls, sender, currhdr,
1042 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1043 NULL, sender_addr, fromlen);
1050 * We have been notified that our writeset has something to read. We don't
1051 * know which socket needs to be read, so we have to check each one
1052 * Then reschedule this function to be called again once more is available.
1054 * @param cls the plugin handle
1055 * @param tc the scheduling context (for rescheduling this function again)
1058 udp_plugin_select (void *cls,
1059 const struct GNUNET_SCHEDULER_TaskContext *tc)
1061 struct Plugin *plugin = cls;
1063 struct UDPMessage *msg;
1064 struct GNUNET_PeerIdentity sender;
1071 const struct GNUNET_MessageHeader *currhdr;
1072 struct IPv4UdpAddress t4;
1073 struct IPv6UdpAddress t6;
1074 const struct sockaddr_in *s4;
1075 const struct sockaddr_in6 *s6;
1078 struct UDP_Sock_Info *udp_sock;
1081 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1082 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1085 if (GNUNET_NETWORK_fdset_isset (tc->read_ready,
1086 plugin->udp_sockv4.desc))
1087 udp_sock = &plugin->udp_sockv4;
1088 else if (GNUNET_NETWORK_fdset_isset (tc->read_ready,
1089 plugin->udp_sockv6.desc))
1090 udp_sock = &plugin->udp_sockv6;
1091 if (NULL == udp_sock)
1096 fromlen = sizeof (addr);
1097 memset (&addr, 0, sizeof(addr));
1099 GNUNET_NETWORK_socket_recvfrom (udp_sock->desc, buf, sizeof (buf),
1100 (struct sockaddr *)&addr, &fromlen);
1102 if (AF_INET == ((struct sockaddr *)addr)->sa_family)
1104 s4 = (const struct sockaddr_in*) &addr;
1105 t4.u4_port = s4->sin_port;
1106 t4.ipv4_addr = s4->sin_addr.s_addr;
1108 calen = sizeof (t4);
1110 else if (AF_INET6 == ((struct sockaddr *)addr)->sa_family)
1112 s6 = (const struct sockaddr_in6*) &addr;
1113 t6.u6_port = s6->sin6_port;
1114 memcpy (&t6.ipv6_addr,
1116 sizeof (struct in6_addr));
1118 calen = sizeof (t6);
1126 if (ret < sizeof (struct UDPMessage))
1128 GNUNET_break_op (0);
1129 plugin->select_task =
1130 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1131 GNUNET_SCHEDULER_NO_TASK,
1132 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1133 NULL, &udp_plugin_select, plugin);
1136 msg = (struct UDPMessage *) buf;
1137 csize = ntohs (msg->header.size);
1138 if ( (csize < sizeof (struct UDPMessage)) ||
1141 GNUNET_break_op (0);
1142 plugin->select_task =
1143 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1144 GNUNET_SCHEDULER_NO_TASK,
1145 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1146 NULL, &udp_plugin_select, plugin);
1149 msgbuf = (char *)&msg[1];
1150 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1152 tsize = csize - sizeof (struct UDPMessage);
1153 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
1155 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
1156 csize = ntohs (currhdr->size);
1157 if ( (csize < sizeof (struct GNUNET_MessageHeader)) ||
1158 (csize > tsize - offset) )
1160 GNUNET_break_op (0);
1163 udp_demultiplexer(plugin, &sender, currhdr,
1164 ca, calen, udp_sock);
1167 plugin->select_task =
1168 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1169 GNUNET_SCHEDULER_NO_TASK,
1170 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1171 NULL, &udp_plugin_select, plugin);
1177 * Check if the given port is plausible (must be either
1178 * our listen port or our advertised port). If it is
1179 * neither, we return GNUNET_SYSERR.
1181 * @param plugin global variables
1182 * @param in_port port number to check
1183 * @return GNUNET_OK if port is either open_port or adv_port
1186 check_port (struct Plugin *plugin, uint16_t in_port)
1190 if (in_port == plugin->port)
1192 return GNUNET_SYSERR;
1197 * Function that will be called to check if a binary address for this
1198 * plugin is well-formed and corresponds to an address for THIS peer
1199 * (as per our configuration). Naturally, if absolutely necessary,
1200 * plugins can be a bit conservative in their answer, but in general
1201 * plugins should make sure that the address does not redirect
1202 * traffic to a 3rd party that might try to man-in-the-middle our
1205 * @param cls closure, should be our handle to the Plugin
1206 * @param addr pointer to the address
1207 * @param addrlen length of addr
1208 * @return GNUNET_OK if this is a plausible address for this peer
1209 * and transport, GNUNET_SYSERR if not
1213 udp_plugin_check_address (void *cls,
1217 struct Plugin *plugin = cls;
1218 struct IPv4UdpAddress *v4;
1219 struct IPv6UdpAddress *v6;
1221 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1222 (addrlen != sizeof (struct IPv6UdpAddress)))
1224 GNUNET_break_op (0);
1225 return GNUNET_SYSERR;
1227 if (addrlen == sizeof (struct IPv4UdpAddress))
1229 v4 = (struct IPv4UdpAddress *) addr;
1231 check_port (plugin, ntohs (v4->u4_port)))
1232 return GNUNET_SYSERR;
1234 GNUNET_NAT_test_address (plugin->nat,
1235 &v4->ipv4_addr, sizeof (struct in_addr)))
1236 return GNUNET_SYSERR;
1240 v6 = (struct IPv6UdpAddress *) addr;
1241 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1243 GNUNET_break_op (0);
1244 return GNUNET_SYSERR;
1247 check_port (plugin, ntohs (v6->u6_port)))
1248 return GNUNET_SYSERR;
1250 GNUNET_NAT_test_address (plugin->nat,
1251 &v6->ipv6_addr, sizeof (struct in6_addr)))
1252 return GNUNET_SYSERR;
1259 * Append our port and forward the result.
1262 append_port (void *cls, const char *hostname)
1264 struct PrettyPrinterContext *ppc = cls;
1267 if (hostname == NULL)
1269 ppc->asc (ppc->asc_cls, NULL);
1273 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1274 ppc->asc (ppc->asc_cls, ret);
1280 * Convert the transports address to a nice, human-readable
1283 * @param cls closure
1284 * @param type name of the transport that generated the address
1285 * @param addr one of the addresses of the host, NULL for the last address
1286 * the specific address format depends on the transport
1287 * @param addrlen length of the address
1288 * @param numeric should (IP) addresses be displayed in numeric form?
1289 * @param timeout after how long should we give up?
1290 * @param asc function to call on each string
1291 * @param asc_cls closure for asc
1294 udp_plugin_address_pretty_printer (void *cls,
1299 struct GNUNET_TIME_Relative timeout,
1300 GNUNET_TRANSPORT_AddressStringCallback asc,
1303 struct PrettyPrinterContext *ppc;
1306 struct sockaddr_in a4;
1307 struct sockaddr_in6 a6;
1308 const struct IPv4UdpAddress *u4;
1309 const struct IPv6UdpAddress *u6;
1312 if (addrlen == sizeof (struct IPv6UdpAddress))
1315 memset (&a6, 0, sizeof (a6));
1316 a6.sin6_family = AF_INET6;
1317 a6.sin6_port = u6->u6_port;
1318 memcpy (&a6.sin6_addr,
1320 sizeof (struct in6_addr));
1321 port = ntohs (u6->u6_port);
1325 else if (addrlen == sizeof (struct IPv4UdpAddress))
1328 memset (&a4, 0, sizeof (a4));
1329 a4.sin_family = AF_INET;
1330 a4.sin_port = u4->u4_port;
1331 a4.sin_addr.s_addr = u4->ipv4_addr;
1332 port = ntohs (u4->u4_port);
1338 /* invalid address */
1339 GNUNET_break_op (0);
1340 asc (asc_cls, NULL);
1343 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1345 ppc->asc_cls = asc_cls;
1347 GNUNET_RESOLVER_hostname_get (sb,
1349 !numeric, timeout, &append_port, ppc);
1354 * Function called for a quick conversion of the binary address to
1355 * a numeric address. Note that the caller must not free the
1356 * address and that the next call to this function is allowed
1357 * to override the address again.
1359 * @param cls closure
1360 * @param addr binary address
1361 * @param addrlen length of the address
1362 * @return string representing the same address
1365 udp_address_to_string (void *cls,
1369 static char rbuf[INET6_ADDRSTRLEN + 10];
1370 char buf[INET6_ADDRSTRLEN];
1374 const struct IPv4UdpAddress *t4;
1375 const struct IPv6UdpAddress *t6;
1379 if (addrlen == sizeof (struct IPv6UdpAddress))
1383 port = ntohs (t6->u6_port);
1384 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1387 else if (addrlen == sizeof (struct IPv4UdpAddress))
1391 port = ntohs (t4->u4_port);
1392 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1397 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1398 GNUNET_snprintf (rbuf,
1408 * Our external IP address/port mapping has changed.
1410 * @param cls closure, the 'struct LocalAddrList'
1411 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1412 * the previous (now invalid) one
1413 * @param addr either the previous or the new public IP address
1414 * @param addrlen actual lenght of the address
1417 udp_nat_port_map_callback (void *cls,
1419 const struct sockaddr *addr,
1422 struct Plugin *plugin = cls;
1423 struct IPv4UdpAddress u4;
1424 struct IPv6UdpAddress u6;
1428 /* convert 'addr' to our internal format */
1429 switch (addr->sa_family)
1432 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1433 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1434 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1439 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1440 memcpy (&u6.ipv6_addr,
1441 &((struct sockaddr_in6 *) addr)->sin6_addr,
1442 sizeof (struct in6_addr));
1443 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1451 /* modify our published address list */
1452 plugin->env->notify_address (plugin->env->cls,
1459 * The exported method. Makes the core api available via a global and
1460 * returns the udp transport API.
1463 libgnunet_plugin_transport_udp_init (void *cls)
1465 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1466 unsigned long long mtu;
1467 unsigned long long port;
1468 struct GNUNET_TRANSPORT_PluginFunctions *api;
1469 struct Plugin *plugin;
1470 int sockets_created;
1471 struct sockaddr_in serverAddrv4;
1472 struct sockaddr_in6 serverAddrv6;
1473 struct sockaddr *serverAddr;
1474 struct sockaddr *addrs[2];
1475 socklen_t addrlens[2];
1480 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1484 port = UDP_NAT_DEFAULT_PORT;
1487 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1488 _("Given `%s' option is out of range: %llu > %u\n"),
1497 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1498 _("MTU %llu for `%s' is probably too low!\n"), mtu,
1500 plugin = GNUNET_malloc (sizeof (struct Plugin));
1501 plugin->port = port;
1503 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1506 api->send = &udp_plugin_send;
1507 api->disconnect = &udp_disconnect;
1508 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1509 api->address_to_string = &udp_address_to_string;
1510 api->check_address = &udp_plugin_check_address;
1512 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(env->cfg,
1515 &plugin->bind_address))
1516 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1517 "Binding udp plugin to specific address: `%s'\n",
1518 plugin->bind_address);
1519 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(env->cfg,
1522 &plugin->bind6_address))
1523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1524 "Binding udp plugin to specific address: `%s'\n",
1525 plugin->bind6_address);
1527 sockets_created = 0;
1529 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
1533 plugin->udp_sockv6.desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1534 if (NULL == plugin->udp_sockv6.desc)
1536 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
1540 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1541 #if HAVE_SOCKADDR_IN_SIN_LEN
1542 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1545 serverAddrv6.sin6_family = AF_INET6;
1546 serverAddrv6.sin6_addr = in6addr_any;
1547 if (plugin->bind6_address != NULL)
1549 if (1 != inet_pton(AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1552 serverAddrv6.sin6_port = htons (plugin->port);
1553 addrlen = sizeof (serverAddrv6);
1554 serverAddr = (struct sockaddr *) &serverAddrv6;
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "Binding to IPv6 port %d\n",
1558 ntohs(serverAddrv6.sin6_port));
1561 while (GNUNET_NETWORK_socket_bind (plugin->udp_sockv6.desc, serverAddr, addrlen) !=
1564 serverAddrv6.sin6_port
1565 = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1568 "IPv6 Binding failed, trying new port %d\n",
1569 ntohs(serverAddrv6.sin6_port));
1574 GNUNET_NETWORK_socket_close (plugin->udp_sockv6.desc);
1575 plugin->udp_sockv6.desc = NULL;
1579 if (plugin->udp_sockv6.desc != NULL)
1581 plugin->udp_sockv6.port = ntohs(serverAddrv6.sin6_port);
1582 addrs[sockets_created] = (struct sockaddr*) &serverAddrv6;
1583 addrlens[sockets_created] = sizeof (serverAddrv6);
1589 plugin->udp_sockv4.desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1590 if (NULL == plugin->udp_sockv4.desc)
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "socket");
1596 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1597 #if HAVE_SOCKADDR_IN_SIN_LEN
1598 serverAddrv4.sin_len = sizeof (serverAddrv4);
1600 serverAddrv4.sin_family = AF_INET;
1601 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1602 if (plugin->bind_address != NULL)
1604 if (1 != inet_pton(AF_INET, plugin->bind_address, &serverAddrv4.sin_addr))
1607 serverAddrv4.sin_port = htons (plugin->port);
1608 addrlen = sizeof (serverAddrv4);
1609 serverAddr = (struct sockaddr *) &serverAddrv4;
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 "Binding to IPv4 port %d\n",
1613 ntohs(serverAddrv4.sin_port));
1616 while (GNUNET_NETWORK_socket_bind (plugin->udp_sockv4.desc, serverAddr, addrlen) !=
1619 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1622 "IPv4 Binding failed, trying new port %d\n",
1623 ntohs(serverAddrv4.sin_port));
1628 GNUNET_NETWORK_socket_close (plugin->udp_sockv4.desc);
1629 plugin->udp_sockv4.desc = NULL;
1633 if (plugin->udp_sockv4.desc != NULL)
1635 plugin->udp_sockv4.port = ntohs(serverAddrv4.sin_port);
1636 addrs[sockets_created] = (struct sockaddr*) &serverAddrv4;
1637 addrlens[sockets_created] = sizeof (serverAddrv4);
1642 plugin->rs = GNUNET_NETWORK_fdset_create ();
1643 GNUNET_NETWORK_fdset_zero (plugin->rs);
1644 if (NULL != plugin->udp_sockv4.desc)
1645 GNUNET_NETWORK_fdset_set (plugin->rs,
1646 plugin->udp_sockv4.desc);
1647 if (NULL != plugin->udp_sockv6.desc)
1648 GNUNET_NETWORK_fdset_set (plugin->rs,
1649 plugin->udp_sockv6.desc);
1651 plugin->select_task =
1652 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1653 GNUNET_SCHEDULER_NO_TASK,
1654 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1655 NULL, &udp_plugin_select, plugin);
1656 if (sockets_created == 0)
1657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1658 _("Failed to open UDP sockets\n"));
1659 plugin->nat = GNUNET_NAT_register (env->cfg,
1663 (const struct sockaddr**) addrs, addrlens,
1664 &udp_nat_port_map_callback,
1665 &udp_plugin_reversal_callback,
1672 libgnunet_plugin_transport_udp_done (void *cls)
1674 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1675 struct Plugin *plugin = api->cls;
1677 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1679 GNUNET_SCHEDULER_cancel (plugin->select_task);
1680 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1682 if (plugin->udp_sockv4.desc != NULL)
1684 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->udp_sockv4.desc));
1685 plugin->udp_sockv4.desc = NULL;
1687 if (plugin->udp_sockv6.desc != NULL)
1689 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->udp_sockv6.desc));
1690 plugin->udp_sockv6.desc = NULL;
1692 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1693 GNUNET_NAT_unregister (plugin->nat);
1695 GNUNET_free (plugin);
1700 /* end of plugin_transport_udp.c */