preliminary ipv6 support - not tested
[svn/Prometheus-QoS/.git] / parseiptables.c
CommitLineData
f19d3cd0 1/* Modified by: xChaos, 20130115 */\r
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
11extern char *proxy_ip;\r
12extern int free_min;\r
13extern int free_max;\r
14extern int include_upload;\r
15\r
16/* ===================== traffic analyser - uses iptables ================ */ \r
17\r
18void get_traffic_statistics(const char *whichiptables)\r
19{\r
20 char *str,*cmd;\r
21 int downloadflag=0;\r
22\r
23 textfile(Pipe,str) *line,*lines=NULL;\r
24 string(str,STRLEN);\r
25 string(cmd,STRLEN);\r
26\r
27 sprintf(cmd,"%s -L -v -x -n -t mangle", whichiptables);\r
28 shell(cmd);\r
29 input(str,STRLEN)\r
30 {\r
31 create(line,Pipe);\r
32 line->str=str;\r
33 string(str,STRLEN);\r
34 append(line,lines);\r
35 }\r
36\r
37 for_each(line,lines)\r
38 {\r
39 int col, accept = 0, proxyflag = 0, valid = 1, setchainname = 0, commonflag = 0; \r
40 unsigned long long traffic = 0;\r
41 unsigned long pkts = 0;\r
42 char *ipaddr = NULL,*ptr;\r
43 \r
44 valid_columns(ptr, line->str, ' ', col) \r
45 if(valid) switch(col)\r
46 { \r
47 case 1: if(eq(ptr,"Chain"))\r
48 {\r
49 setchainname = 1;\r
50 }\r
51 else if(eq(ptr,"pkts")) \r
52 {\r
53 valid = 0;\r
54 }\r
55 else\r
56 {\r
57 sscanf(ptr,"%lu",&pkts); \r
58 }\r
59 break;\r
60 case 2: if(setchainname)\r
61 {\r
62 if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))\r
63 {\r
64 downloadflag = 1; \r
65 }\r
66 else \r
67 {\r
68 if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))\r
69 {\r
70 downloadflag = 0;\r
71 }\r
72 } \r
73 if(eq(ptr,"post_common") || eq(ptr,"forw_common"))\r
74 {\r
75 commonflag = 1;\r
76 }\r
77 }\r
78 else\r
79 {\r
80 sscanf(ptr,"%Lu",&traffic); \r
81 traffic += (1<<19);\r
82 traffic >>= 20;\r
83 }\r
84 break;\r
85 case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)\r
86 {\r
87 accept = eq(ptr,mark);\r
88 }\r
89 /*if(filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/\r
90 break;\r
91 case 8: if(downloadflag)\r
92 { \r
93 if(strstr(proxy_ip,ptr))\r
94 {\r
95 proxyflag = 1;\r
96 }\r
97 }\r
98 else\r
99 {\r
100 ipaddr = ptr;\r
101 }\r
102 break;\r
103 case 9: if(downloadflag)ipaddr = ptr;break;\r
104 }\r
105 \r
106 if(accept && traffic>0 && ipaddr)\r
107 {\r
108 if(proxyflag)\r
109 {\r
110 printf("(proxy) ");\r
111 }\r
112 else if(!downloadflag)\r
113 {\r
114 printf("(upload) ");\r
115 }\r
116 printf("IP %s: %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);\r
117\r
118 if_exists(ip,ips,eq(ip->addr,ipaddr)); \r
119 else \r
120 {\r
121 TheIP(ipaddr);\r
122 if(eq(ip->addr,"0.0.0.0/0"))\r
123 {\r
124 ip->name = "(unregistered)";\r
125 ip->min = free_min;\r
126 ip->max = ip->desired=free_max;\r
127 }\r
128 else\r
129 {\r
130 ip->name = ipaddr;\r
131 }\r
132 }\r
133 \r
134 if(downloadflag)\r
135 {\r
136 if(proxyflag)\r
137 {\r
138 ip->proxy = traffic;\r
139 }\r
140 else\r
141 {\r
142 ip->traffic += traffic;\r
143 }\r
144 ip->direct = ip->traffic-ip->upload-ip->proxy;\r
145 ip->pktsdown = pkts;\r
146 }\r
147 else\r
148 {\r
149 ip->upload = traffic;\r
150 ip->pktsup = pkts;\r
151 if(include_upload)\r
152 {\r
153 ip->traffic += traffic;\r
154 }\r
155 else \r
156 {\r
157 if(traffic > ip->traffic)\r
158 {\r
159 ip->traffic = traffic; \r
160 }\r
161 }\r
162 }\r
163 } \r
164 }\r
165 free(cmd);\r
166}\r
This page took 0.238189 seconds and 4 git commands to generate.