cdfd88eb |
1 | /* Modified by: xChaos, 20131029 */\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 |
9 | extern struct IP *ips, *ip, *sharedip;\r |
10 | extern char *mark;\r |
11 | extern char *proxy_ip;\r |
12 | extern int free_min;\r |
13 | extern int free_max;\r |
14 | extern int include_upload;\r |
15 | \r |
16 | /* ===================== traffic analyser - uses iptables ================ */ \r |
17 | \r |
d7357b63 |
18 | void 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 |
39 | int col, accept = 0, proxyflag = 0, valid = 1, setchainname = 0, commonflag = 0; \r |
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 |
101 | if(downloadflag)\r |
102 | { \r |
103 | if(strstr(proxy_ip,ptr))\r |
104 | {\r |
105 | proxyflag = 1;\r |
106 | }\r |
107 | }\r |
108 | else\r |
f19d3cd0 |
109 | {\r |
d7357b63 |
110 | ipaddr = ptr;\r |
f19d3cd0 |
111 | }\r |
112 | }\r |
d7357b63 |
113 | break;\r |
114 | case 9: if(!ipv6 && downloadflag)\r |
f19d3cd0 |
115 | {\r |
116 | ipaddr = ptr;\r |
117 | }\r |
118 | break;\r |
f19d3cd0 |
119 | }\r |
120 | \r |
121 | if(accept && traffic>0 && ipaddr)\r |
122 | {\r |
d7357b63 |
123 | /* IPv6 subnet - /64 */\r |
124 | char *isipv6 = strstr(ipaddr,"/64");\r |
125 | if(ipv6 && isipv6)\r |
126 | {\r |
127 | *isipv6=0;\r |
fd336391 |
128 | printf("(IPv6) ");\r |
129 | }\r |
130 | else\r |
131 | {\r |
132 | printf("(IPv4) ");\r |
d7357b63 |
133 | }\r |
134 | \r |
f19d3cd0 |
135 | if(proxyflag)\r |
136 | {\r |
137 | printf("(proxy) ");\r |
138 | }\r |
139 | else if(!downloadflag)\r |
140 | {\r |
d7357b63 |
141 | printf("(up) ");\r |
f19d3cd0 |
142 | }\r |
d7357b63 |
143 | else\r |
144 | {\r |
145 | printf("(down) ");\r |
146 | }\r |
147 | \r |
fd336391 |
148 | printf("%s %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);\r |
f19d3cd0 |
149 | \r |
d7357b63 |
150 | if_exists(ip, ips, eqi(ip->addr,ipaddr)); \r |
f19d3cd0 |
151 | else \r |
152 | {\r |
af37be1d |
153 | TheIP(ipaddr, FALSE);\r |
f19d3cd0 |
154 | if(eq(ip->addr,"0.0.0.0/0"))\r |
155 | {\r |
156 | ip->name = "(unregistered)";\r |
157 | ip->min = free_min;\r |
158 | ip->max = ip->desired=free_max;\r |
159 | }\r |
160 | else\r |
161 | {\r |
162 | ip->name = ipaddr;\r |
163 | }\r |
164 | }\r |
165 | \r |
166 | if(downloadflag)\r |
167 | {\r |
168 | if(proxyflag)\r |
169 | {\r |
170 | ip->proxy = traffic;\r |
171 | }\r |
172 | else\r |
173 | {\r |
174 | ip->traffic += traffic;\r |
175 | }\r |
7d05bfc0 |
176 | ip->direct += ip->traffic-ip->upload-ip->proxy;\r |
177 | ip->pktsdown += pkts;\r |
f19d3cd0 |
178 | }\r |
179 | else\r |
180 | {\r |
7d05bfc0 |
181 | ip->upload += traffic;\r |
182 | ip->pktsup += pkts;\r |
f19d3cd0 |
183 | if(include_upload)\r |
184 | {\r |
185 | ip->traffic += traffic;\r |
186 | }\r |
187 | else \r |
188 | {\r |
7d05bfc0 |
189 | if(ip->upload > ip->traffic)\r |
f19d3cd0 |
190 | {\r |
7d05bfc0 |
191 | ip->traffic = ip->upload;\r |
f19d3cd0 |
192 | }\r |
193 | }\r |
194 | }\r |
195 | } \r |
196 | }\r |
197 | free(cmd);\r |
198 | }\r |