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.
37 #include <tr_name_internal.h>
39 #include <mon_internal.h>
41 /* Monitoring response message common code */
44 * Destructor used by talloc to ensure proper cleanup
46 static int mon_resp_destructor(void *object)
48 MON_RESP *resp = talloc_get_type_abort(object, MON_RESP);
49 /* free the message */
51 tr_free_name(resp->message);
53 /* free the payload */
55 json_decref(resp->payload);
61 * Allocate a new monitoring response
63 * Caller must free using mon_resp_free().
65 * Makes its own copy of the message, so caller can dispose of
66 * that after allocating the response.
68 * Increments the reference count of the payload if it is not null.
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
77 MON_RESP *mon_resp_new(TALLOC_CTX *mem_ctx, MON_RESP_CODE code, const char *msg, json_t *payload)
79 MON_RESP *resp = talloc(mem_ctx, MON_RESP);
82 resp->message = tr_new_name(msg);
84 resp->payload = payload;
86 json_incref(resp->payload);
88 talloc_set_destructor((void *)resp, mon_resp_destructor);
89 if (resp->message == NULL) {
90 talloc_free(resp); // destructor will be called
98 * Set or replace the response message
100 * Does not change the message if it fails
104 * @return 1 on success, 0 on error
106 int mon_resp_set_message(MON_RESP *resp, const char *new_msg)
108 TR_NAME *n = tr_new_name(new_msg);
111 return 0; /* failed */
114 tr_free_name(resp->message);
116 return 1; /* succeeded */
120 * Set or replace the payload
122 * Manages JSON reference counts
127 void mon_resp_set_payload(MON_RESP *resp, json_t *new_payload)
130 json_decref(resp->payload);
131 resp->payload = new_payload;
133 json_incref(new_payload);
137 * Free a monitoring response
139 * @param resp request to free, must not be null
141 void mon_resp_free(MON_RESP *resp)