migrate nat_test to MQ
[oweals/gnunet.git] / src / nat / gnunet-nat-server.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
4
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 3, or (at your
8      option) any later version.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file src/nat/gnunet-nat-server.c
23  * @brief Daemon to run on 'gnunet.org' to help test NAT traversal code
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_nat_lib.h"
29 #include "gnunet_protocols.h"
30 #include "nat.h"
31
32
33 /**
34  * Our server.
35  */
36 static struct GNUNET_SERVER_Handle *server;
37
38 /**
39  * Our configuration.
40  */
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43
44 /**
45  * Try contacting the peer using autonomous
46  * NAT traveral method.
47  *
48  * @param dst_ipv4 IPv4 address to send the fake ICMP message
49  * @param dport destination port to include in ICMP message
50  * @param is_tcp mark for TCP (#GNUNET_YES)  or UDP (#GNUNET_NO)
51  */
52 static void
53 try_anat (uint32_t dst_ipv4,
54           uint16_t dport,
55           int is_tcp)
56 {
57   struct GNUNET_NAT_Handle *h;
58   struct sockaddr_in sa;
59
60   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
61               "Asking for connection reversal with %x and code %u\n",
62               (unsigned int) dst_ipv4,
63               (unsigned int) dport);
64   h = GNUNET_NAT_register (cfg,
65                            is_tcp,
66                            dport,
67                            0,
68                            NULL, NULL, NULL, NULL, NULL, NULL);
69   memset (&sa, 0, sizeof (sa));
70   sa.sin_family = AF_INET;
71 #if HAVE_SOCKADDR_IN_SIN_LEN
72   sa.sin_len = sizeof (sa);
73 #endif
74   sa.sin_addr.s_addr = dst_ipv4;
75   GNUNET_NAT_run_client (h, &sa);
76   GNUNET_NAT_unregister (h);
77 }
78
79
80 /**
81  * Closure for 'tcp_send'.
82  */
83 struct TcpContext
84 {
85   /**
86    * TCP  socket.
87    */
88   struct GNUNET_NETWORK_Handle *s;
89
90   /**
91    * Data to transmit.
92    */
93   uint16_t data;
94 };
95
96
97 /**
98  * Task called by the scheduler once we can do the TCP send
99  * (or once we failed to connect...).
100  *
101  * @param cls the 'struct TcpContext'
102  */
103 static void
104 tcp_send (void *cls)
105 {
106   struct TcpContext *ctx = cls;
107   const struct GNUNET_SCHEDULER_TaskContext *tc;
108
109   tc = GNUNET_SCHEDULER_get_task_context ();
110   if ((NULL != tc->write_ready) &&
111       (GNUNET_NETWORK_fdset_isset (tc->write_ready, ctx->s)))
112   {
113     if (-1 ==
114         GNUNET_NETWORK_socket_send (ctx->s, &ctx->data, sizeof (ctx->data)))
115     {
116       GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
117     }
118     GNUNET_NETWORK_socket_shutdown (ctx->s, SHUT_RDWR);
119   }
120   GNUNET_NETWORK_socket_close (ctx->s);
121   GNUNET_free (ctx);
122 }
123
124
125 /**
126  * Try to send @a data to the
127  * IP @a dst_ipv4' at port @a dport via TCP.
128  *
129  * @param dst_ipv4 target IP
130  * @param dport target port
131  * @param data data to send
132  */
133 static void
134 try_send_tcp (uint32_t dst_ipv4,
135               uint16_t dport,
136               uint16_t data)
137 {
138   struct GNUNET_NETWORK_Handle *s;
139   struct sockaddr_in sa;
140   struct TcpContext *ctx;
141
142   s = GNUNET_NETWORK_socket_create (AF_INET,
143                                     SOCK_STREAM,
144                                     0);
145   if (NULL == s)
146   {
147     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
148                          "socket");
149     return;
150   }
151   memset (&sa, 0, sizeof (sa));
152   sa.sin_family = AF_INET;
153 #if HAVE_SOCKADDR_IN_SIN_LEN
154   sa.sin_len = sizeof (sa);
155 #endif
156   sa.sin_addr.s_addr = dst_ipv4;
157   sa.sin_port = htons (dport);
158   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159               "Sending TCP message to `%s'\n",
160               GNUNET_a2s ((struct sockaddr *) &sa,
161                           sizeof (sa)));
162   if ( (GNUNET_OK !=
163         GNUNET_NETWORK_socket_connect (s,
164                                        (const struct sockaddr *) &sa,
165                                        sizeof (sa))) &&
166        (errno != EINPROGRESS) )
167   {
168     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
169                          "connect");
170     GNUNET_NETWORK_socket_close (s);
171     return;
172   }
173   ctx = GNUNET_new (struct TcpContext);
174   ctx->s = s;
175   ctx->data = data;
176   GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS,
177                                   s,
178                                   &tcp_send,
179                                   ctx);
180 }
181
182
183 /**
184  * Try to send @a data to the
185  * IP @a dst_ipv4' at port @a dport via UDP.
186  *
187  * @param dst_ipv4 target IP
188  * @param dport target port
189  * @param data data to send
190  */
191 static void
192 try_send_udp (uint32_t dst_ipv4,
193               uint16_t dport,
194               uint16_t data)
195 {
196   struct GNUNET_NETWORK_Handle *s;
197   struct sockaddr_in sa;
198
199   s = GNUNET_NETWORK_socket_create (AF_INET,
200                                     SOCK_DGRAM,
201                                     0);
202   if (NULL == s)
203   {
204     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
205                          "socket");
206     return;
207   }
208   memset (&sa, 0, sizeof (sa));
209   sa.sin_family = AF_INET;
210 #if HAVE_SOCKADDR_IN_SIN_LEN
211   sa.sin_len = sizeof (sa);
212 #endif
213   sa.sin_addr.s_addr = dst_ipv4;
214   sa.sin_port = htons (dport);
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216               "Sending UDP packet to `%s'\n",
217               GNUNET_a2s ((struct sockaddr *) &sa,
218                           sizeof (sa)));
219   if (-1 ==
220       GNUNET_NETWORK_socket_sendto (s,
221                                     &data,
222                                     sizeof (data),
223                                     (const struct sockaddr *) &sa,
224                                     sizeof (sa)))
225     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
226                          "sendto");
227   GNUNET_NETWORK_socket_close (s);
228 }
229
230
231 /**
232  * We've received a request to probe a NAT
233  * traversal. Do it.
234  *
235  * @param cls unused
236  * @param client handle to client (we always close)
237  * @param msg message with details about what to test
238  */
239 static void
240 test (void *cls,
241       struct GNUNET_SERVER_Client *client,
242       const struct GNUNET_MessageHeader *msg)
243 {
244   const struct GNUNET_NAT_TestMessage *tm;
245   uint16_t dport;
246
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248               "Received test request\n");
249   tm = (const struct GNUNET_NAT_TestMessage *) msg;
250   dport = ntohs (tm->dport);
251   if (0 == dport)
252     try_anat (tm->dst_ipv4,
253               ntohs (tm->data),
254               (int) ntohl (tm->is_tcp));
255   else if (GNUNET_YES == ntohl (tm->is_tcp))
256     try_send_tcp (tm->dst_ipv4,
257                   dport,
258                   tm->data);
259   else
260     try_send_udp (tm->dst_ipv4,
261                   dport,
262                   tm->data);
263   GNUNET_SERVER_receive_done (client,
264                               GNUNET_NO);
265 }
266
267
268 /**
269  * Task run during shutdown.
270  *
271  * @param cls unused
272  */
273 static void
274 shutdown_task (void *cls)
275 {
276   GNUNET_SERVER_destroy (server);
277   server = NULL;
278 }
279
280
281 /**
282  * Main function that will be run.
283  *
284  * @param cls closure
285  * @param args remaining command-line arguments
286  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
287  * @param c configuration
288  */
289 static void
290 run (void *cls,
291      char *const *args,
292      const char *cfgfile,
293      const struct GNUNET_CONFIGURATION_Handle *c)
294 {
295   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
296     {&test, NULL, GNUNET_MESSAGE_TYPE_NAT_TEST,
297      sizeof (struct GNUNET_NAT_TestMessage)},
298     {NULL, NULL, 0, 0}
299   };
300   unsigned int port;
301   struct sockaddr_in in4;
302   struct sockaddr_in6 in6;
303
304   socklen_t slen[] = {
305     sizeof (in4),
306     sizeof (in6),
307     0
308   };
309   struct sockaddr *sa[] = {
310     (struct sockaddr *) &in4,
311     (struct sockaddr *) &in6,
312     NULL
313   };
314
315   cfg = c;
316   if ((args[0] == NULL) || (1 != SSCANF (args[0], "%u", &port)) || (0 == port)
317       || (65536 <= port))
318   {
319     FPRINTF (stderr,
320              _
321              ("Please pass valid port number as the first argument! (got `%s')\n"),
322              args[0]);
323     return;
324   }
325   memset (&in4, 0, sizeof (in4));
326   memset (&in6, 0, sizeof (in6));
327   in4.sin_family = AF_INET;
328   in4.sin_port = htons ((uint16_t) port);
329   in6.sin6_family = AF_INET6;
330   in6.sin6_port = htons ((uint16_t) port);
331 #if HAVE_SOCKADDR_IN_SIN_LEN
332   in4.sin_len = sizeof (in4);
333   in6.sin6_len = sizeof (in6);
334 #endif
335   server =
336       GNUNET_SERVER_create (NULL, NULL, (struct sockaddr * const *) sa, slen,
337                             GNUNET_TIME_UNIT_SECONDS, GNUNET_YES);
338   GNUNET_SERVER_add_handlers (server, handlers);
339   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
340                                  NULL);
341 }
342
343
344 /**
345  * Main function of gnunet-nat-server.
346  *
347  * @param argc number of command-line arguments
348  * @param argv command line
349  * @return 0 on success, -1 on error
350  */
351 int
352 main (int argc, char *const argv[])
353 {
354   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
355     GNUNET_GETOPT_OPTION_END
356   };
357
358   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
359     return 2;
360
361   if (GNUNET_OK !=
362       GNUNET_PROGRAM_run (argc, argv, "gnunet-nat-server [options] PORT",
363                           _("GNUnet NAT traversal test helper daemon"), options,
364                           &run, NULL))
365   {
366     GNUNET_free ((void*) argv);
367     return 1;
368   }
369   GNUNET_free ((void*) argv);
370   return 0;
371 }
372
373
374 /* end of gnunet-nat-server.c */