From 817963b5869a7a101087acb9034619e1ce3e1c21 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 18 Jul 2020 04:49:22 -0600 Subject: [PATCH] cmd: start work on an ifquery command --- Makefile | 13 ++++++- cmd/ifquery.c | 71 +++++++++++++++++++++++++++++++++++++++ libifupdown/libifupdown.h | 1 + 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 cmd/ifquery.c diff --git a/Makefile b/Makefile index b8d5a40..7a3c05b 100644 --- a/Makefile +++ b/Makefile @@ -7,12 +7,23 @@ LIBIFUPDOWN_SRC = \ libifupdown/interface.c LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o} +LIBIFUPDOWN_LIB = libifupdown.a +CMDS = \ + ifquery + +LIBS = ${LIBIFUPDOWN_LIB} + +IFQUERY_SRC = cmd/ifquery.c +IFQUERY_OBJ = ${IFQUERY_SRC:.c=.o} +ifquery: ${LIBS} ${IFQUERY_OBJ} + ${CC} -o $@ ${IFQUERY_OBJ} ${LIBS} + libifupdown.a: ${LIBIFUPDOWN_OBJ} ar -rcs $@ ${LIBIFUPDOWN_OBJ} -all: libifupdown.a +all: libifupdown.a ${CMDS} clean: rm -f ${LIBIFUPDOWN_OBJ} diff --git a/cmd/ifquery.c b/cmd/ifquery.c new file mode 100644 index 0000000..6f74c44 --- /dev/null +++ b/cmd/ifquery.c @@ -0,0 +1,71 @@ +/* + * cmd/ifquery.c + * Purpose: look up information in /etc/network/interfaces + * + * 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 +#include "libifupdown/libifupdown.h" + + +void +print_interface(struct lif_interface *iface) +{ + if (iface->is_auto) + printf("auto %s\n", iface->ifname); + + printf("iface %s", iface->ifname); + + if (iface->is_loopback) + printf(" inet loopback\n"); + else if (iface->is_dhcp) + printf(" inet dhcp\n"); + else + printf("\n"); + + struct lif_node *iter; + LIF_DICT_FOREACH(iter, &iface->vars) + { + struct lif_dict_entry *entry = iter->data; + + if (!strcmp(entry->key, "address")) + { + struct lif_address *addr = entry->data; + char addr_buf[512]; + + if (!lif_address_unparse(addr, addr_buf, sizeof addr_buf, true)) + { + printf(" # warning: failed to unparse address\n"); + continue; + } + + printf(" %s %s\n", entry->key, addr_buf); + } + else + printf(" %s %s\n", entry->key, (const char *) entry->data); + } +} + + +int +main(int argc, char *argv[]) +{ + struct lif_dict collection; + + lif_interface_collection_init(&collection); + + struct lif_interface *if_lo = lif_interface_collection_find(&collection, "lo"); + print_interface(if_lo); + + return EXIT_SUCCESS; +} diff --git a/libifupdown/libifupdown.h b/libifupdown/libifupdown.h index f99336a..a9c287e 100644 --- a/libifupdown/libifupdown.h +++ b/libifupdown/libifupdown.h @@ -18,5 +18,6 @@ #include "libifupdown/list.h" #include "libifupdown/dict.h" +#include "libifupdown/interface.h" #endif