Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/netstat/common.c
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | |||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <sysexits.h> | #include <sysexits.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <err.h> | |||||
#include <libxo/xo.h> | #include <libxo/xo.h> | ||||
#include "netstat.h" | #include "netstat.h" | ||||
#include "common.h" | #include "common.h" | ||||
const char * | const char * | ||||
fmt_flags(const struct bits *p, int f) | fmt_flags(const struct bits *p, int f) | ||||
{ | { | ||||
static char name[33]; | static char name[33]; | ||||
Show All 33 Lines | prepare_ifmap(size_t *pifmap_size) | ||||
struct ifmap_entry *ifmap = NULL; | struct ifmap_entry *ifmap = NULL; | ||||
int ifmap_size = 0; | int ifmap_size = 0; | ||||
/* | /* | ||||
* Retrieve interface list at first | * Retrieve interface list at first | ||||
* since we need #ifindex -> if_xname match | * since we need #ifindex -> if_xname match | ||||
*/ | */ | ||||
if (getifaddrs(&ifap) != 0) | if (getifaddrs(&ifap) != 0) | ||||
err(EX_OSERR, "getifaddrs"); | xo_err(EX_OSERR, "getifaddrs"); | ||||
for (ifa = ifap; ifa; ifa = ifa->ifa_next) { | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { | ||||
if (ifa->ifa_addr->sa_family != AF_LINK) | if (ifa->ifa_addr->sa_family != AF_LINK) | ||||
continue; | continue; | ||||
sdl = (struct sockaddr_dl *)ifa->ifa_addr; | sdl = (struct sockaddr_dl *)ifa->ifa_addr; | ||||
ifindex = sdl->sdl_index; | ifindex = sdl->sdl_index; | ||||
if (ifindex >= ifmap_size) { | if (ifindex >= ifmap_size) { | ||||
size = roundup2(ifindex + 1, 32) * | size = roundup2(ifindex + 1, 32) * | ||||
sizeof(struct ifmap_entry); | sizeof(struct ifmap_entry); | ||||
if ((ifmap = realloc(ifmap, size)) == NULL) | if ((ifmap = realloc(ifmap, size)) == NULL) | ||||
errx(2, "realloc(%d) failed", size); | xo_errx(EX_OSERR, "realloc(%d) failed", size); | ||||
des: `EX_OSERR` | |||||
memset(&ifmap[ifmap_size], 0, | memset(&ifmap[ifmap_size], 0, | ||||
size - ifmap_size * | size - ifmap_size * | ||||
sizeof(struct ifmap_entry)); | sizeof(struct ifmap_entry)); | ||||
ifmap_size = roundup2(ifindex + 1, 32); | ifmap_size = roundup2(ifindex + 1, 32); | ||||
} | } | ||||
if (*ifmap[ifindex].ifname != '\0') | if (*ifmap[ifindex].ifname != '\0') | ||||
Show All 12 Lines |
EX_OSERR