bugfix - no data were logged if no IPV6 traffic detected, by mistake
[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
25 textfile(Pipe,str) *line,*lines=NULL;
26 string(str,STRLEN);
27 string(cmd,STRLEN);
28
29 sprintf(cmd,"%s -L -v -x -n -t mangle", whichiptables);
30 shell(cmd);
31 input(str,STRLEN)
32 {
33 create(line,Pipe);
34 line->str=str;
35 string(str,STRLEN);
36 append(line,lines);
37 }
38
39 for_each(line,lines)
40 {
41 int col, accept = 0, /*proxyflag = 0, */valid = 1, setchainname = 0, commonflag = 0;
42 unsigned long long traffic = 0;
43 unsigned long pkts = 0;
44 char *ipaddr = NULL,*ptr;
45
46 valid_columns(ptr, line->str, ' ', col)
47 if(valid) switch(col)
48 {
49 case 1: if(eq(ptr,"Chain"))
50 {
51 setchainname = 1;
52 }
53 else if(eq(ptr,"pkts"))
54 {
55 valid = 0;
56 }
57 else
58 {
59 sscanf(ptr,"%lu",&pkts);
60 }
61 break;
62 case 2: if(setchainname)
63 {
64 if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))
65 {
66 downloadflag = 1;
67 }
68 else
69 {
70 if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))
71 {
72 downloadflag = 0;
73 }
74 }
75 if(eq(ptr,"post_common") || eq(ptr,"forw_common"))
76 {
77 commonflag = 1;
78 }
79 }
80 else
81 {
82 sscanf(ptr,"%Lu",&traffic);
83 traffic += (1<<19);
84 traffic >>= 20;
85 }
86 break;
87 case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)
88 {
89 accept = eq(ptr,mark);
90 }
91 break;
92 case 7: if(ipv6 && !downloadflag)
93 {
94 ipaddr = ptr;
95 }
96 break;
97 case 8: if(ipv6 && downloadflag)
98 {
99 ipaddr = ptr;
100 }
101 else if(!ipv6)
102 {
103 /* if(downloadflag)
104 {
105 if(strstr(proxy_ip,ptr))
106 {
107 proxyflag = 1;
108 }
109
110 }
111 else
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 traffic_detected = 1;
156 printf("%s %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);
157
158 if_exists(ip, ips, eqi(ip->addr,ipaddr));
159 else
160 {
161 TheIP(ipaddr, FALSE);
162 if(eq(ip->addr,"0.0.0.0/0"))
163 {
164 ip->name = "(unregistered)";
165 ip->min = free_min;
166 ip->max = ip->desired=free_max;
167 }
168 else
169 {
170 ip->name = ipaddr;
171 }
172 }
173
174 if(downloadflag)
175 {
176 /*
177 if(proxyflag)
178 {
179 ip->proxy = traffic;
180 }
181 else
182 {*/
183 ip->traffic += traffic;
184 ip->traffic_down += traffic;
185 /* } */
186 ip->direct += traffic; /*-ip->proxy;*/
187 ip->pktsdown += pkts;
188 }
189 else
190 {
191 ip->upload += traffic;
192 ip->traffic_up += traffic;
193 ip->pktsup += pkts;
194 if(include_upload)
195 {
196 ip->traffic += traffic;
197 }
198 else
199 {
200 if(ip->upload > ip->traffic)
201 {
202 ip->traffic = ip->upload;
203 }
204 }
205 }
206 }
207 }
208 free(cmd);
209 }
This page took 0.430887 seconds and 4 git commands to generate.