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