From d4e19974862cda94284367636af6255ad99b13be Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Mon, 20 Jul 2020 07:42:16 -0600 Subject: [PATCH] add core state management --- Makefile | 6 ++++-- libifupdown/state.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ libifupdown/state.h | 26 ++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 libifupdown/state.c create mode 100644 libifupdown/state.h diff --git a/Makefile b/Makefile index 955a24d..69cbd47 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ PACKAGE_BUGREPORT := https://github.com/kaniini/ifupdown-ng/issues/new INTERFACES_FILE := /etc/network/interfaces -CFLAGS := -ggdb3 -O2 -Wall -I. -DINTERFACES_FILE=\"${INTERFACES_FILE}\" -DPACKAGE_NAME=\"${PACKAGE_NAME}\" -DPACKAGE_VERSION=\"${PACKAGE_VERSION}\" -DPACKAGE_BUGREPORT=\"${PACKAGE_BUGREPORT}\" +STATE_FILE := /run/ifstate +CFLAGS := -ggdb3 -O2 -Wall -I. -DINTERFACES_FILE=\"${INTERFACES_FILE}\" -DSTATE_FILE=\"${STATE_FILE}\" -DPACKAGE_NAME=\"${PACKAGE_NAME}\" -DPACKAGE_VERSION=\"${PACKAGE_VERSION}\" -DPACKAGE_BUGREPORT=\"${PACKAGE_BUGREPORT}\" LIBIFUPDOWN_SRC = \ @@ -13,7 +14,8 @@ LIBIFUPDOWN_SRC = \ libifupdown/interface.c \ libifupdown/interface-file.c \ libifupdown/fgetline.c \ - libifupdown/version.c + libifupdown/version.c \ + libifupdown/state.c LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o} LIBIFUPDOWN_LIB = libifupdown.a diff --git a/libifupdown/state.c b/libifupdown/state.c new file mode 100644 index 0000000..fc7030c --- /dev/null +++ b/libifupdown/state.c @@ -0,0 +1,48 @@ +/* + * libifupdown/state.h + * Purpose: state management + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include +#include "libifupdown/state.h" + +void +lif_state_upsert(struct lif_dict *state, const char *ifname, struct lif_interface *iface) +{ + lif_dict_add(state, ifname, strdup(iface->ifname)); +} + +void +lif_state_delete(struct lif_dict *state, const char *ifname) +{ + struct lif_dict_entry *entry = lif_dict_find(state, ifname); + + if (entry == NULL) + return; + + free(entry->data); + lif_dict_delete_entry(state, entry); +} + +void +lif_state_write(const struct lif_dict *state, FILE *f) +{ + struct lif_node *iter; + + LIF_DICT_FOREACH(iter, state) + { + struct lif_dict_entry *entry = iter->data; + + fprintf(f, "%s=%s\n", entry->key, (const char *) entry->data); + } +} diff --git a/libifupdown/state.h b/libifupdown/state.h new file mode 100644 index 0000000..8043ec6 --- /dev/null +++ b/libifupdown/state.h @@ -0,0 +1,26 @@ +/* + * libifupdown/state.h + * Purpose: state management + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#ifndef LIBIFUPDOWN_STATE_H__GUARD +#define LIBIFUPDOWN_STATE_H__GUARD + +#include +#include "libifupdown/interface.h" + +extern void lif_state_upsert(struct lif_dict *state, const char *ifname, struct lif_interface *iface); +extern void lif_state_delete(struct lif_dict *state, const char *ifname); +extern void lif_state_write(const struct lif_dict *state, FILE *f); + +#endif