libifupdown: add initial interface management code

This commit is contained in:
Ariadne Conill 2020-07-18 04:24:53 -06:00
parent 3f618d2a3b
commit 84a01ffdc4
3 changed files with 285 additions and 1 deletions

View file

@ -3,7 +3,8 @@ CFLAGS := -ggdb3 -O2 -Wall -I.
LIBIFUPDOWN_SRC = \
libifupdown/list.c \
libifupdown/dict.c
libifupdown/dict.c \
libifupdown/interface.c
LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o}

209
libifupdown/interface.c Normal file
View file

@ -0,0 +1,209 @@
/*
* libifupdown/interface.c
* Purpose: interface 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.
*/
#include <stdio.h>
#include <string.h>
#include "libifupdown/interface.h"
bool
lif_address_parse(struct lif_address *address, const char *presentation)
{
char buf[512], *netmask_p;
strlcpy(buf, presentation, sizeof buf);
address->domain = strchr(buf, ':') != NULL ? AF_INET6 : AF_INET;
netmask_p = strrchr(buf, '/');
if (netmask_p != NULL)
{
*netmask_p++ = '\0';
address->netmask = strtol(netmask_p, NULL, 10);
}
else
address->netmask = 0;
return !!inet_pton(address->domain, buf, address->addr_buf);
}
bool
lif_address_unparse(const struct lif_address *address, char *buf, size_t buflen, bool with_netmask)
{
char workbuf[512] = {};
if (!inet_ntop(address->domain, address->addr_buf, workbuf, sizeof workbuf))
return false;
if (!with_netmask || !address->netmask)
{
strlcpy(buf, workbuf, buflen);
return true;
}
snprintf(buf, buflen, "%s/%zu", workbuf, address->netmask);
return true;
}
void
lif_interface_init(struct lif_interface *interface, const char *ifname)
{
memset(interface, '\0', sizeof *interface);
interface->ifname = strdup(ifname);
}
bool
lif_interface_address_add(struct lif_interface *interface, const char *address)
{
struct lif_address *addr = calloc(1, sizeof *addr);
if (!lif_address_parse(addr, address))
{
free(addr);
return false;
}
lif_dict_add(&interface->vars, "address", addr);
return true;
}
void
lif_interface_address_delete(struct lif_interface *interface, const char *address)
{
struct lif_node *iter, *iter_next;
struct lif_address addr;
if (!lif_address_parse(&addr, address))
return;
LIF_DICT_FOREACH_SAFE(iter, iter_next, &interface->vars)
{
struct lif_dict_entry *entry = iter->data;
if (strcmp(entry->key, "address"))
continue;
struct lif_address *entry_addr = entry->data;
char addr_buf[512] = {};
if (!lif_address_unparse(entry_addr, addr_buf, sizeof addr_buf, addr.netmask != 0))
continue;
if (strcmp(addr_buf, address))
continue;
lif_dict_delete_entry(&interface->vars, entry);
free(entry_addr);
}
}
void
lif_interface_fini(struct lif_interface *interface)
{
struct lif_node *iter, *iter_next;
LIF_DICT_FOREACH_SAFE(iter, iter_next, &interface->vars)
{
struct lif_dict_entry *entry = iter->data;
free(entry->data);
lif_dict_delete_entry(&interface->vars, entry);
}
free(interface->ifname);
}
void
lif_interface_collection_init(struct lif_dict *collection)
{
struct lif_interface *if_lo;
memset(collection, '\0', sizeof *collection);
/* always enable loopback interface as part of a collection */
if_lo = lif_interface_collection_find(collection, "lo");
if_lo->is_auto = if_lo->is_loopback = true;
lif_interface_address_add(if_lo, "127.0.0.1/8");
}
void
lif_interface_collection_fini(struct lif_dict *collection)
{
struct lif_node *iter, *iter_next;
LIF_DICT_FOREACH_SAFE(iter, iter_next, collection)
{
struct lif_dict_entry *entry = iter->data;
struct lif_interface *iface = entry->data;
lif_interface_fini(iface);
free(iface);
lif_dict_delete_entry(collection, entry);
}
}
struct lif_interface *
lif_interface_collection_find(struct lif_dict *collection, const char *ifname)
{
struct lif_dict_entry *entry = lif_dict_find(collection, ifname);
if (entry == NULL)
{
struct lif_interface *iface = calloc(1, sizeof *iface);
lif_interface_init(iface, ifname);
lif_dict_add(collection, ifname, iface);
return iface;
}
return entry->data;
}
struct lif_interface *
lif_interface_collection_upsert(struct lif_dict *collection, struct lif_interface *interface)
{
struct lif_dict_entry *entry = lif_dict_find(collection, interface->ifname);
if (entry == NULL)
{
lif_dict_add(collection, interface->ifname, interface);
return interface;
}
if (entry->data == interface)
return interface;
lif_interface_collection_delete(collection, entry->data);
lif_dict_add(collection, interface->ifname, interface);
return interface;
}
void
lif_interface_collection_delete(struct lif_dict *collection, struct lif_interface *interface)
{
struct lif_dict_entry *entry = lif_dict_find(collection, interface->ifname);
if (entry == NULL)
return;
lif_interface_fini(interface);
free(interface);
lif_dict_delete_entry(collection, entry);
}

74
libifupdown/interface.h Normal file
View file

@ -0,0 +1,74 @@
/*
* libifupdown/interface.h
* Purpose: interface 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.
*/
#ifndef LIBIFUPDOWN_INTERFACE_H__GUARD
#define LIBIFUPDOWN_INTERFACE_H__GUARD
#include <arpa/inet.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "libifupdown/dict.h"
/*
* Addresses are stored in a buffer with netmask and type.
*/
struct lif_address {
unsigned char addr_buf[sizeof(struct in6_addr)];
size_t netmask;
int domain;
};
extern bool lif_address_parse(struct lif_address *address, const char *presentation);
extern bool lif_address_unparse(const struct lif_address *address, char *buf, size_t buflen, bool with_netmask);
/*
* Interfaces are contained in a dictionary, with the interfaces mapped by
* interface name to their `struct lif_interface`.
*
* Interfaces are simply upserted as needed. This allows for `auto eth0`
* to create a placeholder `struct lif_interface` with auto set to true.
*
* Configuration variables are simply stored in a `struct lif_dict`, which
* can act as a multidict.
*/
struct lif_interface {
char *ifname;
bool is_dhcp;
bool is_loopback;
bool is_auto;
struct lif_dict vars;
};
#define LIF_INTERFACE_COLLECTION_FOREACH(iter, collection) \
LIF_DICT_FOREACH((iter), (collection))
#define LIF_INTERFACE_COLLECTION_FOREACH_SAFE(iter, iter_next, collection) \
LIF_DICT_FOREACH_SAFE((iter), (iter_next), (collection))
extern void lif_interface_init(struct lif_interface *interface, const char *ifname);
extern bool lif_interface_address_add(struct lif_interface *interface, const char *address);
extern void lif_interface_address_delete(struct lif_interface *interface, const char *address);
extern void lif_interface_fini(struct lif_interface *interface);
extern void lif_interface_collection_init(struct lif_dict *collection);
extern void lif_interface_collection_fini(struct lif_dict *collection);
extern struct lif_interface *lif_interface_collection_find(struct lif_dict *collection, const char *ifname);
extern struct lif_interface *lif_interface_collection_upsert(struct lif_dict *collection, struct lif_interface *interface);
extern void lif_interface_collection_delete(struct lif_dict *collection, struct lif_interface *interface);
#endif