diff options
Diffstat (limited to 'src/network/if_indextoname.c')
-rw-r--r-- | src/network/if_indextoname.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/network/if_indextoname.c b/src/network/if_indextoname.c new file mode 100644 index 0000000..3b368bf --- /dev/null +++ b/src/network/if_indextoname.c @@ -0,0 +1,23 @@ +#define _GNU_SOURCE +#include <net/if.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <string.h> +#include <errno.h> +#include "syscall.h" + +char *if_indextoname(unsigned index, char *name) +{ + struct ifreq ifr; + int fd, r; + + if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; + ifr.ifr_ifindex = index; + r = ioctl(fd, SIOCGIFNAME, &ifr); + __syscall(SYS_close, fd); + if (r < 0) { + if (errno == ENODEV) errno = ENXIO; + return 0; + } + return strncpy(name, ifr.ifr_name, IF_NAMESIZE); +} |