traffic->download bug fixed
[svn/Prometheus-QoS/.git] / parseiptables.c
CommitLineData
39a89851 1/* Modified by: xChaos, 20131208 */\r
f19d3cd0 2\r
3#include "cll1-0.6.2.h"\r
4#include "ipstruct.h"\r
5\r
6#define STRLEN 512\r
7\r
8/* globals declared in prometheus.c */\r
9extern struct IP *ips, *ip, *sharedip;\r
10extern char *mark;\r
7ae5a593 11/* extern char *proxy_ip; */\r
f19d3cd0 12extern int free_min;\r
13extern int free_max;\r
14extern int include_upload;\r
15\r
16/* ===================== traffic analyser - uses iptables ================ */ \r
17\r
d7357b63 18void get_traffic_statistics(const char *whichiptables, int ipv6)\r
f19d3cd0 19{\r
20 char *str,*cmd;\r
21 int downloadflag=0;\r
22\r
23 textfile(Pipe,str) *line,*lines=NULL;\r
24 string(str,STRLEN);\r
25 string(cmd,STRLEN);\r
26\r
27 sprintf(cmd,"%s -L -v -x -n -t mangle", whichiptables);\r
28 shell(cmd);\r
29 input(str,STRLEN)\r
30 {\r
31 create(line,Pipe);\r
32 line->str=str;\r
33 string(str,STRLEN);\r
34 append(line,lines);\r
35 }\r
36\r
37 for_each(line,lines)\r
38 {\r
7ae5a593 39 int col, accept = 0, /*proxyflag = 0, */valid = 1, setchainname = 0, commonflag = 0; \r
f19d3cd0 40 unsigned long long traffic = 0;\r
41 unsigned long pkts = 0;\r
42 char *ipaddr = NULL,*ptr;\r
43 \r
af37be1d 44 valid_columns(ptr, line->str, ' ', col)\r
f19d3cd0 45 if(valid) switch(col)\r
46 { \r
47 case 1: if(eq(ptr,"Chain"))\r
48 {\r
49 setchainname = 1;\r
50 }\r
51 else if(eq(ptr,"pkts")) \r
52 {\r
53 valid = 0;\r
54 }\r
55 else\r
56 {\r
57 sscanf(ptr,"%lu",&pkts); \r
58 }\r
59 break;\r
60 case 2: if(setchainname)\r
61 {\r
62 if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))\r
63 {\r
64 downloadflag = 1; \r
65 }\r
66 else \r
67 {\r
68 if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))\r
69 {\r
70 downloadflag = 0;\r
71 }\r
72 } \r
73 if(eq(ptr,"post_common") || eq(ptr,"forw_common"))\r
74 {\r
75 commonflag = 1;\r
76 }\r
77 }\r
78 else\r
79 {\r
80 sscanf(ptr,"%Lu",&traffic); \r
81 traffic += (1<<19);\r
82 traffic >>= 20;\r
83 }\r
84 break;\r
85 case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)\r
86 {\r
87 accept = eq(ptr,mark);\r
88 }\r
f19d3cd0 89 break;\r
d7357b63 90 case 7: if(ipv6 && !downloadflag)\r
91 {\r
92 ipaddr = ptr;\r
93 }\r
94 break;\r
95 case 8: if(ipv6 && downloadflag)\r
96 {\r
97 ipaddr = ptr;\r
98 }\r
99 else if(!ipv6)\r
100 {\r
7ae5a593 101\r
102/* if(downloadflag)\r
d7357b63 103 { \r
104 if(strstr(proxy_ip,ptr))\r
105 {\r
106 proxyflag = 1;\r
107 }\r
7ae5a593 108\r
109 } \r
110 else \r
111 { \r
112*/\r
113 if(!downloadflag)\r
f19d3cd0 114 {\r
d7357b63 115 ipaddr = ptr;\r
f19d3cd0 116 }\r
117 }\r
d7357b63 118 break;\r
119 case 9: if(!ipv6 && downloadflag)\r
f19d3cd0 120 {\r
121 ipaddr = ptr;\r
122 }\r
123 break;\r
f19d3cd0 124 }\r
125 \r
126 if(accept && traffic>0 && ipaddr)\r
127 {\r
d7357b63 128 /* IPv6 subnet - /64 */\r
129 char *isipv6 = strstr(ipaddr,"/64");\r
130 if(ipv6 && isipv6)\r
131 {\r
132 *isipv6=0;\r
fd336391 133 printf("(IPv6) ");\r
134 }\r
135 else\r
136 {\r
137 printf("(IPv4) ");\r
d7357b63 138 }\r
7ae5a593 139/* \r
f19d3cd0 140 if(proxyflag)\r
141 {\r
142 printf("(proxy) ");\r
143 }\r
7ae5a593 144 else\r
145*/ \r
146 if(!downloadflag)\r
f19d3cd0 147 {\r
d7357b63 148 printf("(up) ");\r
f19d3cd0 149 }\r
d7357b63 150 else\r
151 {\r
152 printf("(down) ");\r
153 }\r
154 \r
fd336391 155 printf("%s %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);\r
f19d3cd0 156\r
d7357b63 157 if_exists(ip, ips, eqi(ip->addr,ipaddr)); \r
f19d3cd0 158 else \r
159 {\r
af37be1d 160 TheIP(ipaddr, FALSE);\r
f19d3cd0 161 if(eq(ip->addr,"0.0.0.0/0"))\r
162 {\r
163 ip->name = "(unregistered)";\r
164 ip->min = free_min;\r
165 ip->max = ip->desired=free_max;\r
166 }\r
167 else\r
168 {\r
169 ip->name = ipaddr;\r
170 }\r
171 }\r
172 \r
173 if(downloadflag)\r
174 {\r
7ae5a593 175/*\r
f19d3cd0 176 if(proxyflag)\r
177 {\r
178 ip->proxy = traffic;\r
179 }\r
180 else\r
7ae5a593 181 {*/\r
f19d3cd0 182 ip->traffic += traffic;\r
7ae5a593 183/* } */\r
39a89851 184 ip->direct += ip->traffic-ip->upload; /*-ip->proxy;*/\r
7d05bfc0 185 ip->pktsdown += pkts;\r
f19d3cd0 186 }\r
187 else\r
188 {\r
7d05bfc0 189 ip->upload += traffic;\r
190 ip->pktsup += pkts;\r
f19d3cd0 191 if(include_upload)\r
192 {\r
193 ip->traffic += traffic;\r
194 }\r
195 else \r
196 {\r
7d05bfc0 197 if(ip->upload > ip->traffic)\r
f19d3cd0 198 {\r
7d05bfc0 199 ip->traffic = ip->upload;\r
f19d3cd0 200 }\r
201 }\r
202 }\r
203 } \r
204 }\r
205 free(cmd);\r
206}\r
This page took 0.384662 seconds and 4 git commands to generate.