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