1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
--- ksysguard/ksysguardd/FreeBSD/netdev.c.orig Tue Nov 5 02:41:09 2002
+++ ksysguard/ksysguardd/FreeBSD/netdev.c Tue Nov 26 11:56:27 2002
@@ -1,7 +1,7 @@
/*
KSysGuard, the KDE System Guard
- Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
+ Copyright (c) 2001 Tobias Koenig <tokoe82@yahoo.de>
This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General Public
@@ -16,304 +16,269 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: patch-df,v 1.6 2003/01/02 09:12:35 skrll Exp $
+ $Id: patch-df,v 1.6 2003/01/02 09:12:35 skrll Exp $
*/
-#include <sys/types.h>
+#include <sys/types.h> /* for sys/socket.h, ifaddrs.h */
+#include <ifaddrs.h>
#include <sys/socket.h>
+#include <sys/param.h> /* for sys/sysctl.h */
#include <sys/sysctl.h>
-#include <sys/time.h>
-
+#include <net/route.h>
#include <net/if.h>
-#include <net/if_mib.h>
-
-#include <fcntl.h>
-#include <stdio.h>
+#include <net/if_dl.h>
+#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include "Command.h"
#include "ksysguardd.h"
#include "netdev.h"
+#define I_bytes 0
+#define I_packs 1
+#define I_errs 2
+#define I_mcasts 3
+#define I_lost 4
typedef struct {
char name[32];
- u_long recBytes;
- u_long recPacks;
- u_long recErrs;
- u_long recDrop;
- u_long recMulticast;
- u_long sentBytes;
- u_long sentPacks;
- u_long sentErrs;
- u_long sentMulticast;
- u_long sentColls;
+ u_long recv[5],Drecv[5],sent[5],Dsent[5];
} NetDevInfo;
+#define LEN(X) (sizeof(X)/sizeof(X[0]))
+
#define MAXNETDEVS 64
-static NetDevInfo NetDevs[MAXNETDEVS];
+static NetDevInfo NetDevs[MAXNETDEVS], newval[MAXNETDEVS];
static int NetDevCnt = 0;
-char **parseCommand(const char *cmd)
-{
- char tmp_cmd[1024];
- char *begin;
- static char sensor[100];
- static char interface[100];
- static char *retval[2];
-
- strncpy(tmp_cmd, cmd, 1024);
-
- begin = rindex(tmp_cmd, '/');
- *begin = '\0';
- begin++;
- strcpy(sensor, begin);
- retval[1] = sensor;
-
- begin = rindex(tmp_cmd, '/');
- *begin = '\0';
- begin = rindex(tmp_cmd, '/');
- begin++;
- strcpy(interface, begin);
- retval[0] = interface;
- return retval;
-}
-
-int numActivIfaces(void)
-{
- int counter = 0;
- int name[6];
- int num_iface, i;
+/* Read the system's traffic registers.
+ * Merely count the IFs if countp nonzero.
+ * Returns count of IFs read, or -1; the data are written into newval.
+ * Based on getifaddrs source; getifaddrs itself seems to
+ * compile incorrectly, omitting the traffic data. (It also
+ * does things this doesn't need, thus this is slightly more efficient.)
+ */
+static int readSys(int countp) {
size_t len;
- struct ifmibdata ifmd;
-
- len = sizeof(num_iface);
- sysctlbyname("net.link.generic.system.ifcount", &num_iface, &len, NULL, 0);
-
- for (i = 1; i < num_iface + 1; i++) {
- name[0] = CTL_NET;
- name[1] = PF_LINK;
- name[2] = NETLINK_GENERIC;
- name[3] = IFMIB_IFDATA;
- name[4] = i;
- name[5] = IFDATA_GENERAL;
-
- len = sizeof(ifmd);
- sysctl(name, 6, &ifmd, &len, NULL, 0);
- if (ifmd.ifmd_flags & IFF_UP)
- counter++;
+ char *bfr, *ptr;
+ struct rt_msghdr *rtm;
+ NetDevInfo *nv;
+ static int mib[] = {
+ /* see sysctl(3): */
+ CTL_NET,
+ PF_ROUTE,
+ 0, /* `currently always 0' */
+ 0, /* `may be set to 0 to select all address families' */
+ NET_RT_IFLIST,
+ 0 /* ignored but six levels are needed */
+ };
+
+ if (-1==sysctl(mib, LEN(mib), NULL, &len, NULL, 0))
+ return -1;
+ if (!(bfr = malloc(len)))
+ return -1;
+ if (-1==sysctl(mib, LEN(mib), bfr, &len, NULL, 0)) {
+ free(bfr);
+ return -1;
}
+ nv = newval;
+ for (ptr=bfr; ptr<bfr+len; ptr+=rtm->rtm_msglen) {
+ struct if_msghdr *ifm;
+
+ rtm = (void*)ptr; /* chg ptr type to router msg */
+ if (rtm->rtm_version != RTM_VERSION) continue;
+ if (rtm->rtm_type != RTM_IFINFO) continue;
+ ifm = (void*)rtm; /* chg ptr type to interface msg */
+ if (!(ifm->ifm_flags & IFF_UP)) continue;
+
+ if (!countp) {
+ /* a sdl is concat'd to the if msg */
+ struct sockaddr_dl *sdl = (void*)(ifm+1);
+
+ /* copy and terminate the name */
+ /*fixme: check for overruns */
+ memcpy(nv->name, sdl->sdl_data, sdl->sdl_nlen);
+ nv->name[sdl->sdl_nlen] = 0;
+
+ /* copy the data */
+ nv->recv[I_bytes] = ifm->ifm_data.ifi_ibytes;
+ nv->recv[I_packs] = ifm->ifm_data.ifi_ipackets;
+ nv->recv[I_errs] = ifm->ifm_data.ifi_ierrors;
+ nv->recv[I_mcasts] = ifm->ifm_data.ifi_imcasts;
+ nv->recv[I_lost] = ifm->ifm_data.ifi_iqdrops;
+ nv->sent[I_bytes] = ifm->ifm_data.ifi_obytes;
+ nv->sent[I_packs] = ifm->ifm_data.ifi_opackets;
+ nv->sent[I_errs] = ifm->ifm_data.ifi_oerrors;
+ nv->sent[I_mcasts] = ifm->ifm_data.ifi_omcasts;
+ nv->sent[I_lost] = ifm->ifm_data.ifi_collisions;
+ }
- return counter;
+ /*fixme: guard against buffer overrun */
+ nv++;
+ }
+ free(bfr);
+ return nv-newval;
}
+
+
+
/* ------------------------------ public part --------------------------- */
-void initNetDev(void)
-{
- int i;
- char monitor[1024];
+static void prVal(const char*, int);
+void printNetDevRecv(const char *cmd) { prVal(cmd,0); }
+void printNetDevSent(const char *cmd) { prVal(cmd,1); }
+
+static void prInfo(const char*, int);
+void printNetDevRecvInfo(const char *cmd) { prInfo(cmd,0); }
+void printNetDevSentInfo(const char *cmd) { prInfo(cmd,1); }
+
+static struct {
+ char *label;
+ cmdExecutor read, inform;
+ struct {
+ char *label, *info;
+ int index;
+ } op[5];
+} opTable[] = {
+ {"receiver",
+ printNetDevRecv, printNetDevRecvInfo,
+ {{"data", "Received Data\t0\t0\tB/s\n", I_bytes},
+ {"packets", "Received Packets\t0\t0\tHz\n", I_packs},
+ {"errors", "Receiver Errors\t0\t0\tHz\n", I_errs},
+ {"multicast", "Received Multicast Packets\t0\t0\tHz\n", I_mcasts},
+ {"drops", "Receiver Drops\t0\t0\tHz\n", I_lost}}},
+ {"transmitter",
+ printNetDevSent, printNetDevSentInfo,
+ {{"data", "Sent Data\t0\t0\tB/s\n", I_bytes},
+ {"packets", "Sent Packets\t0\t0\tHz\n", I_packs},
+ {"errors", "Transmitter Errors\t0\t0\tHz\n", I_errs},
+ {"multicast", "Sent Multicast Packets\t0\t0\tHz\n", I_mcasts},
+ {"collisions", "Transmitter Collisions\t0\t0\tHz\n", I_lost}}}
+};
+
+
+static void prVal(const char *cmd, int N) {
+ char *p, *q, *r;
+ int i, d;
- updateNetDev();
-
- for (i = 0; i < NetDevCnt; i++) {
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/data", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/packets", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/errors", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/drops", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/multicast", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo);
-
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/data", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/packets", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/errors", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/multicast", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/collisions", NetDevs[i].name);
- registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo);
- }
+ if (!(p=rindex(cmd, '/')))
+ return;
+ *p=0;
+ q=rindex(cmd, '/');
+ *q=0;
+ r=rindex(cmd, '/');
+ r++;
+ for (d=NetDevCnt; d--; )
+ if (!strcmp(r, NetDevs[d].name))
+ break;
+ *q=*p='/';
+
+ if (-1 == d) return;
+
+ p++;
+ for (i=0; i<LEN(opTable[0].op); i++)
+ if (!strcmp(p, opTable[N].op[i].label))
+ fprintf(CurrentClient, "%lu",
+ /*fixme: ugly and presumptuous */
+ (N?NetDevs[d].Dsent:NetDevs[d].Drecv)[opTable[N].op[i].index]);
+ fprintf(CurrentClient, "\n");
}
-void exitNetDev(void)
-{
- int i;
- char monitor[1024];
- for (i = 0; i < NetDevCnt; i++) {
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/data", NetDevs[i].name);
- removeMonitor(monitor);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/packets", NetDevs[i].name);
- removeMonitor(monitor);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/errors", NetDevs[i].name);
- removeMonitor(monitor);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/drops", NetDevs[i].name);
- removeMonitor(monitor);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/receiver/multicast", NetDevs[i].name);
- removeMonitor(monitor);
-
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/data", NetDevs[i].name);
- removeMonitor(monitor);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/packets", NetDevs[i].name);
- removeMonitor(monitor);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/errors", NetDevs[i].name);
- removeMonitor(monitor);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/multicast", NetDevs[i].name);
- removeMonitor(monitor);
- snprintf(monitor, sizeof(monitor), "network/interfaces/%s/transmitter/collisions", NetDevs[i].name);
- removeMonitor(monitor);
- }
-}
+static void prInfo(const char *cmd, int N) {
+ char *p, *q;
+ int i;
-void updateNetDev(void)
-{
- int name[6];
- int num_iface, i;
- size_t len;
- struct ifmibdata ifmd;
+ if (!(p=rindex(cmd, '/'))) return;
+ p++;
- len = sizeof(num_iface);
- sysctlbyname("net.link.generic.system.ifcount", &num_iface, &len, NULL, 0);
+ q = p+strlen(p)-1;
+ if ('?' != *q) return;
+ *q=0;
+
+ for (i=0; i<LEN(opTable[0].op); i++)
+ if (!strcmp(p, opTable[N].op[i].label))
+ fputs(opTable[N].op[i].info, CurrentClient);
- NetDevCnt = 0;
- for (i = 1; i < num_iface + 1; i++) {
- name[0] = CTL_NET;
- name[1] = PF_LINK;
- name[2] = NETLINK_GENERIC;
- name[3] = IFMIB_IFDATA;
- name[4] = i;
- name[5] = IFDATA_GENERAL;
-
- len = sizeof(ifmd);
- sysctl(name, 6, &ifmd, &len, NULL, 0);
- if (ifmd.ifmd_flags & IFF_UP) {
- strncpy(NetDevs[NetDevCnt].name, ifmd.ifmd_name, 32);
- NetDevs[NetDevCnt].recBytes = ifmd.ifmd_data.ifi_ibytes - NetDevs[NetDevCnt].recBytes;
- NetDevs[NetDevCnt].recPacks = ifmd.ifmd_data.ifi_ipackets - NetDevs[NetDevCnt].recPacks;
- NetDevs[NetDevCnt].recErrs = ifmd.ifmd_data.ifi_ierrors - NetDevs[NetDevCnt].recErrs;
- NetDevs[NetDevCnt].recDrop = ifmd.ifmd_data.ifi_iqdrops - NetDevs[NetDevCnt].recDrop;
- NetDevs[NetDevCnt].recMulticast = ifmd.ifmd_data.ifi_imcasts - NetDevs[NetDevCnt].recMulticast;
- NetDevs[NetDevCnt].sentBytes = ifmd.ifmd_data.ifi_obytes - NetDevs[NetDevCnt].sentBytes;
- NetDevs[NetDevCnt].sentPacks = ifmd.ifmd_data.ifi_opackets - NetDevs[NetDevCnt].sentPacks;
- NetDevs[NetDevCnt].sentErrs = ifmd.ifmd_data.ifi_oerrors - NetDevs[NetDevCnt].sentErrs;
- NetDevs[NetDevCnt].sentMulticast = ifmd.ifmd_data.ifi_omcasts - NetDevs[NetDevCnt].sentMulticast;
- NetDevs[NetDevCnt].sentColls = ifmd.ifmd_data.ifi_collisions - NetDevs[NetDevCnt].sentColls;
- NetDevCnt++;
- }
- }
+ *q='?';
}
-void checkNetDev(void)
-{
- if (numActivIfaces() != NetDevCnt) {
- /* interface has been added or removed
- so we do a reset */
- exitNetDev();
- initNetDev();
- }
-}
-void printNetDevRecBytes(const char *cmd)
-{
+static void NDreg(int setp) {
int i;
- char **retval;
-
- retval = parseCommand(cmd);
-
- if (retval[0] == NULL || retval[1] == NULL)
- return;
- for (i = 0; i < NetDevCnt; i++) {
- if (!strcmp(NetDevs[i].name, retval[0])) {
- if (!strncmp(retval[1], "data", 4))
- fprintf(CurrentClient, "%lu", NetDevs[i].recBytes);
- if (!strncmp(retval[1], "packets", 7))
- fprintf(CurrentClient, "%lu", NetDevs[i].recPacks);
- if (!strncmp(retval[1], "errors", 6))
- fprintf(CurrentClient, "%lu", NetDevs[i].recErrs);
- if (!strncmp(retval[1], "drops", 5))
- fprintf(CurrentClient, "%lu", NetDevs[i].recDrop);
- if (!strncmp(retval[1], "multicast", 9))
- fprintf(CurrentClient, "%lu", NetDevs[i].recMulticast);
+ for (i = 0; i<NetDevCnt; i++) {
+ int j;
+
+ for (j=0; j<LEN(opTable); j++) {
+ int k;
+
+ for (k=0; k<LEN(opTable[0].op); k++) {
+ char buffer[1024];
+
+ snprintf(buffer, sizeof(buffer),
+ "network/interfaces/%s/%s/%s",
+ NetDevs[i].name,
+ opTable[j].label,
+ opTable[j].op[k].label);
+
+ /* printf("%d %d %d %s\n",i,j,k,buffer); */
+
+ if (setp)
+ registerMonitor(buffer,
+ "integer",
+ opTable[j].read,
+ opTable[j].inform);
+ else
+ removeMonitor(buffer);
+ }
}
}
-
- fprintf(CurrentClient, "\n");
}
-void printNetDevRecBytesInfo(const char *cmd)
-{
- char **retval;
-
- retval = parseCommand(cmd);
-
- if (retval[1] == NULL)
- return;
+void initNetDev(void) {
+ int i;
+ updateNetDev();
+ for (i=LEN(NetDevs); i--;)
+ strcpy(NetDevs[i].name, newval[i].name);
+ NDreg(!0);
+}
- if (!strncmp(retval[1], "data", 4))
- fprintf(CurrentClient, "Received Data\t0\t0\tkBytes/s\n");
- if (!strncmp(retval[1], "packets", 7))
- fprintf(CurrentClient, "Received Packets\t0\t0\t1/s\n");
- if (!strncmp(retval[1], "errors", 6))
- fprintf(CurrentClient, "Receiver Errors\t0\t0\t1/s\n");
- if (!strncmp(retval[1], "drops", 5))
- fprintf(CurrentClient, "Receiver Drops\t0\t0\t1/s\n");
- if (!strncmp(retval[1], "multicast", 9))
- fprintf(CurrentClient, "Received Multicast Packets\t0\t0\t1/s\n");
+void exitNetDev(void) {
+ NDreg(0);
}
-void printNetDevSentBytes(const char *cmd)
-{
- int i;
- char **retval;
-
- retval = parseCommand(cmd);
-
- if (retval[0] == NULL || retval[1] == NULL)
+void updateNetDev(void) {
+ NetDevInfo *p, *q;
+ int n;
+
+ if (-1==(n = readSys(0)))
return;
- for (i = 0; i < NetDevCnt; i++) {
- if (!strcmp(NetDevs[i].name, retval[0])) {
- if (!strncmp(retval[1], "data", 4))
- fprintf(CurrentClient, "%lu", NetDevs[i].sentBytes);
- if (!strncmp(retval[1], "packets", 7))
- fprintf(CurrentClient, "%lu", NetDevs[i].sentPacks);
- if (!strncmp(retval[1], "errors", 6))
- fprintf(CurrentClient, "%lu", NetDevs[i].sentErrs);
- if (!strncmp(retval[1], "multicast", 9))
- fprintf(CurrentClient, "%lu", NetDevs[i].sentMulticast);
- if (!strncmp(retval[1], "collisions", 10))
- fprintf(CurrentClient, "%lu", NetDevs[i].sentColls);
+ NetDevCnt = n;
+ /*fixme: assumes the interfaces are in the same order each time */
+ for (p=NetDevs, q=newval; n--; p++, q++) {
+ int i;
+ /* calculate deltas */
+ for (i=0; i<5; i++) {
+ p->Drecv[i] = q->recv[i]-p->recv[i];
+ p->recv[i] = q->recv[i];
+ p->Dsent[i] = q->sent[i]-p->sent[i];
+ p->sent[i] = q->sent[i];
}
}
-
- fprintf(CurrentClient, "\n");
}
-void printNetDevSentBytesInfo(const char *cmd)
-{
- char **retval;
-
- retval = parseCommand(cmd);
-
- if (retval[1] == NULL)
- return;
- if (!strncmp(retval[1], "data", 4))
- fprintf(CurrentClient, "Sent Data\t0\t0\tkBytes/s\n");
- if (!strncmp(retval[1], "packets", 7))
- fprintf(CurrentClient, "Sent Packets\t0\t0\t1/s\n");
- if (!strncmp(retval[1], "errors", 6))
- fprintf(CurrentClient, "Transmitter Errors\t0\t0\t1/s\n");
- if (!strncmp(retval[1], "multicast", 9))
- fprintf(CurrentClient, "Sent Multicast Packets\t0\t0\t1/s\n");
- if (!strncmp(retval[1], "collisions", 10))
- fprintf(CurrentClient, "Transmitter Collisions\t0\t0\t1/s\n");
+void checkNetDev(void) {
+ if (readSys(!0) != NetDevCnt) {
+ /* interface has been added or removed
+ so we do a reset */
+ exitNetDev();
+ initNetDev();
+ }
}
+
+
+/* eof */
|