Debian Codegraph @

tools/yamlparse.c

raw symbols SCIP
1 /* librist. Copyright © 2024 SipRadius LLC. All right reserved.
2 * Author: Sergio Ammirata, Ph.D. <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdbool.h>
11 #include <yamlparse.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include "headers.h"
15 #include <librist/peer.h>
16 #include "logging.h"
17
18 #ifndef strndup
19 char *strndup (const char *str, size_t max)
20 {
21 size_t len = strnlen (str, max);
22 char *res = malloc (len + 1);
23 if (res)
24 {
25 memcpy (res, str, len);
26 res[len] = '\0';
27 }
28 return res;
29 }
30 #endif
31
32 size_t rist_findchar(const char* str, int c, size_t max) {
33 size_t position = 0;
34 size_t i = 0;
35 for(i = 0; ;i++) {
36 if((unsigned char) str[i] == c) {
37 position = i;
38 break;
39 }
40 if (str[i]=='\0') break;
41 if (i >= (max-1)) break;
42 }
43 return position;
44 }
45
46 yaml_node *parse_yaml_value(char *str, size_t *index, size_t *max) {
47 yaml_node *node = malloc(sizeof(yaml_node));
48 size_t start_pos = 0;
49 node->key = NULL;
50 node->value = NULL;
51 node->next = NULL;
52
53 // First remove all leading spaces
54 while (isspace(str[*index + start_pos])) (start_pos)++;
55
56 // Find next \n
57 size_t cr_pos = rist_findchar(&str[*index + start_pos], '\n', *max - *index);
58
59 if (cr_pos == 0)
60 {
61 *index = *max;
62 return NULL;
63 }
64
65 // Find the first space in this line (if any)
66 size_t space_pos = rist_findchar(&str[*index + start_pos], ' ', cr_pos);
67
68 if (space_pos == 0 && str[*index + start_pos + cr_pos - 1] == ':') // Key only
69 {
70 node->key = strndup(str + *index + start_pos, cr_pos - 1);
71 //printf("Key =%s=\n", node->key);
72 }
73 else if (space_pos > 0 && space_pos < cr_pos && str[*index + start_pos + space_pos - 1] == ':') // Key Value pairs
74 {
75 node->key = strndup(str + *index + start_pos, space_pos - 1);
76 // Remove any additional spaces before the value (bounded at end of line)
77 while (space_pos < cr_pos && isspace(str[*index + start_pos + space_pos])) (space_pos)++;
78 node->value = strndup(str + *index + start_pos + space_pos, cr_pos - space_pos);
79 //printf("Key =%s= Value =%s=\n", node->key, node->value);
80 }
81 else // Value only
82 {
83 // If this is a list, remove the dash and space from it
84 size_t skip_chars = 0;
85 if (str[*index + start_pos] == '-' && cr_pos >= 2)
86 skip_chars = 2;
87 node->value = strndup(str + *index + start_pos + skip_chars, cr_pos - skip_chars);
88 //printf("value =%s=\n", node->value);
89 }
90
91 *index += cr_pos + start_pos;
92
93 return node;
94 }
95
96 yaml_node *parse_yaml_string(char *yaml_str) {
97 size_t index = 0;
98 yaml_node *root = NULL, *current = NULL;
99 size_t max = strlen(yaml_str);
100
101 while (index < max) {
102 yaml_node *new_node = parse_yaml_value(yaml_str, &index, &max);
103 if (!root) {
104 root = new_node;
105 current = root;
106 } else {
107 current->next = new_node;
108 current = new_node;
109 }
110 }
111
112 return root;
113 }
114
115 void print_yaml_node(yaml_node *node) {
116 if (node->key && node->value) {
117 /* Never echo secrets into terminal/log output */
118 if (strcmp(node->key, "secret") == 0)
119 printf("%s: (redacted)\n", node->key);
120 else
121 printf("%s: %s\n", node->key, node->value);
122 } else if (node->key) {
123 printf("%s:\n", node->key);
124 } else {
125 printf("%s\n", node->value);
126 }
127 if (node->next) {
128 print_yaml_node(node->next);
129 }
130 }
131
132 void parse_config_file(rist_tools_config_object *config, char *current_key, yaml_node *node) {
133 if (node->key && !node->value) {
134 if (current_key)
135 free(current_key);
136 current_key = strdup((char *) node->key);
137 //printf("key node -> %s\n", node->key);
138 } else if (node->key) {
139 //printf("key/value node -> %s %s\n", node->key, node->value);
140 current_key = strdup((char *) node->key);
141 if (strcmp(current_key,"buffer") == 0) config->buffer=atoi((char *) node->value);
142 else if (strcmp(current_key,"encryption-type") == 0) config->encryption_type=atoi((char *) node->value);
143 else if (strcmp(current_key,"stats") == 0) config->stats_interval=atoi((char *) node->value);
144 else if (strcmp(current_key,"verbose-level") == 0) config->verbose_level=atoi((char *) node->value);
145 else if (strcmp(current_key,"profile") == 0) {
146 if (strcmp((char *) node->value,"main") == 0) config->profile=1;
147 else if (strcmp((char *) node->value,"advanced") == 0) config->profile=2;
148 else if (strcmp((char *) node->value,"simple") == 0) config->profile=0;
149 else config->profile=atoi((char *) node->value);
150 }
151 else if (strcmp(current_key,"remote-logging") == 0) strapp(&config->remote_log_address,(char *) node->value);
152 else if (strcmp(current_key,"secret") == 0) strapp(&config->secret,(char *) node->value);
153 else if (strcmp(current_key,"null-packet-deletion") == 0) config->null_packet_deletion=atoi((char *) node->value);
154 else if (strcmp(current_key,"fast-start") == 0) config->fast_start=atoi((char *) node->value);
155 else if (strcmp(current_key,"tun-mode") == 0) config->tun_mode=atoi((char *) node->value);
156 else if (strcmp(current_key,"tun") == 0) strapp(&config->tunnel_interface,(char *) node->value);
157 #ifdef HAVE_SRP_SUPPORT
158 else if (strcmp(current_key,"srpfile") == 0) strapp(&config->srp_file,(char *) node->value);
159 #endif
160 #ifdef HAVE_PROMETHEUS_SUPPORT
161 else if (strcmp(current_key,"enable-metrics") == 0) config->enable_metrics=atoi((char *) node->value);
162 else if (strcmp(current_key,"metrics-tags") == 0) strapp(&config->metrics_tags,(char *) node->value);
163 else if (strcmp(current_key,"metrics-multipoint") == 0) config->metrics_multipoint=atoi((char *) node->value);
164 else if (strcmp(current_key,"metrics-nocreated") == 0) config->metrics_nocreated=atoi((char *) node->value);
165 #ifdef HAVE_LIBMICROHTTPD
166 else if (strcmp(current_key,"metrics-http") == 0) config->metrics_http=atoi((char *) node->value);
167 else if (strcmp(current_key,"metrics-port") == 0) config->metrics_port=atoi((char *) node->value);
168 else if (strcmp(current_key,"metrics-ip") == 0) strapp(&config->metrics_ip,(char *) node->value);
169 #endif
170 #if HAVE_SOCK_UN_H
171 else if (strcmp(current_key,"metrics-unix") == 0) strapp(&config->metrics_unix,(char *) node->value);
172 #endif
173 #endif
174 }
175 else if (current_key)
176 {
177 if (strcmp(current_key,"inputurl") == 0)
178 {
179 strapp(&config->input_url,(char *) node->value);
180 strapp(&config->input_url,",");
181 }
182 else if (strcmp(current_key,"outputurl") == 0)
183 {
184 strapp(&config->output_url,(char *) node->value);
185 strapp(&config->output_url,",");
186 }
187 //printf("value node -> %s\n", node->value);
188 }
189 if (node->key)
190 free(node->key);
191 if (node->value)
192 free(node->value);
193
194 if (node->next) {
195 parse_config_file(config, current_key, node->next);
196 free(node);
197 }
198 else
199 {
200 // Remove trailing commas from aggregate URLs
201 if (strlen(config->input_url) > 0) config->input_url[strlen(config->input_url)-1]=0;
202 if (strlen(config->output_url) > 0) config->output_url[strlen(config->output_url)-1]=0;
203 if (current_key)
204 free(current_key);
205 free(node);
206 }
207 }
208
209 // Append to an exisitng string
210 void strapp(char ** original, char * newstr){
211 if (*original){
212 char * appended = malloc((strlen(newstr)+strlen(*original)+1)*sizeof(char));
213 strcpy(appended,*original);
214 strcat(appended,newstr);
215 free(*original);
216 *original = appended;
217 } else {
218 *original = strdup(newstr);
219 }
220 }
221
222 // Function to parse yaml config into a rist_tools_config_object
223 rist_tools_config_object *parse_yaml(char * file){
224
225 /*
226 secret: blarg
227 buffer: 0
228 encryption-type: 256
229 profile: <simple/main/advanced/#>
230 stats: 1000
231 inputurl:
232 - <URL A>
233 - <URL B>
234 - ...
235 outputurl:
236 - <URL A>
237 - <URL B>
238 - ...
239 */
240
241 rist_tools_config_object *config = NULL;
242
243 // Open yaml file
244 FILE *f = fopen(file,"r");
245 if (f == NULL) return config;
246
247 // Initialize rist_tools_config_object (non-zero values)
248 config = calloc(1, sizeof(rist_tools_config_object));
249 config->profile = RIST_DEFAULT_PROFILE;
250 config->stats_interval = 1000;
251 config->verbose_level = RIST_LOG_INFO;
252 #ifdef HAVE_PROMETHEUS_SUPPORT
253 #ifdef HAVE_LIBMICROHTTPD
254 config->metrics_port = 1968;
255 #endif
256 #endif
257
258 char *current_key = NULL;
259 fseek(f, 0, SEEK_END);
260 long fsize = ftell(f);
261 fseek(f, 0, SEEK_SET); /* same as rewind(f); */
262 if (fsize < 0) {
263 fclose(f);
264 return config;
265 }
266 char *yaml_str = malloc((size_t)fsize + 1);
267 if (!yaml_str) {
268 fclose(f);
269 return config;
270 }
271 size_t got = fread(yaml_str, 1, (size_t)fsize, f);
272 yaml_str[got] = '\0';
273 yaml_node *root = parse_yaml_string(yaml_str);
274 // print entire config file (debug)
275 if (root)
276 print_yaml_node(root);
277 // transfer the data to the config structure
278 if (root)
279 parse_config_file(config, current_key, root);
280 free(yaml_str);
281 fclose(f);
282 return config;
283 }
284
285 void cleanup_tools_config(rist_tools_config_object * config)
286 {
287 if (!config)
288 return;
289 if (config->remote_log_address)
290 free(config->remote_log_address);
291 if (config->secret)
292 free(config->secret);
293 if (config->input_url)
294 free(config->input_url);
295 if (config->output_url)
296 free(config->output_url);
297 if (config->tunnel_interface)
298 free(config->tunnel_interface);
299 #ifdef HAVE_SRP_SUPPORT
300 if (config->srp_file)
301 free(config->srp_file);
302 #endif
303 #ifdef HAVE_PROMETHEUS_SUPPORT
304 if (config->metrics_tags)
305 free(config->metrics_tags);
306 #ifdef HAVE_LIBMICROHTTPD
307 if (config->metrics_ip)
308 free(config->metrics_ip);
309 #endif
310 #if HAVE_SOCK_UN_H
311 if (config->metrics_unix)
312 free(config->metrics_unix);
313 #endif
314 #endif
315 free(config);
316 }