|
| 1 | +/** |
| 2 | + * \file src/plugins/input/dummy/config.c |
| 3 | + * \author Lukas Hutak <[email protected]> |
| 4 | + * \brief Example parser of an XML configuration (source file) |
| 5 | + * \date 2018 |
| 6 | + */ |
| 7 | + |
| 8 | +/* Copyright (C) 2018 CESNET, z.s.p.o. |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions |
| 12 | + * are met: |
| 13 | + * 1. Redistributions of source code must retain the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer in |
| 17 | + * the documentation and/or other materials provided with the |
| 18 | + * distribution. |
| 19 | + * 3. Neither the name of the Company nor the names of its contributors |
| 20 | + * may be used to endorse or promote products derived from this |
| 21 | + * software without specific prior written permission. |
| 22 | + * |
| 23 | + * ALTERNATIVELY, provided that this notice is retained in full, this |
| 24 | + * product may be distributed under the terms of the GNU General Public |
| 25 | + * License (GPL) version 2 or later, in which case the provisions |
| 26 | + * of the GPL apply INSTEAD OF those given above. |
| 27 | + * |
| 28 | + * This software is provided ``as is'', and any express or implied |
| 29 | + * warranties, including, but not limited to, the implied warranties of |
| 30 | + * merchantability and fitness for a particular purpose are disclaimed. |
| 31 | + * In no event shall the company or contributors be liable for any |
| 32 | + * direct, indirect, incidental, special, exemplary, or consequential |
| 33 | + * damages (including, but not limited to, procurement of substitute |
| 34 | + * goods or services; loss of use, data, or profits; or business |
| 35 | + * interruption) however caused and on any theory of liability, whether |
| 36 | + * in contract, strict liability, or tort (including negligence or |
| 37 | + * otherwise) arising in any way out of the use of this software, even |
| 38 | + * if advised of the possibility of such damage. |
| 39 | + * |
| 40 | + */ |
| 41 | + |
| 42 | +#include <stdlib.h> |
| 43 | +#include <strings.h> |
| 44 | +#include <limits.h> |
| 45 | +#include "config.h" |
| 46 | + |
| 47 | +/* |
| 48 | + * <params> |
| 49 | + * <odid>...</odid> <!-- optional --> |
| 50 | + * <delay>...</delay> <!-- in microseconds --> |
| 51 | + * <failAfter>...</failAfter> <!-- number of messages before --> |
| 52 | + * <failType>...</failType> <!-- return code of the module --> |
| 53 | + * </params> |
| 54 | + */ |
| 55 | + |
| 56 | +/** XML nodes */ |
| 57 | +enum params_xml_nodes { |
| 58 | + NODE_ODID = 1, |
| 59 | + NODE_DELAY, |
| 60 | + NODE_FAIL_AFTER, |
| 61 | + NODE_FAIL_TYPE |
| 62 | +}; |
| 63 | + |
| 64 | +/** Definition of the \<params\> node */ |
| 65 | +static const struct fds_xml_args args_params[] = { |
| 66 | + FDS_OPTS_ROOT("params"), |
| 67 | + FDS_OPTS_ELEM(NODE_ODID, "odid", FDS_OPTS_T_UINT, FDS_OPTS_P_OPT), |
| 68 | + FDS_OPTS_ELEM(NODE_DELAY, "delay", FDS_OPTS_T_UINT, 0), |
| 69 | + FDS_OPTS_ELEM(NODE_FAIL_AFTER, "failAfter", FDS_OPTS_T_UINT, 0), |
| 70 | + FDS_OPTS_ELEM(NODE_FAIL_TYPE, "failType", FDS_OPTS_T_STRING, 0), |
| 71 | + FDS_OPTS_END |
| 72 | +}; |
| 73 | + |
| 74 | +/** |
| 75 | + * \brief Process \<params\> node |
| 76 | + * \param[in] ctx Plugin context |
| 77 | + * \param[in] root XML context to process |
| 78 | + * \param[in] cfg Parsed configuration |
| 79 | + * \return #IPX_OK on success |
| 80 | + * \return #IPX_ERR_FORMAT in case of failure |
| 81 | + */ |
| 82 | +static int |
| 83 | +config_parser_root(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct instance_config *cfg) |
| 84 | +{ |
| 85 | + const struct fds_xml_cont *content; |
| 86 | + while(fds_xml_next(root, &content) != FDS_EOC) { |
| 87 | + switch (content->id) { |
| 88 | + case NODE_ODID: |
| 89 | + // Observation Domain ID |
| 90 | + assert(content->type == FDS_OPTS_T_UINT); |
| 91 | + if (content->val_uint > UINT32_MAX) { |
| 92 | + IPX_CTX_ERROR(ctx, "The ODID value must be between 0 .. 2^32", '\0'); |
| 93 | + return IPX_ERR_FORMAT; |
| 94 | + } |
| 95 | + cfg->odid = (uint32_t) content->val_uint; |
| 96 | + break; |
| 97 | + case NODE_DELAY: |
| 98 | + // Delay between messages [microseconds] |
| 99 | + assert(content->type == FDS_OPTS_T_UINT); |
| 100 | + cfg->sleep_time.tv_nsec = (content->val_uint % 1000000LL) * 1000LL; |
| 101 | + cfg->sleep_time.tv_sec = content->val_uint / 1000000LL; |
| 102 | + break; |
| 103 | + case NODE_FAIL_AFTER: |
| 104 | + // Fail after generating specific number of messages |
| 105 | + assert(content->type == FDS_OPTS_T_UINT); |
| 106 | + cfg->fail_after = content->val_uint; |
| 107 | + break; |
| 108 | + case NODE_FAIL_TYPE: |
| 109 | + assert(content->type == FDS_OPTS_T_STRING); |
| 110 | + if (strcasecmp(content->ptr_string, "IPX_ERR_DENIED") == 0) { |
| 111 | + cfg->fail_type = IPX_ERR_DENIED; |
| 112 | + } else if (strcasecmp(content->ptr_string, "IPX_ERR_EOF") == 0) { |
| 113 | + cfg->fail_type = IPX_ERR_EOF; |
| 114 | + } else { |
| 115 | + IPX_CTX_ERROR(ctx, "Invalid failure type (expected: IPX_ERR_EOF/IPX_ERR_DENIED", '\0'); |
| 116 | + return IPX_ERR_FORMAT; |
| 117 | + } |
| 118 | + break; |
| 119 | + default: |
| 120 | + // Internal error |
| 121 | + assert(false); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return IPX_OK; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * \brief Set default parameters of the configuration |
| 130 | + * \param[in] cfg Configuration |
| 131 | + */ |
| 132 | +static void |
| 133 | +config_default_set(struct instance_config *cfg) |
| 134 | +{ |
| 135 | + cfg->sleep_time.tv_sec = 0; |
| 136 | + cfg->sleep_time.tv_nsec = 100000000LL; // 100ms between messages |
| 137 | + cfg->odid = 1; |
| 138 | + cfg->fail_type = IPX_ERR_EOF; |
| 139 | + cfg->fail_after = 0; |
| 140 | +} |
| 141 | + |
| 142 | +struct instance_config * |
| 143 | +config_parse(ipx_ctx_t *ctx, const char *params) |
| 144 | +{ |
| 145 | + struct instance_config *cfg = calloc(1, sizeof(*cfg)); |
| 146 | + if (!cfg) { |
| 147 | + IPX_CTX_ERROR(ctx, "Memory allocation error (%s:%d)", __FILE__, __LINE__); |
| 148 | + return NULL; |
| 149 | + } |
| 150 | + |
| 151 | + // Set default parameters |
| 152 | + config_default_set(cfg); |
| 153 | + |
| 154 | + // Create an XML parser |
| 155 | + fds_xml_t *parser = fds_xml_create(); |
| 156 | + if (!parser) { |
| 157 | + IPX_CTX_ERROR(ctx, "Memory allocation error (%s:%d)", __FILE__, __LINE__); |
| 158 | + free(cfg); |
| 159 | + return NULL; |
| 160 | + } |
| 161 | + |
| 162 | + if (fds_xml_set_args(parser, args_params) != IPX_OK) { |
| 163 | + IPX_CTX_ERROR(ctx, "Failed to parse the description of an XML document!", '\0'); |
| 164 | + fds_xml_destroy(parser); |
| 165 | + free(cfg); |
| 166 | + return NULL; |
| 167 | + } |
| 168 | + |
| 169 | + fds_xml_ctx_t *params_ctx = fds_xml_parse_mem(parser, params, true); |
| 170 | + if (params_ctx == NULL) { |
| 171 | + IPX_CTX_ERROR(ctx, "Failed to parse the configuration: %s", fds_xml_last_err(parser)); |
| 172 | + fds_xml_destroy(parser); |
| 173 | + free(cfg); |
| 174 | + return NULL; |
| 175 | + } |
| 176 | + |
| 177 | + // Parse parameters |
| 178 | + int rc = config_parser_root(ctx, params_ctx, cfg); |
| 179 | + fds_xml_destroy(parser); |
| 180 | + if (rc != IPX_OK) { |
| 181 | + free(cfg); |
| 182 | + return NULL; |
| 183 | + } |
| 184 | + |
| 185 | + return cfg; |
| 186 | +} |
| 187 | + |
| 188 | +void |
| 189 | +config_destroy(struct instance_config *cfg) |
| 190 | +{ |
| 191 | + free(cfg); |
| 192 | +} |
0 commit comments