File: drivers.c

package info (click to toggle)
wmnet 1.05-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 172 kB
  • ctags: 592
  • sloc: ansic: 1,854; makefile: 501
file content (537 lines) | stat: -rw-r--r-- 15,707 bytes parent folder | download | duplicates (4)
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
530
531
532
533
534
535
536
537
/*  wmnet -- X IP accounting monitor
 *  Copyright 1998 Jesse B. Off  <joff@iastate.edu>
 *
 *  $Id: drivers.c,v 1.1 1998/10/07 03:42:21 joff Exp joff $
 *  
 *  This software is released under the GNU Public License agreement.
 *  No warranties, whatever.... you know the usuals.... this is free
 *  software.  if you use it, great... if you wanna make a change to it,
 *  great, but please send me the diff.  
 */

#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<X11/Xlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<unistd.h>
#include"config.h"


/* For FreeBSD */
#ifdef USE_KVM
#include<net/if.h>
#include<kvm.h>
#include<nlist.h>

kvm_t *kvmfd;
struct nlist symbols[] = {
 { "_ifnet" },
 { NULL }
};
unsigned long ifnet_savedaddr;
int kvm_test(void);
int kvm_updateStats(void);
#endif /* USE_KVM */


#ifdef USE_LINUX_PPP
#include<net/ppp_defs.h>

#ifdef linux_libc5
# include<linux/if_ppp.h>
#else
# include<net/if_ppp.h>
#endif

#include<sys/ioctl.h>
int pppfd;
int ppp_test(void);
int updateStats_ppp(void);
static struct ifpppstatsreq ppp_stats_req;
#endif

#define ACCOUNT_IN_FOUND        1
#define ACCOUNT_OUT_FOUND       2

extern char buffer[256];
extern char *in_rule_string, *out_rule_string, *device;
extern unsigned long totalbytes_in, totalbytes_out, lastbytes_in, lastbytes_out;
extern unsigned long totalpackets_in, totalpackets_out, lastpackets_in, lastpackets_out;
extern unsigned int diffpackets_in, diffpackets_out, diffbytes_in, diffbytes_out;
extern unsigned int out_rule, in_rule;  /* number of rule in /proc/net/ip_acct to use */
extern Bool current_tx, current_rx, rx, tx;



char *available_drivers(void);

#ifdef USE_IPFWADM
int updateStats_ipfwadm(void);
int ipfwadm_test(void);
#endif
#ifdef USE_IPCHAINS
int updateStats_ipchains(void);
int ipchains_test(void);
#endif
#ifdef USE_2_1_DEV
int updateStats_dev(void);
int dev_test(void);
#endif


typedef int (*parser_func)(void);
static struct drivers_struct {
 char * name;
 parser_func function;
 parser_func test;
} drivers[] = {
#ifdef USE_2_1_DEV
 {"devstats", updateStats_dev, dev_test},
#endif
#ifdef USE_IPFWADM
 {"ipfwadm", updateStats_ipfwadm, ipfwadm_test},
#endif
#ifdef USE_IPCHAINS
 {"ipchains", updateStats_ipchains, ipchains_test},
#endif
#ifdef USE_LINUX_PPP
 {"pppstats",updateStats_ppp, ppp_test},
#endif
#ifdef USE_KVM
 {"kmem",kvm_updateStats, kvm_test},
#endif
 {NULL, NULL}
};

char* available_drivers(void) {
 int ind = 0;
 int len = 0;
 char *string, *ptr;
 while(drivers[ind].name != NULL) {
  len += strlen(drivers[ind].name) + 1;
  ind++;
 }
 ptr = string = (char *)malloc(len);
 *string = '\0';
 ind = 0;
 while(drivers[ind].name != NULL) {
  strcpy(string, drivers[ind].name);
  string += strlen(drivers[ind].name);
  *string++ = ',';
  ind++;
 }
 *(--string) = '\0';
 return ptr; 
} 
   


parser_func find_driver(void) {
 int ind = 0;
 while(drivers[ind].name != NULL) {
  if(drivers[ind].test()) {
   return drivers[ind].function;
  }
  ind++;
 }
 fprintf(stderr, "wmnet: no appropriate stat driver found\n");
 exit(30);
}


parser_func setup_driver(char * parser_name) {
 int ind = 0;
 if (parser_name == NULL) return find_driver(); 
 while(drivers[ind].name != NULL) {
  if(!strcmp(parser_name, drivers[ind].name)) {
    if (drivers[ind].test()) return drivers[ind].function;
    fprintf(stderr, "wmnet: driver %s not appropriate for this machine\n", parser_name);
    exit(18);
  }
  ind++;
 }
 fprintf(stderr, "wmnet: no driver %s\n", parser_name);
 exit(18);
}
  



