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
65 * How long until we give up on transmitting the welcome message?
67 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
70 * Starting port for listening and sending, eventually a config value
72 #define UDP_NAT_DEFAULT_PORT 22086
75 * UDP Message-Packet header.
82 struct GNUNET_MessageHeader header;
85 * What is the identity of the sender (GNUNET_hash of public key)
87 struct GNUNET_PeerIdentity sender;
92 /* Forward definition */
95 struct PrettyPrinterContext
97 GNUNET_TRANSPORT_AddressStringCallback asc;
107 struct MessageQueue *next;
110 * Session this message belongs to
112 struct PeerSession *session;
115 * Actual message to be sent
120 * Size of message buffer to be sent
125 * When to discard this message
127 struct GNUNET_TIME_Absolute timeout;
130 * Continuation to call when this message goes out
132 GNUNET_TRANSPORT_TransmitContinuation cont;
135 * closure for continuation
142 * UDP NAT Probe message definition
144 struct UDP_NAT_ProbeMessage
149 struct GNUNET_MessageHeader header;
154 * UDP NAT Probe message reply definition
156 struct UDP_NAT_ProbeMessageReply
161 struct GNUNET_MessageHeader header;
167 * UDP NAT Probe message confirm definition
169 struct UDP_NAT_ProbeMessageConfirmation
174 struct GNUNET_MessageHeader header;
187 * Stored in a linked list.
189 struct PeerSession *next;
192 * Pointer to the global plugin struct.
194 struct Plugin *plugin;
197 * To whom are we talking to (set to our identity
198 * if we are still waiting for the welcome message)
200 struct GNUNET_PeerIdentity target;
203 * Address of the other peer (either based on our 'connect'
204 * call or on our 'accept' call).
209 * Length of connect_addr.
214 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
216 int expecting_welcome;
219 * From which socket do we need to send to this peer?
221 struct GNUNET_NETWORK_Handle *sock;
224 * Queue of messages for this peer, in the case that
225 * we have to await a connection...
227 struct MessageQueue *messages;
231 struct UDP_NAT_Probes
237 struct UDP_NAT_Probes *next;
240 * Address string that the server process returned to us
242 char *address_string;
245 * Timeout for this set of probes
247 struct GNUNET_TIME_Absolute timeout;
250 * Count of how many probes we've attempted
255 * The plugin this probe belongs to
257 struct Plugin *plugin;
260 * The task used to send these probes
262 GNUNET_SCHEDULER_TaskIdentifier task;
265 * Network address (always ipv4)
267 struct sockaddr_in sock_addr;
270 * The port to send this probe to, 0 to choose randomly
278 * Encapsulation of all of the state of the plugin.
285 struct GNUNET_TRANSPORT_PluginEnvironment *env;
288 * Handle to the network service.
290 struct GNUNET_SERVICE_Context *service;
293 * Session of peers with whom we are currently connected
295 struct PeerSession *sessions;
298 * Handle for request of hostname resolution, non-NULL if pending.
300 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
303 * ID of task used to update our addresses when one expires.
305 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
310 GNUNET_SCHEDULER_TaskIdentifier select_task;
318 * The external address given to us by the user. Must be actual
319 * outside visible address for NAT punching to work.
321 char *external_address;
324 * The internal address given to us by the user (or discovered).
326 char *internal_address;
331 struct GNUNET_NETWORK_FDSet *rs;
334 * stdout pipe handle for the gnunet-nat-server process
336 struct GNUNET_DISK_PipeHandle *server_stdout;
339 * stdout file handle (for reading) for the gnunet-nat-server process
341 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
344 * ID of select gnunet-nat-server stdout read task
346 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
349 * Is this transport configured to be behind a NAT?
354 * Is this transport configured to allow connections to NAT'd peers?
359 * Should this transport advertise only NAT addresses (port set to 0)?
360 * If not, all addresses will be duplicated for NAT punching and regular
363 int only_nat_addresses;
366 * The process id of the server process (if behind NAT)
373 struct UDP_NAT_Probes *probes;
380 /* The network handle */
381 struct GNUNET_NETWORK_Handle *desc;
383 /* The port we bound to */
387 /* *********** globals ************* */
390 * the socket that we transmit all data with
392 static struct UDP_Sock_Info udp_sock;
396 * Forward declaration.
399 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result);
403 * Disconnect from a remote node. Clean up session if we have one for this peer
405 * @param cls closure for this call (should be handle to Plugin)
406 * @param target the peeridentity of the peer to disconnect
407 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
410 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
412 /** TODO: Implement! */
417 * Shutdown the server process (stop receiving inbound traffic). Maybe
420 * @param cls Handle to the plugin for this transport
422 * @return returns the number of sockets successfully closed,
423 * should equal the number of sockets successfully opened
426 udp_transport_server_stop (void *cls)
428 struct Plugin *plugin = cls;
433 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
435 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
436 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
439 ok = GNUNET_NETWORK_socket_close (udp_sock.desc);
441 udp_sock.desc = NULL;
444 if (plugin->behind_nat == GNUNET_YES)
446 if (0 != PLIBC_KILL (plugin->server_pid, SIGTERM))
448 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
450 GNUNET_OS_process_wait (plugin->server_pid);
453 if (ret != GNUNET_OK)
454 return GNUNET_SYSERR;
460 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
462 struct PeerSession *pos;
464 pos = plugin->sessions;
467 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
477 * Actually send out the message, assume we've got the address and
478 * send_handle squared away!
481 * @param send_handle which handle to send message on
482 * @param target who should receive this message (ignored by UDP)
483 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
484 * @param msgbuf_size the size of the msgbuf to send
485 * @param priority how important is the message (ignored by UDP)
486 * @param timeout when should we time out (give up) if we can not transmit?
487 * @param addr the addr to send the message to, needs to be a sockaddr for us
488 * @param addrlen the len of addr
489 * @param cont continuation to call once the message has
490 * been transmitted (or if the transport is ready
491 * for the next transmission call; or if the
492 * peer disconnected...)
493 * @param cont_cls closure for cont
494 * @return the number of bytes written
497 udp_real_send (void *cls,
498 struct GNUNET_NETWORK_Handle *send_handle,
499 const struct GNUNET_PeerIdentity *target,
502 unsigned int priority,
503 struct GNUNET_TIME_Relative timeout,
506 GNUNET_TRANSPORT_TransmitContinuation cont,
509 struct Plugin *plugin = cls;
510 struct UDPMessage *message;
514 if ((addr == NULL) || (addrlen == 0))
517 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
518 ("udp_plugin_send called without address, returning!\n"));
521 cont (cont_cls, target, GNUNET_SYSERR);
522 return 0; /* Can never send if we don't have an address!! */
525 /* Build the message to be sent */
526 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
527 ssize = sizeof (struct UDPMessage) + msgbuf_size;
529 message->header.size = htons (ssize);
530 message->header.type = htons (0);
531 memcpy (&message->sender, plugin->env->my_identity,
532 sizeof (struct GNUNET_PeerIdentity));
533 memcpy (&message[1], msgbuf, msgbuf_size);
535 /* Actually send the message */
537 GNUNET_NETWORK_socket_sendto (send_handle, message, ssize,
543 if (sent == GNUNET_SYSERR)
544 cont (cont_cls, target, GNUNET_SYSERR);
547 cont (cont_cls, target, GNUNET_OK);
551 GNUNET_free (message);
556 * We learned about a peer (possibly behind NAT) so run the
557 * gnunet-nat-client to send dummy ICMP responses
559 * @param plugin the plugin for this transport
560 * @param addr the address of the peer
561 * @param addrlen the length of the address
564 run_gnunet_nat_client (struct Plugin *plugin, const char *addr, size_t addrlen)
566 char inet4[INET_ADDRSTRLEN];
567 char *address_as_string;
568 char *port_as_string;
570 const struct sockaddr *sa = (const struct sockaddr *)addr;
572 if (addrlen < sizeof (struct sockaddr))
574 switch (sa->sa_family)
577 if (addrlen != sizeof (struct sockaddr_in))
579 if (NULL == inet_ntop (AF_INET,
580 &((struct sockaddr_in *) sa)->sin_addr,
581 inet4, INET_ADDRSTRLEN))
583 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
586 address_as_string = GNUNET_strdup (inet4);
593 GNUNET_asprintf(&port_as_string, "%d", plugin->port);
595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
596 _("Running gnunet-nat-client with arguments: %s %s %d\n"), plugin->external_address, address_as_string, plugin->port);
599 /* Start the server process */
600 pid = GNUNET_OS_start_process(NULL, NULL, "gnunet-nat-client", "gnunet-nat-client", plugin->external_address, address_as_string, port_as_string, NULL);
601 GNUNET_free(address_as_string);
602 GNUNET_free(port_as_string);
603 GNUNET_OS_process_wait (pid);
607 * Function that can be used by the transport service to transmit
608 * a message using the plugin.
611 * @param target who should receive this message (ignored by UDP)
612 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
613 * @param msgbuf_size the size of the msgbuf to send
614 * @param priority how important is the message (ignored by UDP)
615 * @param timeout when should we time out (give up) if we can not transmit?
616 * @param session identifier used for this session (can be NULL)
617 * @param addr the addr to send the message to, needs to be a sockaddr for us
618 * @param addrlen the len of addr
619 * @param force_address not used, we had better have an address to send to
620 * because we are stateless!!
621 * @param cont continuation to call once the message has
622 * been transmitted (or if the transport is ready
623 * for the next transmission call; or if the
624 * peer disconnected...)
625 * @param cont_cls closure for cont
627 * @return the number of bytes written (may return 0 and the message can
628 * still be transmitted later!)
631 udp_plugin_send (void *cls,
632 const struct GNUNET_PeerIdentity *target,
635 unsigned int priority,
636 struct GNUNET_TIME_Relative timeout,
637 struct Session *session,
641 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
643 struct Plugin *plugin = cls;
645 struct MessageQueue *temp_message;
646 struct PeerSession *peer_session;
647 struct sockaddr_in *sockaddr = (struct sockaddr_in *)addr;
650 GNUNET_assert (NULL == session);
651 other_peer_natd = GNUNET_NO;
652 if ((sockaddr->sin_family == AF_INET) && (ntohs(sockaddr->sin_port) == 0))
654 other_peer_natd = GNUNET_YES;
659 if ((other_peer_natd == GNUNET_YES) && (plugin->allow_nat == GNUNET_YES))
661 peer_session = find_session(plugin, target);
662 if (peer_session == NULL) /* We have a new peer to add */
665 * The first time, we can assume we have no knowledge of a
666 * working port for this peer, call the ICMP/UDP message sender
669 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
670 peer_session->connect_addr = GNUNET_malloc(addrlen);
671 memcpy(peer_session->connect_addr, addr, addrlen);
672 peer_session->connect_alen = addrlen;
673 peer_session->plugin = plugin;
674 peer_session->sock = NULL;
675 memcpy(&peer_session->target, target, sizeof(struct GNUNET_PeerIdentity));
676 peer_session->expecting_welcome = GNUNET_YES;
678 peer_session->next = plugin->sessions;
679 plugin->sessions = peer_session;
681 peer_session->messages = GNUNET_malloc(sizeof(struct MessageQueue));
682 peer_session->messages->msgbuf = GNUNET_malloc(msgbuf_size);
683 memcpy(peer_session->messages->msgbuf, msgbuf, msgbuf_size);
684 peer_session->messages->msgbuf_size = msgbuf_size;
685 peer_session->messages->next = NULL;
686 peer_session->messages->timeout = GNUNET_TIME_relative_to_absolute(timeout);
687 peer_session->messages->cont = cont;
688 peer_session->messages->cont_cls = cont_cls;
690 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
691 _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target));
693 run_gnunet_nat_client(plugin, addr, addrlen);
697 if (peer_session->expecting_welcome == GNUNET_NO) /* We are "connected" */
699 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);
701 else /* Haven't gotten a response from this peer, queue message */
703 temp_message = GNUNET_malloc(sizeof(struct MessageQueue));
704 temp_message->msgbuf = GNUNET_malloc(msgbuf_size);
705 memcpy(temp_message->msgbuf, msgbuf, msgbuf_size);
706 temp_message->msgbuf_size = msgbuf_size;
707 temp_message->timeout = GNUNET_TIME_relative_to_absolute(timeout);
708 temp_message->cont = cont;
709 temp_message->cont_cls = cont_cls;
710 temp_message->next = peer_session->messages;
711 peer_session->messages = temp_message;
715 else if (other_peer_natd == GNUNET_NO) /* Other peer not behind a NAT, so we can just send the message as is */
717 sent = udp_real_send(cls, udp_sock.desc, target, msgbuf, msgbuf_size, priority, timeout, addr, addrlen, cont, cont_cls);
719 else /* Other peer is NAT'd, but we don't want to play with them (or can't!) */
720 return GNUNET_SYSERR;
722 /* When GNUNET_SYSERR is returned from udp_real_send, we will still call
723 * the callback so must not return GNUNET_SYSERR!
724 * If we do, then transport context get freed twice. */
725 if (sent == GNUNET_SYSERR)
733 * Add the IP of our network interface to the list of
734 * our external IP addresses.
737 process_interfaces (void *cls,
740 const struct sockaddr *addr, socklen_t addrlen)
742 struct Plugin *plugin = cls;
744 struct sockaddr_in *v4;
745 struct sockaddr_in6 *v6;
746 struct sockaddr *addr_nat;
749 af = addr->sa_family;
752 v4 = (struct sockaddr_in *) addr;
753 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
755 v4->sin_port = htons (0); /* Indicates to receiver we are behind NAT */
757 else if (plugin->behind_nat == GNUNET_YES) /* We are behind NAT, but will advertise NAT and normal addresses */
759 addr_nat = GNUNET_malloc(addrlen);
760 memcpy(addr_nat, addr, addrlen);
761 v4 = (struct sockaddr_in *) addr_nat;
762 v4->sin_port = htons(plugin->port);
766 v4->sin_port = htons (plugin->port);
771 GNUNET_assert (af == AF_INET6);
772 v6 = (struct sockaddr_in6 *) addr;
773 if ((plugin->behind_nat == GNUNET_YES) && (plugin->only_nat_addresses == GNUNET_YES))
775 v6->sin6_port = htons (0);
777 else if (plugin->behind_nat == GNUNET_YES) /* We are behind NAT, but will advertise NAT and normal addresses */
779 addr_nat = GNUNET_malloc(addrlen);
780 memcpy(addr_nat, addr, addrlen);
781 v6 = (struct sockaddr_in6 *) addr_nat;
782 v6->sin6_port = htons(plugin->port);
786 v6->sin6_port = htons (plugin->port);
790 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
791 GNUNET_ERROR_TYPE_BULK,
792 "udp", _("Found address `%s' (%s)\n"),
793 GNUNET_a2s (addr, addrlen), name);
795 if (addr_nat != NULL)
797 plugin->env->notify_address (plugin->env->cls,
799 addr_nat, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
800 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
801 GNUNET_ERROR_TYPE_BULK,
802 "udp", _("Found NAT address `%s' (%s)\n"),
803 GNUNET_a2s (addr_nat, addrlen), name);
804 GNUNET_free(addr_nat);
807 plugin->env->notify_address (plugin->env->cls,
809 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
816 * Function called by the resolver for each address obtained from DNS
817 * for our own hostname. Add the addresses to the list of our
818 * external IP addresses.
821 * @param addr one of the addresses of the host, NULL for the last address
822 * @param addrlen length of the address
825 process_hostname_ips (void *cls,
826 const struct sockaddr *addr, socklen_t addrlen)
828 struct Plugin *plugin = cls;
832 plugin->hostname_dns = NULL;
835 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
840 * Send UDP probe messages or UDP keepalive messages, depending on the
841 * state of the connection.
843 * @param cls closure for this call (should be the main Plugin)
844 * @param tc task context for running this
847 send_udp_probe_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
849 struct UDP_NAT_Probes *probe = cls;
850 struct UDP_NAT_ProbeMessage *message;
851 struct Plugin *plugin = probe->plugin;
853 message = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessage));
854 message->header.size = htons(sizeof(struct UDP_NAT_ProbeMessage));
855 message->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE);
856 /* If they gave us a port, use that. If not, try our port. */
857 if (probe->port != 0)
858 probe->sock_addr.sin_port = htons(probe->port);
860 probe->sock_addr.sin_port = htons(plugin->port);
863 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
864 _("Sending a probe to port %d\n"), ntohs(probe->sock_addr.sin_port));
869 udp_real_send(plugin, udp_sock.desc, NULL,
870 (char *)message, ntohs(message->header.size), 0,
871 GNUNET_TIME_relative_get_unit(),
872 &probe->sock_addr, sizeof(probe->sock_addr),
873 &udp_probe_continuation, probe);
875 GNUNET_free(message);
880 * Continuation for probe sends. If the last probe was sent
881 * "successfully", schedule sending of another one. If not,
885 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result)
887 struct UDP_NAT_Probes *probe = cls;
888 struct Plugin *plugin = probe->plugin;
890 if ((result == GNUNET_OK) && (probe->count < MAX_PROBES))
893 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
894 _("Scheduling next probe for 10000 milliseconds\n"));
896 probe->task = GNUNET_SCHEDULER_add_delayed(plugin->env->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10000), &send_udp_probe_message, probe);
898 else /* Destroy the probe context. */
901 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
902 _("Sending probe didn't go well...\n"));
908 * Find probe message by address
910 * @param plugin the plugin for this transport
911 * @param address_string the ip address as a string
913 struct UDP_NAT_Probes *
914 find_probe(struct Plugin *plugin, char * address_string)
916 struct UDP_NAT_Probes *pos;
918 pos = plugin->probes;
920 if (strcmp(pos->address_string, address_string) == 0)
928 * @param cls the plugin handle
929 * @param tc the scheduling context (for rescheduling this function again)
931 * We have been notified that gnunet-nat-server has written something to stdout.
932 * Handle the output, then reschedule this function to be called again once
937 udp_plugin_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
939 struct Plugin *plugin = cls;
942 memset(&mybuf, 0, sizeof(mybuf));
944 struct UDP_NAT_Probes *temp_probe;
947 struct sockaddr_in in_addr;
949 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
952 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, &mybuf, sizeof(mybuf));
957 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
958 _("Finished reading from server stdout with code: %d\n"), bytes);
965 for (i = 0; i < sizeof(mybuf); i++)
967 if (mybuf[i] == '\n')
970 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
973 port_start = &mybuf[i + 1];
977 if (port_start != NULL)
978 port = atoi(port_start);
981 plugin->server_read_task =
982 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
983 GNUNET_TIME_UNIT_FOREVER_REL,
984 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
989 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
990 _("nat-server-read read: %s port %d\n"), &mybuf, port);
994 * We have received an ICMP response, ostensibly from a non-NAT'd peer
995 * that wants to connect to us! Send a message to establish a connection.
997 if (inet_pton(AF_INET, &mybuf[0], &in_addr.sin_addr) != 1)
1000 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp",
1001 _("nat-server-read malformed address\n"), &mybuf, port);
1003 plugin->server_read_task =
1004 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1005 GNUNET_TIME_UNIT_FOREVER_REL,
1006 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1010 temp_probe = find_probe(plugin, &mybuf[0]);
1012 if (temp_probe == NULL)
1014 temp_probe = GNUNET_malloc(sizeof(struct UDP_NAT_Probes));
1015 temp_probe->address_string = strdup(&mybuf[0]);
1016 temp_probe->sock_addr.sin_family = AF_INET;
1017 GNUNET_assert(inet_pton(AF_INET, &mybuf[0], &temp_probe->sock_addr.sin_addr) == 1);
1018 temp_probe->port = port;
1019 temp_probe->next = plugin->probes;
1020 temp_probe->plugin = plugin;
1021 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);
1022 plugin->probes = temp_probe;
1025 plugin->server_read_task =
1026 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1027 GNUNET_TIME_UNIT_FOREVER_REL,
1028 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1034 * Demultiplexer for UDP NAT messages
1036 * @param plugin the main plugin for this transport
1037 * @param sender from which peer the message was received
1038 * @param currhdr pointer to the header of the message
1039 * @param sender_addr the address from which the message was received
1040 * @param fromlen the length of the address
1041 * @param sockinfo which socket did we receive the message on
1044 udp_demultiplexer(struct Plugin *plugin, struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *currhdr, struct sockaddr_storage *sender_addr, socklen_t fromlen, struct UDP_Sock_Info *sockinfo)
1046 struct UDP_NAT_ProbeMessageReply *outgoing_probe_reply;
1047 struct UDP_NAT_ProbeMessageConfirmation *outgoing_probe_confirmation;
1049 char addr_buf[INET_ADDRSTRLEN];
1050 struct UDP_NAT_Probes *outgoing_probe;
1051 struct PeerSession *peer_session;
1052 struct MessageQueue *pending_message;
1053 struct MessageQueue *pending_message_temp;
1055 if (memcmp(sender, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1058 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1059 _("Received a message from myself, dropping!!!\n"));
1064 switch (ntohs(currhdr->type))
1066 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE:
1067 /* Send probe reply */
1068 outgoing_probe_reply = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageReply));
1069 outgoing_probe_reply->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageReply));
1070 outgoing_probe_reply->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY);
1073 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1074 _("Received a probe on listen port %d, sent_from port %d\n"), sockinfo->port, ntohs(((struct sockaddr_in *)sender_addr)->sin_port));
1077 udp_real_send(plugin, sockinfo->desc, NULL,
1078 (char *)outgoing_probe_reply,
1079 ntohs(outgoing_probe_reply->header.size), 0,
1080 GNUNET_TIME_relative_get_unit(),
1081 sender_addr, fromlen,
1085 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1086 _("Sent PROBE REPLY to port %d on outgoing port %d\n"), ntohs(((struct sockaddr_in *)sender_addr)->sin_port), sockinfo->port);
1088 GNUNET_free(outgoing_probe_reply);
1090 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY:
1091 /* Check for existing probe, check ports returned, send confirmation if all is well */
1093 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1094 _("Received PROBE REPLY from port %d on incoming port %d\n"), ntohs(((struct sockaddr_in *)sender_addr)->sin_port), sockinfo->port);
1096 if (sender_addr->ss_family == AF_INET)
1098 memset(&addr_buf, 0, sizeof(addr_buf));
1099 if (NULL == inet_ntop (AF_INET,
1100 &((struct sockaddr_in *) sender_addr)->sin_addr, addr_buf,
1103 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1106 outgoing_probe = find_probe(plugin, &addr_buf[0]);
1107 if (outgoing_probe != NULL)
1110 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1111 _("Sending confirmation that we were reached!\n"));
1113 outgoing_probe_confirmation = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1114 outgoing_probe_confirmation->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1115 outgoing_probe_confirmation->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM);
1117 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);
1119 if (outgoing_probe->task != GNUNET_SCHEDULER_NO_TASK)
1121 GNUNET_SCHEDULER_cancel(plugin->env->sched, outgoing_probe->task);
1122 outgoing_probe->task = GNUNET_SCHEDULER_NO_TASK;
1123 /* Schedule task to timeout and remove probe if confirmation not received */
1125 GNUNET_free(outgoing_probe_confirmation);
1130 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1131 _("Received a probe reply, but have no record of a sent probe!\n"));
1136 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM:
1137 peer_session = find_session(plugin, sender);
1139 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1140 _("Looking up peer session for peer %s\n"), GNUNET_i2s(sender));
1142 if (peer_session == NULL) /* Shouldn't this NOT happen? */
1145 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1146 _("Peer not in list, adding (THIS MAY BE A MISTAKE) %s\n"), GNUNET_i2s(sender));
1148 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
1149 peer_session->connect_addr = GNUNET_malloc(fromlen);
1150 memcpy(peer_session->connect_addr, sender_addr, fromlen);
1151 peer_session->connect_alen = fromlen;
1152 peer_session->plugin = plugin;
1153 peer_session->sock = sockinfo->desc;
1154 memcpy(&peer_session->target, sender, sizeof(struct GNUNET_PeerIdentity));
1155 peer_session->expecting_welcome = GNUNET_NO;
1157 peer_session->next = plugin->sessions;
1158 plugin->sessions = peer_session;
1160 peer_session->messages = NULL;
1162 else if (peer_session->expecting_welcome == GNUNET_YES)
1164 peer_session->expecting_welcome = GNUNET_NO;
1165 peer_session->sock = sockinfo->desc;
1166 ((struct sockaddr_in *)peer_session->connect_addr)->sin_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1168 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1169 _("Received a probe confirmation, will send to peer on port %d\n"), ntohs(((struct sockaddr_in *)peer_session->connect_addr)->sin_port));
1171 if (peer_session->messages != NULL)
1174 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1175 _("Received a probe confirmation, sending queued messages.\n"));
1177 pending_message = peer_session->messages;
1179 while (pending_message != NULL)
1182 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1183 _("sending queued message %d\n"), count);
1185 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);
1186 pending_message_temp = pending_message;
1187 pending_message = pending_message->next;
1188 GNUNET_free(pending_message_temp->msgbuf);
1189 GNUNET_free(pending_message_temp);
1191 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1192 _("finished sending queued message %d\n"), count);
1202 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1203 _("Received probe confirmation for already confirmed peer!\n"));
1206 /* Received confirmation, add peer with address/port specified */
1208 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_KEEPALIVE:
1209 /* Once we've sent NAT_PROBE_CONFIRM change to sending keepalives */
1210 /* If we receive these just ignore! */
1213 plugin->env->receive (plugin->env->cls, sender, currhdr, UDP_DIRECT_DISTANCE,
1214 NULL, (char *)sender_addr, fromlen);
1221 * @param cls the plugin handle
1222 * @param tc the scheduling context (for rescheduling this function again)
1224 * We have been notified that our writeset has something to read. We don't
1225 * know which socket needs to be read, so we have to check each one
1226 * Then reschedule this function to be called again once more is available.
1230 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1232 struct Plugin *plugin = cls;
1234 struct UDPMessage *msg;
1235 struct GNUNET_PeerIdentity *sender;
1236 unsigned int buflen;
1238 struct sockaddr_storage addr;
1244 const struct GNUNET_MessageHeader *currhdr;
1246 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1248 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1254 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock.desc);
1256 if (buflen == GNUNET_NO)
1259 buf = GNUNET_malloc (buflen);
1260 fromlen = sizeof (addr);
1261 memset (&addr, 0, fromlen);
1263 GNUNET_NETWORK_socket_recvfrom (udp_sock.desc, buf, buflen,
1264 (struct sockaddr *) &addr, &fromlen);
1267 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1268 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
1277 msg = (struct UDPMessage *) buf;
1280 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1281 ("header reports message size of %d, type %d\n"),
1282 ntohs (msg->header.size), ntohs (msg->header.type));
1284 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
1290 msgbuf = (char *)&msg[1];
1291 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1292 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1296 tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
1298 while (offset < tsize)
1300 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
1302 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1303 ("processing msg %d: type %d, size %d at offset %d\n"),
1304 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
1306 udp_demultiplexer(plugin, sender, currhdr, &addr, fromlen, &udp_sock);
1308 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1309 ("processing done msg %d: type %d, size %d at offset %d\n"),
1310 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
1312 offset += ntohs(currhdr->size);
1315 GNUNET_free_non_null (buf);
1316 GNUNET_free_non_null (sender);
1319 plugin->select_task =
1320 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1321 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1322 GNUNET_SCHEDULER_NO_TASK,
1323 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1324 NULL, &udp_plugin_select, plugin);
1329 * Create a slew of UDP sockets. If possible, use IPv6, otherwise
1332 * @param cls closure for server start, should be a struct Plugin *
1334 * @return number of sockets created or GNUNET_SYSERR on error
1337 udp_transport_server_start (void *cls)
1339 struct Plugin *plugin = cls;
1340 struct sockaddr_in serverAddrv4;
1341 struct sockaddr_in6 serverAddrv6;
1342 struct sockaddr *serverAddr;
1344 int sockets_created;
1346 sockets_created = 0;
1348 if (plugin->behind_nat == GNUNET_YES)
1350 /* Pipe to read from started processes stdout (on read end) */
1351 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES);
1352 if (plugin->server_stdout == NULL)
1353 return sockets_created;
1355 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1357 "Starting gnunet-nat-server process cmd: %s %s\n", "gnunet-nat-server", plugin->internal_address);
1359 /* Start the server process */
1360 plugin->server_pid = GNUNET_OS_start_process(NULL, plugin->server_stdout, "gnunet-nat-server", "gnunet-nat-server", plugin->internal_address, NULL);
1361 if (plugin->server_pid == GNUNET_SYSERR)
1364 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1366 "Failed to start gnunet-nat-server process\n");
1368 return GNUNET_SYSERR;
1370 /* Close the write end of the read pipe */
1371 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1373 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout, GNUNET_DISK_PIPE_END_READ);
1374 plugin->server_read_task =
1375 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1376 GNUNET_TIME_UNIT_FOREVER_REL,
1377 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1380 udp_sock.desc = NULL;
1383 udp_sock.desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
1384 if (NULL == udp_sock.desc)
1386 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
1387 return sockets_created;
1391 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1392 #if HAVE_SOCKADDR_IN_SIN_LEN
1393 serverAddrv4.sin_len = sizeof (serverAddrv4);
1395 serverAddrv4.sin_family = AF_INET;
1396 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1397 serverAddrv4.sin_port = htons (plugin->port);
1398 addrlen = sizeof (serverAddrv4);
1399 serverAddr = (struct sockaddr *) &serverAddrv4;
1401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1403 "Binding to port %d\n", ntohs(serverAddrv4.sin_port));
1405 while (GNUNET_NETWORK_socket_bind (udp_sock.desc, serverAddr, addrlen) !=
1408 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1410 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1412 "Binding failed, trying new port %d\n", ntohs(serverAddrv4.sin_port));
1415 udp_sock.port = ntohs(serverAddrv4.sin_port);
1420 if ((udp_sock.desc == NULL) && (GNUNET_YES !=
1421 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
1424 udp_sock.desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
1425 if (udp_sock.desc != NULL)
1427 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1428 #if HAVE_SOCKADDR_IN_SIN_LEN
1429 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1431 serverAddrv6.sin6_family = AF_INET6;
1432 serverAddrv6.sin6_addr = in6addr_any;
1433 serverAddrv6.sin6_port = htons (plugin->port);
1434 addrlen = sizeof (serverAddrv6);
1435 serverAddr = (struct sockaddr *) &serverAddrv6;
1440 plugin->rs = GNUNET_NETWORK_fdset_create ();
1442 GNUNET_NETWORK_fdset_zero (plugin->rs);
1445 GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock.desc);
1447 plugin->select_task =
1448 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1449 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1450 GNUNET_SCHEDULER_NO_TASK,
1451 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1452 NULL, &udp_plugin_select, plugin);
1454 return sockets_created;
1459 * Another peer has suggested an address for this peer and transport
1460 * plugin. Check that this could be a valid address. This function
1461 * is not expected to 'validate' the address in the sense of trying to
1462 * connect to it but simply to see if the binary format is technically
1463 * legal for establishing a connection.
1465 * @param cls closure, should be our handle to the Plugin
1466 * @param addr pointer to the address, may be modified (slightly)
1467 * @param addrlen length of addr
1468 * @return GNUNET_OK if this is a plausible address for this peer
1469 * and transport, GNUNET_SYSERR if not
1473 udp_check_address (void *cls, void *addr, size_t addrlen)
1475 struct Plugin *plugin = cls;
1476 char buf[sizeof (struct sockaddr_in6)];
1478 struct sockaddr_in *v4;
1479 struct sockaddr_in6 *v6;
1481 if ((addrlen != sizeof (struct sockaddr_in)) &&
1482 (addrlen != sizeof (struct sockaddr_in6)))
1484 GNUNET_break_op (0);
1485 return GNUNET_SYSERR;
1487 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1488 if (addrlen == sizeof (struct sockaddr_in))
1490 v4 = (struct sockaddr_in *) buf;
1491 v4->sin_port = htons (plugin->port);
1495 v6 = (struct sockaddr_in6 *) buf;
1496 v6->sin6_port = htons (plugin->port);
1500 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1502 "Informing transport service about my address `%s'.\n",
1503 GNUNET_a2s (addr, addrlen));
1510 * Append our port and forward the result.
1513 append_port (void *cls, const char *hostname)
1515 struct PrettyPrinterContext *ppc = cls;
1518 if (hostname == NULL)
1520 ppc->asc (ppc->asc_cls, NULL);
1524 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1525 ppc->asc (ppc->asc_cls, ret);
1531 * Convert the transports address to a nice, human-readable
1534 * @param cls closure
1535 * @param type name of the transport that generated the address
1536 * @param addr one of the addresses of the host, NULL for the last address
1537 * the specific address format depends on the transport
1538 * @param addrlen length of the address
1539 * @param numeric should (IP) addresses be displayed in numeric form?
1540 * @param timeout after how long should we give up?
1541 * @param asc function to call on each string
1542 * @param asc_cls closure for asc
1545 udp_plugin_address_pretty_printer (void *cls,
1550 struct GNUNET_TIME_Relative timeout,
1551 GNUNET_TRANSPORT_AddressStringCallback asc,
1554 struct Plugin *plugin = cls;
1555 const struct sockaddr_in *v4;
1556 const struct sockaddr_in6 *v6;
1557 struct PrettyPrinterContext *ppc;
1559 if ((addrlen != sizeof (struct sockaddr_in)) &&
1560 (addrlen != sizeof (struct sockaddr_in6)))
1562 /* invalid address */
1563 GNUNET_break_op (0);
1564 asc (asc_cls, NULL);
1567 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1569 ppc->asc_cls = asc_cls;
1570 if (addrlen == sizeof (struct sockaddr_in))
1572 v4 = (const struct sockaddr_in *) addr;
1573 ppc->port = ntohs (v4->sin_port);
1577 v6 = (const struct sockaddr_in6 *) addr;
1578 ppc->port = ntohs (v6->sin6_port);
1581 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1585 !numeric, timeout, &append_port, ppc);
1589 * Return the actual path to a file found in the current
1590 * PATH environment variable.
1592 * @param binary the name of the file to find
1595 get_path_from_PATH (char *binary)
1603 p = getenv ("PATH");
1606 path = GNUNET_strdup (p); /* because we write on it */
1607 buf = GNUNET_malloc (strlen (path) + 20);
1610 while (NULL != (end = strchr (pos, ':')))
1613 sprintf (buf, "%s/%s", pos, binary);
1614 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
1621 sprintf (buf, "%s/%s", pos, binary);
1622 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
1633 * Check whether the suid bit is set on a file.
1634 * Attempts to find the file using the current
1635 * PATH environment variable as a search path.
1637 * @param binary the name of the file to check
1640 check_gnunet_nat_binary(char *binary)
1642 struct stat statbuf;
1645 p = get_path_from_PATH (binary);
1648 if (0 != STAT (p, &statbuf))
1651 return GNUNET_SYSERR;
1654 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
1655 (statbuf.st_uid == 0) )
1661 * The exported method. Makes the core api available via a global and
1662 * returns the udp transport API.
1665 libgnunet_plugin_transport_udp_init (void *cls)
1667 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1668 unsigned long long mtu;
1669 unsigned long long port;
1670 struct GNUNET_TRANSPORT_PluginFunctions *api;
1671 struct Plugin *plugin;
1672 struct GNUNET_SERVICE_Context *service;
1673 int sockets_created;
1676 int only_nat_addresses;
1677 char *internal_address;
1678 char *external_address;
1679 struct sockaddr_in in_addr;
1681 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
1682 if (service == NULL)
1684 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
1685 ("Failed to start service for `%s' transport plugin.\n"),
1690 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
1694 /* We are behind nat (according to the user) */
1695 if (check_gnunet_nat_binary("gnunet-nat-server") == GNUNET_YES)
1696 behind_nat = GNUNET_YES;
1699 behind_nat = GNUNET_NO;
1700 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");
1704 behind_nat = GNUNET_NO; /* We are not behind nat! */
1706 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
1710 if (check_gnunet_nat_binary("gnunet-nat-client") == GNUNET_YES)
1711 allow_nat = GNUNET_YES; /* We will try to connect to NAT'd peers */
1714 allow_nat = GNUNET_NO;
1715 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");
1720 allow_nat = GNUNET_NO; /* We don't want to try to help NAT'd peers */
1722 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
1724 "ONLY_NAT_ADDRESSES"))
1725 only_nat_addresses = GNUNET_YES; /* We will only report our addresses as NAT'd */
1727 only_nat_addresses = GNUNET_NO; /* We will report our addresses as NAT'd and non-NAT'd */
1729 external_address = NULL;
1730 if (((GNUNET_YES == behind_nat) || (GNUNET_YES == allow_nat)) && (GNUNET_OK !=
1731 GNUNET_CONFIGURATION_get_value_string (env->cfg,
1734 &external_address)))
1736 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1739 ("Require EXTERNAL_ADDRESS for service `%s' in configuration (either BEHIND_NAT or ALLOW_NAT set to YES)!\n"),
1741 GNUNET_SERVICE_stop (service);
1745 if ((external_address != NULL) && (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1))
1747 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed EXTERNAL_ADDRESS %s given in configuration!\n", external_address);
1750 internal_address = NULL;
1751 if ((GNUNET_YES == behind_nat) && (GNUNET_OK !=
1752 GNUNET_CONFIGURATION_get_value_string (env->cfg,
1755 &internal_address)))
1757 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1760 ("Require INTERNAL_ADDRESS for service `%s' in configuration!\n"),
1762 GNUNET_SERVICE_stop (service);
1763 GNUNET_free_non_null(external_address);
1767 if ((internal_address != NULL) && (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1))
1769 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed INTERNAL_ADDRESS %s given in configuration!\n", internal_address);
1773 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1777 port = UDP_NAT_DEFAULT_PORT;
1778 else if (port > 65535)
1780 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1782 _("Given `%s' option is out of range: %llu > %u\n"),
1786 GNUNET_SERVICE_stop (service);
1787 GNUNET_free_non_null(external_address);
1788 GNUNET_free_non_null(internal_address);
1794 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1796 _("MTU %llu for `%s' is probably too low!\n"), mtu,
1799 plugin = GNUNET_malloc (sizeof (struct Plugin));
1800 plugin->external_address = external_address;
1801 plugin->internal_address = internal_address;
1802 plugin->port = port;
1803 plugin->behind_nat = behind_nat;
1804 plugin->allow_nat = allow_nat;
1805 plugin->only_nat_addresses = only_nat_addresses;
1808 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1811 api->send = &udp_plugin_send;
1812 api->disconnect = &udp_disconnect;
1813 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1814 api->check_address = &udp_check_address;
1816 plugin->service = service;
1818 if (plugin->behind_nat == GNUNET_NO)
1820 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1823 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1826 HOSTNAME_RESOLVE_TIMEOUT,
1827 &process_hostname_ips,
1830 if ((plugin->behind_nat == GNUNET_YES) && (inet_pton(AF_INET, plugin->external_address, &in_addr.sin_addr) == 1))
1832 in_addr.sin_port = htons(0);
1833 in_addr.sin_family = AF_INET;
1834 plugin->env->notify_address (plugin->env->cls,
1836 &in_addr, sizeof(in_addr), GNUNET_TIME_UNIT_FOREVER_REL);
1838 else if ((plugin->external_address != NULL) && (inet_pton(AF_INET, plugin->external_address, &in_addr.sin_addr) == 1))
1840 in_addr.sin_port = htons(plugin->port);
1841 in_addr.sin_family = AF_INET;
1842 plugin->env->notify_address (plugin->env->cls,
1844 &in_addr, sizeof(in_addr), GNUNET_TIME_UNIT_FOREVER_REL);
1847 sockets_created = udp_transport_server_start (plugin);
1849 GNUNET_assert (sockets_created == 1);
1855 libgnunet_plugin_transport_udp_done (void *cls)
1857 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1858 struct Plugin *plugin = api->cls;
1860 udp_transport_server_stop (plugin);
1861 if (NULL != plugin->hostname_dns)
1863 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1864 plugin->hostname_dns = NULL;
1867 GNUNET_SERVICE_stop (plugin->service);
1869 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1870 GNUNET_free (plugin);
1875 /* end of plugin_transport_udp.c */