OpenSSL update to 1.0.1g
[tomato.git] / release / src / router / httpd / bwm.c
blob1d7dcd1a7017a3eb03b97b427c5235657f5d8fc1
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 web_printf("%c'%s':{rx:0x%llx,tx:0x%llx}", comma, ifname, rx, tx);
236 comma = ',';
239 if (sfd >= 0) close(sfd);
240 fclose(f);
242 web_puts("};\n");
245 void asp_iptmon(int argc, char **argv) {
247 char comma;
248 char sa[256];
249 FILE *a;
250 char *exclude;
251 char *include;
253 char ip[INET6_ADDRSTRLEN];
255 int64_t tx, rx;
257 exclude = nvram_safe_get("cstats_exclude");
258 include = nvram_safe_get("cstats_include");
260 char br;
261 char name[] = "/proc/net/ipt_account/lanX";
263 web_puts("\n\niptmon={");
264 comma = ' ';
266 for(br=0 ; br<=3 ; br++) {
268 char wholenetstatsline = 1;
270 char bridge[2] = "0";
271 if (br!=0)
272 bridge[0]+=br;
273 else
274 strcpy(bridge, "");
276 sprintf(name, "/proc/net/ipt_account/lan%s", bridge);
278 if ((a = fopen(name, "r")) == NULL) continue;
280 if (!wholenetstatsline)
281 fgets(sa, sizeof(sa), a); // network
283 while (fgets(sa, sizeof(sa), a)) {
284 if(sscanf(sa,
285 "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",
286 ip, &tx, &rx) != 3 ) continue;
288 if (find_word(exclude, ip)) {
289 wholenetstatsline = 0;
290 continue;
293 if (((find_word(include, ip)) || (wholenetstatsline == 1)) || ((nvram_get_int("cstats_all")) && ((rx > 0) || (tx > 0)) )) {
294 // if ((find_word(include, ip)) || (wholenetstatsline == 1)) {
295 // if ((tx > 0) || (rx > 0) || (wholenetstatsline == 1)) {
296 // if ((tx > 0) || (rx > 0)) {
297 web_printf("%c'%s':{rx:0x%llx,tx:0x%llx}", comma, ip, rx, tx);
298 comma = ',';
300 wholenetstatsline = 0;
302 fclose(a);
304 web_puts("};\n");
307 void asp_bandwidth(int argc, char **argv)
309 char *name;
310 int sig;
312 if ((nvram_get_int("rstats_enable") == 1) && (argc == 1)) {
313 if (strcmp(argv[0], "speed") == 0) {
314 sig = SIGUSR1;
315 name = "/var/spool/rstats-speed.js";
317 else {
318 sig = SIGUSR2;
319 name = "/var/spool/rstats-history.js";
321 unlink(name);
322 killall("rstats", sig);
323 f_wait_exists(name, 5);
324 do_file(name);
325 unlink(name);
329 void asp_ipt_bandwidth(int argc, char **argv)
331 char *name;
332 int sig;
334 if ((nvram_get_int("cstats_enable") == 1) && (argc == 1)) {
335 if (strcmp(argv[0], "speed") == 0) {
336 sig = SIGUSR1;
337 name = "/var/spool/cstats-speed.js";
339 else {
340 sig = SIGUSR2;
341 name = "/var/spool/cstats-history.js";
343 unlink(name);
344 killall("cstats", sig);
345 f_wait_exists(name, 5);
346 do_file(name);
347 unlink(name);