do not write empty logs when started
[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 if(traffic)
85 {
86 traffic_detected = 1;
87 }
88 traffic += (1<<19);
89 traffic >>= 20;
90 }
91 break;
92 case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)
93 {
94 accept = eq(ptr,mark);
95 }
96 break;
97 case 7: if(ipv6 && !downloadflag)
98 {
99 ipaddr = ptr;
100 }
101 break;
102 case 8: if(ipv6 && downloadflag)
103 {
104 ipaddr = ptr;
105 }
106 else if(!ipv6)
107 {
108 /* if(downloadflag)
109 {
110 if(strstr(proxy_ip,ptr))
111 {
112 proxyflag = 1;
113 }
114
115 }
116 else
117 { */
118 if(!downloadflag)
119 {
120 ipaddr = ptr;
121 }
122 }
123 break;
124 case 9: if(!ipv6 && downloadflag)
125 {
126 ipaddr = ptr;
127 }
128 break;
129 }
130
131 if(accept && traffic>0 && ipaddr)
132 {
133 /* IPv6 subnet - /64 */
134 char *isipv6 = strstr(ipaddr,"/64");
135 if(ipv6 && isipv6)
136 {
137 *isipv6=0;
138 printf("(IPv6) ");
139 }
140 else
141 {
142 printf("(IPv4) ");
143 }
144 /*
145 if(proxyflag)
146 {
147 printf("(proxy) ");
148 }
149 else
150 */
151 if(!downloadflag)
152 {
153 printf("(up) ");
154 }
155 else
156 {
157 printf("(down) ");
158 }
159
160 printf("%s %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);
161
162 if_exists(ip, ips, eqi(ip->addr,ipaddr));
163 else
164 {
165 TheIP(ipaddr, FALSE);
166 if(eq(ip->addr,"0.0.0.0/0"))
167 {
168 ip->name = "(unregistered)";
169 ip->min = free_min;
170 ip->max = ip->desired=free_max;
171 }
172 else
173 {
174 ip->name = ipaddr;
175 }
176 }
177
178 if(downloadflag)
179 {
180 /*
181 if(proxyflag)
182 {
183 ip->proxy = traffic;
184 }
185 else
186 {*/
187 ip->traffic += traffic;
188 ip->traffic_down += traffic;
189 /* } */
190 ip->direct += traffic; /*-ip->proxy;*/
191 ip->pktsdown += pkts;
192 }
193 else
194 {
195 ip->upload += traffic;
196 ip->traffic_up += traffic;
197 ip->pktsup += pkts;
198 if(include_upload)
199 {
200 ip->traffic += traffic;
201 }
202 else
203 {
204 if(ip->upload > ip->traffic)
205 {
206 ip->traffic = ip->upload;
207 }
208 }
209 }
210 }
211 }
212 free(cmd);
213 }
This page took 0.383769 seconds and 4 git commands to generate.