Skip to content

Commit

Permalink
Adapt elashell for the new configuration module.
Browse files Browse the repository at this point in the history
  • Loading branch information
kunshanyu authored and stiartsly committed May 28, 2019
1 parent d1bde80 commit 8adb09a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 20 deletions.
20 changes: 0 additions & 20 deletions apps/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -2328,26 +2328,6 @@ int main(int argc, char *argv[])
init_screen();
history_load();

opts.hive_bootstraps_size = cfg->hive_bootstraps_size;
if (opts.hive_bootstraps_size > 0) {
opts.hive_bootstraps = (HiveBootstrapNode *)calloc(1, sizeof(HiveBootstrapNode) * opts.hive_bootstraps_size);
if (!opts.hive_bootstraps) {
fprintf(stderr, "out of memory.");
deref(cfg);
free(opts.bootstraps);
return -1;
}

for (i = 0 ; i < cfg->hive_bootstraps_size; i++) {
HiveBootstrapNode *b = &opts.hive_bootstraps[i];
HiveBootstrapNode *node = cfg->hive_bootstraps[i];

b->ipv4 = node->ipv4;
b->ipv6 = node->ipv6;
b->port = node->port;
}
}

memset(&callbacks, 0, sizeof(callbacks));
callbacks.idle = idle_callback;
callbacks.connection_status = connection_callback;
Expand Down
23 changes: 23 additions & 0 deletions config/carrier.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ bootstraps = (
}
)

hive_bootstraps = (
{
ipv4 = "52.83.159.189"
port = 9094
},
{
ipv4 = "52.83.119.110"
port = 9094
},
{
ipv4 = "3.16.202.140"
port = 9094
},
{
ipv4 = "18.217.147.205"
port = 9094
},
{
ipv4 = "18.219.53.133"
port = 9094
}
)

# Default UDP is enabled
udp-enabled = true

Expand Down
79 changes: 79 additions & 0 deletions config/carrier_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ static void bootstraps_destructor(void *p)
}
}

static void hive_bootstrap_destructor(void *p)
{
int i;
size_t *size = (size_t *)p;
HiveBootstrapNode *bootstraps = (struct HiveBootstrapNode *)(size + 1);

for (i = 0; i < *size; i++) {
HiveBootstrapNode *node = bootstraps + i;

if (node->ipv4)
free((void *)node->ipv4);

if (node->ipv6)
free((void *)node->ipv6);

if (node->port)
free((void *)node->port);
}
}

#if defined(_WIN32) || defined(_WIN64)
#define PATH_SEP "\\"
#define HOME_ENV "LOCALAPPDATA"
Expand Down Expand Up @@ -211,6 +231,60 @@ ElaOptions *carrier_config_load(const char *config_file,
node->public_key = NULL;
}

bootstraps_setting = config_lookup(&cfg, "hive_bootstraps");
if (!bootstraps_setting) {
fprintf(stderr, "Missing hive bootstraps section.\n");
carrier_config_free(options);
config_destroy(&cfg);
return NULL;
}

entries = config_setting_length(bootstraps_setting);
if (entries <= 0) {
fprintf(stderr, "Empty hive bootstraps option.\n");
carrier_config_free(options);
config_destroy(&cfg);
return NULL;
}

mem = (size_t *)rc_zalloc(sizeof(size_t) +
sizeof(HiveBootstrapNode) * entries, hive_bootstrap_destructor);
if (!mem) {
fprintf(stderr, "Load configuration failed, out of memory.\n");
carrier_config_free(options);
config_destroy(&cfg);
return NULL;
}

*mem = entries;
options->hive_bootstraps_size = entries;
options->hive_bootstraps = (HiveBootstrapNode *)(++mem);

for (i = 0; i < entries; i++) {
HiveBootstrapNode *node = options->hive_bootstraps + i;

bootstrap_setting = config_setting_get_elem(bootstraps_setting, i);

rc = config_setting_lookup_string(bootstrap_setting, "ipv4", &stropt);
if (rc && *stropt)
node->ipv4 = (const char *)strdup(stropt);
else
node->ipv4 = NULL;

rc = config_setting_lookup_string(bootstrap_setting, "ipv6", &stropt);
if (rc && *stropt)
node->ipv6 = (const char *)strdup(stropt);
else
node->ipv6 = NULL;

rc = config_setting_lookup_int(bootstrap_setting, "port", &intopt);
if (rc && intopt) {
sprintf(number, "%d", intopt);
node->port = (const char *)strdup(number);
} else
node->port = NULL;
}

options->udp_enabled = true;
rc = config_lookup_bool(&cfg, "udp-enabled", &intopt);
if (rc)
Expand Down Expand Up @@ -341,6 +415,11 @@ void carrier_config_free(ElaOptions *options)
deref(--p);
}

if (options->hive_bootstraps) {
size_t *p = (size_t *)options->hive_bootstraps;
deref(--p);
}

#ifndef NDEBUG
memset(options, 0, sizeof(ElaOptions));
#endif
Expand Down

0 comments on commit 8adb09a

Please sign in to comment.