-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgetifindex.c
More file actions
41 lines (30 loc) · 747 Bytes
/
Copy pathgetifindex.c
File metadata and controls
41 lines (30 loc) · 747 Bytes
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
#include <stdio.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/ioctl.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
int main(int argc, char **argv)
{
int fd;
if (argc != 2) {
fprintf(stderr, "<%s> interface name\n", argv[0]);
return -1;
}
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
return -1;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, argv[1]);
int ret;
ret = ioctl(fd, SIOCGIFINDEX, &ifr);
if (ret < 0) {
fprintf(stderr, "failed to get ifindex %s\n", strerror(errno));
return -1;
}
printf("interface index for [%s] is %d\n", argv[1], ifr.ifr_ifindex);
return 0;
}