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