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 * Encapsulation of all of the state of the plugin.
339 struct GNUNET_TRANSPORT_PluginEnvironment *env;
342 * Handle to the network service.
344 struct GNUNET_SERVICE_Context *service;
347 * Session of peers with whom we are currently connected
349 struct PeerSession *sessions;
352 * Handle for request of hostname resolution, non-NULL if pending.
354 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
357 * ID of task used to update our addresses when one expires.
359 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
364 GNUNET_SCHEDULER_TaskIdentifier select_task;
372 * The external address given to us by the user. Must be actual
373 * outside visible address for NAT punching to work.
375 char *external_address;
378 * The internal address given to us by the user (or discovered).
380 char *internal_address;
383 * List of our IP addresses.
385 struct LocalAddrList *lal_head;
388 * Tail of our IP address list.
390 struct LocalAddrList *lal_tail;
395 struct GNUNET_NETWORK_FDSet *rs;
398 * stdout pipe handle for the gnunet-nat-server process
400 struct GNUNET_DISK_PipeHandle *server_stdout;
403 * stdout file handle (for reading) for the gnunet-nat-server process
405 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
408 * ID of select gnunet-nat-server stdout read task
410 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
413 * Is this transport configured to be behind a NAT?
418 * Is this transport configured to allow connections to NAT'd peers?
423 * Should this transport advertise only NAT addresses (port set to 0)?
424 * If not, all addresses will be duplicated for NAT punching and regular
427 int only_nat_addresses;
430 * The process id of the server process (if behind NAT)
437 struct UDP_NAT_Probes *probes;
444 /* The network handle */
445 struct GNUNET_NETWORK_Handle *desc;
447 /* The port we bound to */
451 /* *********** globals ************* */
454 * the socket that we transmit all data with
456 static struct UDP_Sock_Info udp_sock;
460 * Forward declaration.
463 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result);
467 * Disconnect from a remote node. Clean up session if we have one for this peer
469 * @param cls closure for this call (should be handle to Plugin)
470 * @param target the peeridentity of the peer to disconnect
471 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
474 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
476 /** TODO: Implement! */
481 * Shutdown the server process (stop receiving inbound traffic). Maybe
484 * @param cls Handle to the plugin for this transport
486 * @return returns the number of sockets successfully closed,
487 * should equal the number of sockets successfully opened
490 udp_transport_server_stop (void *cls)
492 struct Plugin *plugin = cls;
497 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
499 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
500 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
503 ok = GNUNET_NETWORK_socket_close (udp_sock.desc);
505 udp_sock.desc = NULL;
508 if (plugin->behind_nat == GNUNET_YES)
510 if (0 != PLIBC_KILL (plugin->server_pid, SIGTERM))
512 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
514 GNUNET_OS_process_wait (plugin->server_pid);
517 if (ret != GNUNET_OK)
518 return GNUNET_SYSERR;
524 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
526 struct PeerSession *pos;
528 pos = plugin->sessions;
531 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
541 * Actually send out the message, assume we've got the address and
542 * send_handle squared away!
545 * @param send_handle which handle to send message on
546 * @param target who should receive this message (ignored by UDP)
547 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
548 * @param msgbuf_size the size of the msgbuf to send
549 * @param priority how important is the message (ignored by UDP)
550 * @param timeout when should we time out (give up) if we can not transmit?
551 * @param addr the addr to send the message to, needs to be a sockaddr for us
552 * @param addrlen the len of addr
553 * @param cont continuation to call once the message has
554 * been transmitted (or if the transport is ready
555 * for the next transmission call; or if the
556 * peer disconnected...)
557 * @param cont_cls closure for cont
558 * @return the number of bytes written
561 udp_real_send (void *cls,
562 struct GNUNET_NETWORK_Handle *send_handle,
563 const struct GNUNET_PeerIdentity *target,
566 unsigned int priority,
567 struct GNUNET_TIME_Relative timeout,
570 GNUNET_TRANSPORT_TransmitContinuation cont,
573 struct Plugin *plugin = cls;
574 struct UDPMessage *message;
577 struct sockaddr_in a4;
578 struct sockaddr_in6 a6;
579 const struct IPv4UdpAddress *t4;
580 const struct IPv6UdpAddress *t6;
584 if ((addr == NULL) || (addrlen == 0))
587 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
588 ("udp_real_send called without address, returning!\n"));
591 cont (cont_cls, target, GNUNET_SYSERR);
592 return 0; /* Can never send if we don't have an address!! */
595 /* Build the message to be sent */
596 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
597 ssize = sizeof (struct UDPMessage) + msgbuf_size;
599 message->header.size = htons (ssize);
600 message->header.type = htons (0);
601 memcpy (&message->sender, plugin->env->my_identity,
602 sizeof (struct GNUNET_PeerIdentity));
603 memcpy (&message[1], msgbuf, msgbuf_size);
605 if (addrlen == sizeof (struct IPv6UdpAddress))
608 memset (&a6, 0, sizeof (a6));
609 #if HAVE_SOCKADDR_IN_SIN_LEN
610 a6.sin6_len = sizeof (a6);
612 a6.sin6_family = AF_INET6;
613 a6.sin6_port = t6->u6_port;
614 memcpy (&a6.sin6_addr,
616 sizeof (struct in6_addr));
620 else if (addrlen == sizeof (struct IPv4UdpAddress))
623 memset (&a4, 0, sizeof (a4));
624 #if HAVE_SOCKADDR_IN_SIN_LEN
625 a4.sin_len = sizeof (a4);
627 a4.sin_family = AF_INET;
628 a4.sin_port = t4->u_port;
629 a4.sin_addr.s_addr = t4->ipv4_addr;
636 GNUNET_free (message);
640 /* Actually send the message */
642 GNUNET_NETWORK_socket_sendto (send_handle, message, ssize,
648 if (sent == GNUNET_SYSERR)
649 cont (cont_cls, target, GNUNET_SYSERR);
652 cont (cont_cls, target, GNUNET_OK);
656 GNUNET_free (message);
661 * We learned about a peer (possibly behind NAT) so run the
662 * gnunet-nat-client to send dummy ICMP responses
664 * @param plugin the plugin for this transport
665 * @param addr the address of the peer
666 * @param addrlen the length of the address
669 run_gnunet_nat_client (struct Plugin *plugin, const char *addr, size_t addrlen)
671 char addr_buf[INET_ADDRSTRLEN];
672 char *address_as_string;
673 char *port_as_string;
675 const struct IPv4UdpAddress *t4;
677 GNUNET_assert(addrlen == sizeof(struct IPv4UdpAddress));
678 t4 = (struct IPv4UdpAddress *)addr;
680 if (NULL == inet_ntop (AF_INET,
682 addr_buf, INET_ADDRSTRLEN))
684 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
687 address_as_string = GNUNET_strdup (addr_buf);
688 GNUNET_asprintf(&port_as_string, "%d", plugin->port);
690 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
691 _("Running gnunet-nat-client with arguments: %s %s %d\n"), plugin->external_address, address_as_string, plugin->port);
694 /* Start the server process */
695 pid = GNUNET_OS_start_process(NULL, NULL, "gnunet-nat-client", "gnunet-nat-client", plugin->external_address, address_as_string, port_as_string, NULL);
696 GNUNET_free(address_as_string);
697 GNUNET_free(port_as_string);
698 GNUNET_OS_process_wait (pid);
702 * Function that can be used by the transport service to transmit
703 * a message using the plugin.
706 * @param target who should receive this message (ignored by UDP)
707 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
708 * @param msgbuf_size the size of the msgbuf to send
709 * @param priority how important is the message (ignored by UDP)
710 * @param timeout when should we time out (give up) if we can not transmit?
711 * @param session identifier used for this session (can be NULL)
712 * @param addr the addr to send the message to, needs to be a sockaddr for us
713 * @param addrlen the len of addr
714 * @param force_address not used, we had better have an address to send to
715 * because we are stateless!!
716 * @param cont continuation to call once the message has
717 * been transmitted (or if the transport is ready
718 * for the next transmission call; or if the
719 * peer disconnected...)
720 * @param cont_cls closure for cont
722 * @return the number of bytes written (may return 0 and the message can
723 * still be transmitted later!)
726 udp_plugin_send (void *cls,
727 const struct GNUNET_PeerIdentity *target,
730 unsigned int priority,
731 struct GNUNET_TIME_Relative timeout,
732 struct Session *session,
736 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
738 struct Plugin *plugin = cls;
740 struct MessageQueue *temp_message;
741 struct PeerSession *peer_session;
743 const struct IPv4UdpAddress *t4;
745 GNUNET_assert (NULL == session);
747 other_peer_natd = GNUNET_NO;
748 if (addrlen == sizeof(struct IPv4UdpAddress))
751 if (ntohs(t4->u_port) == 0)
752 other_peer_natd = GNUNET_YES;
754 else if (addrlen != sizeof(struct IPv6UdpAddress))
757 return -1; /* Must have an address to send to */
762 if ((other_peer_natd == GNUNET_YES) && (plugin->allow_nat == GNUNET_YES))
764 peer_session = find_session(plugin, target);
765 if (peer_session == NULL) /* We have a new peer to add */
768 * The first time, we can assume we have no knowledge of a
769 * working port for this peer, call the ICMP/UDP message sender
772 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
773 peer_session->connect_addr = GNUNET_malloc(addrlen);
774 memcpy(peer_session->connect_addr, addr, addrlen);
775 peer_session->connect_alen = addrlen;
776 peer_session->plugin = plugin;
777 peer_session->sock = NULL;
778 memcpy(&peer_session->target, target, sizeof(struct GNUNET_PeerIdentity));
779 peer_session->expecting_welcome = GNUNET_YES;
781 peer_session->next = plugin->sessions;
782 plugin->sessions = peer_session;
784 peer_session->messages = GNUNET_malloc(sizeof(struct MessageQueue));
785 peer_session->messages->msgbuf = GNUNET_malloc(msgbuf_size);
786 memcpy(peer_session->messages->msgbuf, msgbuf, msgbuf_size);
787 peer_session->messages->msgbuf_size = msgbuf_size;
788 peer_session->messages->timeout = GNUNET_TIME_relative_to_absolute(timeout);
789 peer_session->messages->cont = cont;
790 peer_session->messages->cont_cls = cont_cls;
792 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
793 _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target));
795 run_gnunet_nat_client(plugin, addr, addrlen);
799 if (peer_session->expecting_welcome == GNUNET_NO) /* We are "connected" */
801 sent = udp_real_send(cls, peer_session->sock, target, msgbuf, msgbuf_size, priority, timeout, peer_session->connect_addr, peer_session->connect_alen, cont, cont_cls);
803 else /* Haven't gotten a response from this peer, queue message */
805 temp_message = GNUNET_malloc(sizeof(struct MessageQueue));
806 temp_message->msgbuf = GNUNET_malloc(msgbuf_size);
807 memcpy(temp_message->msgbuf, msgbuf, msgbuf_size);
808 temp_message->msgbuf_size = msgbuf_size;
809 temp_message->timeout = GNUNET_TIME_relative_to_absolute(timeout);
810 temp_message->cont = cont;
811 temp_message->cont_cls = cont_cls;
812 temp_message->next = peer_session->messages;
813 peer_session->messages = temp_message;
817 else if (other_peer_natd == GNUNET_NO) /* Other peer not behind a NAT, so we can just send the message as is */
819 sent = udp_real_send(cls, udp_sock.desc, target, msgbuf, msgbuf_size, priority, timeout, addr, addrlen, cont, cont_cls);
821 else /* Other peer is NAT'd, but we don't want to play with them (or can't!) */
822 return GNUNET_SYSERR;
824 /* When GNUNET_SYSERR is returned from udp_real_send, we will still call
825 * the callback so must not return GNUNET_SYSERR!
826 * If we do, then transport context get freed twice. */
827 if (sent == GNUNET_SYSERR)
835 add_to_address_list (struct Plugin *plugin,
839 struct LocalAddrList *lal;
841 lal = plugin->lal_head;
844 if ( (lal->size == arg_size) &&
845 (0 == memcmp (&lal[1], arg, arg_size)) )
849 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
850 lal->size = arg_size;
851 memcpy (&lal[1], arg, arg_size);
852 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
859 check_local_addr (struct Plugin *plugin,
863 struct LocalAddrList *lal;
865 lal = plugin->lal_head;
868 if ( (lal->size == arg_size) &&
869 (0 == memcmp (&lal[1], arg, arg_size)) )
873 return GNUNET_SYSERR;
878 * Add the IP of our network interface to the list of
879 * our external IP addresses.
882 process_interfaces (void *cls,
885 const struct sockaddr *addr, socklen_t addrlen)
887 struct Plugin *plugin = cls;
889 struct IPv4UdpAddress t4;
890 struct IPv6UdpAddress t6;
896 af = addr->sa_family;
899 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
900 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (uint32_t));
901 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
903 t4.u_port = htons (DEFAULT_NAT_PORT);
905 else if (plugin->behind_nat == GNUNET_YES) /* We are behind NAT, but will advertise NAT and normal addresses */
907 addr_nat = GNUNET_malloc(sizeof(t4));
908 memcpy(addr_nat, &t4, sizeof(t4));
909 t4.u_port = plugin->port;
910 ((struct IPv4UdpAddress *)addr_nat)->u_port = htons(DEFAULT_NAT_PORT);
914 t4.u_port = htons(plugin->port);
919 else if (af == AF_INET6)
921 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
923 /* skip link local addresses */
926 memcpy (&t6.ipv6_addr,
927 &((struct sockaddr_in6 *) addr)->sin6_addr,
928 sizeof (struct in6_addr));
929 add_to_address_list (plugin, &t6.ipv6_addr, sizeof (struct in6_addr));
930 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
932 t6.u6_port = htons (0);
934 else if (plugin->behind_nat == GNUNET_YES)
936 addr_nat = GNUNET_malloc(sizeof(t6));
937 memcpy(addr_nat, &t6, sizeof(t6));
938 t6.u6_port = plugin->port;
939 ((struct IPv6UdpAddress *)addr_nat)->u6_port = htons(DEFAULT_NAT_PORT);
943 t6.u6_port = htons (plugin->port);
955 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
956 GNUNET_ERROR_TYPE_BULK,
957 _("Found address `%s' (%s)\n"),
958 GNUNET_a2s (addr, addrlen), name);
960 if (addr_nat != NULL)
962 plugin->env->notify_address (plugin->env->cls,
964 addr_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
965 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
966 GNUNET_ERROR_TYPE_BULK,
967 _("Found NAT address `%s' (%s)\n"),
968 GNUNET_a2s (addr_nat, args), name);
969 GNUNET_free(addr_nat);
972 plugin->env->notify_address (plugin->env->cls,
974 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
980 * Function called by the resolver for each address obtained from DNS
981 * for our own hostname. Add the addresses to the list of our
982 * external IP addresses.
985 * @param addr one of the addresses of the host, NULL for the last address
986 * @param addrlen length of the address
989 process_hostname_ips (void *cls,
990 const struct sockaddr *addr, socklen_t addrlen)
992 struct Plugin *plugin = cls;
996 plugin->hostname_dns = NULL;
999 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1004 * Send UDP probe messages or UDP keepalive messages, depending on the
1005 * state of the connection.
1007 * @param cls closure for this call (should be the main Plugin)
1008 * @param tc task context for running this
1011 send_udp_probe_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1013 struct UDP_NAT_Probes *probe = cls;
1014 struct UDP_NAT_ProbeMessage *message;
1015 struct Plugin *plugin = probe->plugin;
1017 message = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessage));
1018 message->header.size = htons(sizeof(struct UDP_NAT_ProbeMessage));
1019 message->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE);
1020 /* If they gave us a port, use that. If not, try our port. */
1021 if (ntohs(probe->addr.u_port) == 0)
1022 probe->addr.u_port = htons(plugin->port);
1025 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1026 _("Sending a probe to port %d\n"), ntohs(probe->addr.u_port));
1031 udp_real_send(plugin, udp_sock.desc, NULL,
1032 (char *)message, ntohs(message->header.size), 0,
1033 GNUNET_TIME_relative_get_unit(),
1034 &probe->addr, sizeof(probe->addr),
1035 &udp_probe_continuation, probe);
1037 GNUNET_free(message);
1042 * Continuation for probe sends. If the last probe was sent
1043 * "successfully", schedule sending of another one. If not,
1047 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result)
1049 struct UDP_NAT_Probes *probe = cls;
1050 struct Plugin *plugin = probe->plugin;
1052 if ((result == GNUNET_OK) && (probe->count < MAX_PROBES))
1055 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1056 _("Scheduling next probe for 10000 milliseconds\n"));
1058 probe->task = GNUNET_SCHEDULER_add_delayed(plugin->env->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10000), &send_udp_probe_message, probe);
1060 else /* Destroy the probe context. */
1063 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1064 _("Sending probe didn't go well...\n"));
1070 * Find probe message by address
1072 * @param plugin the plugin for this transport
1073 * @param address_string the ip address as a string
1075 struct UDP_NAT_Probes *
1076 find_probe(struct Plugin *plugin, char * address_string)
1078 struct UDP_NAT_Probes *pos;
1080 pos = plugin->probes;
1082 if (strcmp(pos->address_string, address_string) == 0)
1090 * @param cls the plugin handle
1091 * @param tc the scheduling context (for rescheduling this function again)
1093 * We have been notified that gnunet-nat-server has written something to stdout.
1094 * Handle the output, then reschedule this function to be called again once
1095 * more is available.
1099 udp_plugin_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1101 struct Plugin *plugin = cls;
1104 memset(&mybuf, 0, sizeof(mybuf));
1106 struct UDP_NAT_Probes *temp_probe;
1109 struct IPv4UdpAddress a4;
1111 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1114 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, &mybuf, sizeof(mybuf));
1119 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1120 _("Finished reading from server stdout with code: %d\n"), bytes);
1127 for (i = 0; i < sizeof(mybuf); i++)
1129 if (mybuf[i] == '\n')
1132 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
1135 port_start = &mybuf[i + 1];
1139 if (port_start != NULL)
1140 port = atoi(port_start);
1143 plugin->server_read_task =
1144 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1145 GNUNET_TIME_UNIT_FOREVER_REL,
1146 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1151 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1152 _("nat-server-read read: %s port %d\n"), &mybuf, port);
1156 * We have received an ICMP response, ostensibly from a non-NAT'd peer
1157 * that wants to connect to us! Send a message to establish a connection.
1159 if (inet_pton(AF_INET, &mybuf[0], &a4.ipv4_addr) != 1)
1162 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp",
1163 _("nat-server-read malformed address\n"), &mybuf, port);
1165 plugin->server_read_task =
1166 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1167 GNUNET_TIME_UNIT_FOREVER_REL,
1168 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1172 temp_probe = find_probe(plugin, &mybuf[0]);
1174 if (temp_probe == NULL)
1176 temp_probe = GNUNET_malloc(sizeof(struct UDP_NAT_Probes));
1177 temp_probe->address_string = strdup(&mybuf[0]);
1178 GNUNET_assert (1 == inet_pton(AF_INET, &mybuf[0], &temp_probe->addr.ipv4_addr));
1179 temp_probe->addr.u_port = htons(port);
1180 temp_probe->next = plugin->probes;
1181 temp_probe->plugin = plugin;
1182 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);
1183 plugin->probes = temp_probe;
1186 plugin->server_read_task =
1187 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1188 GNUNET_TIME_UNIT_FOREVER_REL,
1189 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1195 * Demultiplexer for UDP NAT messages
1197 * @param plugin the main plugin for this transport
1198 * @param sender from which peer the message was received
1199 * @param currhdr pointer to the header of the message
1200 * @param sender_addr the address from which the message was received
1201 * @param fromlen the length of the address
1202 * @param sockinfo which socket did we receive the message on
1205 udp_demultiplexer(struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
1206 const struct GNUNET_MessageHeader *currhdr,
1207 const void *sender_addr,
1208 size_t fromlen, struct UDP_Sock_Info *sockinfo)
1210 struct UDP_NAT_ProbeMessageReply *outgoing_probe_reply;
1211 struct UDP_NAT_ProbeMessageConfirmation *outgoing_probe_confirmation;
1213 char addr_buf[INET_ADDRSTRLEN];
1214 struct UDP_NAT_Probes *outgoing_probe;
1215 struct PeerSession *peer_session;
1216 struct MessageQueue *pending_message;
1217 struct MessageQueue *pending_message_temp;
1218 uint16_t incoming_port;
1220 if (memcmp(sender, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1223 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1224 _("Received a message from myself, dropping!!!\n"));
1230 GNUNET_assert(sender_addr != NULL); /* Can recvfrom have a NULL address? */
1231 if (fromlen == sizeof(struct IPv4UdpAddress))
1233 incoming_port = ntohs(((struct IPv4UdpAddress *)sender_addr)->u_port);
1235 else if (fromlen == sizeof(struct IPv6UdpAddress))
1237 incoming_port = ntohs(((struct IPv6UdpAddress *)sender_addr)->u6_port);
1240 switch (ntohs(currhdr->type))
1242 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE:
1243 /* Send probe reply */
1244 outgoing_probe_reply = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageReply));
1245 outgoing_probe_reply->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageReply));
1246 outgoing_probe_reply->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY);
1249 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1250 _("Received a probe on listen port %d, sent_from port %d\n"), sockinfo->port, incoming_port);
1253 udp_real_send(plugin, sockinfo->desc, NULL,
1254 (char *)outgoing_probe_reply,
1255 ntohs(outgoing_probe_reply->header.size), 0,
1256 GNUNET_TIME_relative_get_unit(),
1257 sender_addr, fromlen,
1261 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1262 _("Sent PROBE REPLY to port %d on outgoing port %d\n"), incoming_port, sockinfo->port);
1264 GNUNET_free(outgoing_probe_reply);
1266 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY:
1267 /* Check for existing probe, check ports returned, send confirmation if all is well */
1269 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1270 _("Received PROBE REPLY from port %d on incoming port %d\n"), incoming_port, sockinfo->port);
1272 if (sizeof(sender_addr) == sizeof(struct IPv4UdpAddress))
1274 memset(&addr_buf, 0, sizeof(addr_buf));
1275 if (NULL == inet_ntop (AF_INET,
1276 &((struct IPv4UdpAddress *) sender_addr)->ipv4_addr, addr_buf,
1279 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1282 outgoing_probe = find_probe(plugin, &addr_buf[0]);
1283 if (outgoing_probe != NULL)
1286 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1287 _("Sending confirmation that we were reached!\n"));
1289 outgoing_probe_confirmation = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1290 outgoing_probe_confirmation->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1291 outgoing_probe_confirmation->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM);
1293 udp_real_send(plugin, sockinfo->desc, NULL, (char *)outgoing_probe_confirmation, ntohs(outgoing_probe_confirmation->header.size), 0, GNUNET_TIME_relative_get_unit(), sender_addr, fromlen, NULL, NULL);
1295 if (outgoing_probe->task != GNUNET_SCHEDULER_NO_TASK)
1297 GNUNET_SCHEDULER_cancel(plugin->env->sched, outgoing_probe->task);
1298 outgoing_probe->task = GNUNET_SCHEDULER_NO_TASK;
1299 /* Schedule task to timeout and remove probe if confirmation not received */
1301 GNUNET_free(outgoing_probe_confirmation);
1306 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1307 _("Received a probe reply, but have no record of a sent probe!\n"));
1312 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM:
1313 peer_session = find_session(plugin, sender);
1315 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1316 _("Looking up peer session for peer %s\n"), GNUNET_i2s(sender));
1318 if (peer_session == NULL) /* Shouldn't this NOT happen? */
1321 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1322 _("Peer not in list, adding (THIS MAY BE A MISTAKE) %s\n"), GNUNET_i2s(sender));
1324 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
1325 peer_session->connect_addr = GNUNET_malloc(fromlen);
1326 memcpy(peer_session->connect_addr, sender_addr, fromlen);
1327 peer_session->connect_alen = fromlen;
1328 peer_session->plugin = plugin;
1329 peer_session->sock = sockinfo->desc;
1330 memcpy(&peer_session->target, sender, sizeof(struct GNUNET_PeerIdentity));
1331 peer_session->expecting_welcome = GNUNET_NO;
1333 peer_session->next = plugin->sessions;
1334 plugin->sessions = peer_session;
1336 peer_session->messages = NULL;
1338 else if (peer_session->expecting_welcome == GNUNET_YES)
1340 peer_session->expecting_welcome = GNUNET_NO;
1341 peer_session->sock = sockinfo->desc;
1342 if (peer_session->connect_alen == sizeof(struct IPv4UdpAddress))
1344 ((struct IPv4UdpAddress *)peer_session->connect_addr)->u_port = htons(incoming_port);
1346 else if (peer_session->connect_alen == sizeof(struct IPv4UdpAddress))
1348 ((struct IPv6UdpAddress *)peer_session->connect_addr)->u6_port = htons(incoming_port);
1352 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1353 _("Received a probe confirmation, will send to peer on port %d\n"), incoming_port);
1355 if (peer_session->messages != NULL)
1358 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1359 _("Received a probe confirmation, sending queued messages.\n"));
1361 pending_message = peer_session->messages;
1363 while (pending_message != NULL)
1366 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1367 _("sending queued message %d\n"), count);
1369 udp_real_send(plugin,
1371 &peer_session->target,
1372 pending_message->msgbuf,
1373 pending_message->msgbuf_size, 0,
1374 GNUNET_TIME_relative_get_unit(),
1375 peer_session->connect_addr,
1376 peer_session->connect_alen,
1377 pending_message->cont,
1378 pending_message->cont_cls);
1380 pending_message_temp = pending_message;
1381 pending_message = pending_message->next;
1382 GNUNET_free(pending_message_temp->msgbuf);
1383 GNUNET_free(pending_message_temp);
1385 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1386 _("finished sending queued message %d\n"), count);
1396 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1397 _("Received probe confirmation for already confirmed peer!\n"));
1400 /* Received confirmation, add peer with address/port specified */
1402 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_KEEPALIVE:
1403 /* Once we've sent NAT_PROBE_CONFIRM change to sending keepalives */
1404 /* If we receive these just ignore! */
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1409 _("Sending message type %d to transport!\n"), ntohs(currhdr->type));
1411 plugin->env->receive (plugin->env->cls, sender, currhdr, UDP_DIRECT_DISTANCE,
1412 NULL, sender_addr, fromlen);
1419 * @param cls the plugin handle
1420 * @param tc the scheduling context (for rescheduling this function again)
1422 * We have been notified that our writeset has something to read. We don't
1423 * know which socket needs to be read, so we have to check each one
1424 * Then reschedule this function to be called again once more is available.
1428 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1430 struct Plugin *plugin = cls;
1432 struct UDPMessage *msg;
1433 struct GNUNET_PeerIdentity *sender;
1434 unsigned int buflen;
1442 const struct GNUNET_MessageHeader *currhdr;
1443 struct IPv4UdpAddress t4;
1444 struct IPv6UdpAddress t6;
1445 const struct sockaddr_in *s4;
1446 const struct sockaddr_in6 *s6;
1451 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1453 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1459 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock.desc);
1461 if (buflen == GNUNET_NO)
1464 buf = GNUNET_malloc (buflen);
1465 fromlen = sizeof (addr);
1466 memset (&addr, 0, sizeof(addr));
1468 GNUNET_NETWORK_socket_recvfrom (udp_sock.desc, buf, buflen,
1469 (struct sockaddr *)&addr, &fromlen);
1471 if (AF_INET == ((struct sockaddr *)addr)->sa_family)
1473 s4 = (const struct sockaddr_in*) &addr;
1474 t4.u_port = s4->sin_port;
1475 t4.ipv4_addr = s4->sin_addr.s_addr;
1477 calen = sizeof (t4);
1479 else if (AF_INET6 == ((struct sockaddr *)addr)->sa_family)
1481 s6 = (const struct sockaddr_in6*) &addr;
1482 t6.u6_port = s6->sin6_port;
1483 memcpy (&t6.ipv6_addr,
1485 sizeof (struct in6_addr));
1487 calen = sizeof (t6);
1501 msg = (struct UDPMessage *) buf;
1503 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
1509 msgbuf = (char *)&msg[1];
1510 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1511 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1515 tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
1517 while (offset < tsize)
1519 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
1520 udp_demultiplexer(plugin, sender, currhdr, ca, calen, &udp_sock);
1521 offset += ntohs(currhdr->size);
1524 GNUNET_free_non_null (buf);
1525 GNUNET_free_non_null (sender);
1528 plugin->select_task =
1529 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1530 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1531 GNUNET_SCHEDULER_NO_TASK,
1532 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1533 NULL, &udp_plugin_select, plugin);
1538 * Create a slew of UDP sockets. If possible, use IPv6, otherwise
1541 * @param cls closure for server start, should be a struct Plugin *
1543 * @return number of sockets created or GNUNET_SYSERR on error
1546 udp_transport_server_start (void *cls)
1548 struct Plugin *plugin = cls;
1549 struct sockaddr_in serverAddrv4;
1550 struct sockaddr_in6 serverAddrv6;
1551 struct sockaddr *serverAddr;
1553 int sockets_created;
1555 sockets_created = 0;
1557 if (plugin->behind_nat == GNUNET_YES)
1559 /* Pipe to read from started processes stdout (on read end) */
1560 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES);
1561 if (plugin->server_stdout == NULL)
1562 return sockets_created;
1564 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1566 "Starting gnunet-nat-server process cmd: %s %s\n", "gnunet-nat-server", plugin->internal_address);
1568 /* Start the server process */
1569 plugin->server_pid = GNUNET_OS_start_process(NULL, plugin->server_stdout, "gnunet-nat-server", "gnunet-nat-server", plugin->internal_address, NULL);
1570 if (plugin->server_pid == GNUNET_SYSERR)
1573 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1575 "Failed to start gnunet-nat-server process\n");
1577 return GNUNET_SYSERR;
1579 /* Close the write end of the read pipe */
1580 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1582 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout, GNUNET_DISK_PIPE_END_READ);
1583 plugin->server_read_task =
1584 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1585 GNUNET_TIME_UNIT_FOREVER_REL,
1586 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1589 udp_sock.desc = NULL;
1592 udp_sock.desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
1593 if (NULL == udp_sock.desc)
1595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
1596 return sockets_created;
1600 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1601 #if HAVE_SOCKADDR_IN_SIN_LEN
1602 serverAddrv4.sin_len = sizeof (serverAddrv4);
1604 serverAddrv4.sin_family = AF_INET;
1605 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1606 serverAddrv4.sin_port = htons (plugin->port);
1607 addrlen = sizeof (serverAddrv4);
1608 serverAddr = (struct sockaddr *) &serverAddrv4;
1610 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1612 "Binding to port %d\n", ntohs(serverAddrv4.sin_port));
1614 while (GNUNET_NETWORK_socket_bind (udp_sock.desc, serverAddr, addrlen) !=
1617 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1619 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1621 "Binding failed, trying new port %d\n",
1622 ntohs(serverAddrv4.sin_port));
1625 udp_sock.port = ntohs(serverAddrv4.sin_port);
1630 if ((udp_sock.desc == NULL) && (GNUNET_YES !=
1631 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
1634 udp_sock.desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
1635 if (udp_sock.desc != NULL)
1637 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1638 #if HAVE_SOCKADDR_IN_SIN_LEN
1639 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1641 serverAddrv6.sin6_family = AF_INET6;
1642 serverAddrv6.sin6_addr = in6addr_any;
1643 serverAddrv6.sin6_port = htons (plugin->port);
1644 addrlen = sizeof (serverAddrv6);
1645 serverAddr = (struct sockaddr *) &serverAddrv6;
1650 plugin->rs = GNUNET_NETWORK_fdset_create ();
1652 GNUNET_NETWORK_fdset_zero (plugin->rs);
1655 GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock.desc);
1657 plugin->select_task =
1658 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1659 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1660 GNUNET_SCHEDULER_NO_TASK,
1661 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1662 NULL, &udp_plugin_select, plugin);
1664 return sockets_created;
1670 * Check if the given port is plausible (must be either
1671 * our listen port or our advertised port). If it is
1672 * neither, we return GNUNET_SYSERR.
1674 * @param plugin global variables
1675 * @param in_port port number to check
1676 * @return GNUNET_OK if port is either open_port or adv_port
1679 check_port (struct Plugin *plugin, uint16_t in_port)
1681 if ( (plugin->behind_nat == GNUNET_YES) && (in_port == 0) )
1683 if ( (plugin->only_nat_addresses == GNUNET_YES) &&
1684 (plugin->behind_nat == GNUNET_YES) )
1685 return GNUNET_SYSERR; /* odd case... */
1686 if (in_port == plugin->port)
1688 return GNUNET_SYSERR;
1693 * Function that will be called to check if a binary address for this
1694 * plugin is well-formed and corresponds to an address for THIS peer
1695 * (as per our configuration). Naturally, if absolutely necessary,
1696 * plugins can be a bit conservative in their answer, but in general
1697 * plugins should make sure that the address does not redirect
1698 * traffic to a 3rd party that might try to man-in-the-middle our
1701 * @param cls closure, should be our handle to the Plugin
1702 * @param addr pointer to the address
1703 * @param addrlen length of addr
1704 * @return GNUNET_OK if this is a plausible address for this peer
1705 * and transport, GNUNET_SYSERR if not
1709 udp_check_address (void *cls,
1713 struct Plugin *plugin = cls;
1714 char buf[INET6_ADDRSTRLEN];
1720 struct IPv4UdpAddress *v4;
1721 struct IPv6UdpAddress *v6;
1723 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1724 (addrlen != sizeof (struct IPv6UdpAddress)))
1726 GNUNET_break_op (0);
1727 return GNUNET_SYSERR;
1730 if (addrlen == sizeof (struct IPv4UdpAddress))
1732 v4 = (struct IPv4UdpAddress *) addr;
1734 check_port (plugin, ntohs (v4->u_port)))
1735 return GNUNET_SYSERR;
1737 check_local_addr (plugin, &v4->ipv4_addr, sizeof (uint32_t)))
1738 return GNUNET_SYSERR;
1741 port = ntohs (v4->u_port);
1742 memcpy (&a4, &v4->ipv4_addr, sizeof (a4));
1747 v6 = (struct IPv6UdpAddress *) addr;
1748 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1750 GNUNET_break_op (0);
1751 return GNUNET_SYSERR;
1754 check_port (plugin, ntohs (v6->u6_port)))
1755 return GNUNET_SYSERR;
1757 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1758 return GNUNET_SYSERR;
1761 port = ntohs (v6->u6_port);
1762 memcpy (&a6, &v6->ipv6_addr, sizeof (a6));
1766 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1769 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1771 "Informing transport service about my address `%s:%u'\n",
1780 * Append our port and forward the result.
1783 append_port (void *cls, const char *hostname)
1785 struct PrettyPrinterContext *ppc = cls;
1788 if (hostname == NULL)
1790 ppc->asc (ppc->asc_cls, NULL);
1794 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1795 ppc->asc (ppc->asc_cls, ret);
1801 * Convert the transports address to a nice, human-readable
1804 * @param cls closure
1805 * @param type name of the transport that generated the address
1806 * @param addr one of the addresses of the host, NULL for the last address
1807 * the specific address format depends on the transport
1808 * @param addrlen length of the address
1809 * @param numeric should (IP) addresses be displayed in numeric form?
1810 * @param timeout after how long should we give up?
1811 * @param asc function to call on each string
1812 * @param asc_cls closure for asc
1815 udp_plugin_address_pretty_printer (void *cls,
1820 struct GNUNET_TIME_Relative timeout,
1821 GNUNET_TRANSPORT_AddressStringCallback asc,
1824 struct Plugin *plugin = cls;
1825 const struct sockaddr_in *v4;
1826 const struct sockaddr_in6 *v6;
1827 struct PrettyPrinterContext *ppc;
1829 if ((addrlen != sizeof (struct sockaddr_in)) &&
1830 (addrlen != sizeof (struct sockaddr_in6)))
1832 /* invalid address */
1833 GNUNET_break_op (0);
1834 asc (asc_cls, NULL);
1837 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1839 ppc->asc_cls = asc_cls;
1840 if (addrlen == sizeof (struct sockaddr_in))
1842 v4 = (const struct sockaddr_in *) addr;
1843 ppc->port = ntohs (v4->sin_port);
1847 v6 = (const struct sockaddr_in6 *) addr;
1848 ppc->port = ntohs (v6->sin6_port);
1851 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1855 !numeric, timeout, &append_port, ppc);
1859 * Return the actual path to a file found in the current
1860 * PATH environment variable.
1862 * @param binary the name of the file to find
1865 get_path_from_PATH (char *binary)
1873 p = getenv ("PATH");
1876 path = GNUNET_strdup (p); /* because we write on it */
1877 buf = GNUNET_malloc (strlen (path) + 20);
1880 while (NULL != (end = strchr (pos, ':')))
1883 sprintf (buf, "%s/%s", pos, binary);
1884 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
1891 sprintf (buf, "%s/%s", pos, binary);
1892 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
1903 * Check whether the suid bit is set on a file.
1904 * Attempts to find the file using the current
1905 * PATH environment variable as a search path.
1907 * @param binary the name of the file to check
1910 check_gnunet_nat_binary(char *binary)
1912 struct stat statbuf;
1915 p = get_path_from_PATH (binary);
1918 if (0 != STAT (p, &statbuf))
1921 return GNUNET_SYSERR;
1924 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
1925 (statbuf.st_uid == 0) )
1931 * Function called for a quick conversion of the binary address to
1932 * a numeric address. Note that the caller must not free the
1933 * address and that the next call to this function is allowed
1934 * to override the address again.
1936 * @param cls closure
1937 * @param addr binary address
1938 * @param addrlen length of the address
1939 * @return string representing the same address
1942 udp_address_to_string (void *cls,
1946 static char rbuf[INET6_ADDRSTRLEN + 10];
1947 char buf[INET6_ADDRSTRLEN];
1951 const struct IPv4UdpAddress *t4;
1952 const struct IPv6UdpAddress *t6;
1956 if (addrlen == sizeof (struct IPv6UdpAddress))
1960 port = ntohs (t6->u6_port);
1961 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1964 else if (addrlen == sizeof (struct IPv4UdpAddress))
1968 port = ntohs (t4->u_port);
1969 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1974 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1975 GNUNET_snprintf (rbuf,
1984 * The exported method. Makes the core api available via a global and
1985 * returns the udp transport API.
1988 libgnunet_plugin_transport_udp_init (void *cls)
1990 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1991 unsigned long long mtu;
1992 unsigned long long port;
1993 struct GNUNET_TRANSPORT_PluginFunctions *api;
1994 struct Plugin *plugin;
1995 struct GNUNET_SERVICE_Context *service;
1996 int sockets_created;
1999 int only_nat_addresses;
2000 char *internal_address;
2001 char *external_address;
2002 struct IPv4UdpAddress v4_address;
2004 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
2005 if (service == NULL)
2007 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
2008 ("Failed to start service for `%s' transport plugin.\n"),
2013 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2017 /* We are behind nat (according to the user) */
2018 if (check_gnunet_nat_binary("gnunet-nat-server") == GNUNET_YES)
2019 behind_nat = GNUNET_YES;
2022 behind_nat = GNUNET_NO;
2023 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", "Configuration specified you are behind a NAT, but gnunet-nat-server is not installed properly (suid bit not set)!\n");
2027 behind_nat = GNUNET_NO; /* We are not behind nat! */
2029 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2033 if (check_gnunet_nat_binary("gnunet-nat-client") == GNUNET_YES)
2034 allow_nat = GNUNET_YES; /* We will try to connect to NAT'd peers */
2037 allow_nat = GNUNET_NO;
2038 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", "Configuration specified you want to connect to NAT'd peers, but gnunet-nat-client is not installed properly (suid bit not set)!\n");
2043 allow_nat = GNUNET_NO; /* We don't want to try to help NAT'd peers */
2045 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2047 "ONLY_NAT_ADDRESSES"))
2048 only_nat_addresses = GNUNET_YES; /* We will only report our addresses as NAT'd */
2050 only_nat_addresses = GNUNET_NO; /* We will report our addresses as NAT'd and non-NAT'd */
2052 external_address = NULL;
2053 if (((GNUNET_YES == behind_nat) || (GNUNET_YES == allow_nat)) && (GNUNET_OK !=
2054 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2057 &external_address)))
2059 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2062 ("Require EXTERNAL_ADDRESS for service `%s' in configuration (either BEHIND_NAT or ALLOW_NAT set to YES)!\n"),
2064 GNUNET_SERVICE_stop (service);
2068 if ((external_address != NULL) && (inet_pton(AF_INET, external_address, &v4_address.ipv4_addr) != 1))
2070 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed EXTERNAL_ADDRESS %s given in configuration!\n", external_address);
2073 internal_address = NULL;
2074 if ((GNUNET_YES == behind_nat) && (GNUNET_OK !=
2075 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2078 &internal_address)))
2080 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2083 ("Require INTERNAL_ADDRESS for service `%s' in configuration!\n"),
2085 GNUNET_SERVICE_stop (service);
2086 GNUNET_free_non_null(external_address);
2090 if ((internal_address != NULL) && (inet_pton(AF_INET, internal_address, &v4_address.ipv4_addr) != 1))
2092 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed INTERNAL_ADDRESS %s given in configuration!\n", internal_address);
2096 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2100 port = UDP_NAT_DEFAULT_PORT;
2101 else if (port > 65535)
2103 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2105 _("Given `%s' option is out of range: %llu > %u\n"),
2109 GNUNET_SERVICE_stop (service);
2110 GNUNET_free_non_null(external_address);
2111 GNUNET_free_non_null(internal_address);
2117 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2119 _("MTU %llu for `%s' is probably too low!\n"), mtu,
2122 plugin = GNUNET_malloc (sizeof (struct Plugin));
2123 plugin->external_address = external_address;
2124 plugin->internal_address = internal_address;
2125 plugin->port = port;
2126 plugin->behind_nat = behind_nat;
2127 plugin->allow_nat = allow_nat;
2128 plugin->only_nat_addresses = only_nat_addresses;
2131 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2134 api->send = &udp_plugin_send;
2135 api->disconnect = &udp_disconnect;
2136 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2137 api->address_to_string = &udp_address_to_string;
2138 api->check_address = &udp_check_address;
2140 plugin->service = service;
2142 if (plugin->behind_nat == GNUNET_NO)
2144 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2147 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
2150 HOSTNAME_RESOLVE_TIMEOUT,
2151 &process_hostname_ips,
2154 if ((plugin->behind_nat == GNUNET_YES) && (inet_pton(AF_INET, plugin->external_address, &v4_address.ipv4_addr) == 1))
2156 v4_address.u_port = htons(0);
2157 plugin->env->notify_address (plugin->env->cls,
2159 &v4_address, sizeof(v4_address), GNUNET_TIME_UNIT_FOREVER_REL);
2161 else if ((plugin->external_address != NULL) && (inet_pton(AF_INET, plugin->external_address, &v4_address.ipv4_addr) == 1))
2163 v4_address.u_port = htons(plugin->port);
2164 plugin->env->notify_address (plugin->env->cls,
2166 &v4_address, sizeof(v4_address), GNUNET_TIME_UNIT_FOREVER_REL);
2169 sockets_created = udp_transport_server_start (plugin);
2171 GNUNET_assert (sockets_created == 1);
2177 libgnunet_plugin_transport_udp_done (void *cls)
2179 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2180 struct Plugin *plugin = api->cls;
2181 struct LocalAddrList *lal;
2183 udp_transport_server_stop (plugin);
2184 if (NULL != plugin->hostname_dns)
2186 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
2187 plugin->hostname_dns = NULL;
2190 GNUNET_SERVICE_stop (plugin->service);
2192 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2193 while (NULL != (lal = plugin->lal_head))
2195 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
2200 GNUNET_free (plugin);
2205 /* end of plugin_transport_udp.c */