Parsed and stored disabled commands.

master
Nicolas Favre-Felix 14 years ago
parent c7f855ff39
commit 1209922c03

@ -2,14 +2,18 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <jansson.h>
#include "conf.h"
static struct disabled_command *
conf_disable_commands(json_t *jtab);
struct conf *
conf_read(const char *filename) {
json_t *j, *jtmp;
json_t *j;
json_error_t error;
struct conf *conf;
void *kv;
@ -28,7 +32,8 @@ conf_read(const char *filename) {
}
for(kv = json_object_iter(j); kv; kv = json_object_iter_next(j, kv)) {
jtmp = json_object_iter_value(kv);
json_t *jtmp = json_object_iter_value(kv);
if(strcmp(json_object_iter_key(kv), "redis_host") == 0 && json_typeof(jtmp) == JSON_STRING) {
free(conf->redis_host);
conf->redis_host = strdup(json_string_value(jtmp));
@ -40,7 +45,7 @@ conf_read(const char *filename) {
} else if(strcmp(json_object_iter_key(kv), "http_port") == 0 && json_typeof(jtmp) == JSON_INTEGER) {
conf->http_port = json_integer_value(jtmp);
} else if(strcmp(json_object_iter_key(kv), "disable") == 0 && json_typeof(jtmp) == JSON_OBJECT) {
/* TODO */
conf->disabled = conf_disable_commands(jtmp);
}
}
@ -49,6 +54,75 @@ conf_read(const char *filename) {
return conf;
}
struct disabled_command *
conf_disable_commands(json_t *jtab) {
struct disabled_command *root = NULL;
void *kv;
for(kv = json_object_iter(jtab); kv; kv = json_object_iter_next(jtab, kv)) {
unsigned int i, cur, n;
char *p, *ip;
const char *s;
in_addr_t mask_ip;
short mask_bits = 0;
struct disabled_command *dc;
json_t *val = json_object_iter_value(kv);
if(json_typeof(val) != JSON_ARRAY) {
continue; /* TODO: report error? */
}
/* parse key in format "ip/mask" */
s = json_object_iter_key(kv);
p = strchr(s, ':');
if(!p) {
ip = strdup(s);
} else {
ip = calloc(p - s + 1, 1);
memcpy(ip, s, p - s);
mask_bits = atoi(p+1);
}
mask_ip = inet_addr(ip);
/* count strings in the array */
n = 0;
for(i = 0; i < json_array_size(val); ++i) {
json_t *jelem = json_array_get(val, i);
if(json_typeof(jelem) == JSON_STRING) {
n++;
}
}
/* allocate block */
dc = calloc(1, sizeof(struct disabled_command));
dc->commands = calloc(n, sizeof(char*));
dc->mask_ip = mask_ip;
dc->mask_bits = mask_bits;
dc->next = root;
root = dc;
/* add all disabled commands */
for(i = 0, cur = 0; i < json_array_size(val); ++i) {
json_t *jelem = json_array_get(val, i);
if(json_typeof(jelem) == JSON_STRING) {
s = json_string_value(jelem);
size_t sz = strlen(s);
dc->commands[cur] = calloc(1 + sz, 1);
memcpy(dc->commands[cur], s, sz);
cur++;
}
}
}
return root;
}
void
conf_free(struct conf *conf) {

@ -1,6 +1,18 @@
#ifndef CONF_H
#define CONF_H
#include <stdint.h>
#include <netinet/in.h>
struct disabled_command {
in_addr_t mask_ip;
short mask_bits;
char **commands;
struct disabled_command *next;
};
struct conf {
@ -9,6 +21,8 @@ struct conf {
char *http_host;
short http_port;
struct disabled_command *disabled;
};
struct conf *

Loading…
Cancel
Save