diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2013-08-15 16:27:27 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2013-08-15 16:27:27 +0000 |
commit | 356d73f3e59d50ab3aeee64d6e5384089428747b (patch) | |
tree | b683d79727fa4074b2877d1efd50b53fd454253b /src/testing | |
parent | 2f540d8a505ee8842ea322513b381d7f3b48362d (diff) | |
download | gnunet-356d73f3e59d50ab3aeee64d6e5384089428747b.tar.gz gnunet-356d73f3e59d50ab3aeee64d6e5384089428747b.zip |
- make no distinction between TCP and UDP port while reserving them
Diffstat (limited to 'src/testing')
-rw-r--r-- | src/testing/Makefile.am | 11 | ||||
-rw-r--r-- | src/testing/bindfail.c | 37 | ||||
-rw-r--r-- | src/testing/bindok.c | 30 | ||||
-rw-r--r-- | src/testing/test_testing_portreservation.c | 12 | ||||
-rw-r--r-- | src/testing/testing.c | 64 |
5 files changed, 31 insertions, 123 deletions
diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am index 4f1d8fe17..f2355da5f 100644 --- a/src/testing/Makefile.am +++ b/src/testing/Makefile.am | |||
@@ -97,17 +97,6 @@ test_testing_sharedservices_LDADD = \ | |||
97 | $(top_builddir)/src/testing/libgnunettesting.la \ | 97 | $(top_builddir)/src/testing/libgnunettesting.la \ |
98 | $(top_builddir)/src/util/libgnunetutil.la | 98 | $(top_builddir)/src/util/libgnunetutil.la |
99 | 99 | ||
100 | |||
101 | |||
102 | EXTRA_DIST = \ | 100 | EXTRA_DIST = \ |
103 | test_testing_defaults.conf | 101 | test_testing_defaults.conf |
104 | 102 | ||
105 | |||
106 | noinst_PROGRAMS = \ | ||
107 | bindfail bindok | ||
108 | |||
109 | bindok_SOURCES = bindok.c | ||
110 | |||
111 | bindfail_SOURCES = bindfail.c | ||
112 | bindfail_LDADD = $(top_builddir)/src/util/libgnunetutil.la | ||
113 | |||
diff --git a/src/testing/bindfail.c b/src/testing/bindfail.c deleted file mode 100644 index 629c82df6..000000000 --- a/src/testing/bindfail.c +++ /dev/null | |||
@@ -1,37 +0,0 @@ | |||
1 | #include "platform.h" | ||
2 | #include "gnunet_util_lib.h" | ||
3 | |||
4 | int main() | ||
5 | { | ||
6 | uint16_t port = 12035; | ||
7 | struct GNUNET_NETWORK_Handle *sock1; | ||
8 | struct GNUNET_NETWORK_Handle *sock2; | ||
9 | struct sockaddr_in addr; | ||
10 | int proto; | ||
11 | |||
12 | proto = SOCK_STREAM; | ||
13 | (void) memset (&addr, 0, sizeof (struct sockaddr_in)); | ||
14 | sock1 = GNUNET_NETWORK_socket_create (AF_INET, proto, 0); | ||
15 | sock2 = GNUNET_NETWORK_socket_create (AF_INET, proto, 0); | ||
16 | |||
17 | addr.sin_port = htons (port); | ||
18 | addr.sin_addr.s_addr = INADDR_ANY; | ||
19 | |||
20 | if (GNUNET_SYSERR == | ||
21 | GNUNET_NETWORK_socket_bind (sock1, (const struct sockaddr *) &addr, | ||
22 | sizeof (addr), 0)) | ||
23 | { | ||
24 | fprintf (stderr, "first bind failed. check port\n"); | ||
25 | return 1; | ||
26 | } | ||
27 | if (GNUNET_SYSERR == | ||
28 | GNUNET_NETWORK_socket_bind (sock2, (const struct sockaddr *) &addr, | ||
29 | sizeof (addr), 0)) | ||
30 | { | ||
31 | printf ("All OK\n"); | ||
32 | return 0; | ||
33 | } | ||
34 | fprintf (stderr, "Second bind succeeded! WTF!!\n"); | ||
35 | fgetc (stdin); | ||
36 | return 1; | ||
37 | } | ||
diff --git a/src/testing/bindok.c b/src/testing/bindok.c deleted file mode 100644 index a4b74e7a0..000000000 --- a/src/testing/bindok.c +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | #include "platform.h" | ||
2 | |||
3 | int main() | ||
4 | { | ||
5 | uint16_t port = 12035; | ||
6 | int sock1; | ||
7 | int sock2; | ||
8 | struct sockaddr_in addr; | ||
9 | |||
10 | (void) memset (&addr, 0, sizeof (struct sockaddr_in)); | ||
11 | sock1 = socket (AF_INET, SOCK_DGRAM, 0); | ||
12 | sock2 = socket (AF_INET, SOCK_DGRAM, 0); | ||
13 | |||
14 | addr.sin_port = htons (port); | ||
15 | addr.sin_addr.s_addr = INADDR_ANY; | ||
16 | |||
17 | if (0 != bind (sock1, (const struct sockaddr *) &addr, sizeof (addr))) | ||
18 | { | ||
19 | perror ("bind"); | ||
20 | return 1; | ||
21 | } | ||
22 | if (0 != bind (sock2, (const struct sockaddr *) &addr, sizeof (addr))) | ||
23 | { | ||
24 | printf ("All OK\n"); | ||
25 | return 0; | ||
26 | } | ||
27 | fprintf (stderr, "Second bind succeeded! WTF!!\n"); | ||
28 | fgetc (stdin); | ||
29 | return 1; | ||
30 | } | ||
diff --git a/src/testing/test_testing_portreservation.c b/src/testing/test_testing_portreservation.c index 81ba67e96..50d44a91c 100644 --- a/src/testing/test_testing_portreservation.c +++ b/src/testing/test_testing_portreservation.c | |||
@@ -52,27 +52,27 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
52 | system = GNUNET_TESTING_system_create ("/tmp/gnunet-testing-new", | 52 | system = GNUNET_TESTING_system_create ("/tmp/gnunet-testing-new", |
53 | "localhost", NULL, NULL); | 53 | "localhost", NULL, NULL); |
54 | GNUNET_assert (NULL != system); | 54 | GNUNET_assert (NULL != system); |
55 | new_port1 = GNUNET_TESTING_reserve_port (system, GNUNET_YES); | 55 | new_port1 = GNUNET_TESTING_reserve_port (system); |
56 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 56 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
57 | "Reserved TCP port %u\n", new_port1); | 57 | "Reserved TCP port %u\n", new_port1); |
58 | if (0 == new_port1) | 58 | if (0 == new_port1) |
59 | goto end; | 59 | goto end; |
60 | new_port2 = GNUNET_TESTING_reserve_port (system, GNUNET_YES); | 60 | new_port2 = GNUNET_TESTING_reserve_port (system); |
61 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 61 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
62 | "Reserved TCP port %u\n", new_port2); | 62 | "Reserved TCP port %u\n", new_port2); |
63 | if (0 == new_port2) | 63 | if (0 == new_port2) |
64 | goto end; | 64 | goto end; |
65 | GNUNET_assert (new_port1 != new_port2); | 65 | GNUNET_assert (new_port1 != new_port2); |
66 | GNUNET_TESTING_release_port (system, GNUNET_YES, new_port1); | 66 | GNUNET_TESTING_release_port (system, new_port1); |
67 | old_port1 = new_port1; | 67 | old_port1 = new_port1; |
68 | new_port1 = 0; | 68 | new_port1 = 0; |
69 | new_port1 = GNUNET_TESTING_reserve_port (system, GNUNET_YES); | 69 | new_port1 = GNUNET_TESTING_reserve_port (system); |
70 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 70 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
71 | "Reserved TCP port %u\n", new_port1); | 71 | "Reserved TCP port %u\n", new_port1); |
72 | GNUNET_assert (0 != new_port1); | 72 | GNUNET_assert (0 != new_port1); |
73 | GNUNET_assert (old_port1 == new_port1); | 73 | GNUNET_assert (old_port1 == new_port1); |
74 | GNUNET_TESTING_release_port (system, GNUNET_YES, new_port1); | 74 | GNUNET_TESTING_release_port (system, new_port1); |
75 | GNUNET_TESTING_release_port (system, GNUNET_YES, new_port2); | 75 | GNUNET_TESTING_release_port (system, new_port2); |
76 | status = GNUNET_OK; | 76 | status = GNUNET_OK; |
77 | 77 | ||
78 | end: | 78 | end: |
diff --git a/src/testing/testing.c b/src/testing/testing.c index 70eebc3cc..9e193bd9c 100644 --- a/src/testing/testing.c +++ b/src/testing/testing.c | |||
@@ -132,26 +132,14 @@ struct GNUNET_TESTING_System | |||
132 | unsigned int n_shared_services; | 132 | unsigned int n_shared_services; |
133 | 133 | ||
134 | /** | 134 | /** |
135 | * Bitmap where each TCP port that has already been reserved for | 135 | * Bitmap where each port that has already been reserved for some GNUnet peer |
136 | * some GNUnet peer is recorded. Note that we additionally need to | 136 | * is recorded. Note that we make no distinction between TCP and UDP ports |
137 | * test if a port is already in use by non-GNUnet components before | 137 | * and test if a port is already in use before assigning it to a peer/service. |
138 | * assigning it to a peer/service. If we detect that a port is | 138 | * If we detect that a port is already in use, we also mark it in this bitmap. |
139 | * already in use, we also mark it in this bitmap. So all the bits | 139 | * So all the bits that are zero merely indicate ports that MIGHT be available |
140 | * that are zero merely indicate ports that MIGHT be available for | 140 | * for peers. |
141 | * peers. | ||
142 | */ | 141 | */ |
143 | uint32_t reserved_tcp_ports[65536 / 32]; | 142 | uint32_t reserved_ports[65536 / 32]; |
144 | |||
145 | /** | ||
146 | * Bitmap where each UDP port that has already been reserved for | ||
147 | * some GNUnet peer is recorded. Note that we additionally need to | ||
148 | * test if a port is already in use by non-GNUnet components before | ||
149 | * assigning it to a peer/service. If we detect that a port is | ||
150 | * already in use, we also mark it in this bitmap. So all the bits | ||
151 | * that are zero merely indicate ports that MIGHT be available for | ||
152 | * peers. | ||
153 | */ | ||
154 | uint32_t reserved_udp_ports[65536 / 32]; | ||
155 | 143 | ||
156 | /** | 144 | /** |
157 | * Counter we use to make service home paths unique on this system; | 145 | * Counter we use to make service home paths unique on this system; |
@@ -568,12 +556,10 @@ GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system, | |||
568 | * Reserve a TCP or UDP port for a peer. | 556 | * Reserve a TCP or UDP port for a peer. |
569 | * | 557 | * |
570 | * @param system system to use for reservation tracking | 558 | * @param system system to use for reservation tracking |
571 | * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP | ||
572 | * @return 0 if no free port was available | 559 | * @return 0 if no free port was available |
573 | */ | 560 | */ |
574 | uint16_t | 561 | uint16_t |
575 | GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system, | 562 | GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system) |
576 | int is_tcp) | ||
577 | { | 563 | { |
578 | struct GNUNET_NETWORK_Handle *socket; | 564 | struct GNUNET_NETWORK_Handle *socket; |
579 | struct addrinfo hint; | 565 | struct addrinfo hint; |
@@ -597,15 +583,14 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system, | |||
597 | open in the respective address family | 583 | open in the respective address family |
598 | */ | 584 | */ |
599 | hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */ | 585 | hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */ |
600 | hint.ai_socktype = (GNUNET_YES == is_tcp)? SOCK_STREAM : SOCK_DGRAM; | 586 | hint.ai_socktype = 0; |
601 | hint.ai_protocol = 0; | 587 | hint.ai_protocol = 0; |
602 | hint.ai_addrlen = 0; | 588 | hint.ai_addrlen = 0; |
603 | hint.ai_addr = NULL; | 589 | hint.ai_addr = NULL; |
604 | hint.ai_canonname = NULL; | 590 | hint.ai_canonname = NULL; |
605 | hint.ai_next = NULL; | 591 | hint.ai_next = NULL; |
606 | hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */ | 592 | hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */ |
607 | port_buckets = (GNUNET_YES == is_tcp) ? | 593 | port_buckets = system->reserved_ports; |
608 | system->reserved_tcp_ports : system->reserved_udp_ports; | ||
609 | for (index = (system->lowport / 32) + 1; index < (system->highport / 32); index++) | 594 | for (index = (system->lowport / 32) + 1; index < (system->highport / 32); index++) |
610 | { | 595 | { |
611 | xor_image = (UINT32_MAX ^ port_buckets[index]); | 596 | xor_image = (UINT32_MAX ^ port_buckets[index]); |
@@ -629,10 +614,17 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system, | |||
629 | bind_status = GNUNET_NO; | 614 | bind_status = GNUNET_NO; |
630 | for (ai = ret; NULL != ai; ai = ai->ai_next) | 615 | for (ai = ret; NULL != ai; ai = ai->ai_next) |
631 | { | 616 | { |
632 | socket = GNUNET_NETWORK_socket_create (ai->ai_family, | 617 | socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_STREAM, 0); |
633 | (GNUNET_YES == is_tcp) ? | 618 | if (NULL == socket) |
634 | SOCK_STREAM : SOCK_DGRAM, | 619 | continue; |
635 | 0); | 620 | bind_status = GNUNET_NETWORK_socket_bind (socket, |
621 | ai->ai_addr, | ||
622 | ai->ai_addrlen, | ||
623 | 0); | ||
624 | GNUNET_NETWORK_socket_close (socket); | ||
625 | if (GNUNET_OK != bind_status) | ||
626 | break; | ||
627 | socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_DGRAM, 0); | ||
636 | if (NULL == socket) | 628 | if (NULL == socket) |
637 | continue; | 629 | continue; |
638 | bind_status = GNUNET_NETWORK_socket_bind (socket, | 630 | bind_status = GNUNET_NETWORK_socket_bind (socket, |
@@ -663,20 +655,17 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system, | |||
663 | * (used during GNUNET_TESTING_peer_destroy). | 655 | * (used during GNUNET_TESTING_peer_destroy). |
664 | * | 656 | * |
665 | * @param system system to use for reservation tracking | 657 | * @param system system to use for reservation tracking |
666 | * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP | ||
667 | * @param port reserved port to release | 658 | * @param port reserved port to release |
668 | */ | 659 | */ |
669 | void | 660 | void |
670 | GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system, | 661 | GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system, |
671 | int is_tcp, | ||
672 | uint16_t port) | 662 | uint16_t port) |
673 | { | 663 | { |
674 | uint32_t *port_buckets; | 664 | uint32_t *port_buckets; |
675 | uint16_t bucket; | 665 | uint16_t bucket; |
676 | uint16_t pos; | 666 | uint16_t pos; |
677 | 667 | ||
678 | port_buckets = (GNUNET_YES == is_tcp) ? | 668 | port_buckets = system->reserved_ports; |
679 | system->reserved_tcp_ports : system->reserved_udp_ports; | ||
680 | bucket = port / 32; | 669 | bucket = port / 32; |
681 | pos = port % 32; | 670 | pos = port % 32; |
682 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port); | 671 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port); |
@@ -819,8 +808,7 @@ update_config (void *cls, const char *section, const char *option, | |||
819 | GNUNET_CONFIGURATION_get_value_yesno (uc->cfg, "testing", | 808 | GNUNET_CONFIGURATION_get_value_yesno (uc->cfg, "testing", |
820 | single_variable))) | 809 | single_variable))) |
821 | { | 810 | { |
822 | /* FIXME: What about UDP? */ | 811 | new_port = GNUNET_TESTING_reserve_port (uc->system); |
823 | new_port = GNUNET_TESTING_reserve_port (uc->system, GNUNET_YES); | ||
824 | if (0 == new_port) | 812 | if (0 == new_port) |
825 | { | 813 | { |
826 | uc->status = GNUNET_SYSERR; | 814 | uc->status = GNUNET_SYSERR; |
@@ -998,7 +986,7 @@ associate_shared_service (struct GNUNET_TESTING_System *system, | |||
998 | (void) GNUNET_asprintf (&service_home, "%s/shared/%s/%u", | 986 | (void) GNUNET_asprintf (&service_home, "%s/shared/%s/%u", |
999 | system->tmppath, ss->sname, ss->n_instances); | 987 | system->tmppath, ss->sname, ss->n_instances); |
1000 | (void) GNUNET_asprintf (&i->unix_sock, "%s/sock", service_home); | 988 | (void) GNUNET_asprintf (&i->unix_sock, "%s/sock", service_home); |
1001 | port = GNUNET_TESTING_reserve_port (system, GNUNET_YES); | 989 | port = GNUNET_TESTING_reserve_port (system); |
1002 | if (0 == port) | 990 | if (0 == port) |
1003 | { | 991 | { |
1004 | GNUNET_free (service_home); | 992 | GNUNET_free (service_home); |
@@ -1589,9 +1577,7 @@ GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer) | |||
1589 | if (NULL != peer->ports) | 1577 | if (NULL != peer->ports) |
1590 | { | 1578 | { |
1591 | for (cnt = 0; cnt < peer->nports; cnt++) | 1579 | for (cnt = 0; cnt < peer->nports; cnt++) |
1592 | GNUNET_TESTING_release_port (peer->system, | 1580 | GNUNET_TESTING_release_port (peer->system, peer->ports[cnt]); |
1593 | GNUNET_YES, | ||
1594 | peer->ports[cnt]); | ||
1595 | GNUNET_free (peer->ports); | 1581 | GNUNET_free (peer->ports); |
1596 | } | 1582 | } |
1597 | GNUNET_free (peer); | 1583 | GNUNET_free (peer); |