aboutsummaryrefslogtreecommitdiff
path: root/src/nat/gnunet-nat.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-11-27 14:43:02 +0100
committerChristian Grothoff <christian@grothoff.org>2016-11-27 14:43:02 +0100
commitc30f9fe5daf92a89849f7d6d22eacb603918b42d (patch)
tree70b07926e1170c5b19a3d09a773ec2c1422d84df /src/nat/gnunet-nat.c
parentb035390c73b241ddb513f420671043e9113c237c (diff)
downloadgnunet-c30f9fe5daf92a89849f7d6d22eacb603918b42d.tar.gz
gnunet-c30f9fe5daf92a89849f7d6d22eacb603918b42d.zip
largely completing gnunet-nat tool, using new service API (but untested)
Diffstat (limited to 'src/nat/gnunet-nat.c')
-rw-r--r--src/nat/gnunet-nat.c210
1 files changed, 202 insertions, 8 deletions
diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c
index 10d5aef8e..6be3319b5 100644
--- a/src/nat/gnunet-nat.c
+++ b/src/nat/gnunet-nat.c
@@ -39,9 +39,9 @@ static int global_ret;
39static struct GNUNET_NAT_AutoHandle *ah; 39static struct GNUNET_NAT_AutoHandle *ah;
40 40
41/** 41/**
42 * Port we use. 42 * Port we advertise.
43 */ 43 */
44static unsigned int port; 44static unsigned int adv_port;
45 45
46/** 46/**
47 * Flag set to 1 if we use IPPROTO_UDP. 47 * Flag set to 1 if we use IPPROTO_UDP.
@@ -198,6 +198,68 @@ auto_config_cb (void *cls,
198 198
199 199
200/** 200/**
201 * Function called to report success or failure for
202 * NAT configuration test.
203 *
204 * @param cls closure
205 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
206 */
207static void
208test_report_cb (void *cls,
209 enum GNUNET_NAT_StatusCode result)
210{
211 nt = NULL;
212 PRINTF ("NAT test result: %s\n",
213 GNUNET_NAT_status2string (result));
214 test_finished ();
215}
216
217
218/**
219 * Signature of the callback passed to #GNUNET_NAT_register() for
220 * a function to call whenever our set of 'valid' addresses changes.
221 *
222 * @param cls closure
223 * @param add_remove #GNUNET_YES to add a new public IP address,
224 * #GNUNET_NO to remove a previous (now invalid) one
225 * @param ac address class the address belongs to
226 * @param addr either the previous or the new public IP address
227 * @param addrlen actual length of the @a addr
228 */
229static void
230address_cb (void *cls,
231 int add_remove,
232 enum GNUNET_NAT_AddressClass ac,
233 const struct sockaddr *addr,
234 socklen_t addrlen)
235{
236 // FIXME: print!
237}
238
239
240/**
241 * Signature of the callback passed to #GNUNET_NAT_register().
242 * for a function to call whenever someone asks us to do connection
243 * reversal.
244 *
245 * @param cls closure
246 * @param local_addr address where we received the request
247 * @param local_addrlen actual length of the @a local_addr
248 * @param remote_addr public IP address of the other peer
249 * @param remote_addrlen actual length of the @a remote_addr
250 */
251static void
252reversal_cb (void *cls,
253 const struct sockaddr *local_addr,
254 socklen_t local_addrlen,
255 const struct sockaddr *remote_addr,
256 socklen_t remote_addrlen)
257{
258 // FIXME: print!
259}
260
261
262/**
201 * Task run on shutdown. 263 * Task run on shutdown.
202 * 264 *
203 * @param cls NULL 265 * @param cls NULL
@@ -237,6 +299,14 @@ run (void *cls,
237 const char *cfgfile, 299 const char *cfgfile,
238 const struct GNUNET_CONFIGURATION_Handle *c) 300 const struct GNUNET_CONFIGURATION_Handle *c)
239{ 301{
302 uint8_t af;
303 struct sockaddr_in bind_sa;
304 struct sockaddr_in extern_sa;
305 struct sockaddr *local_sa;
306 struct sockaddr *remote_sa;
307 size_t local_len;
308 size_t remote_len;
309
240 if (use_tcp && use_udp) 310 if (use_tcp && use_udp)
241 { 311 {
242 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 312 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
@@ -256,6 +326,130 @@ run (void *cls,
256 global_ret = 1; 326 global_ret = 1;
257 return; 327 return;
258 } 328 }
329 if (NULL != bind_addr)
330 {
331 if (GNUNET_OK !=
332 GNUNET_STRINGS_to_address_ipv4 (bind_addr,
333 strlen (bind_addr),
334 &bind_sa))
335 {
336 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
337 "Invalid socket address `%s'\n",
338 bind_addr);
339 global_ret = 1;
340 return;
341 }
342 }
343 if (NULL != extern_addr)
344 {
345 if (GNUNET_OK !=
346 GNUNET_STRINGS_to_address_ipv4 (extern_addr,
347 strlen (extern_addr),
348 &extern_sa))
349 {
350 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
351 "Invalid socket address `%s'\n",
352 extern_addr);
353 global_ret = 1;
354 return;
355 }
356 }
357 if (NULL != local_addr)
358 {
359 local_len = GNUNET_STRINGS_parse_socket_addr (local_addr,
360 &af,
361 &local_sa);
362 if (0 == local_len)
363 {
364 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
365 "Invalid socket address `%s'\n",
366 local_addr);
367 global_ret = 1;
368 return;
369 }
370 }
371 if (NULL != remote_addr)
372 {
373 remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
374 &af,
375 &remote_sa);
376 if (0 == remote_len)
377 {
378 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
379 "Invalid socket address `%s'\n",
380 remote_addr);
381 global_ret = 1;
382 return;
383 }
384 }
385
386 if (NULL != bind_addr)
387 {
388 if (NULL == extern_addr)
389 extern_sa = bind_sa;
390 nt = GNUNET_NAT_test_start (c,
391 proto,
392 bind_sa.sin_addr,
393 ntohs (bind_sa.sin_port),
394 extern_sa.sin_addr,
395 ntohs (extern_sa.sin_port),
396 &test_report_cb,
397 NULL);
398 }
399
400 if (NULL != local_addr)
401 {
402 nh = GNUNET_NAT_register (c,
403 proto,
404 (uint16_t) adv_port,
405 1,
406 (const struct sockaddr **) &local_sa,
407 &local_len,
408 &address_cb,
409 (listen_reversal) ? &reversal_cb : NULL,
410 NULL);
411 }
412
413 if (NULL != remote_addr)
414 {
415 int ret;
416
417 if ( (NULL == nh) ||
418 (sizeof (struct sockaddr_in) != local_len) )
419 {
420 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
421 "Require IPv4 local address to initiate connection reversal\n");
422 global_ret = 1;
423 GNUNET_SCHEDULER_shutdown ();
424 return;
425 }
426 if (sizeof (struct sockaddr_in) != remote_len)
427 {
428 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
429 "Require IPv4 reversal target address\n");
430 global_ret = 1;
431 GNUNET_SCHEDULER_shutdown ();
432 return;
433 }
434 ret = GNUNET_NAT_request_reversal (nh,
435 (const struct sockaddr_in *) &local_sa,
436 (const struct sockaddr_in *) &remote_sa);
437 switch (ret)
438 {
439 case GNUNET_SYSERR:
440 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
441 "Connection reversal internal error\n");
442 break;
443 case GNUNET_NO:
444 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
445 "Connection reversal unavailable\n");
446 break;
447 case GNUNET_OK:
448 /* operation in progress */
449 break;
450 }
451 }
452
259 if (do_auto) 453 if (do_auto)
260 { 454 {
261 ah = GNUNET_NAT_autoconfig_start (c, 455 ah = GNUNET_NAT_autoconfig_start (c,
@@ -285,22 +479,22 @@ main (int argc,
285 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto }, 479 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto },
286 {'b', "bind", "ADDRESS", 480 {'b', "bind", "ADDRESS",
287 gettext_noop ("which IP and port are we bound to"), 481 gettext_noop ("which IP and port are we bound to"),
288 GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr}, 482 GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr },
289 {'e', "external", "ADDRESS", 483 {'e', "external", "ADDRESS",
290 gettext_noop ("which external IP and port should be used to test"), 484 gettext_noop ("which external IP and port should be used to test"),
291 GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr}, 485 GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
292 {'l', "local", "ADDRESS", 486 {'l', "local", "ADDRESS",
293 gettext_noop ("which IP and port are we locally using to listen to for connection reversals"), 487 gettext_noop ("which IP and port are we locally using to listen to for connection reversals"),
294 GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr}, 488 GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
295 {'r', "remote", "ADDRESS", 489 {'r', "remote", "ADDRESS",
296 gettext_noop ("which remote IP and port should be asked for connection reversal"), 490 gettext_noop ("which remote IP and port should be asked for connection reversal"),
297 GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr}, 491 GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
298 {'L', "listen", NULL, 492 {'L', "listen", NULL,
299 gettext_noop ("listen for connection reversal requests"), 493 gettext_noop ("listen for connection reversal requests"),
300 GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal }, 494 GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
301 {'p', "port", NULL, 495 {'p', "port", NULL,
302 gettext_noop ("port to use"), 496 gettext_noop ("port to use to advertise"),
303 GNUNET_YES, &GNUNET_GETOPT_set_uint, &port}, 497 GNUNET_YES, &GNUNET_GETOPT_set_uint, &adv_port },
304 {'s', "stun", NULL, 498 {'s', "stun", NULL,
305 gettext_noop ("enable STUN processing"), 499 gettext_noop ("enable STUN processing"),
306 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun }, 500 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },