Merge pull request #82 from painless-security/jennifer/pull_req_feedback
[trust_router.git] / mon / mon_resp_encode.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 #include <talloc.h>
36 #include <jansson.h>
37
38 #include <mon_internal.h>
39
40 /* Monitoring response encoder */
41
42 /* Helper for encoding. Adds a newly allocated JSON object to
43  * jobj. If the allocation or setting fails, returns NULL after
44  * cleaning up. */
45 #define object_set_or_free_and_return(jobj, tmp_jval, key, jval)  \
46   do {                                                            \
47     (tmp_jval) = (jval);                                          \
48     if ( (tmp_jval) == NULL) {                                    \
49       json_decref(jobj);                                          \
50       return NULL;                                                \
51     }                                                             \
52     if (json_object_set_new((jobj), (key), (tmp_jval)) == -1) {   \
53       json_decref(tmp_jval);                                      \
54       json_decref(jobj);                                          \
55       return NULL;                                                \
56     }                                                             \
57   } while(0)
58
59 /**
60  * Encode a monitoring response as a JSON object
61  *
62  * Caller must ensure json_decref() is used to free the return value.
63  *
64  * @param resp response to encode
65  * @return response as a newly allocated JSON object
66  */
67 json_t *mon_resp_encode(MON_RESP *resp)
68 {
69   json_t *resp_json = NULL;
70   json_t *jval = NULL;
71
72   /* Get a JSON object */
73   resp_json = json_object();
74   if (resp_json == NULL)
75     return NULL;
76
77   /* Add properties, cleaning up and returning NULL on failure */
78   object_set_or_free_and_return(resp_json, jval, "code",    json_integer(resp->code));
79   object_set_or_free_and_return(resp_json, jval, "message", tr_name_to_json_string(resp->message));
80
81   /* If we have a payload, add it */
82   if (resp->payload) {
83     object_set_or_free_and_return(resp_json, jval, "payload", resp->payload);
84     json_incref(resp->payload); /* we just created a second reference to the payload */
85   }
86
87   return resp_json;
88 }