more IPs allowed in identical IPv6 uplink network
[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 /* if(downloadflag)
102 {
103 if(strstr(proxy_ip,ptr))
104 {
105 proxyflag = 1;
106 }
107
108 }
109 else
110 { */
111 if(!downloadflag)
112 {
113 ipaddr = ptr;
114 }
115 }
116 break;
117 case 9: if(!ipv6 && downloadflag)
118 {
119 ipaddr = ptr;
120 }
121 break;
122 }
123
124 if(accept && traffic>0 && ipaddr)
125 {
126 /* IPv6 subnet - /64 */
127 char *isipv6 = strstr(ipaddr,"/64");
128 if(ipv6 && isipv6)
129 {
130 *isipv6=0;
131 printf("(IPv6) ");
132 }
133 else
134 {
135 printf("(IPv4) ");
136 }
137 /*
138 if(proxyflag)
139 {
140 printf("(proxy) ");
141 }
142 else
143 */
144 if(!downloadflag)
145 {
146 printf("(up) ");
147 }
148 else
149 {
150 printf("(down) ");
151 }
152
153 printf("%s %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);
154
155 if_exists(ip, ips, eqi(ip->addr,ipaddr));
156 else
157 {
158 TheIP(ipaddr, FALSE);
159 if(eq(ip->addr,"0.0.0.0/0"))
160 {
161 ip->name = "(unregistered)";
162 ip->min = free_min;
163 ip->max = ip->desired=free_max;
164 }
165 else
166 {
167 ip->name = ipaddr;
168 }
169 }
170
171 if(downloadflag)
172 {
173 /*
174 if(proxyflag)
175 {
176 ip->proxy = traffic;
177 }
178 else
179 {*/
180 ip->traffic += traffic;
181 /* } */
182 ip->direct += traffic; /*-ip->proxy;*/
183 ip->pktsdown += pkts;
184 }
185 else
186 {
187 ip->upload += traffic;
188 ip->pktsup += pkts;
189 if(include_upload)
190 {
191 ip->traffic += traffic;
192 }
193 else
194 {
195 if(ip->upload > ip->traffic)
196 {
197 ip->traffic = ip->upload;
198 }
199 }
200 }
201 }
202 }
203 free(cmd);
204 }
This page took 0.52217 seconds and 4 git commands to generate.