| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 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 |
|
| 54 |
while (isspace(str[*index + start_pos])) (start_pos)++; |
| 55 |
|
| 56 |
|
| 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 |
|
| 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] == ':') |
| 69 |
{ |
| 70 |
node->key = strndup(str + *index + start_pos, cr_pos - 1); |
| 71 |
|
| 72 |
} |
| 73 |
else if (space_pos < cr_pos && str[*index + start_pos + space_pos - 1] == ':') |
| 74 |
{ |
| 75 |
node->key = strndup(str + *index + start_pos, space_pos - 1); |
| 76 |
|
| 77 |
while (isspace(str[*index + start_pos + space_pos])) (space_pos)++; |
| 78 |
node->value = strndup(str + *index + start_pos + space_pos, cr_pos - space_pos); |
| 79 |
|
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
|
| 84 |
size_t skip_chars = 0; |
| 85 |
if (str[*index + start_pos] == '-') |
| 86 |
skip_chars = 2; |
| 87 |
node->value = strndup(str + *index + start_pos + skip_chars, cr_pos - skip_chars); |
| 88 |
|
| 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 |
printf("%s: %s\n", node->key, node->value); |
| 118 |
} else if (node->key) { |
| 119 |
printf("%s:\n", node->key); |
| 120 |
} else { |
| 121 |
printf("%s\n", node->value); |
| 122 |
} |
| 123 |
if (node->next) { |
| 124 |
print_yaml_node(node->next); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void parse_config_file(rist_tools_config_object *config, char *current_key, yaml_node *node) { |
| 129 |
if (node->key && !node->value) { |
| 130 |
if (current_key) |
| 131 |
free(current_key); |
| 132 |
current_key = strdup((char *) node->key); |
| 133 |
|
| 134 |
} else if (node->key) { |
| 135 |
|
| 136 |
current_key = strdup((char *) node->key); |
| 137 |
if (strcmp(current_key,"buffer") == 0) config->buffer=atoi((char *) node->value); |
| 138 |
else if (strcmp(current_key,"encryption-type") == 0) config->encryption_type=atoi((char *) node->value); |
| 139 |
else if (strcmp(current_key,"stats") == 0) config->stats_interval=atoi((char *) node->value); |
| 140 |
else if (strcmp(current_key,"verbose-level") == 0) config->verbose_level=atoi((char *) node->value); |
| 141 |
else if (strcmp(current_key,"profile") == 0) { |
| 142 |
if (strcmp((char *) node->value,"main") == 0) config->profile=1; |
| 143 |
else if (strcmp((char *) node->value,"advanced") == 0) config->profile=2; |
| 144 |
else if (strcmp((char *) node->value,"simple") == 0) config->profile=0; |
| 145 |
else config->profile=atoi((char *) node->value); |
| 146 |
} |
| 147 |
else if (strcmp(current_key,"remote-logging") == 0) strapp(&config->remote_log_address,(char *) node->value); |
| 148 |
else if (strcmp(current_key,"secret") == 0) strapp(&config->secret,(char *) node->value); |
| 149 |
else if (strcmp(current_key,"null-packet-deletion") == 0) config->null_packet_deletion=atoi((char *) node->value); |
| 150 |
else if (strcmp(current_key,"fast-start") == 0) config->fast_start=atoi((char *) node->value); |
| 151 |
else if (strcmp(current_key,"tun-mode") == 0) config->tun_mode=atoi((char *) node->value); |
| 152 |
else if (strcmp(current_key,"tun") == 0) strapp(&config->tunnel_interface,(char *) node->value); |
| 153 |
#ifdef HAVE_SRP_SUPPORT |
| 154 |
else if (strcmp(current_key,"srpfile") == 0) strapp(&config->srp_file,(char *) node->value); |
| 155 |
#endif |
| 156 |
#ifdef HAVE_PROMETHEUS_SUPPORT |
| 157 |
else if (strcmp(current_key,"enable-metrics") == 0) config->enable_metrics=atoi((char *) node->value); |
| 158 |
else if (strcmp(current_key,"metrics-tags") == 0) strapp(&config->metrics_tags,(char *) node->value); |
| 159 |
else if (strcmp(current_key,"metrics-multipoint") == 0) config->metrics_multipoint=atoi((char *) node->value); |
| 160 |
else if (strcmp(current_key,"metrics-nocreated") == 0) config->metrics_nocreated=atoi((char *) node->value); |
| 161 |
#ifdef HAVE_LIBMICROHTTPD |
| 162 |
else if (strcmp(current_key,"metrics-http") == 0) config->metrics_http=atoi((char *) node->value); |
| 163 |
else if (strcmp(current_key,"metrics-port") == 0) config->metrics_port=atoi((char *) node->value); |
| 164 |
else if (strcmp(current_key,"metrics-ip") == 0) strapp(&config->metrics_ip,(char *) node->value); |
| 165 |
#endif |
| 166 |
#if HAVE_SOCK_UN_H |
| 167 |
else if (strcmp(current_key,"metrics-unix") == 0) strapp(&config->metrics_unix,(char *) node->value); |
| 168 |
#endif |
| 169 |
#endif |
| 170 |
} |
| 171 |
else if (current_key) |
| 172 |
{ |
| 173 |
if (strcmp(current_key,"inputurl") == 0) |
| 174 |
{ |
| 175 |
strapp(&config->input_url,(char *) node->value); |
| 176 |
strapp(&config->input_url,","); |
| 177 |
} |
| 178 |
else if (strcmp(current_key,"outputurl") == 0) |
| 179 |
{ |
| 180 |
strapp(&config->output_url,(char *) node->value); |
| 181 |
strapp(&config->output_url,","); |
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
if (node->key) |
| 186 |
free(node->key); |
| 187 |
if (node->value) |
| 188 |
free(node->value); |
| 189 |
|
| 190 |
if (node->next) { |
| 191 |
parse_config_file(config, current_key, node->next); |
| 192 |
free(node); |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
|
| 197 |
if (strlen(config->input_url) > 0) config->input_url[strlen(config->input_url)-1]=0; |
| 198 |
if (strlen(config->output_url) > 0) config->output_url[strlen(config->output_url)-1]=0; |
| 199 |
if (current_key) |
| 200 |
free(current_key); |
| 201 |
free(node); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
void strapp(char ** original, char * newstr){ |
| 207 |
if (*original){ |
| 208 |
char * appended = malloc((strlen(newstr)+strlen(*original)+1)*sizeof(char)); |
| 209 |
strcpy(appended,*original); |
| 210 |
strcat(appended,newstr); |
| 211 |
free(*original); |
| 212 |
*original = appended; |
| 213 |
} else { |
| 214 |
*original = strdup(newstr); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
rist_tools_config_object *parse_yaml(char * file){ |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
rist_tools_config_object *config = NULL; |
| 238 |
|
| 239 |
|
| 240 |
FILE *f = fopen(file,"r"); |
| 241 |
if (f == NULL) return config; |
| 242 |
|
| 243 |
|
| 244 |
config = calloc(1, sizeof(rist_tools_config_object)); |
| 245 |
config->profile = RIST_DEFAULT_PROFILE; |
| 246 |
config->stats_interval = 1000; |
| 247 |
config->verbose_level = RIST_LOG_INFO; |
| 248 |
#ifdef HAVE_PROMETHEUS_SUPPORT |
| 249 |
#ifdef HAVE_LIBMICROHTTPD |
| 250 |
config->metrics_port = 1968; |
| 251 |
#endif |
| 252 |
#endif |
| 253 |
|
| 254 |
char *current_key = NULL; |
| 255 |
fseek(f, 0, SEEK_END); |
| 256 |
long fsize = ftell(f); |
| 257 |
fseek(f, 0, SEEK_SET); |
| 258 |
char *yaml_str = malloc(fsize + 1); |
| 259 |
size_t read = fread(yaml_str, fsize, 1, f); |
| 260 |
(void)read; |
| 261 |
yaml_node *root = parse_yaml_string(yaml_str); |
| 262 |
|
| 263 |
print_yaml_node(root); |
| 264 |
|
| 265 |
parse_config_file(config, current_key, root); |
| 266 |
fclose(f); |
| 267 |
return config; |
| 268 |
} |
| 269 |
|
| 270 |
void cleanup_tools_config(rist_tools_config_object * config) |
| 271 |
{ |
| 272 |
if (config->remote_log_address) |
| 273 |
free(config->remote_log_address); |
| 274 |
if (config->secret) |
| 275 |
free(config->secret); |
| 276 |
if (config->input_url) |
| 277 |
free(config->input_url); |
| 278 |
if (config->output_url) |
| 279 |
free(config->output_url); |
| 280 |
if (config->tunnel_interface) |
| 281 |
free(config->tunnel_interface); |
| 282 |
#ifdef HAVE_SRP_SUPPORT |
| 283 |
if (config->srp_file) |
| 284 |
free(config->srp_file); |
| 285 |
#endif |
| 286 |
#ifdef HAVE_PROMETHEUS_SUPPORT |
| 287 |
if (config->metrics_tags) |
| 288 |
free(config->metrics_tags); |
| 289 |
#ifdef HAVE_LIBMICROHTTPD |
| 290 |
if (config->metrics_ip) |
| 291 |
free(config->metrics_ip); |
| 292 |
#endif |
| 293 |
#if HAVE_SOCK_UN_H |
| 294 |
if (config->metrics_unix) |
| 295 |
free(config->metrics_unix); |
| 296 |
#endif |
| 297 |
#endif |
| 298 |
free(config); |
| 299 |
} |