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