comments
[oweals/gnunet.git] / src / nat / nat_test.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 nat/nat_test.c
23  * @brief functions to test if the NAT configuration is successful at achieving NAT traversal (with the help of a gnunet-nat-server)
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_nat_lib.h"
29 #include "nat.h"
30
31
32 /**
33  * Entry we keep for each incoming connection.
34  */
35 struct NatActivity
36 {
37   /**
38    * This is a doubly-linked list.
39    */
40   struct NatActivity *next;
41
42   /**
43    * This is a doubly-linked list.
44    */
45   struct NatActivity *prev;
46
47   /**
48    * Socket of the incoming connection.
49    */
50   struct GNUNET_NETWORK_Handle *sock;
51
52   /**
53    * Handle of the master context.
54    */
55   struct GNUNET_NAT_Test *h;
56
57   /**
58    * Task reading from the incoming connection.
59    */
60   GNUNET_SCHEDULER_TaskIdentifier rtask;
61 };
62
63
64 /**
65  * Handle to a NAT test.
66  */
67 struct GNUNET_NAT_Test
68 {
69
70   /**
71    * Configuration used
72    */
73   const struct GNUNET_CONFIGURATION_Handle *cfg;
74
75   /**
76    * Function to call with success report
77    */
78   GNUNET_NAT_TestCallback report;
79   
80   /**
81    * Closure for 'report'.
82    */
83   void *report_cls;
84
85   /**
86    * Handle to NAT traversal in use
87    */
88   struct GNUNET_NAT_Handle *nat;
89
90   /**
91    * Handle to listen socket, or NULL
92    */
93   struct GNUNET_NETWORK_Handle *lsock;
94
95   /**
96    * Head of list of nat activities.
97    */
98   struct NatActivity *head;
99
100   /**
101    * Tail of list of nat activities.
102    */
103   struct NatActivity *tail;
104
105   /**
106    * Identity of task for the listen socket (if any)
107    */
108   GNUNET_SCHEDULER_TaskIdentifier ltask;
109
110   /**
111    * GNUNET_YES if we're testing TCP
112    */
113   int is_tcp;
114
115   /**
116    * Data that should be transmitted or source-port.
117    */
118   uint16_t data;
119
120   /**
121    * Advertised port to the other peer.
122    */
123   uint16_t adv_port;
124
125 };
126
127
128 /**
129  * Function called from GNUNET_NAT_register
130  * whenever someone asks us to do connection
131  * reversal.
132  *
133  * @param cls closure, our 'struct GNUNET_NAT_Handle'
134  * @param addr public IP address of the other peer
135  * @param addrlen actual lenght of the address
136  */
137 static void
138 reversal_cb (void *cls, 
139              const struct sockaddr *addr,
140              socklen_t addrlen)
141 {
142   struct GNUNET_NAT_Test *h = cls;
143   const struct sockaddr_in *sa;
144
145   if (addrlen != sizeof (struct sockaddr_in))
146     return;
147   sa = (const struct sockaddr_in *) addr;
148   if (h->data != sa->sin_port)
149     {
150       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151                   "Received connection reversal request for wrong port\n");
152       return; /* wrong port */
153     }
154   /* report success */
155   h->report (h->report_cls, GNUNET_OK);
156 }
157
158
159 /**
160  * Activity on our incoming socket.  Read data from the
161  * incoming connection.
162  *
163  * @param cls the 'struct NatActivity'
164  * @param tc scheduler context
165  */
166 static void
167 do_read (void *cls,
168          const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170   struct NatActivity *na = cls;
171   struct GNUNET_NAT_Test *tst;
172
173   na->rtask = GNUNET_SCHEDULER_NO_TASK;
174   tst = na->h;
175   GNUNET_CONTAINER_DLL_remove (tst->head,
176                                tst->tail,
177                                na);
178   if (1)
179     {
180       // fimxe: read from socket...
181     }
182   GNUNET_NETWORK_socket_close (na->sock);
183   GNUNET_free (na);
184 }
185
186
187 /**
188  * Activity on our listen socket. Accept the
189  * incoming connection.
190  *
191  * @param cls the 'struct GNUNET_NAT_Test'
192  * @param tc scheduler context
193  */
194 static void
195 do_accept (void *cls,
196            const struct GNUNET_SCHEDULER_TaskContext *tc)
197 {
198   struct GNUNET_NAT_Test *tst = cls;
199   struct GNUNET_NETWORK_Handle *s;
200   struct NatActivity *wl;
201
202   tst->ltask = GNUNET_SCHEDULER_NO_TASK;
203   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
204     return; 
205   tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
206                                               tst->lsock,
207                                               &do_accept,
208                                               tst);
209   s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
210   if (NULL == s)
211     return; /* odd error */
212   wl = GNUNET_malloc (sizeof (struct NatActivity));
213   wl->sock = s;
214   wl->h = tst;
215   wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
216                                              wl->sock,
217                                              &do_read,
218                                              wl);
219   GNUNET_CONTAINER_DLL_insert (tst->head,
220                                tst->tail,
221                                wl);
222 }
223
224
225 /**
226  * Address-callback, used to send message to gnunet-nat-server.
227  *
228  * @param cls closure
229  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
230  *     the previous (now invalid) one
231  * @param addr either the previous or the new public IP address
232  * @param addrlen actual lenght of the address
233  */
234 static void 
235 addr_cb (void *cls,
236          int add_remove,
237          const struct sockaddr *addr,
238          socklen_t addrlen)
239 {
240   struct GNUNET_NAT_Test *h = cls;
241   struct GNUNET_CLIENT_Connection *client;
242   struct GNUNET_NAT_TestMessage msg;
243   const struct sockaddr_in *sa;
244
245   if (GNUNET_YES != add_remove)
246     return;
247   if (addrlen != sizeof (struct sockaddr_in))
248     return; /* ignore IPv6 here */
249   sa = (const struct sockaddr_in*) addr;
250   msg.header.size = htons (sizeof(struct GNUNET_NAT_TestMessage));
251   msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
252   msg.dst_ipv4 = sa->sin_addr.s_addr;
253   msg.dport = sa->sin_port;
254   msg.data = h->data;
255   msg.is_tcp = htonl ((uint32_t) h->is_tcp);
256
257   client = GNUNET_CLIENT_connect ("gnunet-nat-server",
258                                   h->cfg);
259   GNUNET_break (GNUNET_OK ==
260                 GNUNET_CLIENT_transmit_and_get_response (client,
261                                                          &msg.header,
262                                                          GNUNET_TIME_UNIT_SECONDS,
263                                                          GNUNET_YES,
264                                                          NULL, NULL));
265   GNUNET_CLIENT_disconnect (client, GNUNET_YES);  
266 }
267
268
269 /**
270  * Start testing if NAT traversal works using the
271  * given configuration (IPv4-only).
272  *
273  * @param cfg configuration for the NAT traversal
274  * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
275  * @param bnd_port port to bind to, 0 for connection reversal
276  * @param adv_port externally advertised port to use
277  * @param report function to call with the result of the test
278  * @param report_cls closure for report
279  * @return handle to cancel NAT test
280  */
281 struct GNUNET_NAT_Test *
282 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
283                        int is_tcp,
284                        uint16_t bnd_port,
285                        uint16_t adv_port,
286                        GNUNET_NAT_TestCallback report,
287                        void *report_cls)
288 {
289   struct GNUNET_NAT_Test *ret;
290   struct sockaddr_in sa;
291   const struct sockaddr *addrs[] = { (const struct sockaddr*) &sa };
292   const socklen_t addrlens[] = { sizeof (sa) };
293
294   memset (&sa, 0, sizeof (sa));
295   sa.sin_port = htons (bnd_port);
296 #if HAVE_SOCKADDR_IN_SIN_LEN
297   sa.sin_len = sizeof (sa);
298 #endif
299   
300   ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
301   ret->cfg = cfg;
302   ret->is_tcp = is_tcp;
303   ret->data = bnd_port;
304   ret->adv_port = adv_port;
305   ret->report = report;
306   ret->report_cls = report_cls;
307   if (bnd_port == 0)
308     {      
309       ret->nat = GNUNET_NAT_register (cfg, is_tcp,
310                                       0, 
311                                       0, NULL, NULL,
312                                       &addr_cb, &reversal_cb, ret);
313     }
314   else
315     {
316       ret->lsock = GNUNET_NETWORK_socket_create (AF_INET, 
317                                                  (is_tcp==GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM, 0);
318       if ( (ret->lsock == NULL) ||
319            (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret->lsock,
320                                                      (const struct sockaddr*) &sa,
321                                                      sizeof (sa))) )
322         {
323           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
324                       _("Failed to create listen socket for NAT test\n"));
325           if (NULL != ret->lsock)
326             GNUNET_NETWORK_socket_close (ret->lsock);
327           GNUNET_free (ret);
328           return NULL;
329         }
330       GNUNET_break (GNUNET_OK ==
331                     GNUNET_NETWORK_socket_listen (ret->lsock, 5));
332       ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
333                                                   ret->lsock,
334                                                   &do_accept,
335                                                   ret);
336       ret->nat = GNUNET_NAT_register (cfg, is_tcp,
337                                       adv_port, 
338                                       1, addrs, addrlens,
339                                       &addr_cb, NULL, ret);
340     }
341   return ret;
342 }
343
344
345 /**
346  * Stop an active NAT test.
347  *
348  * @param tst test to stop.
349  */
350 void
351 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
352 {
353   struct NatActivity *pos;
354
355   while (NULL != (pos = tst->head))
356     {
357       GNUNET_CONTAINER_DLL_remove (tst->head,
358                                    tst->tail,
359                                    pos);
360       GNUNET_SCHEDULER_cancel (pos->rtask);
361       GNUNET_NETWORK_socket_close (pos->sock);
362       GNUNET_free (pos);
363     }
364   if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
365     GNUNET_SCHEDULER_cancel (tst->ltask);
366   if (NULL != tst->lsock)
367     GNUNET_NETWORK_socket_close (tst->lsock);
368   GNUNET_NAT_unregister (tst->nat);
369   GNUNET_free (tst);
370 }
371
372 /* end of nat_test.c */