1#define _GNU_SOURCE
2#include <net/if.h>
3#include <sys/socket.h>
4#include <sys/ioctl.h>
5#include <string.h>
6#include <errno.h>
7#include "syscall.h"
8
9char *if_indextoname(unsigned index, char *name)
10{
11	struct ifreq ifr;
12	int fd, r;
13
14	if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0;
15	ifr.ifr_ifindex = index;
16	r = ioctl(fd, SIOCGIFNAME, &ifr);
17	__syscall(SYS_close, fd);
18	if (r < 0) {
19		if (errno == ENODEV) errno = ENXIO;
20		return 0;
21	}
22	return strncpy(name, ifr.ifr_name, IF_NAMESIZE);
23}
24