4 Copyright (C) 2006-2009 Jonathan Zarate
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
)
28 if (stat(hfn
, &st
) == 0) {
35 killall("rstats", SIGHUP
);
36 for (i
= 10; i
> 0; --i
) {
37 if ((stat(hfn
, &st
) == 0) && (st
.st_mtime
!= t
)) break;
41 send_error(500, NULL
, NULL
);
44 send_header(200, NULL
, mime_binary
, 0);
48 void wo_iptbackup(char *url
)
54 if (stat(ifn
, &st
) == 0) {
61 killall("cstats", SIGHUP
);
62 for (i
= 10; i
> 0; --i
) {
63 if ((stat(ifn
, &st
) == 0) && (st
.st_mtime
!= t
)) break;
67 send_error(500, NULL
, NULL
);
70 send_header(200, NULL
, mime_binary
, 0);
74 void wi_bwmrestore(char *url
, int len
, char *boundary
)
86 error
= "Error reading file";
89 if (!skip_header(&len
)) {
93 if ((len
< 64) || (len
> 10240)) {
97 if ((buf
= malloc(len
)) == NULL
) {
98 error
= "Not enough memory";
102 n
= web_read(buf
, len
);
105 sprintf(tmp
, "%s.new", hfn
);
106 if (f_write(tmp
, buf
, n
, 0, 0600) != n
) {
108 error
= "Error writing temporary file";
111 f_write("/var/tmp/rstats-load", NULL
, 0, 0, 0600);
112 killall("rstats", SIGHUP
);
116 rboot
= 1; // used as "ok"
121 if (error
!= NULL
) resmsg_set(error
);
124 void wi_iptrestore(char *url
, int len
, char *boundary
)
136 error
= "Error reading file";
139 if (!skip_header(&len
)) {
143 if ((len
< 64) || (len
> 10240)) {
147 if ((buf
= malloc(len
)) == NULL
) {
148 error
= "Not enough memory";
152 n
= web_read(buf
, len
);
155 sprintf(tmp
, "%s.new", ifn
);
156 if (f_write(tmp
, buf
, n
, 0, 0600) != n
) {
158 error
= "Error writing temporary file";
161 f_write("/var/tmp/cstats-load", NULL
, 0, 0, 0600);
162 killall("cstats", SIGHUP
);
166 rboot
= 1; // used as "ok"
171 if (error
!= NULL
) resmsg_set(error
);
174 void wo_bwmrestore(char *url
)
177 redirect("/bwm-daily.asp");
180 parse_asp("error.asp");
184 void wo_iptrestore(char *url
)
187 redirect("/ipt-daily.asp");
190 parse_asp("error.asp");
194 void asp_netdev(int argc
, char **argv
)
198 unsigned long rx
, tx
;
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
); // "
210 while (fgets(buf
, sizeof(buf
), f
)) {
211 if ((p
= strchr(buf
, ':')) == NULL
) continue;
213 if ((ifname
= strrchr(buf
, ' ')) == NULL
) ifname
= buf
;
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
);
228 void asp_iptmon(int argc
, char **argv
) {
234 char ip
[INET6_ADDRSTRLEN
];
236 unsigned long tx
, rx
;
239 char name
[] = "/proc/net/ipt_account/lanX";
241 web_puts("\n\niptmon={");
244 for(br
=0 ; br
<=3 ; br
++) {
245 char bridge
[2] = "0";
251 sprintf(name
, "/proc/net/ipt_account/lan%s", bridge
);
253 if ((a
= fopen(name
, "r")) == NULL
) continue;
255 // fgets(sa, sizeof(sa), a); // network
256 while (fgets(sa
, sizeof(sa
), a
)) {
258 "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",
259 ip
, &tx
, &rx
) != 3 ) continue;
260 if ((tx
> 0) || (rx
> 0)) {
261 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma
, ip
, rx
, tx
);
270 void asp_bandwidth(int argc
, char **argv
)
275 if ((nvram_get_int("rstats_enable") == 1) && (argc
== 1)) {
276 if (strcmp(argv
[0], "speed") == 0) {
278 name
= "/var/spool/rstats-speed.js";
282 name
= "/var/spool/rstats-history.js";
285 killall("rstats", sig
);
286 f_wait_exists(name
, 5);
292 void asp_ipt_bandwidth(int argc
, char **argv
)
297 if ((nvram_get_int("cstats_enable") == 1) && (argc
== 1)) {
298 if (strcmp(argv
[0], "speed") == 0) {
300 name
= "/var/spool/cstats-speed.js";
304 name
= "/var/spool/cstats-history.js";
307 killall("cstats", sig
);
308 f_wait_exists(name
, 5);
314 void asp_iptraffic(int argc
, char **argv
) {
322 char ip
[INET6_ADDRSTRLEN
];
324 unsigned long tx_bytes
, rx_bytes
;
325 unsigned long tp_tcp
, rp_tcp
;
326 unsigned long tp_udp
, rp_udp
;
327 unsigned long tp_icmp
, rp_icmp
;
328 unsigned long ct_tcp
, ct_udp
;
331 // needs extra tweaks in the code before this works as it should
332 // so we'll stick to IPv4-only for now...
333 // #if defined(TCONFIG_IPV6) && defined(LINUX26)
334 // const char conntrack[] = "/proc/net/nf_conntrack";
336 const char conntrack
[] = "/proc/net/ip_conntrack";
339 if ((b
= fopen(conntrack
, "r")) == NULL
) return;
342 char name
[] = "/proc/net/ipt_account/lanX";
344 web_puts("\n\niptraffic=[");
347 for(br
=0 ; br
<=3 ; br
++) {
348 char bridge
[2] = "0";
354 sprintf(name
, "/proc/net/ipt_account/lan%s", bridge
);
356 if ((a
= fopen(name
, "r")) == NULL
) continue;
358 fgets(sa
, sizeof(sa
), a
); // network
359 while (fgets(sa
, sizeof(sa
), a
)) {
361 "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",
362 ip
, &tx_bytes
, &tp_tcp
, &tp_udp
, &tp_icmp
, &rx_bytes
, &rp_tcp
, &rp_udp
, &rp_icmp
) != 9 ) continue;
363 if ((tx_bytes
> 0) || (rx_bytes
> 0)) {
367 while (fgets(sb
, sizeof(sb
), b
)) {
368 if ((p
= strstr(sb
, ip
)) == NULL
) continue;
369 if (strncmp(sb
, "tcp", 3) == 0) ct_tcp
++;
370 if (strncmp(sb
, "udp", 3) == 0) ct_udp
++;
372 web_printf("%c['%s', %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu]",
373 comma
, ip
, tx_bytes
, rx_bytes
, tp_tcp
, rp_tcp
, tp_udp
, rp_udp
, tp_icmp
, rp_icmp
, ct_tcp
, ct_udp
);