2020-07-20 13:42:16 +00:00
|
|
|
/*
|
2020-07-20 14:19:23 +00:00
|
|
|
* libifupdown/state.c
|
2020-07-20 13:42:16 +00:00
|
|
|
* Purpose: state management
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-09-09 19:27:34 +00:00
|
|
|
#include <limits.h>
|
2020-07-20 13:42:16 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "libifupdown/state.h"
|
2020-07-20 14:19:23 +00:00
|
|
|
#include "libifupdown/fgetline.h"
|
2020-09-08 20:46:07 +00:00
|
|
|
#include "libifupdown/tokenize.h"
|
2020-07-20 14:19:23 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
lif_state_read(struct lif_dict *state, FILE *fd)
|
|
|
|
{
|
|
|
|
char linebuf[4096];
|
2020-09-08 20:46:07 +00:00
|
|
|
|
2020-07-20 14:19:23 +00:00
|
|
|
while (lif_fgetline(linebuf, sizeof linebuf, fd))
|
|
|
|
{
|
2020-09-08 21:37:35 +00:00
|
|
|
char *bufp = linebuf;
|
2020-09-08 20:46:07 +00:00
|
|
|
char *ifname = lif_next_token(&bufp);
|
2020-09-08 20:59:58 +00:00
|
|
|
char *refcount = lif_next_token(&bufp);
|
2020-10-21 14:29:54 +00:00
|
|
|
char *explicit = lif_next_token(&bufp);
|
2020-09-08 20:59:58 +00:00
|
|
|
size_t rc = 1;
|
2020-07-20 14:19:23 +00:00
|
|
|
char *equals_p = strchr(linebuf, '=');
|
2020-10-21 14:29:54 +00:00
|
|
|
bool is_explicit = false;
|
|
|
|
|
2020-10-21 14:55:30 +00:00
|
|
|
if (*explicit && !strcmp(explicit, "explicit"))
|
2020-10-21 14:29:54 +00:00
|
|
|
is_explicit = true;
|
2020-07-20 14:19:23 +00:00
|
|
|
|
2020-09-08 20:59:58 +00:00
|
|
|
if (*refcount)
|
2020-09-09 17:56:19 +00:00
|
|
|
{
|
2020-09-08 20:59:58 +00:00
|
|
|
rc = strtoul(refcount, NULL, 10);
|
|
|
|
|
2020-09-09 17:56:19 +00:00
|
|
|
if (rc == 0 || rc == ULONG_MAX)
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
2020-07-20 14:19:23 +00:00
|
|
|
if (equals_p == NULL)
|
|
|
|
{
|
2020-10-21 14:29:54 +00:00
|
|
|
lif_state_upsert(state, ifname, &(struct lif_interface){ .ifname = ifname, .refcount = rc, .is_explicit = is_explicit });
|
2020-07-20 14:19:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*equals_p++ = '\0';
|
2020-10-21 14:29:54 +00:00
|
|
|
lif_state_upsert(state, ifname, &(struct lif_interface){ .ifname = equals_p, .refcount = rc, .is_explicit = is_explicit });
|
2020-07-20 14:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
lif_state_read_path(struct lif_dict *state, const char *path)
|
|
|
|
{
|
|
|
|
FILE *fd = fopen(path, "r");
|
|
|
|
bool ret;
|
|
|
|
|
2020-07-24 13:13:04 +00:00
|
|
|
/* if file cannot be opened, assume an empty state */
|
2020-07-20 14:19:23 +00:00
|
|
|
if (fd == NULL)
|
2020-07-24 13:13:04 +00:00
|
|
|
return true;
|
2020-07-20 14:19:23 +00:00
|
|
|
|
|
|
|
ret = lif_state_read(state, fd);
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-07-20 13:42:16 +00:00
|
|
|
|
2020-09-08 21:05:02 +00:00
|
|
|
void
|
|
|
|
lif_state_ref_if(struct lif_dict *state, const char *ifname, struct lif_interface *iface)
|
|
|
|
{
|
|
|
|
iface->refcount++;
|
|
|
|
lif_state_upsert(state, ifname, iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lif_state_unref_if(struct lif_dict *state, const char *ifname, struct lif_interface *iface)
|
|
|
|
{
|
2020-09-08 21:28:36 +00:00
|
|
|
if (iface->refcount == 0)
|
|
|
|
return;
|
|
|
|
|
2020-09-08 21:05:02 +00:00
|
|
|
iface->refcount--;
|
|
|
|
|
|
|
|
if (iface->refcount)
|
|
|
|
lif_state_upsert(state, ifname, iface);
|
|
|
|
else
|
|
|
|
lif_state_delete(state, ifname);
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:42:16 +00:00
|
|
|
void
|
|
|
|
lif_state_upsert(struct lif_dict *state, const char *ifname, struct lif_interface *iface)
|
|
|
|
{
|
2020-09-08 21:02:39 +00:00
|
|
|
lif_state_delete(state, ifname);
|
|
|
|
|
2020-09-08 20:59:58 +00:00
|
|
|
struct lif_state_record *rec = calloc(1, sizeof(*rec));
|
|
|
|
|
|
|
|
rec->mapped_if = strdup(iface->ifname);
|
|
|
|
rec->refcount = iface->refcount;
|
2020-10-21 14:15:52 +00:00
|
|
|
rec->is_explicit = iface->is_explicit;
|
2020-09-08 20:59:58 +00:00
|
|
|
|
|
|
|
lif_dict_add(state, ifname, rec);
|
2020-07-20 13:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-09-08 20:59:58 +00:00
|
|
|
struct lif_state_record *rec = entry->data;
|
|
|
|
free(rec->mapped_if);
|
|
|
|
free(rec);
|
|
|
|
|
2020-07-20 13:42:16 +00:00
|
|
|
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;
|
2020-09-08 20:59:58 +00:00
|
|
|
struct lif_state_record *rec = entry->data;
|
2020-07-20 13:42:16 +00:00
|
|
|
|
2020-10-21 14:29:54 +00:00
|
|
|
fprintf(f, "%s=%s %zu%s\n", entry->key, rec->mapped_if, rec->refcount,
|
|
|
|
rec->is_explicit ? " explicit" : "");
|
2020-07-20 13:42:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 13:45:39 +00:00
|
|
|
|
2020-07-20 14:19:23 +00:00
|
|
|
bool
|
|
|
|
lif_state_write_path(const struct lif_dict *state, const char *path)
|
|
|
|
{
|
|
|
|
FILE *fd = fopen(path, "w");
|
|
|
|
|
|
|
|
if (fd == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
lif_state_write(state, fd);
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:45:39 +00:00
|
|
|
struct lif_interface *
|
|
|
|
lif_state_lookup(struct lif_dict *state, struct lif_dict *if_collection, const char *ifname)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = lif_dict_find(state, ifname);
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2020-09-08 20:59:58 +00:00
|
|
|
struct lif_state_record *rec = entry->data;
|
|
|
|
struct lif_dict_entry *if_entry = lif_dict_find(if_collection, rec->mapped_if);
|
2020-07-20 13:45:39 +00:00
|
|
|
|
|
|
|
if (if_entry == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return if_entry->data;
|
|
|
|
}
|
2020-07-24 10:48:50 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
lif_state_sync(struct lif_dict *state, struct lif_dict *if_collection)
|
|
|
|
{
|
|
|
|
struct lif_node *iter;
|
|
|
|
|
|
|
|
LIF_DICT_FOREACH(iter, state)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
2020-09-08 20:59:58 +00:00
|
|
|
struct lif_state_record *rec = entry->data;
|
2020-09-09 01:25:00 +00:00
|
|
|
struct lif_interface *iface = lif_interface_collection_find(if_collection, rec->mapped_if);
|
2020-07-24 10:48:50 +00:00
|
|
|
|
2020-09-08 20:59:58 +00:00
|
|
|
iface->refcount = rec->refcount;
|
2020-10-21 14:30:35 +00:00
|
|
|
iface->is_explicit = rec->is_explicit;
|
2020-07-24 10:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|