/* * 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. */ #define _GNU_SOURCE #include #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); } } void usage() { printf("usage: ifquery [options] \n"); printf(" ifquery [options] --list\n"); printf("\nOptions:\n"); printf(" -h, --help this help\n"); printf(" -V, --version show this program's version\n"); printf(" -i, --interfaces FILE use FILE for interface definitions\n"); exit(1); } int main(int argc, char *argv[]) { struct lif_dict collection; struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"interfaces", required_argument, 0, 'i'}, {NULL, 0, 0, 0} }; char *interfaces_file = INTERFACES_FILE; for (;;) { int c = getopt_long(argc, argv, "hVi:", long_options, NULL); if (c == -1) break; switch (c) { case 'h': usage(); break; case 'V': lif_common_version(); break; case 'i': interfaces_file = optarg; break; } } if (!lif_interface_file_parse(&collection, interfaces_file)) { fprintf(stderr, "ifquery: could not parse %s\n", interfaces_file); return EXIT_FAILURE; } 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) { fprintf(stderr, "ifquery: unknown interface %s\n", argv[idx]); return EXIT_FAILURE; } print_interface(entry->data); } return EXIT_SUCCESS; }