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