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