+#include "gnunet_nat_lib.h"
+#include "gnunet_protocols.h"
+#include "nat.h"
+
+
+/**
+ * Our server.
+ */
+static struct GNUNET_SERVER_Handle *server;
+
+/**
+ * Our configuration.
+ */
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+
+/**
+ * Try contacting the peer using autonomous
+ * NAT traveral method.
+ *
+ * @param dst_ipv4 IPv4 address to send the fake ICMP message
+ * @param dport destination port to include in ICMP message
+ * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO)
+ */
+static void
+try_anat (uint32_t dst_ipv4,
+ uint16_t dport,
+ int is_tcp)
+{
+ struct GNUNET_NAT_Handle *h;
+ struct sockaddr_in sa;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Asking for connection reversal with %x and code %u\n",
+ (unsigned int) dst_ipv4,
+ (unsigned int) dport);
+ h = GNUNET_NAT_register (cfg,
+ is_tcp,
+ dport,
+ 0,
+ NULL, NULL, NULL, NULL, NULL, NULL);
+ memset (&sa, 0, sizeof (sa));
+ sa.sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ sa.sin_len = sizeof (sa);
+#endif
+ sa.sin_addr.s_addr = dst_ipv4;
+ GNUNET_NAT_run_client (h, &sa);
+ GNUNET_NAT_unregister (h);
+}
+
+
+/**
+ * Closure for 'tcp_send'.
+ */
+struct TcpContext
+{
+ /**
+ * TCP socket.
+ */
+ struct GNUNET_NETWORK_Handle *s;
+
+ /**
+ * Data to transmit.
+ */
+ uint16_t data;
+};
+
+
+/**
+ * Task called by the scheduler once we can do the TCP send
+ * (or once we failed to connect...).
+ *
+ * @param cls the 'struct TcpContext'
+ */
+static void
+tcp_send (void *cls)
+{
+ struct TcpContext *ctx = cls;
+ const struct GNUNET_SCHEDULER_TaskContext *tc;
+
+ tc = GNUNET_SCHEDULER_get_task_context ();
+ if ((NULL != tc->write_ready) &&
+ (GNUNET_NETWORK_fdset_isset (tc->write_ready, ctx->s)))
+ {
+ if (-1 ==
+ GNUNET_NETWORK_socket_send (ctx->s, &ctx->data, sizeof (ctx->data)))
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
+ }
+ GNUNET_NETWORK_socket_shutdown (ctx->s, SHUT_RDWR);
+ }
+ GNUNET_NETWORK_socket_close (ctx->s);
+ GNUNET_free (ctx);
+}
+
+
+/**
+ * Try to send @a data to the
+ * IP @a dst_ipv4' at port @a dport via TCP.
+ *
+ * @param dst_ipv4 target IP
+ * @param dport target port
+ * @param data data to send
+ */
+static void
+try_send_tcp (uint32_t dst_ipv4,
+ uint16_t dport,
+ uint16_t data)
+{
+ struct GNUNET_NETWORK_Handle *s;
+ struct sockaddr_in sa;
+ struct TcpContext *ctx;
+
+ s = GNUNET_NETWORK_socket_create (AF_INET,
+ SOCK_STREAM,
+ 0);
+ if (NULL == s)
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+ "socket");
+ return;
+ }
+ memset (&sa, 0, sizeof (sa));
+ sa.sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ sa.sin_len = sizeof (sa);
+#endif
+ sa.sin_addr.s_addr = dst_ipv4;
+ sa.sin_port = htons (dport);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Sending TCP message to `%s'\n",
+ GNUNET_a2s ((struct sockaddr *) &sa,
+ sizeof (sa)));
+ if ( (GNUNET_OK !=
+ GNUNET_NETWORK_socket_connect (s,
+ (const struct sockaddr *) &sa,
+ sizeof (sa))) &&
+ (errno != EINPROGRESS) )
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+ "connect");
+ GNUNET_NETWORK_socket_close (s);
+ return;
+ }
+ ctx = GNUNET_new (struct TcpContext);
+ ctx->s = s;
+ ctx->data = data;
+ GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS,
+ s,
+ &tcp_send,
+ ctx);
+}
+