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