1570af302Sopenharmony_ci#define _GNU_SOURCE 2570af302Sopenharmony_ci#include <net/if.h> 3570af302Sopenharmony_ci#include <sys/socket.h> 4570af302Sopenharmony_ci#include <sys/ioctl.h> 5570af302Sopenharmony_ci#include <string.h> 6570af302Sopenharmony_ci#include "syscall.h" 7570af302Sopenharmony_ci 8570af302Sopenharmony_ciunsigned if_nametoindex(const char *name) 9570af302Sopenharmony_ci{ 10570af302Sopenharmony_ci struct ifreq ifr; 11570af302Sopenharmony_ci int fd, r; 12570af302Sopenharmony_ci#ifdef __LITEOS_A__ 13570af302Sopenharmony_ci if ((fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; 14570af302Sopenharmony_ci#else 15570af302Sopenharmony_ci if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; 16570af302Sopenharmony_ci#endif 17570af302Sopenharmony_ci strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 18570af302Sopenharmony_ci r = ioctl(fd, SIOCGIFINDEX, &ifr); 19570af302Sopenharmony_ci __syscall(SYS_close, fd); 20570af302Sopenharmony_ci return r < 0 ? 0 : ifr.ifr_ifindex; 21570af302Sopenharmony_ci} 22