if_nametoindex.c 489 字节
Newer Older
1 2 3 4 5
#define _GNU_SOURCE
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <string.h>
W
w00349915 已提交
6
#include <unsupported_api.h>
7 8 9 10 11 12
#include "syscall.h"

unsigned if_nametoindex(const char *name)
{
	struct ifreq ifr;
	int fd, r;
W
w00349915 已提交
13
	unsupported_api(__FUNCTION__);
14
	if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0;
15 16 17
	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
	r = ioctl(fd, SIOCGIFINDEX, &ifr);
	__syscall(SYS_close, fd);
18
	return r < 0 ? 0 : ifr.ifr_ifindex;
19
}