diff --git a/include/net/if.h b/include/net/if.h index 32fb49c..6385acf 100644 --- a/include/net/if.h +++ b/include/net/if.h @@ -218,6 +218,10 @@ struct sockaddr_hw { unsigned char shw_addr[8]; /* address */ }; +/* Convert an interface name to an index, and vice versa. */ +extern unsigned short if_nametoindex (const char *__ifname) __THROW; +extern char *if_indextoname (unsigned short __ifindex, char __ifname[IF_NAMESIZE]) __THROW; + __END_DECLS #endif /* _NET_IF_H */ diff --git a/socket/SRCFILES b/socket/SRCFILES index c1f3da4..0e1de76 100644 --- a/socket/SRCFILES +++ b/socket/SRCFILES @@ -38,6 +38,8 @@ SRCFILES = \ hstrerror.c \ hton.c \ ident_sock.c \ + if_indextoname.c \ + if_nametoindex.c \ inet_addr.c \ inet_lnaof.c \ inet_makeaddr.c \ diff --git a/socket/if_indextoname.c b/socket/if_indextoname.c new file mode 100644 index 0000000..63d125f --- /dev/null +++ b/socket/if_indextoname.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +__typeof__(if_indextoname) __if_indextoname; + +char * +__if_indextoname(unsigned short index, char name[IF_NAMESIZE]) +{ + struct ifreq ifr; + int fd, r; + + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ + __set_errno (ENXIO); + return NULL; + } + ifr.ifr_ifindex = index; + r = ioctl(fd, SIOCGIFNAME_ETH, &ifr); + close(fd); + if(!r){ + return strncpy(name, ifr.ifr_name, IF_NAMESIZE); + } + __set_errno (ENXIO); + return NULL; +} +weak_alias (__if_indextoname, if_indextoname) \ No newline at end of file diff --git a/socket/if_nametoindex.c b/socket/if_nametoindex.c new file mode 100644 index 0000000..6141f63 --- /dev/null +++ b/socket/if_nametoindex.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +__typeof__(if_nametoindex) __if_nametoindex; + +unsigned short +if_nametoindex(const char *name) +{ + struct ifreq ifr; + int fd, r; + + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + __set_errno (ENODEV); + return 0; + } + strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name); + r = ioctl(fd, SIOCGIFINDEX, &ifr); + close(fd); + if(!r){ + return ifr.ifr_ifindex; + } + __set_errno (ENODEV); + return 0; +} +weak_alias (__if_nametoindex, if_nametoindex) \ No newline at end of file