cc2daacc5df3db6810a4657837ec4c4b17e9977e
[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
131 if(proxyflag)
132 {
133 printf("(proxy) ");
134 }
135 else if(!downloadflag)
136 {
137 printf("(up) ");
138 }
139 else
140 {
141 printf("(down) ");
142 }
143
144 printf("IP %s: %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);
145
146 if_exists(ip, ips, eqi(ip->addr,ipaddr));
147 else
148 {
149 TheIP(ipaddr);
150 if(eq(ip->addr,"0.0.0.0/0"))
151 {
152 ip->name = "(unregistered)";
153 ip->min = free_min;
154 ip->max = ip->desired=free_max;
155 }
156 else
157 {
158 ip->name = ipaddr;
159 }
160 }
161
162 if(downloadflag)
163 {
164 if(proxyflag)
165 {
166 ip->proxy = traffic;
167 }
168 else
169 {
170 ip->traffic += traffic;
171 }
172 ip->direct = ip->traffic-ip->upload-ip->proxy;
173 ip->pktsdown = pkts;
174 }
175 else
176 {
177 ip->upload = traffic;
178 ip->pktsup = pkts;
179 if(include_upload)
180 {
181 ip->traffic += traffic;
182 }
183 else
184 {
185 if(traffic > ip->traffic)
186 {
187 ip->traffic = traffic;
188 }
189 }
190 }
191 }
192 }
193 free(cmd);
194 }
This page took 0.558051 seconds and 3 git commands to generate.