diff options
Diffstat (limited to 'src/json_rpc.c')
| -rw-r--r-- | src/json_rpc.c | 226 |
1 files changed, 103 insertions, 123 deletions
diff --git a/src/json_rpc.c b/src/json_rpc.c index b81a5a0..2ab4624 100644 --- a/src/json_rpc.c +++ b/src/json_rpc.c @@ -6,6 +6,7 @@ */ #include "json_rpc.h" + #include "json.h" #include "log.h" @@ -17,24 +18,23 @@ #include <string.h> struct jsonrpc_request { - struct json_object *impl; + struct json_object* impl; }; struct jsonrpc_response { - struct json_object *impl; + struct json_object* impl; }; -static const char *const jsonrpc_key_version = "jsonrpc"; -static const char *const jsonrpc_key_id = "id"; -static const char *const jsonrpc_key_method = "method"; -static const char *const jsonrpc_key_params = "params"; +static const char* const jsonrpc_key_version = "jsonrpc"; +static const char* const jsonrpc_key_id = "id"; +static const char* const jsonrpc_key_method = "method"; +static const char* const jsonrpc_key_params = "params"; -static const char *const jsonrpc_value_version = "2.0"; +static const char* const jsonrpc_value_version = "2.0"; -static int jsonrpc_check_version(struct json_object *obj) -{ - const char *key = jsonrpc_key_version; - const char *version = NULL; +static int jsonrpc_check_version(struct json_object* obj) { + const char* key = jsonrpc_key_version; + const char* version = NULL; int ret = libjson_get_string(obj, key, &version); if (ret < 0) @@ -48,20 +48,17 @@ static int jsonrpc_check_version(struct json_object *obj) return 0; } -static int jsonrpc_set_version(struct json_object *obj) -{ +static int jsonrpc_set_version(struct json_object* obj) { return libjson_set_string_const_key(obj, jsonrpc_key_version, jsonrpc_value_version); } static _Atomic int jsonrpc_id_counter = 1; -int jsonrpc_generate_request_id(void) -{ +int jsonrpc_generate_request_id(void) { return jsonrpc_id_counter++; } -static int jsonrpc_check_id_type(struct json_object *id) -{ +static int jsonrpc_check_id_type(struct json_object* id) { if (!json_object_is_type(id, json_type_string) && !json_object_is_type(id, json_type_int)) { log_err("JSON-RPC: key '%s' must be either an integer or a string\n", jsonrpc_key_id); @@ -70,9 +67,8 @@ static int jsonrpc_check_id_type(struct json_object *id) return 0; } -static int jsonrpc_check_id(struct json_object *obj, int required) -{ - const char *key = jsonrpc_key_id; +static int jsonrpc_check_id(struct json_object* obj, int required) { + const char* key = jsonrpc_key_id; if (!libjson_has(obj, key)) { if (!required) @@ -81,7 +77,7 @@ static int jsonrpc_check_id(struct json_object *obj, int required) return -1; } - struct json_object *id = NULL; + struct json_object* id = NULL; int ret = libjson_get(obj, key, &id); if (ret < 0) @@ -89,25 +85,21 @@ static int jsonrpc_check_id(struct json_object *obj, int required) return jsonrpc_check_id_type(id); } -static int jsonrpc_set_id(struct json_object *obj, int id) -{ +static int jsonrpc_set_id(struct json_object* obj, int id) { return libjson_set_int_const_key(obj, jsonrpc_key_id, id); } -static int jsonrpc_check_method(struct json_object *obj) -{ - const char *key = jsonrpc_key_method; - const char *method = NULL; +static int jsonrpc_check_method(struct json_object* obj) { + const char* key = jsonrpc_key_method; + const char* method = NULL; return libjson_get_string(obj, key, &method); } -static int jsonrpc_set_method(struct json_object *obj, const char *method) -{ +static int jsonrpc_set_method(struct json_object* obj, const char* method) { return libjson_set_string_const_key(obj, jsonrpc_key_method, method); } -static int jsonrpc_check_params_type(struct json_object *params) -{ +static int jsonrpc_check_params_type(struct json_object* params) { if (!json_object_is_type(params, json_type_object) && !json_object_is_type(params, json_type_array)) { log_err("JSON-RPC: key '%s' must be either an object or an array\n", @@ -117,14 +109,13 @@ static int jsonrpc_check_params_type(struct json_object *params) return 0; } -static int jsonrpc_check_params(struct json_object *obj) -{ - const char *key = jsonrpc_key_params; +static int jsonrpc_check_params(struct json_object* obj) { + const char* key = jsonrpc_key_params; if (!libjson_has(obj, key)) return 0; - struct json_object *params = NULL; + struct json_object* params = NULL; int ret = libjson_get(obj, key, ¶ms); if (ret < 0) @@ -132,9 +123,8 @@ static int jsonrpc_check_params(struct json_object *obj) return jsonrpc_check_params_type(params); } -static int jsonrpc_set_params(struct json_object *obj, struct json_object *params) -{ - const char *key = jsonrpc_key_params; +static int jsonrpc_set_params(struct json_object* obj, struct json_object* params) { + const char* key = jsonrpc_key_params; int ret = jsonrpc_check_params_type(params); if (ret < 0) @@ -142,16 +132,15 @@ static int jsonrpc_set_params(struct json_object *obj, struct json_object *param return libjson_set_const_key(obj, key, params); } -static const char *const jsonrpc_key_result = "result"; -static const char *const jsonrpc_key_error = "error"; +static const char* const jsonrpc_key_result = "result"; +static const char* const jsonrpc_key_error = "error"; -static const char *const jsonrpc_key_code = "code"; -static const char *const jsonrpc_key_message = "message"; +static const char* const jsonrpc_key_code = "code"; +static const char* const jsonrpc_key_message = "message"; -static int jsonrpc_check_error(struct json_object *obj) -{ - const char *key = jsonrpc_key_error; - struct json_object *error = NULL; +static int jsonrpc_check_error(struct json_object* obj) { + const char* key = jsonrpc_key_error; + struct json_object* error = NULL; int ret = libjson_get(obj, key, &error); if (ret < 0) @@ -165,7 +154,7 @@ static int jsonrpc_check_error(struct json_object *obj) return -1; } - const char *message = NULL; + const char* message = NULL; ret = libjson_get_string(error, jsonrpc_key_message, &message); if (ret < 0) { @@ -176,10 +165,9 @@ static int jsonrpc_check_error(struct json_object *obj) return ret; } -static int jsonrpc_check_result_or_error(struct json_object *obj) -{ - const char *key_result = jsonrpc_key_result; - const char *key_error = jsonrpc_key_error; +static int jsonrpc_check_result_or_error(struct json_object* obj) { + const char* key_result = jsonrpc_key_result; + const char* key_error = jsonrpc_key_error; if (!libjson_has(obj, key_result) && !libjson_has(obj, key_error)) { log_err("JSON-RPC: either '%s' or '%s' must be present\n", key_result, key_error); @@ -192,12 +180,13 @@ static int jsonrpc_check_result_or_error(struct json_object *obj) return jsonrpc_check_error(obj); } -static int jsonrpc_request_create_internal(struct jsonrpc_request **_request, int *id, - const char *method, struct json_object *params) -{ +static int jsonrpc_request_create_internal(struct jsonrpc_request** _request, + int* id, + const char* method, + struct json_object* params) { int ret = 0; - struct jsonrpc_request *request = malloc(sizeof(struct jsonrpc_request)); + struct jsonrpc_request* request = malloc(sizeof(struct jsonrpc_request)); if (!request) { log_errno("malloc"); ret = -1; @@ -239,31 +228,29 @@ exit: return ret; } -int jsonrpc_request_create(struct jsonrpc_request **_request, int id, const char *method, - struct json_object *params) -{ +int jsonrpc_request_create(struct jsonrpc_request** _request, + int id, + const char* method, + struct json_object* params) { return jsonrpc_request_create_internal(_request, &id, method, params); } -void jsonrpc_request_destroy(struct jsonrpc_request *request) -{ +void jsonrpc_request_destroy(struct jsonrpc_request* request) { libjson_free(request->impl); free(request); } -int jsonrpc_notification_create(struct jsonrpc_request **_request, const char *method, - struct json_object *params) -{ +int jsonrpc_notification_create(struct jsonrpc_request** _request, + const char* method, + struct json_object* params) { return jsonrpc_request_create_internal(_request, NULL, method, params); } -int jsonrpc_request_is_notification(const struct jsonrpc_request *request) -{ +int jsonrpc_request_is_notification(const struct jsonrpc_request* request) { return !libjson_has(request->impl, jsonrpc_key_id); } -static int jsonrpc_request_from_json(struct jsonrpc_request **_request, struct json_object *impl) -{ +static int jsonrpc_request_from_json(struct jsonrpc_request** _request, struct json_object* impl) { int ret = 0; ret = jsonrpc_check_version(impl); @@ -279,7 +266,7 @@ static int jsonrpc_request_from_json(struct jsonrpc_request **_request, struct j if (ret < 0) return ret; - struct jsonrpc_request *request = malloc(sizeof(struct jsonrpc_request)); + struct jsonrpc_request* request = malloc(sizeof(struct jsonrpc_request)); if (!request) { log_errno("malloc"); return -1; @@ -290,14 +277,12 @@ static int jsonrpc_request_from_json(struct jsonrpc_request **_request, struct j return ret; } -int jsonrpc_request_send(const struct jsonrpc_request *request, int fd) -{ +int jsonrpc_request_send(const struct jsonrpc_request* request, int fd) { return libjson_send(request->impl, fd); } -int jsonrpc_request_recv(struct jsonrpc_request **request, int fd) -{ - struct json_object *impl = libjson_recv(fd); +int jsonrpc_request_recv(struct jsonrpc_request** request, int fd) { + struct json_object* impl = libjson_recv(fd); if (!impl) { log_err("JSON-RPC: failed to receive request\n"); return -1; @@ -315,9 +300,8 @@ free_impl: return ret; } -const char *jsonrpc_request_get_method(const struct jsonrpc_request *request) -{ - const char *method = NULL; +const char* jsonrpc_request_get_method(const struct jsonrpc_request* request) { + const char* method = NULL; int ret = libjson_get_string(request->impl, jsonrpc_key_method, &method); if (ret < 0) { /* Should never happen. */ @@ -326,13 +310,12 @@ const char *jsonrpc_request_get_method(const struct jsonrpc_request *request) return method; } -static struct json_object *jsonrpc_request_create_params(struct jsonrpc_request *request) -{ +static struct json_object* jsonrpc_request_create_params(struct jsonrpc_request* request) { int ret = 0; - const char *const key = jsonrpc_key_params; + const char* const key = jsonrpc_key_params; if (!libjson_has(request->impl, key)) { - struct json_object *params = NULL; + struct json_object* params = NULL; ret = libjson_new_object(¶ms); if (ret < 0) @@ -346,62 +329,62 @@ static struct json_object *jsonrpc_request_create_params(struct jsonrpc_request return params; } - struct json_object *params = NULL; + struct json_object* params = NULL; ret = libjson_get(request->impl, key, ¶ms); if (ret < 0) return NULL; return params; } -int jsonrpc_request_get_param_string(const struct jsonrpc_request *request, const char *name, - const char **value) -{ - struct json_object *params = NULL; +int jsonrpc_request_get_param_string(const struct jsonrpc_request* request, + const char* name, + const char** value) { + struct json_object* params = NULL; int ret = libjson_get(request->impl, jsonrpc_key_params, ¶ms); if (ret < 0) return ret; return libjson_get_string(params, name, value); } -int jsonrpc_request_set_param_string(struct jsonrpc_request *request, const char *name, - const char *value) -{ - struct json_object *params = jsonrpc_request_create_params(request); +int jsonrpc_request_set_param_string(struct jsonrpc_request* request, + const char* name, + const char* value) { + struct json_object* params = jsonrpc_request_create_params(request); if (!params) return -1; return libjson_set_string(params, name, value); } -int jsonrpc_request_get_param_int(const struct jsonrpc_request *request, const char *name, - int64_t *value) -{ - struct json_object *params = NULL; +int jsonrpc_request_get_param_int(const struct jsonrpc_request* request, + const char* name, + int64_t* value) { + struct json_object* params = NULL; int ret = libjson_get(request->impl, jsonrpc_key_params, ¶ms); if (ret < 0) return ret; return libjson_get_int(params, name, value); } -int jsonrpc_request_set_param_int(struct jsonrpc_request *request, const char *name, int64_t value) -{ - struct json_object *params = jsonrpc_request_create_params(request); +int jsonrpc_request_set_param_int(struct jsonrpc_request* request, + const char* name, + int64_t value) { + struct json_object* params = jsonrpc_request_create_params(request); if (!params) return -1; return libjson_set_int(params, name, value); } -const char *jsonrpc_response_to_string(const struct jsonrpc_response *response) -{ +const char* jsonrpc_response_to_string(const struct jsonrpc_response* response) { return libjson_to_string_pretty(response->impl); } -int jsonrpc_response_create_internal(struct jsonrpc_response **_response, - const struct jsonrpc_request *request, - struct json_object *result, struct json_object *error) -{ +int jsonrpc_response_create_internal(struct jsonrpc_response** _response, + const struct jsonrpc_request* request, + struct json_object* result, + struct json_object* error) { int ret = 0; - struct jsonrpc_response *response = malloc(sizeof(struct jsonrpc_response)); + struct jsonrpc_response* response = malloc(sizeof(struct jsonrpc_response)); if (!response) { log_errno("malloc"); ret = -1; @@ -416,7 +399,7 @@ int jsonrpc_response_create_internal(struct jsonrpc_response **_response, if (ret < 0) goto free_impl; - struct json_object *id = NULL; + struct json_object* id = NULL; ret = libjson_clone(request->impl, jsonrpc_key_id, &id); if (ret < 0) goto free_impl; @@ -448,23 +431,23 @@ exit: return ret; } -int jsonrpc_response_create(struct jsonrpc_response **response, - const struct jsonrpc_request *request, struct json_object *result) -{ +int jsonrpc_response_create(struct jsonrpc_response** response, + const struct jsonrpc_request* request, + struct json_object* result) { return jsonrpc_response_create_internal(response, request, result, NULL); } -void jsonrpc_response_destroy(struct jsonrpc_response *response) -{ +void jsonrpc_response_destroy(struct jsonrpc_response* response) { libjson_free(response->impl); free(response); } -int jsonrpc_error_create(struct jsonrpc_response **response, struct jsonrpc_request *request, - int code, const char *message) -{ +int jsonrpc_error_create(struct jsonrpc_response** response, + struct jsonrpc_request* request, + int code, + const char* message) { int ret = 0; - struct json_object *error = NULL; + struct json_object* error = NULL; ret = libjson_new_object(&error); if (ret < 0) @@ -489,13 +472,12 @@ free: return ret; } -int jsonrpc_response_is_error(const struct jsonrpc_response *response) -{ +int jsonrpc_response_is_error(const struct jsonrpc_response* response) { return libjson_has(response->impl, jsonrpc_key_error); } -static int jsonrpc_response_from_json(struct jsonrpc_response **_response, struct json_object *impl) -{ +static int jsonrpc_response_from_json(struct jsonrpc_response** _response, + struct json_object* impl) { int ret = 0; ret = jsonrpc_check_version(impl); @@ -508,7 +490,7 @@ static int jsonrpc_response_from_json(struct jsonrpc_response **_response, struc if (ret < 0) return ret; - struct jsonrpc_response *response = malloc(sizeof(struct jsonrpc_response)); + struct jsonrpc_response* response = malloc(sizeof(struct jsonrpc_response)); if (!response) { log_errno("malloc"); return -1; @@ -519,14 +501,12 @@ static int jsonrpc_response_from_json(struct jsonrpc_response **_response, struc return ret; } -int jsonrpc_response_send(const struct jsonrpc_response *response, int fd) -{ +int jsonrpc_response_send(const struct jsonrpc_response* response, int fd) { return libjson_send(response->impl, fd); } -int jsonrpc_response_recv(struct jsonrpc_response **response, int fd) -{ - struct json_object *impl = libjson_recv(fd); +int jsonrpc_response_recv(struct jsonrpc_response** response, int fd) { + struct json_object* impl = libjson_recv(fd); if (!impl) { log_err("JSON-RPC: failed to receive response\n"); return -1; |
