diff options
-rw-r--r-- | README | 19 | ||||
-rw-r--r-- | src/ping903.c | 62 |
2 files changed, 81 insertions, 0 deletions
@@ -192,6 +192,25 @@ The default channel for communication with the ping903 daemon is the HTTP socket open on localhost port 8080. Only GET requests are allowed. The following endpoints are provided: +** /id + +Identifies the running instance. On success, a JSON object with the +following attributes is returned: + +- "package": string + The package name. + +- "version": string + Package version string. + +- "pid": number + PID of the running instance. + +** /id/ATTR + +ATTR is one of the attributes discussed above. Returned is the value +of that attribute. + ** /host/NAME NAME is the IP address or hostname. The server will look up this diff --git a/src/ping903.c b/src/ping903.c index 1b8a668..3de8e97 100644 --- a/src/ping903.c +++ b/src/ping903.c @@ -294,6 +294,67 @@ httpd_json_response(struct MHD_Connection *conn, return ret; } +static struct json_value * +ident_to_json(void) +{ + struct json_value *obj, *jv; + + obj = json_new_object(); + if (!obj) + return NULL; + if (!(jv = json_new_string(PACKAGE)) + || json_object_set(obj, "package", jv)) + goto err; + if (!(jv = json_new_string(PACKAGE_VERSION)) + || json_object_set(obj, "version", jv)) + goto err; + if (!(jv = json_new_number(getpid())) + || json_object_set(obj, "pid", jv)) + goto err; + return obj; +err: + json_value_free(jv); + json_value_free(obj); + return NULL; +} + +static int +ept_ident(struct MHD_Connection *conn, + const char *url, const char *method, const char *suffix, + struct json_value *unused) +{ + int ret; + struct json_value *obj; + + while (*suffix == '/') + suffix++; + obj = ident_to_json(); + if (!obj) + return http_response(conn, method, url, + MHD_HTTP_INTERNAL_SERVER_ERROR); + if (*suffix) { + struct json_value *jv, *cp; + if (json_object_get(obj, suffix, &jv)) { + ret = http_response(conn, method, url, + (errno == ENOENT) + ? MHD_HTTP_NOT_FOUND + : MHD_HTTP_INTERNAL_SERVER_ERROR); + json_value_free(obj); + return ret; + } + + if (json_value_copy(jv, &cp)) { + ret = http_response(conn, method, url, + MHD_HTTP_INTERNAL_SERVER_ERROR); + json_value_free(obj); + return ret; + } + json_value_free(obj); + obj = cp; + } + return httpd_json_response(conn, url, method, MHD_HTTP_OK, obj); +} + static int ept_config(struct MHD_Connection *conn, const char *url, const char *method, const char *suffix, @@ -774,6 +835,7 @@ struct endpoint { }; static struct endpoint endpoint[] = { + { "/id", EPT_PREFIX, MHD_HTTP_METHOD_GET, ept_ident }, { "/config/ip-list", EPT_PREFIX, MHD_HTTP_METHOD_PUT, ept_ip_put }, { "/config/ip-list", EPT_PREFIX, MHD_HTTP_METHOD_POST, ept_ip_post }, { "/config/ip-list", EPT_PREFIX, MHD_HTTP_METHOD_DELETE, ept_ip_delete }, |