4 Copyright (C) 2006-2009 Jonathan Zarate
11 #include <sys/ioctl.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
)
29 if (stat(hfn
, &st
) == 0) {
36 killall("rstats", SIGHUP
);
37 for (i
= 10; i
> 0; --i
) {
38 if ((stat(hfn
, &st
) == 0) && (st
.st_mtime
!= t
)) break;
42 send_error(500, NULL
, NULL
);
45 send_header(200, NULL
, mime_binary
, 0);
49 void wo_iptbackup(char *url
)
55 if (stat(ifn
, &st
) == 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;
68 send_error(500, NULL
, NULL
);
71 send_header(200, NULL
, mime_binary
, 0);
75 void wi_bwmrestore(char *url
, int len
, char *boundary
)
87 error
= "Error reading file";
90 if (!skip_header(&len
)) {
94 if ((len
< 64) || (len
> 10240)) {
98 if ((buf
= malloc(len
)) == NULL
) {
99 error
= "Not enough memory";
103 n
= web_read(buf
, len
);
106 sprintf(tmp
, "%s.new", hfn
);
107 if (f_write(tmp
, buf
, n
, 0, 0600) != n
) {
109 error
= "Error writing temporary file";
112 f_write("/var/tmp/rstats-load", NULL
, 0, 0, 0600);
113 killall("rstats", SIGHUP
);
117 rboot
= 1; // used as "ok"
122 if (error
!= NULL
) resmsg_set(error
);
125 void wi_iptrestore(char *url
, int len
, char *boundary
)
137 error
= "Error reading file";
140 if (!skip_header(&len
)) {
144 if ((len
< 64) || (len
> 102400)) { // up to 100k
148 if ((buf
= malloc(len
)) == NULL
) {
149 error
= "Not enough memory";
153 n
= web_read(buf
, len
);
156 sprintf(tmp
, "%s.new", ifn
);
157 if (f_write(tmp
, buf
, n
, 0, 0600) != n
) {
159 error
= "Error writing temporary file";
162 f_write("/var/tmp/cstats-load", NULL
, 0, 0, 0600);
163 killall("cstats", SIGHUP
);
167 rboot
= 1; // used as "ok"
172 if (error
!= NULL
) resmsg_set(error
);
175 void wo_bwmrestore(char *url
)
178 redirect("/bwm-daily.asp");
181 parse_asp("error.asp");
185 void wo_iptrestore(char *url
)
188 redirect("/ipt-daily.asp");
191 parse_asp("error.asp");
195 void asp_netdev(int argc
, char **argv
)
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
); // "
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;
221 if ((ifname
= strrchr(buf
, ' ')) == NULL
) ifname
= buf
;
223 // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
224 if ((strcmp(ifname
, "lo") == 0) || (find_word(exclude
, ifname
))) continue;
226 // skip down interfaces
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
);
239 if (sfd
>= 0) close(sfd
);
245 void asp_iptmon(int argc
, char **argv
) {
253 char ip
[INET6_ADDRSTRLEN
];
257 exclude
= nvram_safe_get("cstats_exclude");
258 include
= nvram_safe_get("cstats_include");
261 char name
[] = "/proc/net/ipt_account/lanX";
263 web_puts("\n\niptmon={");
266 for(br
=0 ; br
<=3 ; br
++) {
268 char wholenetstatsline
= 1;
270 char bridge
[2] = "0";
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
)) {
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;
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
);
300 wholenetstatsline
= 0;
307 void asp_bandwidth(int argc
, char **argv
)
312 if ((nvram_get_int("rstats_enable") == 1) && (argc
== 1)) {
313 if (strcmp(argv
[0], "speed") == 0) {
315 name
= "/var/spool/rstats-speed.js";
319 name
= "/var/spool/rstats-history.js";
322 killall("rstats", sig
);
323 f_wait_exists(name
, 5);
329 void asp_ipt_bandwidth(int argc
, char **argv
)
334 if ((nvram_get_int("cstats_enable") == 1) && (argc
== 1)) {
335 if (strcmp(argv
[0], "speed") == 0) {
337 name
= "/var/spool/cstats-speed.js";
341 name
= "/var/spool/cstats-history.js";
344 killall("cstats", sig
);
345 f_wait_exists(name
, 5);