Add descriptions for 2.4 and 2.2.17pre5 netstat variables.
[platform/upstream/net-tools.git] / statistics.c
1 /*
2  * Copyright 1997,1999,2000 Andi Kleen. Subject to the GPL. 
3  * $Id: statistics.c,v 1.12 2000/06/21 23:48:53 ak Exp $
4  * 19980630 - i18n - Arnaldo Carvalho de Melo <acme@conectiva.com.br> 
5  * 19981113 - i18n fixes - Arnaldo Carvalho de Melo <acme@conectiva.com.br> 
6  * 19990101 - added net/netstat, -t, -u, -w supprt - Bernd Eckenfels 
7  */
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "config.h"
13 #include "intl.h"
14
15 /* #define WARN 1 */
16
17 #ifdef WARN
18 #define UFWARN(x) x
19 #else
20 #define UFWARN(x)
21 #endif
22
23 int print_static,f_raw,f_tcp,f_udp;
24
25 enum State {
26     number = 0, opt_number, i_forward, i_inp_icmp, i_outp_icmp, i_rto_alg,
27     MaxState
28 };
29
30 #define normal number
31
32 struct entry {
33     char *title;
34     char *out;
35     enum State type;
36 };
37
38 struct statedesc { 
39     int indent;
40     char *title; 
41 }; 
42
43 struct statedesc states[] = { 
44     [number] = { 4, NULL },
45     [opt_number] = { 4, NULL }, 
46     [i_forward] = { 4, NULL },
47     [i_inp_icmp] = { 8, N_("ICMP input histogram:") },
48     [i_outp_icmp] = { 8, N_("ICMP output histogram:") },
49     [MaxState] = {0},
50 }; 
51
52 static enum State state;
53
54 #define I_STATIC (1<<16)        /* static configuration option. */
55 #define I_TITLE  (1<<17)
56
57 /* 
58  * XXX check against the snmp mib rfc.
59  *
60  * Don't mark the first field as translatable! It's a snmp MIB standard.
61  * - acme
62  */
63 struct entry Iptab[] =
64 {
65     {"Forwarding", N_("Forwarding is %s"), i_forward | I_STATIC},
66     {"DefaultTTL", N_("Default TTL is %u"), number | I_STATIC},
67     {"InReceives", N_("%u total packets received"), number},
68     {"InHdrErrors", N_("%u with invalid headers"), opt_number},
69     {"InAddrErrors", N_("%u with invalid addresses"), opt_number},
70     {"ForwDatagrams", N_("%u forwarded"), number},
71     {"InUnknownProtos", N_("%u with unknown protocol"), opt_number},
72     {"InDiscards", N_("%u incoming packets discarded"), number},
73     {"InDelivers", N_("%u incoming packets delivered"), number},
74     {"OutRequests", N_("%u requests sent out"), number},        /*? */
75     {"OutDiscards", N_("%u outgoing packets dropped"), opt_number},
76     {"OutNoRoutes", N_("%u dropped because of missing route"), opt_number},
77     {"ReasmTimeout", N_("%u fragments dropped after timeout"), opt_number},
78     {"ReasmReqds", N_("%u reassemblies required"), opt_number}, /* ? */
79     {"ReasmOKs", N_("%u packets reassembled ok"), opt_number},
80     {"ReasmFails", N_("%u packet reassembles failed"), opt_number},
81     {"FragOKs", N_("%u fragments received ok"), opt_number},
82     {"FragFails", N_("%u fragments failed"), opt_number},
83     {"FragCreates", N_("%u fragments created"), opt_number}
84 };
85
86 struct entry Icmptab[] =
87 {
88     {"InMsgs", N_("%u ICMP messages received"), number},
89     {"InErrors", N_("%u input ICMP message failed."), number},
90     {"InDestUnreachs", N_("destination unreachable: %u"), i_inp_icmp | I_TITLE},
91     {"InTimeExcds", N_("timeout in transit: %u"), i_inp_icmp | I_TITLE},
92     {"InParmProbs", N_("wrong parameters: %u"), i_inp_icmp | I_TITLE},  /*? */
93     {"InSrcQuenchs", N_("source quenches: %u"), i_inp_icmp | I_TITLE},
94     {"InRedirects", N_("redirects: %u"), i_inp_icmp | I_TITLE},
95     {"InEchos", N_("echo requests: %u"), i_inp_icmp | I_TITLE},
96     {"InEchoReps", N_("echo replies: %u"), i_inp_icmp | I_TITLE},
97     {"InTimestamps", N_("timestamp request: %u"), i_inp_icmp | I_TITLE},
98     {"InTimestampReps", N_("timestamp reply: %u"), i_inp_icmp | I_TITLE},
99     {"InAddrMasks", N_("address mask request: %u"), i_inp_icmp | I_TITLE},      /*? */
100     {"InAddrMaskReps", N_("address mask replies: %u"), i_inp_icmp | I_TITLE},   /*? */
101     {"OutMsgs", N_("%u ICMP messages sent"), number},
102     {"OutErrors", N_("%u ICMP messages failed"), number},
103     {"OutDestUnreachs", N_("destination unreachable: %u"), i_outp_icmp | I_TITLE},
104     {"OutTimeExcds", N_("time exceeded: %u"), i_outp_icmp | I_TITLE},
105     {"OutParmProbs", N_("wrong parameters: %u"), i_outp_icmp | I_TITLE},        /*? */
106     {"OutSrcQuenchs", N_("source quench: %u"), i_outp_icmp | I_TITLE},
107     {"OutRedirects", N_("redirect: %u"), i_outp_icmp | I_TITLE},
108     {"OutEchos", N_("echo request: %u"), i_outp_icmp | I_TITLE},
109     {"OutEchoReps", N_("echo replies: %u"), i_outp_icmp | I_TITLE},
110     {"OutTimestamps", N_("timestamp requests: %u"), i_outp_icmp | I_TITLE},
111     {"OutTimestampReps", N_("timestamp replies: %u"), i_outp_icmp | I_TITLE},
112     {"OutAddrMasks", N_("address mask requests: %u"), i_outp_icmp | I_TITLE},
113     {"OutAddrMaskReps", N_("address mask replies: %u"), i_outp_icmp | I_TITLE},
114 };
115
116 struct entry Tcptab[] =
117 {
118     {"RtoAlgorithm", N_("RTO algorithm is %s"), i_rto_alg | I_STATIC},
119     {"RtoMin", "", number},
120     {"RtoMax", "", number},
121     {"MaxConn", "", number},
122     {"ActiveOpens", N_("%u active connections openings"), number},
123     {"PassiveOpens", N_("%u passive connection openings"), number},
124     {"AttemptFails", N_("%u failed connection attempts"), number},
125     {"EstabResets", N_("%u connection resets received"), number},
126     {"CurrEstab", N_("%u connections established"), number},
127     {"InSegs", N_("%u segments received"), number},
128     {"OutSegs", N_("%u segments send out"), number},
129     {"RetransSegs", N_("%u segments retransmited"), number},
130     {"InErrs", N_("%u bad segments received."), number},
131     {"OutRsts", N_("%u resets sent"), number},
132 };
133
134 struct entry Udptab[] =
135 {
136     {"InDatagrams", N_("%u packets received"), number},
137     {"NoPorts", N_("%u packets to unknown port received."), number},
138     {"InErrors", N_("%u packet receive errors"), number},
139     {"OutDatagrams", N_("%u packets sent"), number},
140 };
141
142 struct entry Tcpexttab[] =
143 {
144     {"SyncookiesSent", N_("%u SYN cookies sent"), opt_number},
145     {"SyncookiesRecv", N_("%u SYN cookies received"), opt_number},
146     {"SyncookiesFailed", N_("%u invalid SYN cookies received"), opt_number},
147
148     { "EmbryonicRsts", N_("%u resets received for embryonic SYN_RECV sockets"),
149       opt_number },  
150     { "PruneCalled", N_("%u packets pruned from receive queue because of socket"
151                         " buffer overrun"), opt_number },  
152     /* obsolete: 2.2.0 doesn't do that anymore */
153     { "RcvPruned", N_("%u packets pruned from receive queue"), opt_number },
154     { "OfoPruned", N_("%u packets dropped from out-of-order queue because of"
155                       " socket buffer overrun"), opt_number }, 
156     { "OutOfWindowIcmps", N_("%u ICMP packets dropped because they were "
157                              "out-of-window"), opt_number }, 
158     { "LockDroppedIcmps", N_("%u ICMP packets dropped because"
159                              " socket was locked"), opt_number },
160     { "TW", N_("%u TCP sockets finished time wait in fast timer"), opt_number },
161     { "TWRecycled", N_("%u time wait sockets recycled by time stamp"), opt_number }, 
162     { "TWKilled", N_("%u TCP sockets finished time wait in slow timer"), opt_number },
163     { "PAWSPassive", N_("%u passive connections rejected because of"
164                         " time stamp"), opt_number },
165     { "PAWSActive", N_("%u active connections rejected because of "
166                        "time stamp"), opt_number },
167     { "PAWSEstab", N_("%u packets rejects in established connections because of"
168                       " timestamp"), opt_number },
169     { "DelayedACKs", N_("%u delayed acks sent"), opt_number },
170     { "DelayedACKLocked", N_("%u delayed acks further delayed because of"
171                              " locked socket"), opt_number },
172     { "DelayedACKLost", N_("Quick ack mode was activated %u times"), opt_number },
173     { "ListenOverflows", N_("%u times the listen queue of a socket overflowed"),
174       opt_number },
175     { "ListenDrops", N_("%u SYNs to LISTEN sockets ignored"), opt_number },
176     { "TCPPrequeued", N_("%u packets directly queued to recvmsg prequeue."), 
177       opt_number },
178     { "TCPDirectCopyFromBacklog", N_("%u packets directly received"
179                                      " from backlog"), opt_number },
180     { "TCPDirectCopyFromPrequeue", N_("%u packets directly received"
181                                       " from prequeue"), opt_number },
182     { "TCPPrequeueDropped", N_("%u packets dropped from prequeue"), opt_number },
183     { "TCPHPHits", N_("%u packets header predicted"), number },
184     { "TCPHPHitsToUser", N_("%u packets header predicted and "
185                             "directly queued to user"), opt_number },
186     { "SockMallocOOM", N_("Ran %u times out of system memory during " 
187                           "packet sending"), opt_number }, 
188 };
189
190 struct tabtab {
191     char *title;
192     struct entry *tab;
193     size_t size;
194     int *flag; 
195 };
196
197 struct tabtab snmptabs[] =
198 {
199     {"Ip", Iptab, sizeof(Iptab), &f_raw},
200     {"Icmp", Icmptab, sizeof(Icmptab), &f_raw},
201     {"Tcp", Tcptab, sizeof(Tcptab), &f_tcp},
202     {"Udp", Udptab, sizeof(Udptab), &f_udp},
203     {"TcpExt", Tcpexttab, sizeof(Tcpexttab), &f_tcp},
204     {NULL}
205 };
206
207 /* XXX IGMP */
208
209 int cmpentries(const void *a, const void *b)
210 {
211     return strcmp(((struct entry *) a)->title, ((struct entry *) b)->title);
212 }
213
214 void printval(struct tabtab *tab, char *title, int val)
215 {
216     struct entry *ent, key;
217     int type;
218     char buf[512];
219
220     key.title = title;
221     ent = bsearch(&key, tab->tab, tab->size / sizeof(struct entry),
222                   sizeof(struct entry), cmpentries);
223     if (!ent) {                 /* try our best */
224         printf("%*s%s: %d\n", states[state].indent, "", title, val);
225         return;
226     }
227     type = ent->type;
228     if (type & I_STATIC) {
229         type &= ~I_STATIC;
230         if (!print_static)
231             return;
232     }
233     if (*ent->out == '\0')
234         return;
235
236     if (type & I_TITLE) {
237         type &= ~I_TITLE;
238         if (state != type)
239             printf("%*s%s\n", states[state].indent, "", _(states[type].title));
240     }
241     buf[0] = '\0';
242     switch (type) {
243     case opt_number:
244         if (val == 0) 
245             break;
246         /*FALL THOUGH*/
247     case number:
248         snprintf(buf, sizeof(buf), _(ent->out), val);
249         break;
250     case i_forward:
251         type = normal;
252         snprintf(buf, sizeof(buf), _(ent->out), val == 2 ? _("enabled") : _("disabled"));
253         break;
254     case i_outp_icmp:
255     case i_inp_icmp:
256         if (val > 0)
257             snprintf(buf, sizeof(buf), _(ent->out), val);
258         break;
259     case i_rto_alg:             /* XXXX */
260         break;
261     default:
262         abort();
263     }
264     if (buf[0])
265         printf("%*s%s\n", states[type].indent, "", buf);
266
267     state = type;
268 }
269
270 struct tabtab *newtable(struct tabtab *tabs, char *title)
271 {
272     struct tabtab *t;
273
274     for (t = tabs; t->title; t++)
275         if (!strcmp(title, t->title)) {
276             if (*(t->flag))
277                 printf("%s:\n", _(title));
278             state = normal;
279             return t;
280         }
281     return NULL;
282 }
283
284
285 void process_fd(FILE *f)
286 {
287     char buf1[512], buf2[512];
288     char *sp, *np, *p;
289     while (fgets(buf1, sizeof buf1, f)) {
290         int endflag;
291         struct tabtab *tab;
292
293         if (!fgets(buf2, sizeof buf2, f))
294             break;
295         sp = strchr(buf1, ':');
296         np = strchr(buf2, ':');
297         if (!np || !sp)
298             goto formaterr;
299         *sp = '\0';
300         tab = newtable(snmptabs, buf1);
301         if (tab == NULL) {
302             UFWARN((printf(_("unknown title %s\n"), buf1)));
303             continue;
304         }
305         np++;
306         sp++;
307
308         endflag = 0;
309         while (!endflag) {
310             sp += strspn(sp, " \t\n"); 
311             np += strspn(np, " \t\n"); 
312             /*if (*np == '\0') goto formaterr; */
313
314             p = sp+strcspn(sp, " \t\n");
315             if (*p == '\0')
316                 endflag = 1;
317             *p = '\0';
318
319             if (*sp != '\0' && *(tab->flag))    
320                 printval(tab, sp, strtoul(np, &np, 10));
321
322             sp = p + 1;
323         }
324     }
325   return;
326   
327 formaterr:
328   perror(_("error parsing /proc/net/snmp"));
329   return;
330 }
331
332
333 void parsesnmp(int flag_raw, int flag_tcp, int flag_udp)
334 {
335     FILE *f;
336
337     f_raw = flag_raw; f_tcp = flag_tcp; f_udp = flag_udp;
338     
339     f = fopen("/proc/net/snmp", "r");
340     if (!f) {
341         perror(_("cannot open /proc/net/snmp"));
342         return;
343     }
344     process_fd(f);
345
346     if (ferror(f))
347         perror("/proc/net/snmp");
348
349     fclose(f);
350
351     f = fopen("/proc/net/netstat", "r");
352
353     if (f) {
354         process_fd(f);
355
356         if (ferror(f))
357             perror("/proc/net/netstat");
358     
359         fclose(f);
360     }
361     return;
362 }
363     
364
365 void inittab(void)
366 {
367     struct tabtab *t;
368
369     /* we sort at runtime because I'm lazy ;) */
370     for (t = snmptabs; t->title; t++)
371         qsort(t->tab, t->size / sizeof(struct entry),
372               sizeof(struct entry), cmpentries);
373 }