2 * Copyright (c) 2012-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.
38 #include <tr_config.h>
39 #include <tr_cfgwatch.h>
42 * Parse an unsigned integer
44 * If the key does not exist in the src object, returns success but does fill in *dest.
46 * @param src JSON object to pull a value from
47 * @param key key to pull
48 * @param dest (output) pointer to an allocated unsigned integer
49 * @return TR_CFG_SUCCESS or an error code
51 static TR_CFG_RC tr_cfg_parse_unsigned(json_t *src, const char *key, unsigned int *dest)
55 /* Validate parameters */
56 if ((src == NULL) || (key == NULL) || (dest == NULL))
57 return TR_CFG_BAD_PARAMS;
59 /* See if we have a value for this key; do nothing if not */
60 jtmp = json_object_get(src, key);
62 if (json_is_number(jtmp)) {
63 *dest = (unsigned int) json_integer_value(jtmp);
65 tr_debug("tr_cfg_parse_unsigned: Parsing error, %s is not a number.", key);
66 return TR_CFG_NOPARSE;
70 return TR_CFG_SUCCESS;
76 * If the key does not exist in the src object, returns success but does not allocate
77 * a return value in dest. Nulls the destination pointer if there is no return value.
79 * Return value is allocated in talloc's NULL context and must be freed with talloc_free()
80 * or put into a non-NULL context with talloc_steal()
82 * @param src JSON object to pull a value from
83 * @param key key to pull
84 * @param dest (output) pointer to a pointer that will hold the newly allocated return value
85 * @return TR_CFG_SUCCESS or an error code
87 static TR_CFG_RC tr_cfg_parse_string(json_t *src, const char *key, const char **dest)
91 /* Validate parameters */
92 if ((src == NULL) || (key == NULL) || (dest == NULL))
93 return TR_CFG_BAD_PARAMS;
95 /* See if we have a value for this key; do nothing if not */
96 jtmp = json_object_get(src, key);
98 *dest = NULL; /* No return value, null this out */
100 if (json_is_string(jtmp)) {
101 *dest = talloc_strdup(NULL, json_string_value(jtmp));
103 tr_debug("tr_cfg_parse_string: Parsing error, %s is not a string.", key);
104 return TR_CFG_NOPARSE;
108 return TR_CFG_SUCCESS;
112 * Set default values for settings that have them
114 * @param cfg configuration structure to fill in, not null
116 static void set_defaults(TR_CFG_INTERNAL *cfg)
118 cfg->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
119 cfg->tids_port = TR_DEFAULT_TIDS_PORT;
120 cfg->trps_port = TR_DEFAULT_TRPS_PORT;
121 cfg->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
122 cfg->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
123 cfg->trp_connect_interval = TR_DEFAULT_TRP_CONNECT_INTERVAL;
124 cfg->trp_sweep_interval = TR_DEFAULT_TRP_SWEEP_INTERVAL;
125 cfg->trp_update_interval = TR_DEFAULT_TRP_UPDATE_INTERVAL;
126 cfg->tid_req_timeout = TR_DEFAULT_TID_REQ_TIMEOUT;
127 cfg->tid_resp_numer = TR_DEFAULT_TID_RESP_NUMER;
128 cfg->tid_resp_denom = TR_DEFAULT_TID_RESP_DENOM;
129 cfg->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
130 cfg->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
133 /* Helper that checks return value of a parse fn and returns if it failed */
134 #define NOPARSE_UNLESS(x) \
136 if ((x) != TR_CFG_SUCCESS) \
137 return TR_CFG_NOPARSE; \
141 * Parse internal configuration JSON
143 * @param trc configuration structure to fill in
144 * @param jint internal configuration JSON object
145 * @return TR_CFG_SUCCESS or an error code
147 TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jint)
150 const char *s = NULL;
152 if ((!trc) || (!jint))
153 return TR_CFG_BAD_PARAMS;
155 /* If we don't yet have an internal config, allocate one and set defaults. If it
156 * already exists, do not disturb existing settings. */
157 if (NULL == trc->internal) {
158 if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
160 set_defaults(trc->internal); /* Install defaults for any unspecified settings */
163 NOPARSE_UNLESS(tr_cfg_parse_string(jint, "hostname", &(trc->internal->hostname)));
164 talloc_steal(trc->internal, trc->internal->hostname);
166 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "max_tree_depth", &(trc->internal->max_tree_depth)));
167 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tids_port", &(trc->internal->tids_port)));
168 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trps_port", &(trc->internal->trps_port)));
169 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "cfg_poll_interval", &(trc->internal->cfg_poll_interval)));
170 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "cfg_settling_time", &(trc->internal->cfg_settling_time)));
171 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_connect_interval", &(trc->internal->trp_connect_interval)));
172 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_sweep_interval", &(trc->internal->trp_sweep_interval)));
173 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_update_interval", &(trc->internal->trp_update_interval)));
174 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_request_timeout", &(trc->internal->tid_req_timeout)));
175 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_response_numerator", &(trc->internal->tid_resp_numer)));
176 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_response_denominator", &(trc->internal->tid_resp_denom)));
178 /* Parse the logging section */
179 if (NULL != (jtmp = json_object_get(jint, "logging"))) {
180 NOPARSE_UNLESS(tr_cfg_parse_string(jtmp, "log_threshold", &s));
182 trc->internal->log_threshold = str2sev(s);
183 talloc_free((void *) s);
186 NOPARSE_UNLESS(tr_cfg_parse_string(jtmp, "console_threshold", &s));
188 trc->internal->console_threshold = str2sev(s);
189 talloc_free((void *) s);
193 /* Parse the monitoring section */
194 if (NULL != (jtmp = json_object_get(jint, "monitoring"))) {
195 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jtmp, "port", &(trc->internal->monitoring_port)));
198 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
199 return TR_CFG_SUCCESS;