From a13df377d4f1c38ac65e6dc837b2973951c8101c Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 23 Mar 2015 14:04:22 +1100 Subject: [PATCH] Add an API command to get a list of clients by username --- src/stratifier.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/stratifier.c b/src/stratifier.c index 817bfef2..c42538a9 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -2469,6 +2469,43 @@ out: _Close(sockd); } +static void getclients(sdata_t *sdata, const char *buf, int *sockd) +{ + json_t *val = NULL, *client_arr; + stratum_instance_t *client; + char *username = NULL; + user_instance_t *user; + json_error_t err_val; + + val = json_loads(buf, 0, &err_val); + if (unlikely(!val)) { + val = json_encode_errormsg(&err_val); + goto out; + } + if (!json_get_string(&username, val, "user")) { + val = json_errormsg("Failed to find user key"); + goto out; + } + if (!strlen(username)) { + val = json_errormsg("Zero length user key"); + goto out; + } + user = get_user(sdata, username); + client_arr = json_array(); + + ck_rlock(&sdata->instance_lock); + DL_FOREACH(user->clients, client) { + json_array_append_new(client_arr, json_integer(client->id)); + } + ck_runlock(&sdata->instance_lock); + + JSON_CPACK(val, "{so}", "clients", client_arr); +out: + free(username); + send_api_response(val, *sockd); + _Close(sockd); +} + static worker_instance_t *get_worker(sdata_t *sdata, user_instance_t *user, const char *workername); static void getworker(sdata_t *sdata, const char *buf, int *sockd) @@ -2692,6 +2729,10 @@ retry: getworker(sdata, buf + 10, &sockd); goto retry; } + if (cmdmatch(buf, "userclients")) { + getclients(sdata, buf + 12, &sockd); + goto retry; + } if (cmdmatch(buf, "getproxy")) { getproxy(sdata, buf + 9, &sockd); goto retry;