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
|
/*
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"
#define Rst 0
#define Fin 1
int conn_len(u_long ip_src,u_long ip_dst,u_short s_port,u_short d_port, double len);
struct statistics
{
unsigned long s_ip;
unsigned long d_ip;
unsigned short s_port;
unsigned short d_port;
double tot_len;
int lin;
}
stat_conn[100];
void bytecounting ();
void n_bytecounting (); /*with graphic*/
int run_bc (char *dev, char *filter);
void ptimecounting();
int nconns = 0;
int liness = 13;
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];
if (!dev)
{
w_error(1, "Device is null!\n");
}
#ifdef HAVE_LIBNCURSES
if (graph)
init_scr();
#endif
if (demonize)
n_print ("winfo",1,1,0,"Is very useless demonize me in finding gateway! Omit");
/* 1 to avoid inf */
ptime=1;
begin = time(NULL);
/* open pcap sniffer */
if ((pcap_lookupnet(dev, &netp, &maskp, ebuf))==-1)
{
w_error(1,"pcap_lookupnet error: %s\n\n", ebuf);
}
if ((descr = pcap_open_live(dev, BUFSIZ, PROMISC, 10, ebuf))==NULL)
{
w_error(1, "pcap_open_live error: %s\n\n", ebuf);
}
if ((offset=(device(dev,descr)))==-1) return -1;
if (strcmp (filter, "any") && strcmp (filter, "")) /* filter!="any" */
{
if ((pcap_compile (descr, &fp, filter, 0, netp))==-1)
{
w_error(0, "pcap_compile error\n\n");
return 0;
}
if ((pcap_setfilter (descr, &fp))==-1)
{
w_error(0, "pcap_setfilter error\n\n");
return 0;
}
n_print ("princ",0,1,0,"Filter \"%s\" has been applied to \"%s\"\n\n", filter, dev);
}
else
n_print ("princ",0,1,0,"Reading from \"%s\"\n\n", dev);
pthread_create (&pt[0], NULL, (void *) ptimecounting, NULL);
if(graph)
pthread_create (&pt[1], NULL, (void *) n_bytecounting, NULL);
else
bytecounting() ;
return 0;
}
void bytecounting ()
{
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];
int line;
total=0;
icons=0;
number=0;
partial=0;
line = 4;
n_print (NULL,0,0,0,"Packets\t\tTotal\t\tCurrent speed\t\tAvarage speed\n");
n_print (NULL,0,0,0,"---------------------------------------------------------------------\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);
}
}
pcap_close(descr);
}
void n_bytecounting()
{
u_short icons;
double total;
double tot;
double pspeed, tspeed; /* current and total speed */
unsigned long long number;
char *units[] =
{
"B/s", "kB/s", "MB/s", "GB/s"
};
char value[15];
int line, l;
int tcp,udp,icmp,igmp,arp,rarp,others;
struct libnet_ipv4_hdr *ip;
struct libnet_tcp_hdr *tcph;
u_int16_t type;
int k;
total=tot=0;
icons=0;
number=0;
partial=0;
line = 4;
l = 11;
tcp = udp = icmp = igmp = others = arp = rarp = k = 0;
for(k=0;k<100;k++)
{
memset(&stat_conn[k], 0, sizeof(stat_conn[k]));
}
n_print ("princ",1,1,0,"Packets\t\tTotal\t\tCurrent speed\t\tAvarage speed\n");
n_print ("princ",2,1,0,"-----------------------------------------------------------------------------\n");
n_print ("princ",6,1,0,"ARP RARP ICMP IGMP TCP UDP Others\n");
n_print ("princ",7,1,0,"-----------------------------------------------------------------------------\n");
n_print ("princ",11,1,0,"From Port To Port Total traffic");
n_print("princ",12,1,0,"-----------------------------------------------------------------------------\n");
while(bc_glob!=0)
{
if ((packet = (u_char *) pcap_next (descr, &hdr))!=NULL)
{
total+=(double)(hdr.len)/1024; /* sum (Kbytes)*/
partial+=(double)(hdr.len)/1024;
++number;
tot=(double)(hdr.len)/1024;
type = handle_ethernet (packet);
ip = (struct libnet_ipv4_hdr *) (packet+offset);
tcph = (struct libnet_tcp_hdr *) (packet + LIBNET_IPV4_H + offset);
switch(ip->ip_p)
{
case IPPROTO_TCP:
++tcp;
switch(tcph->th_flags)
{
case TH_SYN:
conn_len(ip->ip_src.s_addr,ip->ip_dst.s_addr,htons(tcph->th_sport),htons(tcph->th_dport),tot);
break;
case TH_ACK:
conn_len(ip->ip_src.s_addr,ip->ip_dst.s_addr,htons(tcph->th_sport),htons(tcph->th_dport),tot);
break;
case TH_RST:
conn_len(ip->ip_src.s_addr,ip->ip_dst.s_addr,htons(tcph->th_sport),htons(tcph->th_dport),tot);
break;
case (TH_SYN|TH_ACK):
conn_len(ip->ip_src.s_addr,ip->ip_dst.s_addr,htons(tcph->th_sport),htons(tcph->th_dport),tot);
break;
case (TH_ACK|TH_PUSH):
conn_len(ip->ip_src.s_addr,ip->ip_dst.s_addr,htons(tcph->th_sport),htons(tcph->th_dport),tot);
break;
case (TH_URG|TH_ACK):
conn_len(ip->ip_src.s_addr,ip->ip_dst.s_addr,htons(tcph->th_sport),htons(tcph->th_dport),tot);
break;
case (TH_FIN|TH_ACK):
conn_len(ip->ip_src.s_addr,ip->ip_dst.s_addr,htons(tcph->th_sport),htons(tcph->th_dport),tot);
break;
case (TH_RST|TH_ACK):
conn_len(ip->ip_src.s_addr,ip->ip_dst.s_addr,htons(tcph->th_sport),htons(tcph->th_dport),tot);
break;
default:
break;
}
break;
case IPPROTO_UDP:
++udp;
break;
case IPPROTO_ICMP:
++icmp;
break;
case IPPROTO_IGMP:
++igmp;
break;
default:
if (type==ETHERTYPE_ARP)
++arp;
else if (type==ETHERTYPE_REVARP)
++rarp;
else ++others;
break;
}
n_print ("princ",8,1,0," ");
n_print ("princ",8,3,0,"%d",arp);
n_print ("princ",8,15,0,"%d",rarp);
n_print ("princ",8,27,0,"%d",icmp);
n_print ("princ",8,39,0,"%d",igmp);
n_print ("princ",8,51,0,"%d",tcp);
n_print ("princ",8,63,0,"%d",udp);
n_print ("princ",8,75,0,"%d",others);
switch (icons)
{
case 0: n_print ("princ",3,1,0,"\\ "); break;
case 1: n_print ("princ",3,1,0,"| "); break;
case 2: n_print ("princ",3,1,0,"/ "); break;
case 3: n_print ("princ",3,1,0,"- "); break;
}
if (icons==3) icons=0;
else icons++;
n_print ("princ",3,1,0," ");
sprintf (value, "%Ld", number);
n_print ("princ",3,3,0,"%s",value);
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);
n_print ("princ",3,24,0,"%s",value);
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]);
n_print ("princ",3,40,0,"%s",value);
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]);
n_print ("princ",3,64,0,"%s",value);
}
}
pcap_close(descr);
}
int conn_len(u_long ip_src,u_long ip_dst,u_short s_port,u_short d_port, double len)
{
int i;
char value[15];
for(i=0;i<100;i++)
if((ip_src==stat_conn[i].s_ip && ip_dst==stat_conn[i].d_ip && s_port==stat_conn[i].s_port && d_port==stat_conn[i].d_port) || (ip_src==stat_conn[i].d_ip && ip_dst==stat_conn[i].s_ip && s_port==stat_conn[i].d_port && d_port==stat_conn[i].s_port))
{
stat_conn[i].tot_len += len;
if (stat_conn[i].tot_len < 1)
sprintf (value, "%.0fB", stat_conn[i].tot_len*1024);
else if (stat_conn[i].tot_len < 1024)
sprintf (value, "%.2fkB", stat_conn[i].tot_len);
else if (stat_conn[i].tot_len < 1024*1024)
sprintf (value, "%.2fMB", stat_conn[i].tot_len/1024);
else
sprintf (value, "%.2fGB", stat_conn[i].tot_len/1024*1024);
n_print("princ",stat_conn[i].lin,60,0,"%s",value);
return(0); /*ce l'ho gi�(duplicato)*/
}
for(i=0;i<100;i++)/*cerco spazio vuoto*/
{
if(stat_conn[i].s_ip)continue;
else
{
stat_conn[i].s_ip = ip_src;
stat_conn[i].d_ip = ip_dst;
stat_conn[i].s_port = s_port;
stat_conn[i].d_port = d_port;
stat_conn[i].tot_len = len;
n_print("princ",liness,1,0,"%s",libnet_addr2name4(stat_conn[i].s_ip, LIBNET_DONT_RESOLVE));
n_print("princ",liness,18,0,"%d",stat_conn[i].s_port);
n_print("princ",liness,29,0,"%s",libnet_addr2name4(stat_conn[i].d_ip, LIBNET_DONT_RESOLVE));
n_print("princ",liness,49,0,"%d",stat_conn[i].d_port);
if (stat_conn[i].tot_len < 1)
sprintf (value, "%.0fB", stat_conn[i].tot_len*1024);
else if (stat_conn[i].tot_len < 1024)
sprintf (value, "%.2fkB", stat_conn[i].tot_len);
else if (stat_conn[i].tot_len < 1024*1024)
sprintf (value, "%.2fMB", stat_conn[i].tot_len/1024);
else
sprintf (value, "%.2fGB", stat_conn[i].tot_len/1024*1024);
n_print("princ",stat_conn[i].lin,60,0,"%s",value);
stat_conn[i].lin=liness;
nconns++;
liness++;
return(1);
}
}
return(0);
}
void ptimecounting()
{
for (;;)
{
sleep(1);
if (ptime==10)
{
/* refresh every X seconds */
ptime=1;
partial=0;
}
else
ptime++;
}
}
|