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_os_lib.h"
45 #include "gnunet_peerinfo_service.h"
46 #include "gnunet_protocols.h"
47 #include "gnunet_resolver_service.h"
48 #include "gnunet_server_lib.h"
49 #include "gnunet_signatures.h"
50 #include "gnunet_statistics_service.h"
51 #include "gnunet_transport_service.h"
52 #include "gnunet_transport_plugin.h"
53 #include "transport.h"
55 #define DEBUG_UDP GNUNET_NO
60 * Transport cost to peer, always 1 for UDP (direct connection)
62 #define UDP_DIRECT_DISTANCE 1
64 #define DEFAULT_NAT_PORT 0
67 * How long until we give up on transmitting the welcome message?
69 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
72 * Starting port for listening and sending, eventually a config value
74 #define UDP_NAT_DEFAULT_PORT 22086
77 * UDP Message-Packet header.
84 struct GNUNET_MessageHeader header;
87 * What is the identity of the sender (GNUNET_hash of public key)
89 struct GNUNET_PeerIdentity sender;
94 * Network format for IPv4 addresses.
99 * IPv4 address, in network byte order.
101 uint32_t ipv4_addr GNUNET_PACKED;
104 * Port number, in network byte order.
106 uint16_t u_port GNUNET_PACKED;
111 * Network format for IPv6 addresses.
113 struct IPv6UdpAddress
118 struct in6_addr ipv6_addr GNUNET_PACKED;
121 * Port number, in network byte order.
123 uint16_t u6_port GNUNET_PACKED;
126 /* Forward definition */
129 struct PrettyPrinterContext
131 GNUNET_TRANSPORT_AddressStringCallback asc;
141 struct MessageQueue *next;
144 * Session this message belongs to
146 struct PeerSession *session;
149 * Actual message to be sent
154 * Size of message buffer to be sent
159 * When to discard this message
161 struct GNUNET_TIME_Absolute timeout;
164 * Continuation to call when this message goes out
166 GNUNET_TRANSPORT_TransmitContinuation cont;
169 * closure for continuation
176 * UDP NAT Probe message definition
178 struct UDP_NAT_ProbeMessage
183 struct GNUNET_MessageHeader header;
188 * UDP NAT Probe message reply definition
190 struct UDP_NAT_ProbeMessageReply
195 struct GNUNET_MessageHeader header;
201 * UDP NAT Probe message confirm definition
203 struct UDP_NAT_ProbeMessageConfirmation
208 struct GNUNET_MessageHeader header;
214 * Local network addresses (actual IP address follows this struct).
215 * PORT is NOT included!
221 * This is a doubly linked list.
223 struct LocalAddrList *next;
226 * This is a doubly linked list.
228 struct LocalAddrList *prev;
231 * Number of bytes of the address that follow
245 * Stored in a linked list.
247 struct PeerSession *next;
250 * Pointer to the global plugin struct.
252 struct Plugin *plugin;
255 * To whom are we talking to (set to our identity
256 * if we are still waiting for the welcome message)
258 struct GNUNET_PeerIdentity target;
261 * Address of the other peer (either based on our 'connect'
262 * call or on our 'accept' call).
267 * Length of connect_addr.
272 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
274 int expecting_welcome;
277 * From which socket do we need to send to this peer?
279 struct GNUNET_NETWORK_Handle *sock;
282 * Queue of messages for this peer, in the case that
283 * we have to await a connection...
285 struct MessageQueue *messages;
289 struct UDP_NAT_Probes
295 struct UDP_NAT_Probes *next;
298 * Address string that the server process returned to us
300 char *address_string;
303 * Timeout for this set of probes
305 struct GNUNET_TIME_Absolute timeout;
308 * Count of how many probes we've attempted
313 * The plugin this probe belongs to
315 struct Plugin *plugin;
318 * The task used to send these probes
320 GNUNET_SCHEDULER_TaskIdentifier task;
323 * Network address (always ipv4!)
325 struct IPv4UdpAddress addr;
331 * Information we keep for each of our listen sockets.
338 struct GNUNET_NETWORK_Handle *desc;
341 * The port we bound to
348 * Encapsulation of all of the state of the plugin.
355 struct GNUNET_TRANSPORT_PluginEnvironment *env;
358 * Session of peers with whom we are currently connected
360 struct PeerSession *sessions;
363 * Handle for request of hostname resolution, non-NULL if pending.
365 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
368 * ID of task used to update our addresses when one expires.
370 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
375 GNUNET_SCHEDULER_TaskIdentifier select_task;
383 * The external address given to us by the user. Must be actual
384 * outside visible address for NAT punching to work.
386 char *external_address;
389 * The internal address given to us by the user (or discovered).
391 char *internal_address;
394 * Address we were told to bind to exclusively (IPv4).
399 * Address we were told to bind to exclusively (IPv6).
404 * List of our IP addresses.
406 struct LocalAddrList *lal_head;
409 * Tail of our IP address list.
411 struct LocalAddrList *lal_tail;
416 struct GNUNET_NETWORK_FDSet *rs;
419 * stdout pipe handle for the gnunet-nat-server process
421 struct GNUNET_DISK_PipeHandle *server_stdout;
424 * stdout file handle (for reading) for the gnunet-nat-server process
426 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
431 struct UDP_NAT_Probes *probes;
434 * socket that we transmit all IPv4 data with
436 struct UDP_Sock_Info udp_sockv4;
439 * socket that we transmit all IPv6 data with
441 struct UDP_Sock_Info udp_sockv6;
444 * ID of select gnunet-nat-server stdout read task
446 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
449 * Is this transport configured to be behind a NAT?
454 * Is this transport configured to allow connections to NAT'd peers?
459 * Should this transport advertise only NAT addresses (port set to 0)?
460 * If not, all addresses will be duplicated for NAT punching and regular
463 int only_nat_addresses;
466 * use local addresses?
468 int use_localaddresses;
471 * The process id of the server process (if behind NAT)
473 struct GNUNET_OS_Process *server_proc;
479 * Forward declaration.
482 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result);
486 * Disconnect from a remote node. Clean up session if we have one for this peer
488 * @param cls closure for this call (should be handle to Plugin)
489 * @param target the peeridentity of the peer to disconnect
490 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
493 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
495 /** TODO: Implement! */
500 * Shutdown the server process (stop receiving inbound traffic). Maybe
503 * @param cls Handle to the plugin for this transport
505 * @return returns the number of sockets successfully closed,
506 * should equal the number of sockets successfully opened
509 udp_transport_server_stop (void *cls)
511 struct Plugin *plugin = cls;
513 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
515 GNUNET_SCHEDULER_cancel (plugin->select_task);
516 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
518 if (plugin->udp_sockv4.desc != NULL)
520 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->udp_sockv4.desc));
521 plugin->udp_sockv4.desc = NULL;
523 if (plugin->udp_sockv6.desc != NULL)
525 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->udp_sockv6.desc));
526 plugin->udp_sockv6.desc = NULL;
528 if (plugin->behind_nat == GNUNET_YES)
530 if (0 != GNUNET_OS_process_kill (plugin->server_proc, SIGTERM))
531 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
532 GNUNET_OS_process_wait (plugin->server_proc);
533 GNUNET_OS_process_close (plugin->server_proc);
534 plugin->server_proc = NULL;
541 find_session (struct Plugin *plugin,
542 const struct GNUNET_PeerIdentity *peer)
544 struct PeerSession *pos;
546 pos = plugin->sessions;
549 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
559 * Actually send out the message, assume we've got the address and
560 * send_handle squared away!
563 * @param send_handle which handle to send message on
564 * @param target who should receive this message (ignored by UDP)
565 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
566 * @param msgbuf_size the size of the msgbuf to send
567 * @param priority how important is the message (ignored by UDP)
568 * @param timeout when should we time out (give up) if we can not transmit?
569 * @param addr the addr to send the message to, needs to be a sockaddr for us
570 * @param addrlen the len of addr
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
576 * @return the number of bytes written
579 udp_real_send (void *cls,
580 struct GNUNET_NETWORK_Handle *send_handle,
581 const struct GNUNET_PeerIdentity *target,
584 unsigned int priority,
585 struct GNUNET_TIME_Relative timeout,
588 GNUNET_TRANSPORT_TransmitContinuation cont,
591 struct Plugin *plugin = cls;
592 struct UDPMessage *message;
595 struct sockaddr_in a4;
596 struct sockaddr_in6 a6;
597 const struct IPv4UdpAddress *t4;
598 const struct IPv6UdpAddress *t6;
602 if (send_handle == NULL)
604 /* failed to open send socket for AF */
606 cont (cont_cls, target, GNUNET_SYSERR);
609 if ((addr == NULL) || (addrlen == 0))
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
613 "udp_real_send called without address, returning!\n");
616 cont (cont_cls, target, GNUNET_SYSERR);
617 return 0; /* Can never send if we don't have an address!! */
620 /* Build the message to be sent */
621 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
622 ssize = sizeof (struct UDPMessage) + msgbuf_size;
624 message->header.size = htons (ssize);
625 message->header.type = htons (0);
626 memcpy (&message->sender, plugin->env->my_identity,
627 sizeof (struct GNUNET_PeerIdentity));
628 memcpy (&message[1], msgbuf, msgbuf_size);
630 if (addrlen == sizeof (struct IPv6UdpAddress))
633 memset (&a6, 0, sizeof (a6));
634 #if HAVE_SOCKADDR_IN_SIN_LEN
635 a6.sin6_len = sizeof (a6);
637 a6.sin6_family = AF_INET6;
638 a6.sin6_port = t6->u6_port;
639 memcpy (&a6.sin6_addr,
641 sizeof (struct in6_addr));
645 else if (addrlen == sizeof (struct IPv4UdpAddress))
648 memset (&a4, 0, sizeof (a4));
649 #if HAVE_SOCKADDR_IN_SIN_LEN
650 a4.sin_len = sizeof (a4);
652 a4.sin_family = AF_INET;
653 a4.sin_port = t4->u_port;
654 a4.sin_addr.s_addr = t4->ipv4_addr;
661 GNUNET_free (message);
665 /* Actually send the message */
667 GNUNET_NETWORK_socket_sendto (send_handle, message, ssize,
670 if (GNUNET_SYSERR == sent)
671 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "sendto");
672 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
673 "UDP transmit %u-byte message to %s (%d: %s)\n",
674 (unsigned int) ssize,
675 GNUNET_a2s (sb, sbs),
677 (sent < 0) ? STRERROR (errno) : "ok");
680 if (sent == GNUNET_SYSERR)
681 cont (cont_cls, target, GNUNET_SYSERR);
684 cont (cont_cls, target, GNUNET_OK);
688 GNUNET_free (message);
693 * We learned about a peer (possibly behind NAT) so run the
694 * gnunet-nat-client to send dummy ICMP responses
696 * @param plugin the plugin for this transport
697 * @param addr the address of the peer
698 * @param addrlen the length of the address
701 run_gnunet_nat_client (struct Plugin *plugin, const char *addr, size_t addrlen)
703 char addr_buf[INET_ADDRSTRLEN];
704 char *address_as_string;
705 char *port_as_string;
706 struct GNUNET_OS_Process *proc;
707 const struct IPv4UdpAddress *t4;
709 GNUNET_assert(addrlen == sizeof(struct IPv4UdpAddress));
710 t4 = (struct IPv4UdpAddress *)addr;
712 if (NULL == inet_ntop (AF_INET,
714 addr_buf, INET_ADDRSTRLEN))
716 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
719 address_as_string = GNUNET_strdup (addr_buf);
720 GNUNET_asprintf(&port_as_string, "%d", plugin->port);
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 _("Running gnunet-nat-client with arguments: %s %s %d\n"), plugin->external_address, address_as_string, plugin->port);
726 /* Start the server process */
727 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-nat-client", "gnunet-nat-client", plugin->external_address, address_as_string, port_as_string, NULL);
728 GNUNET_free(address_as_string);
729 GNUNET_free(port_as_string);
732 GNUNET_OS_process_wait (proc);
733 GNUNET_OS_process_close (proc);
739 * Function that can be used by the transport service to transmit
740 * a message using the plugin.
743 * @param target who should receive this message (ignored by UDP)
744 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
745 * @param msgbuf_size the size of the msgbuf to send
746 * @param priority how important is the message (ignored by UDP)
747 * @param timeout when should we time out (give up) if we can not transmit?
748 * @param session identifier used for this session (can be NULL)
749 * @param addr the addr to send the message to, needs to be a sockaddr for us
750 * @param addrlen the len of addr
751 * @param force_address not used, we had better have an address to send to
752 * because we are stateless!!
753 * @param cont continuation to call once the message has
754 * been transmitted (or if the transport is ready
755 * for the next transmission call; or if the
756 * peer disconnected...)
757 * @param cont_cls closure for cont
759 * @return the number of bytes written (may return 0 and the message can
760 * still be transmitted later!)
763 udp_plugin_send (void *cls,
764 const struct GNUNET_PeerIdentity *target,
767 unsigned int priority,
768 struct GNUNET_TIME_Relative timeout,
769 struct Session *session,
773 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
775 struct Plugin *plugin = cls;
777 struct MessageQueue *temp_message;
778 struct PeerSession *peer_session;
780 const struct IPv4UdpAddress *t4;
782 if (force_address == GNUNET_SYSERR)
783 return GNUNET_SYSERR;
784 GNUNET_assert (NULL == session);
786 other_peer_natd = GNUNET_NO;
787 if (addrlen == sizeof(struct IPv4UdpAddress))
790 if (ntohs(t4->u_port) == 0)
791 other_peer_natd = GNUNET_YES;
793 else if (addrlen != sizeof(struct IPv6UdpAddress))
796 return -1; /* Must have an address to send to */
800 if ((other_peer_natd == GNUNET_YES) && (plugin->allow_nat == GNUNET_YES))
802 peer_session = find_session(plugin, target);
803 if (peer_session == NULL) /* We have a new peer to add */
806 * The first time, we can assume we have no knowledge of a
807 * working port for this peer, call the ICMP/UDP message sender
810 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
811 peer_session->connect_addr = GNUNET_malloc(addrlen);
812 memcpy(peer_session->connect_addr, addr, addrlen);
813 peer_session->connect_alen = addrlen;
814 peer_session->plugin = plugin;
815 peer_session->sock = NULL;
816 memcpy(&peer_session->target, target, sizeof(struct GNUNET_PeerIdentity));
817 peer_session->expecting_welcome = GNUNET_YES;
819 peer_session->next = plugin->sessions;
820 plugin->sessions = peer_session;
822 peer_session->messages = GNUNET_malloc(sizeof(struct MessageQueue));
823 peer_session->messages->msgbuf = GNUNET_malloc(msgbuf_size);
824 memcpy(peer_session->messages->msgbuf, msgbuf, msgbuf_size);
825 peer_session->messages->msgbuf_size = msgbuf_size;
826 peer_session->messages->timeout = GNUNET_TIME_relative_to_absolute(timeout);
827 peer_session->messages->cont = cont;
828 peer_session->messages->cont_cls = cont_cls;
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target));
833 run_gnunet_nat_client(plugin, addr, addrlen);
837 if (peer_session->expecting_welcome == GNUNET_NO) /* We are "connected" */
839 sent = udp_real_send(cls,
844 peer_session->connect_addr, peer_session->connect_alen,
847 else /* Haven't gotten a response from this peer, queue message */
849 temp_message = GNUNET_malloc(sizeof(struct MessageQueue));
850 temp_message->msgbuf = GNUNET_malloc(msgbuf_size);
851 memcpy(temp_message->msgbuf, msgbuf, msgbuf_size);
852 temp_message->msgbuf_size = msgbuf_size;
853 temp_message->timeout = GNUNET_TIME_relative_to_absolute(timeout);
854 temp_message->cont = cont;
855 temp_message->cont_cls = cont_cls;
856 temp_message->next = peer_session->messages;
857 peer_session->messages = temp_message;
861 else if (other_peer_natd == GNUNET_NO) /* Other peer not behind a NAT, so we can just send the message as is */
863 sent = udp_real_send(cls,
864 (addrlen == sizeof (struct IPv4UdpAddress)) ? plugin->udp_sockv4.desc : plugin->udp_sockv6.desc,
867 priority, timeout, addr, addrlen,
870 else /* Other peer is NAT'd, but we don't want to play with them (or can't!) */
872 return GNUNET_SYSERR;
875 /* When GNUNET_SYSERR is returned from udp_real_send, we will still call
876 * the callback so must not return GNUNET_SYSERR!
877 * If we did, then transport context would get freed twice. */
878 if (sent == GNUNET_SYSERR)
885 add_to_address_list (struct Plugin *plugin,
889 struct LocalAddrList *lal;
891 lal = plugin->lal_head;
894 if ( (lal->size == arg_size) &&
895 (0 == memcmp (&lal[1], arg, arg_size)) )
899 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
900 lal->size = arg_size;
901 memcpy (&lal[1], arg, arg_size);
902 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
909 check_local_addr (struct Plugin *plugin,
913 struct LocalAddrList *lal;
915 lal = plugin->lal_head;
918 if ( (lal->size == arg_size) &&
919 (0 == memcmp (&lal[1], arg, arg_size)) )
923 return GNUNET_SYSERR;
926 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
929 int local = GNUNET_NO;
930 int af = addr->sa_family;
935 uint32_t netmask = 0x7F000000;
936 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
937 res = (address >> 24) ^ (netmask >> 24);
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
950 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
951 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
967 * Add the IP of our network interface to the list of
968 * our external IP addresses.
971 process_interfaces (void *cls,
974 const struct sockaddr *addr, socklen_t addrlen)
976 struct Plugin *plugin = cls;
978 struct IPv4UdpAddress t4;
979 struct IPv6UdpAddress t6;
983 char buf[INET6_ADDRSTRLEN];
986 af = addr->sa_family;
988 if (plugin->use_localaddresses == GNUNET_NO)
990 if (GNUNET_YES == check_localaddress (addr, addrlen))
993 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
995 "Not notifying transport of address `%s' (local address)\n",
996 GNUNET_a2s (addr, addrlen));
1002 memset(buf, 0, INET6_ADDRSTRLEN);
1005 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1006 GNUNET_assert(NULL != inet_ntop(AF_INET, &t4.ipv4_addr, &buf[0], INET_ADDRSTRLEN));
1007 if ((plugin->bind6_address != NULL) || ((plugin->bind_address != NULL) && (0 != strcmp(buf, plugin->bind_address))))
1009 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Not notifying transport of address %s\n", "UDP", GNUNET_a2s (addr, addrlen));
1012 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (uint32_t));
1013 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
1015 t4.u_port = htons (DEFAULT_NAT_PORT);
1017 else if (plugin->behind_nat == GNUNET_YES) /* We are behind NAT, but will advertise NAT and normal addresses */
1019 addr_nat = GNUNET_malloc(sizeof(t4));
1020 t4.u_port = htons (DEFAULT_NAT_PORT);
1021 memcpy(addr_nat, &t4, sizeof(t4));
1022 t4.u_port = plugin->port;
1026 t4.u_port = htons(plugin->port);
1031 else if (af == AF_INET6)
1033 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1035 /* skip link local addresses */
1038 memcpy (&t6.ipv6_addr,
1039 &((struct sockaddr_in6 *) addr)->sin6_addr,
1040 sizeof (struct in6_addr));
1041 GNUNET_assert(NULL != inet_ntop(AF_INET6, &t6.ipv6_addr, &buf[0], INET6_ADDRSTRLEN));
1042 if (((plugin->bind_address != NULL) && (0 != strcmp(buf, plugin->bind_address)))
1043 || ((plugin->bind6_address != NULL) && (0 != strcmp(buf, plugin->bind6_address))))
1045 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Not notifying transport of address %s\n", "UDP", GNUNET_a2s (addr, addrlen));
1048 add_to_address_list (plugin, &t6.ipv6_addr, sizeof (struct in6_addr));
1049 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
1051 t6.u6_port = htons (DEFAULT_NAT_PORT);
1053 else if (plugin->behind_nat == GNUNET_YES)
1055 addr_nat = GNUNET_malloc(sizeof(t6));
1056 t6.u6_port = htons (DEFAULT_NAT_PORT);
1057 memcpy(addr_nat, &t6, sizeof(t6));
1058 t6.u6_port = plugin->port;
1062 t6.u6_port = htons (plugin->port);
1074 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1075 GNUNET_ERROR_TYPE_BULK,
1076 _("Found address `%s' (%s)\n"),
1077 GNUNET_a2s (addr, addrlen), name);
1079 if (addr_nat != NULL)
1081 plugin->env->notify_address (plugin->env->cls,
1083 addr_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
1084 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1085 GNUNET_ERROR_TYPE_BULK,
1086 _("Found NAT address `%s' (%s)\n"),
1087 GNUNET_a2s (addr_nat, args), name);
1088 GNUNET_free(addr_nat);
1091 plugin->env->notify_address (plugin->env->cls,
1093 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
1099 * Function called by the resolver for each address obtained from DNS
1100 * for our own hostname. Add the addresses to the list of our
1101 * external IP addresses.
1103 * @param cls closure
1104 * @param addr one of the addresses of the host, NULL for the last address
1105 * @param addrlen length of the address
1108 process_hostname_ips (void *cls,
1109 const struct sockaddr *addr, socklen_t addrlen)
1111 struct Plugin *plugin = cls;
1115 plugin->hostname_dns = NULL;
1118 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1123 * Send UDP probe messages or UDP keepalive messages, depending on the
1124 * state of the connection.
1126 * @param cls closure for this call (should be the main Plugin)
1127 * @param tc task context for running this
1130 send_udp_probe_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1132 struct UDP_NAT_Probes *probe = cls;
1133 struct UDP_NAT_ProbeMessage message;
1134 struct Plugin *plugin = probe->plugin;
1136 memset (&message, 0, sizeof (message));
1137 message.header.size = htons(sizeof(struct UDP_NAT_ProbeMessage));
1138 message.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE);
1139 /* If they gave us a port, use that. If not, try our port. */
1140 if (ntohs(probe->addr.u_port) == 0)
1141 probe->addr.u_port = htons(plugin->port);
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 _("Sending a probe to port %d\n"), ntohs(probe->addr.u_port));
1148 udp_real_send(plugin,
1149 plugin->udp_sockv4.desc,
1151 (char *)&message, ntohs(message.header.size), 0,
1152 GNUNET_TIME_relative_get_unit(),
1153 &probe->addr, sizeof(struct IPv4UdpAddress),
1154 &udp_probe_continuation, probe);
1159 * Continuation for probe sends. If the last probe was sent
1160 * "successfully", schedule sending of another one. If not,
1164 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result)
1166 struct UDP_NAT_Probes *probe = cls;
1167 /*struct Plugin *plugin = probe->plugin;*/
1169 if ((result == GNUNET_OK) && (probe->count < MAX_PROBES))
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1173 _("Scheduling next probe for 10000 milliseconds\n"));
1175 probe->task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10000), &send_udp_probe_message, probe);
1177 else /* Destroy the probe context. */
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 _("Sending probe didn't go well...\n"));
1187 * Find probe message by address
1189 * @param plugin the plugin for this transport
1190 * @param address_string the ip address as a string
1192 struct UDP_NAT_Probes *
1193 find_probe(struct Plugin *plugin, char * address_string)
1195 struct UDP_NAT_Probes *pos;
1197 pos = plugin->probes;
1199 if (strcmp(pos->address_string, address_string) == 0)
1207 * @param cls the plugin handle
1208 * @param tc the scheduling context (for rescheduling this function again)
1210 * We have been notified that gnunet-nat-server has written something to stdout.
1211 * Handle the output, then reschedule this function to be called again once
1212 * more is available.
1216 udp_plugin_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 struct Plugin *plugin = cls;
1221 memset(&mybuf, 0, sizeof(mybuf));
1223 struct UDP_NAT_Probes *temp_probe;
1226 struct IPv4UdpAddress a4;
1228 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1231 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, &mybuf, sizeof(mybuf));
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 _("Finished reading from server stdout with code: %d\n"), bytes);
1243 for (i = 0; i < sizeof(mybuf); i++)
1245 if (mybuf[i] == '\n')
1248 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
1251 port_start = &mybuf[i + 1];
1255 if (port_start != NULL)
1256 port = atoi(port_start);
1259 plugin->server_read_task =
1260 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1261 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 _("nat-server-read read: %s port %d\n"), &mybuf, port);
1271 * We have received an ICMP response, ostensibly from a non-NAT'd peer
1272 * that wants to connect to us! Send a message to establish a connection.
1274 if (inet_pton(AF_INET, &mybuf[0], &a4.ipv4_addr) != 1)
1277 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1278 _("nat-server-read malformed address\n"), &mybuf, port);
1280 plugin->server_read_task =
1281 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1282 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1286 temp_probe = find_probe(plugin, &mybuf[0]);
1288 if (temp_probe == NULL)
1290 temp_probe = GNUNET_malloc(sizeof(struct UDP_NAT_Probes));
1291 temp_probe->address_string = strdup(&mybuf[0]);
1292 GNUNET_assert (1 == inet_pton(AF_INET, &mybuf[0], &temp_probe->addr.ipv4_addr));
1293 temp_probe->addr.u_port = htons(port);
1294 temp_probe->next = plugin->probes;
1295 temp_probe->plugin = plugin;
1296 temp_probe->task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500), &send_udp_probe_message, temp_probe);
1297 plugin->probes = temp_probe;
1300 plugin->server_read_task =
1301 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1302 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1308 * Demultiplexer for UDP NAT messages
1310 * @param plugin the main plugin for this transport
1311 * @param sender from which peer the message was received
1312 * @param currhdr pointer to the header of the message
1313 * @param sender_addr the address from which the message was received
1314 * @param fromlen the length of the address
1315 * @param sockinfo which socket did we receive the message on
1318 udp_demultiplexer(struct Plugin *plugin,
1319 struct GNUNET_PeerIdentity *sender,
1320 const struct GNUNET_MessageHeader *currhdr,
1321 const void *sender_addr,
1322 size_t fromlen, struct UDP_Sock_Info *sockinfo)
1324 struct UDP_NAT_ProbeMessageReply *outgoing_probe_reply;
1325 struct UDP_NAT_ProbeMessageConfirmation *outgoing_probe_confirmation;
1326 char addr_buf[INET_ADDRSTRLEN];
1327 struct UDP_NAT_Probes *outgoing_probe;
1328 struct PeerSession *peer_session;
1329 struct MessageQueue *pending_message;
1330 struct MessageQueue *pending_message_temp;
1331 uint16_t incoming_port;
1332 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1333 if (memcmp(sender, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1337 _("Received a message from myself, dropping!!!\n"));
1343 GNUNET_assert(sender_addr != NULL); /* Can recvfrom have a NULL address? */
1344 if (fromlen == sizeof(struct IPv4UdpAddress))
1346 incoming_port = ntohs(((struct IPv4UdpAddress *)sender_addr)->u_port);
1348 else if (fromlen == sizeof(struct IPv6UdpAddress))
1350 incoming_port = ntohs(((struct IPv6UdpAddress *)sender_addr)->u6_port);
1353 switch (ntohs(currhdr->type))
1355 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE:
1356 /* Send probe reply */
1357 outgoing_probe_reply = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageReply));
1358 outgoing_probe_reply->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageReply));
1359 outgoing_probe_reply->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY);
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 _("Received a probe on listen port %d, sent_from port %d\n"),
1364 sockinfo->port, incoming_port);
1367 udp_real_send(plugin, sockinfo->desc, NULL,
1368 (char *)outgoing_probe_reply,
1369 ntohs(outgoing_probe_reply->header.size), 0,
1370 GNUNET_TIME_relative_get_unit(),
1371 sender_addr, fromlen,
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 _("Sent PROBE REPLY to port %d on outgoing port %d\n"),
1377 incoming_port, sockinfo->port);
1379 GNUNET_free(outgoing_probe_reply);
1381 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY:
1382 /* Check for existing probe, check ports returned, send confirmation if all is well */
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 _("Received PROBE REPLY from port %d on incoming port %d\n"), incoming_port, sockinfo->port);
1387 if (fromlen == sizeof(struct IPv4UdpAddress))
1389 memset(&addr_buf, 0, sizeof(addr_buf));
1390 if (NULL == inet_ntop (AF_INET,
1391 &((struct IPv4UdpAddress *) sender_addr)->ipv4_addr, addr_buf,
1394 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1397 outgoing_probe = find_probe(plugin, &addr_buf[0]);
1398 if (outgoing_probe != NULL)
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 _("Sending confirmation that we were reached!\n"));
1404 outgoing_probe_confirmation = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1405 outgoing_probe_confirmation->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1406 outgoing_probe_confirmation->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM);
1407 udp_real_send(plugin, sockinfo->desc, NULL,
1408 (char *)outgoing_probe_confirmation,
1409 ntohs(outgoing_probe_confirmation->header.size), 0,
1410 GNUNET_TIME_relative_get_unit(),
1411 sender_addr, fromlen, NULL, NULL);
1413 if (outgoing_probe->task != GNUNET_SCHEDULER_NO_TASK)
1415 GNUNET_SCHEDULER_cancel(outgoing_probe->task);
1416 outgoing_probe->task = GNUNET_SCHEDULER_NO_TASK;
1417 /* Schedule task to timeout and remove probe if confirmation not received */
1419 GNUNET_free(outgoing_probe_confirmation);
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1425 _("Received a probe reply, but have no record of a sent probe!\n"));
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1433 _("Received a probe reply, but sender address size is WRONG (should be %d, is %d)!\n"), sizeof(struct IPv4UdpAddress), fromlen);
1437 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM:
1438 peer_session = find_session(plugin, sender);
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441 _("Looking up peer session for peer %s\n"), GNUNET_i2s(sender));
1443 if (peer_session == NULL) /* Shouldn't this NOT happen? */
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1447 _("Peer not in list, adding (THIS MAY BE A MISTAKE) %s\n"), GNUNET_i2s(sender));
1449 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
1450 peer_session->connect_addr = GNUNET_malloc(fromlen);
1451 memcpy(peer_session->connect_addr, sender_addr, fromlen);
1452 peer_session->connect_alen = fromlen;
1453 peer_session->plugin = plugin;
1454 peer_session->sock = sockinfo->desc;
1455 memcpy(&peer_session->target, sender, sizeof(struct GNUNET_PeerIdentity));
1456 peer_session->expecting_welcome = GNUNET_NO;
1458 peer_session->next = plugin->sessions;
1459 plugin->sessions = peer_session;
1461 peer_session->messages = NULL;
1463 else if (peer_session->expecting_welcome == GNUNET_YES)
1465 peer_session->expecting_welcome = GNUNET_NO;
1466 peer_session->sock = sockinfo->desc;
1467 if (peer_session->connect_alen == sizeof(struct IPv4UdpAddress))
1469 ((struct IPv4UdpAddress *)peer_session->connect_addr)->u_port = htons(incoming_port);
1471 else if (peer_session->connect_alen == sizeof(struct IPv4UdpAddress))
1473 ((struct IPv6UdpAddress *)peer_session->connect_addr)->u6_port = htons(incoming_port);
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 _("Received a probe confirmation, will send to peer on port %d\n"), incoming_port);
1480 if (peer_session->messages != NULL)
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 _("Received a probe confirmation, sending queued messages.\n"));
1486 pending_message = peer_session->messages;
1488 while (pending_message != NULL)
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 _("sending queued message %d\n"), count);
1494 udp_real_send(plugin,
1496 &peer_session->target,
1497 pending_message->msgbuf,
1498 pending_message->msgbuf_size, 0,
1499 GNUNET_TIME_relative_get_unit(),
1500 peer_session->connect_addr,
1501 peer_session->connect_alen,
1502 pending_message->cont,
1503 pending_message->cont_cls);
1505 pending_message_temp = pending_message;
1506 pending_message = pending_message->next;
1507 GNUNET_free(pending_message_temp->msgbuf);
1508 GNUNET_free(pending_message_temp);
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 _("finished sending queued message %d\n"), count);
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 _("Received probe confirmation for already confirmed peer!\n"));
1525 /* Received confirmation, add peer with address/port specified */
1527 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_KEEPALIVE:
1528 /* Once we've sent NAT_PROBE_CONFIRM change to sending keepalives */
1529 /* If we receive these just ignore! */
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1535 "Sending message type %d to transport!\n",
1536 ntohs(currhdr->type));
1539 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1540 distance[0].value = htonl (UDP_DIRECT_DISTANCE);
1541 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1542 distance[1].value = htonl (0);
1544 plugin->env->receive (plugin->env->cls, sender, currhdr,
1545 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1546 NULL, sender_addr, fromlen);
1553 * @param cls the plugin handle
1554 * @param tc the scheduling context (for rescheduling this function again)
1556 * We have been notified that our writeset has something to read. We don't
1557 * know which socket needs to be read, so we have to check each one
1558 * Then reschedule this function to be called again once more is available.
1562 udp_plugin_select (void *cls,
1563 const struct GNUNET_SCHEDULER_TaskContext *tc)
1565 struct Plugin *plugin = cls;
1567 struct UDPMessage *msg;
1568 struct GNUNET_PeerIdentity sender;
1575 const struct GNUNET_MessageHeader *currhdr;
1576 struct IPv4UdpAddress t4;
1577 struct IPv6UdpAddress t6;
1578 const struct sockaddr_in *s4;
1579 const struct sockaddr_in6 *s6;
1582 struct UDP_Sock_Info *udp_sock;
1585 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1586 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1589 if (GNUNET_NETWORK_fdset_isset (tc->read_ready,
1590 plugin->udp_sockv4.desc))
1591 udp_sock = &plugin->udp_sockv4;
1592 else if (GNUNET_NETWORK_fdset_isset (tc->read_ready,
1593 plugin->udp_sockv6.desc))
1594 udp_sock = &plugin->udp_sockv6;
1595 if (NULL == udp_sock)
1600 fromlen = sizeof (addr);
1601 memset (&addr, 0, sizeof(addr));
1603 GNUNET_NETWORK_socket_recvfrom (udp_sock->desc, buf, sizeof (buf),
1604 (struct sockaddr *)&addr, &fromlen);
1606 if (AF_INET == ((struct sockaddr *)addr)->sa_family)
1608 s4 = (const struct sockaddr_in*) &addr;
1609 t4.u_port = s4->sin_port;
1610 t4.ipv4_addr = s4->sin_addr.s_addr;
1612 calen = sizeof (t4);
1614 else if (AF_INET6 == ((struct sockaddr *)addr)->sa_family)
1616 s6 = (const struct sockaddr_in6*) &addr;
1617 t6.u6_port = s6->sin6_port;
1618 memcpy (&t6.ipv6_addr,
1620 sizeof (struct in6_addr));
1622 calen = sizeof (t6);
1630 if (ret < sizeof (struct UDPMessage))
1632 GNUNET_break_op (0);
1633 plugin->select_task =
1634 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1635 GNUNET_SCHEDULER_NO_TASK,
1636 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1637 NULL, &udp_plugin_select, plugin);
1640 msg = (struct UDPMessage *) buf;
1641 csize = ntohs (msg->header.size);
1642 if ( (csize < sizeof (struct UDPMessage)) ||
1645 GNUNET_break_op (0);
1646 plugin->select_task =
1647 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1648 GNUNET_SCHEDULER_NO_TASK,
1649 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1650 NULL, &udp_plugin_select, plugin);
1653 msgbuf = (char *)&msg[1];
1654 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1656 tsize = csize - sizeof (struct UDPMessage);
1657 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
1659 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
1660 csize = ntohs (currhdr->size);
1661 if ( (csize < sizeof (struct GNUNET_MessageHeader)) ||
1662 (csize > tsize - offset) )
1664 GNUNET_break_op (0);
1667 udp_demultiplexer(plugin, &sender, currhdr,
1668 ca, calen, udp_sock);
1671 plugin->select_task =
1672 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1673 GNUNET_SCHEDULER_NO_TASK,
1674 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1675 NULL, &udp_plugin_select, plugin);
1680 * Create a slew of UDP sockets. If possible, use IPv6 and IPv4.
1682 * @param cls closure for server start, should be a struct Plugin *
1683 * @return number of sockets created or GNUNET_SYSERR on error
1686 udp_transport_server_start (void *cls)
1688 struct Plugin *plugin = cls;
1689 struct sockaddr_in serverAddrv4;
1690 struct sockaddr_in6 serverAddrv6;
1691 struct sockaddr *serverAddr;
1693 int sockets_created;
1697 sockets_created = 0;
1698 if (plugin->behind_nat == GNUNET_YES)
1700 /* Pipe to read from started processes stdout (on read end) */
1701 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES);
1702 if (plugin->server_stdout == NULL)
1703 return sockets_created;
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706 "Starting gnunet-nat-server process cmd: %s %s\n",
1707 "gnunet-nat-server",
1708 plugin->internal_address);
1710 /* Start the server process */
1711 plugin->server_proc = GNUNET_OS_start_process(NULL,
1712 plugin->server_stdout,
1713 "gnunet-nat-server",
1714 "gnunet-nat-server",
1715 plugin->internal_address, NULL);
1716 if (plugin->server_proc == NULL)
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1720 "Failed to start gnunet-nat-server process\n");
1722 return GNUNET_SYSERR;
1724 /* Close the write end of the read pipe */
1725 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1727 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout, GNUNET_DISK_PIPE_END_READ);
1728 plugin->server_read_task =
1729 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1730 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1734 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "transport-udp",
1737 plugin->udp_sockv6.desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1738 if (NULL == plugin->udp_sockv6.desc)
1740 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
1744 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1745 #if HAVE_SOCKADDR_IN_SIN_LEN
1746 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1749 serverAddrv6.sin6_family = AF_INET6;
1750 serverAddrv6.sin6_addr = in6addr_any;
1751 if (plugin->bind6_address != NULL)
1753 if (1 != inet_pton(AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1757 serverAddrv6.sin6_port = htons (plugin->port);
1758 addrlen = sizeof (serverAddrv6);
1759 serverAddr = (struct sockaddr *) &serverAddrv6;
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "Binding to IPv6 port %d\n",
1763 ntohs(serverAddrv6.sin6_port));
1766 while (GNUNET_NETWORK_socket_bind (plugin->udp_sockv6.desc, serverAddr, addrlen) !=
1769 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772 "IPv6 Binding failed, trying new port %d\n",
1773 ntohs(serverAddrv6.sin6_port));
1778 GNUNET_NETWORK_socket_close (plugin->udp_sockv6.desc);
1779 plugin->udp_sockv6.desc = NULL;
1783 if (plugin->udp_sockv6.desc != NULL)
1785 plugin->udp_sockv6.port = ntohs(serverAddrv6.sin6_port);
1791 plugin->udp_sockv4.desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1792 if (NULL == plugin->udp_sockv4.desc)
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "socket");
1798 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1799 #if HAVE_SOCKADDR_IN_SIN_LEN
1800 serverAddrv4.sin_len = sizeof (serverAddrv4);
1802 serverAddrv4.sin_family = AF_INET;
1803 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1804 if (plugin->bind_address != NULL)
1806 if (1 != inet_pton(AF_INET, plugin->bind_address, &serverAddrv4.sin_addr))
1809 serverAddrv4.sin_port = htons (plugin->port);
1810 addrlen = sizeof (serverAddrv4);
1811 serverAddr = (struct sockaddr *) &serverAddrv4;
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1814 "Binding to IPv4 port %d\n",
1815 ntohs(serverAddrv4.sin_port));
1818 while (GNUNET_NETWORK_socket_bind (plugin->udp_sockv4.desc, serverAddr, addrlen) !=
1821 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1824 "IPv4 Binding failed, trying new port %d\n",
1825 ntohs(serverAddrv4.sin_port));
1830 GNUNET_NETWORK_socket_close (plugin->udp_sockv4.desc);
1831 plugin->udp_sockv4.desc = NULL;
1835 if (plugin->udp_sockv4.desc != NULL)
1837 plugin->udp_sockv4.port = ntohs(serverAddrv4.sin_port);
1842 plugin->rs = GNUNET_NETWORK_fdset_create ();
1843 GNUNET_NETWORK_fdset_zero (plugin->rs);
1844 if (NULL != plugin->udp_sockv4.desc)
1845 GNUNET_NETWORK_fdset_set (plugin->rs,
1846 plugin->udp_sockv4.desc);
1847 if (NULL != plugin->udp_sockv6.desc)
1848 GNUNET_NETWORK_fdset_set (plugin->rs,
1849 plugin->udp_sockv6.desc);
1851 plugin->select_task =
1852 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1853 GNUNET_SCHEDULER_NO_TASK,
1854 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1855 NULL, &udp_plugin_select, plugin);
1856 return sockets_created;
1862 * Check if the given port is plausible (must be either
1863 * our listen port or our advertised port). If it is
1864 * neither, we return GNUNET_SYSERR.
1866 * @param plugin global variables
1867 * @param in_port port number to check
1868 * @return GNUNET_OK if port is either open_port or adv_port
1871 check_port (struct Plugin *plugin, uint16_t in_port)
1873 if ( (plugin->behind_nat == GNUNET_YES) && (in_port == 0) )
1875 if ( (plugin->only_nat_addresses == GNUNET_YES) &&
1876 (plugin->behind_nat == GNUNET_YES) )
1877 return GNUNET_SYSERR; /* odd case... */
1878 if (in_port == plugin->port)
1880 return GNUNET_SYSERR;
1885 * Function that will be called to check if a binary address for this
1886 * plugin is well-formed and corresponds to an address for THIS peer
1887 * (as per our configuration). Naturally, if absolutely necessary,
1888 * plugins can be a bit conservative in their answer, but in general
1889 * plugins should make sure that the address does not redirect
1890 * traffic to a 3rd party that might try to man-in-the-middle our
1893 * @param cls closure, should be our handle to the Plugin
1894 * @param addr pointer to the address
1895 * @param addrlen length of addr
1896 * @return GNUNET_OK if this is a plausible address for this peer
1897 * and transport, GNUNET_SYSERR if not
1901 udp_check_address (void *cls,
1905 struct Plugin *plugin = cls;
1906 char buf[INET6_ADDRSTRLEN];
1912 struct IPv4UdpAddress *v4;
1913 struct IPv6UdpAddress *v6;
1915 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1916 (addrlen != sizeof (struct IPv6UdpAddress)))
1918 GNUNET_break_op (0);
1919 return GNUNET_SYSERR;
1922 if (addrlen == sizeof (struct IPv4UdpAddress))
1924 v4 = (struct IPv4UdpAddress *) addr;
1926 check_port (plugin, ntohs (v4->u_port)))
1927 return GNUNET_SYSERR;
1929 check_local_addr (plugin, &v4->ipv4_addr, sizeof (uint32_t)))
1930 return GNUNET_SYSERR;
1933 port = ntohs (v4->u_port);
1934 memcpy (&a4, &v4->ipv4_addr, sizeof (a4));
1939 v6 = (struct IPv6UdpAddress *) addr;
1940 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1942 GNUNET_break_op (0);
1943 return GNUNET_SYSERR;
1946 check_port (plugin, ntohs (v6->u6_port)))
1947 return GNUNET_SYSERR;
1949 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1950 return GNUNET_SYSERR;
1953 port = ntohs (v6->u6_port);
1954 memcpy (&a6, &v6->ipv6_addr, sizeof (a6));
1958 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "Informing transport service about my address `%s:%u'\n",
1971 * Append our port and forward the result.
1974 append_port (void *cls, const char *hostname)
1976 struct PrettyPrinterContext *ppc = cls;
1979 if (hostname == NULL)
1981 ppc->asc (ppc->asc_cls, NULL);
1985 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1986 ppc->asc (ppc->asc_cls, ret);
1992 * Convert the transports address to a nice, human-readable
1995 * @param cls closure
1996 * @param type name of the transport that generated the address
1997 * @param addr one of the addresses of the host, NULL for the last address
1998 * the specific address format depends on the transport
1999 * @param addrlen length of the address
2000 * @param numeric should (IP) addresses be displayed in numeric form?
2001 * @param timeout after how long should we give up?
2002 * @param asc function to call on each string
2003 * @param asc_cls closure for asc
2006 udp_plugin_address_pretty_printer (void *cls,
2011 struct GNUNET_TIME_Relative timeout,
2012 GNUNET_TRANSPORT_AddressStringCallback asc,
2015 struct Plugin *plugin = cls;
2016 struct PrettyPrinterContext *ppc;
2019 struct sockaddr_in a4;
2020 struct sockaddr_in6 a6;
2021 const struct IPv4UdpAddress *u4;
2022 const struct IPv6UdpAddress *u6;
2025 if (addrlen == sizeof (struct IPv6UdpAddress))
2028 memset (&a6, 0, sizeof (a6));
2029 a6.sin6_family = AF_INET6;
2030 a6.sin6_port = u6->u6_port;
2031 memcpy (&a6.sin6_addr,
2033 sizeof (struct in6_addr));
2034 port = ntohs (u6->u6_port);
2038 else if (addrlen == sizeof (struct IPv4UdpAddress))
2041 memset (&a4, 0, sizeof (a4));
2042 a4.sin_family = AF_INET;
2043 a4.sin_port = u4->u_port;
2044 a4.sin_addr.s_addr = u4->ipv4_addr;
2045 port = ntohs (u4->u_port);
2051 /* invalid address */
2052 GNUNET_break_op (0);
2053 asc (asc_cls, NULL);
2056 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
2058 ppc->asc_cls = asc_cls;
2060 GNUNET_RESOLVER_hostname_get (plugin->env->cfg,
2063 !numeric, timeout, &append_port, ppc);
2067 * Return the actual path to a file found in the current
2068 * PATH environment variable.
2070 * @param binary the name of the file to find
2073 get_path_from_PATH (char *binary)
2081 p = getenv ("PATH");
2084 path = GNUNET_strdup (p); /* because we write on it */
2085 buf = GNUNET_malloc (strlen (path) + 20);
2088 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2091 sprintf (buf, "%s/%s", pos, binary);
2092 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2099 sprintf (buf, "%s/%s", pos, binary);
2100 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2111 * Check whether the suid bit is set on a file.
2112 * Attempts to find the file using the current
2113 * PATH environment variable as a search path.
2115 * @param binary the name of the file to check
2118 check_gnunet_nat_binary(char *binary)
2120 struct stat statbuf;
2128 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2129 p = get_path_from_PATH (binaryexe);
2132 p = get_path_from_PATH (binary);
2136 if (0 != STAT (p, &statbuf))
2139 return GNUNET_SYSERR;
2143 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2144 (statbuf.st_uid == 0) )
2148 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2149 if (INVALID_SOCKET == rawsock)
2151 DWORD err = GetLastError ();
2152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
2153 return GNUNET_NO; /* not running as administrator */
2155 closesocket (rawsock);
2161 * Function called for a quick conversion of the binary address to
2162 * a numeric address. Note that the caller must not free the
2163 * address and that the next call to this function is allowed
2164 * to override the address again.
2166 * @param cls closure
2167 * @param addr binary address
2168 * @param addrlen length of the address
2169 * @return string representing the same address
2172 udp_address_to_string (void *cls,
2176 static char rbuf[INET6_ADDRSTRLEN + 10];
2177 char buf[INET6_ADDRSTRLEN];
2181 const struct IPv4UdpAddress *t4;
2182 const struct IPv6UdpAddress *t6;
2186 if (addrlen == sizeof (struct IPv6UdpAddress))
2190 port = ntohs (t6->u6_port);
2191 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
2194 else if (addrlen == sizeof (struct IPv4UdpAddress))
2198 port = ntohs (t4->u_port);
2199 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
2204 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
2205 GNUNET_snprintf (rbuf,
2214 * The exported method. Makes the core api available via a global and
2215 * returns the udp transport API.
2218 libgnunet_plugin_transport_udp_init (void *cls)
2220 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2221 unsigned long long mtu;
2222 unsigned long long port;
2223 struct GNUNET_TRANSPORT_PluginFunctions *api;
2224 struct Plugin *plugin;
2225 int sockets_created;
2228 int only_nat_addresses;
2229 int use_localaddresses;
2230 char *internal_address;
2231 char *external_address;
2232 struct IPv4UdpAddress v4_address;
2234 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2238 /* We are behind nat (according to the user) */
2239 if (check_gnunet_nat_binary("gnunet-nat-server") == GNUNET_YES)
2240 behind_nat = GNUNET_YES;
2243 behind_nat = GNUNET_NO;
2244 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Configuration specified you are behind a NAT, but gnunet-nat-server is not installed properly (suid bit not set)!\n");
2248 behind_nat = GNUNET_NO; /* We are not behind nat! */
2250 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2254 if (check_gnunet_nat_binary("gnunet-nat-client") == GNUNET_YES)
2255 allow_nat = GNUNET_YES; /* We will try to connect to NAT'd peers */
2258 allow_nat = GNUNET_NO;
2259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Configuration specified you want to connect to NAT'd peers, but gnunet-nat-client is not installed properly (suid bit not set)!\n");
2264 allow_nat = GNUNET_NO; /* We don't want to try to help NAT'd peers */
2266 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2268 "ONLY_NAT_ADDRESSES"))
2269 only_nat_addresses = GNUNET_YES; /* We will only report our addresses as NAT'd */
2271 only_nat_addresses = GNUNET_NO; /* We will report our addresses as NAT'd and non-NAT'd */
2273 external_address = NULL;
2274 if (((GNUNET_YES == behind_nat) || (GNUNET_YES == allow_nat)) && (GNUNET_OK !=
2275 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2278 &external_address)))
2280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2281 _("Require EXTERNAL_ADDRESS for service `%s' in configuration (either BEHIND_NAT or ALLOW_NAT set to YES)!\n"),
2286 if ((external_address != NULL) && (inet_pton(AF_INET, external_address, &v4_address.ipv4_addr) != 1))
2288 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Malformed EXTERNAL_ADDRESS %s given in configuration!\n", external_address);
2291 internal_address = NULL;
2292 if ((GNUNET_YES == behind_nat) && (GNUNET_OK !=
2293 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2296 &internal_address)))
2298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2299 _("Require INTERNAL_ADDRESS for service `%s' in configuration!\n"),
2301 GNUNET_free_non_null(external_address);
2305 if ((internal_address != NULL) && (inet_pton(AF_INET, internal_address, &v4_address.ipv4_addr) != 1))
2307 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Malformed INTERNAL_ADDRESS %s given in configuration!\n", internal_address);
2311 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2315 port = UDP_NAT_DEFAULT_PORT;
2316 else if (port > 65535)
2318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2319 _("Given `%s' option is out of range: %llu > %u\n"),
2323 GNUNET_free_non_null(external_address);
2324 GNUNET_free_non_null(internal_address);
2330 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2331 _("MTU %llu for `%s' is probably too low!\n"), mtu,
2334 use_localaddresses = GNUNET_NO;
2335 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2336 "transport-udp", "USE_LOCALADDR"))
2338 use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2343 plugin = GNUNET_malloc (sizeof (struct Plugin));
2344 plugin->external_address = external_address;
2345 plugin->internal_address = internal_address;
2346 plugin->port = port;
2347 plugin->behind_nat = behind_nat;
2348 plugin->allow_nat = allow_nat;
2349 plugin->only_nat_addresses = only_nat_addresses;
2351 plugin->use_localaddresses = use_localaddresses;
2353 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2356 api->send = &udp_plugin_send;
2357 api->disconnect = &udp_disconnect;
2358 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2359 api->address_to_string = &udp_address_to_string;
2360 api->check_address = &udp_check_address;
2362 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-udp", "BINDTO", &plugin->bind_address))
2363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Binding udp plugin to specific address: `%s'\n", plugin->bind_address);
2364 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-udp", "BINDTO6", &plugin->bind6_address))
2365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Binding udp plugin to specific address: `%s'\n", plugin->bind6_address);
2367 if (plugin->behind_nat == GNUNET_NO)
2369 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2372 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->cfg,
2374 HOSTNAME_RESOLVE_TIMEOUT,
2375 &process_hostname_ips,
2378 if ((plugin->behind_nat == GNUNET_YES) && (inet_pton(AF_INET, plugin->external_address, &v4_address.ipv4_addr) == 1))
2380 v4_address.u_port = htons(0);
2381 plugin->env->notify_address (plugin->env->cls,
2383 &v4_address, sizeof(v4_address), GNUNET_TIME_UNIT_FOREVER_REL);
2384 add_to_address_list (plugin, &v4_address.ipv4_addr, sizeof (uint32_t));
2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Notifying plugin of address %s:0\n", plugin->external_address);
2387 else if ((plugin->external_address != NULL) && (inet_pton(AF_INET, plugin->external_address, &v4_address.ipv4_addr) == 1))
2389 v4_address.u_port = htons(plugin->port);
2390 plugin->env->notify_address (plugin->env->cls,
2392 &v4_address, sizeof(v4_address), GNUNET_TIME_UNIT_FOREVER_REL);
2393 add_to_address_list (plugin, &v4_address.ipv4_addr, sizeof (uint32_t));
2396 sockets_created = udp_transport_server_start (plugin);
2397 if (sockets_created == 0)
2398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2399 _("Failed to open UDP sockets\n"));
2404 libgnunet_plugin_transport_udp_done (void *cls)
2406 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2407 struct Plugin *plugin = api->cls;
2408 struct LocalAddrList *lal;
2410 udp_transport_server_stop (plugin);
2411 if (NULL != plugin->hostname_dns)
2413 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
2414 plugin->hostname_dns = NULL;
2417 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2418 while (NULL != (lal = plugin->lal_head))
2420 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
2425 GNUNET_free (plugin);
2430 /* end of plugin_transport_udp.c */