#ifdef linux
/* All the data gathering is done in here. 
 * Return True if no change to tx/rx.
 * Return False if display will need to be updated.
 */	 
#ifdef USE_IPFWADM
int ipfwadm_test(void) {
  if(open("/proc/net/ip_acct", O_RDONLY) == -1) return False;
  fprintf(stderr, "wmnet: using ipfwadm driver to monitor accounting rules %d and %d\n", in_rule, out_rule);
  return True;
}

int updateStats_ipfwadm(void) {
	FILE *ip_acct;
	unsigned int flag = 0, lineno = 0;
        unsigned int offset = 37;
	char *ptr;
	rx = False;
	tx = False;


	if ((ip_acct = fopen("/proc/net/ip_acct", "r")) == NULL) {
		fprintf(stderr, "wmnet: /proc/net/ip_acct unavailable\n"
				"You either don't have IP accounting compiled in, or this isn't a 2.0 linux kernel.\n");
		exit(4);
	}

	/* IP Accounting Rules for 2.0.x linux kernels*/
	while(flag != (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND) && fgets(buffer, 256, ip_acct)) {
		switch(lineno == out_rule ? ACCOUNT_OUT_FOUND : ( lineno == in_rule ? ACCOUNT_IN_FOUND : -1 ) ) {
			case ACCOUNT_IN_FOUND:
				/* accounting in */
				flag |= ACCOUNT_IN_FOUND;
				while(buffer[offset++] != ' ');
				offset += 18; 
				totalpackets_in = strtoul(&buffer[offset], &ptr, 10);	
				if (totalpackets_in == lastpackets_in) break;
				totalbytes_in = strtoul(ptr, NULL, 10);
				diffpackets_in += totalpackets_in - lastpackets_in;
				diffbytes_in += totalbytes_in - lastbytes_in;
				lastpackets_in = totalpackets_in;
				lastbytes_in = totalbytes_in;
				rx = True; 
				break;
			case ACCOUNT_OUT_FOUND:
				/* accounting out */
				flag |= ACCOUNT_OUT_FOUND;
				while(buffer[offset++] != ' ');
				offset += 18; 
				totalpackets_out = strtoul(&buffer[offset], &ptr, 10);	
				if (totalpackets_out == lastpackets_out) break;
				totalbytes_out = strtoul(ptr, NULL, 10);
				diffpackets_out += totalpackets_out - lastpackets_out;
				diffbytes_out += totalbytes_out - lastbytes_out;
				lastpackets_out = totalpackets_out;
				lastbytes_out = totalbytes_out;
				tx = True; 
				break;
		}
		lineno++;
		offset = 37;
	}
	
	if(flag != (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND)) {
		fprintf(stderr,"wmnet: couldn't find %s accounting rule to monitor in /proc/net/ip_acct\n", 
		  (flag == ACCOUNT_IN_FOUND) ? "the TX" : ((flag == ACCOUNT_OUT_FOUND) ? "the RX" : "a single"));
		exit(4);
	}
	fclose(ip_acct);

	/* return True if no change to tx/rx
	 * return False if display will need to be updated
	 */	 
	return((rx == current_rx) && (tx == current_tx));

}
#endif /* USE_IPFWADM */

#ifdef USE_IPCHAINS
int ipchains_test(void) {
  if (open("/proc/net/ip_fwchains",O_RDONLY) == -1) return False;
  if (in_rule_string == NULL) in_rule_string = "acctin";
  if (out_rule_string == NULL) out_rule_string = "acctout";
  fprintf(stderr, "wmnet: using ipchains driver to monitor chains %s and %s\n", in_rule_string, out_rule_string);
  return True;
}

