From 2aa468e93a57dd0f9fbcef52c4b483710fb65438 Mon Sep 17 00:00:00 2001 From: Hajime Tazaki Date: Fri, 14 Apr 2017 17:35:42 +0900 Subject: [PATCH] lkl-upstream: add lkl_ifname_to_ifindex() Signed-off-by: Hajime Tazaki --- tools/lkl/include/lkl.h | 8 ++++++++ tools/lkl/lib/net.c | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/tools/lkl/include/lkl.h b/tools/lkl/include/lkl.h index 7a99a604ab37b5..e54d05a6f6549c 100644 --- a/tools/lkl/include/lkl.h +++ b/tools/lkl/include/lkl.h @@ -363,6 +363,14 @@ int lkl_set_ipv6_gateway(void* addr); int lkl_if_set_ipv6_gateway(int ifindex, void *addr, unsigned int netmask_len, void *gw_addr); +/** + * lkl_ifname_to_ifindex - obtain ifindex of an interface by name + * + * @name - string of an interface + * @returns - return an integer of ifindex if no error + */ +int lkl_ifname_to_ifindex(const char *name); + /** * lkl_netdev - host network device handle, defined in lkl_host.h. */ diff --git a/tools/lkl/lib/net.c b/tools/lkl/lib/net.c index f9faeb57509426..750c97f89d82d4 100644 --- a/tools/lkl/lib/net.c +++ b/tools/lkl/lib/net.c @@ -17,6 +17,24 @@ static inline int ifindex_to_name(int sock, struct lkl_ifreq *ifr, int ifindex) return lkl_sys_ioctl(sock, LKL_SIOCGIFNAME, (long)ifr); } +int lkl_ifname_to_ifindex(const char *name) +{ + struct lkl_ifreq ifr; + int fd, ret; + + fd = lkl_sys_socket(LKL_AF_INET, LKL_SOCK_DGRAM, 0); + if (fd < 0) + return fd; + + strcpy(ifr.lkl_ifr_name, name); + + ret = lkl_sys_ioctl(fd, LKL_SIOCGIFINDEX, (long)&ifr); + if (ret < 0) + return ret; + + return ifr.lkl_ifr_ifindex; +} + int lkl_if_up(int ifindex) { struct lkl_ifreq ifr;