ifupdown-ng/cmd/ifquery.c

129 lines
2.8 KiB
C
Raw Normal View History

2020-07-18 10:49:22 +00:00
/*
* cmd/ifquery.c
* Purpose: look up information in /etc/network/interfaces
*
* 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-07-18 23:44:58 +00:00
#define _GNU_SOURCE
2020-07-18 10:49:22 +00:00
#include <stdio.h>
#include <string.h>
2020-07-18 23:44:58 +00:00
#include <getopt.h>
2020-07-18 10:49:22 +00:00
#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);
}
}
2020-07-18 23:44:58 +00:00
void
usage()
{
printf("usage: ifquery [options] <interfaces>\n");
printf(" ifquery [options] --list\n");
printf("\nOptions:\n");
2020-07-19 01:09:08 +00:00
printf(" -h, --help this help\n");
printf(" -V, --version show this program's version\n");
2020-07-18 23:44:58 +00:00
printf(" -i, --interfaces FILE use FILE for interface definitions\n");
exit(1);
}
2020-07-18 10:49:22 +00:00
int
main(int argc, char *argv[])
{
struct lif_dict collection;
2020-07-18 23:44:58 +00:00
struct option long_options[] = {
2020-07-19 01:09:08 +00:00
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
2020-07-18 23:44:58 +00:00
{"interfaces", required_argument, 0, 'i'},
{NULL, 0, 0, 0}
};
char *interfaces_file = INTERFACES_FILE;
for (;;)
{
2020-07-19 01:09:08 +00:00
int c = getopt_long(argc, argv, "hVi:", long_options, NULL);
2020-07-18 23:44:58 +00:00
if (c == -1)
break;
switch (c) {
2020-07-19 01:09:08 +00:00
case 'h':
usage();
break;
case 'V':
lif_common_version();
break;
2020-07-18 23:44:58 +00:00
case 'i':
interfaces_file = optarg;
break;
}
}
2020-07-18 10:49:22 +00:00
2020-07-19 00:02:25 +00:00
if (!lif_interface_file_parse(&collection, interfaces_file))
{
fprintf(stderr, "ifquery: could not parse %s\n", interfaces_file);
return EXIT_FAILURE;
}
2020-07-18 10:49:22 +00:00
2020-07-18 23:44:58 +00:00
if (optind >= argc)
usage();
int idx = optind;
for (; idx < argc; idx++)
{
struct lif_dict_entry *entry = lif_dict_find(&collection, argv[idx]);
if (entry == NULL)
{
2020-07-19 00:02:25 +00:00
fprintf(stderr, "ifquery: unknown interface %s\n", argv[idx]);
2020-07-18 23:44:58 +00:00
return EXIT_FAILURE;
}
print_interface(entry->data);
}
2020-07-18 10:49:22 +00:00
return EXIT_SUCCESS;
}