/* ipchains parser mostly from Bjoern Kriews <bkr@cut.de> */
int updateStats_ipchains(void) {
	FILE *ip_acct;
	unsigned int flag = 0;
	static char name[32];
	unsigned long pack, bytes;
	rx = False;
	tx = False;


	if ((ip_acct = fopen("/proc/net/ip_fwchains", "r")) == NULL) {
		fprintf(stderr, "/proc/net/ip_fwchains does not exist?\n"
				"Do you have IP accounting in your kernel?\n");
		exit(4);
	}

	/* IP Chain Rules for Linux kernel 2_1.x */
	while(flag != (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND) && fgets(buffer, 256, ip_acct)) {
		*name = 0;

		sscanf(buffer, "%30s %*s - %*d %*d %*d %*d %lu %*d %lu",
			name, &pack, &bytes);

				
		if(strcmp(name, in_rule_string) == 0) {
			flag |= ACCOUNT_IN_FOUND;

			totalpackets_in = pack;	
			if (totalpackets_in != lastpackets_in) {
				totalbytes_in = bytes;
				diffpackets_in += totalpackets_in - lastpackets_in;
				diffbytes_in += totalbytes_in - lastbytes_in;
				lastpackets_in = totalpackets_in;
				lastbytes_in = totalbytes_in;
				rx = True; 
			}
			
		} else if (strcmp(name, out_rule_string) == 0) {
			flag |= ACCOUNT_OUT_FOUND;
			
			totalpackets_out = pack;	
			if (totalpackets_out != lastpackets_out) {
				totalbytes_out = bytes;
				diffpackets_out += totalpackets_out - lastpackets_out;
				diffbytes_out += totalbytes_out - lastbytes_out;
				lastpackets_out = totalpackets_out;
				lastbytes_out = totalbytes_out;
				tx = True; 
			}
		}
	}
	
	if(flag != (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND)) {
		fprintf(stderr,"I couldn't find %s IP chain to monitor in /proc/net/ip_fwchains.\n", 
		  (flag == ACCOUNT_IN_FOUND) ? "the TX" : ((flag == ACCOUNT_OUT_FOUND) ? "the RX" : "a single"));
		exit(4);
	}
	fclose(ip_acct);

	/* return True if no change to tx/rx
	 * return False if display will need to be updated
	 */	 
	return((rx == current_rx) && (tx == current_tx));

}
#endif /* USE_IPCHAINS */


#ifdef USE_2_1_DEV

/* nelson@media.mit.edu - simple macro to skip to the next field in a character string.
 * First skip all the non-space characters, then skip all the space characters.
 */
#define NEXTFIELD(p) while (!isspace(*(p))) (p)++; while (isspace(*(p))) (p)++;

int updateStats_dev(void) {
	FILE *dev;
        char *ptr;
	unsigned int flag = 0;
	char *name;
	rx = False;
	tx = False;


	if ((dev = fopen("/proc/net/dev", "r")) == NULL) {
		fprintf(stderr, "/proc/net/dev does not exist?\n"
				"Perhaps we are not running Linux?\n");
		exit(4);
	}
        /* the first two lines we can skip - just text headers */
        fgets(buffer, 256, dev);
        fgets(buffer, 256, dev);

	/* devstats for Linux kernel 2_1.x */
	while(flag != (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND) && fgets(buffer, 256, dev)) {
                /* scan to the first : to find the device name */
		ptr = buffer;
		while(*ptr == ' ') ptr++;
		name = ptr;
                while(*ptr != ':') ptr++;
                *ptr = '\0';                      /* ptr used below! */

		if (!strcmp(name, device)) {
                        /* found the device we are monitoring, parse data. */
                        /* nelson@media.mit.edu - hacked to deal with variable width data */

                        int i;
                        char *bytes_in_str, *packets_in_str, *bytes_out_str, *packets_out_str;
				
			flag = (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND);

                        /* Bytes in is the first field after the :
                         * Note, there may be no whitespace after :
                         */
                        bytes_in_str = ptr + 1;
                        while (isspace(*bytes_in_str))
                            bytes_in_str++;
                        
                        /* Packets in is the next field after bytes in */
                        packets_in_str = bytes_in_str;
                        NEXTFIELD(packets_in_str);
                        
			totalpackets_in = strtoul(packets_in_str, NULL, 10);	
			if (totalpackets_in != lastpackets_in) {
				totalbytes_in = strtoul(bytes_in_str, NULL, 10);
				diffpackets_in += totalpackets_in - lastpackets_in;
				diffbytes_in += totalbytes_in - lastbytes_in;
				lastpackets_in = totalpackets_in;
				lastbytes_in = totalbytes_in;
				rx = True; 
			}
			
                        /* Bytes out is the seventh field after packets in */
                        bytes_out_str = packets_in_str;
                        for (i = 0; i < 7; i++) {
                            NEXTFIELD(bytes_out_str);
                        }
                        
                        /* Packets out is the next field after bytes out */
                        packets_out_str = bytes_out_str;
                        NEXTFIELD(packets_out_str);
			
			totalpackets_out = strtoul(packets_out_str, NULL, 10);	
			if (totalpackets_out != lastpackets_out) {
				totalbytes_out = strtoul(bytes_out_str, NULL, 10);
				diffpackets_out += totalpackets_out - lastpackets_out;
				diffbytes_out += totalbytes_out - lastbytes_out;
				lastpackets_out = totalpackets_out;
				lastbytes_out = totalbytes_out;
				tx = True; 
			}
#ifdef NELSONDEBUG
                        fprintf(stderr, "%lu %lu %lu %lu\n", totalbytes_in, totalpackets_in,
                                totalbytes_out, totalpackets_out);
#endif
		}
	}
	
	fclose(dev);

	/* return True if no change to tx/rx
	 * return False if display will need to be updated
	 */	 
	return((rx == current_rx) && (tx == current_tx));
}

