aboutsummaryrefslogtreecommitdiff
path: root/src/nat/nat_test.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-06-24 15:15:15 +0000
committerChristian Grothoff <christian@grothoff.org>2016-06-24 15:15:15 +0000
commita5b71afba9ee175223fc90e53359adfe6157c33a (patch)
tree5648b65e1a40efddba75693744ff01596a4f06ba /src/nat/nat_test.c
parent4c1969ee4afbb673085e1850f4bf6d5c3950344b (diff)
downloadgnunet-a5b71afba9ee175223fc90e53359adfe6157c33a.tar.gz
gnunet-a5b71afba9ee175223fc90e53359adfe6157c33a.zip
migrate nat_test to MQ
Diffstat (limited to 'src/nat/nat_test.c')
-rw-r--r--src/nat/nat_test.c204
1 files changed, 144 insertions, 60 deletions
diff --git a/src/nat/nat_test.c b/src/nat/nat_test.c
index b2f22781e..803ff23e3 100644
--- a/src/nat/nat_test.c
+++ b/src/nat/nat_test.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V. 3 Copyright (C) 2011, 2016 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -60,7 +60,7 @@ struct NatActivity
60 /** 60 /**
61 * Task reading from the incoming connection. 61 * Task reading from the incoming connection.
62 */ 62 */
63 struct GNUNET_SCHEDULER_Task * rtask; 63 struct GNUNET_SCHEDULER_Task *rtask;
64}; 64};
65 65
66 66
@@ -82,7 +82,12 @@ struct ClientActivity
82 /** 82 /**
83 * Socket of the incoming connection. 83 * Socket of the incoming connection.
84 */ 84 */
85 struct GNUNET_CLIENT_Connection *client; 85 struct GNUNET_MQ_Handle *mq;
86
87 /**
88 * Handle to overall NAT test.
89 */
90 struct GNUNET_NAT_Test *h;
86 91
87}; 92};
88 93
@@ -141,15 +146,15 @@ struct GNUNET_NAT_Test
141 /** 146 /**
142 * Identity of task for the listen socket (if any) 147 * Identity of task for the listen socket (if any)
143 */ 148 */
144 struct GNUNET_SCHEDULER_Task * ltask; 149 struct GNUNET_SCHEDULER_Task *ltask;
145 150
146 /** 151 /**
147 * Task identifier for the timeout (if any) 152 * Task identifier for the timeout (if any)
148 */ 153 */
149 struct GNUNET_SCHEDULER_Task * ttask; 154 struct GNUNET_SCHEDULER_Task *ttask;
150 155
151 /** 156 /**
152 * GNUNET_YES if we're testing TCP 157 * #GNUNET_YES if we're testing TCP
153 */ 158 */
154 int is_tcp; 159 int is_tcp;
155 160
@@ -176,7 +181,7 @@ struct GNUNET_NAT_Test
176 * 181 *
177 * @param cls closure, our `struct GNUNET_NAT_Handle` 182 * @param cls closure, our `struct GNUNET_NAT_Handle`
178 * @param addr public IP address of the other peer 183 * @param addr public IP address of the other peer
179 * @param addrlen actual lenght of the address 184 * @param addrlen actual lenght of the @a addr
180 */ 185 */
181static void 186static void
182reversal_cb (void *cls, 187reversal_cb (void *cls,
@@ -196,7 +201,8 @@ reversal_cb (void *cls,
196 return; /* wrong port */ 201 return; /* wrong port */
197 } 202 }
198 /* report success */ 203 /* report success */
199 h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS); 204 h->report (h->report_cls,
205 GNUNET_NAT_ERROR_SUCCESS);
200} 206}
201 207
202 208
@@ -217,14 +223,19 @@ do_udp_read (void *cls)
217 tst->ltask = 223 tst->ltask =
218 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 224 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
219 tst->lsock, 225 tst->lsock,
220 &do_udp_read, tst); 226 &do_udp_read,
227 tst);
221 if ((NULL != tc->write_ready) && 228 if ((NULL != tc->write_ready) &&
222 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) && 229 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
230 tst->lsock)) &&
223 (sizeof (data) == 231 (sizeof (data) ==
224 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data)))) 232 GNUNET_NETWORK_socket_recv (tst->lsock,
233 &data,
234 sizeof (data))))
225 { 235 {
226 if (data == tst->data) 236 if (data == tst->data)
227 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 237 tst->report (tst->report_cls,
238 GNUNET_NAT_ERROR_SUCCESS);
228 else 239 else
229 LOG (GNUNET_ERROR_TYPE_DEBUG, 240 LOG (GNUNET_ERROR_TYPE_DEBUG,
230 "Received data mismatches expected value\n"); 241 "Received data mismatches expected value\n");
@@ -256,12 +267,16 @@ do_read (void *cls)
256 tst->na_tail, 267 tst->na_tail,
257 na); 268 na);
258 if ((NULL != tc->write_ready) && 269 if ((NULL != tc->write_ready) &&
259 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) && 270 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
271 na->sock)) &&
260 (sizeof (data) == 272 (sizeof (data) ==
261 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data)))) 273 GNUNET_NETWORK_socket_recv (na->sock,
274 &data,
275 sizeof (data))))
262 { 276 {
263 if (data == tst->data) 277 if (data == tst->data)
264 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 278 tst->report (tst->report_cls,
279 GNUNET_NAT_ERROR_SUCCESS);
265 else 280 else
266 LOG (GNUNET_ERROR_TYPE_DEBUG, 281 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 "Received data does not match expected value\n"); 282 "Received data does not match expected value\n");
@@ -287,14 +302,17 @@ do_accept (void *cls)
287 struct GNUNET_NETWORK_Handle *s; 302 struct GNUNET_NETWORK_Handle *s;
288 struct NatActivity *wl; 303 struct NatActivity *wl;
289 304
290 tst->ltask = 305 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
291 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 306 tst->lsock,
292 tst->lsock, 307 &do_accept,
293 &do_accept, tst); 308 tst);
294 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL); 309 s = GNUNET_NETWORK_socket_accept (tst->lsock,
310 NULL,
311 NULL);
295 if (NULL == s) 312 if (NULL == s)
296 { 313 {
297 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept"); 314 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
315 "accept");
298 return; /* odd error */ 316 return; /* odd error */
299 } 317 }
300 LOG (GNUNET_ERROR_TYPE_DEBUG, 318 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -305,7 +323,8 @@ do_accept (void *cls)
305 wl->rtask = 323 wl->rtask =
306 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 324 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
307 wl->sock, 325 wl->sock,
308 &do_read, wl); 326 &do_read,
327 wl);
309 GNUNET_CONTAINER_DLL_insert (tst->na_head, 328 GNUNET_CONTAINER_DLL_insert (tst->na_head,
310 tst->na_tail, 329 tst->na_tail,
311 wl); 330 wl);
@@ -313,6 +332,28 @@ do_accept (void *cls)
313 332
314 333
315/** 334/**
335 * We got disconnected from the NAT server. Stop
336 * waiting for a reply.
337 *
338 * @param cls the `struct ClientActivity`
339 * @param error error code
340 */
341static void
342mq_error_handler (void *cls,
343 enum GNUNET_MQ_Error error)
344{
345 struct ClientActivity *ca = cls;
346 struct GNUNET_NAT_Test *tst = ca->h;
347
348 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
349 tst->ca_tail,
350 ca);
351 GNUNET_MQ_destroy (ca->mq);
352 GNUNET_free (ca);
353}
354
355
356/**
316 * Address-callback, used to send message to gnunet-nat-server. 357 * Address-callback, used to send message to gnunet-nat-server.
317 * 358 *
318 * @param cls closure 359 * @param cls closure
@@ -329,8 +370,8 @@ addr_cb (void *cls,
329{ 370{
330 struct GNUNET_NAT_Test *h = cls; 371 struct GNUNET_NAT_Test *h = cls;
331 struct ClientActivity *ca; 372 struct ClientActivity *ca;
332 struct GNUNET_CLIENT_Connection *client; 373 struct GNUNET_MQ_Envelope *env;
333 struct GNUNET_NAT_TestMessage msg; 374 struct GNUNET_NAT_TestMessage *msg;
334 const struct sockaddr_in *sa; 375 const struct sockaddr_in *sa;
335 376
336 if (GNUNET_YES != add_remove) 377 if (GNUNET_YES != add_remove)
@@ -339,35 +380,41 @@ addr_cb (void *cls,
339 { 380 {
340 LOG (GNUNET_ERROR_TYPE_DEBUG, 381 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "NAT test ignores IPv6 address `%s' returned from NAT library\n", 382 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
342 GNUNET_a2s (addr, addrlen)); 383 GNUNET_a2s (addr,
384 addrlen));
343 return; /* ignore IPv6 here */ 385 return; /* ignore IPv6 here */
344 } 386 }
345 LOG (GNUNET_ERROR_TYPE_INFO, 387 LOG (GNUNET_ERROR_TYPE_INFO,
346 "Asking gnunet-nat-server to connect to `%s'\n", 388 "Asking gnunet-nat-server to connect to `%s'\n",
347 GNUNET_a2s (addr, addrlen)); 389 GNUNET_a2s (addr,
348 sa = (const struct sockaddr_in *) addr; 390 addrlen));
349 msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage)); 391
350 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST); 392 ca = GNUNET_new (struct ClientActivity);
351 msg.dst_ipv4 = sa->sin_addr.s_addr; 393 ca->h = h;
352 msg.dport = sa->sin_port; 394 ca->mq = GNUNET_CLIENT_connecT (h->cfg,
353 msg.data = h->data; 395 "gnunet-nat-server",
354 msg.is_tcp = htonl ((uint32_t) h->is_tcp); 396 NULL,
355 397 &mq_error_handler,
356 client = GNUNET_CLIENT_connect ("gnunet-nat-server", h->cfg); 398 ca);
357 if (NULL == client) 399 if (NULL == ca->mq)
358 { 400 {
401 GNUNET_free (ca);
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 _("Failed to connect to `gnunet-nat-server'\n")); 403 _("Failed to connect to `gnunet-nat-server'\n"));
361 return; 404 return;
362 } 405 }
363 ca = GNUNET_new (struct ClientActivity); 406 GNUNET_CONTAINER_DLL_insert (h->ca_head,
364 ca->client = client; 407 h->ca_tail,
365 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca); 408 ca);
366 GNUNET_break (GNUNET_OK == 409 sa = (const struct sockaddr_in *) addr;
367 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header, 410 env = GNUNET_MQ_msg (msg,
368 NAT_SERVER_TIMEOUT, 411 GNUNET_MESSAGE_TYPE_NAT_TEST);
369 GNUNET_YES, NULL, 412 msg->dst_ipv4 = sa->sin_addr.s_addr;
370 NULL)); 413 msg->dport = sa->sin_port;
414 msg->data = h->data;
415 msg->is_tcp = htonl ((uint32_t) h->is_tcp);
416 GNUNET_MQ_send (ca->mq,
417 env);
371} 418}
372 419
373 420
@@ -418,8 +465,12 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
418{ 465{
419 struct GNUNET_NAT_Test *nh; 466 struct GNUNET_NAT_Test *nh;
420 struct sockaddr_in sa; 467 struct sockaddr_in sa;
421 const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa }; 468 const struct sockaddr *addrs[] = {
422 const socklen_t addrlens[] = { sizeof (sa) }; 469 (const struct sockaddr *) &sa
470 };
471 const socklen_t addrlens[] = {
472 sizeof (sa)
473 };
423 474
424 memset (&sa, 0, sizeof (sa)); 475 memset (&sa, 0, sizeof (sa));
425 sa.sin_family = AF_INET; 476 sa.sin_family = AF_INET;
@@ -439,10 +490,16 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
439 if (0 == bnd_port) 490 if (0 == bnd_port)
440 { 491 {
441 nh->nat 492 nh->nat
442 = GNUNET_NAT_register (cfg, is_tcp, 0, 0, 493 = GNUNET_NAT_register (cfg,
443 NULL, NULL, 494 is_tcp,
495 0,
496 0,
497 NULL,
498 NULL,
444 &addr_cb, 499 &addr_cb,
445 &reversal_cb, nh, NULL); 500 &reversal_cb,
501 nh,
502 NULL);
446 } 503 }
447 else 504 else
448 { 505 {
@@ -453,12 +510,14 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
453 0); 510 0);
454 if ((nh->lsock == NULL) || 511 if ((nh->lsock == NULL) ||
455 (GNUNET_OK != 512 (GNUNET_OK !=
456 GNUNET_NETWORK_socket_bind (nh->lsock, (const struct sockaddr *) &sa, 513 GNUNET_NETWORK_socket_bind (nh->lsock,
514 (const struct sockaddr *) &sa,
457 sizeof (sa)))) 515 sizeof (sa))))
458 { 516 {
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
460 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"), 518 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
461 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)), 519 GNUNET_a2s ((const struct sockaddr *) &sa,
520 sizeof (sa)),
462 STRERROR (errno)); 521 STRERROR (errno));
463 if (NULL != nh->lsock) 522 if (NULL != nh->lsock)
464 { 523 {
@@ -466,31 +525,43 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
466 nh->lsock = NULL; 525 nh->lsock = NULL;
467 } 526 }
468 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR; 527 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
469 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh); 528 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout,
529 nh);
470 return nh; 530 return nh;
471 } 531 }
472 if (GNUNET_YES == is_tcp) 532 if (GNUNET_YES == is_tcp)
473 { 533 {
474 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5)); 534 GNUNET_break (GNUNET_OK ==
535 GNUNET_NETWORK_socket_listen (nh->lsock,
536 5));
475 nh->ltask = 537 nh->ltask =
476 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 538 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
477 nh->lsock, 539 nh->lsock,
478 &do_accept, nh); 540 &do_accept,
541 nh);
479 } 542 }
480 else 543 else
481 { 544 {
482 nh->ltask = 545 nh->ltask =
483 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 546 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
484 nh->lsock, 547 nh->lsock,
485 &do_udp_read, nh); 548 &do_udp_read,
549 nh);
486 } 550 }
487 LOG (GNUNET_ERROR_TYPE_INFO, 551 LOG (GNUNET_ERROR_TYPE_INFO,
488 "NAT test listens on port %u (%s)\n", 552 "NAT test listens on port %u (%s)\n",
489 bnd_port, 553 bnd_port,
490 (GNUNET_YES == is_tcp) ? "tcp" : "udp"); 554 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
491 nh->nat = GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, 555 nh->nat = GNUNET_NAT_register (cfg,
492 addrs, addrlens, 556 is_tcp,
493 &addr_cb, NULL, nh, NULL); 557 adv_port,
558 1,
559 addrs,
560 addrlens,
561 &addr_cb,
562 NULL,
563 nh,
564 NULL);
494 if (NULL == nh->nat) 565 if (NULL == nh->nat)
495 { 566 {
496 LOG (GNUNET_ERROR_TYPE_INFO, 567 LOG (GNUNET_ERROR_TYPE_INFO,
@@ -506,7 +577,8 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
506 nh->lsock = NULL; 577 nh->lsock = NULL;
507 } 578 }
508 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED; 579 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
509 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh); 580 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout,
581 nh);
510 return nh; 582 return nh;
511 } 583 }
512 } 584 }
@@ -535,7 +607,7 @@ GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
535 GNUNET_CONTAINER_DLL_remove (tst->ca_head, 607 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
536 tst->ca_tail, 608 tst->ca_tail,
537 cpos); 609 cpos);
538 GNUNET_CLIENT_disconnect (cpos->client); 610 GNUNET_MQ_destroy (cpos->mq);
539 GNUNET_free (cpos); 611 GNUNET_free (cpos);
540 } 612 }
541 while (NULL != (pos = tst->na_head)) 613 while (NULL != (pos = tst->na_head))
@@ -548,13 +620,25 @@ GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
548 GNUNET_free (pos); 620 GNUNET_free (pos);
549 } 621 }
550 if (NULL != tst->ttask) 622 if (NULL != tst->ttask)
623 {
551 GNUNET_SCHEDULER_cancel (tst->ttask); 624 GNUNET_SCHEDULER_cancel (tst->ttask);
625 tst->ttask = NULL;
626 }
552 if (NULL != tst->ltask) 627 if (NULL != tst->ltask)
628 {
553 GNUNET_SCHEDULER_cancel (tst->ltask); 629 GNUNET_SCHEDULER_cancel (tst->ltask);
630 tst->ltask = NULL;
631 }
554 if (NULL != tst->lsock) 632 if (NULL != tst->lsock)
633 {
555 GNUNET_NETWORK_socket_close (tst->lsock); 634 GNUNET_NETWORK_socket_close (tst->lsock);
635 tst->lsock = NULL;
636 }
556 if (NULL != tst->nat) 637 if (NULL != tst->nat)
638 {
557 GNUNET_NAT_unregister (tst->nat); 639 GNUNET_NAT_unregister (tst->nat);
640 tst->nat = NULL;
641 }
558 GNUNET_free (tst); 642 GNUNET_free (tst);
559} 643}
560 644