diff options
author | Rich Felker <dalias@aerifal.cx> | 2011-04-05 13:20:08 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2011-04-05 13:20:08 -0400 |
commit | 92b2eb8d0331abcf7afbddeca80ebdcf136d8b3d (patch) | |
tree | 6b5a787b564328033b614decbcd4a17f3562346a | |
parent | ef8b4b1aa68608d7dc65cad41f19b85ec0d3e7af (diff) | |
download | musl-92b2eb8d0331abcf7afbddeca80ebdcf136d8b3d.tar.gz musl-92b2eb8d0331abcf7afbddeca80ebdcf136d8b3d.tar.xz musl-92b2eb8d0331abcf7afbddeca80ebdcf136d8b3d.zip |
implement if_indextoname and if_nametoindex functions
-rw-r--r-- | src/network/if_indextoname.c | 18 | ||||
-rw-r--r-- | src/network/if_nametoindex.c | 18 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/network/if_indextoname.c b/src/network/if_indextoname.c new file mode 100644 index 00000000..f18f17a6 --- /dev/null +++ b/src/network/if_indextoname.c @@ -0,0 +1,18 @@ +#define _GNU_SOURCE +#include <net/if.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <string.h> +#include "syscall.h" + +char *if_indextoname(unsigned index, char *name) +{ + struct ifreq ifr; + int fd, r; + + if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) return 0; + ifr.ifr_ifindex = index; + r = ioctl(fd, SIOCGIFNAME, &ifr); + __syscall(SYS_close, fd); + return r < 0 ? 0 : strncpy(name, ifr.ifr_name, IF_NAMESIZE); +} diff --git a/src/network/if_nametoindex.c b/src/network/if_nametoindex.c new file mode 100644 index 00000000..419931f9 --- /dev/null +++ b/src/network/if_nametoindex.c @@ -0,0 +1,18 @@ +#define _GNU_SOURCE +#include <net/if.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <string.h> +#include "syscall.h" + +unsigned if_nametoindex(const char *name) +{ + struct ifreq ifr; + int fd, r; + + if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) return -1; + strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); + r = ioctl(fd, SIOCGIFINDEX, &ifr); + __syscall(SYS_close, fd); + return r < 0 ? r : ifr.ifr_ifindex; +} |