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 or both is behind a NAT. This is based on pwnat
30 * (http://samy.pl/pwnat) created by Samy Kamkar. This transport will
31 * take external IP addresses for a NAT'd peer, start a server daemon
32 * which sends ICMP messages out to a predefined address. Then it forks
33 * a client process which sends ICMP death messages to the given external
34 * IP address for the other peer (which has already also started the
35 * server). If everything works according to plan, the transport will
36 * be given a port which has worked to punch into the other external
37 * addresses NAT to communicate with the other gnunet peer. Then keep
38 * alive messages will be sent every (???)ms or so to keep the tunnel
43 #include "gnunet_hello_lib.h"
44 #include "gnunet_connection_lib.h"
45 #include "gnunet_os_lib.h"
46 #include "gnunet_peerinfo_service.h"
47 #include "gnunet_protocols.h"
48 #include "gnunet_resolver_service.h"
49 #include "gnunet_server_lib.h"
50 #include "gnunet_service_lib.h"
51 #include "gnunet_signatures.h"
52 #include "gnunet_statistics_service.h"
53 #include "gnunet_transport_service.h"
54 #include "plugin_transport.h"
55 #include "transport.h"
57 #define DEBUG_UDP_NAT GNUNET_NO
60 * Transport cost to peer, always 1 for UDP (direct connection)
62 #define UDP_DIRECT_DISTANCE 1
65 * Handle for request of hostname resolution, non-NULL if pending.
67 static struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
70 * How long until we give up on transmitting the welcome message?
72 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
75 * How many ports do we want to listen on (and send on) in order to get replies?
77 * FIXME: make this value dynamic, specified by configuration
79 #define UDP_NAT_LISTEN_PORTS 257
82 * Starting port for listening and sending, eventually a config value
84 #define UDP_NAT_STARTING_PORT 22086
87 * UDP Message-Packet header.
94 struct GNUNET_MessageHeader header;
97 * What is the identity of the sender (GNUNET_hash of public key)
99 struct GNUNET_PeerIdentity sender;
105 * UDP NAT Probe message definition
107 struct UDP_NAT_ProbeMessage
112 struct GNUNET_MessageHeader header;
117 * UDP NAT Probe message reply definition
119 struct UDP_NAT_ProbeMessageReply
124 struct GNUNET_MessageHeader header;
130 * UDP NAT Probe message confirm definition
132 struct UDP_NAT_ProbeMessageConfirmation
137 struct GNUNET_MessageHeader header;
143 /* Forward definition */
146 struct PrettyPrinterContext
148 GNUNET_TRANSPORT_AddressStringCallback asc;
158 struct MessageQueue *next;
161 * Session this message belongs to
163 struct PeerSession *session;
166 * Actual message to be sent
171 * Size of message buffer to be sent
176 * When to discard this message
178 struct GNUNET_TIME_Absolute timeout;
181 * Continuation to call when this message goes out
183 GNUNET_TRANSPORT_TransmitContinuation cont;
186 * closure for continuation
199 * Stored in a linked list.
201 struct PeerSession *next;
204 * Pointer to the global plugin struct.
206 struct Plugin *plugin;
209 * To whom are we talking to (set to our identity
210 * if we are still waiting for the welcome message)
212 struct GNUNET_PeerIdentity target;
215 * Address of the other peer (either based on our 'connect'
216 * call or on our 'accept' call).
221 * Length of connect_addr.
226 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
228 int expecting_welcome;
231 * From which socket do we need to send to this peer?
233 struct GNUNET_NETWORK_Handle *sock;
236 * Queue of messages for this peer, in the case that
237 * we have to await a connection...
239 struct MessageQueue *messages;
244 struct UDP_NAT_Probes
250 struct UDP_NAT_Probes *next;
253 * Address string that the server process returned to us
255 char *address_string;
258 * Timeout for this set of probes
260 struct GNUNET_TIME_Absolute timeout;
263 * Current index (which port to send on next basically)
268 * The plugin this probe belongs to
270 struct Plugin *plugin;
273 * The task used to send these probes
275 GNUNET_SCHEDULER_TaskIdentifier task;
278 * Network address (always ipv4)
280 struct sockaddr_in sock_addr;
283 * The port to send this probe to, 0 to choose randomly
291 * Encapsulation of all of the state of the plugin.
298 struct GNUNET_TRANSPORT_PluginEnvironment *env;
301 * Handle for the statistics service.
303 struct GNUNET_STATISTICS_Handle *statistics;
306 * Handle to the network service.
308 struct GNUNET_SERVICE_Context *service;
311 * Session of peers with whom we are currently connected
313 struct PeerSession *sessions;
316 * Handle for request of hostname resolution, non-NULL if pending.
318 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
321 * ID of task used to update our addresses when one expires.
323 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
328 GNUNET_SCHEDULER_TaskIdentifier select_task;
331 * Starting port, will listen on num_ports (if available) starting
332 * at this number (or will listen only on this port if non-nat'd)
334 uint16_t starting_port;
337 * Starting port for sending out crazy messages
339 uint32_t random_starting_port;
342 * How many ports should we be using?
347 * The external address given to us by the user. Must be actual
348 * outside visible address for NAT punching to work.
350 char *external_address;
353 * The internal address given to us by the user (or discovered).
355 char *internal_address;
360 struct GNUNET_NETWORK_FDSet *rs;
363 * stdout pipe handle for the gnunet-nat-server process
365 struct GNUNET_DISK_PipeHandle *server_stdout;
368 * stdout file handle (for reading) for the gnunet-nat-server process
370 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
373 * ID of select gnunet-nat-server stdout read task
375 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
378 * Is this transport configured to be behind a NAT?
383 * The process id of the server process (if behind NAT)
390 struct UDP_NAT_Probes *probes;
397 /* The network handle */
398 struct GNUNET_NETWORK_Handle *desc;
400 /* The port we bound to */
404 /* *********** globals ************* */
407 * the socket that we transmit all data with
409 static struct UDP_Sock_Info *udp_nat_socks[UDP_NAT_LISTEN_PORTS];
413 * Forward declaration.
416 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result);
420 * Disconnect from a remote node. Clean up session if we have one for this peer
422 * @param cls closure for this call (should be handle to Plugin)
423 * @param target the peeridentity of the peer to disconnect
424 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
427 udp_nat_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
429 /** TODO: Implement! */
434 * Shutdown the server process (stop receiving inbound traffic). Maybe
437 * @param cls Handle to the plugin for this transport
439 * @return returns the number of sockets successfully closed,
440 * should equal the number of sockets successfully opened
443 udp_nat_transport_server_stop (void *cls)
445 struct Plugin *plugin = cls;
451 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
453 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
454 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
457 for (i = 0; i < plugin->num_ports; i++)
459 ok = GNUNET_NETWORK_socket_close (udp_nat_socks[i]->desc);
461 udp_nat_socks[i]->desc = NULL;
465 if (plugin->behind_nat == GNUNET_YES)
467 if (0 != PLIBC_KILL (plugin->server_pid, SIGTERM))
469 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
471 GNUNET_OS_process_wait (plugin->server_pid);
474 if (ret != GNUNET_OK)
475 return GNUNET_SYSERR;
481 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
483 struct PeerSession *pos;
485 pos = plugin->sessions;
488 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
498 * Actually send out the message, assume we've got the address and
499 * send_handle squared away!
502 * @param send_handle which handle to send message on
503 * @param target who should receive this message (ignored by UDP)
504 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
505 * @param msgbuf_size the size of the msgbuf to send
506 * @param priority how important is the message (ignored by UDP)
507 * @param timeout when should we time out (give up) if we can not transmit?
508 * @param addr the addr to send the message to, needs to be a sockaddr for us
509 * @param addrlen the len of addr
510 * @param cont continuation to call once the message has
511 * been transmitted (or if the transport is ready
512 * for the next transmission call; or if the
513 * peer disconnected...)
514 * @param cont_cls closure for cont
515 * @return the number of bytes written
518 udp_nat_real_send (void *cls,
519 struct GNUNET_NETWORK_Handle *send_handle,
520 const struct GNUNET_PeerIdentity *target,
523 unsigned int priority,
524 struct GNUNET_TIME_Relative timeout,
527 GNUNET_TRANSPORT_TransmitContinuation cont,
530 struct Plugin *plugin = cls;
531 struct UDPMessage *message;
535 if ((addr == NULL) || (addrlen == 0))
538 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
539 ("udp_nat_plugin_send called without address, returning!\n"));
541 cont (cont_cls, target, GNUNET_SYSERR);
542 return 0; /* Can never send if we don't have an address!! */
545 /* Build the message to be sent */
546 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
547 ssize = sizeof (struct UDPMessage) + msgbuf_size;
549 message->header.size = htons (ssize);
550 message->header.type = htons (0);
551 memcpy (&message->sender, plugin->env->my_identity,
552 sizeof (struct GNUNET_PeerIdentity));
553 memcpy (&message[1], msgbuf, msgbuf_size);
555 /* Actually send the message */
557 GNUNET_NETWORK_socket_sendto (send_handle, message, ssize,
563 if (sent == GNUNET_SYSERR)
564 cont (cont_cls, target, GNUNET_SYSERR);
567 cont (cont_cls, target, GNUNET_OK);
570 GNUNET_free (message);
575 * We learned about a peer (possibly behind NAT) so run the
576 * gnunet-nat-client to send a dummy ICMP response
578 * @param plugin the plugin for this transport
579 * @param addr the address of the peer
580 * @param addrlen the length of the address
583 run_gnunet_nat_client (struct Plugin *plugin, const char *addr, size_t addrlen)
585 char inet4[INET_ADDRSTRLEN];
586 char *address_as_string;
587 char *port_as_string;
589 const struct sockaddr *sa = (const struct sockaddr *)addr;
591 if (addrlen < sizeof (struct sockaddr))
593 switch (sa->sa_family)
596 if (addrlen != sizeof (struct sockaddr_in))
599 &((struct sockaddr_in *) sa)->sin_addr,
600 inet4, INET_ADDRSTRLEN);
601 address_as_string = GNUNET_strdup (inet4);
608 GNUNET_asprintf(&port_as_string, "%d", plugin->starting_port);
610 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
611 _("Running gnunet-nat-client with arguments: %s %s %d\n"), plugin->external_address, address_as_string, plugin->starting_port);
613 /* Start the server process */
614 pid = GNUNET_OS_start_process(NULL, NULL, "gnunet-nat-client", "gnunet-nat-client", plugin->external_address, address_as_string, port_as_string, NULL);
615 GNUNET_free(address_as_string);
616 GNUNET_free(port_as_string);
617 GNUNET_OS_process_wait (pid);
621 * Function that can be used by the transport service to transmit
622 * a message using the plugin.
625 * @param target who should receive this message (ignored by UDP)
626 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
627 * @param msgbuf_size the size of the msgbuf to send
628 * @param priority how important is the message (ignored by UDP)
629 * @param timeout when should we time out (give up) if we can not transmit?
630 * @param addr the addr to send the message to, needs to be a sockaddr for us
631 * @param addrlen the len of addr
632 * @param force_address not used, we had better have an address to send to
633 * because we are stateless!!
634 * @param cont continuation to call once the message has
635 * been transmitted (or if the transport is ready
636 * for the next transmission call; or if the
637 * peer disconnected...)
638 * @param cont_cls closure for cont
640 * @return the number of bytes written (may return 0 and the message can
641 * still be transmitted later!)
644 udp_nat_plugin_send (void *cls,
645 const struct GNUNET_PeerIdentity *target,
648 unsigned int priority,
649 struct GNUNET_TIME_Relative timeout,
653 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
655 struct Plugin *plugin = cls;
657 struct MessageQueue *temp_message;
658 struct PeerSession *peer_session;
659 struct sockaddr_in *sockaddr = (struct sockaddr_in *)addr;
662 other_peer_natd = GNUNET_NO;
663 if ((sockaddr->sin_family == AF_INET) && (ntohs(sockaddr->sin_port) == 0))
665 other_peer_natd = GNUNET_YES;
670 if (other_peer_natd == GNUNET_YES)
672 peer_session = find_session(plugin, target);
673 if (peer_session == NULL) /* We have a new peer to add */
676 * The first time, we can assume we have no knowledge of a
677 * working port for this peer, call the ICMP message sender
680 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
681 peer_session->connect_addr = GNUNET_malloc(addrlen);
682 memcpy(peer_session->connect_addr, addr, addrlen);
683 peer_session->connect_alen = addrlen;
684 peer_session->plugin = plugin;
685 peer_session->sock = NULL;
686 memcpy(&peer_session->target, target, sizeof(struct GNUNET_PeerIdentity));
687 peer_session->expecting_welcome = GNUNET_YES;
689 peer_session->next = plugin->sessions;
690 plugin->sessions = peer_session;
692 peer_session->messages = GNUNET_malloc(sizeof(struct MessageQueue));
693 peer_session->messages->msgbuf = GNUNET_malloc(msgbuf_size);
694 memcpy(peer_session->messages->msgbuf, msgbuf, msgbuf_size);
695 peer_session->messages->msgbuf_size = msgbuf_size;
696 peer_session->messages->next = NULL;
697 peer_session->messages->timeout = GNUNET_TIME_relative_to_absolute(timeout);
698 peer_session->messages->cont = cont;
699 peer_session->messages->cont_cls = cont_cls;
701 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
702 _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target));
704 run_gnunet_nat_client(plugin, addr, addrlen);
709 if (peer_session->expecting_welcome == GNUNET_NO) /* We are "connected" */
711 sent = udp_nat_real_send(cls, peer_session->sock, target, msgbuf, msgbuf_size, priority, timeout, peer_session->connect_addr, peer_session->connect_alen, cont, cont_cls);
713 else /* Haven't gotten a response from this peer, queue message */
715 temp_message = GNUNET_malloc(sizeof(struct MessageQueue));
716 temp_message->msgbuf = GNUNET_malloc(msgbuf_size);
717 memcpy(temp_message->msgbuf, msgbuf, msgbuf_size);
718 temp_message->msgbuf_size = msgbuf_size;
719 temp_message->timeout = GNUNET_TIME_relative_to_absolute(timeout);
720 temp_message->cont = cont;
721 temp_message->cont_cls = cont_cls;
722 temp_message->next = peer_session->messages;
723 peer_session->messages = temp_message;
727 else /* Other peer not behind a NAT, so we can just send the message as is */
729 sent = udp_nat_real_send(cls, udp_nat_socks[0]->desc, target, msgbuf, msgbuf_size, priority, timeout, addr, addrlen, cont, cont_cls);
737 * Add the IP of our network interface to the list of
738 * our external IP addresses.
741 process_interfaces (void *cls,
744 const struct sockaddr *addr, socklen_t addrlen)
746 struct Plugin *plugin = cls;
748 struct sockaddr_in *v4;
749 struct sockaddr_in6 *v6;
752 * FIXME: Do we want to advertise _all_ of our UDP NAT addresses?
753 * Probably not. Can we assume that we'll only send these out of band?
754 * Ostensibly we won't accept just any connection on these ports anyways
758 af = addr->sa_family;
761 v4 = (struct sockaddr_in *) addr;
762 if (plugin->behind_nat == GNUNET_YES)
764 GNUNET_assert(inet_pton(AF_INET, plugin->external_address, &v4->sin_addr) == GNUNET_OK);
765 v4->sin_port = htons (0); /* Indicates to receiver we are behind NAT */
768 v4->sin_port = htons (plugin->starting_port);
772 GNUNET_assert (af == AF_INET6);
773 v6 = (struct sockaddr_in6 *) addr;
774 if (plugin->behind_nat == GNUNET_YES)
775 v6->sin6_port = htons (0);
777 v6->sin6_port = htons (plugin->starting_port);
784 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
785 GNUNET_ERROR_TYPE_BULK,
786 "udp-nat", _("Found address `%s' (%s)\n"),
787 GNUNET_a2s (addr, addrlen), name);
788 plugin->env->notify_address (plugin->env->cls,
790 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
800 * Function called by the resolver for each address obtained from DNS
801 * for our own hostname. Add the addresses to the list of our
802 * external IP addresses.
805 * @param addr one of the addresses of the host, NULL for the last address
806 * @param addrlen length of the address
809 process_hostname_ips (void *cls,
810 const struct sockaddr *addr, socklen_t addrlen)
812 struct Plugin *plugin = cls;
816 plugin->hostname_dns = NULL;
819 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
824 * Send UDP probe messages or UDP keepalive messages, depending on the
825 * state of the connection.
827 * @param cls closure for this call (should be the main Plugin)
828 * @param tc task context for running this
831 send_udp_probe_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 struct UDP_NAT_Probes *probe = cls;
834 struct UDP_NAT_ProbeMessage *message;
835 struct Plugin *plugin = probe->plugin;
837 message = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessage));
838 message->header.size = htons(sizeof(struct UDP_NAT_ProbeMessage));
839 message->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE);
840 /* Try the agreed upon port first, then go in order starting with our randomly chosen port */
841 if (probe->index == 0)
843 if (probe->port != 0)
844 probe->sock_addr.sin_port = htons(probe->port);
846 probe->sock_addr.sin_port = htons(plugin->starting_port);
849 probe->sock_addr.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a non-root port */
851 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
852 _("Sending a probe to port %d\n"), ntohs(probe->sock_addr.sin_port));
854 if (probe->index + 1 < plugin->num_ports)
859 udp_nat_real_send(plugin, udp_nat_socks[probe->index]->desc, NULL,
860 (char *)message, ntohs(message->header.size), 0,
861 GNUNET_TIME_relative_get_unit(),
862 &probe->sock_addr, sizeof(probe->sock_addr),
863 &udp_probe_continuation, probe);
865 GNUNET_free(message);
870 * Continuation for probe sends. If the last probe was sent
871 * "successfully", schedule sending of another one. If not,
875 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result)
877 struct UDP_NAT_Probes *probe = cls;
878 struct Plugin *plugin = probe->plugin;
880 if (result == GNUNET_OK)
882 if (probe->index == 0)
885 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
886 _("Scheduling next probe for 10000 milliseconds\n"));
888 probe->task = GNUNET_SCHEDULER_add_delayed(plugin->env->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10000), &send_udp_probe_message, probe);
893 probe->task = GNUNET_SCHEDULER_add_delayed(plugin->env->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50), &send_udp_probe_message, probe);
896 else /* Destroy the probe context? */
899 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
900 _("Sending probe didn't go well...\n"));
907 * Find probe message by address
909 * @param plugin the plugin for this transport
910 * @param address_string the ip address as a string
912 struct UDP_NAT_Probes *
913 find_probe(struct Plugin *plugin, char * address_string)
915 struct UDP_NAT_Probes *pos;
917 pos = plugin->probes;
919 if (strcmp(pos->address_string, address_string) == 0)
927 * @param cls the plugin handle
928 * @param tc the scheduling context (for rescheduling this function again)
930 * We have been notified that gnunet-nat-server has written something to stdout.
931 * Handle the output, then reschedule this function to be called again once
936 udp_nat_plugin_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
938 struct Plugin *plugin = cls;
941 memset(&mybuf, 0, sizeof(mybuf));
943 struct UDP_NAT_Probes *temp_probe;
946 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, &mybuf, sizeof(mybuf));
951 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
952 _("Finished reading from server stdout with code: %d\n"), bytes);
959 for (i = 0; i < sizeof(mybuf); i++)
961 if (mybuf[i] == '\n')
964 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
967 port_start = &mybuf[i + 1];
971 if (port_start != NULL)
972 port = atoi(port_start);
975 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
976 _("nat-server-read read: %s port %d\n"), &mybuf, port);
979 /* Schedule sending of messages to peer on random ports */
980 temp_probe = find_probe(plugin, &mybuf[0]);
982 if (temp_probe == NULL)
984 temp_probe = GNUNET_malloc(sizeof(struct UDP_NAT_Probes));
985 temp_probe->address_string = strdup(&mybuf[0]);
986 temp_probe->sock_addr.sin_family = AF_INET;
987 GNUNET_assert(inet_pton(AF_INET, &mybuf[0], &temp_probe->sock_addr.sin_addr) == 1);
988 temp_probe->index = 0;
989 temp_probe->port = port;
990 temp_probe->next = plugin->probes;
991 temp_probe->plugin = plugin;
992 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);
993 plugin->probes = temp_probe;
996 plugin->server_read_task =
997 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
998 GNUNET_TIME_UNIT_FOREVER_REL,
999 plugin->server_stdout_handle, &udp_nat_plugin_server_read, plugin);
1005 * Demultiplexer for UDP NAT messages
1007 * @param plugin the main plugin for this transport
1008 * @param sender from which peer the message was received
1009 * @param currhdr pointer to the header of the message
1010 * @param sender_addr the address from which the message was received
1011 * @param fromlen the length of the address
1012 * @param sockinfo which socket did we receive the message on
1015 udp_nat_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)
1017 struct UDP_NAT_ProbeMessageReply *outgoing_probe_reply;
1018 struct UDP_NAT_ProbeMessageConfirmation *outgoing_probe_confirmation;
1020 char addr_buf[INET_ADDRSTRLEN];
1021 struct UDP_NAT_Probes *outgoing_probe;
1022 struct PeerSession *peer_session;
1023 struct MessageQueue *pending_message;
1024 struct MessageQueue *pending_message_temp;
1026 if (memcmp(sender, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1029 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
1030 _("Received a message from myself, dropping!!!\n"));
1035 switch (ntohs(currhdr->type))
1037 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE:
1038 /* Send probe reply */
1039 outgoing_probe_reply = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageReply));
1040 outgoing_probe_reply->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageReply));
1041 outgoing_probe_reply->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY);
1044 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
1045 _("Received a probe on listen port %d, sent_from port %d\n"), sockinfo->port, ntohs(((struct sockaddr_in *)sender_addr)->sin_port));
1048 udp_nat_real_send(plugin, sockinfo->desc, NULL,
1049 (char *)outgoing_probe_reply,
1050 ntohs(outgoing_probe_reply->header.size), 0,
1051 GNUNET_TIME_relative_get_unit(),
1052 sender_addr, fromlen,
1056 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
1057 _("Sent PROBE REPLY to port %d on outgoing port %d\n"), ntohs(((struct sockaddr_in *)sender_addr)->sin_port), sockinfo->port);
1059 GNUNET_free(outgoing_probe_reply);
1061 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY:
1062 /* Check for existing probe, check ports returned, send confirmation if all is well */
1064 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
1065 _("Received PROBE REPLY from port %d on incoming port %d\n"), ntohs(((struct sockaddr_in *)sender_addr)->sin_port), sockinfo->port);
1067 /* FIXME: use nonce, then IPv6 replies could work I think... */
1068 if (sender_addr->ss_family == AF_INET)
1070 memset(&addr_buf, 0, sizeof(addr_buf));
1071 inet_ntop(AF_INET, &((struct sockaddr_in *) sender_addr)->sin_addr, addr_buf, INET_ADDRSTRLEN);
1072 outgoing_probe = find_probe(plugin, &addr_buf[0]);
1073 if (outgoing_probe != NULL)
1076 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
1077 _("Sending confirmation that we were reached!\n"));
1079 outgoing_probe_confirmation = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1080 outgoing_probe_confirmation->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1081 outgoing_probe_confirmation->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM);
1083 udp_nat_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);
1085 if (outgoing_probe->task != GNUNET_SCHEDULER_NO_TASK)
1087 GNUNET_SCHEDULER_cancel(plugin->env->sched, outgoing_probe->task);
1088 outgoing_probe->task = GNUNET_SCHEDULER_NO_TASK;
1089 /* Schedule task to timeout and remove probe if confirmation not received */
1091 GNUNET_free(outgoing_probe_confirmation);
1096 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp-nat",
1097 _("Received a probe reply, but have no record of a sent probe!\n"));
1102 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM:
1103 peer_session = find_session(plugin, sender);
1105 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
1106 _("Looking up peer session for peer %s\n"), GNUNET_i2s(sender));
1108 if (peer_session == NULL) /* Shouldn't this NOT happen? */
1111 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat",
1112 _("Peer not in list, adding (THIS MAY BE A MISTAKE) %s\n"), GNUNET_i2s(sender));
1114 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
1115 peer_session->connect_addr = GNUNET_malloc(fromlen);
1116 memcpy(peer_session->connect_addr, sender_addr, fromlen);
1117 peer_session->connect_alen = fromlen;
1118 peer_session->plugin = plugin;
1119 peer_session->sock = sockinfo->desc;
1120 memcpy(&peer_session->target, sender, sizeof(struct GNUNET_PeerIdentity));
1121 peer_session->expecting_welcome = GNUNET_NO;
1123 peer_session->next = plugin->sessions;
1124 plugin->sessions = peer_session;
1126 peer_session->messages = NULL;
1128 else if (peer_session->expecting_welcome == GNUNET_YES)
1130 peer_session->expecting_welcome = GNUNET_NO;
1131 /* FIXME: There is no way to find this based on receiving port at the moment! */
1132 peer_session->sock = sockinfo->desc; /* This may matter, not sure right now... */
1133 ((struct sockaddr_in *)peer_session->connect_addr)->sin_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1135 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp-nat",
1136 _("Received a probe confirmation, will send to peer on port %d\n"), ntohs(((struct sockaddr_in *)peer_session->connect_addr)->sin_port));
1138 if (peer_session->messages != NULL)
1141 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp-nat",
1142 _("Received a probe confirmation, sending queued messages.\n"));
1144 pending_message = peer_session->messages;
1146 while (pending_message != NULL)
1149 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp-nat",
1150 _("sending queued message %d\n"), count);
1152 udp_nat_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);
1153 pending_message_temp = pending_message;
1154 pending_message = pending_message->next;
1155 GNUNET_free(pending_message_temp->msgbuf);
1156 GNUNET_free(pending_message_temp);
1158 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp-nat",
1159 _("finished sending queued message %d\n"), count);
1169 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp-nat",
1170 _("Received probe confirmation for already confirmed peer!\n"));
1173 /* Received confirmation, add peer with address/port specified */
1175 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_KEEPALIVE:
1176 /* Once we've sent NAT_PROBE_CONFIRM change to sending keepalives */
1177 /* If we receive these just ignore! */
1180 plugin->env->receive (plugin->env->cls, sender, currhdr, UDP_DIRECT_DISTANCE, (char *)sender_addr, fromlen);
1187 * @param cls the plugin handle
1188 * @param tc the scheduling context (for rescheduling this function again)
1190 * We have been notified that our writeset has something to read. We don't
1191 * know which socket needs to be read, so we have to check each one
1192 * Then reschedule this function to be called again once more is available.
1196 udp_nat_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1198 struct Plugin *plugin = cls;
1200 struct UDPMessage *msg;
1201 struct GNUNET_PeerIdentity *sender;
1202 unsigned int buflen;
1204 struct sockaddr_storage addr;
1211 const struct GNUNET_MessageHeader *currhdr;
1216 for (i = 0; i < plugin->num_ports; i++)
1218 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_nat_socks[i]->desc);
1220 if (buflen == GNUNET_NO)
1223 buf = GNUNET_malloc (buflen);
1224 fromlen = sizeof (addr);
1225 memset (&addr, 0, fromlen);
1227 GNUNET_NETWORK_socket_recvfrom (udp_nat_socks[i]->desc, buf, buflen,
1228 (struct sockaddr *) &addr, &fromlen);
1231 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp-nat", _
1232 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
1241 msg = (struct UDPMessage *) buf;
1244 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1245 ("header reports message size of %d, type %d\n"),
1246 ntohs (msg->header.size), ntohs (msg->header.type));
1248 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
1254 msgbuf = (char *)&msg[1];
1255 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1256 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1260 tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
1262 while (offset < tsize)
1264 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
1266 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1267 ("processing msg %d: type %d, size %d at offset %d\n"),
1268 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
1270 udp_nat_demultiplexer(plugin, sender, currhdr, &addr, fromlen, udp_nat_socks[i]);
1272 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1273 ("processing done msg %d: type %d, size %d at offset %d\n"),
1274 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
1276 offset += ntohs(currhdr->size);
1279 GNUNET_free_non_null (buf);
1280 GNUNET_free_non_null (sender);
1283 plugin->select_task =
1284 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1285 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1286 GNUNET_SCHEDULER_NO_TASK,
1287 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1288 NULL, &udp_nat_plugin_select, plugin);
1293 * Create a slew of UDP sockets. If possible, use IPv6, otherwise
1296 * @param cls closure for server start, should be a struct Plugin *
1298 * @return number of sockets created or GNUNET_SYSERR on error
1301 udp_nat_transport_server_start (void *cls)
1303 struct Plugin *plugin = cls;
1304 struct sockaddr_in serverAddrv4;
1306 struct sockaddr_in6 serverAddrv6;
1308 struct sockaddr *serverAddr;
1311 int sockets_created;
1313 /* Pipe to read from started processes stdout (on read end) */
1314 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES);
1316 sockets_created = 0;
1317 if (plugin->server_stdout == NULL)
1318 return sockets_created;
1320 if (plugin->behind_nat == GNUNET_YES)
1323 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1325 "Starting gnunet-nat-server process\n");
1327 /* Start the server process */
1328 plugin->server_pid = GNUNET_OS_start_process(NULL, plugin->server_stdout, "gnunet-nat-server", "gnunet-nat-server", plugin->internal_address, NULL);
1329 if (plugin->server_pid == GNUNET_SYSERR)
1330 return GNUNET_SYSERR;
1331 /* Close the write end of the read pipe */
1332 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1334 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout, GNUNET_DISK_PIPE_END_READ);
1335 plugin->server_read_task =
1336 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1337 GNUNET_TIME_UNIT_FOREVER_REL,
1338 plugin->server_stdout_handle, &udp_nat_plugin_server_read, plugin);
1341 /* Open all our sockets for reading/writing */
1342 for (i = 0; i < plugin->num_ports; i++)
1344 udp_nat_socks[i] = GNUNET_malloc(sizeof(struct UDP_Sock_Info));
1345 udp_nat_socks[i]->desc = NULL;
1348 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
1351 udp_nat_socks[i] = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
1352 if (udp_nat_socks[i] != NULL)
1354 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1355 #if HAVE_SOCKADDR_IN_SIN_LEN
1356 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1358 serverAddrv6.sin6_family = AF_INET6;
1359 serverAddrv6.sin6_addr = in6addr_any;
1360 serverAddrv6.sin6_port = htons (i + plugin->starting_port);
1361 addrlen = sizeof (serverAddrv6);
1362 serverAddr = (struct sockaddr *) &serverAddrv6;
1366 if (NULL == udp_nat_socks[i]->desc)
1368 udp_nat_socks[i]->desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
1369 if (NULL == udp_nat_socks[i]->desc)
1371 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat", "socket");
1372 return sockets_created;
1376 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1377 #if HAVE_SOCKADDR_IN_SIN_LEN
1378 serverAddrv4.sin_len = sizeof (serverAddrv4);
1380 serverAddrv4.sin_family = AF_INET;
1381 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1383 serverAddrv4.sin_port = htons (plugin->starting_port);
1385 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a non-root port */
1386 addrlen = sizeof (serverAddrv4);
1387 serverAddr = (struct sockaddr *) &serverAddrv4;
1389 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1391 "Binding to port %d\n", ntohs(serverAddrv4.sin_port));
1393 while (GNUNET_NETWORK_socket_bind (udp_nat_socks[i]->desc, serverAddr, addrlen) !=
1396 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1398 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1400 "Binding failed, trying new port %d\n", ntohs(serverAddrv4.sin_port));
1403 udp_nat_socks[i]->port = ntohs(serverAddrv4.sin_port);
1410 plugin->rs = GNUNET_NETWORK_fdset_create ();
1412 GNUNET_NETWORK_fdset_zero (plugin->rs);
1414 for (i = 0; i < plugin->num_ports; i++)
1416 GNUNET_NETWORK_fdset_set (plugin->rs, udp_nat_socks[i]->desc);
1419 plugin->select_task =
1420 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1421 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1422 GNUNET_SCHEDULER_NO_TASK,
1423 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1424 NULL, &udp_nat_plugin_select, plugin);
1426 return sockets_created;
1431 * Check if the given port is plausible (must be either
1432 * our listen port or our advertised port). If it is
1433 * neither, we return one of these two ports at random.
1435 * @return either in_port or a more plausible port
1438 check_port (struct Plugin *plugin, uint16_t in_port)
1441 /* FIXME: remember what ports we are using to better respond to this */
1444 for (i = plugin->starting_port; i < plugin->num_ports + plugin->starting_port; i++)
1450 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1451 plugin->num_ports) + plugin->starting_port;
1457 * Another peer has suggested an address for this peer and transport
1458 * plugin. Check that this could be a valid address. This function
1459 * is not expected to 'validate' the address in the sense of trying to
1460 * connect to it but simply to see if the binary format is technically
1461 * legal for establishing a connection.
1463 * @param cls closure, should be our handle to the Plugin
1464 * @param addr pointer to the address, may be modified (slightly)
1465 * @param addrlen length of addr
1466 * @return GNUNET_OK if this is a plausible address for this peer
1467 * and transport, GNUNET_SYSERR if not
1471 udp_nat_check_address (void *cls, void *addr, size_t addrlen)
1473 struct Plugin *plugin = cls;
1474 char buf[sizeof (struct sockaddr_in6)];
1476 struct sockaddr_in *v4;
1478 struct sockaddr_in6 *v6;
1481 if ((addrlen != sizeof (struct sockaddr_in)) &&
1482 (addrlen != sizeof (struct sockaddr_in6)))
1484 if (addrlen != sizeof (struct sockaddr_in))
1487 GNUNET_break_op (0);
1488 return GNUNET_SYSERR;
1490 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1491 if (addrlen == sizeof (struct sockaddr_in))
1493 v4 = (struct sockaddr_in *) buf;
1494 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1499 v6 = (struct sockaddr_in6 *) buf;
1500 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1504 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1506 "Informing transport service about my address `%s'.\n",
1507 GNUNET_a2s (addr, addrlen));
1514 * Append our port and forward the result.
1517 append_port (void *cls, const char *hostname)
1519 struct PrettyPrinterContext *ppc = cls;
1522 if (hostname == NULL)
1524 ppc->asc (ppc->asc_cls, NULL);
1528 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1529 ppc->asc (ppc->asc_cls, ret);
1535 * Convert the transports address to a nice, human-readable
1538 * @param cls closure
1539 * @param type name of the transport that generated the address
1540 * @param addr one of the addresses of the host, NULL for the last address
1541 * the specific address format depends on the transport
1542 * @param addrlen length of the address
1543 * @param numeric should (IP) addresses be displayed in numeric form?
1544 * @param timeout after how long should we give up?
1545 * @param asc function to call on each string
1546 * @param asc_cls closure for asc
1549 udp_nat_plugin_address_pretty_printer (void *cls,
1554 struct GNUNET_TIME_Relative timeout,
1555 GNUNET_TRANSPORT_AddressStringCallback asc,
1558 struct Plugin *plugin = cls;
1559 const struct sockaddr_in *v4;
1560 const struct sockaddr_in6 *v6;
1561 struct PrettyPrinterContext *ppc;
1563 if ((addrlen != sizeof (struct sockaddr_in)) &&
1564 (addrlen != sizeof (struct sockaddr_in6)))
1566 /* invalid address */
1567 GNUNET_break_op (0);
1568 asc (asc_cls, NULL);
1571 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1573 ppc->asc_cls = asc_cls;
1574 if (addrlen == sizeof (struct sockaddr_in))
1576 v4 = (const struct sockaddr_in *) addr;
1577 ppc->port = ntohs (v4->sin_port);
1581 v6 = (const struct sockaddr_in6 *) addr;
1582 ppc->port = ntohs (v6->sin6_port);
1585 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1589 !numeric, timeout, &append_port, ppc);
1594 * The exported method. Makes the core api available via a global and
1595 * returns the udp transport API.
1598 libgnunet_plugin_transport_udp_nat_init (void *cls)
1600 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1601 unsigned long long mtu;
1602 unsigned long long starting_port;
1603 struct GNUNET_TRANSPORT_PluginFunctions *api;
1604 struct Plugin *plugin;
1605 struct GNUNET_SERVICE_Context *service;
1606 int sockets_created;
1608 char *internal_address;
1609 char *external_address;
1611 service = GNUNET_SERVICE_start ("transport-udp-nat", env->sched, env->cfg);
1612 if (service == NULL)
1614 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp_nat", _
1615 ("Failed to start service for `%s' transport plugin.\n"),
1621 GNUNET_CONFIGURATION_get_value_string (env->cfg,
1622 "transport-udp-nat",
1626 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1629 ("Require EXTERNAL_ADDRESS for service `%s' in configuration!\n"),
1631 GNUNET_SERVICE_stop (service);
1636 GNUNET_CONFIGURATION_get_value_string (env->cfg,
1637 "transport-udp-nat",
1641 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1644 ("Require INTERNAL_ADDRESS for service `%s' in configuration!\n"),
1646 GNUNET_SERVICE_stop (service);
1647 GNUNET_free_non_null(external_address);
1651 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
1652 "transport-udp-nat",
1654 behind_nat = GNUNET_NO; /* We are not behind nat, or so says the user! */
1656 behind_nat = GNUNET_YES; /* Assume we are behind nat (default) */
1659 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1660 "transport-udp-nat",
1663 starting_port = UDP_NAT_STARTING_PORT;
1664 else if (starting_port > 65535)
1666 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1668 _("Given `%s' option is out of range: %llu > %u\n"),
1672 GNUNET_SERVICE_stop (service);
1673 GNUNET_free_non_null(external_address);
1674 GNUNET_free_non_null(internal_address);
1680 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1682 _("MTU %llu for `%s' is probably too low!\n"), mtu,
1685 plugin = GNUNET_malloc (sizeof (struct Plugin));
1686 plugin->external_address = external_address;
1687 plugin->internal_address = internal_address;
1688 plugin->starting_port = (uint16_t) UDP_NAT_STARTING_PORT;
1689 plugin->behind_nat = behind_nat;
1690 if (plugin->behind_nat == GNUNET_NO)
1691 plugin->num_ports = 1; /* Only use one port/socket */
1693 plugin->num_ports = UDP_NAT_LISTEN_PORTS;
1696 plugin->statistics = NULL;
1697 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1700 api->send = &udp_nat_plugin_send;
1701 api->disconnect = &udp_nat_disconnect;
1702 api->address_pretty_printer = &udp_nat_plugin_address_pretty_printer;
1703 api->check_address = &udp_nat_check_address;
1705 plugin->service = service;
1707 plugin->random_starting_port = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000;
1709 /* FIXME: do the two calls below periodically again and
1710 not just once (since the info we get might change...) */
1711 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1712 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1715 HOSTNAME_RESOLVE_TIMEOUT,
1716 &process_hostname_ips,
1719 sockets_created = udp_nat_transport_server_start (plugin);
1721 GNUNET_assert (sockets_created == plugin->num_ports);
1728 libgnunet_plugin_transport_udp_nat_done (void *cls)
1730 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1731 struct Plugin *plugin = api->cls;
1733 udp_nat_transport_server_stop (plugin);
1734 if (NULL != hostname_dns)
1736 GNUNET_RESOLVER_request_cancel (hostname_dns);
1737 hostname_dns = NULL;
1739 GNUNET_SERVICE_stop (plugin->service);
1741 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1742 GNUNET_free (plugin);
1747 /* end of plugin_transport_udp.c */