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
if_indextoname(unsigned index,char * name)9 char *if_indextoname(unsigned index, char *name)
10 {
11 struct ifreq ifr;
12 int fd, r;
13
14 #ifdef __LITEOS_A__
15 if ((fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0;
16 #else
17 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0;
18 #endif
19 ifr.ifr_ifindex = index;
20 r = ioctl(fd, SIOCGIFNAME, &ifr);
21 __syscall(SYS_close, fd);
22 if (r < 0) {
23 if (errno == ENODEV) errno = ENXIO;
24 return 0;
25 }
26 return strncpy(name, ifr.ifr_name, IF_NAMESIZE);
27 }
28