Latest updates from Teaman-IPTraffic
[tomato.git] / release / src / router / httpd / bwm.c
blobd34a801192d67b69263f6fc2983e3a847a924800
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sys/wait.h>
16 #include <typedefs.h>
19 static const char *hfn = "/var/lib/misc/rstats-history.gz";
20 static const char *ifn = "/var/lib/misc/cstats-history.gz";
22 void wo_bwmbackup(char *url)
24 struct stat st;
25 time_t t;
26 int i;
28 if (stat(hfn, &st) == 0) {
29 t = st.st_mtime;
30 sleep(1);
32 else {
33 t = 0;
35 killall("rstats", SIGHUP);
36 for (i = 10; i > 0; --i) {
37 if ((stat(hfn, &st) == 0) && (st.st_mtime != t)) break;
38 sleep(1);
40 if (i == 0) {
41 send_error(500, NULL, NULL);
42 return;
44 send_header(200, NULL, mime_binary, 0);
45 do_file((char *)hfn);
48 void wo_iptbackup(char *url)
50 struct stat st;
51 time_t t;
52 int i;
54 if (stat(ifn, &st) == 0) {
55 t = st.st_mtime;
56 sleep(1);
58 else {
59 t = 0;
61 killall("cstats", SIGHUP);
62 for (i = 10; i > 0; --i) {
63 if ((stat(ifn, &st) == 0) && (st.st_mtime != t)) break;
64 sleep(1);
66 if (i == 0) {
67 send_error(500, NULL, NULL);
68 return;
70 send_header(200, NULL, mime_binary, 0);
71 do_file((char *)ifn);
74 void wi_bwmrestore(char *url, int len, char *boundary)
76 char *buf;
77 const char *error;
78 int ok;
79 int n;
80 char tmp[64];
82 check_id(url);
84 tmp[0] = 0;
85 buf = NULL;
86 error = "Error reading file";
87 ok = 0;
89 if (!skip_header(&len)) {
90 goto ERROR;
93 if ((len < 64) || (len > 10240)) {
94 goto ERROR;
97 if ((buf = malloc(len)) == NULL) {
98 error = "Not enough memory";
99 goto ERROR;
102 n = web_read(buf, len);
103 len -= n;
105 sprintf(tmp, "%s.new", hfn);
106 if (f_write(tmp, buf, n, 0, 0600) != n) {
107 unlink(tmp);
108 error = "Error writing temporary file";
109 goto ERROR;
111 f_write("/var/tmp/rstats-load", NULL, 0, 0, 0600);
112 killall("rstats", SIGHUP);
113 sleep(1);
115 error = NULL;
116 rboot = 1; // used as "ok"
118 ERROR:
119 free(buf);
120 web_eat(len);
121 if (error != NULL) resmsg_set(error);
124 void wi_iptrestore(char *url, int len, char *boundary)
126 char *buf;
127 const char *error;
128 int ok;
129 int n;
130 char tmp[64];
132 check_id(url);
134 tmp[0] = 0;
135 buf = NULL;
136 error = "Error reading file";
137 ok = 0;
139 if (!skip_header(&len)) {
140 goto ERROR;
143 if ((len < 64) || (len > 10240)) {
144 goto ERROR;
147 if ((buf = malloc(len)) == NULL) {
148 error = "Not enough memory";
149 goto ERROR;
152 n = web_read(buf, len);
153 len -= n;
155 sprintf(tmp, "%s.new", ifn);
156 if (f_write(tmp, buf, n, 0, 0600) != n) {
157 unlink(tmp);
158 error = "Error writing temporary file";
159 goto ERROR;
161 f_write("/var/tmp/cstats-load", NULL, 0, 0, 0600);
162 killall("cstats", SIGHUP);
163 sleep(1);
165 error = NULL;
166 rboot = 1; // used as "ok"
168 ERROR:
169 free(buf);
170 web_eat(len);
171 if (error != NULL) resmsg_set(error);
174 void wo_bwmrestore(char *url)
176 if (rboot) {
177 redirect("/bwm-daily.asp");
179 else {
180 parse_asp("error.asp");
184 void wo_iptrestore(char *url)
186 if (rboot) {
187 redirect("/ipt-daily.asp");
189 else {
190 parse_asp("error.asp");
194 void asp_netdev(int argc, char **argv)
196 FILE *f;
197 char buf[256];
198 unsigned long rx, tx;
199 char *p;
200 char *ifname;
201 char comma;
202 char *exclude;
204 exclude = nvram_safe_get("rstats_exclude");
205 web_puts("\n\nnetdev={");
206 if ((f = fopen("/proc/net/dev", "r")) != NULL) {
207 fgets(buf, sizeof(buf), f); // header
208 fgets(buf, sizeof(buf), f); // "
209 comma = ' ';
210 while (fgets(buf, sizeof(buf), f)) {
211 if ((p = strchr(buf, ':')) == NULL) continue;
212 *p = 0;
213 if ((ifname = strrchr(buf, ' ')) == NULL) ifname = buf;
214 else ++ifname;
215 // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
216 if ((strcmp(ifname, "lo") == 0) || (find_word(exclude, ifname))) continue;
218 // <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...>
219 if (sscanf(p + 1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &rx, &tx) != 2) continue;
220 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma, ifname, rx, tx);
221 comma = ',';
223 fclose(f);
225 web_puts("};\n");
228 void asp_climon(int argc, char **argv) {
229 FILE *ti;
230 FILE *to;
232 char bi[512];
233 char bo[512];
235 char ip[64];
236 unsigned long rx, tx;
238 char comma;
240 web_puts("\n\nclimon={");
241 if ((to = popen("iptables -vnxL traffic_out", "r")) != NULL) {
242 if ((ti = popen("iptables -vnxL traffic_in", "r")) != NULL) {
243 comma = ' ';
244 while ((fgets(bi, sizeof(bi), ti)) && (fgets(bo, sizeof(bo), to))) {
245 if( (sscanf(bi, "%*s %lu %*s %*s %*s %*s %*s %s", &rx, ip)!=2) ||
246 (sscanf(bo, "%*s %lu %*s %*s %*s %*s %*s %*s", &tx)!=1) ) continue;
247 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma, ip, rx, tx);
248 comma = ',';
251 pclose(ti);
253 pclose(to);
254 web_puts("};\n");
257 void asp_iptmon(int argc, char **argv) {
259 char comma;
260 char sa[256];
261 FILE *a;
263 char ip[INET6_ADDRSTRLEN];
265 unsigned long tx, rx;
267 char br;
268 char name[] = "/proc/net/ipt_account/lanX";
270 web_puts("\n\niptmon={");
271 comma = ' ';
273 for(br=0 ; br<=3 ; br++) {
274 char bridge[2] = "0";
275 if (br!=0)
276 bridge[0]+=br;
277 else
278 strcpy(bridge, "");
280 sprintf(name, "/proc/net/ipt_account/lan%s", bridge);
282 if ((a = fopen(name, "r")) == NULL) continue;
284 // fgets(sa, sizeof(sa), a); // network
285 while (fgets(sa, sizeof(sa), a)) {
286 if(sscanf(sa,
287 "ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %*u %*u %*u %*u bytes_dest = %lu %*u %*u %*u %*u packets_dest = %*u %*u %*u %*u %*u time = %*u",
288 ip, &tx, &rx) != 3 ) continue;
289 if ((tx > 0) || (rx > 0)) {
290 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma, ip, rx, tx);
291 comma = ',';
294 fclose(a);
296 web_puts("};\n");
299 void asp_bandwidth(int argc, char **argv)
301 char *name;
302 int sig;
304 if ((nvram_get_int("rstats_enable") == 1) && (argc == 1)) {
305 if (strcmp(argv[0], "speed") == 0) {
306 sig = SIGUSR1;
307 name = "/var/spool/rstats-speed.js";
309 else {
310 sig = SIGUSR2;
311 name = "/var/spool/rstats-history.js";
313 unlink(name);
314 killall("rstats", sig);
315 f_wait_exists(name, 5);
316 do_file(name);
317 unlink(name);
321 void asp_ipt_bandwidth(int argc, char **argv)
323 char *name;
324 int sig;
326 if ((nvram_get_int("cstats_enable") == 1) && (argc == 1)) {
327 if (strcmp(argv[0], "speed") == 0) {
328 sig = SIGUSR1;
329 name = "/var/spool/cstats-speed.js";
331 else {
332 sig = SIGUSR2;
333 name = "/var/spool/cstats-history.js";
335 unlink(name);
336 killall("cstats", sig);
337 f_wait_exists(name, 5);
338 do_file(name);
339 unlink(name);
343 void asp_iptraffic(int argc, char **argv) {
345 char comma;
346 char sa[256];
347 char sb[256];
348 FILE *a;
349 FILE *b;
350 char *p;
351 char ip[INET6_ADDRSTRLEN];
353 unsigned long tx_bytes, rx_bytes;
354 unsigned long tp_tcp, rp_tcp;
355 unsigned long tp_udp, rp_udp;
356 unsigned long tp_icmp, rp_icmp;
357 unsigned long ct_tcp, ct_udp;
360 // needs extra tweaks in the code before this works as it should
361 // so we'll stick to IPv4-only for now...
362 // #if defined(TCONFIG_IPV6) && defined(LINUX26)
363 // const char conntrack[] = "/proc/net/nf_conntrack";
364 // #else
365 const char conntrack[] = "/proc/net/ip_conntrack";
366 // #endif
368 if ((b = fopen(conntrack, "r")) == NULL) return;
370 char br;
371 char name[] = "/proc/net/ipt_account/lanX";
373 web_puts("\n\niptraffic=[");
374 comma = ' ';
376 for(br=0 ; br<=3 ; br++) {
377 char bridge[2] = "0";
378 if (br!=0)
379 bridge[0]+=br;
380 else
381 strcpy(bridge, "");
383 sprintf(name, "/proc/net/ipt_account/lan%s", bridge);
385 if ((a = fopen(name, "r")) == NULL) continue;
387 fgets(sa, sizeof(sa), a); // network
388 while (fgets(sa, sizeof(sa), a)) {
389 if(sscanf(sa,
390 "ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %lu %lu %lu %*u bytes_dest = %lu %*u %*u %*u %*u packets_dest = %*u %lu %lu %lu %*u time = %*u",
391 ip, &tx_bytes, &tp_tcp, &tp_udp, &tp_icmp, &rx_bytes, &rp_tcp, &rp_udp, &rp_icmp) != 9 ) continue;
392 if ((tx_bytes > 0) || (rx_bytes > 0)) {
393 rewind(b);
394 ct_tcp = 0;
395 ct_udp = 0;
396 while (fgets(sb, sizeof(sb), b)) {
397 if ((p = strstr(sb, ip)) == NULL) continue;
398 if (strncmp(sb, "tcp", 3) == 0) ct_tcp++;
399 if (strncmp(sb, "udp", 3) == 0) ct_udp++;
401 web_printf("%c['%s', %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu]",
402 comma, ip, tx_bytes, rx_bytes, tp_tcp, rp_tcp, tp_udp, rp_udp, tp_icmp, rp_icmp, ct_tcp, ct_udp);
403 comma = ',';
406 fclose(a);
408 web_puts("];\n");