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