Add encoder for monitoring responses
[trust_router.git] / mon / mon_req_decode.c
1 /*
2  * Copyright (c) 2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35
36 #include <talloc.h>
37 #include <jansson.h>
38
39 #include <mon_internal.h>
40
41 // Monitoring request decoders
42
43 /**
44  * Decode a single option
45  *
46  * Format:
47  * { "type": "some_tpye" }
48  *
49  * @param opt_json JSON object reference
50  * @param dest allocated memory for the result
51  * @return MON_SUCCESS on success, error on error
52  */
53 static MON_RC mon_decode_one_opt(json_t *opt_json, MON_OPT *dest)
54 {
55   json_t *jstr = NULL;
56   MON_OPT_TYPE opt_type = OPT_TYPE_UNKNOWN;
57
58   if ( (opt_json == NULL) || (dest == NULL))
59     return MON_BADARG;
60
61   if (! json_is_object(opt_json))
62     return MON_NOPARSE;
63
64   jstr = json_object_get(opt_json, "type");
65   if ( (jstr == NULL) || (! json_is_string(jstr)) )
66     return MON_NOPARSE;
67
68   opt_type = mon_opt_type_from_string(json_string_value(jstr));
69   if (opt_type == OPT_TYPE_UNKNOWN)
70     return MON_NOPARSE;
71
72   dest->type = opt_type;
73   return MON_SUCCESS;
74 }
75
76 /**
77  * Decode options array
78  *
79  * Format:
80  * [{option}, {option}, ...]
81  *
82  */
83 static MON_RC mon_options_decode(json_t *opts_json, MON_REQ *req)
84 {
85   MON_OPT opt; // not a pointer
86   size_t n_opts=0;
87   size_t ii=0;
88
89   if ( (opts_json == NULL) || (req == NULL))
90     return MON_BADARG;
91
92   if (! json_is_array(opts_json))
93     return MON_NOPARSE;
94
95   n_opts = json_array_size(opts_json);
96   for (ii=0; ii < n_opts; ii++) {
97     if (mon_decode_one_opt(json_array_get(opts_json, ii),
98                            &opt) != MON_SUCCESS) {
99       return MON_NOPARSE;
100     }
101     mon_req_add_option(req, opt.type);
102   }
103   return MON_SUCCESS;
104 }
105
106 /**
107  * Parse JSON for a request
108  */
109 static json_t *mon_req_parse(const char *input)
110 {
111   json_t *parsed_json = NULL;
112   json_error_t json_error;
113
114   parsed_json = json_loads(input, JSON_REJECT_DUPLICATES, &json_error);
115   return parsed_json;
116 }
117
118 /**
119  * Decode a JSON request
120  *
121  * Expected format:
122  * {
123  *   "command": "some_command_name",
124  *   "options": [{option1}, ...]
125  * }
126  *
127  * (options are optional)
128  *
129  * Caller must free the return value with MON_REQ_free().
130  *
131  * @param mem_ctx talloc context for the returned struct
132  * @param req_json reference to JSON request object
133  * @return decoded request struct or NULL on failure
134  */
135 MON_REQ *mon_req_decode(TALLOC_CTX *mem_ctx, const char *req_str)
136 {
137   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
138   MON_REQ *req = NULL;
139   json_t *req_json = NULL;
140   json_t *jval = NULL;
141   json_t *opts_json = NULL;
142   MON_CMD cmd = MON_CMD_UNKNOWN;
143
144   req_json = mon_req_parse(req_str); // TODO: Check errors
145
146   if (! json_is_object(req_json))
147     goto cleanup;
148
149   // Get the command and verify that it is a string value
150   jval = json_object_get(req_json, "command");
151   if (! json_is_string(jval))
152     goto cleanup;
153
154   cmd = mon_cmd_from_string(json_string_value(jval));
155   if (cmd == MON_CMD_UNKNOWN)
156     goto cleanup;
157
158   /* Command is good. Allocate the request in the tmp context */
159   req = mon_req_new(tmp_ctx, cmd);
160   if (req == NULL)
161     goto cleanup;
162
163   /* Parse options if we have any */
164   opts_json = json_object_get(req_json, "options");
165   if (opts_json) {
166     if (mon_options_decode(opts_json, req) != MON_SUCCESS) {
167       req = NULL; // memory still in tmp_ctx, so it will be cleaned up
168       goto cleanup;
169     }
170   }
171
172   /* Success! Put the request in the caller's talloc context */
173   talloc_steal(mem_ctx, req);
174
175 cleanup:
176   talloc_free(tmp_ctx);
177   if (req_json)
178     json_decref(req_json);
179
180   return req;
181 }