Merge pull request #49 from painless-security/jennifer/mon_msg_encoders
[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  * 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
72  * calling this.
73  *
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
80  */
81 MON_RESP *mon_resp_new(TALLOC_CTX *mem_ctx,
82                           MON_REQ *req,
83                           MON_RESP_CODE code,
84                           const char *msg,
85                           json_t *payload)
86 {
87   MON_RESP *resp = talloc(mem_ctx, MON_RESP);
88   if (resp) {
89     resp->req = req;
90     resp->code = code;
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
96       resp = NULL;
97     }
98   }
99   return resp;
100 }
101
102 /**
103  * Free a monitoring response
104  *
105  * @param resp request to free, must not be null
106  */
107 void mon_resp_free(MON_RESP *resp)
108 {
109   talloc_free(resp);
110 }