File: ifparse.c

package info (click to toggle)
ifupdown-ng 0.12.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: ansic: 3,572; sh: 980; makefile: 233
file content (225 lines) | stat: -rw-r--r-- 5,402 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * cmd/ifparse.c
 * Purpose: Redisplay /e/n/i in alternative formats.
 *
 * 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.
 */

#define _GNU_SOURCE
#include <fnmatch.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "libifupdown/libifupdown.h"

#ifdef CONFIG_YAML
# include "libifupdown/yaml-base.h"
# include "libifupdown/yaml-writer.h"
#endif

#include "cmd/multicall.h"
#include "cmd/pretty-print-iface.h"

static bool show_all = false;
static bool allow_undefined = false;

static void
set_show_all(const char *arg)
{
	(void) arg;

	show_all = true;
}

static void
set_allow_undefined(const char *arg)
{
	(void) arg;

	allow_undefined = true;
}

static const char *output_fmt = "ifupdown";

static void
set_output_fmt(const char *arg)
{
	output_fmt = arg;
}

static struct if_option local_options[] = {
	{'F', "format", NULL, "output format to use", true, set_output_fmt},
	{'A', "all", NULL, "show all interfaces", false, set_show_all},
	{'U', "allow-undefined", NULL, "allow querying undefined (virtual) interfaces", false, set_allow_undefined},
};

static struct if_option_group local_option_group = {
	.desc = "Program-specific options",
	.group_size = ARRAY_SIZE(local_options),
	.group = local_options
};

#ifdef CONFIG_YAML
static void
prettyprint_interface_yaml(struct lif_interface *iface)
{
	struct lif_yaml_node doc = {};

	lif_yaml_document_init(&doc, "interfaces");

	struct lif_yaml_node *iface_node = lif_yaml_node_new_list(iface->ifname);
	lif_yaml_node_append_child(&doc, iface_node);

	if (iface->is_auto)
	{
		struct lif_yaml_node *iface_entry_node = lif_yaml_node_new_boolean("auto", true);
		lif_yaml_node_append_child(iface_node, iface_entry_node);
	}

	struct lif_node *iter;
	LIF_DICT_FOREACH(iter, &iface->vars)
	{
		struct lif_dict_entry *entry = iter->data;
		const char *value = entry->data;
		char addr_buf[512];

		if (!strcmp(entry->key, "address"))
		{
			struct lif_address *addr = entry->data;

			if (!lif_address_unparse(addr, addr_buf, sizeof addr_buf, true))
				continue;

			value = addr_buf;
		}

		struct lif_yaml_node *iface_entry_node = lif_yaml_node_new_string(entry->key, value);
		lif_yaml_node_append_child(iface_node, iface_entry_node);
	}

	lif_yaml_write(iface_node, stdout, true);
	lif_yaml_node_free(&doc);
}
#endif

struct prettyprint_impl_map {
	const char *name;
	void (*handle)(struct lif_interface *iface);
};

struct prettyprint_impl_map pp_impl_map[] = {
	{"ifupdown", prettyprint_interface_eni},
#ifdef CONFIG_YAML
	{"yaml-raw", prettyprint_interface_yaml},
#endif
};

static int
pp_impl_cmp(const void *a, const void *b)
{
	const char *key = a;
	const struct prettyprint_impl_map *impl = b;

	return strcmp(key, impl->name);
}

static int
ifparse_main(int argc, char *argv[])
{
	struct lif_dict state = {};
	struct lif_dict collection = {};
	struct lif_interface_file_parse_state parse_state = {
		.collection = &collection,
	};

	lif_interface_collection_init(&collection);

	if (!lif_state_read_path(&state, exec_opts.state_file))
	{
		fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.state_file);
		return EXIT_FAILURE;
	}

	if (!lif_interface_file_parse(&parse_state, exec_opts.interfaces_file))
	{
		fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.interfaces_file);
		return EXIT_FAILURE;
	}

	if (match_opts.property == NULL && lif_lifecycle_count_rdepends(&exec_opts, &collection) == -1)
	{
		fprintf(stderr, "%s: could not validate dependency tree\n", argv0);
		return EXIT_FAILURE;
	}

	if (!lif_compat_apply(&collection))
	{
		fprintf(stderr, "%s: failed to apply compatibility glue\n", argv0);
		return EXIT_FAILURE;
	}

	struct prettyprint_impl_map *m = bsearch(output_fmt, pp_impl_map, ARRAY_SIZE(pp_impl_map), sizeof(*m), pp_impl_cmp);
	if (m == NULL)
	{
		fprintf(stderr, "%s: %s: output format not supported\n", argv0, output_fmt);
		return EXIT_FAILURE;
	}

	if (show_all)
	{
		struct lif_node *n;

		LIF_DICT_FOREACH(n, &collection)
		{
			struct lif_dict_entry *entry = n->data;

			m->handle(entry->data);
		}

		return EXIT_SUCCESS;
	}

	if (optind >= argc)
		generic_usage(self_applet, EXIT_FAILURE);

	int idx = optind;
	for (; idx < argc; idx++)
	{
		struct lif_dict_entry *entry = lif_dict_find(&collection, argv[idx]);
		struct lif_interface *iface = NULL;

		if (entry != NULL)
			iface = entry->data;

		if (entry == NULL && allow_undefined)
			iface = lif_interface_collection_find(&collection, argv[idx]);

		if (iface == NULL)
		{
			fprintf(stderr, "%s: unknown interface %s\n", argv0, argv[idx]);
			return EXIT_FAILURE;
		}

		m->handle(iface);
	}

	return EXIT_SUCCESS;
}

struct if_applet ifparse_applet = {
	.name = "ifparse",
	.desc = "redisplay interface configuration",
	.main = ifparse_main,
	.usage = "ifparse [options] <interfaces>\n  ifparse [options] --all",
	.manpage = "8 ifparse",
	.groups = { &global_option_group, &match_option_group, &exec_option_group, &local_option_group },
};
APPLET_REGISTER(ifparse_applet)