-
Notifications
You must be signed in to change notification settings - Fork 13
/
parseiptables.c
212 lines (198 loc) · 4.53 KB
/
parseiptables.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Modified by: xChaos, 20131208 */
#include "cll1-0.6.2.h"
#include "ipstruct.h"
#define STRLEN 256
/* globals declared in prometheus.c */
extern struct IP *ips, *ip, *sharedip;
extern char *mark;
/* extern char *proxy_ip; */
extern int free_min;
extern int free_max;
extern int include_upload;
int traffic_detected = 0;
extern char *iptablesdump;
/* ===================== traffic analyser - uses iptables ================ */
void get_traffic_statistics(const char *whichiptables, int ipv6)
{
char *str,*cmd;
int downloadflag = 0;
FILE *f;
string(cmd,STRLEN);
sprintf(cmd, "%s -L -v -x -n -t mangle>%s", whichiptables, iptablesdump);
/*-----------------------------------------------------------------*/
printf("Running %s ...\n", cmd);
/*-----------------------------------------------------------------*/
system(cmd);
/*-----------------------------------------------------------------*/
printf("Processing %s ...\n", iptablesdump);
/*-----------------------------------------------------------------*/
f = fopen(iptablesdump,"r");
if(!f)
perror(iptablesdump);
while(!feof(f))
{
int col, accept = 0, /*proxyflag = 0, */valid = 1, setchainname = 0, commonflag = 0;
unsigned long long traffic = 0;
unsigned long pkts = 0;
char *ipaddr = NULL,*ptr;
string(str, STRLEN);
fgets(str, STRLEN, f);
valid_columns(ptr, str, ' ', col)
if(valid) switch(col)
{
case 1: if(eq(ptr,"Chain"))
{
setchainname = 1;
}
else if(eq(ptr,"pkts"))
{
valid = 0;
}
else
{
sscanf(ptr,"%lu",&pkts);
}
break;
case 2: if(setchainname)
{
if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))
{
downloadflag = 1;
}
else
{
if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))
{
downloadflag = 0;
}
}
if(eq(ptr,"post_common") || eq(ptr,"forw_common"))
{
commonflag = 1;
}
}
else
{
sscanf(ptr,"%Lu",&traffic);
traffic += (1<<19);
traffic >>= 20;
}
break;
case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)
{
accept = eq(ptr,mark);
}
break;
case 7: if(ipv6 && !downloadflag)
{
ipaddr = ptr;
}
break;
case 8: if(ipv6 && downloadflag)
{
ipaddr = ptr;
}
else if(!ipv6)
{
/* if(downloadflag)
{
if(strstr(proxy_ip,ptr))
{
proxyflag = 1;
}
}
else
{ */
if(!downloadflag)
{
ipaddr = ptr;
}
}
break;
case 9: if(!ipv6 && downloadflag)
{
ipaddr = ptr;
}
break;
}
if(accept && traffic>0 && ipaddr)
{
/* IPv6 subnet - /64 */
char *isipv6 = strstr(ipaddr,"/64");
if(ipv6 && isipv6)
{
*isipv6=0;
printf("(IPv6) ");
}
else
{
printf("(IPv4) ");
}
/*
if(proxyflag)
{
printf("(proxy) ");
}
else
*/
if(!downloadflag)
{
printf("(up) ");
}
else
{
printf("(down) ");
}
traffic_detected = 1;
printf("%s %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);
if_exists(ip, ips, eqi(ip->addr,ipaddr));
else
{
TheIP(ipaddr, FALSE);
if(eq(ip->addr,"0.0.0.0/0"))
{
ip->name = "(unregistered)";
ip->min = free_min;
ip->max = ip->desired=free_max;
}
else
{
ip->name = ipaddr;
}
}
if(downloadflag)
{
/*
if(proxyflag)
{
ip->proxy = traffic;
}
else
{*/
ip->traffic += traffic;
ip->traffic_down += traffic;
/* } */
ip->direct += traffic; /*-ip->proxy;*/
ip->pktsdown += pkts;
}
else
{
ip->upload += traffic;
ip->traffic_up += traffic;
ip->pktsup += pkts;
if(include_upload)
{
ip->traffic += traffic;
}
else
{
if(ip->upload > ip->traffic)
{
ip->traffic = ip->upload;
}
}
}
}
}
free(cmd);
}