diff options
-rw-r--r-- | plugins/inet.c | 25 | ||||
-rw-r--r-- | plugins/inet.h | 2 |
2 files changed, 27 insertions, 0 deletions
diff --git a/plugins/inet.c b/plugins/inet.c index e3fedfb9..97f338ec 100644 --- a/plugins/inet.c +++ b/plugins/inet.c @@ -35,6 +35,31 @@ #include "inet.h" +int inet_name2index(const char *name) +{ + struct ifreq ifr; + int sk, err; + + if (name == NULL) + return -1; + + sk = socket(PF_INET, SOCK_DGRAM, 0); + if (sk < 0) + return -1; + + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); + + err = ioctl(sk, SIOCGIFINDEX, &ifr); + + close(sk); + + if (err < 0) + return -1; + + return ifr.ifr_ifindex; +} + char *inet_index2name(int index) { struct ifreq ifr; diff --git a/plugins/inet.h b/plugins/inet.h index 3246cc13..23437ac5 100644 --- a/plugins/inet.h +++ b/plugins/inet.h @@ -19,6 +19,8 @@ * */ +int inet_name2index(const char *name); + char *inet_index2name(int index); char *inet_index2ident(int index, const char *prefix); |