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