1 /* Copyright 2022 IGEL Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "lease-config.h"
24 #define CONFIG_ERROR(x, ...) ERROR_LOG("%s: " x, filename, ##__VA_ARGS__)
26 static bool populate_connector_names(struct lease_config *config,
29 int cnames = toml_array_nelem(conns);
30 config->connector_names = calloc(cnames, sizeof(char *));
31 if (!config->connector_names) {
32 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
36 config->cnames = cnames;
38 for (int i = 0; i < config->cnames; i++) {
39 toml_datum_t conn = toml_string_at(conns, i);
43 config->connector_names[i] = conn.u.s;
48 int parse_config(char *filename, struct lease_config **parsed_config)
50 struct lease_config *config = NULL;
51 int nconfigs, i, ret = 0;
52 char parse_error[160];
54 FILE *fp = fopen(filename, "r");
58 toml_table_t *t_config =
59 toml_parse_file(fp, parse_error, sizeof parse_error);
61 CONFIG_ERROR("configuration file parse error: %s\n",
67 toml_array_t *leases = toml_array_in(t_config, "lease");
70 "Invalid config - cannot find any 'lease' configs");
73 nconfigs = toml_array_nelem(leases);
74 config = calloc(nconfigs, sizeof *config);
77 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
81 for (i = 0; i < toml_array_nelem(leases); i++) {
82 toml_table_t *lease = toml_table_at(leases, i);
84 toml_datum_t name = toml_string_in(lease, "name");
86 CONFIG_ERROR("Invalid lease name in entry #%d\n", i);
90 config[i].lease_name = name.u.s;
92 toml_array_t *conns = toml_array_in(lease, "connectors");
93 if (conns && !populate_connector_names(&config[i], conns)) {
94 CONFIG_ERROR("Non string connector name in lease: %s\n",
95 config[i].lease_name);
100 *parsed_config = config;
107 release_config(i, config);
111 void release_config(int num_leases, struct lease_config *config)
113 for (int i = 0; i < num_leases; i++) {
114 struct lease_config *c = &config[i];
116 for (int j = 0; j < c->cnames; j++)
117 free(c->connector_names[j]);
118 free(c->connector_names);