aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-05-26 10:54:09 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-05-26 10:54:09 +0000
commitcf99c0faa8634abfdcb54a7c7e8d2e5e72210e36 (patch)
treea6306dca0dbdd36cfffbd174233990181f485b89
parent7b869adf703f0585dacc422f89acc57bd96c51a6 (diff)
downloadgnunet-cf99c0faa8634abfdcb54a7c7e8d2e5e72210e36.tar.gz
gnunet-cf99c0faa8634abfdcb54a7c7e8d2e5e72210e36.zip
-fixed infinite loop
-rw-r--r--src/testing/testing_new.c78
1 files changed, 43 insertions, 35 deletions
diff --git a/src/testing/testing_new.c b/src/testing/testing_new.c
index a836a4905..4622ae874 100644
--- a/src/testing/testing_new.c
+++ b/src/testing/testing_new.c
@@ -137,6 +137,17 @@ struct GNUNET_TESTING_Peer
137 137
138 138
139/** 139/**
140 * The lowest port bucket for ports available for GNUnet testing
141 */
142#define LOW_PORT_BUCKET (LOW_PORT / 32)
143
144/**
145 * The highest port bucket for ports available for GNUNET_testing
146 */
147#define HIGH_PORT_BUCKET (HIGH_PORT / 32)
148
149
150/**
140 * Create a system handle. There must only be one system 151 * Create a system handle. There must only be one system
141 * handle per operating system. 152 * handle per operating system.
142 * 153 *
@@ -199,57 +210,54 @@ reserve_port (struct GNUNET_TESTING_System *system,
199 struct addrinfo *ret; 210 struct addrinfo *ret;
200 uint32_t *port_buckets; 211 uint32_t *port_buckets;
201 char *open_port_str; 212 char *open_port_str;
213 int pos;
214 int bind_status;
202 uint32_t xor_image; 215 uint32_t xor_image;
203 uint16_t index; 216 uint16_t index;
204 uint16_t open_port; 217 uint16_t open_port;
205 uint8_t pos;
206 218
207 if (GNUNET_YES == is_tcp) 219 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
208 { 220 hint.ai_socktype = (GNUNET_YES == is_tcp)? SOCK_STREAM : SOCK_DGRAM;
209 socket = GNUNET_NETWORK_socket_create (AF_UNSPEC, 221 hint.ai_protocol = 0;
210 SOCK_STREAM, 222 hint.ai_addrlen = 0;
211 0); 223 hint.ai_addr = NULL;
212 port_buckets = system->reserved_tcp_ports; 224 hint.ai_canonname = NULL;
213 } 225 hint.ai_next = NULL;
214 else 226 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
215 { 227 port_buckets = (GNUNET_YES == is_tcp) ?
216 socket = GNUNET_NETWORK_socket_create (AF_UNSPEC, 228 system->reserved_tcp_ports : system->reserved_udp_ports;
217 SOCK_DGRAM, 229 for (index = LOW_PORT_BUCKET + 1; index < HIGH_PORT_BUCKET; index++)
218 0);
219 port_buckets = system->reserved_udp_ports;
220 }
221 for (index = (LOW_PORT / 32) + 1; index < (HIGH_PORT / 32); index++)
222 { 230 {
223 xor_image = (UINT32_MAX ^ port_buckets[index]); 231 xor_image = (UINT32_MAX ^ port_buckets[index]);
224 if (0 == xor_image) /* Ports in the bucket are full */ 232 if (0 == xor_image) /* Ports in the bucket are full */
225 continue; 233 continue;
226
227 pos = 0; 234 pos = 0;
228 while (pos < 32) 235 while (pos < 32)
229 { 236 {
230 if (0 == ((xor_image >> pos) & 1U)) 237 if (0 == ((xor_image >> pos) & 1U))
231 continue; 238 {
239 pos++;
240 continue;
241 }
232 open_port = (index * 32) + pos; 242 open_port = (index * 32) + pos;
233 GNUNET_asprintf (&open_port_str, "%u", open_port); 243 GNUNET_asprintf (&open_port_str, "%u", open_port);
234 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
235 hint.ai_socktype = (GNUNET_YES == is_tcp)? SOCK_STREAM : SOCK_DGRAM;
236 hint.ai_protocol = 0;
237 hint.ai_addrlen = 0;
238 hint.ai_addr = NULL;
239 hint.ai_canonname = NULL;
240 hint.ai_next = NULL;
241 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
242 ret = NULL; 244 ret = NULL;
243 GNUNET_assert (0 != getaddrinfo (NULL, open_port_str, &hint, &ret)); 245 GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret));
244 GNUNET_free (open_port_str); 246 GNUNET_free (open_port_str);
245 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (socket, ret->ai_addr, ret->ai_addrlen)) 247 socket = GNUNET_NETWORK_socket_create (AF_UNSPEC,
246 { 248 (GNUNET_YES == is_tcp) ?
247 freeaddrinfo (ret); 249 SOCK_STREAM : SOCK_DGRAM,
248 return open_port; 250 0);
249 } 251 GNUNET_assert (NULL != socket);
252 bind_status = GNUNET_NETWORK_socket_bind (socket,
253 ret->ai_addr,
254 ret->ai_addrlen);
250 freeaddrinfo (ret); 255 freeaddrinfo (ret);
251 /* This port is in use by some other application */ 256 GNUNET_NETWORK_socket_close (socket);
252 port_buckets[index] |= (1U << pos); 257 socket = NULL;
258 port_buckets[index] |= (1U << pos); /* Set the port bit */
259 if (GNUNET_OK == bind_status)
260 return open_port;
253 pos++; 261 pos++;
254 } 262 }
255 } 263 }