From 5dd076cfbeda82f35396908afd9fa688ca291dfb Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 21 Nov 2014 15:51:16 +1100 Subject: [PATCH] Allow serverurl to take an array of entries for future binding to multiple interfaces/ports --- src/ckpool.c | 57 ++++++++++++++++++++++++++++++++++++++++++------ src/ckpool.h | 3 ++- src/connector.c | 6 ++--- src/stratifier.c | 20 +++++++++-------- 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 0a87c430..252c9a7f 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -890,9 +890,8 @@ static void sighandler(int sig) exit(0); } -bool json_get_string(char **store, json_t *val, const char *res) +static bool _json_get_string(char **store, json_t *entry, const char *res) { - json_t *entry = json_object_get(val, res); bool ret = false; const char *buf; @@ -913,6 +912,11 @@ out: return ret; } +bool json_get_string(char **store, json_t *val, const char *res) +{ + return _json_get_string(store, json_object_get(val, res), res); +} + static void json_get_int64(int64_t *store, json_t *val, const char *res) { json_t *entry = json_object_get(val, res); @@ -1005,10 +1009,41 @@ static void parse_proxies(ckpool_t *ckp, json_t *arr_val, int arr_size) } } +static bool parse_serverurls(ckpool_t *ckp, json_t *arr_val) +{ + bool ret = false; + int arr_size, i; + + if (!arr_val) + goto out; + if (!json_is_array(arr_val)) { + LOGWARNING("Unable to parse serverurl entries as an array"); + goto out; + } + arr_size = json_array_size(arr_val); + if (!arr_size) { + LOGWARNING("Serverurl array empty"); + goto out; + } + ckp->serverurls = arr_size; + ckp->serverurl = ckalloc(sizeof(char *) * arr_size); + for (i = 0; i < arr_size; i++) { + json_t *val = json_array_get(arr_val, i); + + if (!_json_get_string(&ckp->serverurl[i], val, "serverurl")) + LOGWARNING("Invalid serverurl entry number %d", i); + } + ret = true; +out: + return ret; +} + static void parse_config(ckpool_t *ckp) { json_t *json_conf, *arr_val; json_error_t err_val; + int arr_size; + char *url; json_conf = json_load_file(ckp->config, JSON_DISABLE_EOF_CHECK, &err_val); if (!json_conf) { @@ -1018,8 +1053,7 @@ static void parse_config(ckpool_t *ckp) } arr_val = json_object_get(json_conf, "btcd"); if (arr_val && json_is_array(arr_val)) { - int arr_size = json_array_size(arr_val); - + arr_size = json_array_size(arr_val); if (arr_size) parse_btcds(ckp, arr_val, arr_size); } @@ -1033,7 +1067,15 @@ static void parse_config(ckpool_t *ckp) json_get_int(&ckp->nonce1length, json_conf, "nonce1length"); json_get_int(&ckp->nonce2length, json_conf, "nonce2length"); json_get_int(&ckp->update_interval, json_conf, "update_interval"); - json_get_string(&ckp->serverurl, json_conf, "serverurl"); + /* Look for an array first and then a single entry */ + arr_val = json_object_get(json_conf, "serverurl"); + if (!parse_serverurls(ckp, arr_val)) { + if (json_get_string(&url, json_conf, "serverurl")) { + ckp->serverurl = ckalloc(sizeof(char *)); + ckp->serverurl[0] = url; + ckp->serverurls = 1; + } + } json_get_int64(&ckp->mindiff, json_conf, "mindiff"); json_get_int64(&ckp->startdiff, json_conf, "startdiff"); json_get_int64(&ckp->maxdiff, json_conf, "maxdiff"); @@ -1041,8 +1083,7 @@ static void parse_config(ckpool_t *ckp) json_get_int(&ckp->maxclients, json_conf, "maxclients"); arr_val = json_object_get(json_conf, "proxy"); if (arr_val && json_is_array(arr_val)) { - int arr_size = json_array_size(arr_val); - + arr_size = json_array_size(arr_val); if (arr_size) parse_proxies(ckp, arr_val, arr_size); } @@ -1342,6 +1383,8 @@ int main(int argc, char **argv) ckp.startdiff = 42; if (!ckp.logdir) ckp.logdir = strdup("logs"); + if (!ckp.serverurls) + ckp.serverurl = ckzalloc(sizeof(char *)); if (ckp.proxy && !ckp.proxies) quit(0, "No proxy entries found in config file %s", ckp.config); diff --git a/src/ckpool.h b/src/ckpool.h index f7bcc85a..b495becf 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -164,7 +164,8 @@ struct ckpool_instance { /* Stratum options */ server_instance_t **servers; - char *serverurl; // URL to bind our server/proxy to + char **serverurl; // Array of URLs to bind our server/proxy to + int serverurls; // Number of server bindings int update_interval; // Seconds between stratum updates int chosen_server; // Chosen server for next connection diff --git a/src/connector.c b/src/connector.c index 64b77afd..eba91ca0 100644 --- a/src/connector.c +++ b/src/connector.c @@ -742,9 +742,9 @@ int connector(proc_instance_t *pi) if (ckp->oldconnfd > 0) { sockd = ckp->oldconnfd; - } else if (ckp->serverurl) { - if (!extract_sockaddr(ckp->serverurl, &url, &port)) { - LOGWARNING("Failed to extract server address from %s", ckp->serverurl); + } else if (ckp->serverurls && ckp->serverurl[0]) { + if (!extract_sockaddr(ckp->serverurl[0], &url, &port)) { + LOGWARNING("Failed to extract server address from %s", ckp->serverurl[0]); ret = 1; goto out; } diff --git a/src/stratifier.c b/src/stratifier.c index 9efecb29..94e9c546 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -603,7 +603,7 @@ static void send_workinfo(ckpool_t *ckp, workbase_t *wb) "createdate", cdfield, "createby", "code", "createcode", __func__, - "createinet", ckp->serverurl); + "createinet", ckp->serverurl[0]); ckdbq_add(ckp, ID_WORKINFO, val); } @@ -622,7 +622,7 @@ static void send_ageworkinfo(ckpool_t *ckp, int64_t id) "createdate", cdfield, "createby", "code", "createcode", __func__, - "createinet", ckp->serverurl); + "createinet", ckp->serverurl[0]); ckdbq_add(ckp, ID_AGEWORKINFO, val); } @@ -2304,7 +2304,7 @@ test_blocksolve(stratum_instance_t *client, workbase_t *wb, const uchar *data, c "createdate", cdfield, "createby", "code", "createcode", __func__, - "createinet", ckp->serverurl); + "createinet", ckp->serverurl[0]); val_copy = json_deep_copy(val); block_ckmsg = ckalloc(sizeof(ckmsg_t)); block_ckmsg->data = val_copy; @@ -2604,7 +2604,7 @@ out_unlock: json_set_string(val, "createdate", cdfield); json_set_string(val, "createby", "code"); json_set_string(val, "createcode", __func__); - json_set_string(val, "createinet", ckp->serverurl); + json_set_string(val, "createinet", ckp->serverurl[0]); json_set_string(val, "workername", client->workername); json_set_string(val, "username", user_instance->username); @@ -2656,7 +2656,7 @@ out: "createdate", cdfield, "createby", "code", "createcode", __func__, - "createinet", ckp->serverurl); + "createinet", ckp->serverurl[0]); ckdbq_add(ckp, ID_SHAREERR, val); LOGINFO("Invalid share from client %ld: %s", client->id, client->workername); } @@ -3595,7 +3595,7 @@ static void *statsupdate(void *arg) "createdate", cdfield, "createby", "code", "createcode", __func__, - "createinet", ckp->serverurl); + "createinet", ckp->serverurl[0]); ckdbq_add(ckp, ID_POOLSTATS, val); /* Update stats 3 times per minute for smooth values, displaying @@ -3660,7 +3660,7 @@ static void *ckdb_heartbeat(void *arg) "createdate", cdfield, "createby", "code", "createcode", __func__, - "createinet", ckp->serverurl); + "createinet", ckp->serverurl[0]); ckdbq_add(ckp, ID_HEARTBEAT, val); } return NULL; @@ -3713,8 +3713,10 @@ int stratifier(proc_instance_t *pi) dealloc(buf); - if (!ckp->serverurl) - ckp->serverurl = "127.0.0.1"; + if (!ckp->serverurls) { + ckp->serverurl[0] = "127.0.0.1"; + ckp->serverurls = 1; + } cklock_init(&sdata->instance_lock); mutex_init(&sdata->ckdb_lock);