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 * 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 * Starting port for listening and sending, eventually a config value
77 #define UDP_NAT_DEFAULT_PORT 22086
80 * UDP Message-Packet header.
87 struct GNUNET_MessageHeader header;
90 * What is the identity of the sender (GNUNET_hash of public key)
92 struct GNUNET_PeerIdentity sender;
97 /* Forward definition */
100 struct PrettyPrinterContext
102 GNUNET_TRANSPORT_AddressStringCallback asc;
112 struct MessageQueue *next;
115 * Session this message belongs to
117 struct PeerSession *session;
120 * Actual message to be sent
125 * Size of message buffer to be sent
130 * When to discard this message
132 struct GNUNET_TIME_Absolute timeout;
135 * Continuation to call when this message goes out
137 GNUNET_TRANSPORT_TransmitContinuation cont;
140 * closure for continuation
147 * UDP NAT Probe message definition
149 struct UDP_NAT_ProbeMessage
154 struct GNUNET_MessageHeader header;
159 * UDP NAT Probe message reply definition
161 struct UDP_NAT_ProbeMessageReply
166 struct GNUNET_MessageHeader header;
172 * UDP NAT Probe message confirm definition
174 struct UDP_NAT_ProbeMessageConfirmation
179 struct GNUNET_MessageHeader header;
192 * Stored in a linked list.
194 struct PeerSession *next;
197 * Pointer to the global plugin struct.
199 struct Plugin *plugin;
202 * To whom are we talking to (set to our identity
203 * if we are still waiting for the welcome message)
205 struct GNUNET_PeerIdentity target;
208 * Address of the other peer (either based on our 'connect'
209 * call or on our 'accept' call).
214 * Length of connect_addr.
219 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
221 int expecting_welcome;
224 * From which socket do we need to send to this peer?
226 struct GNUNET_NETWORK_Handle *sock;
229 * Queue of messages for this peer, in the case that
230 * we have to await a connection...
232 struct MessageQueue *messages;
236 struct UDP_NAT_Probes
242 struct UDP_NAT_Probes *next;
245 * Address string that the server process returned to us
247 char *address_string;
250 * Timeout for this set of probes
252 struct GNUNET_TIME_Absolute timeout;
255 * Count of how many probes we've attempted
260 * The plugin this probe belongs to
262 struct Plugin *plugin;
265 * The task used to send these probes
267 GNUNET_SCHEDULER_TaskIdentifier task;
270 * Network address (always ipv4)
272 struct sockaddr_in sock_addr;
275 * The port to send this probe to, 0 to choose randomly
283 * Encapsulation of all of the state of the plugin.
290 struct GNUNET_TRANSPORT_PluginEnvironment *env;
293 * Handle to the network service.
295 struct GNUNET_SERVICE_Context *service;
298 * Session of peers with whom we are currently connected
300 struct PeerSession *sessions;
303 * Handle for request of hostname resolution, non-NULL if pending.
305 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
308 * ID of task used to update our addresses when one expires.
310 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
315 GNUNET_SCHEDULER_TaskIdentifier select_task;
323 * The external address given to us by the user. Must be actual
324 * outside visible address for NAT punching to work.
326 char *external_address;
329 * The internal address given to us by the user (or discovered).
331 char *internal_address;
336 struct GNUNET_NETWORK_FDSet *rs;
339 * stdout pipe handle for the gnunet-nat-server process
341 struct GNUNET_DISK_PipeHandle *server_stdout;
344 * stdout file handle (for reading) for the gnunet-nat-server process
346 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
349 * ID of select gnunet-nat-server stdout read task
351 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
354 * Is this transport configured to be behind a NAT?
359 * The process id of the server process (if behind NAT)
366 struct UDP_NAT_Probes *probes;
373 /* The network handle */
374 struct GNUNET_NETWORK_Handle *desc;
376 /* The port we bound to */
380 /* *********** globals ************* */
383 * the socket that we transmit all data with
385 static struct UDP_Sock_Info udp_sock;
389 * Forward declaration.
392 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result);
396 * Disconnect from a remote node. Clean up session if we have one for this peer
398 * @param cls closure for this call (should be handle to Plugin)
399 * @param target the peeridentity of the peer to disconnect
400 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
403 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
405 /** TODO: Implement! */
410 * Shutdown the server process (stop receiving inbound traffic). Maybe
413 * @param cls Handle to the plugin for this transport
415 * @return returns the number of sockets successfully closed,
416 * should equal the number of sockets successfully opened
419 udp_transport_server_stop (void *cls)
421 struct Plugin *plugin = cls;
426 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
428 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
429 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
433 ok = GNUNET_NETWORK_socket_close (udp_sock.desc);
435 udp_sock.desc = NULL;
439 if (plugin->behind_nat == GNUNET_YES)
441 if (0 != PLIBC_KILL (plugin->server_pid, SIGTERM))
443 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
445 GNUNET_OS_process_wait (plugin->server_pid);
448 if (ret != GNUNET_OK)
449 return GNUNET_SYSERR;
455 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
457 struct PeerSession *pos;
459 pos = plugin->sessions;
462 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
472 * Actually send out the message, assume we've got the address and
473 * send_handle squared away!
476 * @param send_handle which handle to send message on
477 * @param target who should receive this message (ignored by UDP)
478 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
479 * @param msgbuf_size the size of the msgbuf to send
480 * @param priority how important is the message (ignored by UDP)
481 * @param timeout when should we time out (give up) if we can not transmit?
482 * @param addr the addr to send the message to, needs to be a sockaddr for us
483 * @param addrlen the len of addr
484 * @param cont continuation to call once the message has
485 * been transmitted (or if the transport is ready
486 * for the next transmission call; or if the
487 * peer disconnected...)
488 * @param cont_cls closure for cont
489 * @return the number of bytes written
492 udp_real_send (void *cls,
493 struct GNUNET_NETWORK_Handle *send_handle,
494 const struct GNUNET_PeerIdentity *target,
497 unsigned int priority,
498 struct GNUNET_TIME_Relative timeout,
501 GNUNET_TRANSPORT_TransmitContinuation cont,
504 struct Plugin *plugin = cls;
505 struct UDPMessage *message;
509 if ((addr == NULL) || (addrlen == 0))
512 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
513 ("udp_plugin_send called without address, returning!\n"));
515 cont (cont_cls, target, GNUNET_SYSERR);
516 return 0; /* Can never send if we don't have an address!! */
519 /* Build the message to be sent */
520 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
521 ssize = sizeof (struct UDPMessage) + msgbuf_size;
523 message->header.size = htons (ssize);
524 message->header.type = htons (0);
525 memcpy (&message->sender, plugin->env->my_identity,
526 sizeof (struct GNUNET_PeerIdentity));
527 memcpy (&message[1], msgbuf, msgbuf_size);
529 /* Actually send the message */
531 GNUNET_NETWORK_socket_sendto (send_handle, message, ssize,
537 if (sent == GNUNET_SYSERR)
538 cont (cont_cls, target, GNUNET_SYSERR);
541 cont (cont_cls, target, GNUNET_OK);
545 GNUNET_free (message);
550 * We learned about a peer (possibly behind NAT) so run the
551 * gnunet-nat-client to send dummy ICMP responses
553 * @param plugin the plugin for this transport
554 * @param addr the address of the peer
555 * @param addrlen the length of the address
558 run_gnunet_nat_client (struct Plugin *plugin, const char *addr, size_t addrlen)
560 char inet4[INET_ADDRSTRLEN];
561 char *address_as_string;
562 char *port_as_string;
564 const struct sockaddr *sa = (const struct sockaddr *)addr;
566 if (addrlen < sizeof (struct sockaddr))
568 switch (sa->sa_family)
571 if (addrlen != sizeof (struct sockaddr_in))
574 &((struct sockaddr_in *) sa)->sin_addr,
575 inet4, INET_ADDRSTRLEN);
576 address_as_string = GNUNET_strdup (inet4);
583 GNUNET_asprintf(&port_as_string, "%d", plugin->port);
585 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
586 _("Running gnunet-nat-client with arguments: %s %s %d\n"), plugin->external_address, address_as_string, plugin->port);
588 /* Start the server process */
589 pid = GNUNET_OS_start_process(NULL, NULL, "gnunet-nat-client", "gnunet-nat-client", plugin->external_address, address_as_string, port_as_string, NULL);
590 GNUNET_free(address_as_string);
591 GNUNET_free(port_as_string);
592 GNUNET_OS_process_wait (pid);
596 * Function that can be used by the transport service to transmit
597 * a message using the plugin.
600 * @param target who should receive this message (ignored by UDP)
601 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
602 * @param msgbuf_size the size of the msgbuf to send
603 * @param priority how important is the message (ignored by UDP)
604 * @param timeout when should we time out (give up) if we can not transmit?
605 * @param session identifier used for this session (can be NULL)
606 * @param addr the addr to send the message to, needs to be a sockaddr for us
607 * @param addrlen the len of addr
608 * @param force_address not used, we had better have an address to send to
609 * because we are stateless!!
610 * @param cont continuation to call once the message has
611 * been transmitted (or if the transport is ready
612 * for the next transmission call; or if the
613 * peer disconnected...)
614 * @param cont_cls closure for cont
616 * @return the number of bytes written (may return 0 and the message can
617 * still be transmitted later!)
620 udp_plugin_send (void *cls,
621 const struct GNUNET_PeerIdentity *target,
624 unsigned int priority,
625 struct GNUNET_TIME_Relative timeout,
626 struct Session *session,
630 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
632 struct Plugin *plugin = cls;
634 struct MessageQueue *temp_message;
635 struct PeerSession *peer_session;
636 struct sockaddr_in *sockaddr = (struct sockaddr_in *)addr;
639 GNUNET_assert (NULL == session);
640 other_peer_natd = GNUNET_NO;
641 if ((sockaddr->sin_family == AF_INET) && (ntohs(sockaddr->sin_port) == 0))
643 other_peer_natd = GNUNET_YES;
648 if (other_peer_natd == GNUNET_YES)
650 peer_session = find_session(plugin, target);
651 if (peer_session == NULL) /* We have a new peer to add */
654 * The first time, we can assume we have no knowledge of a
655 * working port for this peer, call the ICMP/UDP message sender
658 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
659 peer_session->connect_addr = GNUNET_malloc(addrlen);
660 memcpy(peer_session->connect_addr, addr, addrlen);
661 peer_session->connect_alen = addrlen;
662 peer_session->plugin = plugin;
663 peer_session->sock = NULL;
664 memcpy(&peer_session->target, target, sizeof(struct GNUNET_PeerIdentity));
665 peer_session->expecting_welcome = GNUNET_YES;
667 peer_session->next = plugin->sessions;
668 plugin->sessions = peer_session;
670 peer_session->messages = GNUNET_malloc(sizeof(struct MessageQueue));
671 peer_session->messages->msgbuf = GNUNET_malloc(msgbuf_size);
672 memcpy(peer_session->messages->msgbuf, msgbuf, msgbuf_size);
673 peer_session->messages->msgbuf_size = msgbuf_size;
674 peer_session->messages->next = NULL;
675 peer_session->messages->timeout = GNUNET_TIME_relative_to_absolute(timeout);
676 peer_session->messages->cont = cont;
677 peer_session->messages->cont_cls = cont_cls;
679 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
680 _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target));
682 run_gnunet_nat_client(plugin, addr, addrlen);
686 if (peer_session->expecting_welcome == GNUNET_NO) /* We are "connected" */
688 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);
690 else /* Haven't gotten a response from this peer, queue message */
692 temp_message = GNUNET_malloc(sizeof(struct MessageQueue));
693 temp_message->msgbuf = GNUNET_malloc(msgbuf_size);
694 memcpy(temp_message->msgbuf, msgbuf, msgbuf_size);
695 temp_message->msgbuf_size = msgbuf_size;
696 temp_message->timeout = GNUNET_TIME_relative_to_absolute(timeout);
697 temp_message->cont = cont;
698 temp_message->cont_cls = cont_cls;
699 temp_message->next = peer_session->messages;
700 peer_session->messages = temp_message;
704 else /* Other peer not behind a NAT, so we can just send the message as is */
706 sent = udp_real_send(cls, udp_sock.desc, target, msgbuf, msgbuf_size, priority, timeout, addr, addrlen, cont, cont_cls);
709 if (sent == GNUNET_SYSERR)
717 * Add the IP of our network interface to the list of
718 * our external IP addresses.
721 process_interfaces (void *cls,
724 const struct sockaddr *addr, socklen_t addrlen)
726 struct Plugin *plugin = cls;
728 struct sockaddr_in *v4;
729 struct sockaddr_in6 *v6;
731 af = addr->sa_family;
734 v4 = (struct sockaddr_in *) addr;
735 if (plugin->behind_nat == GNUNET_YES)
737 GNUNET_assert(inet_pton(AF_INET, plugin->external_address, &v4->sin_addr) == GNUNET_OK);
738 v4->sin_port = htons (0); /* Indicates to receiver we are behind NAT */
741 v4->sin_port = htons (plugin->port);
745 GNUNET_assert (af == AF_INET6);
746 v6 = (struct sockaddr_in6 *) addr;
747 if (plugin->behind_nat == GNUNET_YES)
748 v6->sin6_port = htons (0);
750 v6->sin6_port = htons (plugin->port);
753 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
754 GNUNET_ERROR_TYPE_BULK,
755 "udp", _("Found address `%s' (%s)\n"),
756 GNUNET_a2s (addr, addrlen), name);
757 plugin->env->notify_address (plugin->env->cls,
759 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
766 * Function called by the resolver for each address obtained from DNS
767 * for our own hostname. Add the addresses to the list of our
768 * external IP addresses.
771 * @param addr one of the addresses of the host, NULL for the last address
772 * @param addrlen length of the address
775 process_hostname_ips (void *cls,
776 const struct sockaddr *addr, socklen_t addrlen)
778 struct Plugin *plugin = cls;
782 plugin->hostname_dns = NULL;
785 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
790 * Send UDP probe messages or UDP keepalive messages, depending on the
791 * state of the connection.
793 * @param cls closure for this call (should be the main Plugin)
794 * @param tc task context for running this
797 send_udp_probe_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
799 struct UDP_NAT_Probes *probe = cls;
800 struct UDP_NAT_ProbeMessage *message;
801 struct Plugin *plugin = probe->plugin;
803 message = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessage));
804 message->header.size = htons(sizeof(struct UDP_NAT_ProbeMessage));
805 message->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE);
806 /* If they gave us a port, use that. If not, try our port. */
807 if (probe->port != 0)
808 probe->sock_addr.sin_port = htons(probe->port);
810 probe->sock_addr.sin_port = htons(plugin->port);
813 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
814 _("Sending a probe to port %d\n"), ntohs(probe->sock_addr.sin_port));
819 udp_real_send(plugin, udp_sock.desc, NULL,
820 (char *)message, ntohs(message->header.size), 0,
821 GNUNET_TIME_relative_get_unit(),
822 &probe->sock_addr, sizeof(probe->sock_addr),
823 &udp_probe_continuation, probe);
825 GNUNET_free(message);
830 * Continuation for probe sends. If the last probe was sent
831 * "successfully", schedule sending of another one. If not,
835 udp_probe_continuation (void *cls, const struct GNUNET_PeerIdentity *target, int result)
837 struct UDP_NAT_Probes *probe = cls;
838 struct Plugin *plugin = probe->plugin;
840 if ((result == GNUNET_OK) && (probe->count < MAX_PROBES))
843 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
844 _("Scheduling next probe for 10000 milliseconds\n"));
846 probe->task = GNUNET_SCHEDULER_add_delayed(plugin->env->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10000), &send_udp_probe_message, probe);
848 else /* Destroy the probe context. */
851 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
852 _("Sending probe didn't go well...\n"));
858 * Find probe message by address
860 * @param plugin the plugin for this transport
861 * @param address_string the ip address as a string
863 struct UDP_NAT_Probes *
864 find_probe(struct Plugin *plugin, char * address_string)
866 struct UDP_NAT_Probes *pos;
868 pos = plugin->probes;
870 if (strcmp(pos->address_string, address_string) == 0)
878 * @param cls the plugin handle
879 * @param tc the scheduling context (for rescheduling this function again)
881 * We have been notified that gnunet-nat-server has written something to stdout.
882 * Handle the output, then reschedule this function to be called again once
887 udp_plugin_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
889 struct Plugin *plugin = cls;
892 memset(&mybuf, 0, sizeof(mybuf));
894 struct UDP_NAT_Probes *temp_probe;
897 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, &mybuf, sizeof(mybuf));
902 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
903 _("Finished reading from server stdout with code: %d\n"), bytes);
910 for (i = 0; i < sizeof(mybuf); i++)
912 if (mybuf[i] == '\n')
915 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
918 port_start = &mybuf[i + 1];
922 if (port_start != NULL)
923 port = atoi(port_start);
926 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
927 _("nat-server-read read: %s port %d\n"), &mybuf, port);
930 /** We have received an ICMP response, ostensibly from a non-NAT'd peer
931 * that wants to connect to us! Send a message to establish a connection.
933 temp_probe = find_probe(plugin, &mybuf[0]);
935 if (temp_probe == NULL)
937 temp_probe = GNUNET_malloc(sizeof(struct UDP_NAT_Probes));
938 temp_probe->address_string = strdup(&mybuf[0]);
939 temp_probe->sock_addr.sin_family = AF_INET;
940 GNUNET_assert(inet_pton(AF_INET, &mybuf[0], &temp_probe->sock_addr.sin_addr) == 1);
941 temp_probe->port = port;
942 temp_probe->next = plugin->probes;
943 temp_probe->plugin = plugin;
944 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);
945 plugin->probes = temp_probe;
948 plugin->server_read_task =
949 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
950 GNUNET_TIME_UNIT_FOREVER_REL,
951 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
957 * Demultiplexer for UDP NAT messages
959 * @param plugin the main plugin for this transport
960 * @param sender from which peer the message was received
961 * @param currhdr pointer to the header of the message
962 * @param sender_addr the address from which the message was received
963 * @param fromlen the length of the address
964 * @param sockinfo which socket did we receive the message on
967 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)
969 struct UDP_NAT_ProbeMessageReply *outgoing_probe_reply;
970 struct UDP_NAT_ProbeMessageConfirmation *outgoing_probe_confirmation;
972 char addr_buf[INET_ADDRSTRLEN];
973 struct UDP_NAT_Probes *outgoing_probe;
974 struct PeerSession *peer_session;
975 struct MessageQueue *pending_message;
976 struct MessageQueue *pending_message_temp;
978 if (memcmp(sender, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
981 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
982 _("Received a message from myself, dropping!!!\n"));
987 switch (ntohs(currhdr->type))
989 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE:
990 /* Send probe reply */
991 outgoing_probe_reply = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageReply));
992 outgoing_probe_reply->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageReply));
993 outgoing_probe_reply->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY);
996 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
997 _("Received a probe on listen port %d, sent_from port %d\n"), sockinfo->port, ntohs(((struct sockaddr_in *)sender_addr)->sin_port));
1000 udp_real_send(plugin, sockinfo->desc, NULL,
1001 (char *)outgoing_probe_reply,
1002 ntohs(outgoing_probe_reply->header.size), 0,
1003 GNUNET_TIME_relative_get_unit(),
1004 sender_addr, fromlen,
1008 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1009 _("Sent PROBE REPLY to port %d on outgoing port %d\n"), ntohs(((struct sockaddr_in *)sender_addr)->sin_port), sockinfo->port);
1011 GNUNET_free(outgoing_probe_reply);
1013 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_REPLY:
1014 /* Check for existing probe, check ports returned, send confirmation if all is well */
1016 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1017 _("Received PROBE REPLY from port %d on incoming port %d\n"), ntohs(((struct sockaddr_in *)sender_addr)->sin_port), sockinfo->port);
1019 /* FIXME: use nonce, then IPv6 replies could work I think... */
1020 if (sender_addr->ss_family == AF_INET)
1022 memset(&addr_buf, 0, sizeof(addr_buf));
1023 inet_ntop(AF_INET, &((struct sockaddr_in *) sender_addr)->sin_addr, addr_buf, INET_ADDRSTRLEN);
1024 outgoing_probe = find_probe(plugin, &addr_buf[0]);
1025 if (outgoing_probe != NULL)
1028 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1029 _("Sending confirmation that we were reached!\n"));
1031 outgoing_probe_confirmation = GNUNET_malloc(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1032 outgoing_probe_confirmation->header.size = htons(sizeof(struct UDP_NAT_ProbeMessageConfirmation));
1033 outgoing_probe_confirmation->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM);
1035 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);
1037 if (outgoing_probe->task != GNUNET_SCHEDULER_NO_TASK)
1039 GNUNET_SCHEDULER_cancel(plugin->env->sched, outgoing_probe->task);
1040 outgoing_probe->task = GNUNET_SCHEDULER_NO_TASK;
1041 /* Schedule task to timeout and remove probe if confirmation not received */
1043 GNUNET_free(outgoing_probe_confirmation);
1048 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1049 _("Received a probe reply, but have no record of a sent probe!\n"));
1054 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_CONFIRM:
1055 peer_session = find_session(plugin, sender);
1057 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1058 _("Looking up peer session for peer %s\n"), GNUNET_i2s(sender));
1060 if (peer_session == NULL) /* Shouldn't this NOT happen? */
1063 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
1064 _("Peer not in list, adding (THIS MAY BE A MISTAKE) %s\n"), GNUNET_i2s(sender));
1066 peer_session = GNUNET_malloc(sizeof(struct PeerSession));
1067 peer_session->connect_addr = GNUNET_malloc(fromlen);
1068 memcpy(peer_session->connect_addr, sender_addr, fromlen);
1069 peer_session->connect_alen = fromlen;
1070 peer_session->plugin = plugin;
1071 peer_session->sock = sockinfo->desc;
1072 memcpy(&peer_session->target, sender, sizeof(struct GNUNET_PeerIdentity));
1073 peer_session->expecting_welcome = GNUNET_NO;
1075 peer_session->next = plugin->sessions;
1076 plugin->sessions = peer_session;
1078 peer_session->messages = NULL;
1080 else if (peer_session->expecting_welcome == GNUNET_YES)
1082 peer_session->expecting_welcome = GNUNET_NO;
1083 /* FIXME: There is no way to find this based on receiving port at the moment! */
1084 peer_session->sock = sockinfo->desc; /* This may matter, not sure right now... */
1085 ((struct sockaddr_in *)peer_session->connect_addr)->sin_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1087 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1088 _("Received a probe confirmation, will send to peer on port %d\n"), ntohs(((struct sockaddr_in *)peer_session->connect_addr)->sin_port));
1090 if (peer_session->messages != NULL)
1093 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1094 _("Received a probe confirmation, sending queued messages.\n"));
1096 pending_message = peer_session->messages;
1098 while (pending_message != NULL)
1101 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1102 _("sending queued message %d\n"), count);
1104 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);
1105 pending_message_temp = pending_message;
1106 pending_message = pending_message->next;
1107 GNUNET_free(pending_message_temp->msgbuf);
1108 GNUNET_free(pending_message_temp);
1110 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1111 _("finished sending queued message %d\n"), count);
1121 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp",
1122 _("Received probe confirmation for already confirmed peer!\n"));
1125 /* Received confirmation, add peer with address/port specified */
1127 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_NAT_PROBE_KEEPALIVE:
1128 /* Once we've sent NAT_PROBE_CONFIRM change to sending keepalives */
1129 /* If we receive these just ignore! */
1132 plugin->env->receive (plugin->env->cls, sender, currhdr, UDP_DIRECT_DISTANCE,
1133 NULL, (char *)sender_addr, fromlen);
1140 * @param cls the plugin handle
1141 * @param tc the scheduling context (for rescheduling this function again)
1143 * We have been notified that our writeset has something to read. We don't
1144 * know which socket needs to be read, so we have to check each one
1145 * Then reschedule this function to be called again once more is available.
1149 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1151 struct Plugin *plugin = cls;
1153 struct UDPMessage *msg;
1154 struct GNUNET_PeerIdentity *sender;
1155 unsigned int buflen;
1157 struct sockaddr_storage addr;
1164 const struct GNUNET_MessageHeader *currhdr;
1169 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock.desc);
1171 if (buflen == GNUNET_NO)
1174 buf = GNUNET_malloc (buflen);
1175 fromlen = sizeof (addr);
1176 memset (&addr, 0, fromlen);
1178 GNUNET_NETWORK_socket_recvfrom (udp_sock.desc, buf, buflen,
1179 (struct sockaddr *) &addr, &fromlen);
1182 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1183 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
1192 msg = (struct UDPMessage *) buf;
1195 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1196 ("header reports message size of %d, type %d\n"),
1197 ntohs (msg->header.size), ntohs (msg->header.type));
1199 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
1205 msgbuf = (char *)&msg[1];
1206 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1207 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1211 tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
1213 while (offset < tsize)
1215 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
1217 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1218 ("processing msg %d: type %d, size %d at offset %d\n"),
1219 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
1221 udp_demultiplexer(plugin, sender, currhdr, &addr, fromlen, &udp_sock);
1223 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
1224 ("processing done msg %d: type %d, size %d at offset %d\n"),
1225 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
1227 offset += ntohs(currhdr->size);
1230 GNUNET_free_non_null (buf);
1231 GNUNET_free_non_null (sender);
1234 plugin->select_task =
1235 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1236 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1237 GNUNET_SCHEDULER_NO_TASK,
1238 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1239 NULL, &udp_plugin_select, plugin);
1244 * Create a slew of UDP sockets. If possible, use IPv6, otherwise
1247 * @param cls closure for server start, should be a struct Plugin *
1249 * @return number of sockets created or GNUNET_SYSERR on error
1252 udp_transport_server_start (void *cls)
1254 struct Plugin *plugin = cls;
1255 struct sockaddr_in serverAddrv4;
1256 struct sockaddr_in6 serverAddrv6;
1257 struct sockaddr *serverAddr;
1259 int sockets_created;
1261 /* Pipe to read from started processes stdout (on read end) */
1262 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES);
1264 sockets_created = 0;
1265 if (plugin->server_stdout == NULL)
1266 return sockets_created;
1268 if (plugin->behind_nat == GNUNET_YES)
1271 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1273 "Starting gnunet-nat-server process\n");
1275 /* Start the server process */
1276 plugin->server_pid = GNUNET_OS_start_process(NULL, plugin->server_stdout, "gnunet-nat-server", "gnunet-nat-server", plugin->internal_address, NULL);
1277 if (plugin->server_pid == GNUNET_SYSERR)
1280 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1282 "Failed to start gnunet-nat-server process\n");
1284 return GNUNET_SYSERR;
1286 /* Close the write end of the read pipe */
1287 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1289 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout, GNUNET_DISK_PIPE_END_READ);
1290 plugin->server_read_task =
1291 GNUNET_SCHEDULER_add_read_file (plugin->env->sched,
1292 GNUNET_TIME_UNIT_FOREVER_REL,
1293 plugin->server_stdout_handle, &udp_plugin_server_read, plugin);
1296 udp_sock.desc = NULL;
1299 udp_sock.desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
1300 if (NULL == udp_sock.desc)
1302 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
1303 return sockets_created;
1307 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1308 #if HAVE_SOCKADDR_IN_SIN_LEN
1309 serverAddrv4.sin_len = sizeof (serverAddrv4);
1311 serverAddrv4.sin_family = AF_INET;
1312 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
1313 serverAddrv4.sin_port = htons (plugin->port);
1314 addrlen = sizeof (serverAddrv4);
1315 serverAddr = (struct sockaddr *) &serverAddrv4;
1317 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1319 "Binding to port %d\n", ntohs(serverAddrv4.sin_port));
1321 while (GNUNET_NETWORK_socket_bind (udp_sock.desc, serverAddr, addrlen) !=
1324 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1326 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1328 "Binding failed, trying new port %d\n", ntohs(serverAddrv4.sin_port));
1331 udp_sock.port = ntohs(serverAddrv4.sin_port);
1336 if ((udp_sock.desc == NULL) && (GNUNET_YES !=
1337 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
1340 udp_sock.desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
1341 if (udp_sock.desc != NULL)
1343 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1344 #if HAVE_SOCKADDR_IN_SIN_LEN
1345 serverAddrv6.sin6_len = sizeof (serverAddrv6);
1347 serverAddrv6.sin6_family = AF_INET6;
1348 serverAddrv6.sin6_addr = in6addr_any;
1349 serverAddrv6.sin6_port = htons (plugin->port);
1350 addrlen = sizeof (serverAddrv6);
1351 serverAddr = (struct sockaddr *) &serverAddrv6;
1356 plugin->rs = GNUNET_NETWORK_fdset_create ();
1358 GNUNET_NETWORK_fdset_zero (plugin->rs);
1361 GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock.desc);
1363 plugin->select_task =
1364 GNUNET_SCHEDULER_add_select (plugin->env->sched,
1365 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1366 GNUNET_SCHEDULER_NO_TASK,
1367 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1368 NULL, &udp_plugin_select, plugin);
1370 return sockets_created;
1375 * Check if the given port is plausible (must be either
1376 * our listen port or our advertised port). If it is
1377 * neither, we return one of these two ports at random.
1379 * @return either in_port or a more plausible port
1382 check_port (struct Plugin *plugin, uint16_t in_port)
1385 /* FIXME: remember what ports we are using to better respond to this */
1388 for (i = plugin->starting_port; i < plugin->num_ports + plugin->starting_port; i++)
1394 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1395 plugin->num_ports) + plugin->starting_port;
1401 * Another peer has suggested an address for this peer and transport
1402 * plugin. Check that this could be a valid address. This function
1403 * is not expected to 'validate' the address in the sense of trying to
1404 * connect to it but simply to see if the binary format is technically
1405 * legal for establishing a connection.
1407 * @param cls closure, should be our handle to the Plugin
1408 * @param addr pointer to the address, may be modified (slightly)
1409 * @param addrlen length of addr
1410 * @return GNUNET_OK if this is a plausible address for this peer
1411 * and transport, GNUNET_SYSERR if not
1415 udp_check_address (void *cls, void *addr, size_t addrlen)
1417 struct Plugin *plugin = cls;
1418 char buf[sizeof (struct sockaddr_in6)];
1420 struct sockaddr_in *v4;
1421 struct sockaddr_in6 *v6;
1423 if ((addrlen != sizeof (struct sockaddr_in)) &&
1424 (addrlen != sizeof (struct sockaddr_in6)))
1426 GNUNET_break_op (0);
1427 return GNUNET_SYSERR;
1429 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1430 if (addrlen == sizeof (struct sockaddr_in))
1432 v4 = (struct sockaddr_in *) buf;
1433 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1437 v6 = (struct sockaddr_in6 *) buf;
1438 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1442 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1444 "Informing transport service about my address `%s'.\n",
1445 GNUNET_a2s (addr, addrlen));
1452 * Append our port and forward the result.
1455 append_port (void *cls, const char *hostname)
1457 struct PrettyPrinterContext *ppc = cls;
1460 if (hostname == NULL)
1462 ppc->asc (ppc->asc_cls, NULL);
1466 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1467 ppc->asc (ppc->asc_cls, ret);
1473 * Convert the transports address to a nice, human-readable
1476 * @param cls closure
1477 * @param type name of the transport that generated the address
1478 * @param addr one of the addresses of the host, NULL for the last address
1479 * the specific address format depends on the transport
1480 * @param addrlen length of the address
1481 * @param numeric should (IP) addresses be displayed in numeric form?
1482 * @param timeout after how long should we give up?
1483 * @param asc function to call on each string
1484 * @param asc_cls closure for asc
1487 udp_plugin_address_pretty_printer (void *cls,
1492 struct GNUNET_TIME_Relative timeout,
1493 GNUNET_TRANSPORT_AddressStringCallback asc,
1496 struct Plugin *plugin = cls;
1497 const struct sockaddr_in *v4;
1498 const struct sockaddr_in6 *v6;
1499 struct PrettyPrinterContext *ppc;
1501 if ((addrlen != sizeof (struct sockaddr_in)) &&
1502 (addrlen != sizeof (struct sockaddr_in6)))
1504 /* invalid address */
1505 GNUNET_break_op (0);
1506 asc (asc_cls, NULL);
1509 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1511 ppc->asc_cls = asc_cls;
1512 if (addrlen == sizeof (struct sockaddr_in))
1514 v4 = (const struct sockaddr_in *) addr;
1515 ppc->port = ntohs (v4->sin_port);
1519 v6 = (const struct sockaddr_in6 *) addr;
1520 ppc->port = ntohs (v6->sin6_port);
1523 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1527 !numeric, timeout, &append_port, ppc);
1532 * The exported method. Makes the core api available via a global and
1533 * returns the udp transport API.
1536 libgnunet_plugin_transport_udp_init (void *cls)
1538 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1539 unsigned long long mtu;
1540 unsigned long long port;
1541 struct GNUNET_TRANSPORT_PluginFunctions *api;
1542 struct Plugin *plugin;
1543 struct GNUNET_SERVICE_Context *service;
1544 int sockets_created;
1546 char *internal_address;
1547 char *external_address;
1549 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
1550 if (service == NULL)
1552 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
1553 ("Failed to start service for `%s' transport plugin.\n"),
1558 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
1561 behind_nat = GNUNET_YES; /* We are behind nat (according to the user) */
1563 behind_nat = GNUNET_NO; /* We are not behind nat! */
1565 external_address = NULL;
1566 if ((GNUNET_YES == behind_nat) && (GNUNET_OK !=
1567 GNUNET_CONFIGURATION_get_value_string (env->cfg,
1570 &external_address)))
1572 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1575 ("Require EXTERNAL_ADDRESS for service `%s' in configuration!\n"),
1577 GNUNET_SERVICE_stop (service);
1581 internal_address = NULL;
1582 if ((GNUNET_YES == behind_nat) && (GNUNET_OK !=
1583 GNUNET_CONFIGURATION_get_value_string (env->cfg,
1586 &internal_address)))
1588 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1591 ("Require INTERNAL_ADDRESS for service `%s' in configuration!\n"),
1593 GNUNET_SERVICE_stop (service);
1594 GNUNET_free_non_null(external_address);
1599 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1603 port = UDP_NAT_DEFAULT_PORT;
1604 else if (port > 65535)
1606 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1608 _("Given `%s' option is out of range: %llu > %u\n"),
1612 GNUNET_SERVICE_stop (service);
1613 GNUNET_free_non_null(external_address);
1614 GNUNET_free_non_null(internal_address);
1620 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1622 _("MTU %llu for `%s' is probably too low!\n"), mtu,
1625 plugin = GNUNET_malloc (sizeof (struct Plugin));
1626 plugin->external_address = external_address;
1627 plugin->internal_address = internal_address;
1628 plugin->port = port;
1629 plugin->behind_nat = behind_nat;
1632 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1635 api->send = &udp_plugin_send;
1636 api->disconnect = &udp_disconnect;
1637 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1638 api->check_address = &udp_check_address;
1640 plugin->service = service;
1642 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1643 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1646 HOSTNAME_RESOLVE_TIMEOUT,
1647 &process_hostname_ips,
1650 sockets_created = udp_transport_server_start (plugin);
1652 GNUNET_assert (sockets_created == 1);
1659 libgnunet_plugin_transport_udp_done (void *cls)
1661 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1662 struct Plugin *plugin = api->cls;
1664 udp_transport_server_stop (plugin);
1665 if (NULL != hostname_dns)
1667 GNUNET_RESOLVER_request_cancel (hostname_dns);
1668 hostname_dns = NULL;
1670 GNUNET_SERVICE_stop (plugin->service);
1672 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1673 GNUNET_free (plugin);
1678 /* end of plugin_transport_udp.c */