aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_hosts.c
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/testbed_api_hosts.c
parentc833091d99957968f26307c2044f1cb03fae2dce (diff)
downloadgnunet-b30797c4f36c80e8bdc22fbb30446329b45cf8c1.tar.gz
gnunet-b30797c4f36c80e8bdc22fbb30446329b45cf8c1.zip
- allow parsing hosts with optional username and port number
Diffstat (limited to 'src/testbed/testbed_api_hosts.c')
-rw-r--r--src/testbed/testbed_api_hosts.c80
1 files changed, 56 insertions, 24 deletions
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;