#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_mib.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <fnmatch.h>
#include "systat.h"
#include "extern.h"
#include "convtbl.h"
#define C1 0
#define C2 20
#define C3 40
#define C4 60
#define C5 80
static const int col2 = C2;
static const int col3 = C3;
static const int col4 = C4;
static SLIST_HEAD(, if_stat) curlist;
struct if_stat {
SLIST_ENTRY(if_stat) link;
char display_name[IF_NAMESIZE];
char dev_name[IFNAMSIZ];
struct ifmibdata if_mib;
struct timeval tv;
struct timeval tv_lastchanged;
uint64_t if_in_curtraffic;
uint64_t if_out_curtraffic;
uint64_t if_in_traffic_peak;
uint64_t if_out_traffic_peak;
uint64_t if_in_curpps;
uint64_t if_out_curpps;
uint64_t if_in_pps_peak;
uint64_t if_out_pps_peak;
u_int if_row;
int if_ypos;
bool display;
u_int match;
};
static int needclear = 0;
static bool displayall = false;
static void format_device_name(struct if_stat *);
static int getifmibdata(const int, struct ifmibdata *);
static void sort_interface_list(void);
static u_int getifnum(void);
static void clearifstat(void);
#define IFSTAT_ERR(n, s) do { \
putchar('\014'); \
closeifstat(wnd); \
err((n), (s)); \
} while (0)
#define TOPLINE 3
#define TOPLABEL \
" Interface Traffic Peak Total"
#define STARTING_ROW (TOPLINE + 1)
#define ROW_SPACING (3)
#define IN_col2 (showpps ? ifp->if_in_curpps : ifp->if_in_curtraffic)
#define OUT_col2 (showpps ? ifp->if_out_curpps : ifp->if_out_curtraffic)
#define IN_col3 (showpps ? \
ifp->if_in_pps_peak : ifp->if_in_traffic_peak)
#define OUT_col3 (showpps ? \
ifp->if_out_pps_peak : ifp->if_out_traffic_peak)
#define IN_col4 (showpps ? \
ifp->if_mib.ifmd_data.ifi_ipackets : ifp->if_mib.ifmd_data.ifi_ibytes)
#define OUT_col4 (showpps ? \
ifp->if_mib.ifmd_data.ifi_opackets : ifp->if_mib.ifmd_data.ifi_obytes)
#define EMPTY_COLUMN " "
#define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
#define DOPUTRATE(c, r, d) do { \
CLEAR_COLUMN(r, c); \
if (showpps) { \
mvprintw(r, (c), "%10.3f %cp%s ", \
convert(d##_##c, curscale), \
*get_string(d##_##c, curscale), \
"/s"); \
} \
else { \
mvprintw(r, (c), "%10.3f %s%s ", \
convert(d##_##c, curscale), \
get_string(d##_##c, curscale), \
"/s"); \
} \
} while (0)
#define DOPUTTOTAL(c, r, d) do { \
CLEAR_COLUMN((r), (c)); \
if (showpps) { \
mvprintw((r), (c), "%12.3f %cp ", \
convert(d##_##c, SC_AUTO), \
*get_string(d##_##c, SC_AUTO)); \
} \
else { \
mvprintw((r), (c), "%12.3f %s ", \
convert(d##_##c, SC_AUTO), \
get_string(d##_##c, SC_AUTO)); \
} \
} while (0)
#define PUTRATE(c, r) do { \
DOPUTRATE(c, (r), IN); \
DOPUTRATE(c, (r)+1, OUT); \
} while (0)
#define PUTTOTAL(c, r) do { \
DOPUTTOTAL(c, (r), IN); \
DOPUTTOTAL(c, (r)+1, OUT); \
} while (0)
#define PUTNAME(p) do { \
mvprintw(p->if_ypos, 0, "%s", p->display_name); \
mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
} while (0)
WINDOW *
openifstat(void)
{
return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
}
void
closeifstat(WINDOW *w)
{
struct if_stat *node = NULL;
while (!SLIST_EMPTY(&curlist)) {
node = SLIST_FIRST(&curlist);
SLIST_REMOVE_HEAD(&curlist, link);
free(node);
}
if (w != NULL) {
wclear(w);
wrefresh(w);
delwin(w);
}
return;
}
void
labelifstat(void)
{
wmove(wnd, TOPLINE, 0);
wclrtoeol(wnd);
mvprintw(TOPLINE, 0, "%s", TOPLABEL);
return;
}
void
showifstat(void)
{
struct if_stat *ifp = NULL;
SLIST_FOREACH(ifp, &curlist, link) {
if (ifp->if_ypos < LINES - 3 && ifp->if_ypos != -1) {
if (!ifp->display || ifp->match == 0) {
wmove(wnd, ifp->if_ypos, 0);
wclrtoeol(wnd);
wmove(wnd, ifp->if_ypos + 1, 0);
wclrtoeol(wnd);
} else {
PUTNAME(ifp);
PUTRATE(col2, ifp->if_ypos);
PUTRATE(col3, ifp->if_ypos);
PUTTOTAL(col4, ifp->if_ypos);
}
}
}
return;
}
int
initifstat(void)
{
struct if_stat *p = NULL;
u_int n, i;
n = getifnum();
if (n <= 0)
return (-1);
SLIST_INIT(&curlist);
for (i = 0; i < n; i++) {
p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
if (p == NULL)
IFSTAT_ERR(1, "out of memory");
p->if_row = i+1;
if (getifmibdata(p->if_row, &p->if_mib) == -1) {
free(p);
continue;
}
SLIST_INSERT_HEAD(&curlist, p, link);
format_device_name(p);
p->match = 1;
if (displayall || p->if_mib.ifmd_data.ifi_ibytes != 0)
p->display = true;
}
sort_interface_list();
return (1);
}
void
fetchifstat(void)
{
struct if_stat *ifp = NULL, *temp_var;
struct timeval tv, new_tv, old_tv;
double elapsed = 0.0;
uint64_t new_inb, new_outb, old_inb, old_outb = 0;
uint64_t new_inp, new_outp, old_inp, old_outp = 0;
SLIST_FOREACH_SAFE(ifp, &curlist, link, temp_var) {
old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
(void)gettimeofday(&new_tv, NULL);
if (getifmibdata(ifp->if_row, &ifp->if_mib) == -1 ) {
SLIST_REMOVE(&curlist, ifp, if_stat, link);
free(ifp);
needsort = 1;
continue;
} else if (strcmp(ifp->dev_name, ifp->if_mib.ifmd_name) != 0 ) {
format_device_name(ifp);
old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
needsort = 1;
}
new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
new_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
new_outp = ifp->if_mib.ifmd_data.ifi_opackets;
if (!ifp->display && new_inb > 0 && old_inb == 0) {
ifp->display = true;
needsort = 1;
}
old_tv = ifp->tv;
timersub(&new_tv, &old_tv, &tv);
elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
ifp->if_in_curtraffic = new_inb - old_inb;
ifp->if_out_curtraffic = new_outb - old_outb;
ifp->if_in_curpps = new_inp - old_inp;
ifp->if_out_curpps = new_outp - old_outp;
ifp->if_in_curtraffic /= elapsed;
ifp->if_out_curtraffic /= elapsed;
ifp->if_in_curpps /= elapsed;
ifp->if_out_curpps /= elapsed;
if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
if (ifp->if_in_curpps > ifp->if_in_pps_peak)
ifp->if_in_pps_peak = ifp->if_in_curpps;
if (ifp->if_out_curpps > ifp->if_out_pps_peak)
ifp->if_out_pps_peak = ifp->if_out_curpps;
ifp->tv.tv_sec = new_tv.tv_sec;
ifp->tv.tv_usec = new_tv.tv_usec;
}
if (needsort)
sort_interface_list();
return;
}
static void
format_device_name(struct if_stat *ifp)
{
if (ifp != NULL ) {
snprintf(ifp->display_name, IF_NAMESIZE, "%*s", IF_NAMESIZE-1,
ifp->if_mib.ifmd_name);
strcpy(ifp->dev_name, ifp->if_mib.ifmd_name);
}
}
static int
check_match(const char *ifname)
{
char *p = matchline, *ch, t;
int match = 0, mlen;
if (matchline == NULL)
return (0);
while (*p == ' ')
p ++;
ch = p;
while ((mlen = strcspn(ch, " ;,")) != 0) {
p = ch + mlen;
t = *p;
if (p - ch > 0) {
*p = '\0';
if (fnmatch(ch, ifname, FNM_CASEFOLD) == 0) {
*p = t;
return (1);
}
*p = t;
ch = p + strspn(p, " ;,");
}
else {
ch = p + strspn(p, " ;,");
}
}
return (match);
}
void
sort_interface_list(void)
{
struct if_stat *ifp = NULL;
u_int y = 0;
y = STARTING_ROW;
SLIST_FOREACH(ifp, &curlist, link) {
if (matchline && !check_match(ifp->if_mib.ifmd_name))
ifp->match = 0;
else
ifp->match = 1;
if (ifp->display && ifp->match) {
ifp->if_ypos = y;
y += ROW_SPACING;
}
else
ifp->if_ypos = -1;
}
needsort = 0;
needclear = 1;
}
static
unsigned int
getifnum(void)
{
u_int data = 0;
size_t datalen = 0;
static int name[] = { CTL_NET,
PF_LINK,
NETLINK_GENERIC,
IFMIB_SYSTEM,
IFMIB_IFCOUNT };
datalen = sizeof(data);
if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
(size_t)0) != 0)
IFSTAT_ERR(1, "sysctl error");
return (data);
}
static int
getifmibdata(int row, struct ifmibdata *data)
{
int ret = 0;
size_t datalen = 0;
static int name[] = { CTL_NET,
PF_LINK,
NETLINK_GENERIC,
IFMIB_IFDATA,
0,
IFDATA_GENERAL };
datalen = sizeof(*data);
name[4] = row;
ret = sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
(size_t)0);
if ((ret != 0) && (errno != ENOENT))
IFSTAT_ERR(2, "sysctl error getting interface data");
return (ret);
}
int
cmdifstat(const char *cmd, const char *args)
{
int retval = 0;
retval = ifcmd(cmd, args);
if (retval == 1) {
if (needclear)
clearifstat();
}
else if (prefix(cmd, "all")) {
retval = 1;
displayall = true;
}
return (retval);
}
static void
clearifstat(void)
{
showifstat();
refresh();
werase(wnd);
labelifstat();
needclear = 0;
}