summaryrefslogtreecommitdiffstats
path: root/admin
diff options
context:
space:
mode:
authorOpenGnSys Support Team <soporte-og@soleta.eu>2019-05-21 16:56:49 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2019-05-27 13:02:52 +0200
commit73c4bdffade0bfab9dc8125c1967346712d686d8 (patch)
tree5379dc2f459e45ce259d8a92dd91e81c372e6aaa /admin
parent62c056026f346e9c8187d39f7a229a627178fb6c (diff)
#915 add wol command to REST API in ogAdmServer
Send Wake-On-Lan to clients (through POST method): Request: curl -X POST http://127.0.0.1:8888/wol { "type" : "unicast", "clients" : [ { "addr" : "192.168.2.1", "mac" : "00AABBCCDD01" } ] } Reply: 200 OK
Diffstat (limited to 'admin')
-rw-r--r--admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp108
-rwxr-xr-xadmin/Sources/Services/ogAdmServer/tests/run-tests.sh1
-rw-r--r--admin/Sources/Services/ogAdmServer/tests/wol.json1
3 files changed, 110 insertions, 0 deletions
diff --git a/admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp b/admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp
index 41400d64..a83a47d7 100644
--- a/admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp
+++ b/admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp
@@ -3627,7 +3627,9 @@ static int og_client_state_process_payload(struct og_client *cli)
struct og_msg_params {
const char *ips_array[64];
+ const char *mac_array[64];
unsigned int ips_array_len;
+ const char *wol_type;
};
static int og_json_parse_clients(json_t *element, struct og_msg_params *params)
@@ -3757,6 +3759,103 @@ static int og_cmd_get_clients(json_t *element, struct og_msg_params *params,
return 0;
}
+static int og_json_parse_target(json_t *element, struct og_msg_params *params)
+{
+ const char *key;
+ json_t *value;
+
+ if (json_typeof(element) != JSON_OBJECT) {
+ return -1;
+ }
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "addr")) {
+ if (json_typeof(value) != JSON_STRING)
+ return -1;
+
+ params->ips_array[params->ips_array_len] =
+ json_string_value(value);
+ } else if (!strcmp(key, "mac")) {
+ if (json_typeof(value) != JSON_STRING)
+ return -1;
+
+ params->mac_array[params->ips_array_len] =
+ json_string_value(value);
+ }
+ }
+
+ return 0;
+}
+
+static int og_json_parse_targets(json_t *element, struct og_msg_params *params)
+{
+ unsigned int i;
+ json_t *k;
+ int err;
+
+ if (json_typeof(element) != JSON_ARRAY)
+ return -1;
+
+ for (i = 0; i < json_array_size(element); i++) {
+ k = json_array_get(element, i);
+
+ if (json_typeof(k) != JSON_OBJECT)
+ return -1;
+
+ err = og_json_parse_target(k, params);
+ if (err < 0)
+ return err;
+
+ params->ips_array_len++;
+ }
+ return 0;
+}
+
+static int og_json_parse_type(json_t *element, struct og_msg_params *params)
+{
+ const char *type;
+
+ if (json_typeof(element) != JSON_STRING)
+ return -1;
+
+ params->wol_type = json_string_value(element);
+
+ type = json_string_value(element);
+ if (!strcmp(type, "unicast"))
+ params->wol_type = "2";
+ else if (!strcmp(type, "broadcast"))
+ params->wol_type = "1";
+
+ return 0;
+}
+
+static int og_cmd_wol(json_t *element, struct og_msg_params *params)
+{
+ const char *key;
+ json_t *value;
+ int err = 0;
+
+ if (json_typeof(element) != JSON_OBJECT)
+ return -1;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "clients")) {
+ err = og_json_parse_targets(value, params);
+ } else if (!strcmp(key, "type")) {
+ err = og_json_parse_type(value, params);
+ }
+
+ if (err < 0)
+ break;
+ }
+
+ if (!Levanta((char **)params->ips_array, (char **)params->mac_array,
+ params->ips_array_len, (char *)params->wol_type))
+ return -1;
+
+ return 0;
+}
+
static int og_client_not_found(struct og_client *cli)
{
char buf[] = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
@@ -3835,6 +3934,15 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_get_clients(root, &params, buf_reply);
break;
}
+ } else if (!strncmp(cmd, "wol", strlen("wol"))) {
+ if (method != OG_METHOD_POST)
+ return -1;
+
+ if (!root) {
+ syslog(LOG_ERR, "command wol with no payload\n");
+ return og_client_not_found(cli);
+ }
+ err = og_cmd_wol(root, &params);
} else {
syslog(LOG_ERR, "unknown command %s\n", cmd);
err = og_client_not_found(cli);
diff --git a/admin/Sources/Services/ogAdmServer/tests/run-tests.sh b/admin/Sources/Services/ogAdmServer/tests/run-tests.sh
index a543ef00..40c339d8 100755
--- a/admin/Sources/Services/ogAdmServer/tests/run-tests.sh
+++ b/admin/Sources/Services/ogAdmServer/tests/run-tests.sh
@@ -1,2 +1,3 @@
curl -X POST http://127.0.0.1:8888/clients -d @post_clients.json
curl -X GET http://127.0.0.1:8888/clients
+curl -X POST http://127.0.0.1:8888/wol -d @wol.json
diff --git a/admin/Sources/Services/ogAdmServer/tests/wol.json b/admin/Sources/Services/ogAdmServer/tests/wol.json
new file mode 100644
index 00000000..2a2f799f
--- /dev/null
+++ b/admin/Sources/Services/ogAdmServer/tests/wol.json
@@ -0,0 +1 @@
+{ "type" : "unicast", "clients" : [ { "addr" : "192.168.2.1", "mac" : "00AABBCCDD01" } ] }