Commit be965496 authored by Klaas Freitag's avatar Klaas Freitag

Some cleanups:

- Finish net/netstat support
- Fix a few bugs
- Cleanups
- Print some statistics only when >0
parent b72719fc
/* Copyright '97 by Andi Kleen. Subject to the GPL. */ /* Copyright 1997,1999 by Andi Kleen. Subject to the GPL. */
/* $Id: statistics.c,v 1.10 1999/01/05 20:53:05 philip Exp $ */ /* $Id: statistics.c,v 1.11 1999/01/06 12:05:49 freitag Exp $ */
/* 19980630 - i18n - Arnaldo Carvalho de Melo <acme@conectiva.com.br> */ /* 19980630 - i18n - Arnaldo Carvalho de Melo <acme@conectiva.com.br> */
/* 19981113 - i18n fixes - Arnaldo Carvalho de Melo <acme@conectiva.com.br> */ /* 19981113 - i18n fixes - Arnaldo Carvalho de Melo <acme@conectiva.com.br> */
/* 19990101 - added net/netstat, -t, -u, -w supprt - Bernd Eckenfels */ /* 19990101 - added net/netstat, -t, -u, -w supprt - Bernd Eckenfels */
/*
XXX: add some long-text to TcpExtt
*/
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -24,7 +21,8 @@ ...@@ -24,7 +21,8 @@
int print_static,f_raw,f_tcp,f_udp; int print_static,f_raw,f_tcp,f_udp;
enum State { enum State {
number = 0, i_forward, i_inp_icmp, i_outp_icmp, i_rto_alg number = 0, opt_number, i_forward, i_inp_icmp, i_outp_icmp, i_rto_alg,
MaxState
}; };
#define normal number #define normal number
...@@ -35,49 +33,52 @@ struct entry { ...@@ -35,49 +33,52 @@ struct entry {
enum State type; enum State type;
}; };
struct statedesc {
int indent;
char *title;
};
struct statedesc states[] = {
[number] = { 4, NULL },
[opt_number] = { 4, NULL },
[i_forward] = { 4, NULL },
[i_inp_icmp] = { 8, N_("ICMP input histogram:") },
[i_outp_icmp] = { 8, N_("ICMP output histogram:") },
[MaxState] = {0},
};
static enum State state; static enum State state;
static int indent[] =
{4, 4, 8, 8, 4}; /* for each state */
#define I_STATIC (1<<16) /* static configuration option. */ #define I_STATIC (1<<16) /* static configuration option. */
#define I_TITLE (1<<17) #define I_TITLE (1<<17)
char *titles[] = /*
{ /* for each state */ * XXX check against the snmp mib rfc.
NULL, NULL, *
N_("ICMP input histogram:"), * Don't mark the first field as translatable! It's a snmp MIB standard.
N_("ICMP output histogram:"), * - acme
NULL
};
/* XXX check against the snmp mib rfc.
*/ */
/*
Don't mark the first field as translatable! It's a snmp MIB standard.
- acme
*/
struct entry Iptab[] = struct entry Iptab[] =
{ {
{"Forwarding", N_("Forwarding is %s"), i_forward | I_STATIC}, {"Forwarding", N_("Forwarding is %s"), i_forward | I_STATIC},
{"DefaultTTL", N_("Default TTL is %d"), number | I_STATIC}, {"DefaultTTL", N_("Default TTL is %d"), number | I_STATIC},
{"InReceives", N_("%d total packets received"), number}, {"InReceives", N_("%d total packets received"), number},
{"InHdrErrors", N_("%d with invalid headers"), number}, {"InHdrErrors", N_("%d with invalid headers"), opt_number},
{"InAddrErrors", N_("%d with invalid addresses"), number}, {"InAddrErrors", N_("%d with invalid addresses"), opt_number},
{"ForwDatagrams", N_("%d forwarded"), number}, {"ForwDatagrams", N_("%d forwarded"), number},
{"InUnknownProtos", N_("%d with unknown protocol"), number}, {"InUnknownProtos", N_("%d with unknown protocol"), opt_number},
{"InDiscards", N_("%d incoming packets discarded"), number}, {"InDiscards", N_("%d incoming packets discarded"), number},
{"InDelivers", N_("%d incoming packets delivered"), number}, {"InDelivers", N_("%d incoming packets delivered"), number},
{"OutRequests", N_("%d requests sent out"), number}, /*? */ {"OutRequests", N_("%d requests sent out"), number}, /*? */
{"OutDiscards", N_("%d outgoing packets dropped"), number}, {"OutDiscards", N_("%d outgoing packets dropped"), opt_number},
{"OutNoRoutes", N_("%d dropped because of missing route"), number}, {"OutNoRoutes", N_("%d dropped because of missing route"), opt_number},
{"ReasmTimeout", N_("%d fragments dropped after timeout"), number}, {"ReasmTimeout", N_("%d fragments dropped after timeout"), opt_number},
{"ReasmReqds", N_("%d reassemblies required"), number}, /* ? */ {"ReasmReqds", N_("%d reassemblies required"), opt_number}, /* ? */
{"ReasmOKs", N_("%d packets reassembled ok"), number}, {"ReasmOKs", N_("%d packets reassembled ok"), opt_number},
{"ReasmFails", N_("%d packet reassembles failed"), number}, {"ReasmFails", N_("%d packet reassembles failed"), opt_number},
{"FragOKs", N_("%d fragments received ok"), number}, {"FragOKs", N_("%d fragments received ok"), opt_number},
{"FragFails", N_("%d fragments failed"), number}, {"FragFails", N_("%d fragments failed"), opt_number},
{"FragCreates", N_("%d fragments created"), number} {"FragCreates", N_("%d fragments created"), opt_number}
}; };
struct entry Icmptab[] = struct entry Icmptab[] =
...@@ -116,8 +117,8 @@ struct entry Tcptab[] = ...@@ -116,8 +117,8 @@ struct entry Tcptab[] =
{"RtoMin", "", number}, {"RtoMin", "", number},
{"RtoMax", "", number}, {"RtoMax", "", number},
{"MaxConn", "", number}, {"MaxConn", "", number},
{"ActiveOpens", N_("%d active opens"), number}, {"ActiveOpens", N_("%d active connections openings"), number},
{"PassiveOpens", N_("%d passive opens"), number}, {"PassiveOpens", N_("%d passive connection openings"), number},
{"AttemptFails", N_("%d failed connection attempts"), number}, {"AttemptFails", N_("%d failed connection attempts"), number},
{"EstabResets", N_("%d connection resets received"), number}, {"EstabResets", N_("%d connection resets received"), number},
{"CurrEstab", N_("%d connections established"), number}, {"CurrEstab", N_("%d connections established"), number},
...@@ -133,47 +134,46 @@ struct entry Udptab[] = ...@@ -133,47 +134,46 @@ struct entry Udptab[] =
{"InDatagrams", N_("%d packets received"), number}, {"InDatagrams", N_("%d packets received"), number},
{"NoPorts", N_("%d packets to unknown port received."), number}, {"NoPorts", N_("%d packets to unknown port received."), number},
{"InErrors", N_("%d packet receive errors"), number}, {"InErrors", N_("%d packet receive errors"), number},
{"OutDatagrams", N_("%d packets send"), number}, {"OutDatagrams", N_("%d packets sent"), number},
}; };
struct entry Tcpexttab[] = struct entry Tcpexttab[] =
{ {
{"SyncookiesSent", N_("%d SYN cookies sent"), number}, {"SyncookiesSent", N_("%d SYN cookies sent"), opt_number},
{"SyncookiesRecv", N_("%d SYN cookies received"), number}, {"SyncookiesRecv", N_("%d SYN cookies received"), opt_number},
{"SyncookiesFailed", N_("%d SYN cookies failed"), number}, {"SyncookiesFailed", N_("%d invalid SYN cookies received"), opt_number},
/* XXX */
/* EmbryonicRsts { "EmbryonicRsts", N_("%d resets received for embryonic SYN_RECV sockets"),
PruneCalled opt_number },
RcvPruned { "PruneCalled", N_("%d packets pruned from receive queue because of socket"
OfoPruned " buffer overrun"), opt_number },
OutOfWindowIcmps /* obsolete: 2.2.0 doesn't do that anymore */
LockDroppedIcmps */ { "RcvPruned", N_("%d packets pruned from out-of-order queue"), opt_number },
{ "OfoPruned", N_("%d packets dropped from out-of-order queue because of"
" socket buffer overrun"), opt_number },
{ "OutOfWindowIcmps", N_("%d ICMP packets dropped because they were "
"out-of-window"), opt_number },
{ "LockDroppedIcmps", N_("%d ICMP packets dropped because socket was locked"),
opt_number },
}; };
struct tabtab { struct tabtab {
char *title; char *title;
struct entry *tab; struct entry *tab;
size_t size; size_t size;
int *flag;
}; };
struct tabtab snmptabs[] = struct tabtab snmptabs[] =
{ {
{"Ip", Iptab, sizeof(Iptab)}, {"Ip", Iptab, sizeof(Iptab), &f_raw},
{"Icmp", Icmptab, sizeof(Icmptab)}, {"Icmp", Icmptab, sizeof(Icmptab), &f_raw},
{"Tcp", Tcptab, sizeof(Tcptab)}, {"Tcp", Tcptab, sizeof(Tcptab), &f_tcp},
{"Udp", Udptab, sizeof(Udptab)}, {"Udp", Udptab, sizeof(Udptab), &f_udp},
{"TcpExt", Tcpexttab, sizeof(Tcpexttab)}, {"TcpExt", Tcpexttab, sizeof(Tcpexttab), &f_tcp},
{NULL} {NULL}
}; };
static char *skiptok(char *s)
{
while (!isspace(*s) && *s != '\0')
s++;
return s;
}
/* XXX IGMP */ /* XXX IGMP */
int cmpentries(const void *a, const void *b) int cmpentries(const void *a, const void *b)
...@@ -187,12 +187,11 @@ void printval(struct tabtab *tab, char *title, int val) ...@@ -187,12 +187,11 @@ void printval(struct tabtab *tab, char *title, int val)
int type; int type;
char buf[512]; char buf[512];
/* printf("key: %s value: %d\n",title,val); */
key.title = title; key.title = title;
ent = bsearch(&key, tab->tab, tab->size / sizeof(struct entry), ent = bsearch(&key, tab->tab, tab->size / sizeof(struct entry),
sizeof(struct entry), cmpentries); sizeof(struct entry), cmpentries);
if (!ent) { /* try our best */ if (!ent) { /* try our best */
printf("%*s%s: %d\n", indent[state], "", title, val); printf("%*s%s: %d\n", states[state].indent, "", title, val);
return; return;
} }
type = ent->type; type = ent->type;
...@@ -207,10 +206,14 @@ void printval(struct tabtab *tab, char *title, int val) ...@@ -207,10 +206,14 @@ void printval(struct tabtab *tab, char *title, int val)
if (type & I_TITLE) { if (type & I_TITLE) {
type &= ~I_TITLE; type &= ~I_TITLE;
if (state != type) if (state != type)
printf("%*s%s\n", indent[state], "", _(titles[type])); printf("%*s%s\n", states[state].indent, "", _(states[type].title));
} }
buf[0] = '\0'; buf[0] = '\0';
switch (type) { switch (type) {
case opt_number:
if (val == 0)
break;
/*FALL THOUGH*/
case number: case number:
snprintf(buf, sizeof(buf), _(ent->out), val); snprintf(buf, sizeof(buf), _(ent->out), val);
break; break;
...@@ -229,7 +232,7 @@ void printval(struct tabtab *tab, char *title, int val) ...@@ -229,7 +232,7 @@ void printval(struct tabtab *tab, char *title, int val)
abort(); abort();
} }
if (buf[0]) if (buf[0])
printf("%*s%s\n", indent[type], "", buf); printf("%*s%s\n", states[type].indent, "", buf);
state = type; state = type;
} }
...@@ -240,12 +243,8 @@ struct tabtab *newtable(struct tabtab *tabs, char *title) ...@@ -240,12 +243,8 @@ struct tabtab *newtable(struct tabtab *tabs, char *title)
for (t = tabs; t->title; t++) for (t = tabs; t->title; t++)
if (!strcmp(title, t->title)) { if (!strcmp(title, t->title)) {
if ( if (*(t->flag))
(((t->tab==Iptab) ||(t->tab==Icmptab)) &&f_raw) || printf("%s:\n", _(title));
(((t->tab==Tcptab)||(t->tab==Tcpexttab))&&f_tcp) ||
( (t->tab==Udptab) &&f_udp)
)
printf("%s:\n", _(title));
state = normal; state = normal;
return t; return t;
} }
...@@ -278,27 +277,18 @@ void process_fd(FILE *f) ...@@ -278,27 +277,18 @@ void process_fd(FILE *f)
endflag = 0; endflag = 0;
while (!endflag) { while (!endflag) {
while (isspace(*sp)) sp += strspn(sp, " \t\n");
sp++; np += strspn(np, " \t\n");
while (isspace(*np))
np++;
/*if (*np == '\0') goto formaterr; */ /*if (*np == '\0') goto formaterr; */
p = skiptok(sp); p = sp+strcspn(sp, " \t\n");
if (*p == '\0') if (*p == '\0')
endflag = 1; endflag = 1;
*p = '\0'; *p = '\0';
/* printf("f: %d %d %d %p %p %p %p %p\n",f_raw, f_tcp, f_udp, Iptab, Icmptab, Tcptab, Tcpexttab, Udptab, tab); */ if (*sp != '\0' && *(tab->flag))
if (*sp != '\0') { /* XXX */
if (
(((tab->tab==Iptab) ||(tab->tab==Icmptab)) &&f_raw) ||
(((tab->tab==Tcptab)||(tab->tab==Tcpexttab))&&f_tcp) ||
( (tab->tab==Udptab) &&f_udp)
)
printval(tab, sp, strtoul(np, &np, 10)); printval(tab, sp, strtoul(np, &np, 10));
}
sp = p + 1; sp = p + 1;
} }
} }
...@@ -342,7 +332,7 @@ void parsesnmp(int flag_raw, int flag_tcp, int flag_udp) ...@@ -342,7 +332,7 @@ void parsesnmp(int flag_raw, int flag_tcp, int flag_udp)
} }
void inittab() void inittab(void)
{ {
struct tabtab *t; struct tabtab *t;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment