summaryrefslogtreecommitdiffstats
path: root/utils/iftest.c
blob: 02f580755b4b2efae45d685be6f3c37fe7ace176 (plain)
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
/* simple test for index to interface name API */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>

#include <libnfnetlink/libnfnetlink.h>

int main(int argc, char *argv[])
{
	int idx;
	struct nlif_handle *h;
	char name[IFNAMSIZ];
	unsigned int flags;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	h = nlif_open();
	if (h == NULL) {
		perror("nlif_open");
		exit(EXIT_FAILURE);
	}

	if (nlif_query(h) == -1) {
		fprintf(stderr, "failed query to retrieve interfaces: %s\n",
			strerror(errno));
		exit(EXIT_FAILURE);
	}

	idx = if_nametoindex(argv[1]);

	/* Superfluous: just to make sure nlif_index2name is working fine */
	if (nlif_index2name(h, idx, name) == -1)
		fprintf(stderr, "Cannot translate device idx=%u\n", idx);

	if (nlif_get_ifflags(h, idx, &flags) == -1) {
		fprintf(stderr, "Cannot get flags for device `%s'\n", argv[1]);
		exit(EXIT_FAILURE);
	}

	printf("index (%d) is %s (%s) (%s)\n", idx, argv[1],
		flags & IFF_RUNNING ? "RUNNING" : "NOT RUNNING",
		flags & IFF_UP ? "UP" : "DOWN");

	nlif_close(h);
	return EXIT_SUCCESS;
}