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

Function prepare_ifmap

tools/netstat/common.c:91–139  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89}
90
91struct ifmap_entry *
92prepare_ifmap(size_t *pifmap_size)
93{
94 int ifindex = 0, size;
95 struct ifaddrs *ifap, *ifa;
96 struct sockaddr_dl *sdl;
97
98 struct ifmap_entry *ifmap = NULL;
99 int ifmap_size = 0;
100
101 /*
102 * Retrieve interface list at first
103 * since we need #ifindex -> if_xname match
104 */
105 if (getifaddrs(&ifap) != 0)
106 err(EX_OSERR, "getifaddrs");
107
108 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
109
110 if (ifa->ifa_addr->sa_family != AF_LINK)
111 continue;
112
113 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
114 ifindex = sdl->sdl_index;
115
116 if (ifindex >= ifmap_size) {
117 size = roundup(ifindex + 1, 32) *
118 sizeof(struct ifmap_entry);
119 if ((ifmap = realloc(ifmap, size)) == NULL)
120 errx(2, "realloc(%d) failed", size);
121 memset(&ifmap[ifmap_size], 0,
122 size - ifmap_size *
123 sizeof(struct ifmap_entry));
124
125 ifmap_size = roundup(ifindex + 1, 32);
126 }
127
128 if (*ifmap[ifindex].ifname != '\0')
129 continue;
130
131 strlcpy(ifmap[ifindex].ifname, ifa->ifa_name, IFNAMSIZ);
132 }
133
134 freeifaddrs(ifap);
135
136 *pifmap_size = ifmap_size;
137
138 return (ifmap);
139}
140

Callers 3

nhgrp_printFunction · 0.85
nhops_printFunction · 0.85
p_rtable_sysctlFunction · 0.85

Calls 5

getifaddrsFunction · 0.85
memsetFunction · 0.85
freeifaddrsFunction · 0.85
reallocFunction · 0.50
strlcpyFunction · 0.50

Tested by

no test coverage detected