• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* simple test for index to interface name API */
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <net/if.h>
8 
9 #include <libnfnetlink/libnfnetlink.h>
10 
main(int argc,char * argv[])11 int main(int argc, char *argv[])
12 {
13 	int idx;
14 	struct nlif_handle *h;
15 	char name[IFNAMSIZ];
16 	unsigned int flags;
17 
18 	if (argc != 2) {
19 		fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
20 		exit(EXIT_FAILURE);
21 	}
22 
23 	h = nlif_open();
24 	if (h == NULL) {
25 		perror("nlif_open");
26 		exit(EXIT_FAILURE);
27 	}
28 
29 	if (nlif_query(h) == -1) {
30 		fprintf(stderr, "failed query to retrieve interfaces: %s\n",
31 			strerror(errno));
32 		exit(EXIT_FAILURE);
33 	}
34 
35 	idx = if_nametoindex(argv[1]);
36 
37 	/* Superfluous: just to make sure nlif_index2name is working fine */
38 	if (nlif_index2name(h, idx, name) == -1)
39 		fprintf(stderr, "Cannot translate device idx=%u\n", idx);
40 
41 	if (nlif_get_ifflags(h, idx, &flags) == -1) {
42 		fprintf(stderr, "Cannot get flags for device `%s'\n", argv[1]);
43 		exit(EXIT_FAILURE);
44 	}
45 
46 	printf("index (%d) is %s (%s) (%s)\n", idx, argv[1],
47 		flags & IFF_RUNNING ? "RUNNING" : "NOT RUNNING",
48 		flags & IFF_UP ? "UP" : "DOWN");
49 
50 	nlif_close(h);
51 	return EXIT_SUCCESS;
52 }
53