aboutsummaryrefslogtreecommitdiff
path: root/src/testbed
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2013-08-20 13:22:35 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2013-08-20 13:22:35 +0000
commitb30797c4f36c80e8bdc22fbb30446329b45cf8c1 (patch)
treec2aa27b1e153aed529573a58c742fb7d52c6a8f7 /src/testbed
parentc833091d99957968f26307c2044f1cb03fae2dce (diff)
downloadgnunet-b30797c4f36c80e8bdc22fbb30446329b45cf8c1.tar.gz
gnunet-b30797c4f36c80e8bdc22fbb30446329b45cf8c1.zip
- allow parsing hosts with optional username and port number
Diffstat (limited to 'src/testbed')
-rw-r--r--src/testbed/sample_hosts.txt18
-rw-r--r--src/testbed/test_testbed_api_hosts.c49
-rw-r--r--src/testbed/testbed_api_hosts.c80
3 files changed, 108 insertions, 39 deletions
diff --git a/src/testbed/sample_hosts.txt b/src/testbed/sample_hosts.txt
index d0ce5cb45..b38578cad 100644
--- a/src/testbed/sample_hosts.txt
+++ b/src/testbed/sample_hosts.txt
@@ -1,17 +1,7 @@
1totakura@192.168.0.1:22 1totakura@192.168.0.1:22
2totakura@192.168.0.2:22 2totakura@192.168.0.2:22
3totakura@192.168.0.3:22
4totakura@192.168.0.4:22
5totakura@192.168.0.5:22
6totakura@192.168.0.6:22
7totakura@192.168.0.7:22
8totakura@192.168.0.8:22
9totakura@192.168.0.9:22
10totakura@192.168.0.10:22
11totakura@192.168.0.11:22
12totakura@192.168.0.12:22
13totakura@192.168.0.13:22
14totakura@192.168.0.14:22
15totakura@192.168.0.15:22
16totakura@opt01:22 3totakura@opt01:22
17totakura@i19-n015:22 4totakura@i19-n015:2022
5totakura@asgard
6rivendal
7rohan:561
diff --git a/src/testbed/test_testbed_api_hosts.c b/src/testbed/test_testbed_api_hosts.c
index 17ccc7050..593879188 100644
--- a/src/testbed/test_testbed_api_hosts.c
+++ b/src/testbed/test_testbed_api_hosts.c
@@ -99,6 +99,8 @@ static void
99run (void *cls, char *const *args, const char *cfgfile, 99run (void *cls, char *const *args, const char *cfgfile,
100 const struct GNUNET_CONFIGURATION_Handle *config) 100 const struct GNUNET_CONFIGURATION_Handle *config)
101{ 101{
102 unsigned int cnt;
103
102 cfg = GNUNET_CONFIGURATION_dup (config); 104 cfg = GNUNET_CONFIGURATION_dup (config);
103 host = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0); 105 host = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
104 GNUNET_assert (NULL != host); 106 GNUNET_assert (NULL != host);
@@ -110,8 +112,53 @@ run (void *cls, char *const *args, const char *cfgfile,
110 GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0)); 112 GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0));
111 hosts = NULL; 113 hosts = NULL;
112 num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", cfg, &hosts); 114 num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", cfg, &hosts);
113 GNUNET_assert (17 == num_hosts); 115 GNUNET_assert (7 == num_hosts);
114 GNUNET_assert (NULL != hosts); 116 GNUNET_assert (NULL != hosts);
117 for (cnt = 0; cnt < num_hosts; cnt++)
118 {
119 if (cnt < 3)
120 {
121 GNUNET_assert (0 == strcmp ("totakura",
122 GNUNET_TESTBED_host_get_username_
123 (hosts[cnt])));
124 GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
125 GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
126 }
127 if (3 == cnt)
128 {
129 GNUNET_assert (0 == strcmp ("totakura",
130 GNUNET_TESTBED_host_get_username_
131 (hosts[cnt])));
132 GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
133 GNUNET_assert (2022 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
134 }
135 if (4 == cnt)
136 {
137 GNUNET_assert (0 == strcmp ("totakura",
138 GNUNET_TESTBED_host_get_username_
139 (hosts[cnt])));
140 GNUNET_assert (0 == strcmp ("asgard",
141 GNUNET_TESTBED_host_get_hostname
142 (hosts[cnt])));
143 GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
144 }
145 if (5 == cnt)
146 {
147 GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
148 GNUNET_assert (0 == strcmp ("rivendal",
149 GNUNET_TESTBED_host_get_hostname
150 (hosts[cnt])));
151 GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
152 }
153 if (6 == cnt)
154 {
155 GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
156 GNUNET_assert (0 == strcmp ("rohan",
157 GNUNET_TESTBED_host_get_hostname
158 (hosts[cnt])));
159 GNUNET_assert (561 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
160 }
161 }
115 status = GNUNET_YES; 162 status = GNUNET_YES;
116 shutdown_id = 163 shutdown_id =
117 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (0), &do_shutdown, NULL); 164 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (0), &do_shutdown, NULL);
diff --git a/src/testbed/testbed_api_hosts.c b/src/testbed/testbed_api_hosts.c
index b7c824bc2..c0b64cf8f 100644
--- a/src/testbed/testbed_api_hosts.c
+++ b/src/testbed/testbed_api_hosts.c
@@ -37,6 +37,7 @@
37#include "testbed_api_operations.h" 37#include "testbed_api_operations.h"
38 38
39#include <zlib.h> 39#include <zlib.h>
40#include <regex.h>
40 41
41/** 42/**
42 * Generic logging shorthand 43 * Generic logging shorthand
@@ -397,12 +398,13 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
397 //struct GNUNET_TESTBED_Host **host_array; 398 //struct GNUNET_TESTBED_Host **host_array;
398 struct GNUNET_TESTBED_Host *starting_host; 399 struct GNUNET_TESTBED_Host *starting_host;
399 char *data; 400 char *data;
400 char *buf; 401 char *buf;
401 char username[256]; 402 char *username;
402 char hostname[256]; 403 char *hostname;
404 regex_t rex;
405 regmatch_t pmatch[6];
403 uint64_t fs; 406 uint64_t fs;
404 short int port; 407 short int port;
405 int ret;
406 unsigned int offset; 408 unsigned int offset;
407 unsigned int count; 409 unsigned int count;
408 410
@@ -415,12 +417,12 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
415 } 417 }
416 if (GNUNET_OK != 418 if (GNUNET_OK !=
417 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES)) 419 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
418 fs = 0; 420 fs = 0;
419 if (0 == fs) 421 if (0 == fs)
420 { 422 {
421 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename); 423 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
422 return 0; 424 return 0;
423 } 425 }
424 data = GNUNET_malloc (fs); 426 data = GNUNET_malloc (fs);
425 if (fs != GNUNET_DISK_fn_read (filename, data, fs)) 427 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
426 { 428 {
@@ -433,37 +435,67 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
433 offset = 0; 435 offset = 0;
434 starting_host = NULL; 436 starting_host = NULL;
435 count = 0; 437 count = 0;
438 /* refer RFC 952 and RFC 1123 for valid hostnames */
439 GNUNET_assert (0 == regcomp (&rex,
440 "^(([[:alnum:]]+)@)?" /* username */
441 "([[:alnum:]]+[-[:alnum:]_\\.]+)" /* hostname */
442 "(:([[:digit:]]{1,5}))?", /* port */
443 REG_EXTENDED | REG_ICASE));
436 while (offset < (fs - 1)) 444 while (offset < (fs - 1))
437 { 445 {
438 offset++; 446 offset++;
439 if (((data[offset] == '\n')) && (buf != &data[offset])) 447 if (((data[offset] == '\n')) && (buf != &data[offset]))
440 { 448 {
449 unsigned int size;
450
441 data[offset] = '\0'; 451 data[offset] = '\0';
442 ret = 452 username = NULL;
443 SSCANF (buf, "%255[a-zA-Z0-9_]@%255[.a-zA-Z0-9-]:%5hd", username, 453 hostname = NULL;
444 hostname, &port); 454 port = 0;
445 if (3 == ret) 455 if ((REG_NOMATCH == regexec (&rex, buf, 6, pmatch, 0))
456 || (-1 == pmatch[3].rm_so))
446 { 457 {
447 LOG (GNUNET_ERROR_TYPE_DEBUG,
448 "Successfully read host %s, port %d and user %s from file\n",
449 hostname, port, username);
450 /* We store hosts in a static list; hence we only require the starting
451 * host pointer in that list to access the newly created list of hosts */
452 if (NULL == starting_host)
453 starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
454 port);
455 else
456 (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
457 count++;
458 }
459 else
460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
461 "Error reading line `%s' in hostfile\n", buf); 459 "Error reading line `%s' in hostfile\n", buf);
460 buf = &data[offset + 1];
461 continue;
462 }
463 if (-1 != pmatch[2].rm_so)
464 {
465 size = pmatch[2].rm_eo - pmatch[2].rm_so;
466 username = GNUNET_malloc (size + 1);
467 username[size] = '\0';
468 GNUNET_assert (NULL != strncpy (username, buf + pmatch[2].rm_so, size));
469 }
470 if (-1 != pmatch[5].rm_so)
471 {
472 (void) SSCANF (buf + pmatch[5].rm_so, "%5hd", &port);
473 }
474 size = pmatch[3].rm_eo - pmatch[3].rm_so;
475 hostname = GNUNET_malloc (size + 1);
476 hostname[size] = '\0';
477 GNUNET_assert (NULL != strncpy (hostname, buf + pmatch[3].rm_so, size));
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "Successfully read host %s, port %d and user %s from file\n",
480 (NULL == hostname) ? "NULL" : hostname,
481 port,
482 (NULL == username) ? "NULL" : username);
483 /* We store hosts in a static list; hence we only require the starting
484 * host pointer in that list to access the newly created list of hosts */
485 if (NULL == starting_host)
486 starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
487 port);
488 else
489 (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
490 count++;
491 GNUNET_free_non_null (username);
492 GNUNET_free (hostname);
462 buf = &data[offset + 1]; 493 buf = &data[offset + 1];
463 } 494 }
464 else if ((data[offset] == '\n') || (data[offset] == '\0')) 495 else if ((data[offset] == '\n') || (data[offset] == '\0'))
465 buf = &data[offset + 1]; 496 buf = &data[offset + 1];
466 } 497 }
498 regfree (&rex);
467 GNUNET_free (data); 499 GNUNET_free (data);
468 if (NULL == starting_host) 500 if (NULL == starting_host)
469 return 0; 501 return 0;