if_indextoname.c 569 字节
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>
D
Daniel Sabogal 已提交
6
#include <errno.h>
W
w00349915 已提交
7
#include <unsupported_api.h>
8 9 10 11 12 13
#include "syscall.h"

char *if_indextoname(unsigned index, char *name)
{
	struct ifreq ifr;
	int fd, r;
W
w00349915 已提交
14
	unsupported_api(__FUNCTION__);
15
	if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0;
16 17 18
	ifr.ifr_ifindex = index;
	r = ioctl(fd, SIOCGIFNAME, &ifr);
	__syscall(SYS_close, fd);
D
Daniel Sabogal 已提交
19 20 21 22 23
	if (r < 0) {
		if (errno == ENODEV) errno = ENXIO;
		return 0;
	}
	return strncpy(name, ifr.ifr_name, IF_NAMESIZE);
24
}