From bcef77a7fb3b73d2a2fbcea51012014b62755bb5 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 31 May 2019 18:42:01 +0200 Subject: Switch to a JSON-based config format We can get rid of a lot of code by ditching out INI format parser. We also remove the support for "subtype" and "generator" templates for now; rather than implementing this in NeCo itself, templates could be implemented as a Lua DSL later. --- src/device-interface.c | 117 ++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 55 deletions(-) (limited to 'src/device-interface.c') diff --git a/src/device-interface.c b/src/device-interface.c index a2aae14..ef85089 100644 --- a/src/device-interface.c +++ b/src/device-interface.c @@ -1,5 +1,4 @@ #include "device.h" -#include "keywords.h" #include "netlink.h" #include "util.h" #include "vector.h" @@ -109,22 +108,43 @@ static bool parse_prefix(ipaddr_prefix_t *prefix, const char *str, bool allow_ho return true; } -static bool process_section_device(device_interface_t *iface, const ini_section_t *section) { - ini_field_t *field; +static void interface_free(device_t *dev) { + device_interface_t *iface = container_of(dev, device_interface_t, device); - list_for_each_entry(field, §ion->fields, node) { - switch (lookup_keyword(field->key)) { - default: - fprintf(stderr, "interface: [Device]: unknown field %s\n", field->key); - } - } + VECTOR_FREE(iface->addrs); + free(NODE_NAME(dev)); + free(iface); +} +static bool process_section_device(device_interface_t *iface, struct json_object *section) { return true; } -static bool process_section_static(device_interface_t *iface, const ini_section_t *section) { - ini_field_t *field; +static bool process_section_static(device_interface_t *iface, struct json_object *section) { + struct json_object *addresses = neco_json_get_value(section, "addresses", json_type_array); + if (addresses) { + for (size_t i = 0; i < json_object_array_length(addresses); i++) { + struct json_object *address = json_object_array_get_idx(addresses, i); + if (!json_object_is_type(address, json_type_string)) { + fprintf(stderr, "interface: static: invalid address entry of type %s\n", json_type_to_name(json_object_get_type(address))); + continue; + } + + ipaddr_prefix_t p; + if (!parse_prefix(&p, json_object_get_string(address), true)) { + fprintf(stderr, "interface: static: unable to parse Address %s\n", json_object_get_string(address)); + break; + } + if (!VECTOR_ADD(iface->addrs, p)) { + fprintf(stderr, "interface: static: adding address failed\n"); + return false; + } + } + } + + + /* list_for_each_entry(field, §ion->fields, node) { switch (lookup_keyword(field->key)) { case KW_Address: { @@ -146,16 +166,42 @@ static bool process_section_static(device_interface_t *iface, const ini_section_ fprintf(stderr, "interface: [Static]: unknown field %s\n", field->key); } } + */ return true; } -static void interface_free(device_t *dev) { - device_interface_t *iface = container_of(dev, device_interface_t, device); +static device_t * interface_process_config(const char *name, struct json_object *config) { + device_interface_t *iface = calloc(1, sizeof(*iface)); + if (!iface) + return NULL; - VECTOR_FREE(iface->addrs); - free(NODE_NAME(dev)); - free(iface); + device_t *dev = &iface->device; + dev->type = &device_type_interface; + + NODE_NAME(dev) = strdup(name); + if (!NODE_NAME(dev)) { + free(iface); + return NULL; + } + + struct json_object *sec_device = neco_json_get_value(config, "device", json_type_object); + if (sec_device) { + if (!process_section_device(iface, sec_device)) + goto err; + } + + struct json_object *sec_static = neco_json_get_value(config, "static", json_type_object); + if (sec_static) { + if (!process_section_static(iface, sec_static)) + goto err; + } + + return dev; + +err: + interface_free(dev); + return NULL; } static bool interface_set_link_flags(unsigned index, unsigned change, unsigned flags) { @@ -281,45 +327,6 @@ static void interface_release(device_t *dev) { interface_set_link_state(index, false); } -static device_t * interface_process_config(const char *name, const ini_file_t *config) { - device_interface_t *iface = calloc(1, sizeof(*iface)); - if (!iface) - return NULL; - - device_t *dev = &iface->device; - dev->type = &device_type_interface; - - NODE_NAME(dev) = strdup(name); - if (!NODE_NAME(dev)) { - free(iface); - return NULL; - } - - const ini_section_t *section; - list_for_each_entry(section, &config->sections, node) { - switch (lookup_keyword(section->name)) { - case KW_Device: - if (!process_section_device(iface, section)) - goto err; - break; - - case KW_Static: - if (!process_section_static(iface, section)) - goto err; - break; - - default: - fprintf(stderr, "interface: unknown section %s\n", section->name); - } - } - - return dev; - -err: - interface_free(dev); - return NULL; -} - static device_type_t device_type_interface = { .process_config = interface_process_config, .free = interface_free, -- cgit v1.2.3