aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-08-29 12:35:14 +0000
committerChristian Grothoff <christian@grothoff.org>2012-08-29 12:35:14 +0000
commit11fd96a2b8d53a0f87b2e0b75168e49d1ecd6f59 (patch)
treefc5dae8a241594d54c21186145fcb30337192062 /src/testing
parentbc7d3adbc732f732ddbac13f295adc0f87449596 (diff)
downloadgnunet-11fd96a2b8d53a0f87b2e0b75168e49d1ecd6f59.tar.gz
gnunet-11fd96a2b8d53a0f87b2e0b75168e49d1ecd6f59.zip
extend API to enalbe exclusive port ranges to be specified for testing-system objects
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/testing.c51
1 files changed, 46 insertions, 5 deletions
diff --git a/src/testing/testing.c b/src/testing/testing.c
index bba46231d..342db52a9 100644
--- a/src/testing/testing.c
+++ b/src/testing/testing.c
@@ -124,6 +124,16 @@ struct GNUNET_TESTING_System
124 * The number of hostkeys 124 * The number of hostkeys
125 */ 125 */
126 uint32_t total_hostkeys; 126 uint32_t total_hostkeys;
127
128 /**
129 * Lowest port we are allowed to use.
130 */
131 uint16_t lowport;
132
133 /**
134 * Highest port we are allowed to use.
135 */
136 uint16_t highport;
127}; 137};
128 138
129 139
@@ -249,21 +259,26 @@ hostkeys_unload (struct GNUNET_TESTING_System *system)
249 * @param testdir only the directory name without any path. This is used for 259 * @param testdir only the directory name without any path. This is used for
250 * all service homes; the directory will be created in a temporary 260 * all service homes; the directory will be created in a temporary
251 * location depending on the underlying OS 261 * location depending on the underlying OS
252 *
253 * @param controller hostname of the controlling host, 262 * @param controller hostname of the controlling host,
254 * service configurations are modified to allow 263 * service configurations are modified to allow
255 * control connections from this host; can be NULL 264 * control connections from this host; can be NULL
265 * @param lowport lowest port number this system is allowed to allocate (inclusive)
266 * @param highport highest port number this system is allowed to allocate (exclusive)
256 * @return handle to this system, NULL on error 267 * @return handle to this system, NULL on error
257 */ 268 */
258struct GNUNET_TESTING_System * 269struct GNUNET_TESTING_System *
259GNUNET_TESTING_system_create (const char *testdir, 270GNUNET_TESTING_system_create_with_portrange (const char *testdir,
260 const char *controller) 271 const char *controller,
272 uint16_t lowport,
273 uint16_t highport)
261{ 274{
262 struct GNUNET_TESTING_System *system; 275 struct GNUNET_TESTING_System *system;
263 276
264 GNUNET_assert (NULL != testdir); 277 GNUNET_assert (NULL != testdir);
265 system = GNUNET_malloc (sizeof (struct GNUNET_TESTING_System)); 278 system = GNUNET_malloc (sizeof (struct GNUNET_TESTING_System));
266 system->tmppath = GNUNET_DISK_mkdtemp (testdir); 279 system->tmppath = GNUNET_DISK_mkdtemp (testdir);
280 system->lowport = lowport;
281 system->highport = highport;
267 if (NULL == system->tmppath) 282 if (NULL == system->tmppath)
268 { 283 {
269 GNUNET_free (system); 284 GNUNET_free (system);
@@ -281,6 +296,30 @@ GNUNET_TESTING_system_create (const char *testdir,
281 296
282 297
283/** 298/**
299 * Create a system handle. There must only be one system
300 * handle per operating system.
301 *
302 * @param testdir only the directory name without any path. This is used for
303 * all service homes; the directory will be created in a temporary
304 * location depending on the underlying OS
305 *
306 * @param controller hostname of the controlling host,
307 * service configurations are modified to allow
308 * control connections from this host; can be NULL
309 * @return handle to this system, NULL on error
310 */
311struct GNUNET_TESTING_System *
312GNUNET_TESTING_system_create (const char *testdir,
313 const char *controller)
314{
315 return GNUNET_TESTING_system_create_with_portrange (testdir,
316 controller,
317 LOW_PORT,
318 HIGH_PORT);
319}
320
321
322/**
284 * Free system resources. 323 * Free system resources.
285 * 324 *
286 * @param system system to be freed 325 * @param system system to be freed
@@ -342,12 +381,12 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
342 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */ 381 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
343 port_buckets = (GNUNET_YES == is_tcp) ? 382 port_buckets = (GNUNET_YES == is_tcp) ?
344 system->reserved_tcp_ports : system->reserved_udp_ports; 383 system->reserved_tcp_ports : system->reserved_udp_ports;
345 for (index = (LOW_PORT / 32) + 1; index < (HIGH_PORT / 32); index++) 384 for (index = (system->lowport / 32) + 1; index < (system->highport / 32); index++)
346 { 385 {
347 xor_image = (UINT32_MAX ^ port_buckets[index]); 386 xor_image = (UINT32_MAX ^ port_buckets[index]);
348 if (0 == xor_image) /* Ports in the bucket are full */ 387 if (0 == xor_image) /* Ports in the bucket are full */
349 continue; 388 continue;
350 pos = 0; 389 pos = system->lowport % 32;
351 while (pos < 32) 390 while (pos < 32)
352 { 391 {
353 if (0 == ((xor_image >> pos) & 1U)) 392 if (0 == ((xor_image >> pos) & 1U))
@@ -356,6 +395,8 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
356 continue; 395 continue;
357 } 396 }
358 open_port = (index * 32) + pos; 397 open_port = (index * 32) + pos;
398 if (open_port >= system->highport)
399 return 0;
359 GNUNET_asprintf (&open_port_str, "%u", (unsigned int) open_port); 400 GNUNET_asprintf (&open_port_str, "%u", (unsigned int) open_port);
360 ret = NULL; 401 ret = NULL;
361 GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret)); 402 GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret));