Merge pull request #61 from painless-security/jennifer/request_id
[trust_router.git] / mon / mon_req.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 <gmodule.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_req_destructor(void *object)
47 {
48   MON_REQ *req = talloc_get_type_abort(object, MON_REQ);
49   if (req->options) {
50     g_array_unref(req->options);
51   }
52   return 0;
53 }
54
55 /**
56  * Allocate a new monitoring request
57  *
58  * @param mem_ctx talloc context for the new request
59  * @param cmd command for the request
60  * @return newly allocated request, or null on error
61  */
62 MON_REQ *mon_req_new(TALLOC_CTX *mem_ctx, MON_CMD cmd)
63 {
64   MON_REQ *req=talloc(mem_ctx, MON_REQ);
65   if (req) {
66     req->command = cmd;
67     req->options = g_array_new(FALSE, FALSE, sizeof(MON_OPT));
68     talloc_set_destructor((void *)req, mon_req_destructor);
69   }
70   return req;
71 }
72
73 /**
74  * Free a monitoring request
75  *
76  * @param req request to free, must not be null
77  */
78 void mon_req_free(MON_REQ *req)
79 {
80   talloc_free(req);
81 }
82
83 /**
84  * Add an option to a MON_REQ
85  * @param req request to operate on, not null
86  * @param opt_type type of option
87  * @return MON_SUCCESS on success, error code on error
88  */
89 MON_RC mon_req_add_option(MON_REQ *req, MON_OPT_TYPE opt_type)
90 {
91   MON_OPT new_opt; // not a pointer
92
93   /* Validate parameters */
94   if ((req == NULL) || (opt_type == OPT_TYPE_UNKNOWN)) {
95     return MON_BADARG;
96   }
97
98   new_opt.type = opt_type;
99
100   /* Add the new option to the list */
101   g_array_append_val(req->options, new_opt);
102   return MON_SUCCESS;
103 }
104
105 size_t mon_req_opt_count(MON_REQ *req)
106 {
107   return req->options->len;
108 }
109
110 MON_OPT *mon_req_opt_index(MON_REQ *req, size_t index)
111 {
112   MON_OPT *result = &g_array_index(req->options, MON_OPT, index);
113   return result;
114 }
115