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

📄 bcount.c

📁 Nast是一个基于Libnet 和Libpcap的sniffer包和LAN分析器。它可以在通常模式或混合模式下检查通过网络接口的数据包
💻 C
字号:
/*    nast    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include "include/nast.h"void bytecounting (pcap_t *pd); /* pd is pcap_t descriptor to read to */int run_bc (char *dev, char *filter);void ptimecounting();unsigned long ptime; /* parzial time of execution, in seconds */double partial; /* partial traffic */time_t begin; /* save begin time for calculate avarage rate */int run_bc (char *dev, char *filter){   char ebuf[PCAP_ERRBUF_SIZE];   pthread_t pt;   /* 1 to avoid inf */   ptime=1;   begin = time(NULL);   /* open pcap sniffer */   if ((pcap_lookupnet(dev, &netp, &maskp, ebuf))==-1)     {	fprintf (stderr, "pcap_lookupnet error: %s\n\n", ebuf);	return -1;     }   if ((descr = pcap_open_live(dev, BUFSIZ, PROMISC, 10, ebuf))==NULL)     {	fprintf (stderr, "pcap_open_live error: %s\n\n", ebuf);	return -1;     }   if (strcmp (filter, "any")) /* filter!="any" */     {	if ((pcap_compile (descr, &fp, filter, 0, netp))==-1)	  {	     fprintf (stderr, "pcap_compile error\n\n");	     return -1;	  }	if ((pcap_setfilter (descr, &fp))==-1)	  {	     fprintf (stderr, "pcap_setfilter error\n\n");	     return -1;	  }	printf ("Filter \"%s\" has been applied to \"%s\"\n\n", filter, dev);     }   else     printf ("Reading from \"%s\"\n\n", dev);   /* demonize */   if (demonize)     printf ("Is very useless demonize me here! Omit\n\n");   pthread_create (&pt, NULL, (void *) ptimecounting, NULL);   bytecounting (descr);   return 0;}void bytecounting (pcap_t *pd) /* pd is pcap_t descriptor to read to */{   u_short icons;   double total;   double pspeed, tspeed; /* current and total speed */   unsigned long long number;   char *units[] =     {	"B/s", "kB/s", "MB/s", "GB/s"     };   char value[15];   total=0;   icons=0;   number=0;   partial=0;   printf ("Packets\t\tTotal\t\tCurrent speed\t\tAvarage speed\n");   printf ("---------------------------------------------------------------------\n");   while (1)     {	if ((packet = (u_char *) pcap_next (descr, &hdr))!=NULL)	  {	     total+=(double)(hdr.len)/1024; /* sum (Kbytes)*/	     partial+=(double)(hdr.len)/1024;	     number++;	     /* clean line */	     printf ("\r                                                                     \r");	     switch (icons)	       {		case 0: printf ("\\ "); break;		case 1: printf ("| "); break;		case 2: printf ("/ "); break;		case 3: printf ("- "); break;	       }	     if (icons==3) icons=0;	     else icons++;             sprintf (value, "%Ld", number);	     printf (value);	     /* calculate space */	     if (strlen(value) < 6) printf ("\t\t");	     else printf ("\t");	     if (total < 1)	       sprintf (value, "%.0fB", total*1024);	     else if (total < 1024)	       sprintf (value, "%.2fkB", total);	     else if (total < 1024*1024)	       sprintf (value, "%.2fMB", total/1024);	     else	       sprintf (value, "%.2fGB", total/1024*1024);	     printf ("%s", value);	     /* calculate space */	     if (strlen (value) < 8) printf ("\t\t");	     else printf ("\t");	     pspeed = partial/ptime;	     if (pspeed < 1)	       sprintf (value, "%.0f%s", pspeed*1024, units[0]);	     else if (pspeed < 1024)	       sprintf (value, "%.2f%s", pspeed, units[1]);	     else if (pspeed < 1024*1024)	       sprintf (value, "%.2f%s", pspeed/1024, units[2]);	     else	       sprintf (value, "%.2f%s", pspeed/1024*1024, units[3]);	     printf ("%s", value);	     /* calculate space */	     if (strlen (value) < 7) printf ("\t\t\t");	     else  if (strlen (value) < 13) printf ("\t\t");	     else printf ("\t");	     tspeed = total/((int)(time(NULL)-begin));	     if (tspeed < 1)	       sprintf (value, "%.0f%s", tspeed*1024, units[0]);	     else if (tspeed < 1024)	       sprintf (value, "%.2f%s", tspeed, units[1]);	     else if (tspeed < 1024*1024)	       sprintf (value, "%.2f%s", tspeed/1024, units[2]);	     else	       sprintf (value, "%.2f%s", tspeed/1024*1024, units[3]);	     printf ("%s", value);	     fflush (stdout);	  }     }}void ptimecounting(){   for (;;)     {	sleep(1);	if (ptime==10)	  {	     /* refresh every X seconds */	     ptime=1;	     partial=0;	  }	else	  ptime++;     }}

⌨️ 快捷键说明

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