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 2, 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_os_lib.h"
44 #include "gnunet_peerinfo_service.h"
45 #include "gnunet_protocols.h"
46 #include "gnunet_resolver_service.h"
47 #include "gnunet_server_lib.h"
48 #include "gnunet_service_lib.h"
49 #include "gnunet_signatures.h"
50 #include "gnunet_statistics_service.h"
51 #include "gnunet_transport_service.h"
52 #include "plugin_transport.h"
53 #include "transport.h"
55 #define DEBUG_UDP GNUNET_YES
60 * Transport cost to peer, always 1 for UDP (direct connection)
62 #define UDP_DIRECT_DISTANCE 1
64 #define DEFAULT_NAT_PORT 0
67 * How long until we give up on transmitting the welcome message?
69 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
72 * Starting port for listening and sending, eventually a config value
74 #define UDP_NAT_DEFAULT_PORT 22086
77 * UDP Message-Packet header.
84 struct GNUNET_MessageHeader header;
87 * What is the identity of the sender (GNUNET_hash of public key)
89 struct GNUNET_PeerIdentity sender;
94 * Network format for IPv4 addresses.
99 * IPv4 address, in network byte order.
104 * Port number, in network byte order.
111 * Network format for IPv6 addresses.
113 struct IPv6UdpAddress
118 unsigned char ipv6_addr[16];
121 * Port number, in network byte order.
126 /* Forward definition */
129 struct PrettyPrinterContext
131 GNUNET_TRANSPORT_AddressStringCallback asc;
141 struct MessageQueue *next;
144 * Session this message belongs to
146 struct PeerSession *session;
149 * Actual message to be sent
154 * Size of message buffer to be sent
159 * When to discard this message
161 struct GNUNET_TIME_Absolute timeout;
164 * Continuation to call when this message goes out
166 GNUNET_TRANSPORT_TransmitContinuation cont;
169 * closure for continuation
176 * UDP NAT Probe message definition
178 struct UDP_NAT_ProbeMessage
183 struct GNUNET_MessageHeader header;
188 * UDP NAT Probe message reply definition
190 struct UDP_NAT_ProbeMessageReply
195 struct GNUNET_MessageHeader header;
201 * UDP NAT Probe message confirm definition
203 struct UDP_NAT_ProbeMessageConfirmation
208 struct GNUNET_MessageHeader header;
221 * Stored in a linked list.
223 struct PeerSession *next;
226 * Pointer to the global plugin struct.
228 struct Plugin *plugin;
231 * To whom are we talking to (set to our identity
232 * if we are still waiting for the welcome message)
234 struct GNUNET_PeerIdentity target;
237 * Address of the other peer (either based on our 'connect'
238 * call or on our 'accept' call).
243 * Length of connect_addr.
248 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
250 int expecting_welcome;
253 * From which socket do we need to send to this peer?
255 struct GNUNET_NETWORK_Handle *sock;
258 * Queue of messages for this peer, in the case that
259 * we have to await a connection...
261 struct MessageQueue *messages;
265 struct UDP_NAT_Probes
271 struct UDP_NAT_Probes *next;
274 * Address string that the server process returned to us
276 char *address_string;
279 * Timeout for this set of probes
281 struct GNUNET_TIME_Absolute timeout;
284 * Count of how many probes we've attempted
289 * The plugin this probe belongs to
291 struct Plugin *plugin;
294 * The task used to send these probes
296 GNUNET_SCHEDULER_TaskIdentifier task;
299 * Network address (always ipv4)
301 struct sockaddr_in sock_addr;
304 * The port to send this probe to, 0 to choose randomly
312 * Encapsulation of all of the state of the plugin.
319 struct GNUNET_TRANSPORT_PluginEnvironment *env;
322 * Handle to the network service.
324 struct GNUNET_SERVICE_Context *service;
327 * Session of peers with whom we are currently connected
329 struct PeerSession *sessions;
332 * Handle for request of hostname resolution, non-NULL if pending.
334 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
337 * ID of task used to update our addresses when one expires.
339 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
344 GNUNET_SCHEDULER_TaskIdentifier select_task;
352 * The external address given to us by the user. Must be actual
353 * outside visible address for NAT punching to work.
355 char *external_address;
358 * The internal address given to us by the user (or discovered).
360 char *internal_address;
365 struct GNUNET_NETWORK_FDSet *rs;
368 * stdout pipe handle for the gnunet-nat-server process
370 struct GNUNET_DISK_PipeHandle *server_stdout;
373 * stdout file handle (for reading) for the gnunet-nat-server process
375 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
378 * ID of select gnunet-nat-server stdout read task
380 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
383 * Is this transport configured to be behind a NAT?
388 * Is this transport configured to allow connections to NAT'd peers?
393 * Should this transport advertise only NAT addresses (port set to 0)?
394 * If not, all addresses will be duplicated for NAT punching and regular
397 int only_nat_addresses;
400 * The process id of the server process (if behind NAT)
407 struct UDP_NAT_Probes *probes;
414 /* The network handle */
415 struct GNUNET_NETWORK_Handle *desc;
417 /* The port we bound to */
421 /* *********** globals ************* */
424 * the socket that we transmit all data with
426 static struct UDP_Sock_Info udp_sock;
430 * Forward declaration.
433 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result);
437 * Disconnect from a remote node. Clean up session if we have one for this peer
439 * @param cls closure for this call (should be handle to Plugin)
440 * @param target the peeridentity of the peer to disconnect
441 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
444 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
446 /** TODO: Implement! */
451 * Shutdown the server process (stop receiving inbound traffic). Maybe
454 * @param cls Handle to the plugin for this transport
456 * @return returns the number of sockets successfully closed,
457 * should equal the number of sockets successfully opened
460 udp_transport_server_stop (void *cls)
462 struct Plugin *plugin = cls;
467 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
469 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
470 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
473 ok = GNUNET_NETWORK_socket_close (udp_sock.desc);
475 udp_sock.desc = NULL;
478 if (plugin->behind_nat == GNUNET_YES)
480 if (0 != PLIBC_KILL (plugin->server_pid, SIGTERM))
482 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
484 GNUNET_OS_process_wait (plugin->server_pid);
487 if (ret != GNUNET_OK)
488 return GNUNET_SYSERR;
494 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
496 struct PeerSession *pos;
498 pos = plugin->sessions;
501 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
511 * Actually send out the message, assume we've got the address and
512 * send_handle squared away!
515 * @param send_handle which handle to send message on
516 * @param target who should receive this message (ignored by UDP)
517 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
518 * @param msgbuf_size the size of the msgbuf to send
519 * @param priority how important is the message (ignored by UDP)
520 * @param timeout when should we time out (give up) if we can not transmit?
521 * @param addr the addr to send the message to, needs to be a sockaddr for us
522 * @param addrlen the len of addr
523 * @param cont continuation to call once the message has
524 * been transmitted (or if the transport is ready
525 * for the next transmission call; or if the
526 * peer disconnected...)
527 * @param cont_cls closure for cont
528 * @return the number of bytes written
531 udp_real_send (void *cls,
532 struct GNUNET_NETWORK_Handle *send_handle,
533 const struct GNUNET_PeerIdentity *target,
536 unsigned int priority,
537 struct GNUNET_TIME_Relative timeout,
540 GNUNET_TRANSPORT_TransmitContinuation cont,
543 struct Plugin *plugin = cls;
544 struct UDPMessage *message;
547 struct sockaddr_in a4;
548 struct sockaddr_in6 a6;
549 const struct IPv4UdpAddress *t4;
550 const struct IPv6UdpAddress *t6;
554 if ((addr == NULL) || (addrlen == 0))
557 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
558 ("udp_real_send called without address, returning!\n"));
561 cont (cont_cls, target, GNUNET_SYSERR);
562 return 0; /* Can never send if we don't have an address!! */
565 /* Build the message to be sent */
566 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
567 ssize = sizeof (struct UDPMessage) + msgbuf_size;
569 message->header.size = htons (ssize);
570 message->header.type = htons (0);
571 memcpy (&message->sender, plugin->env->my_identity,
572 sizeof (struct GNUNET_PeerIdentity));
573 memcpy (&message[1], msgbuf, msgbuf_size);
575 if (addrlen == sizeof (struct IPv6UdpAddress))
578 memset (&a6, 0, sizeof (a6));
579 #if HAVE_SOCKADDR_IN_SIN_LEN
580 a6.sin6_len = sizeof (a6);
582 a6.sin6_family = AF_INET6;
583 a6.sin6_port = t6->u6_port;
584 memcpy (&a6.sin6_addr,
586 sizeof (struct in6_addr));
590 else if (addrlen == sizeof (struct IPv4UdpAddress))
593 memset (&a4, 0, sizeof (a4));
594 #if HAVE_SOCKADDR_IN_SIN_LEN
595 a4.sin_len = sizeof (a4);
597 a4.sin_family = AF_INET;
598 a4.sin_port = t4->u_port;
599 a4.sin_addr.s_addr = t4->ipv4_addr;
606 GNUNET_free (message);
610 /* Actually send the message */
612 GNUNET_NETWORK_socket_sendto (send_handle, message, ssize,
618 if (sent == GNUNET_SYSERR)
619 cont (cont_cls, target, GNUNET_SYSERR);
622 cont (cont_cls, target, GNUNET_OK);
626 GNUNET_free (message);
631 * We learned about a peer (possibly behind NAT) so run the
632 * gnunet-nat-client to send dummy ICMP responses
634 * @param plugin the plugin for this transport
635 * @param addr the address of the peer
636 * @param addrlen the length of the address
639 run_gnunet_nat_client (struct Plugin *plugin, const char *addr, size_t addrlen)
641 char inet4[INET_ADDRSTRLEN];
642 char *address_as_string;
643 char *port_as_string;
645 const struct sockaddr *sa = (const struct sockaddr *)addr;
647 if (addrlen < sizeof (struct sockaddr))
649 switch (sa->sa_family)
652 if (addrlen != sizeof (struct sockaddr_in))
654 if (NULL == inet_ntop (AF_INET,
655 &((struct sockaddr_in *) sa)->sin_addr,
656 inet4, INET_ADDRSTRLEN))
658 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
661 address_as_string = GNUNET_strdup (inet4);
668 GNUNET_asprintf(&port_as_string, "%d", plugin->port);
670 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
671 _("Running gnunet-nat-client with arguments: %s %s %d\n"), plugin->external_address, address_as_string, plugin->port);
674 /* Start the server process */
675 pid = GNUNET_OS_start_process(NULL, NULL, "gnunet-nat-client", "gnunet-nat-client", plugin->external_address, address_as_string, port_as_string, NULL);
676 GNUNET_free(address_as_string);
677 GNUNET_free(port_as_string);
678 GNUNET_OS_process_wait (pid);
682 * Function that can be used by the transport service to transmit
683 * a message using the plugin.
686 * @param target who should receive this message (ignored by UDP)
687 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
688 * @param msgbuf_size the size of the msgbuf to send
689 * @param priority how important is the message (ignored by UDP)
690 * @param timeout when should we time out (give up) if we can not transmit?
691 * @param session identifier used for this session (can be NULL)
692 * @param addr the addr to send the message to, needs to be a sockaddr for us
693 * @param addrlen the len of addr
694 * @param force_address not used, we had better have an address to send to
695 * because we are stateless!!
696 * @param cont continuation to call once the message has
697 * been transmitted (or if the transport is ready
698 * for the next transmission call; or if the
699 * peer disconnected...)
700 * @param cont_cls closure for cont
702 * @return the number of bytes written (may return 0 and the message can
703 * still be transmitted later!)
706 udp_plugin_send (void *cls,
707 const struct GNUNET_PeerIdentity *target,
710 unsigned int priority,
711 struct GNUNET_TIME_Relative timeout,
712 struct Session *session,
716 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
718 struct Plugin *plugin = cls;
720 struct MessageQueue *temp_message;
721 struct PeerSession *peer_session;
723 const struct IPv4UdpAddress *t4;
725 GNUNET_assert (NULL == session);
726 other_peer_natd = GNUNET_NO;
728 if (addrlen == sizeof(struct IPv4UdpAddress))
731 if (ntohs(t4->u_port) == 0)
732 other_peer_natd = GNUNET_YES;
734 else if (addrlen == sizeof(struct IPv6UdpAddress))
745 if ((other_peer_natd == GNUNET_YES) && (plugin->allow_nat == GNUNET_YES))
747 peer_session = find_session(plugin, target);
748 if (peer_session == NULL) /* We have a new peer to add */
751 * The first time, we can assume we have no knowledge of a
752 * working port for this peer, call the ICMP/UDP message sender
755 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
756 peer_session->connect_addr = GNUNET_malloc(addrlen);
757 memcpy(peer_session->connect_addr, addr, addrlen);
758 peer_session->connect_alen = addrlen;
759 peer_session->plugin = plugin;
760 peer_session->sock = NULL;
761 memcpy(&peer_session->target, target, sizeof(struct GNUNET_PeerIdentity));
762 peer_session->expecting_welcome = GNUNET_YES;
764 peer_session->next = plugin->sessions;
765 plugin->sessions = peer_session;
767 peer_session->messages = GNUNET_malloc(sizeof(struct MessageQueue));
768 peer_session->messages->msgbuf = GNUNET_malloc(msgbuf_size);
769 memcpy(peer_session->messages->msgbuf, msgbuf, msgbuf_size);
770 peer_session->messages->msgbuf_size = msgbuf_size;
771 peer_session->messages->timeout = GNUNET_TIME_relative_to_absolute(timeout);
772 peer_session->messages->cont = cont;
773 peer_session->messages->cont_cls = cont_cls;
775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
776 _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target));
778 run_gnunet_nat_client(plugin, addr, addrlen);
782 if (peer_session->expecting_welcome == GNUNET_NO) /* We are "connected" */
784 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);
786 else /* Haven't gotten a response from this peer, queue message */
788 temp_message = GNUNET_malloc(sizeof(struct MessageQueue));
789 temp_message->msgbuf = GNUNET_malloc(msgbuf_size);
790 memcpy(temp_message->msgbuf, msgbuf, msgbuf_size);
791 temp_message->msgbuf_size = msgbuf_size;
792 temp_message->timeout = GNUNET_TIME_relative_to_absolute(timeout);
793 temp_message->cont = cont;
794 temp_message->cont_cls = cont_cls;
795 temp_message->next = peer_session->messages;
796 peer_session->messages = temp_message;
800 else if (other_peer_natd == GNUNET_NO) /* Other peer not behind a NAT, so we can just send the message as is */
802 sent = udp_real_send(cls, udp_sock.desc, target, msgbuf, msgbuf_size, priority, timeout, addr, addrlen, cont, cont_cls);
804 else /* Other peer is NAT'd, but we don't want to play with them (or can't!) */
805 return GNUNET_SYSERR;
807 /* When GNUNET_SYSERR is returned from udp_real_send, we will still call
808 * the callback so must not return GNUNET_SYSERR!
809 * If we do, then transport context get freed twice. */
810 if (sent == GNUNET_SYSERR)
818 * Add the IP of our network interface to the list of
819 * our external IP addresses.
822 process_interfaces (void *cls,
825 const struct sockaddr *addr, socklen_t addrlen)
827 struct Plugin *plugin = cls;
829 struct IPv4UdpAddress t4;
830 struct IPv6UdpAddress t6;
836 af = addr->sa_family;
839 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
840 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
842 t4.u_port = htons (DEFAULT_NAT_PORT);
844 else if (plugin->behind_nat == GNUNET_YES) /* We are behind NAT, but will advertise NAT and normal addresses */
846 addr_nat = GNUNET_malloc(sizeof(t4));
847 memcpy(addr_nat, &t4, sizeof(t4));
848 t4.u_port = plugin->port;
849 ((struct IPv4UdpAddress *)addr_nat)->u_port = htons(DEFAULT_NAT_PORT);
853 t4.u_port = htons(plugin->port);
858 else if (af == AF_INET6)
861 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
863 /* skip link local addresses */
866 memcpy (&t6.ipv6_addr,
867 &((struct sockaddr_in6 *) addr)->sin6_addr,
868 sizeof (struct in6_addr));
869 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
871 t6.u6_port = htons (0);
873 else if (plugin->behind_nat == GNUNET_YES)
875 addr_nat = GNUNET_malloc(sizeof(t6));
876 memcpy(addr_nat, &t6, sizeof(t6));
877 t6.u6_port = plugin->port;
878 ((struct IPv6UdpAddress *)addr_nat)->u6_port = htons(DEFAULT_NAT_PORT);
882 t6.u6_port = htons (plugin->port);
894 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
895 GNUNET_ERROR_TYPE_BULK,
896 _("Found address `%s' (%s)\n"),
897 GNUNET_a2s (addr, addrlen), name);
899 if (addr_nat != NULL)
901 plugin->env->notify_address (plugin->env->cls,
903 addr_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
904 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
905 GNUNET_ERROR_TYPE_BULK,
906 _("Found NAT address `%s' (%s)\n"),
907 GNUNET_a2s (addr_nat, args), name);
908 GNUNET_free(addr_nat);
911 plugin->env->notify_address (plugin->env->cls,
913 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
920 * Function called by the resolver for each address obtained from DNS
921 * for our own hostname. Add the addresses to the list of our
922 * external IP addresses.
925 * @param addr one of the addresses of the host, NULL for the last address
926 * @param addrlen length of the address
929 process_hostname_ips (void *cls,
930 const struct sockaddr *addr, socklen_t addrlen)
932 struct Plugin *plugin = cls;
936 plugin->hostname_dns = NULL;
939 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
944 * Send UDP probe messages or UDP keepalive messages, depending on the
945 * state of the connection.
947 * @param cls closure for this call (should be the main Plugin)
948 * @param tc task context for running this
951 send_udp_probe_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
953 struct UDP_NAT_Probes *probe = cls;
954 struct UDP_NAT_ProbeMessage *message;
955 struct Plugin *plugin = probe->plugin;
957 message = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessage));
958 message->header.size = htons(sizeof(struct UDP_NAT_ProbeMessage));
959 message->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE);
960 /* If they gave us a port, use that. If not, try our port. */
961 if (probe->port != 0)
962 probe->sock_addr.sin_port = htons(probe->port);
964 probe->sock_addr.sin_port = htons(plugin->port);
967 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
968 _("Sending a probe to port %d\n"), ntohs(probe->sock_addr.sin_port));
973 udp_real_send(plugin, udp_sock.desc, NULL,
974 (char *)message, ntohs(message->header.size), 0,
975 GNUNET_TIME_relative_get_unit(),
976 &probe->sock_addr, sizeof(probe->sock_addr),
977 &udp_probe_continuation, probe);
979 GNUNET_free(message);
984 * Continuation for probe sends. If the last probe was sent
985 * "successfully", schedule sending of another one. If not,
989 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result)
991 struct UDP_NAT_Probes *probe = cls;
992 struct Plugin *plugin = probe->plugin;
994 if ((result == GNUNET_OK) && (probe->count < MAX_PROBES))
997 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
998 _("Scheduling next probe for 10000 milliseconds\n"));
1000 probe->task = GNUNET_SCHEDULER_add_delayed(plugin->env->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10000), &send_udp_probe_message, probe);
1002 else /* Destroy the probe context. */
1005 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1006 _("Sending probe didn't go well...\n"));
1012 * Find probe message by address
1014 * @param plugin the plugin for this transport
1015 * @param address_string the ip address as a string
1017 struct UDP_NAT_Probes *
1018 find_probe(struct Plugin *plugin, char * address_string)
1020 struct UDP_NAT_Probes *pos;
1022 pos = plugin->probes;
1024 if (strcmp(pos->address_string, address_string) == 0)
1032 * @param cls the plugin handle
1033 * @param tc the scheduling context (for rescheduling this function again)
1035 * We have been notified that gnunet-nat-server has written something to stdout.
1036 * Handle the output, then reschedule this function to be called again once
1037 * more is available.
1041 udp_plugin_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1043 struct Plugin *plugin = cls;
1046 memset(&mybuf, 0, sizeof(mybuf));
1048 struct UDP_NAT_Probes *temp_probe;
1051 struct sockaddr_in in_addr;
1053 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1056 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, &mybuf, sizeof(mybuf));
1061 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1062 _("Finished reading from server stdout with code: %d\n"), bytes);
1069 for (i = 0; i < sizeof(mybuf); i++)
1071 if (mybuf[i] == '\n')
1074 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
1077 port_start = &mybuf[i + 1];
1081 if (port_start != NULL)
1082 port = atoi(port_start);
1085 plugin->server_read_task =
1086 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1087 GNUNET_TIME_UNIT_FOREVER_REL,
1088 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1093 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1094 _("nat-server-read read: %s port %d\n"), &mybuf, port);
1098 * We have received an ICMP response, ostensibly from a non-NAT'd peer
1099 * that wants to connect to us! Send a message to establish a connection.
1101 if (inet_pton(AF_INET, &mybuf[0], &in_addr.sin_addr) != 1)
1104 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp",
1105 _("nat-server-read malformed address\n"), &mybuf, port);
1107 plugin->server_read_task =
1108 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1109 GNUNET_TIME_UNIT_FOREVER_REL,
1110 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1114 temp_probe = find_probe(plugin, &mybuf[0]);
1116 if (temp_probe == NULL)
1118 temp_probe = GNUNET_malloc(sizeof(struct UDP_NAT_Probes));
1119 temp_probe->address_string = strdup(&mybuf[0]);
1120 temp_probe->sock_addr.sin_family = AF_INET;
1121 GNUNET_assert(inet_pton(AF_INET, &mybuf[0], &temp_probe->sock_addr.sin_addr) == 1);
1122 temp_probe->port = port;
1123 temp_probe->next = plugin->probes;
1124 temp_probe->plugin = plugin;
1125 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);
1126 plugin->probes = temp_probe;
1129 plugin->server_read_task =
1130 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1131 GNUNET_TIME_UNIT_FOREVER_REL,
1132 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1138 * Demultiplexer for UDP NAT messages
1140 * @param plugin the main plugin for this transport
1141 * @param sender from which peer the message was received
1142 * @param currhdr pointer to the header of the message
1143 * @param sender_addr the address from which the message was received
1144 * @param fromlen the length of the address
1145 * @param sockinfo which socket did we receive the message on
1148 udp_demultiplexer(struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
1149 const struct GNUNET_MessageHeader *currhdr,
1150 const void *sender_addr,
1151 size_t fromlen, struct UDP_Sock_Info *sockinfo)
1153 struct UDP_NAT_ProbeMessageReply *outgoing_probe_reply;
1154 struct UDP_NAT_ProbeMessageConfirmation *outgoing_probe_confirmation;
1156 char addr_buf[INET_ADDRSTRLEN];
1157 struct UDP_NAT_Probes *outgoing_probe;
1158 struct PeerSession *peer_session;
1159 struct MessageQueue *pending_message;
1160 struct MessageQueue *pending_message_temp;
1162 if (memcmp(sender, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1165 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1166 _("Received a message from myself, dropping!!!\n"));
1171 switch (ntohs(currhdr->type))
1173 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE:
1174 /* Send probe reply */
1175 outgoing_probe_reply = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageReply));
1176 outgoing_probe_reply->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageReply));
1177 outgoing_probe_reply->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY);
1180 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1181 _("Received a probe on listen port %d, sent_from port %d\n"), sockinfo->port, ntohs(((struct sockaddr_in *)sender_addr)->sin_port));
1184 udp_real_send(plugin, sockinfo->desc, NULL,
1185 (char *)outgoing_probe_reply,
1186 ntohs(outgoing_probe_reply->header.size), 0,
1187 GNUNET_TIME_relative_get_unit(),
1188 sender_addr, fromlen,
1192 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1193 _("Sent PROBE REPLY to port %d on outgoing port %d\n"), ntohs(((struct sockaddr_in *)sender_addr)->sin_port), sockinfo->port);
1195 GNUNET_free(outgoing_probe_reply);
1197 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY:
1198 /* Check for existing probe, check ports returned, send confirmation if all is well */
1200 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1201 _("Received PROBE REPLY from port %d on incoming port %d\n"), ntohs(((struct sockaddr_in *)sender_addr)->sin_port), sockinfo->port);
1203 if ((sender_addr != NULL) && (sizeof(sender_addr) == sizeof(struct IPv4UdpAddress)))
1205 memset(&addr_buf, 0, sizeof(addr_buf));
1206 if (NULL == inet_ntop (AF_INET,
1207 &((struct sockaddr_in *) sender_addr)->sin_addr, addr_buf,
1210 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1213 outgoing_probe = find_probe(plugin, &addr_buf[0]);
1214 if (outgoing_probe != NULL)
1217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1218 _("Sending confirmation that we were reached!\n"));
1220 outgoing_probe_confirmation = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1221 outgoing_probe_confirmation->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1222 outgoing_probe_confirmation->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM);
1224 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);
1226 if (outgoing_probe->task != GNUNET_SCHEDULER_NO_TASK)
1228 GNUNET_SCHEDULER_cancel(plugin->env->sched, outgoing_probe->task);
1229 outgoing_probe->task = GNUNET_SCHEDULER_NO_TASK;
1230 /* Schedule task to timeout and remove probe if confirmation not received */
1232 GNUNET_free(outgoing_probe_confirmation);
1237 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1238 _("Received a probe reply, but have no record of a sent probe!\n"));
1243 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM:
1244 peer_session = find_session(plugin, sender);
1246 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1247 _("Looking up peer session for peer %s\n"), GNUNET_i2s(sender));
1249 if (peer_session == NULL) /* Shouldn't this NOT happen? */
1252 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1253 _("Peer not in list, adding (THIS MAY BE A MISTAKE) %s\n"), GNUNET_i2s(sender));
1255 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
1256 peer_session->connect_addr = GNUNET_malloc(fromlen);
1257 memcpy(peer_session->connect_addr, sender_addr, fromlen);
1258 peer_session->connect_alen = fromlen;
1259 peer_session->plugin = plugin;
1260 peer_session->sock = sockinfo->desc;
1261 memcpy(&peer_session->target, sender, sizeof(struct GNUNET_PeerIdentity));
1262 peer_session->expecting_welcome = GNUNET_NO;
1264 peer_session->next = plugin->sessions;
1265 plugin->sessions = peer_session;
1267 peer_session->messages = NULL;
1269 else if (peer_session->expecting_welcome == GNUNET_YES)
1271 peer_session->expecting_welcome = GNUNET_NO;
1272 peer_session->sock = sockinfo->desc;
1273 ((struct sockaddr_in *)peer_session->connect_addr)->sin_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1275 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1276 _("Received a probe confirmation, will send to peer on port %d\n"), ntohs(((struct sockaddr_in *)peer_session->connect_addr)->sin_port));
1278 if (peer_session->messages != NULL)
1281 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1282 _("Received a probe confirmation, sending queued messages.\n"));
1284 pending_message = peer_session->messages;
1286 while (pending_message != NULL)
1289 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1290 _("sending queued message %d\n"), count);
1292 udp_real_send(plugin, peer_session->sock, &peer_session->target, pending_message->msgbuf, pending_message->msgbuf_size, 0, GNUNET_TIME_relative_get_unit(), peer_session->connect_addr, peer_session->connect_alen, pending_message->cont, pending_message->cont_cls);
1293 pending_message_temp = pending_message;
1294 pending_message = pending_message->next;
1295 GNUNET_free(pending_message_temp->msgbuf);
1296 GNUNET_free(pending_message_temp);
1298 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1299 _("finished sending queued message %d\n"), count);
1309 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1310 _("Received probe confirmation for already confirmed peer!\n"));
1313 /* Received confirmation, add peer with address/port specified */
1315 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_KEEPALIVE:
1316 /* Once we've sent NAT_PROBE_CONFIRM change to sending keepalives */
1317 /* If we receive these just ignore! */
1320 plugin->env->receive (plugin->env->cls, sender, currhdr, UDP_DIRECT_DISTANCE,
1321 NULL, sender_addr, fromlen);
1328 * @param cls the plugin handle
1329 * @param tc the scheduling context (for rescheduling this function again)
1331 * We have been notified that our writeset has something to read. We don't
1332 * know which socket needs to be read, so we have to check each one
1333 * Then reschedule this function to be called again once more is available.
1337 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1339 struct Plugin *plugin = cls;
1341 struct UDPMessage *msg;
1342 struct GNUNET_PeerIdentity *sender;
1343 unsigned int buflen;
1351 const struct GNUNET_MessageHeader *currhdr;
1352 struct IPv4UdpAddress t4;
1353 struct IPv6UdpAddress t6;
1354 const struct sockaddr_in *s4;
1355 const struct sockaddr_in6 *s6;
1360 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1362 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1368 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock.desc);
1370 if (buflen == GNUNET_NO)
1373 buf = GNUNET_malloc (buflen);
1374 fromlen = sizeof (addr);
1375 memset (&addr, 0, sizeof(addr));
1377 GNUNET_NETWORK_socket_recvfrom (udp_sock.desc, buf, buflen,
1378 (struct sockaddr *)&addr, &fromlen);
1380 if (fromlen == sizeof (struct sockaddr_in))
1382 s4 = (const struct sockaddr_in*) &addr;
1383 t4.u_port = s4->sin_port;
1384 t4.ipv4_addr = s4->sin_addr.s_addr;
1386 calen = sizeof (t4);
1388 else if (fromlen == sizeof (struct sockaddr_in6))
1390 s6 = (const struct sockaddr_in6*) &addr;
1391 t6.u6_port = s6->sin6_port;
1392 memcpy (&t6.ipv6_addr,
1394 sizeof (struct in6_addr));
1396 calen = sizeof (t6);
1406 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1407 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
1416 msg = (struct UDPMessage *) buf;
1419 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1420 ("header reports message size of %d, type %d\n"),
1421 ntohs (msg->header.size), ntohs (msg->header.type));
1423 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
1429 msgbuf = (char *)&msg[1];
1430 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1431 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1435 tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
1437 while (offset < tsize)
1439 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
1441 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1442 ("processing msg %d: type %d, size %d at offset %d\n"),
1443 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
1445 udp_demultiplexer(plugin, sender, currhdr, ca, calen, &udp_sock);
1447 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1448 ("processing done msg %d: type %d, size %d at offset %d\n"),
1449 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
1451 offset += ntohs(currhdr->size);
1454 GNUNET_free_non_null (buf);
1455 GNUNET_free_non_null (sender);
1458 plugin->select_task =
1459 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1460 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1461 GNUNET_SCHEDULER_NO_TASK,
1462 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1463 NULL, &udp_plugin_select, plugin);
1468 * Create a slew of UDP sockets. If possible, use IPv6, otherwise
1471 * @param cls closure for server start, should be a struct Plugin *
1473 * @return number of sockets created or GNUNET_SYSERR on error
1476 udp_transport_server_start (void *cls)
1478 struct Plugin *plugin = cls;
1479 struct sockaddr_in serverAddrv4;
1480 struct sockaddr_in6 serverAddrv6;
1481 struct sockaddr *serverAddr;
1483 int sockets_created;
1485 sockets_created = 0;
1487 if (plugin->behind_nat == GNUNET_YES)
1489 /* Pipe to read from started processes stdout (on read end) */
1490 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES);
1491 if (plugin->server_stdout == NULL)
1492 return sockets_created;
1494 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1496 "Starting gnunet-nat-server process cmd: %s %s\n", "gnunet-nat-server", plugin->internal_address);
1498 /* Start the server process */
1499 plugin->server_pid = GNUNET_OS_start_process(NULL, plugin->server_stdout, "gnunet-nat-server", "gnunet-nat-server", plugin->internal_address, NULL);
1500 if (plugin->server_pid == GNUNET_SYSERR)
1503 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1505 "Failed to start gnunet-nat-server process\n");
1507 return GNUNET_SYSERR;
1509 /* Close the write end of the read pipe */
1510 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1512 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout, GNUNET_DISK_PIPE_END_READ);
1513 plugin->server_read_task =
1514 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1515 GNUNET_TIME_UNIT_FOREVER_REL,
1516 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1519 udp_sock.desc = NULL;
1522 udp_sock.desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
1523 if (NULL == udp_sock.desc)
1525 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
1526 return sockets_created;
1530 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1531 #if HAVE_SOCKADDR_IN_SIN_LEN
1532 serverAddrv4.sin_len = sizeof (serverAddrv4);
1534 serverAddrv4.sin_family = AF_INET;
1535 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1536 serverAddrv4.sin_port = htons (plugin->port);
1537 addrlen = sizeof (serverAddrv4);
1538 serverAddr = (struct sockaddr *) &serverAddrv4;
1540 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1542 "Binding to port %d\n", ntohs(serverAddrv4.sin_port));
1544 while (GNUNET_NETWORK_socket_bind (udp_sock.desc, serverAddr, addrlen) !=
1547 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1549 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1551 "Binding failed, trying new port %d\n", ntohs(serverAddrv4.sin_port));
1554 udp_sock.port = ntohs(serverAddrv4.sin_port);
1559 if ((udp_sock.desc == NULL) && (GNUNET_YES !=
1560 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
1563 udp_sock.desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
1564 if (udp_sock.desc != NULL)
1566 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1567 #if HAVE_SOCKADDR_IN_SIN_LEN
1568 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1570 serverAddrv6.sin6_family = AF_INET6;
1571 serverAddrv6.sin6_addr = in6addr_any;
1572 serverAddrv6.sin6_port = htons (plugin->port);
1573 addrlen = sizeof (serverAddrv6);
1574 serverAddr = (struct sockaddr *) &serverAddrv6;
1579 plugin->rs = GNUNET_NETWORK_fdset_create ();
1581 GNUNET_NETWORK_fdset_zero (plugin->rs);
1584 GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock.desc);
1586 plugin->select_task =
1587 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1588 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1589 GNUNET_SCHEDULER_NO_TASK,
1590 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1591 NULL, &udp_plugin_select, plugin);
1593 return sockets_created;
1598 * Another peer has suggested an address for this peer and transport
1599 * plugin. Check that this could be a valid address. This function
1600 * is not expected to 'validate' the address in the sense of trying to
1601 * connect to it but simply to see if the binary format is technically
1602 * legal for establishing a connection.
1604 * @param cls closure, should be our handle to the Plugin
1605 * @param addr pointer to the address, may be modified (slightly)
1606 * @param addrlen length of addr
1607 * @return GNUNET_OK if this is a plausible address for this peer
1608 * and transport, GNUNET_SYSERR if not
1612 udp_check_address (void *cls, void *addr, size_t addrlen)
1614 struct Plugin *plugin = cls;
1615 char buf[sizeof (struct sockaddr_in6)];
1617 struct sockaddr_in *v4;
1618 struct sockaddr_in6 *v6;
1620 if ((addrlen != sizeof (struct sockaddr_in)) &&
1621 (addrlen != sizeof (struct sockaddr_in6)))
1623 GNUNET_break_op (0);
1624 return GNUNET_SYSERR;
1626 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1627 if (addrlen == sizeof (struct sockaddr_in))
1629 v4 = (struct sockaddr_in *) buf;
1630 v4->sin_port = htons (plugin->port);
1634 v6 = (struct sockaddr_in6 *) buf;
1635 v6->sin6_port = htons (plugin->port);
1639 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1641 "Informing transport service about my address `%s'.\n",
1642 GNUNET_a2s (addr, addrlen));
1649 * Append our port and forward the result.
1652 append_port (void *cls, const char *hostname)
1654 struct PrettyPrinterContext *ppc = cls;
1657 if (hostname == NULL)
1659 ppc->asc (ppc->asc_cls, NULL);
1663 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1664 ppc->asc (ppc->asc_cls, ret);
1670 * Convert the transports address to a nice, human-readable
1673 * @param cls closure
1674 * @param type name of the transport that generated the address
1675 * @param addr one of the addresses of the host, NULL for the last address
1676 * the specific address format depends on the transport
1677 * @param addrlen length of the address
1678 * @param numeric should (IP) addresses be displayed in numeric form?
1679 * @param timeout after how long should we give up?
1680 * @param asc function to call on each string
1681 * @param asc_cls closure for asc
1684 udp_plugin_address_pretty_printer (void *cls,
1689 struct GNUNET_TIME_Relative timeout,
1690 GNUNET_TRANSPORT_AddressStringCallback asc,
1693 struct Plugin *plugin = cls;
1694 const struct sockaddr_in *v4;
1695 const struct sockaddr_in6 *v6;
1696 struct PrettyPrinterContext *ppc;
1698 if ((addrlen != sizeof (struct sockaddr_in)) &&
1699 (addrlen != sizeof (struct sockaddr_in6)))
1701 /* invalid address */
1702 GNUNET_break_op (0);
1703 asc (asc_cls, NULL);
1706 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1708 ppc->asc_cls = asc_cls;
1709 if (addrlen == sizeof (struct sockaddr_in))
1711 v4 = (const struct sockaddr_in *) addr;
1712 ppc->port = ntohs (v4->sin_port);
1716 v6 = (const struct sockaddr_in6 *) addr;
1717 ppc->port = ntohs (v6->sin6_port);
1720 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1724 !numeric, timeout, &append_port, ppc);
1728 * Return the actual path to a file found in the current
1729 * PATH environment variable.
1731 * @param binary the name of the file to find
1734 get_path_from_PATH (char *binary)
1742 p = getenv ("PATH");
1745 path = GNUNET_strdup (p); /* because we write on it */
1746 buf = GNUNET_malloc (strlen (path) + 20);
1749 while (NULL != (end = strchr (pos, ':')))
1752 sprintf (buf, "%s/%s", pos, binary);
1753 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
1760 sprintf (buf, "%s/%s", pos, binary);
1761 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
1772 * Check whether the suid bit is set on a file.
1773 * Attempts to find the file using the current
1774 * PATH environment variable as a search path.
1776 * @param binary the name of the file to check
1779 check_gnunet_nat_binary(char *binary)
1781 struct stat statbuf;
1784 p = get_path_from_PATH (binary);
1787 if (0 != STAT (p, &statbuf))
1790 return GNUNET_SYSERR;
1793 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
1794 (statbuf.st_uid == 0) )
1800 * Function called for a quick conversion of the binary address to
1801 * a numeric address. Note that the caller must not free the
1802 * address and that the next call to this function is allowed
1803 * to override the address again.
1805 * @param cls closure
1806 * @param addr binary address
1807 * @param addrlen length of the address
1808 * @return string representing the same address
1811 udp_address_to_string (void *cls,
1815 static char rbuf[INET6_ADDRSTRLEN + 10];
1816 char buf[INET6_ADDRSTRLEN];
1820 const struct IPv4UdpAddress *t4;
1821 const struct IPv6UdpAddress *t6;
1825 if (addrlen == sizeof (struct IPv6UdpAddress))
1829 port = ntohs (t6->u6_port);
1830 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1833 else if (addrlen == sizeof (struct IPv4UdpAddress))
1837 port = ntohs (t4->u_port);
1838 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1843 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1844 GNUNET_snprintf (rbuf,
1853 * The exported method. Makes the core api available via a global and
1854 * returns the udp transport API.
1857 libgnunet_plugin_transport_udp_init (void *cls)
1859 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1860 unsigned long long mtu;
1861 unsigned long long port;
1862 struct GNUNET_TRANSPORT_PluginFunctions *api;
1863 struct Plugin *plugin;
1864 struct GNUNET_SERVICE_Context *service;
1865 int sockets_created;
1868 int only_nat_addresses;
1869 char *internal_address;
1870 char *external_address;
1871 struct IPv4UdpAddress v4_address;
1873 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
1874 if (service == NULL)
1876 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
1877 ("Failed to start service for `%s' transport plugin.\n"),
1882 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
1886 /* We are behind nat (according to the user) */
1887 if (check_gnunet_nat_binary("gnunet-nat-server") == GNUNET_YES)
1888 behind_nat = GNUNET_YES;
1891 behind_nat = GNUNET_NO;
1892 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");
1896 behind_nat = GNUNET_NO; /* We are not behind nat! */
1898 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
1902 if (check_gnunet_nat_binary("gnunet-nat-client") == GNUNET_YES)
1903 allow_nat = GNUNET_YES; /* We will try to connect to NAT'd peers */
1906 allow_nat = GNUNET_NO;
1907 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");
1912 allow_nat = GNUNET_NO; /* We don't want to try to help NAT'd peers */
1914 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
1916 "ONLY_NAT_ADDRESSES"))
1917 only_nat_addresses = GNUNET_YES; /* We will only report our addresses as NAT'd */
1919 only_nat_addresses = GNUNET_NO; /* We will report our addresses as NAT'd and non-NAT'd */
1921 external_address = NULL;
1922 if (((GNUNET_YES == behind_nat) || (GNUNET_YES == allow_nat)) && (GNUNET_OK !=
1923 GNUNET_CONFIGURATION_get_value_string (env->cfg,
1926 &external_address)))
1928 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1931 ("Require EXTERNAL_ADDRESS for service `%s' in configuration (either BEHIND_NAT or ALLOW_NAT set to YES)!\n"),
1933 GNUNET_SERVICE_stop (service);
1937 if ((external_address != NULL) && (inet_pton(AF_INET, external_address, &v4_address.ipv4_addr) != 1))
1939 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed EXTERNAL_ADDRESS %s given in configuration!\n", external_address);
1942 internal_address = NULL;
1943 if ((GNUNET_YES == behind_nat) && (GNUNET_OK !=
1944 GNUNET_CONFIGURATION_get_value_string (env->cfg,
1947 &internal_address)))
1949 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1952 ("Require INTERNAL_ADDRESS for service `%s' in configuration!\n"),
1954 GNUNET_SERVICE_stop (service);
1955 GNUNET_free_non_null(external_address);
1959 if ((internal_address != NULL) && (inet_pton(AF_INET, internal_address, &v4_address.ipv4_addr) != 1))
1961 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed INTERNAL_ADDRESS %s given in configuration!\n", internal_address);
1965 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1969 port = UDP_NAT_DEFAULT_PORT;
1970 else if (port > 65535)
1972 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1974 _("Given `%s' option is out of range: %llu > %u\n"),
1978 GNUNET_SERVICE_stop (service);
1979 GNUNET_free_non_null(external_address);
1980 GNUNET_free_non_null(internal_address);
1986 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1988 _("MTU %llu for `%s' is probably too low!\n"), mtu,
1991 plugin = GNUNET_malloc (sizeof (struct Plugin));
1992 plugin->external_address = external_address;
1993 plugin->internal_address = internal_address;
1994 plugin->port = port;
1995 plugin->behind_nat = behind_nat;
1996 plugin->allow_nat = allow_nat;
1997 plugin->only_nat_addresses = only_nat_addresses;
2000 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2003 api->send = &udp_plugin_send;
2004 api->disconnect = &udp_disconnect;
2005 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2006 api->address_to_string = &udp_address_to_string;
2007 api->check_address = &udp_check_address;
2009 plugin->service = service;
2011 if (plugin->behind_nat == GNUNET_NO)
2013 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2016 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
2019 HOSTNAME_RESOLVE_TIMEOUT,
2020 &process_hostname_ips,
2023 if ((plugin->behind_nat == GNUNET_YES) && (inet_pton(AF_INET, plugin->external_address, &v4_address.ipv4_addr) == 1))
2025 v4_address.u_port = htons(0);
2026 plugin->env->notify_address (plugin->env->cls,
2028 &v4_address, sizeof(v4_address), GNUNET_TIME_UNIT_FOREVER_REL);
2030 else if ((plugin->external_address != NULL) && (inet_pton(AF_INET, plugin->external_address, &v4_address.ipv4_addr) == 1))
2032 v4_address.u_port = htons(plugin->port);
2033 plugin->env->notify_address (plugin->env->cls,
2035 &v4_address, sizeof(v4_address), GNUNET_TIME_UNIT_FOREVER_REL);
2038 sockets_created = udp_transport_server_start (plugin);
2040 GNUNET_assert (sockets_created == 1);
2046 libgnunet_plugin_transport_udp_done (void *cls)
2048 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2049 struct Plugin *plugin = api->cls;
2051 udp_transport_server_stop (plugin);
2052 if (NULL != plugin->hostname_dns)
2054 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
2055 plugin->hostname_dns = NULL;
2058 GNUNET_SERVICE_stop (plugin->service);
2060 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2061 GNUNET_free (plugin);
2066 /* end of plugin_transport_udp.c */