MCPcopy Create free account
hub / github.com/F-Stack/f-stack / if_nametoindex

Function if_nametoindex

tools/compat/if_nametoindex.c:75–112  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73 */
74
75unsigned int
76if_nametoindex(const char *ifname)
77{
78 int s;
79 struct ifreq ifr;
80 struct ifaddrs *ifaddrs, *ifa;
81 unsigned int ni;
82
83 s = _socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
84 if (s != -1) {
85 memset(&ifr, 0, sizeof(ifr));
86 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
87 if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
88 _close(s);
89 return (ifr.ifr_index);
90 }
91 _close(s);
92 }
93
94 if (getifaddrs(&ifaddrs) < 0)
95 return(0);
96
97 ni = 0;
98
99 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
100 if (ifa->ifa_addr &&
101 ifa->ifa_addr->sa_family == AF_LINK &&
102 strcmp(ifa->ifa_name, ifname) == 0) {
103 ni = LLINDEX((struct sockaddr_dl*)ifa->ifa_addr);
104 break;
105 }
106 }
107
108 freeifaddrs(ifaddrs);
109 if (!ni)
110 errno = ENXIO;
111 return(ni);
112}

Callers 15

mainFunction · 0.85
setdefifFunction · 0.85
mainFunction · 0.85
setnd6defifFunction · 0.85
isnd6defifFunction · 0.85
mainFunction · 0.85
mlx4_pci_probeFunction · 0.85
mlx5_os_pci_probe_pfFunction · 0.85
osdep_iface_index_getFunction · 0.85
osdep_iface_mac_getFunction · 0.85
osdep_iface_index_getFunction · 0.85

Calls 5

memsetFunction · 0.85
getifaddrsFunction · 0.85
strcmpFunction · 0.85
freeifaddrsFunction · 0.85
strlcpyFunction · 0.70

Tested by

no test coverage detected