3adb2a30b554a5f4026edfdc557e6ecffea3f1e3
[trust_router.git] / mon / tr_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 #include <string.h>
39
40 #include "tr_mon_req.h"
41
42 // Monitoring request message common code
43
44 /**
45  * Destructor used by talloc to ensure proper cleanup
46  */
47 static int tr_mon_req_destructor(void *object)
48 {
49   TR_MON_REQ *req = talloc_get_type_abort(object, TR_MON_REQ);
50   if (req->options) {
51     g_array_unref(req->options);
52   }
53   return 0;
54 }
55
56 /**
57  * Allocate a new monitoring request
58  *
59  * @param mem_ctx talloc context for the new request
60  * @param cmd command for the request
61  * @return newly allocated request, or null on error
62  */
63 TR_MON_REQ *tr_mon_req_new(TALLOC_CTX *mem_ctx, TR_MON_CMD cmd)
64 {
65   TR_MON_REQ *req=talloc(mem_ctx, TR_MON_REQ);
66   if (req) {
67     req->command = cmd;
68     req->options = g_array_new(FALSE, FALSE, sizeof(TR_MON_OPT));
69     talloc_set_destructor((void *)req, tr_mon_req_destructor);
70   }
71   return req;
72 }
73
74 /**
75  * Free a monitoring request
76  *
77  * @param req request to free, must not be null
78  */
79 void tr_mon_req_free(TR_MON_REQ *req)
80 {
81   talloc_free(req);
82 }
83
84 /**
85  * Add an option to a TR_MON_REQ
86  * @param req request to operate on, not null
87  * @param opt_type type of option
88  * @return TR_MON_SUCCESS on success, error code on error
89  */
90 TR_MON_RC tr_mon_req_add_option(TR_MON_REQ *req, TR_MON_OPT_TYPE opt_type)
91 {
92   TR_MON_OPT new_opt; // not a pointer
93
94   /* Validate parameters */
95   if ((req == NULL) || (opt_type == OPT_TYPE_UNKNOWN)) {
96     return TR_MON_BADARG;
97   }
98
99   new_opt.type = opt_type;
100
101   /* Add the new option to the list */
102   g_array_append_val(req->options, new_opt);
103   return TR_MON_SUCCESS;
104 }
105
106 size_t tr_mon_req_opt_count(TR_MON_REQ *req)
107 {
108   return req->options->len;
109 }
110
111 TR_MON_OPT *tr_mon_req_opt_index(TR_MON_REQ *req, size_t index)
112 {
113   TR_MON_OPT *result = (TR_MON_OPT *) &g_array_index(req->options, TR_MON_OPT, index);
114   return result;
115 }
116
117 /**
118  * This method defines the command strings
119  */
120 const char *cmd_to_string(TR_MON_CMD cmd)
121 {
122   switch(cmd) {
123     case MON_CMD_UNKNOWN:
124       return NULL;
125
126     case MON_CMD_RECONFIGURE:
127       return "reconfigure";
128
129     case MON_CMD_SHOW:
130       return "show";
131   }
132   return NULL;
133 }
134
135 // Helper macro for the cmd_from_string method
136 #define return_if_matches(s, cmd)            \
137   do {                                       \
138     if (strcmp((s), cmd_to_string(cmd))==0)  \
139       return (cmd);                          \
140   } while(0)
141
142 TR_MON_CMD cmd_from_string(const char *s)
143 {
144   return_if_matches(s, MON_CMD_RECONFIGURE);
145   return_if_matches(s, MON_CMD_SHOW);
146   return MON_CMD_UNKNOWN;
147 }
148 #undef return_if_matches
149
150 /**
151  * This method defines the option type strings
152  */
153 const char *opt_type_to_string(TR_MON_OPT_TYPE opt_type)
154 {
155   switch(opt_type) {
156     case OPT_TYPE_UNKNOWN:
157       return NULL;
158
159     case OPT_TYPE_SHOW_VERSION:
160       return "version";
161
162     case OPT_TYPE_SHOW_SERIAL:
163       return "serial";
164
165     case OPT_TYPE_SHOW_UPTIME:
166       return "uptime";
167
168     case OPT_TYPE_SHOW_TID_REQ_COUNT:
169       return "tid_req_count";
170
171     case OPT_TYPE_SHOW_TID_REQ_PENDING:
172       return "tid_req_pending";
173
174     case OPT_TYPE_SHOW_ROUTES:
175       return "routes";
176
177     case OPT_TYPE_SHOW_COMMUNITIES:
178       return "communities";
179   }
180   return NULL;
181 }
182
183 // Helper macro for the opt_type_from_string method
184 #define return_if_matches(s, cmd)                 \
185   do {                                            \
186     if (strcmp((s), opt_type_to_string(cmd))==0)  \
187       return (cmd);                               \
188   } while(0)
189
190 TR_MON_OPT_TYPE opt_type_from_string(const char *s)
191 {
192   return_if_matches(s, OPT_TYPE_SHOW_VERSION);
193   return_if_matches(s, OPT_TYPE_SHOW_SERIAL);
194   return_if_matches(s, OPT_TYPE_SHOW_UPTIME);
195   return_if_matches(s, OPT_TYPE_SHOW_TID_REQ_COUNT);
196   return_if_matches(s, OPT_TYPE_SHOW_TID_REQ_PENDING);
197   return_if_matches(s, OPT_TYPE_SHOW_ROUTES);
198   return_if_matches(s, OPT_TYPE_SHOW_COMMUNITIES);
199   return OPT_TYPE_UNKNOWN;
200 }
201 #undef return_if_matches