/** * Parse internal configuration JSON * * @param trc configuration structure to fill in * @param jint internal configuration JSON object * @return TR_CFG_SUCCESS or an error code */ TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jint) { json_t *jtmp = NULL; const char *s = NULL; if ((!trc) || (!jint)) return TR_CFG_BAD_PARAMS; /* If we don't yet have an internal config, allocate one and set defaults. If it * already exists, do not disturb existing settings. */ if (NULL == trc->internal) { if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL))) return TR_CFG_NOMEM; set_defaults(trc->internal); /* Install defaults for any unspecified settings */ } const char *allowed_keys[] = {"hostname", "cfg_poll_interval", "cfg_settling_time", "logging", "tid_protocol", "tr_protocol", "monitoring", "tids_port", 0}; const char *obsolete_keys[] = {"tids_port", "trps_port", "trp_connect_interval", "trp_sweep_interval", "trp_update_interval", "tid_request_timeout", "tid_response_numerator", "tid_response_denominator", 0}; if (!check_allowed_keys(jint, allowed_keys, obsolete_keys)) return TR_CFG_NOPARSE; /* Parse the main section */ NOPARSE_UNLESS(tr_cfg_parse_string(jint, "hostname", &(trc->internal->hostname))); talloc_steal(trc->internal, trc->internal->hostname); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "cfg_poll_interval", &(trc->internal->cfg_poll_interval))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "cfg_settling_time", &(trc->internal->cfg_settling_time))); NOPARSE_UNLESS(tr_cfg_parse_integer(jint, "tids_port", &(trc->internal->tids_port))); NOPARSE_UNLESS(tr_cfg_parse_integer(jint, "trps_port", &(trc->internal->trps_port))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_connect_interval", &(trc->internal->trp_connect_interval))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_sweep_interval", &(trc->internal->trp_sweep_interval))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_update_interval", &(trc->internal->trp_update_interval))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_request_timeout", &(trc->internal->tid_req_timeout))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_response_numerator", &(trc->internal->tid_resp_numer))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_response_denominator", &(trc->internal->tid_resp_denom))); /* Parse the logging section */ if (NULL != (jtmp = json_object_get(jint, "logging"))) { const char *allowed_keys[] = {"log_threshold", "console_threshold", 0}; if (!check_allowed_keys(jtmp, allowed_keys, NULL)) return TR_CFG_NOPARSE; NOPARSE_UNLESS(tr_cfg_parse_string(jtmp, "log_threshold", &s)); if (s) { trc->internal->log_threshold = str2sev(s); talloc_free((void *) s); } NOPARSE_UNLESS(tr_cfg_parse_string(jtmp, "console_threshold", &s)); if (s) { trc->internal->console_threshold = str2sev(s); talloc_free((void *) s); } } /* Parse the monitoring section */ if (NULL != (jtmp = json_object_get(jint, "monitoring"))) { NOPARSE_UNLESS(tr_cfg_parse_monitoring(trc, jtmp)); } /* Parse the tid_protocol section */ if (NULL != (jtmp = json_object_get(jint, "tid_protocol"))) { const char *allowed_keys[] = {"port", "request_timeout", "response_numerator", "response_denominator", 0}; if (!check_allowed_keys(jtmp, allowed_keys, NULL)) return TR_CFG_NOPARSE; NOPARSE_UNLESS(tr_cfg_parse_integer(jtmp, "port", &(trc->internal->tids_port))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jtmp, "request_timeout", &(trc->internal->tid_req_timeout))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jtmp, "response_numerator", &(trc->internal->tid_resp_numer))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jtmp, "response_denominator", &(trc->internal->tid_resp_denom))); } /* Parse the tr_protocol section */ if (NULL != (jtmp = json_object_get(jint, "tr_protocol"))) { const char *allowed_keys[] = {"port", "connect_interval", "sweep_interval", "update_interval", 0}; if (!check_allowed_keys(jtmp, allowed_keys, NULL)) return TR_CFG_NOPARSE; NOPARSE_UNLESS(tr_cfg_parse_integer(jtmp, "port", &(trc->internal->trps_port))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jtmp, "connect_interval", &(trc->internal->trp_connect_interval))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jtmp, "sweep_interval", &(trc->internal->trp_sweep_interval))); NOPARSE_UNLESS(tr_cfg_parse_unsigned(jtmp, "update_interval", &(trc->internal->trp_update_interval))); } tr_debug("tr_cfg_parse_internal: Internal config parsed."); return TR_CFG_SUCCESS; }
static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) { json_t *jint = NULL; json_t *jmtd = NULL; json_t *jtp = NULL; json_t *jhname = NULL; json_t *jlog = NULL; json_t *jconthres = NULL; json_t *jlogthres = NULL; if ((!trc) || (!jcfg)) return TR_CFG_BAD_PARAMS; if (NULL == trc->internal) { if (NULL == (trc->internal = talloc(trc, TR_CFG_INTERNAL))) return TR_CFG_NOMEM; memset(trc->internal, 0, sizeof(TR_CFG_INTERNAL)); } if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) { if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) { if (json_is_number(jmtd)) { trc->internal->max_tree_depth = json_integer_value(jmtd); } else { tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number."); return TR_CFG_NOPARSE; } } else { /* If not configured, use the default */ trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH; } if (NULL != (jtp = json_object_get(jint, "tids_port"))) { if (json_is_number(jtp)) { trc->internal->tids_port = json_integer_value(jtp); } else { tr_debug("tr_cfg_parse_internal: Parsing error, port is not a number."); return TR_CFG_NOPARSE; } } else { /* If not configured, use the default */ trc->internal->tids_port = TR_DEFAULT_TIDS_PORT; } if (NULL != (jhname = json_object_get(jint, "hostname"))) { if (json_is_string(jhname)) { trc->internal->hostname = json_string_value(jhname); } else { tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string."); return TR_CFG_NOPARSE; } } if (NULL != (jlog = json_object_get(jint, "logging"))) { if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) { if (json_is_string(jlogthres)) { trc->internal->log_threshold = str2sev(json_string_value(jlogthres)); } else { tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string."); return TR_CFG_NOPARSE; } } else { /* If not configured, use the default */ trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD; } if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) { if (json_is_string(jconthres)) { trc->internal->console_threshold = str2sev(json_string_value(jconthres)); } else { tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string."); return TR_CFG_NOPARSE; } } else { /* If not configured, use the default */ trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD; } } else { /* If not configured, use the default */ trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD; trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD; } tr_debug("tr_cfg_parse_internal: Internal config parsed."); return TR_CFG_SUCCESS; } return TR_CFG_SUCCESS; }