Merge pull request #56 from painless-security/jennifer/show_realms
[trust_router.git] / mon / mon_resp.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 <tr_name_internal.h>
38
39 #include <mon_internal.h>
40
41 // Monitoring request message common code
42
43 /**
44  * Destructor used by talloc to ensure proper cleanup
45  */
46 static int mon_resp_destructor(void *object)
47 {
48   MON_RESP *resp = talloc_get_type_abort(object, MON_RESP);
49   /* free the message */
50   if (resp->message) {
51     tr_free_name(resp->message);
52   }
53   /* free the payload */
54   if (resp->payload) {
55     json_decref(resp->payload);
56   }
57   return 0;
58 }
59
60 /**
61  * Allocate a new monitoring response
62  *
63  * Caller must free using mon_resp_free().
64  *
65  * Makes its own copy of the message, so caller can dispose of
66  * that after allocating the response.
67  *
68  * Increments the reference count of the payload if it is not null.
69  *
70  * @param mem_ctx talloc context for allocation
71  * @param req MON_REQ this response corresponds to
72  * @param code numeric response code
73  * @param msg string description of response code
74  * @param payload JSON object to be send as payload, or null for no payload
75  * @return response allocated in the requested talloc context, null on failure
76  */
77 MON_RESP *mon_resp_new(TALLOC_CTX *mem_ctx, MON_RESP_CODE code, const char *msg, json_t *payload)
78 {
79   MON_RESP *resp = talloc(mem_ctx, MON_RESP);
80   if (resp) {
81     resp->code = code;
82     resp->message = tr_new_name(msg);
83
84     resp->payload = payload;
85     if (resp->payload)
86       json_incref(resp->payload);
87
88     talloc_set_destructor((void *)resp, mon_resp_destructor);
89     if (resp->message == NULL) {
90       talloc_free(resp); // destructor will be called
91       resp = NULL;
92     }
93   }
94   return resp;
95 }
96
97 /**
98  * Set or replace the response message
99  *
100  * Does not change the message if it fails
101  *
102  * @param resp
103  * @param new_msg
104  * @return 1 on success, 0 on error
105  */
106 int mon_resp_set_message(MON_RESP *resp, const char *new_msg)
107 {
108   TR_NAME *n = tr_new_name(new_msg);
109
110   if (n == NULL)
111     return 0; /* failed */
112
113   if (resp->message)
114     tr_free_name(resp->message);
115   resp->message = n;
116   return 1; /* succeeded */
117 }
118
119 /**
120  * Set or replace the payload
121  *
122  * Manages JSON reference counts
123  *
124  * @param resp
125  * @param new_payload
126  */
127 void mon_resp_set_payload(MON_RESP *resp, json_t *new_payload)
128 {
129   if (resp->payload)
130     json_decref(resp->payload);
131   resp->payload = new_payload;
132   if (resp->payload)
133     json_incref(new_payload);
134 }
135
136 /**
137  * Free a monitoring response
138  *
139  * @param resp request to free, must not be null
140  */
141 void mon_resp_free(MON_RESP *resp)
142 {
143   talloc_free(resp);
144 }