2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2003-01-04 22:07:24 +00:00
|
|
|
* Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTIFSTAT_ERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2018-05-11 00:01:43 +00:00
|
|
|
#include <sys/queue.h>
|
2003-01-04 22:07:24 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_mib.h>
|
|
|
|
|
2020-07-11 13:56:36 +00:00
|
|
|
#include <stdbool.h>
|
2003-01-04 22:07:24 +00:00
|
|
|
#include <stdlib.h>
|
2004-03-09 11:57:28 +00:00
|
|
|
#include <string.h>
|
2003-01-04 22:07:24 +00:00
|
|
|
#include <err.h>
|
2008-01-12 00:11:26 +00:00
|
|
|
#include <errno.h>
|
2013-02-20 13:47:05 +00:00
|
|
|
#include <fnmatch.h>
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
#include "systat.h"
|
|
|
|
#include "extern.h"
|
|
|
|
#include "convtbl.h"
|
|
|
|
|
2011-10-15 13:20:36 +00:00
|
|
|
/* Column numbers */
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
#define C1 0 /* 0-19 */
|
2006-04-30 04:47:23 +00:00
|
|
|
#define C2 20 /* 20-39 */
|
2003-01-04 22:07:24 +00:00
|
|
|
#define C3 40 /* 40-59 */
|
|
|
|
#define C4 60 /* 60-80 */
|
|
|
|
#define C5 80 /* Used for label positioning. */
|
|
|
|
|
2004-03-09 11:57:28 +00:00
|
|
|
static const int col0 = 0;
|
|
|
|
static const int col1 = C1;
|
|
|
|
static const int col2 = C2;
|
|
|
|
static const int col3 = C3;
|
|
|
|
static const int col4 = C4;
|
|
|
|
static const int col5 = C5;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2006-04-30 04:47:23 +00:00
|
|
|
SLIST_HEAD(, if_stat) curlist;
|
2003-01-04 22:07:24 +00:00
|
|
|
SLIST_HEAD(, if_stat_disp) displist;
|
|
|
|
|
|
|
|
struct if_stat {
|
|
|
|
SLIST_ENTRY(if_stat) link;
|
2020-07-11 13:56:36 +00:00
|
|
|
char display_name[IF_NAMESIZE];
|
|
|
|
char dev_name[IFNAMSIZ]; /* copied from ifmibdata */
|
2003-01-04 22:07:24 +00:00
|
|
|
struct ifmibdata if_mib;
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval tv_lastchanged;
|
2014-09-29 17:38:50 +00:00
|
|
|
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;
|
2003-01-04 22:07:24 +00:00
|
|
|
u_int if_row; /* Index into ifmib sysctl */
|
2014-05-09 16:20:55 +00:00
|
|
|
int if_ypos; /* -1 if not being displayed */
|
2020-07-11 13:56:36 +00:00
|
|
|
bool display;
|
2013-02-20 13:47:05 +00:00
|
|
|
u_int match;
|
2003-01-04 22:07:24 +00:00
|
|
|
};
|
|
|
|
|
2013-02-20 13:47:05 +00:00
|
|
|
extern int curscale;
|
|
|
|
extern char *matchline;
|
|
|
|
extern int showpps;
|
|
|
|
extern int needsort;
|
|
|
|
|
|
|
|
static int needclear = 0;
|
2020-07-11 13:56:36 +00:00
|
|
|
static bool displayall = false;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2020-07-11 13:56:36 +00:00
|
|
|
static void format_device_name(struct if_stat *);
|
|
|
|
static int getifmibdata(const int, struct ifmibdata *);
|
2003-01-04 22:07:24 +00:00
|
|
|
static void sort_interface_list(void);
|
|
|
|
static u_int getifnum(void);
|
2020-07-11 13:56:36 +00:00
|
|
|
static void clearifstat(void);
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
#define IFSTAT_ERR(n, s) do { \
|
2012-02-05 09:17:49 +00:00
|
|
|
putchar('\014'); \
|
2003-01-04 22:07:24 +00:00
|
|
|
closeifstat(wnd); \
|
|
|
|
err((n), (s)); \
|
|
|
|
} while (0)
|
|
|
|
|
2006-04-30 04:26:46 +00:00
|
|
|
#define TOPLINE 3
|
2003-01-04 22:07:24 +00:00
|
|
|
#define TOPLABEL \
|
|
|
|
" Interface Traffic Peak Total"
|
|
|
|
|
2006-04-30 04:26:46 +00:00
|
|
|
#define STARTING_ROW (TOPLINE + 1)
|
|
|
|
#define ROW_SPACING (3)
|
|
|
|
|
2013-02-20 13:47:05 +00:00
|
|
|
#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)
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
#define EMPTY_COLUMN " "
|
|
|
|
#define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
|
|
|
|
|
|
|
|
#define DOPUTRATE(c, r, d) do { \
|
|
|
|
CLEAR_COLUMN(r, c); \
|
2013-02-20 13:47:05 +00:00
|
|
|
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"); \
|
|
|
|
} \
|
2003-01-04 22:07:24 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DOPUTTOTAL(c, r, d) do { \
|
2011-10-15 13:20:36 +00:00
|
|
|
CLEAR_COLUMN((r), (c)); \
|
2013-02-20 13:47:05 +00:00
|
|
|
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)); \
|
|
|
|
} \
|
2003-01-04 22:07:24 +00:00
|
|
|
} 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 { \
|
2020-07-11 13:56:36 +00:00
|
|
|
mvprintw(p->if_ypos, 0, "%s", p->display_name); \
|
2003-01-04 22:07:24 +00:00
|
|
|
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)
|
|
|
|
{
|
2006-04-30 04:26:46 +00:00
|
|
|
return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-02-20 13:47:05 +00:00
|
|
|
|
2003-01-04 22:07:24 +00:00
|
|
|
SLIST_FOREACH(ifp, &curlist, link) {
|
2014-05-09 16:20:55 +00:00
|
|
|
if (ifp->if_ypos < LINES - 3 && ifp->if_ypos != -1)
|
2020-07-11 13:56:36 +00:00
|
|
|
if (!ifp->display || ifp->match == 0) {
|
2014-05-09 16:20:55 +00:00
|
|
|
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);
|
|
|
|
}
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-04-30 04:47:23 +00:00
|
|
|
int
|
2003-01-04 22:07:24 +00:00
|
|
|
initifstat(void)
|
|
|
|
{
|
|
|
|
struct if_stat *p = NULL;
|
2020-07-11 13:56:36 +00:00
|
|
|
u_int n, i;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
n = getifnum();
|
|
|
|
if (n <= 0)
|
2013-02-20 14:19:09 +00:00
|
|
|
return (-1);
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
SLIST_INIT(&curlist);
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2008-01-12 00:11:26 +00:00
|
|
|
p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
|
2003-01-04 22:07:24 +00:00
|
|
|
if (p == NULL)
|
|
|
|
IFSTAT_ERR(1, "out of memory");
|
|
|
|
p->if_row = i+1;
|
2020-07-11 13:56:36 +00:00
|
|
|
if (getifmibdata(p->if_row, &p->if_mib) == -1) {
|
|
|
|
free(p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SLIST_INSERT_HEAD(&curlist, p, link);
|
|
|
|
format_device_name(p);
|
2013-02-20 13:47:05 +00:00
|
|
|
p->match = 1;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2006-04-30 04:47:23 +00:00
|
|
|
/*
|
2003-01-04 22:07:24 +00:00
|
|
|
* Initially, we only display interfaces that have
|
2020-07-11 13:56:36 +00:00
|
|
|
* received some traffic unless display-all is on.
|
2003-01-04 22:07:24 +00:00
|
|
|
*/
|
2020-07-11 13:56:36 +00:00
|
|
|
if (displayall || p->if_mib.ifmd_data.ifi_ibytes != 0)
|
|
|
|
p->display = true;
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort_interface_list();
|
|
|
|
|
2013-02-20 14:19:09 +00:00
|
|
|
return (1);
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fetchifstat(void)
|
|
|
|
{
|
2020-07-11 13:56:36 +00:00
|
|
|
struct if_stat *ifp = NULL, *temp_var;
|
2003-01-04 22:07:24 +00:00
|
|
|
struct timeval tv, new_tv, old_tv;
|
|
|
|
double elapsed = 0.0;
|
2014-09-29 17:38:50 +00:00
|
|
|
uint64_t new_inb, new_outb, old_inb, old_outb = 0;
|
|
|
|
uint64_t new_inp, new_outp, old_inp, old_outp = 0;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2020-07-11 13:56:36 +00:00
|
|
|
SLIST_FOREACH_SAFE(ifp, &curlist, link, temp_var) {
|
2006-04-30 04:47:23 +00:00
|
|
|
/*
|
2003-01-04 22:07:24 +00:00
|
|
|
* Grab a copy of the old input/output values before we
|
|
|
|
* call getifmibdata().
|
|
|
|
*/
|
2006-04-30 04:47:23 +00:00
|
|
|
old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
|
2003-01-04 22:07:24 +00:00
|
|
|
old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
|
2013-02-20 13:47:05 +00:00
|
|
|
old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
|
|
|
|
old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
|
2003-01-04 22:07:24 +00:00
|
|
|
ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
|
|
|
|
|
2012-09-01 14:45:15 +00:00
|
|
|
(void)gettimeofday(&new_tv, NULL);
|
2020-07-11 13:56:36 +00:00
|
|
|
if (getifmibdata(ifp->if_row, &ifp->if_mib) == -1 ) {
|
|
|
|
/* if a device was removed */
|
|
|
|
SLIST_REMOVE(&curlist, ifp, if_stat, link);
|
|
|
|
free(ifp);
|
|
|
|
needsort = 1;
|
|
|
|
clearifstat();
|
|
|
|
} else if (strcmp(ifp->dev_name, ifp->if_mib.ifmd_name) != 0 ) {
|
|
|
|
/* a device was removed and another one was added */
|
|
|
|
format_device_name(ifp);
|
|
|
|
/* clear to the current value for the new device */
|
|
|
|
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;
|
|
|
|
}
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2011-10-15 13:20:36 +00:00
|
|
|
new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
|
|
|
|
new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
|
2013-02-20 13:47:05 +00:00
|
|
|
new_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
|
|
|
|
new_outp = ifp->if_mib.ifmd_data.ifi_opackets;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
/* Display interface if it's received some traffic. */
|
2020-07-11 13:56:36 +00:00
|
|
|
if (!ifp->display && new_inb > 0 && old_inb == 0) {
|
|
|
|
ifp->display = true;
|
2013-02-20 13:47:05 +00:00
|
|
|
needsort = 1;
|
2006-04-30 04:47:23 +00:00
|
|
|
}
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The rest is pretty trivial. Calculate the new values
|
|
|
|
* for our current traffic rates, and while we're there,
|
|
|
|
* see if we have new peak rates.
|
|
|
|
*/
|
2011-10-15 13:20:36 +00:00
|
|
|
old_tv = ifp->tv;
|
|
|
|
timersub(&new_tv, &old_tv, &tv);
|
|
|
|
elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
ifp->if_in_curtraffic = new_inb - old_inb;
|
|
|
|
ifp->if_out_curtraffic = new_outb - old_outb;
|
|
|
|
|
2013-02-20 13:47:05 +00:00
|
|
|
ifp->if_in_curpps = new_inp - old_inp;
|
|
|
|
ifp->if_out_curpps = new_outp - old_outp;
|
|
|
|
|
2003-01-04 22:07:24 +00:00
|
|
|
/*
|
|
|
|
* Rather than divide by the time specified on the comm-
|
|
|
|
* and line, we divide by ``elapsed'' as this is likely
|
|
|
|
* to be more accurate.
|
|
|
|
*/
|
2011-10-15 13:20:36 +00:00
|
|
|
ifp->if_in_curtraffic /= elapsed;
|
|
|
|
ifp->if_out_curtraffic /= elapsed;
|
2013-02-20 13:47:05 +00:00
|
|
|
ifp->if_in_curpps /= elapsed;
|
|
|
|
ifp->if_out_curpps /= elapsed;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-02-20 13:47:05 +00:00
|
|
|
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;
|
|
|
|
|
2003-01-04 22:07:24 +00:00
|
|
|
ifp->tv.tv_sec = new_tv.tv_sec;
|
|
|
|
ifp->tv.tv_usec = new_tv.tv_usec;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-20 13:47:05 +00:00
|
|
|
if (needsort)
|
2003-01-04 22:07:24 +00:00
|
|
|
sort_interface_list();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-04-30 04:47:23 +00:00
|
|
|
/*
|
2003-01-04 22:07:24 +00:00
|
|
|
* We want to right justify our interface names against the first column
|
|
|
|
* (first sixteen or so characters), so we need to do some alignment.
|
2020-07-11 13:56:36 +00:00
|
|
|
* We save original name so that we can find a same spot is take by a
|
|
|
|
* different device.
|
2003-01-04 22:07:24 +00:00
|
|
|
*/
|
|
|
|
static void
|
2020-07-11 13:56:36 +00:00
|
|
|
format_device_name(struct if_stat *ifp)
|
2003-01-04 22:07:24 +00:00
|
|
|
{
|
|
|
|
|
2020-07-11 13:56:36 +00:00
|
|
|
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);
|
|
|
|
}
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 13:47:05 +00:00
|
|
|
static int
|
|
|
|
check_match(const char *ifname)
|
|
|
|
{
|
|
|
|
char *p = matchline, *c, t;
|
|
|
|
int match = 0, mlen;
|
|
|
|
|
2013-02-20 14:19:09 +00:00
|
|
|
if (matchline == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2013-02-20 13:47:05 +00:00
|
|
|
/* Strip leading whitespaces */
|
|
|
|
while (*p == ' ')
|
|
|
|
p ++;
|
|
|
|
|
|
|
|
c = p;
|
|
|
|
while ((mlen = strcspn(c, " ;,")) != 0) {
|
|
|
|
p = c + mlen;
|
|
|
|
t = *p;
|
|
|
|
if (p - c > 0) {
|
|
|
|
*p = '\0';
|
|
|
|
if (fnmatch(c, ifname, FNM_CASEFOLD) == 0) {
|
|
|
|
*p = t;
|
2013-02-20 14:19:09 +00:00
|
|
|
return (1);
|
2013-02-20 13:47:05 +00:00
|
|
|
}
|
|
|
|
*p = t;
|
|
|
|
c = p + strspn(p, " ;,");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c = p + strspn(p, " ;,");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 14:19:09 +00:00
|
|
|
return (match);
|
2013-02-20 13:47:05 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:07:24 +00:00
|
|
|
/*
|
|
|
|
* This function iterates through our list of interfaces, identifying
|
|
|
|
* those that are to be displayed (ifp->display = 1). For each interf-
|
|
|
|
* rface that we're displaying, we generate an appropriate position for
|
|
|
|
* it on the screen (ifp->if_ypos).
|
|
|
|
*
|
|
|
|
* This function is called any time a change is made to an interface's
|
|
|
|
* ``display'' state.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sort_interface_list(void)
|
|
|
|
{
|
|
|
|
struct if_stat *ifp = NULL;
|
2006-04-30 04:47:23 +00:00
|
|
|
u_int y = 0;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
y = STARTING_ROW;
|
|
|
|
SLIST_FOREACH(ifp, &curlist, link) {
|
2013-02-20 13:47:05 +00:00
|
|
|
if (matchline && !check_match(ifp->if_mib.ifmd_name))
|
|
|
|
ifp->match = 0;
|
|
|
|
else
|
|
|
|
ifp->match = 1;
|
|
|
|
if (ifp->display && ifp->match) {
|
2003-01-04 22:07:24 +00:00
|
|
|
ifp->if_ypos = y;
|
|
|
|
y += ROW_SPACING;
|
|
|
|
}
|
2014-05-09 16:20:55 +00:00
|
|
|
else
|
|
|
|
ifp->if_ypos = -1;
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
2013-02-20 13:47:05 +00:00
|
|
|
|
|
|
|
needsort = 0;
|
|
|
|
needclear = 1;
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2004-03-09 11:57:28 +00:00
|
|
|
if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
|
|
|
|
(size_t)0) != 0)
|
2003-01-04 22:07:24 +00:00
|
|
|
IFSTAT_ERR(1, "sysctl error");
|
2013-02-20 14:19:09 +00:00
|
|
|
return (data);
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 13:56:36 +00:00
|
|
|
static int
|
2003-01-04 22:07:24 +00:00
|
|
|
getifmibdata(int row, struct ifmibdata *data)
|
|
|
|
{
|
2020-07-11 13:56:36 +00:00
|
|
|
int ret = 0;
|
2003-01-04 22:07:24 +00:00
|
|
|
size_t datalen = 0;
|
|
|
|
static int name[] = { CTL_NET,
|
|
|
|
PF_LINK,
|
|
|
|
NETLINK_GENERIC,
|
|
|
|
IFMIB_IFDATA,
|
|
|
|
0,
|
|
|
|
IFDATA_GENERAL };
|
|
|
|
datalen = sizeof(*data);
|
|
|
|
name[4] = row;
|
|
|
|
|
2020-07-11 13:56:36 +00:00
|
|
|
ret = sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
|
|
|
|
(size_t)0);
|
|
|
|
if ((ret != 0) && (errno != ENOENT))
|
2003-01-04 22:07:24 +00:00
|
|
|
IFSTAT_ERR(2, "sysctl error getting interface data");
|
2020-07-11 13:56:36 +00:00
|
|
|
|
|
|
|
return (ret);
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmdifstat(const char *cmd, const char *args)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
retval = ifcmd(cmd, args);
|
|
|
|
/* ifcmd() returns 1 on success */
|
|
|
|
if (retval == 1) {
|
2020-07-11 13:56:36 +00:00
|
|
|
if (needclear)
|
|
|
|
clearifstat();
|
|
|
|
}
|
|
|
|
else if (prefix(cmd, "all")) {
|
|
|
|
retval = 1;
|
|
|
|
displayall = true;
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
2013-02-20 14:19:09 +00:00
|
|
|
return (retval);
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
2020-07-11 13:56:36 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
clearifstat(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
showifstat();
|
|
|
|
refresh();
|
|
|
|
werase(wnd);
|
|
|
|
labelifstat();
|
|
|
|
needclear = 0;
|
|
|
|
}
|