getting ready for IP6tables support
[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)
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 /*if(filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/
90 break;
91 case 8: if(downloadflag)
92 {
93 if(strstr(proxy_ip,ptr))
94 {
95 proxyflag = 1;
96 }
97 }
98 else
99 {
100 ipaddr = ptr;
101 }
102 break;
103 case 9: if(downloadflag)ipaddr = ptr;break;
104 }
105
106 if(accept && traffic>0 && ipaddr)
107 {
108 if(proxyflag)
109 {
110 printf("(proxy) ");
111 }
112 else if(!downloadflag)
113 {
114 printf("(upload) ");
115 }
116 printf("IP %s: %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);
117
118 if_exists(ip,ips,eq(ip->addr,ipaddr));
119 else
120 {
121 TheIP(ipaddr);
122 if(eq(ip->addr,"0.0.0.0/0"))
123 {
124 ip->name = "(unregistered)";
125 ip->min = free_min;
126 ip->max = ip->desired=free_max;
127 }
128 else
129 {
130 ip->name = ipaddr;
131 }
132 }
133
134 if(downloadflag)
135 {
136 if(proxyflag)
137 {
138 ip->proxy = traffic;
139 }
140 else
141 {
142 ip->traffic += traffic;
143 }
144 ip->direct = ip->traffic-ip->upload-ip->proxy;
145 ip->pktsdown = pkts;
146 }
147 else
148 {
149 ip->upload = traffic;
150 ip->pktsup = pkts;
151 if(include_upload)
152 {
153 ip->traffic += traffic;
154 }
155 else
156 {
157 if(traffic > ip->traffic)
158 {
159 ip->traffic = traffic;
160 }
161 }
162 }
163 }
164 }
165 free(cmd);
166 }
This page took 0.348862 seconds and 4 git commands to generate.