From 608709f6206eb4061a6d59eff68babd3a8562129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Hueso=20G=C3=B3mez?= Date: Mon, 29 Jun 2020 14:09:07 +0200 Subject: [PATCH] #980 Add GET /modes REST request This patch implements HTTP GET /modes request which returns all modes available for clients: Request: GET /modes HTTP/1.0 Response: 200 OK { "modes": [ "pxe", "10", "13", "00unknown", "11", "19pxeADMIN", "12" ] } --- src/rest.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/rest.c b/src/rest.c index 097cbf8..43871e4 100644 --- a/src/rest.c +++ b/src/rest.c @@ -21,6 +21,7 @@ #include #include #include +#include #include struct ev_loop *og_loop; @@ -837,6 +838,54 @@ static int og_cmd_reboot(json_t *element, struct og_msg_params *params) return og_send_request(OG_METHOD_POST, OG_CMD_REBOOT, params, NULL); } +#define OG_TFTP_TMPL_PATH "/opt/opengnsys/tftpboot/menu.lst/templates" + +static int og_cmd_get_modes(json_t *element, struct og_msg_params *params, + char *buffer_reply) +{ + struct og_buffer og_buffer = { + .data = buffer_reply + }; + json_t *root, *modes; + struct dirent *dent; + DIR *d = NULL; + + root = json_object(); + if (!root) + return -1; + + modes = json_array(); + if (!modes) { + json_decref(root); + return -1; + } + + d = opendir(OG_TFTP_TMPL_PATH); + if (!d) { + json_decref(modes); + json_decref(root); + syslog(LOG_ERR, "Cannot open directory %s\n", + OG_TFTP_TMPL_PATH); + return -1; + } + + dent = readdir(d); + while (dent) { + if (dent->d_type != DT_REG) { + dent = readdir(d); + continue; + } + json_array_append_new(modes, json_string(dent->d_name)); + dent = readdir(d); + } + + json_object_set_new(root, "modes", modes); + json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); + json_decref(root); + + return 0; +} + static int og_cmd_stop(json_t *element, struct og_msg_params *params) { const char *key; @@ -3116,6 +3165,11 @@ int og_client_state_process_payload_rest(struct og_client *cli) return og_client_bad_request(cli); } err = og_cmd_reboot(root, ¶ms); + } else if (!strncmp(cmd, "modes", strlen("modes"))) { + if (method != OG_METHOD_GET) + return og_client_method_not_found(cli); + + err = og_cmd_get_modes(root, ¶ms, buf_reply); } else if (!strncmp(cmd, "stop", strlen("stop"))) { if (method != OG_METHOD_POST) return og_client_method_not_found(cli);