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