2 * Copyright (c) 2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
35 /* Handlers for monitoring requests */
40 #include <mon_internal.h>
41 #include <mons_handlers.h>
44 /* Static Prototypes */
45 static int dispatch_entry_matches(MONS_DISPATCH_TABLE_ENTRY *e, MON_CMD command, MON_OPT_TYPE opt_type);
46 static MONS_HANDLER_FUNC *mons_find_handler(MONS_INSTANCE *mons, MON_CMD cmd, MON_OPT_TYPE opt_type);
47 static void request_helper(void *element, void *data);
49 struct request_helper_data {
51 MON_OPT_TYPE opt_type;
52 json_t *payload; /* json object to add responses to */
56 struct handler_result {
57 MON_OPT_TYPE opt_type; /* what opt type set this? */
58 MON_RC rc; /* what was its result code? */
59 json_t *json_data; /* what data, if any, is it returning? */
63 * Call the appropriate handler for a request
65 * TODO: report errors from handlers
67 * @return a MON_RESP structure or null if there was a processing error
69 MON_RESP *mons_handle_request(TALLOC_CTX *mem_ctx, MONS_INSTANCE *mons, MON_REQ *req)
71 MON_RESP *resp = NULL;
72 json_t *payload = NULL;
73 struct request_helper_data cookie = {0};
76 tr_debug("mons_handle_request: Handling a request");
78 /* Start off by allocating our response with a generic error message */
79 resp = mon_resp_new(mem_ctx,
81 "error processing request",
84 /* we can't respond, just return */
85 tr_crit("mons_handle_request: Error allocating response structure.");
89 /* Now get a JSON object for our return payload */
90 payload = json_object();
91 if (payload == NULL) {
92 tr_crit("mons_handle_request: Error allocating response payload.");
93 goto cleanup; /* This will return the generic error message set earlier */
96 /* Now call handlers */
97 cookie.command = req->command;
98 cookie.results = g_array_new(FALSE, TRUE, sizeof(struct handler_result));
100 if (mon_req_opt_count(req) == 0) {
101 /* call every handler that matches the command */
102 cookie.opt_type = OPT_TYPE_ANY;
103 g_ptr_array_foreach(mons->handlers, request_helper, &cookie);
105 /* call only those handlers that match an option */
106 for (ii=0; ii < mon_req_opt_count(req); ii++) {
107 cookie.opt_type = mon_req_opt_index(req, ii)->type;
108 /* Loop over all handlers - we know we can only have one match for each opt type */
109 g_ptr_array_foreach(mons->handlers, request_helper, &cookie);
113 /* We now have an array of results in cookie.results. If any of these failed, return an error. */
114 tr_debug("mons_handle_request: Examining %d handler results", cookie.results->len);
115 resp->code = MON_RESP_SUCCESS; /* tentatively set this to success */
116 for (ii=0; ii < cookie.results->len; ii++) {
117 struct handler_result *this = &g_array_index(cookie.results, struct handler_result, ii);
118 if (this->rc != MON_SUCCESS) {
119 tr_debug("mons_handle_request: Result %d was an error.", ii);
120 resp->code = MON_RESP_ERROR;
123 /* add the JSON response even if there was an error */
124 if (this->json_data) {
125 tr_debug("mons_handle_request: Result %d returned JSON data.", ii);
126 json_object_set_new(payload, mon_opt_type_to_string(this->opt_type), this->json_data);
130 if (resp->code == MON_RESP_SUCCESS) {
131 if (mon_resp_set_message(resp, "success") == 0) {
132 /* Failed to set the response message to success - fail ironically, don't send
133 * an inconsistent response. */
134 tr_crit("mons_handle_request: Error setting response message to 'success'.");
138 /* Failed - send a response indicating that the overall command succeeded */
139 if (mon_resp_set_message(resp, "request processed but an error occurred") == 0) {
140 tr_crit("mons_handle_request: Error setting response message after a handler error.");
145 /* Attach the accumulated payload to the response */
146 if (json_object_size(payload) > 0) {
147 tr_debug("mons_handle_request: Attaching payload to response.");
148 mon_resp_set_payload(resp, payload);
151 tr_debug("mons_handle_request: Successfully processed request.");
155 json_decref(payload);
157 g_array_free(cookie.results, TRUE);
162 * Register a handler for a command/option combination
171 MON_RC mons_register_handler(MONS_INSTANCE *mons,
173 MON_OPT_TYPE opt_type,
174 MONS_HANDLER_FUNC *f,
177 MONS_DISPATCH_TABLE_ENTRY *entry = NULL;
179 if (mons_find_handler(mons, cmd, opt_type) != NULL) {
183 /* Put these in the mons talloc context so we don't have to muck about with
184 * a free function for the GPtrArray */
185 entry = talloc(mons, MONS_DISPATCH_TABLE_ENTRY);
189 entry->command = cmd;
190 entry->opt_type = opt_type;
192 entry->cookie = cookie;
194 g_ptr_array_add(mons->handlers, entry);
199 * Two table entries match if none of the commands or opt_types are unknown,
200 * if the commands match, and if the opt types either match or at least one is
203 * No comparison of the handler pointer is included.
205 * @return 1 if the two match, 0 if not
207 static int dispatch_entry_matches(MONS_DISPATCH_TABLE_ENTRY *e,
209 MON_OPT_TYPE opt_type)
211 if ((command == MON_CMD_UNKNOWN) || (opt_type == OPT_TYPE_UNKNOWN))
212 return 0; /* request is invalid */
214 if ((e->command == MON_CMD_UNKNOWN) || (e->opt_type == OPT_TYPE_UNKNOWN))
215 return 0; /* e1 is invalid */
217 if (e->command != command)
218 return 0; /* commands do not match */
220 if (e->opt_type == opt_type)
221 return 1; /* exact match */
223 if ( (e->opt_type == OPT_TYPE_ANY) || (opt_type == OPT_TYPE_ANY) )
224 return 1; /* one is a wildcard */
226 return 0; /* commands matched but opt_types did not */
229 static MONS_HANDLER_FUNC *mons_find_handler(MONS_INSTANCE *mons, MON_CMD cmd, MON_OPT_TYPE opt_type)
233 for (index=0; index < mons->handlers->len; index++) {
234 if (dispatch_entry_matches(g_ptr_array_index(mons->handlers, index), cmd, opt_type))
235 return g_ptr_array_index(mons->handlers, index);
241 * This calls every request handler that matches a command/opt_type,
242 * gathering their results.
247 static void request_helper(void *element, void *data)
249 MONS_DISPATCH_TABLE_ENTRY *entry = talloc_get_type_abort(element, MONS_DISPATCH_TABLE_ENTRY);
250 struct request_helper_data *helper_data = data;
251 struct handler_result result = {0};
253 if (dispatch_entry_matches(entry, helper_data->command, helper_data->opt_type)) {
254 result.rc = entry->handler(entry->cookie, &(result.json_data));
255 result.opt_type = entry->opt_type;
256 g_array_append_val(helper_data->results, result);