wireless filter limit increased to 250
[tomato.git] / release / src / router / httpd / bwm.c
blobfdb6fc7c022887267840440b1df895e3ec405e28
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <sys/stat.h>
11 #include <sys/ioctl.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <sys/wait.h>
17 #include <typedefs.h>
20 static const char *hfn = "/var/lib/misc/rstats-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 wi_bwmrestore(char *url, int len, char *boundary)
50 char *buf;
51 const char *error;
52 int ok;
53 int n;
54 char tmp[64];
56 check_id(url);
58 tmp[0] = 0;
59 buf = NULL;
60 error = "Error reading file";
61 ok = 0;
63 if (!skip_header(&len)) {
64 goto ERROR;
67 if ((len < 64) || (len > 10240)) {
68 goto ERROR;
71 if ((buf = malloc(len)) == NULL) {
72 error = "Not enough memory";
73 goto ERROR;
76 n = web_read(buf, len);
77 len -= n;
79 sprintf(tmp, "%s.new", hfn);
80 if (f_write(tmp, buf, n, 0, 0600) != n) {
81 unlink(tmp);
82 error = "Error writing temporary file";
83 goto ERROR;
85 f_write("/var/tmp/rstats-load", NULL, 0, 0, 0600);
86 killall("rstats", SIGHUP);
87 sleep(1);
89 error = NULL;
90 rboot = 1; // used as "ok"
92 ERROR:
93 free(buf);
94 web_eat(len);
95 if (error != NULL) resmsg_set(error);
98 void wo_bwmrestore(char *url)
100 if (rboot) {
101 redirect("/bwm-daily.asp");
103 else {
104 parse_asp("error.asp");
108 void asp_netdev(int argc, char **argv)
110 FILE *f;
111 char buf[256];
112 unsigned long rx, tx;
113 char *p;
114 char *ifname;
115 char comma;
116 char *exclude;
117 int sfd;
118 struct ifreq ifr;
120 exclude = nvram_safe_get("rstats_exclude");
121 web_puts("\n\nnetdev={");
122 if ((f = fopen("/proc/net/dev", "r")) != NULL) {
123 fgets(buf, sizeof(buf), f); // header
124 fgets(buf, sizeof(buf), f); // "
125 comma = ' ';
127 if ((sfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
128 _dprintf("[%s %d]: error opening socket %m\n", __FUNCTION__, __LINE__);
131 while (fgets(buf, sizeof(buf), f)) {
132 if ((p = strchr(buf, ':')) == NULL) continue;
133 *p = 0;
134 if ((ifname = strrchr(buf, ' ')) == NULL) ifname = buf;
135 else ++ifname;
136 // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
137 if ((strcmp(ifname, "lo") == 0) || (find_word(exclude, ifname))) continue;
139 // skip down interfaces
140 if (sfd >= 0) {
141 strcpy(ifr.ifr_name, ifname);
142 if (ioctl(sfd, SIOCGIFFLAGS, &ifr) != 0) continue;
143 if ((ifr.ifr_flags & IFF_UP) == 0) continue;
146 // <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...>
147 if (sscanf(p + 1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &rx, &tx) != 2) continue;
148 if (!strcmp(ifname, "imq0"))
149 web_printf("%c'%s':{rx:0x0,tx:0x%lx}", comma, ifname, rx, tx);
150 else if (!strcmp(ifname, "imq1"))
151 web_printf("%c'%s':{rx:0x%lx,tx:0x0}", comma, ifname, rx, tx);
152 else
153 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma, ifname, rx, tx);
155 comma = ',';
158 if (sfd >= 0) close(sfd);
159 fclose(f);
161 web_puts("};\n");
164 void asp_bandwidth(int argc, char **argv)
166 char *name;
167 int sig;
169 if ((nvram_get_int("rstats_enable") == 1) && (argc == 1)) {
170 if (strcmp(argv[0], "speed") == 0) {
171 sig = SIGUSR1;
172 name = "/var/spool/rstats-speed.js";
174 else {
175 sig = SIGUSR2;
176 name = "/var/spool/rstats-history.js";
178 unlink(name);
179 killall("rstats", sig);
180 f_wait_exists(name, 5);
181 do_file(name);
182 unlink(name);