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