int dev_test(void) {
  int devfd;
  if((devfd = open("/proc/net/dev", O_RDONLY)) == -1) return False;
  read(devfd, buffer, 36);
  if(buffer[35] == '|') return False;
  if(device == NULL) device = "eth0";
  fprintf(stderr, "wmnet: using devstats driver to monitor %s\n", device);
  return True;
}
   
#endif /* USE_2_1_DEV */

#ifdef USE_LINUX_PPP
int ppp_test(void) {
  pppfd = socket(AF_INET, SOCK_DGRAM, 0);
  if(device == NULL) device = "ppp0";
  strncpy(ppp_stats_req.ifr__name, device, 15);
  ppp_stats_req.stats_ptr =(caddr_t) &ppp_stats_req.stats;
  fprintf(stderr, "wmnet: using pppstats driver to monitor %s\n", device);
  return True; 
  
}

int updateStats_ppp(void) {
 if(ioctl(pppfd, SIOCGPPPSTATS, &ppp_stats_req) != 0) {
   return False;
 }
        totalpackets_in = ppp_stats_req.stats.p.ppp_ipackets;
        if (totalpackets_in != lastpackets_in) {
                totalbytes_in = ppp_stats_req.stats.p.ppp_ibytes;
                diffpackets_in += totalpackets_in - lastpackets_in;
                diffbytes_in += totalbytes_in - lastbytes_in;
                lastpackets_in = totalpackets_in;
                lastbytes_in = totalbytes_in;
                rx = True;
        }


        totalpackets_out = ppp_stats_req.stats.p.ppp_opackets;
        if (totalpackets_out != lastpackets_out) {
                totalbytes_out =ppp_stats_req.stats.p.ppp_obytes;
                diffpackets_out += totalpackets_out - lastpackets_out;
                diffbytes_out += totalbytes_out - lastbytes_out;
                lastpackets_out = totalpackets_out;
                lastbytes_out = totalbytes_out;
                tx = True;
        }

	/* return True if no change to tx/rx
	 * return False if display will need to be updated
	 */	 
  return((rx == current_rx) && (tx == current_tx));

}
 

#endif /* USE_LINUX_PPP */


#endif /* linux */

#ifdef USE_KVM
int kvm_test(void) {
  if (((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, buffer)) == NULL) ||
      (kvm_nlist(kvmfd, symbols) < 0) ||
      kvm_read(kvmfd, (unsigned long)symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1 ) return False;
  if(device == NULL) device = "ec0";
  fprintf(stderr, "wmnet: using kmem driver to monitor %s\n", device);
  return True;
}

int kvm_updateStats(void) {
 struct ifnet * ifnet = (struct ifnet *)buffer;
 unsigned long ifnet_addr = ifnet_savedaddr;
 char devname[16];
 int flag = 0;
 while (ifnet_addr && flag != (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND)) {
  kvm_read(kvmfd, ifnet_addr, buffer, sizeof(struct ifnet));
  kvm_read(kvmfd, (unsigned long)ifnet->if_name, devname, 15);
  snprintf(devname, 15, "%s%d", devname, ifnet->if_unit);
  if(!strncmp(devname, device, strlen(device))) { /* we found our struct */
        totalpackets_in =ifnet->if_ipackets;
        if (totalpackets_in != lastpackets_in) {
                totalbytes_in = ifnet->if_ibytes;
                diffpackets_in += totalpackets_in - lastpackets_in;
                diffbytes_in += totalbytes_in - lastbytes_in;
                lastpackets_in = totalpackets_in;
                lastbytes_in = totalbytes_in;
                rx = True;
        }


        totalpackets_out = ifnet->if_opackets;
        if (totalpackets_out != lastpackets_out) {
                totalbytes_out =ifnet->if_obytes;
                diffpackets_out += totalpackets_out - lastpackets_out;
                diffbytes_out += totalbytes_out - lastbytes_out;
                lastpackets_out = totalpackets_out;
                lastbytes_out = totalbytes_out;
                tx = True;
        }
        flag = (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND);

  } else {
        ifnet_addr = (unsigned long)ifnet->if_next;
  }
 }

/* return True if no change to tx/rx
 * return False if display will need to be updated
 */	 
 return((rx == current_rx) && (tx == current_tx));
}


#endif