-/* ===================== traffic analyser - uses iptables ================ */ \r
-\r
-void get_traffic_statistics(void)\r
-{\r
- char *str,*cmd;\r
- int downloadflag=0;\r
-\r
- textfile(Pipe,str) *line,*lines=NULL;\r
- string(str,STRLEN);\r
- string(cmd,STRLEN);\r
-\r
- sprintf(cmd,"%s -L -v -x -n -t mangle",iptables);\r
- shell(cmd);\r
- input(str,STRLEN)\r
- {\r
- create(line,Pipe);\r
- line->str=str;\r
- string(str,STRLEN);\r
- append(line,lines);\r
- }\r
-\r
- for_each(line,lines)\r
- {\r
- int col, accept=0,proxyflag=0,valid=1,setchainname=0,commonflag=0; \r
- unsigned long long traffic=0;\r
- unsigned long pkts=0;\r
- char *ipaddr=NULL,*ptr;\r
- \r
- /* debug puts(line->str); */\r
- valid_columns(ptr,line->str,' ',col) \r
- if(valid) switch(col)\r
- { \r
- case 1: if(eq(ptr,"Chain"))\r
- {\r
- setchainname=1;\r
- }\r
- else if(eq(ptr,"pkts")) \r
- {\r
- valid=0;\r
- }\r
- else\r
- {\r
- sscanf(ptr,"%lu",&pkts); \r
- }\r
- break;\r
- case 2: if(setchainname)\r
- {\r
- if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))\r
- {\r
- downloadflag = 1; \r
- }\r
- else \r
- {\r
- if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))\r
- {\r
- downloadflag = 0;\r
- }\r
- } \r
- if(eq(ptr,"post_common") || eq(ptr,"forw_common"))\r
- {\r
- commonflag = 1;\r
- }\r
- }\r
- else\r
- {\r
- sscanf(ptr,"%Lu",&traffic); \r
- traffic += (1<<19);\r
- traffic >>= 20;\r
- }\r
- break;\r
- case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)\r
- {\r
- accept=eq(ptr,mark);\r
- }\r
- /*if(filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/\r
- break;\r
- case 8: if(downloadflag)\r
- { \r
- if(strstr(proxy_ip,ptr))\r
- {\r
- proxyflag=1; \r
- }\r
- }\r
- else\r
- {\r
- ipaddr=ptr; \r
- }\r
- break;\r
- case 9: if(downloadflag)ipaddr=ptr;break;\r
- }\r
- \r
- if(accept && traffic>0 && ipaddr)\r
- {\r
- if(proxyflag)\r
- {\r
- printf("(proxy) ");\r
- }\r
- else if(!downloadflag)\r
- {\r
- printf("(upload) ");\r
- }\r
- printf("IP %s: %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);\r
-\r
- if_exists(ip,ips,eq(ip->addr,ipaddr)); \r
- else \r
- {\r
- TheIP();\r
- ip->addr = ipaddr;\r
- if(eq(ip->addr,"0.0.0.0/0"))\r
- {\r
- ip->name = "(unregistered)";\r
- ip->min = free_min;\r
- ip->max = ip->desired=free_max;\r
- }\r
- else\r
- {\r
- ip->name = ipaddr;\r
- }\r
- }\r
- \r
- if(downloadflag)\r
- {\r
- if(proxyflag)\r
- {\r
- ip->proxy=traffic;\r
- }\r
- else\r
- {\r
- ip->traffic+=traffic;\r
- }\r
- ip->direct=ip->traffic-ip->upload-ip->proxy;\r
- ip->pktsdown=pkts;\r
- }\r
- else\r
- {\r
- ip->upload=traffic;\r
- ip->pktsup=pkts;\r
- if(include_upload)\r
- {\r
- ip->traffic+=traffic;\r
- }\r
- else \r
- {\r
- if(traffic>ip->traffic)\r
- {\r
- ip->traffic=traffic; \r
- }\r
- }\r
- }\r
- } \r
- }\r
- free(cmd);\r
-}\r