summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/rest.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/src/rest.c b/src/rest.c
index da75f26..2864de2 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -5537,6 +5537,158 @@ err_return:
return -1;
}
+static int og_cmd_get_servers(char *buffer_reply)
+{
+ json_t *root, *servers, *address;
+ struct og_buffer og_buffer = {
+ .data = buffer_reply,
+ };
+ char ipaddr[50] = {};
+ struct og_dbi *dbi;
+ dbi_result result;
+ int id, ret;
+
+ root = json_object();
+ if (!root)
+ return -1;
+
+ servers = json_array();
+ if (!servers) {
+ json_decref(root);
+ return -1;
+ }
+
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
+ __func__, __LINE__);
+ json_decref(root);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT identorno, ipserveradm "
+ "FROM entornos");
+
+ while (dbi_result_next_row(result)) {
+ address = json_object();
+ id = dbi_result_get_int(result, "identorno");
+ snprintf(ipaddr, sizeof(ipaddr), "%s",
+ dbi_result_get_string(result, "ipserveradm"));
+
+ json_object_set_new(address, "id", json_integer(id));
+ json_object_set_new(address, "address", json_string(ipaddr));
+
+ json_array_append_new(servers, address);
+ }
+
+ json_object_set_new(root, "servers", servers);
+
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+
+ ret = json_dump_callback(root, og_json_dump_clients, &og_buffer, 0);
+ json_decref(root);
+
+ return ret;
+}
+
+static int og_cmd_post_server(json_t *element,
+ struct og_msg_params *params,
+ char *buffer_reply)
+{
+ struct og_buffer og_buffer = {
+ .data = buffer_reply,
+ };
+ const char *key, *msglog;
+ json_t *value, *root;
+ int err = 0, id, ret;
+ struct in_addr addr;
+ struct og_dbi *dbi;
+ dbi_result result;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "address")) {
+ err = og_json_parse_string(value, &params->name);
+ params->flags |= OG_REST_PARAM_NAME;
+ }
+
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_msg_params_validate(params, OG_REST_PARAM_NAME))
+ return -1;
+
+ if (inet_pton(AF_INET, params->name, &addr) <= 0) {
+ syslog(LOG_ERR, "invalid server ip address (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open conection database (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT identorno FROM entornos WHERE ipserveradm='%s'",
+ params->name);
+
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ if (dbi_result_get_numrows(result) > 0) {
+ syslog(LOG_ERR, "Address %s already exists\n",
+ params->name);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(dbi->conn,
+ "INSERT INTO entornos("
+ " ipserveradm,"
+ " protoclonacion) VALUES ("
+ "'%s', 'UNICAST')",
+ params->name);
+
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to add new ogserver address to database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ root = json_object();
+ if (!root) {
+ syslog(LOG_ERR, "Cannot allocate JSON object (%s:%d)\n",
+ __func__, __LINE__);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return -1;
+ }
+ id = dbi_conn_sequence_last(dbi->conn, NULL);
+ json_object_set_new(root, "id", json_integer(id));
+ ret = json_dump_callback(root, og_json_dump_clients, &og_buffer, 0);
+
+ json_decref(root);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+
+ return ret;
+}
+
static int og_dbi_update_oglive(struct og_dbi *dbi, const char *mac,
const char * oglive)
{
@@ -6366,6 +6518,23 @@ int og_client_state_process_payload_rest(struct og_client *cli)
goto err_process_rest_payload;
}
err = og_cmd_post_task_add(root, &params);
+ } else if (!strncmp(cmd, "server", strlen("server"))) {
+ switch (method) {
+ case OG_METHOD_GET:
+ err = og_cmd_get_servers(buf_reply);
+ break;
+ case OG_METHOD_POST:
+ if (!root) {
+ syslog(LOG_ERR, "address add command with no payload\n");
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+ err = og_cmd_post_server(root, &params, buf_reply);
+ break;
+ default:
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
} else if (!strncmp(cmd, "stats", strlen("stats"))) {
if (method != OG_METHOD_GET) {
err = og_client_method_not_found(cli);