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_service_lib.h"
50 #include "gnunet_signatures.h"
51 #include "gnunet_statistics_service.h"
52 #include "gnunet_transport_service.h"
53 #include "plugin_transport.h"
54 #include "transport.h"
56 #define DEBUG_UDP GNUNET_YES
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 u_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;
215 * Local network addresses (actual IP address follows this struct).
216 * PORT is NOT included!
222 * This is a doubly linked list.
224 struct LocalAddrList *next;
227 * This is a doubly linked list.
229 struct LocalAddrList *prev;
232 * Number of bytes of the address that follow
246 * Stored in a linked list.
248 struct PeerSession *next;
251 * Pointer to the global plugin struct.
253 struct Plugin *plugin;
256 * To whom are we talking to (set to our identity
257 * if we are still waiting for the welcome message)
259 struct GNUNET_PeerIdentity target;
262 * Address of the other peer (either based on our 'connect'
263 * call or on our 'accept' call).
268 * Length of connect_addr.
273 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
275 int expecting_welcome;
278 * From which socket do we need to send to this peer?
280 struct GNUNET_NETWORK_Handle *sock;
283 * Queue of messages for this peer, in the case that
284 * we have to await a connection...
286 struct MessageQueue *messages;
290 struct UDP_NAT_Probes
296 struct UDP_NAT_Probes *next;
299 * Address string that the server process returned to us
301 char *address_string;
304 * Timeout for this set of probes
306 struct GNUNET_TIME_Absolute timeout;
309 * Count of how many probes we've attempted
314 * The plugin this probe belongs to
316 struct Plugin *plugin;
319 * The task used to send these probes
321 GNUNET_SCHEDULER_TaskIdentifier task;
324 * Network address (always ipv4!)
326 struct IPv4UdpAddress addr;
332 * Information we keep for each of our listen sockets.
339 struct GNUNET_NETWORK_Handle *desc;
342 * The port we bound to
349 * Encapsulation of all of the state of the plugin.
356 struct GNUNET_TRANSPORT_PluginEnvironment *env;
359 * Handle to the network service.
361 struct GNUNET_SERVICE_Context *service;
364 * Session of peers with whom we are currently connected
366 struct PeerSession *sessions;
369 * Handle for request of hostname resolution, non-NULL if pending.
371 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
374 * ID of task used to update our addresses when one expires.
376 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
381 GNUNET_SCHEDULER_TaskIdentifier select_task;
389 * The external address given to us by the user. Must be actual
390 * outside visible address for NAT punching to work.
392 char *external_address;
395 * The internal address given to us by the user (or discovered).
397 char *internal_address;
400 * Address we were told to bind to exclusively (IPv4).
405 * Address we were told to bind to exclusively (IPv6).
410 * List of our IP addresses.
412 struct LocalAddrList *lal_head;
415 * Tail of our IP address list.
417 struct LocalAddrList *lal_tail;
422 struct GNUNET_NETWORK_FDSet *rs;
425 * stdout pipe handle for the gnunet-nat-server process
427 struct GNUNET_DISK_PipeHandle *server_stdout;
430 * stdout file handle (for reading) for the gnunet-nat-server process
432 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
437 struct UDP_NAT_Probes *probes;
440 * socket that we transmit all IPv4 data with
442 struct UDP_Sock_Info udp_sockv4;
445 * socket that we transmit all IPv6 data with
447 struct UDP_Sock_Info udp_sockv6;
450 * ID of select gnunet-nat-server stdout read task
452 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
455 * Is this transport configured to be behind a NAT?
460 * Is this transport configured to allow connections to NAT'd peers?
465 * Should this transport advertise only NAT addresses (port set to 0)?
466 * If not, all addresses will be duplicated for NAT punching and regular
469 int only_nat_addresses;
472 * The process id of the server process (if behind NAT)
480 * Forward declaration.
483 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result);
487 * Disconnect from a remote node. Clean up session if we have one for this peer
489 * @param cls closure for this call (should be handle to Plugin)
490 * @param target the peeridentity of the peer to disconnect
491 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
494 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
496 /** TODO: Implement! */
501 * Shutdown the server process (stop receiving inbound traffic). Maybe
504 * @param cls Handle to the plugin for this transport
506 * @return returns the number of sockets successfully closed,
507 * should equal the number of sockets successfully opened
510 udp_transport_server_stop (void *cls)
512 struct Plugin *plugin = cls;
514 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
516 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
517 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
519 if (plugin->udp_sockv4.desc != NULL)
521 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->udp_sockv4.desc));
522 plugin->udp_sockv4.desc = NULL;
524 if (plugin->udp_sockv6.desc != NULL)
526 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->udp_sockv6.desc));
527 plugin->udp_sockv6.desc = NULL;
529 if (plugin->behind_nat == GNUNET_YES)
531 if (0 != PLIBC_KILL (plugin->server_pid, SIGTERM))
532 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
533 GNUNET_OS_process_wait (plugin->server_pid);
540 find_session (struct Plugin *plugin,
541 const struct GNUNET_PeerIdentity *peer)
543 struct PeerSession *pos;
545 pos = plugin->sessions;
548 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
558 * Actually send out the message, assume we've got the address and
559 * send_handle squared away!
562 * @param send_handle which handle to send message on
563 * @param target who should receive this message (ignored by UDP)
564 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
565 * @param msgbuf_size the size of the msgbuf to send
566 * @param priority how important is the message (ignored by UDP)
567 * @param timeout when should we time out (give up) if we can not transmit?
568 * @param addr the addr to send the message to, needs to be a sockaddr for us
569 * @param addrlen the len of addr
570 * @param cont continuation to call once the message has
571 * been transmitted (or if the transport is ready
572 * for the next transmission call; or if the
573 * peer disconnected...)
574 * @param cont_cls closure for cont
575 * @return the number of bytes written
578 udp_real_send (void *cls,
579 struct GNUNET_NETWORK_Handle *send_handle,
580 const struct GNUNET_PeerIdentity *target,
583 unsigned int priority,
584 struct GNUNET_TIME_Relative timeout,
587 GNUNET_TRANSPORT_TransmitContinuation cont,
590 struct Plugin *plugin = cls;
591 struct UDPMessage *message;
594 struct sockaddr_in a4;
595 struct sockaddr_in6 a6;
596 const struct IPv4UdpAddress *t4;
597 const struct IPv6UdpAddress *t6;
601 if (send_handle == NULL)
603 /* failed to open send socket for AF */
605 cont (cont_cls, target, GNUNET_SYSERR);
608 if ((addr == NULL) || (addrlen == 0))
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "udp_real_send called without address, returning!\n");
615 cont (cont_cls, target, GNUNET_SYSERR);
616 return 0; /* Can never send if we don't have an address!! */
619 /* Build the message to be sent */
620 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
621 ssize = sizeof (struct UDPMessage) + msgbuf_size;
623 message->header.size = htons (ssize);
624 message->header.type = htons (0);
625 memcpy (&message->sender, plugin->env->my_identity,
626 sizeof (struct GNUNET_PeerIdentity));
627 memcpy (&message[1], msgbuf, msgbuf_size);
629 if (addrlen == sizeof (struct IPv6UdpAddress))
632 memset (&a6, 0, sizeof (a6));
633 #if HAVE_SOCKADDR_IN_SIN_LEN
634 a6.sin6_len = sizeof (a6);
636 a6.sin6_family = AF_INET6;
637 a6.sin6_port = t6->u6_port;
638 memcpy (&a6.sin6_addr,
640 sizeof (struct in6_addr));
644 else if (addrlen == sizeof (struct IPv4UdpAddress))
647 memset (&a4, 0, sizeof (a4));
648 #if HAVE_SOCKADDR_IN_SIN_LEN
649 a4.sin_len = sizeof (a4);
651 a4.sin_family = AF_INET;
652 a4.sin_port = t4->u_port;
653 a4.sin_addr.s_addr = t4->ipv4_addr;
660 GNUNET_free (message);
664 /* Actually send the message */
666 GNUNET_NETWORK_socket_sendto (send_handle, message, ssize,
669 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
670 "UDP transmit %u-byte message to %s (%d: %s)\n",
671 (unsigned int) ssize,
672 GNUNET_a2s (sb, sbs),
674 (sent < 0) ? STRERROR (errno) : "ok");
677 if (sent == GNUNET_SYSERR)
678 cont (cont_cls, target, GNUNET_SYSERR);
681 cont (cont_cls, target, GNUNET_OK);
685 GNUNET_free (message);
690 * We learned about a peer (possibly behind NAT) so run the
691 * gnunet-nat-client to send dummy ICMP responses
693 * @param plugin the plugin for this transport
694 * @param addr the address of the peer
695 * @param addrlen the length of the address
698 run_gnunet_nat_client (struct Plugin *plugin, const char *addr, size_t addrlen)
700 char addr_buf[INET_ADDRSTRLEN];
701 char *address_as_string;
702 char *port_as_string;
704 const struct IPv4UdpAddress *t4;
706 GNUNET_assert(addrlen == sizeof(struct IPv4UdpAddress));
707 t4 = (struct IPv4UdpAddress *)addr;
709 if (NULL == inet_ntop (AF_INET,
711 addr_buf, INET_ADDRSTRLEN))
713 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
716 address_as_string = GNUNET_strdup (addr_buf);
717 GNUNET_asprintf(&port_as_string, "%d", plugin->port);
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 _("Running gnunet-nat-client with arguments: %s %s %d\n"), plugin->external_address, address_as_string, plugin->port);
723 /* Start the server process */
724 pid = GNUNET_OS_start_process(NULL, NULL, "gnunet-nat-client", "gnunet-nat-client", plugin->external_address, address_as_string, port_as_string, NULL);
725 GNUNET_free(address_as_string);
726 GNUNET_free(port_as_string);
727 GNUNET_OS_process_wait (pid);
731 * Function that can be used by the transport service to transmit
732 * a message using the plugin.
735 * @param target who should receive this message (ignored by UDP)
736 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
737 * @param msgbuf_size the size of the msgbuf to send
738 * @param priority how important is the message (ignored by UDP)
739 * @param timeout when should we time out (give up) if we can not transmit?
740 * @param session identifier used for this session (can be NULL)
741 * @param addr the addr to send the message to, needs to be a sockaddr for us
742 * @param addrlen the len of addr
743 * @param force_address not used, we had better have an address to send to
744 * because we are stateless!!
745 * @param cont continuation to call once the message has
746 * been transmitted (or if the transport is ready
747 * for the next transmission call; or if the
748 * peer disconnected...)
749 * @param cont_cls closure for cont
751 * @return the number of bytes written (may return 0 and the message can
752 * still be transmitted later!)
755 udp_plugin_send (void *cls,
756 const struct GNUNET_PeerIdentity *target,
759 unsigned int priority,
760 struct GNUNET_TIME_Relative timeout,
761 struct Session *session,
765 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
767 struct Plugin *plugin = cls;
769 struct MessageQueue *temp_message;
770 struct PeerSession *peer_session;
772 const struct IPv4UdpAddress *t4;
774 if (force_address == GNUNET_SYSERR)
775 return GNUNET_SYSERR;
776 GNUNET_assert (NULL == session);
778 other_peer_natd = GNUNET_NO;
779 if (addrlen == sizeof(struct IPv4UdpAddress))
782 if (ntohs(t4->u_port) == 0)
783 other_peer_natd = GNUNET_YES;
785 else if (addrlen != sizeof(struct IPv6UdpAddress))
788 return -1; /* Must have an address to send to */
792 if ((other_peer_natd == GNUNET_YES) && (plugin->allow_nat == GNUNET_YES))
794 peer_session = find_session(plugin, target);
795 if (peer_session == NULL) /* We have a new peer to add */
798 * The first time, we can assume we have no knowledge of a
799 * working port for this peer, call the ICMP/UDP message sender
802 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
803 peer_session->connect_addr = GNUNET_malloc(addrlen);
804 memcpy(peer_session->connect_addr, addr, addrlen);
805 peer_session->connect_alen = addrlen;
806 peer_session->plugin = plugin;
807 peer_session->sock = NULL;
808 memcpy(&peer_session->target, target, sizeof(struct GNUNET_PeerIdentity));
809 peer_session->expecting_welcome = GNUNET_YES;
811 peer_session->next = plugin->sessions;
812 plugin->sessions = peer_session;
814 peer_session->messages = GNUNET_malloc(sizeof(struct MessageQueue));
815 peer_session->messages->msgbuf = GNUNET_malloc(msgbuf_size);
816 memcpy(peer_session->messages->msgbuf, msgbuf, msgbuf_size);
817 peer_session->messages->msgbuf_size = msgbuf_size;
818 peer_session->messages->timeout = GNUNET_TIME_relative_to_absolute(timeout);
819 peer_session->messages->cont = cont;
820 peer_session->messages->cont_cls = cont_cls;
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
823 _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target));
825 run_gnunet_nat_client(plugin, addr, addrlen);
829 if (peer_session->expecting_welcome == GNUNET_NO) /* We are "connected" */
831 sent = udp_real_send(cls,
836 peer_session->connect_addr, peer_session->connect_alen,
839 else /* Haven't gotten a response from this peer, queue message */
841 temp_message = GNUNET_malloc(sizeof(struct MessageQueue));
842 temp_message->msgbuf = GNUNET_malloc(msgbuf_size);
843 memcpy(temp_message->msgbuf, msgbuf, msgbuf_size);
844 temp_message->msgbuf_size = msgbuf_size;
845 temp_message->timeout = GNUNET_TIME_relative_to_absolute(timeout);
846 temp_message->cont = cont;
847 temp_message->cont_cls = cont_cls;
848 temp_message->next = peer_session->messages;
849 peer_session->messages = temp_message;
853 else if (other_peer_natd == GNUNET_NO) /* Other peer not behind a NAT, so we can just send the message as is */
855 sent = udp_real_send(cls,
856 (addrlen == sizeof (struct IPv4UdpAddress)) ? plugin->udp_sockv4.desc : plugin->udp_sockv6.desc,
859 priority, timeout, addr, addrlen,
862 else /* Other peer is NAT'd, but we don't want to play with them (or can't!) */
864 return GNUNET_SYSERR;
867 /* When GNUNET_SYSERR is returned from udp_real_send, we will still call
868 * the callback so must not return GNUNET_SYSERR!
869 * If we did, then transport context would get freed twice. */
870 if (sent == GNUNET_SYSERR)
877 add_to_address_list (struct Plugin *plugin,
881 struct LocalAddrList *lal;
883 lal = plugin->lal_head;
886 if ( (lal->size == arg_size) &&
887 (0 == memcmp (&lal[1], arg, arg_size)) )
891 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
892 lal->size = arg_size;
893 memcpy (&lal[1], arg, arg_size);
894 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
901 check_local_addr (struct Plugin *plugin,
905 struct LocalAddrList *lal;
907 lal = plugin->lal_head;
910 if ( (lal->size == arg_size) &&
911 (0 == memcmp (&lal[1], arg, arg_size)) )
915 return GNUNET_SYSERR;
920 * Add the IP of our network interface to the list of
921 * our external IP addresses.
924 process_interfaces (void *cls,
927 const struct sockaddr *addr, socklen_t addrlen)
929 struct Plugin *plugin = cls;
931 struct IPv4UdpAddress t4;
932 struct IPv6UdpAddress t6;
936 char buf[INET6_ADDRSTRLEN];
939 af = addr->sa_family;
941 memset(buf, 0, INET6_ADDRSTRLEN);
944 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
945 GNUNET_assert(NULL != inet_ntop(AF_INET, &t4.ipv4_addr, &buf[0], INET_ADDRSTRLEN));
946 if ((plugin->bind6_address != NULL) || ((plugin->bind_address != NULL) && (0 != strcmp(buf, plugin->bind_address))))
948 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Not notifying transport of address %s\n", "UDP", GNUNET_a2s (addr, addrlen));
951 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (uint32_t));
952 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
954 t4.u_port = htons (DEFAULT_NAT_PORT);
956 else if (plugin->behind_nat == GNUNET_YES) /* We are behind NAT, but will advertise NAT and normal addresses */
958 addr_nat = GNUNET_malloc(sizeof(t4));
959 t4.u_port = htons (DEFAULT_NAT_PORT);
960 memcpy(addr_nat, &t4, sizeof(t4));
961 t4.u_port = plugin->port;
965 t4.u_port = htons(plugin->port);
970 else if (af == AF_INET6)
972 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
974 /* skip link local addresses */
977 memcpy (&t6.ipv6_addr,
978 &((struct sockaddr_in6 *) addr)->sin6_addr,
979 sizeof (struct in6_addr));
980 GNUNET_assert(NULL != inet_ntop(AF_INET6, &t6.ipv6_addr, &buf[0], INET6_ADDRSTRLEN));
981 if (((plugin->bind_address != NULL) && (0 != strcmp(buf, plugin->bind_address)))
982 || ((plugin->bind6_address != NULL) && (0 != strcmp(buf, plugin->bind6_address))))
984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Not notifying transport of address %s\n", "UDP", GNUNET_a2s (addr, addrlen));
987 add_to_address_list (plugin, &t6.ipv6_addr, sizeof (struct in6_addr));
988 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
990 t6.u6_port = htons (DEFAULT_NAT_PORT);
992 else if (plugin->behind_nat == GNUNET_YES)
994 addr_nat = GNUNET_malloc(sizeof(t6));
995 t6.u6_port = htons (DEFAULT_NAT_PORT);
996 memcpy(addr_nat, &t6, sizeof(t6));
997 t6.u6_port = plugin->port;
1001 t6.u6_port = htons (plugin->port);
1013 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1014 GNUNET_ERROR_TYPE_BULK,
1015 _("Found address `%s' (%s)\n"),
1016 GNUNET_a2s (addr, addrlen), name);
1018 if (addr_nat != NULL)
1020 plugin->env->notify_address (plugin->env->cls,
1022 addr_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
1023 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1024 GNUNET_ERROR_TYPE_BULK,
1025 _("Found NAT address `%s' (%s)\n"),
1026 GNUNET_a2s (addr_nat, args), name);
1027 GNUNET_free(addr_nat);
1030 plugin->env->notify_address (plugin->env->cls,
1032 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
1038 * Function called by the resolver for each address obtained from DNS
1039 * for our own hostname. Add the addresses to the list of our
1040 * external IP addresses.
1042 * @param cls closure
1043 * @param addr one of the addresses of the host, NULL for the last address
1044 * @param addrlen length of the address
1047 process_hostname_ips (void *cls,
1048 const struct sockaddr *addr, socklen_t addrlen)
1050 struct Plugin *plugin = cls;
1054 plugin->hostname_dns = NULL;
1057 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1062 * Send UDP probe messages or UDP keepalive messages, depending on the
1063 * state of the connection.
1065 * @param cls closure for this call (should be the main Plugin)
1066 * @param tc task context for running this
1069 send_udp_probe_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1071 struct UDP_NAT_Probes *probe = cls;
1072 struct UDP_NAT_ProbeMessage message;
1073 struct Plugin *plugin = probe->plugin;
1075 memset (&message, 0, sizeof (message));
1076 message.header.size = htons(sizeof(struct UDP_NAT_ProbeMessage));
1077 message.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE);
1078 /* If they gave us a port, use that. If not, try our port. */
1079 if (ntohs(probe->addr.u_port) == 0)
1080 probe->addr.u_port = htons(plugin->port);
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 _("Sending a probe to port %d\n"), ntohs(probe->addr.u_port));
1087 udp_real_send(plugin,
1088 plugin->udp_sockv4.desc,
1090 (char *)&message, ntohs(message.header.size), 0,
1091 GNUNET_TIME_relative_get_unit(),
1092 &probe->addr, sizeof(struct IPv4UdpAddress),
1093 &udp_probe_continuation, probe);
1098 * Continuation for probe sends. If the last probe was sent
1099 * "successfully", schedule sending of another one. If not,
1103 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result)
1105 struct UDP_NAT_Probes *probe = cls;
1106 struct Plugin *plugin = probe->plugin;
1108 if ((result == GNUNET_OK) && (probe->count < MAX_PROBES))
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 _("Scheduling next probe for 10000 milliseconds\n"));
1114 probe->task = GNUNET_SCHEDULER_add_delayed(plugin->env->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10000), &send_udp_probe_message, probe);
1116 else /* Destroy the probe context. */
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 _("Sending probe didn't go well...\n"));
1126 * Find probe message by address
1128 * @param plugin the plugin for this transport
1129 * @param address_string the ip address as a string
1131 struct UDP_NAT_Probes *
1132 find_probe(struct Plugin *plugin, char * address_string)
1134 struct UDP_NAT_Probes *pos;
1136 pos = plugin->probes;
1138 if (strcmp(pos->address_string, address_string) == 0)
1146 * @param cls the plugin handle
1147 * @param tc the scheduling context (for rescheduling this function again)
1149 * We have been notified that gnunet-nat-server has written something to stdout.
1150 * Handle the output, then reschedule this function to be called again once
1151 * more is available.
1155 udp_plugin_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1157 struct Plugin *plugin = cls;
1160 memset(&mybuf, 0, sizeof(mybuf));
1162 struct UDP_NAT_Probes *temp_probe;
1165 struct IPv4UdpAddress a4;
1167 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1170 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, &mybuf, sizeof(mybuf));
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 _("Finished reading from server stdout with code: %d\n"), bytes);
1182 for (i = 0; i < sizeof(mybuf); i++)
1184 if (mybuf[i] == '\n')
1187 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
1190 port_start = &mybuf[i + 1];
1194 if (port_start != NULL)
1195 port = atoi(port_start);
1198 plugin->server_read_task =
1199 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1200 GNUNET_TIME_UNIT_FOREVER_REL,
1201 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 _("nat-server-read read: %s port %d\n"), &mybuf, port);
1211 * We have received an ICMP response, ostensibly from a non-NAT'd peer
1212 * that wants to connect to us! Send a message to establish a connection.
1214 if (inet_pton(AF_INET, &mybuf[0], &a4.ipv4_addr) != 1)
1217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1218 _("nat-server-read malformed address\n"), &mybuf, port);
1220 plugin->server_read_task =
1221 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1222 GNUNET_TIME_UNIT_FOREVER_REL,
1223 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1227 temp_probe = find_probe(plugin, &mybuf[0]);
1229 if (temp_probe == NULL)
1231 temp_probe = GNUNET_malloc(sizeof(struct UDP_NAT_Probes));
1232 temp_probe->address_string = strdup(&mybuf[0]);
1233 GNUNET_assert (1 == inet_pton(AF_INET, &mybuf[0], &temp_probe->addr.ipv4_addr));
1234 temp_probe->addr.u_port = htons(port);
1235 temp_probe->next = plugin->probes;
1236 temp_probe->plugin = plugin;
1237 temp_probe->task = GNUNET_SCHEDULER_add_delayed(plugin->env->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500), &send_udp_probe_message, temp_probe);
1238 plugin->probes = temp_probe;
1241 plugin->server_read_task =
1242 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1243 GNUNET_TIME_UNIT_FOREVER_REL,
1244 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1250 * Demultiplexer for UDP NAT messages
1252 * @param plugin the main plugin for this transport
1253 * @param sender from which peer the message was received
1254 * @param currhdr pointer to the header of the message
1255 * @param sender_addr the address from which the message was received
1256 * @param fromlen the length of the address
1257 * @param sockinfo which socket did we receive the message on
1260 udp_demultiplexer(struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
1261 const struct GNUNET_MessageHeader *currhdr,
1262 const void *sender_addr,
1263 size_t fromlen, struct UDP_Sock_Info *sockinfo)
1265 struct UDP_NAT_ProbeMessageReply *outgoing_probe_reply;
1266 struct UDP_NAT_ProbeMessageConfirmation *outgoing_probe_confirmation;
1268 char addr_buf[INET_ADDRSTRLEN];
1269 struct UDP_NAT_Probes *outgoing_probe;
1270 struct PeerSession *peer_session;
1271 struct MessageQueue *pending_message;
1272 struct MessageQueue *pending_message_temp;
1273 uint16_t incoming_port;
1275 if (memcmp(sender, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1278 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1279 _("Received a message from myself, dropping!!!\n"));
1285 GNUNET_assert(sender_addr != NULL); /* Can recvfrom have a NULL address? */
1286 if (fromlen == sizeof(struct IPv4UdpAddress))
1288 incoming_port = ntohs(((struct IPv4UdpAddress *)sender_addr)->u_port);
1290 else if (fromlen == sizeof(struct IPv6UdpAddress))
1292 incoming_port = ntohs(((struct IPv6UdpAddress *)sender_addr)->u6_port);
1295 switch (ntohs(currhdr->type))
1297 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE:
1298 /* Send probe reply */
1299 outgoing_probe_reply = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageReply));
1300 outgoing_probe_reply->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageReply));
1301 outgoing_probe_reply->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY);
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 _("Received a probe on listen port %d, sent_from port %d\n"),
1306 sockinfo->port, incoming_port);
1309 udp_real_send(plugin, sockinfo->desc, NULL,
1310 (char *)outgoing_probe_reply,
1311 ntohs(outgoing_probe_reply->header.size), 0,
1312 GNUNET_TIME_relative_get_unit(),
1313 sender_addr, fromlen,
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318 _("Sent PROBE REPLY to port %d on outgoing port %d\n"),
1319 incoming_port, sockinfo->port);
1321 GNUNET_free(outgoing_probe_reply);
1323 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY:
1324 /* Check for existing probe, check ports returned, send confirmation if all is well */
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 _("Received PROBE REPLY from port %d on incoming port %d\n"), incoming_port, sockinfo->port);
1329 if (fromlen == sizeof(struct IPv4UdpAddress))
1331 memset(&addr_buf, 0, sizeof(addr_buf));
1332 if (NULL == inet_ntop (AF_INET,
1333 &((struct IPv4UdpAddress *) sender_addr)->ipv4_addr, addr_buf,
1336 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1339 outgoing_probe = find_probe(plugin, &addr_buf[0]);
1340 if (outgoing_probe != NULL)
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1344 _("Sending confirmation that we were reached!\n"));
1346 outgoing_probe_confirmation = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1347 outgoing_probe_confirmation->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1348 outgoing_probe_confirmation->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM);
1349 udp_real_send(plugin, sockinfo->desc, NULL,
1350 (char *)outgoing_probe_confirmation,
1351 ntohs(outgoing_probe_confirmation->header.size), 0,
1352 GNUNET_TIME_relative_get_unit(),
1353 sender_addr, fromlen, NULL, NULL);
1355 if (outgoing_probe->task != GNUNET_SCHEDULER_NO_TASK)
1357 GNUNET_SCHEDULER_cancel(plugin->env->sched, outgoing_probe->task);
1358 outgoing_probe->task = GNUNET_SCHEDULER_NO_TASK;
1359 /* Schedule task to timeout and remove probe if confirmation not received */
1361 GNUNET_free(outgoing_probe_confirmation);
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 _("Received a probe reply, but have no record of a sent probe!\n"));
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 _("Received a probe reply, but sender address size is WRONG (should be %d, is %d)!\n"), sizeof(struct IPv4UdpAddress), fromlen);
1379 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM:
1380 peer_session = find_session(plugin, sender);
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 _("Looking up peer session for peer %s\n"), GNUNET_i2s(sender));
1385 if (peer_session == NULL) /* Shouldn't this NOT happen? */
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 _("Peer not in list, adding (THIS MAY BE A MISTAKE) %s\n"), GNUNET_i2s(sender));
1391 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
1392 peer_session->connect_addr = GNUNET_malloc(fromlen);
1393 memcpy(peer_session->connect_addr, sender_addr, fromlen);
1394 peer_session->connect_alen = fromlen;
1395 peer_session->plugin = plugin;
1396 peer_session->sock = sockinfo->desc;
1397 memcpy(&peer_session->target, sender, sizeof(struct GNUNET_PeerIdentity));
1398 peer_session->expecting_welcome = GNUNET_NO;
1400 peer_session->next = plugin->sessions;
1401 plugin->sessions = peer_session;
1403 peer_session->messages = NULL;
1405 else if (peer_session->expecting_welcome == GNUNET_YES)
1407 peer_session->expecting_welcome = GNUNET_NO;
1408 peer_session->sock = sockinfo->desc;
1409 if (peer_session->connect_alen == sizeof(struct IPv4UdpAddress))
1411 ((struct IPv4UdpAddress *)peer_session->connect_addr)->u_port = htons(incoming_port);
1413 else if (peer_session->connect_alen == sizeof(struct IPv4UdpAddress))
1415 ((struct IPv6UdpAddress *)peer_session->connect_addr)->u6_port = htons(incoming_port);
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 _("Received a probe confirmation, will send to peer on port %d\n"), incoming_port);
1422 if (peer_session->messages != NULL)
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1426 _("Received a probe confirmation, sending queued messages.\n"));
1428 pending_message = peer_session->messages;
1430 while (pending_message != NULL)
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1434 _("sending queued message %d\n"), count);
1436 udp_real_send(plugin,
1438 &peer_session->target,
1439 pending_message->msgbuf,
1440 pending_message->msgbuf_size, 0,
1441 GNUNET_TIME_relative_get_unit(),
1442 peer_session->connect_addr,
1443 peer_session->connect_alen,
1444 pending_message->cont,
1445 pending_message->cont_cls);
1447 pending_message_temp = pending_message;
1448 pending_message = pending_message->next;
1449 GNUNET_free(pending_message_temp->msgbuf);
1450 GNUNET_free(pending_message_temp);
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1453 _("finished sending queued message %d\n"), count);
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 _("Received probe confirmation for already confirmed peer!\n"));
1467 /* Received confirmation, add peer with address/port specified */
1469 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_KEEPALIVE:
1470 /* Once we've sent NAT_PROBE_CONFIRM change to sending keepalives */
1471 /* If we receive these just ignore! */
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "Sending message type %d to transport!\n",
1477 ntohs(currhdr->type));
1479 plugin->env->receive (plugin->env->cls, sender, currhdr, UDP_DIRECT_DISTANCE,
1480 NULL, sender_addr, fromlen);
1487 * @param cls the plugin handle
1488 * @param tc the scheduling context (for rescheduling this function again)
1490 * We have been notified that our writeset has something to read. We don't
1491 * know which socket needs to be read, so we have to check each one
1492 * Then reschedule this function to be called again once more is available.
1496 udp_plugin_select (void *cls,
1497 const struct GNUNET_SCHEDULER_TaskContext *tc)
1499 struct Plugin *plugin = cls;
1501 struct UDPMessage *msg;
1502 struct GNUNET_PeerIdentity sender;
1510 const struct GNUNET_MessageHeader *currhdr;
1511 struct IPv4UdpAddress t4;
1512 struct IPv6UdpAddress t6;
1513 const struct sockaddr_in *s4;
1514 const struct sockaddr_in6 *s6;
1517 struct UDP_Sock_Info *udp_sock;
1519 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1520 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1523 if (GNUNET_NETWORK_fdset_isset (tc->read_ready,
1524 plugin->udp_sockv4.desc))
1525 udp_sock = &plugin->udp_sockv4;
1526 else if (GNUNET_NETWORK_fdset_isset (tc->read_ready,
1527 plugin->udp_sockv6.desc))
1528 udp_sock = &plugin->udp_sockv6;
1529 if (NULL == udp_sock)
1534 fromlen = sizeof (addr);
1535 memset (&addr, 0, sizeof(addr));
1537 GNUNET_NETWORK_socket_recvfrom (udp_sock->desc, buf, sizeof (buf),
1538 (struct sockaddr *)&addr, &fromlen);
1540 if (AF_INET == ((struct sockaddr *)addr)->sa_family)
1542 s4 = (const struct sockaddr_in*) &addr;
1543 t4.u_port = s4->sin_port;
1544 t4.ipv4_addr = s4->sin_addr.s_addr;
1546 calen = sizeof (t4);
1548 else if (AF_INET6 == ((struct sockaddr *)addr)->sa_family)
1550 s6 = (const struct sockaddr_in6*) &addr;
1551 t6.u6_port = s6->sin6_port;
1552 memcpy (&t6.ipv6_addr,
1554 sizeof (struct in6_addr));
1556 calen = sizeof (t6);
1564 if (ret < sizeof (struct UDPMessage))
1566 GNUNET_break_op (0);
1567 plugin->select_task =
1568 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1569 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1570 GNUNET_SCHEDULER_NO_TASK,
1571 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1572 NULL, &udp_plugin_select, plugin);
1575 msg = (struct UDPMessage *) buf;
1576 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
1578 GNUNET_break_op (0);
1579 plugin->select_task =
1580 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1581 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1582 GNUNET_SCHEDULER_NO_TASK,
1583 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1584 NULL, &udp_plugin_select, plugin);
1587 msgbuf = (char *)&msg[1];
1588 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1591 tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
1592 while (offset < tsize)
1594 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
1595 udp_demultiplexer(plugin, &sender, currhdr, ca, calen, udp_sock);
1596 offset += ntohs(currhdr->size);
1599 plugin->select_task =
1600 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1601 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1602 GNUNET_SCHEDULER_NO_TASK,
1603 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1604 NULL, &udp_plugin_select, plugin);
1609 * Create a slew of UDP sockets. If possible, use IPv6 and IPv4.
1611 * @param cls closure for server start, should be a struct Plugin *
1612 * @return number of sockets created or GNUNET_SYSERR on error
1615 udp_transport_server_start (void *cls)
1617 struct Plugin *plugin = cls;
1618 struct sockaddr_in serverAddrv4;
1619 struct sockaddr_in6 serverAddrv6;
1620 struct sockaddr *serverAddr;
1622 int sockets_created;
1626 sockets_created = 0;
1627 if (plugin->behind_nat == GNUNET_YES)
1629 /* Pipe to read from started processes stdout (on read end) */
1630 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES);
1631 if (plugin->server_stdout == NULL)
1632 return sockets_created;
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Starting gnunet-nat-server process cmd: %s %s\n",
1636 "gnunet-nat-server",
1637 plugin->internal_address);
1639 /* Start the server process */
1640 plugin->server_pid = GNUNET_OS_start_process(NULL,
1641 plugin->server_stdout,
1642 "gnunet-nat-server",
1643 "gnunet-nat-server",
1644 plugin->internal_address, NULL);
1645 if (plugin->server_pid == GNUNET_SYSERR)
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649 "Failed to start gnunet-nat-server process\n");
1651 return GNUNET_SYSERR;
1653 /* Close the write end of the read pipe */
1654 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1656 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout, GNUNET_DISK_PIPE_END_READ);
1657 plugin->server_read_task =
1658 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1659 GNUNET_TIME_UNIT_FOREVER_REL,
1660 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1664 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "transport-udp",
1667 plugin->udp_sockv6.desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1668 if (NULL == plugin->udp_sockv6.desc)
1670 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
1674 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1675 #if HAVE_SOCKADDR_IN_SIN_LEN
1676 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1679 serverAddrv6.sin6_family = AF_INET6;
1680 serverAddrv6.sin6_addr = in6addr_any;
1681 if (plugin->bind6_address != NULL)
1683 if (1 != inet_pton(AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
1687 serverAddrv6.sin6_port = htons (plugin->port);
1688 addrlen = sizeof (serverAddrv6);
1689 serverAddr = (struct sockaddr *) &serverAddrv6;
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Binding to IPv6 port %d\n",
1693 ntohs(serverAddrv6.sin6_port));
1696 while (GNUNET_NETWORK_socket_bind (plugin->udp_sockv6.desc, serverAddr, addrlen) !=
1699 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 "IPv6 Binding failed, trying new port %d\n",
1703 ntohs(serverAddrv6.sin6_port));
1708 GNUNET_NETWORK_socket_close (plugin->udp_sockv6.desc);
1709 plugin->udp_sockv6.desc = NULL;
1713 if (plugin->udp_sockv6.desc != NULL)
1715 plugin->udp_sockv6.port = ntohs(serverAddrv6.sin6_port);
1721 plugin->udp_sockv4.desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1722 if (NULL == plugin->udp_sockv4.desc)
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "socket");
1728 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1729 #if HAVE_SOCKADDR_IN_SIN_LEN
1730 serverAddrv4.sin_len = sizeof (serverAddrv4);
1732 serverAddrv4.sin_family = AF_INET;
1733 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1734 if (plugin->bind_address != NULL)
1736 if (1 != inet_pton(AF_INET, plugin->bind_address, &serverAddrv4.sin_addr))
1739 serverAddrv4.sin_port = htons (plugin->port);
1740 addrlen = sizeof (serverAddrv4);
1741 serverAddr = (struct sockaddr *) &serverAddrv4;
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1744 "Binding to IPv4 port %d\n",
1745 ntohs(serverAddrv4.sin_port));
1748 while (GNUNET_NETWORK_socket_bind (plugin->udp_sockv4.desc, serverAddr, addrlen) !=
1751 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "IPv4 Binding failed, trying new port %d\n",
1755 ntohs(serverAddrv4.sin_port));
1760 GNUNET_NETWORK_socket_close (plugin->udp_sockv4.desc);
1761 plugin->udp_sockv4.desc = NULL;
1765 if (plugin->udp_sockv4.desc != NULL)
1767 plugin->udp_sockv4.port = ntohs(serverAddrv4.sin_port);
1772 plugin->rs = GNUNET_NETWORK_fdset_create ();
1773 GNUNET_NETWORK_fdset_zero (plugin->rs);
1774 if (NULL != plugin->udp_sockv4.desc)
1775 GNUNET_NETWORK_fdset_set (plugin->rs,
1776 plugin->udp_sockv4.desc);
1777 if (NULL != plugin->udp_sockv6.desc)
1778 GNUNET_NETWORK_fdset_set (plugin->rs,
1779 plugin->udp_sockv6.desc);
1781 plugin->select_task =
1782 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1783 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1784 GNUNET_SCHEDULER_NO_TASK,
1785 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1786 NULL, &udp_plugin_select, plugin);
1787 return sockets_created;
1793 * Check if the given port is plausible (must be either
1794 * our listen port or our advertised port). If it is
1795 * neither, we return GNUNET_SYSERR.
1797 * @param plugin global variables
1798 * @param in_port port number to check
1799 * @return GNUNET_OK if port is either open_port or adv_port
1802 check_port (struct Plugin *plugin, uint16_t in_port)
1804 if ( (plugin->behind_nat == GNUNET_YES) && (in_port == 0) )
1806 if ( (plugin->only_nat_addresses == GNUNET_YES) &&
1807 (plugin->behind_nat == GNUNET_YES) )
1808 return GNUNET_SYSERR; /* odd case... */
1809 if (in_port == plugin->port)
1811 return GNUNET_SYSERR;
1816 * Function that will be called to check if a binary address for this
1817 * plugin is well-formed and corresponds to an address for THIS peer
1818 * (as per our configuration). Naturally, if absolutely necessary,
1819 * plugins can be a bit conservative in their answer, but in general
1820 * plugins should make sure that the address does not redirect
1821 * traffic to a 3rd party that might try to man-in-the-middle our
1824 * @param cls closure, should be our handle to the Plugin
1825 * @param addr pointer to the address
1826 * @param addrlen length of addr
1827 * @return GNUNET_OK if this is a plausible address for this peer
1828 * and transport, GNUNET_SYSERR if not
1832 udp_check_address (void *cls,
1836 struct Plugin *plugin = cls;
1837 char buf[INET6_ADDRSTRLEN];
1843 struct IPv4UdpAddress *v4;
1844 struct IPv6UdpAddress *v6;
1846 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1847 (addrlen != sizeof (struct IPv6UdpAddress)))
1849 GNUNET_break_op (0);
1850 return GNUNET_SYSERR;
1853 if (addrlen == sizeof (struct IPv4UdpAddress))
1855 v4 = (struct IPv4UdpAddress *) addr;
1857 check_port (plugin, ntohs (v4->u_port)))
1858 return GNUNET_SYSERR;
1860 check_local_addr (plugin, &v4->ipv4_addr, sizeof (uint32_t)))
1861 return GNUNET_SYSERR;
1864 port = ntohs (v4->u_port);
1865 memcpy (&a4, &v4->ipv4_addr, sizeof (a4));
1870 v6 = (struct IPv6UdpAddress *) addr;
1871 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1873 GNUNET_break_op (0);
1874 return GNUNET_SYSERR;
1877 check_port (plugin, ntohs (v6->u6_port)))
1878 return GNUNET_SYSERR;
1880 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1881 return GNUNET_SYSERR;
1884 port = ntohs (v6->u6_port);
1885 memcpy (&a6, &v6->ipv6_addr, sizeof (a6));
1889 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1893 "Informing transport service about my address `%s:%u'\n",
1902 * Append our port and forward the result.
1905 append_port (void *cls, const char *hostname)
1907 struct PrettyPrinterContext *ppc = cls;
1910 if (hostname == NULL)
1912 ppc->asc (ppc->asc_cls, NULL);
1916 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1917 ppc->asc (ppc->asc_cls, ret);
1923 * Convert the transports address to a nice, human-readable
1926 * @param cls closure
1927 * @param type name of the transport that generated the address
1928 * @param addr one of the addresses of the host, NULL for the last address
1929 * the specific address format depends on the transport
1930 * @param addrlen length of the address
1931 * @param numeric should (IP) addresses be displayed in numeric form?
1932 * @param timeout after how long should we give up?
1933 * @param asc function to call on each string
1934 * @param asc_cls closure for asc
1937 udp_plugin_address_pretty_printer (void *cls,
1942 struct GNUNET_TIME_Relative timeout,
1943 GNUNET_TRANSPORT_AddressStringCallback asc,
1946 struct Plugin *plugin = cls;
1947 struct PrettyPrinterContext *ppc;
1950 struct sockaddr_in a4;
1951 struct sockaddr_in6 a6;
1952 const struct IPv4UdpAddress *u4;
1953 const struct IPv6UdpAddress *u6;
1956 if (addrlen == sizeof (struct IPv6UdpAddress))
1959 memset (&a6, 0, sizeof (a6));
1960 a6.sin6_family = AF_INET6;
1961 a6.sin6_port = u6->u6_port;
1962 memcpy (&a6.sin6_addr,
1964 sizeof (struct in6_addr));
1965 port = ntohs (u6->u6_port);
1969 else if (addrlen == sizeof (struct IPv4UdpAddress))
1972 memset (&a4, 0, sizeof (a4));
1973 a4.sin_family = AF_INET;
1974 a4.sin_port = u4->u_port;
1975 a4.sin_addr.s_addr = u4->ipv4_addr;
1976 port = ntohs (u4->u_port);
1982 /* invalid address */
1983 GNUNET_break_op (0);
1984 asc (asc_cls, NULL);
1987 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1989 ppc->asc_cls = asc_cls;
1991 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1995 !numeric, timeout, &append_port, ppc);
1999 * Return the actual path to a file found in the current
2000 * PATH environment variable.
2002 * @param binary the name of the file to find
2005 get_path_from_PATH (char *binary)
2013 p = getenv ("PATH");
2016 path = GNUNET_strdup (p); /* because we write on it */
2017 buf = GNUNET_malloc (strlen (path) + 20);
2020 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2023 sprintf (buf, "%s/%s", pos, binary);
2024 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2031 sprintf (buf, "%s/%s", pos, binary);
2032 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2043 * Check whether the suid bit is set on a file.
2044 * Attempts to find the file using the current
2045 * PATH environment variable as a search path.
2047 * @param binary the name of the file to check
2050 check_gnunet_nat_binary(char *binary)
2052 struct stat statbuf;
2060 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2061 p = get_path_from_PATH (binaryexe);
2064 p = get_path_from_PATH (binary);
2068 if (0 != STAT (p, &statbuf))
2071 return GNUNET_SYSERR;
2075 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2076 (statbuf.st_uid == 0) )
2080 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2081 if (INVALID_SOCKET == rawsock)
2083 DWORD err = GetLastError ();
2084 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
2085 return GNUNET_NO; /* not running as administrator */
2087 closesocket (rawsock);
2093 * Function called for a quick conversion of the binary address to
2094 * a numeric address. Note that the caller must not free the
2095 * address and that the next call to this function is allowed
2096 * to override the address again.
2098 * @param cls closure
2099 * @param addr binary address
2100 * @param addrlen length of the address
2101 * @return string representing the same address
2104 udp_address_to_string (void *cls,
2108 static char rbuf[INET6_ADDRSTRLEN + 10];
2109 char buf[INET6_ADDRSTRLEN];
2113 const struct IPv4UdpAddress *t4;
2114 const struct IPv6UdpAddress *t6;
2118 if (addrlen == sizeof (struct IPv6UdpAddress))
2122 port = ntohs (t6->u6_port);
2123 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
2126 else if (addrlen == sizeof (struct IPv4UdpAddress))
2130 port = ntohs (t4->u_port);
2131 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
2136 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
2137 GNUNET_snprintf (rbuf,
2146 * The exported method. Makes the core api available via a global and
2147 * returns the udp transport API.
2150 libgnunet_plugin_transport_udp_init (void *cls)
2152 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2153 unsigned long long mtu;
2154 unsigned long long port;
2155 struct GNUNET_TRANSPORT_PluginFunctions *api;
2156 struct Plugin *plugin;
2157 struct GNUNET_SERVICE_Context *service;
2158 int sockets_created;
2161 int only_nat_addresses;
2162 char *internal_address;
2163 char *external_address;
2164 struct IPv4UdpAddress v4_address;
2166 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
2167 if (service == NULL)
2169 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
2170 ("Failed to start service for `%s' transport plugin.\n"),
2175 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2179 /* We are behind nat (according to the user) */
2180 if (check_gnunet_nat_binary("gnunet-nat-server") == GNUNET_YES)
2181 behind_nat = GNUNET_YES;
2184 behind_nat = GNUNET_NO;
2185 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");
2189 behind_nat = GNUNET_NO; /* We are not behind nat! */
2191 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2195 if (check_gnunet_nat_binary("gnunet-nat-client") == GNUNET_YES)
2196 allow_nat = GNUNET_YES; /* We will try to connect to NAT'd peers */
2199 allow_nat = GNUNET_NO;
2200 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");
2205 allow_nat = GNUNET_NO; /* We don't want to try to help NAT'd peers */
2207 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2209 "ONLY_NAT_ADDRESSES"))
2210 only_nat_addresses = GNUNET_YES; /* We will only report our addresses as NAT'd */
2212 only_nat_addresses = GNUNET_NO; /* We will report our addresses as NAT'd and non-NAT'd */
2214 external_address = NULL;
2215 if (((GNUNET_YES == behind_nat) || (GNUNET_YES == allow_nat)) && (GNUNET_OK !=
2216 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2219 &external_address)))
2221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2222 _("Require EXTERNAL_ADDRESS for service `%s' in configuration (either BEHIND_NAT or ALLOW_NAT set to YES)!\n"),
2224 GNUNET_SERVICE_stop (service);
2228 if ((external_address != NULL) && (inet_pton(AF_INET, external_address, &v4_address.ipv4_addr) != 1))
2230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Malformed EXTERNAL_ADDRESS %s given in configuration!\n", external_address);
2233 internal_address = NULL;
2234 if ((GNUNET_YES == behind_nat) && (GNUNET_OK !=
2235 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2238 &internal_address)))
2240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2241 _("Require INTERNAL_ADDRESS for service `%s' in configuration!\n"),
2243 GNUNET_SERVICE_stop (service);
2244 GNUNET_free_non_null(external_address);
2248 if ((internal_address != NULL) && (inet_pton(AF_INET, internal_address, &v4_address.ipv4_addr) != 1))
2250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Malformed INTERNAL_ADDRESS %s given in configuration!\n", internal_address);
2254 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2258 port = UDP_NAT_DEFAULT_PORT;
2259 else if (port > 65535)
2261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2262 _("Given `%s' option is out of range: %llu > %u\n"),
2266 GNUNET_SERVICE_stop (service);
2267 GNUNET_free_non_null(external_address);
2268 GNUNET_free_non_null(internal_address);
2274 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2275 _("MTU %llu for `%s' is probably too low!\n"), mtu,
2278 plugin = GNUNET_malloc (sizeof (struct Plugin));
2279 plugin->external_address = external_address;
2280 plugin->internal_address = internal_address;
2281 plugin->port = port;
2282 plugin->behind_nat = behind_nat;
2283 plugin->allow_nat = allow_nat;
2284 plugin->only_nat_addresses = only_nat_addresses;
2287 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2290 api->send = &udp_plugin_send;
2291 api->disconnect = &udp_disconnect;
2292 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2293 api->address_to_string = &udp_address_to_string;
2294 api->check_address = &udp_check_address;
2296 plugin->service = service;
2298 GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-udp", "BINDTO", &plugin->bind_address);
2300 GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-udp", "BINDTO6", &plugin->bind6_address);
2302 if (plugin->behind_nat == GNUNET_NO)
2304 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2307 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
2310 HOSTNAME_RESOLVE_TIMEOUT,
2311 &process_hostname_ips,
2314 if ((plugin->behind_nat == GNUNET_YES) && (inet_pton(AF_INET, plugin->external_address, &v4_address.ipv4_addr) == 1))
2316 v4_address.u_port = htons(0);
2317 plugin->env->notify_address (plugin->env->cls,
2319 &v4_address, sizeof(v4_address), GNUNET_TIME_UNIT_FOREVER_REL);
2320 add_to_address_list (plugin, &v4_address.ipv4_addr, sizeof (uint32_t));
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Notifying plugin of address %s:0\n", plugin->external_address);
2323 else if ((plugin->external_address != NULL) && (inet_pton(AF_INET, plugin->external_address, &v4_address.ipv4_addr) == 1))
2325 v4_address.u_port = htons(plugin->port);
2326 plugin->env->notify_address (plugin->env->cls,
2328 &v4_address, sizeof(v4_address), GNUNET_TIME_UNIT_FOREVER_REL);
2329 add_to_address_list (plugin, &v4_address.ipv4_addr, sizeof (uint32_t));
2332 sockets_created = udp_transport_server_start (plugin);
2333 if (sockets_created == 0)
2334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2335 _("Failed to open UDP sockets\n"));
2340 libgnunet_plugin_transport_udp_done (void *cls)
2342 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2343 struct Plugin *plugin = api->cls;
2344 struct LocalAddrList *lal;
2346 udp_transport_server_stop (plugin);
2347 if (NULL != plugin->hostname_dns)
2349 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
2350 plugin->hostname_dns = NULL;
2353 GNUNET_SERVICE_stop (plugin->service);
2355 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2356 while (NULL != (lal = plugin->lal_head))
2358 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
2363 GNUNET_free (plugin);
2368 /* end of plugin_transport_udp.c */