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 request 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 * Steals the reference to the payload JSON object. Does not modify the
69 * object. Caller should not modify it after allocating the response or
70 * undefined behavior will result. If allocation fails, the stolen reference
71 * will be released --- if you need to keep a reference, use incref before
74 * @param mem_ctx talloc context for allocation
75 * @param req MON_REQ this response corresponds to
76 * @param code numeric response code
77 * @param msg string description of response code
78 * @param payload JSON object to be send as payload, or null for no payload
79 * @return response allocated in the requested talloc context, null on failure
81 MON_RESP *mon_resp_new(TALLOC_CTX *mem_ctx,
87 MON_RESP *resp = talloc(mem_ctx, MON_RESP);
91 resp->message = tr_new_name(msg);
92 resp->payload = payload;
93 talloc_set_destructor((void *)resp, mon_resp_destructor);
94 if (resp->message == NULL) {
95 talloc_free(resp); // destructor will be called
103 * Free a monitoring response
105 * @param resp request to free, must not be null
107 void mon_resp_free(MON_RESP *resp)