Changes to update Tomato RAF.
[tomato.git] / release / src / router / httpd / bwm.c
blobf76a78adc4f40d8a9b9fda91e8ce0e319df3fb4d
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";
21 static const char *ifn = "/var/lib/misc/cstats-history.gz";
23 void wo_bwmbackup(char *url)
25 struct stat st;
26 time_t t;
27 int i;
29 if (stat(hfn, &st) == 0) {
30 t = st.st_mtime;
31 sleep(1);
33 else {
34 t = 0;
36 killall("rstats", SIGHUP);
37 for (i = 10; i > 0; --i) {
38 if ((stat(hfn, &st) == 0) && (st.st_mtime != t)) break;
39 sleep(1);
41 if (i == 0) {
42 send_error(500, NULL, NULL);
43 return;
45 send_header(200, NULL, mime_binary, 0);
46 do_file((char *)hfn);
49 void wo_iptbackup(char *url)
51 struct stat st;
52 time_t t;
53 int i;
55 if (stat(ifn, &st) == 0) {
56 t = st.st_mtime;
57 sleep(1);
59 else {
60 t = 0;
62 killall("cstats", SIGHUP);
63 for (i = 20; i > 0; --i) { // may take a long time for gzip to complete
64 if ((stat(ifn, &st) == 0) && (st.st_mtime != t)) break;
65 sleep(1);
67 if (i == 0) {
68 send_error(500, NULL, NULL);
69 return;
71 send_header(200, NULL, mime_binary, 0);
72 do_file((char *)ifn);
75 void wi_bwmrestore(char *url, int len, char *boundary)
77 char *buf;
78 const char *error;
79 int ok;
80 int n;
81 char tmp[64];
83 check_id(url);
85 tmp[0] = 0;
86 buf = NULL;
87 error = "Error reading file";
88 ok = 0;
90 if (!skip_header(&len)) {
91 goto ERROR;
94 if ((len < 64) || (len > 10240)) {
95 goto ERROR;
98 if ((buf = malloc(len)) == NULL) {
99 error = "Not enough memory";
100 goto ERROR;
103 n = web_read(buf, len);
104 len -= n;
106 sprintf(tmp, "%s.new", hfn);
107 if (f_write(tmp, buf, n, 0, 0600) != n) {
108 unlink(tmp);
109 error = "Error writing temporary file";
110 goto ERROR;
112 f_write("/var/tmp/rstats-load", NULL, 0, 0, 0600);
113 killall("rstats", SIGHUP);
114 sleep(1);
116 error = NULL;
117 rboot = 1; // used as "ok"
119 ERROR:
120 free(buf);
121 web_eat(len);
122 if (error != NULL) resmsg_set(error);
125 void wi_iptrestore(char *url, int len, char *boundary)
127 char *buf;
128 const char *error;
129 int ok;
130 int n;
131 char tmp[64];
133 check_id(url);
135 tmp[0] = 0;
136 buf = NULL;
137 error = "Error reading file";
138 ok = 0;
140 if (!skip_header(&len)) {
141 goto ERROR;
144 if ((len < 64) || (len > 102400)) { // up to 100k
145 goto ERROR;
148 if ((buf = malloc(len)) == NULL) {
149 error = "Not enough memory";
150 goto ERROR;
153 n = web_read(buf, len);
154 len -= n;
156 sprintf(tmp, "%s.new", ifn);
157 if (f_write(tmp, buf, n, 0, 0600) != n) {
158 unlink(tmp);
159 error = "Error writing temporary file";
160 goto ERROR;
162 f_write("/var/tmp/cstats-load", NULL, 0, 0, 0600);
163 killall("cstats", SIGHUP);
164 sleep(1);
166 error = NULL;
167 rboot = 1; // used as "ok"
169 ERROR:
170 free(buf);
171 web_eat(len);
172 if (error != NULL) resmsg_set(error);
175 void wo_bwmrestore(char *url)
177 if (rboot) {
178 redirect("/bwm-daily.asp");
180 else {
181 parse_asp("error.asp");
185 void wo_iptrestore(char *url)
187 if (rboot) {
188 redirect("/ipt-daily.asp");
190 else {
191 parse_asp("error.asp");
195 void asp_netdev(int argc, char **argv)
197 FILE *f;
198 char buf[256];
199 int64_t rx, tx;
200 char *p;
201 char *ifname;
202 char comma;
203 char *exclude;
204 int sfd;
205 struct ifreq ifr;
207 exclude = nvram_safe_get("rstats_exclude");
208 web_puts("\n\nnetdev={");
209 if ((f = fopen("/proc/net/dev", "r")) != NULL) {
210 fgets(buf, sizeof(buf), f); // header
211 fgets(buf, sizeof(buf), f); // "
212 comma = ' ';
214 if ((sfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
215 _dprintf("[%s %d]: error opening socket %m\n", __FUNCTION__, __LINE__);
218 while (fgets(buf, sizeof(buf), f)) {
219 if ((p = strchr(buf, ':')) == NULL) continue;
220 *p = 0;
221 if ((ifname = strrchr(buf, ' ')) == NULL) ifname = buf;
222 else ++ifname;
223 // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
224 if ((strcmp(ifname, "lo") == 0) || (find_word(exclude, ifname))) continue;
226 // skip down interfaces
227 if (sfd >= 0) {
228 strcpy(ifr.ifr_name, ifname);
229 if (ioctl(sfd, SIOCGIFFLAGS, &ifr) != 0) continue;
230 if ((ifr.ifr_flags & IFF_UP) == 0) continue;
233 // <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...>
234 if (sscanf(p + 1, "%llu%*u%*u%*u%*u%*u%*u%*u%llu", &rx, &tx) != 2) continue;
235 if (!strcmp(ifname, "imq1"))
236 web_printf("%c'%s':{rx:0x0,tx:0x%llx}", comma, ifname, rx, tx);
237 else if (!strcmp(ifname, "imq2"))
238 web_printf("%c'%s':{rx:0x%llx,tx:0x0}", comma, ifname, rx, tx);
239 else
240 web_printf("%c'%s':{rx:0x%llx,tx:0x%llx}", comma, ifname, rx, tx);
242 comma = ',';
245 if (sfd >= 0) close(sfd);
246 fclose(f);
248 web_puts("};\n");
251 void asp_iptmon(int argc, char **argv) {
253 char comma;
254 char sa[256];
255 FILE *a;
256 char *exclude;
257 char *include;
259 char ip[INET6_ADDRSTRLEN];
261 int64_t tx, rx;
263 exclude = nvram_safe_get("cstats_exclude");
264 include = nvram_safe_get("cstats_include");
266 char br;
267 char name[] = "/proc/net/ipt_account/lanX";
269 web_puts("\n\niptmon={");
270 comma = ' ';
272 for(br=0 ; br<=3 ; br++) {
274 char wholenetstatsline = 1;
276 char bridge[2] = "0";
277 if (br!=0)
278 bridge[0]+=br;
279 else
280 strcpy(bridge, "");
282 sprintf(name, "/proc/net/ipt_account/lan%s", bridge);
284 if ((a = fopen(name, "r")) == NULL) continue;
286 if (!wholenetstatsline)
287 fgets(sa, sizeof(sa), a); // network
289 while (fgets(sa, sizeof(sa), a)) {
290 if(sscanf(sa,
291 "ip = %s bytes_src = %llu %*u %*u %*u %*u packets_src = %*u %*u %*u %*u %*u bytes_dst = %llu %*u %*u %*u %*u packets_dst = %*u %*u %*u %*u %*u time = %*u",
292 ip, &tx, &rx) != 3 ) continue;
294 if (find_word(exclude, ip)) {
295 wholenetstatsline = 0;
296 continue;
299 if (((find_word(include, ip)) || (wholenetstatsline == 1)) || ((nvram_get_int("cstats_all")) && ((rx > 0) || (tx > 0)) )) {
300 // if ((find_word(include, ip)) || (wholenetstatsline == 1)) {
301 // if ((tx > 0) || (rx > 0) || (wholenetstatsline == 1)) {
302 // if ((tx > 0) || (rx > 0)) {
303 web_printf("%c'%s':{rx:0x%llx,tx:0x%llx}", comma, ip, rx, tx);
304 comma = ',';
306 wholenetstatsline = 0;
308 fclose(a);
310 web_puts("};\n");
313 void asp_bandwidth(int argc, char **argv)
315 char *name;
316 int sig;
318 if ((nvram_get_int("rstats_enable") == 1) && (argc == 1)) {
319 if (strcmp(argv[0], "speed") == 0) {
320 sig = SIGUSR1;
321 name = "/var/spool/rstats-speed.js";
323 else {
324 sig = SIGUSR2;
325 name = "/var/spool/rstats-history.js";
327 unlink(name);
328 killall("rstats", sig);
329 f_wait_exists(name, 5);
330 do_file(name);
331 unlink(name);
335 void asp_ipt_bandwidth(int argc, char **argv)
337 char *name;
338 int sig;
340 if ((nvram_get_int("cstats_enable") == 1) && (argc == 1)) {
341 if (strcmp(argv[0], "speed") == 0) {
342 sig = SIGUSR1;
343 name = "/var/spool/cstats-speed.js";
345 else {
346 sig = SIGUSR2;
347 name = "/var/spool/cstats-history.js";
349 unlink(name);
350 killall("cstats", sig);
351 f_wait_exists(name, 5);
352 do_file(name);
353 unlink(name);