API implement addpool command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
diff --git a/README b/README
index c3a28a9..e4e744b 100644
--- a/README
+++ b/README
@@ -660,6 +660,13 @@ The list of requests - a (*) means it requires privileged access - and replies a
stating the results of enabling pool N
The Msg includes the pool URL
+ addpool|URL,USR,PASS (*)
+ none There is no reply section just the STATUS section
+ stating the results of attempting to add pool N
+ The Msg includes the pool URL
+ Use '\\' to get a '\' and '\,' to include a comma
+ inside URL, USR or PASS
+
disablepool|N (*)
none There is no reply section just the STATUS section
stating the results of disabling pool N
diff --git a/api.c b/api.c
index b3aea26..9528230 100644
--- a/api.c
+++ b/api.c
@@ -264,6 +264,10 @@ static const char *JSON_PARAMETER = "parameter";
#define MSG_ALRENAP 49
#define MSG_ALRDISP 50
#define MSG_DISLASTP 51
+#define MSG_MISPDP 52
+#define MSG_INVPDP 53
+#define MSG_TOOMANYP 54
+#define MSG_ADDPOOL 55
enum code_severity {
SEVERITY_ERR,
@@ -356,6 +360,10 @@ struct CODES {
{ SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
{ SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
{ SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
+ { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
+ { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
+ { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
+ { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
{ SEVERITY_FAIL, 0, 0, NULL }
};
@@ -986,6 +994,81 @@ static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
}
+static void copyadvanceafter(char ch, char **param, char **buf)
+{
+#define src_p (*param)
+#define dst_b (*buf)
+
+ while (*src_p && *src_p != ch) {
+ if (*src_p == '\\' && *(src_p+1) != '\0')
+ src_p++;
+
+ *(dst_b++) = *(src_p++);
+ }
+ if (*src_p)
+ src_p++;
+
+ *(dst_b++) = '\0';
+}
+
+static bool pooldetails(char *param, char **url, char **user, char **pass)
+{
+ char *ptr, *buf;
+
+ ptr = buf = malloc(strlen(param)+1);
+ if (unlikely(!buf))
+ quit(1, "Failed to malloc pooldetails buf");
+
+ *url = buf;
+
+ // copy url
+ copyadvanceafter(',', ¶m, &buf);
+
+ if (!(*param)) // missing user
+ goto exitsama;
+
+ *user = buf;
+
+ // copy user
+ copyadvanceafter(',', ¶m, &buf);
+
+ if (!*param) // missing pass
+ goto exitsama;
+
+ *pass = buf;
+
+ // copy pass
+ copyadvanceafter(',', ¶m, &buf);
+
+ return true;
+
+exitsama:
+ free(ptr);
+ return false;
+}
+
+static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
+{
+ char *url, *user, *pass;
+
+ if (param == NULL || *param == '\0') {
+ strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
+ return;
+ }
+
+ if (!pooldetails(param, &url, &user, &pass)) {
+ strcpy(io_buffer, message(MSG_INVPDP, 0, param, isjson));
+ return;
+ }
+
+ if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
+ strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
+ return;
+ }
+
+ strcpy(io_buffer, message(MSG_ADDPOOL, 0, url, isjson));
+}
+
static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
{
struct pool *pool;
@@ -1271,7 +1354,7 @@ struct CMDS {
{ "gpucount", gpucount, false },
{ "cpucount", cpucount, false },
{ "switchpool", switchpool, true },
-// { "addpool", addpool, true }, Not yet ...
+ { "addpool", addpool, true },
{ "enablepool", enablepool, true },
{ "disablepool", disablepool, true },
{ "gpuintensity", gpuintensity, true },