⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 监视PC机上所有界面的连接活动
💻 C
字号:
#include "main.h"#define DEBUG 0int just_list=0, inGB=0, inKB=0, mon=0;char *make_string(char *str) { // converts bt->kb->mb->gb	long long num = atoll(str); // for BIG values 	if (inGB)		sprintf(str,"%.2f GB ",num/1024./1024/1024);	else if(inKB)		sprintf(str,"%.2f KB ",num/1024.);	else		sprintf(str,"%.2f MB ",num/1024./1024);	return str;}void trim(char *str) {	char *p = (char *)str;	while (*p==' ') p++;	strcpy(str,p);}void parsebuf(char *str) { // parses the input and leaves only digits	char newstr[40],*p = (char *)str;	int i=0;	for (;isdigit(*p);i++,p++) 		newstr[i]=*p;	newstr[i]='\0';	strcpy(str,newstr);}void readfile(char *buf, char *dev, char *file) { // read info from file	char path[80]="/sys/class/net/";	FILE *fp;	strcat(strcat(strcat(path,dev),"/statistics/"),file); // create filepath	if ((fp=fopen(path,"r"))!=NULL) {		fread(buf,20,1,fp); // read 		fclose(fp);		parsebuf(buf); // parse	} else strcpy(buf,"0");}void newprint(char *str) {	// more ellegant way to obtain data	char dev[16], *p=(char*)str, nstr[100], temp[30];	int i=0;	for (;*p!=':';p++,i++)		dev[i]=*p; // interface name	dev[i]='\0';	if (just_list) {		puts(dev); // list and exit		return;	}	strcat(strcpy(nstr,dev),"\nReceived: "); // concatenate interface, new line and "Received: "	readfile(temp,dev,"rx_bytes"); // Read received	strcat(strcat(nstr,make_string(temp)),"("); // concatenate (with conversion to KB,MB,GB) + open bracket for packets	readfile(temp,dev,"rx_packets"); // Read received packets	strcat(strcat(nstr,temp)," packets)\nSent: "); // concatenate packets, close bracket, new line + Sent: 	readfile(temp,dev,"tx_bytes"); // Read sent	strcat(strcat(nstr,make_string(temp)),"("); // concatenate (with conversion) + open bracket	readfile(temp,dev,"tx_packets"); // Read sent packets	strcat(strcat(nstr,temp)," packets)"); // concatenate packets, close bracket	puts(nstr);}void show_help(void) {	puts("Usage:");	puts("\tconstat [OPTIONS] <interface>");	puts("\tExample:");	puts("\t         constat eth0 -- show info for eth0");	puts("\t         constat ppp  -- show info for interfaces, matching ppp*");	puts("\t         constat all  -- show info for all available interfaces");	puts("\tOptions:");	puts("\t         -a   -- the same as for 'all'");	puts("\t         -g   -- show sent/received in GB");	puts("\t         -h   -- show help and exit (the same as --help)");	puts("\t         -k   -- show sent/received in KB");	puts("\t         -l   -- show the list of available interfaces ");	puts("\t         -m   -- monitors activity (endless 5-second timer)");	puts("\t         -v   -- show version and exit (the same as --version)");	puts("\t--help    - show this help and exit");	puts("\t--version - show version and exit");	}void show_version(void) {	printf("Current version: %s\n",VERSION);}int parse_args(char *dev) { // parse arguments	if (!strcmp(dev,"--version") || !strcmp(dev,"-v")) { // show version info and exit		show_version();		return 0;	}		if (!strcmp(dev,"--help") || !strcmp(dev,"-h")) { // show help and exit		show_help();		return 0;	}		if (!strcmp(dev,"-l")) { // list all, setting just_list (show only devnames) and then set to 'all'		just_list=1;		strcpy(dev,"all");	}		if (!strcmp(dev,"-a")) // show info for all devices		strcpy(dev,"all");		if (!strcmp(dev,"-g")) // show sent/recieved in GB		inGB=1;		if (!strcmp(dev,"-k")) // show sent/revieved in KB		inKB=1;		if (!strcmp(dev,"-m")) 		mon=1;		return 1;}int main(int argc,char *argv[]) {	char dev[15],filename[]="/proc/net/dev",data[257];	FILE *fp;	int i=0, found_flag=0, cont;	if (argc==1) { // if interface is not specified		if (DEBUG) { // may be useful for debugging			puts("Choose interface ('all' to list all data): "); gets(dev);		} else {			show_help();			return 0;		}	} else	strcpy(dev,argv[1]);	cont = parse_args(dev);	if (argc>2) { // more than one argument specified		if (strncmp(dev,"-",1)) {			show_help();			return 0;		}		strcpy(dev,argv[2]);	}		if (!cont) return 0;	if (mon) { // monitoring, starts an endless timer that shows info for the specified device		strcat(strcat(argv[0]," "),dev);		while(1) {			system("clear");			system(argv[0]);			sleep(5);						}	}	if ((fp = fopen(filename,"r"))!=NULL) { // opening /proc/net/dev		for (;i<2;i++) fgets(data,256,fp); //scroll 2 first lines		while(fgets(data,256,fp)) { // iterating over the file to find what we need			trim(data);	// remove whitespaces from the beginning of the line			if (!strcmp(dev,"all")) 				newprint(data);  // if we chose 'all' then we print info for every device			else 				if (!strncmp(data,dev,strlen(dev)))					newprint(data); // otherwise we compare the device and decide whether to show it or not			if (!strcmp(dev,"all") || !strncmp(data,dev,strlen(dev))) 				found_flag=1; // if we have found what we need, set the flag to 1		}		if(!found_flag) puts("Error: interface not found"); // if nothing found - error		fclose(fp);	} else puts("Error: failed to obtain data"); // failed to open	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -