+static const char* getifname(int idx) {
+ static char t[256];
+ static struct ifreq ifreq;
+ int fd = -1;
+
+ memset(&ifreq, 0, sizeof(ifreq));
+
+ if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
+ if ((fd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
+ goto fail;
+
+ ifreq.ifr_ifindex = idx;
+ if (ioctl(fd, SIOCGIFNAME, &ifreq) < 0)
+ goto fail;
+
+ close(fd);
+
+ return ifreq.ifr_name;
+
+
+fail:
+ if (fd >= 0)
+ close(fd);
+
+ snprintf(t, sizeof(t), "#%i", idx);
+ return t;
+}
+