more IPs allowed in identical IPv6 uplink network
[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/* if(downloadflag)\r
d7357b63 102 { \r
103 if(strstr(proxy_ip,ptr))\r
104 {\r
105 proxyflag = 1;\r
106 }\r
7ae5a593 107\r
108 } \r
109 else \r
21c4f9f3 110 { */\r
7ae5a593 111 if(!downloadflag)\r
f19d3cd0 112 {\r
d7357b63 113 ipaddr = ptr;\r
f19d3cd0 114 }\r
115 }\r
d7357b63 116 break;\r
117 case 9: if(!ipv6 && downloadflag)\r
f19d3cd0 118 {\r
119 ipaddr = ptr;\r
120 }\r
121 break;\r
f19d3cd0 122 }\r
123 \r
124 if(accept && traffic>0 && ipaddr)\r
125 {\r
d7357b63 126 /* IPv6 subnet - /64 */\r
127 char *isipv6 = strstr(ipaddr,"/64");\r
128 if(ipv6 && isipv6)\r
129 {\r
130 *isipv6=0;\r
fd336391 131 printf("(IPv6) ");\r
132 }\r
133 else\r
134 {\r
135 printf("(IPv4) ");\r
d7357b63 136 }\r
7ae5a593 137/* \r
f19d3cd0 138 if(proxyflag)\r
139 {\r
140 printf("(proxy) ");\r
141 }\r
7ae5a593 142 else\r
143*/ \r
144 if(!downloadflag)\r
f19d3cd0 145 {\r
d7357b63 146 printf("(up) ");\r
f19d3cd0 147 }\r
d7357b63 148 else\r
149 {\r
150 printf("(down) ");\r
151 }\r
152 \r
fd336391 153 printf("%s %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);\r
f19d3cd0 154\r
d7357b63 155 if_exists(ip, ips, eqi(ip->addr,ipaddr)); \r
f19d3cd0 156 else \r
157 {\r
af37be1d 158 TheIP(ipaddr, FALSE);\r
f19d3cd0 159 if(eq(ip->addr,"0.0.0.0/0"))\r
160 {\r
161 ip->name = "(unregistered)";\r
162 ip->min = free_min;\r
163 ip->max = ip->desired=free_max;\r
164 }\r
165 else\r
166 {\r
167 ip->name = ipaddr;\r
168 }\r
169 }\r
170 \r
171 if(downloadflag)\r
172 {\r
7ae5a593 173/*\r
f19d3cd0 174 if(proxyflag)\r
175 {\r
176 ip->proxy = traffic;\r
177 }\r
178 else\r
7ae5a593 179 {*/\r
21c4f9f3 180 ip->traffic += traffic;\r
7ae5a593 181/* } */\r
21c4f9f3 182 ip->direct += traffic; /*-ip->proxy;*/\r
7d05bfc0 183 ip->pktsdown += pkts;\r
f19d3cd0 184 }\r
185 else\r
186 {\r
7d05bfc0 187 ip->upload += traffic;\r
188 ip->pktsup += pkts;\r
f19d3cd0 189 if(include_upload)\r
190 {\r
191 ip->traffic += traffic;\r
192 }\r
193 else \r
194 {\r
7d05bfc0 195 if(ip->upload > ip->traffic)\r
f19d3cd0 196 {\r
7d05bfc0 197 ip->traffic = ip->upload;\r
f19d3cd0 198 }\r
199 }\r
200 }\r
201 } \r
202 }\r
203 free(cmd);\r
204}\r
This page took 0.29566 seconds and 4 git commands to generate.