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
= 10; i
> 0; --i
) {
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
> 10240)) {
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
)
199 unsigned long rx
, tx
;
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, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &rx
, &tx
) != 2) continue;
235 if (!strcmp(ifname
, "imq1"))
236 web_printf("%c'%s':{rx:0x0,tx:0x%lx}", comma
, ifname
, rx
, tx
);
237 else if (!strcmp(ifname
, "imq2"))
238 web_printf("%c'%s':{rx:0x%lx,tx:0x0}", comma
, ifname
, rx
, tx
);
240 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma
, ifname
, rx
, tx
);
245 if (sfd
>= 0) close(sfd
);
251 void asp_iptmon(int argc
, char **argv
) {
259 char ip
[INET6_ADDRSTRLEN
];
261 unsigned long tx
, rx
;
263 exclude
= nvram_safe_get("cstats_exclude");
264 include
= nvram_safe_get("cstats_include");
267 char name
[] = "/proc/net/ipt_account/lanX";
269 web_puts("\n\niptmon={");
272 for(br
=0 ; br
<=3 ; br
++) {
274 char wholenetstatsline
= 1;
276 char bridge
[2] = "0";
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
)) {
292 "ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %*u %*u %*u %*u bytes_dst = %lu %*u %*u %*u %*u packets_dst = %*u %*u %*u %*u %*u time = %*u",
294 "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",
296 ip
, &tx
, &rx
) != 3 ) continue;
298 if (find_word(exclude
, ip
)) {
299 wholenetstatsline
= 0;
303 if (((find_word(include
, ip
)) || (wholenetstatsline
== 1)) || ((nvram_get_int("cstats_all")) && ((rx
> 0) || (tx
> 0)) )) {
304 // if ((find_word(include, ip)) || (wholenetstatsline == 1)) {
305 // if ((tx > 0) || (rx > 0) || (wholenetstatsline == 1)) {
306 // if ((tx > 0) || (rx > 0)) {
307 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma
, ip
, rx
, tx
);
310 wholenetstatsline
= 0;
317 void asp_bandwidth(int argc
, char **argv
)
322 if ((nvram_get_int("rstats_enable") == 1) && (argc
== 1)) {
323 if (strcmp(argv
[0], "speed") == 0) {
325 name
= "/var/spool/rstats-speed.js";
329 name
= "/var/spool/rstats-history.js";
332 killall("rstats", sig
);
333 f_wait_exists(name
, 5);
339 void asp_ipt_bandwidth(int argc
, char **argv
)
344 if ((nvram_get_int("cstats_enable") == 1) && (argc
== 1)) {
345 if (strcmp(argv
[0], "speed") == 0) {
347 name
= "/var/spool/cstats-speed.js";
351 name
= "/var/spool/cstats-history.js";
354 killall("cstats", sig
);
355 f_wait_exists(name
, 5);
361 void asp_iptraffic(int argc
, char **argv
) {
369 char ip
[INET6_ADDRSTRLEN
];
373 unsigned long tx_bytes
, rx_bytes
;
374 unsigned long tp_tcp
, rp_tcp
;
375 unsigned long tp_udp
, rp_udp
;
376 unsigned long tp_icmp
, rp_icmp
;
377 unsigned long ct_tcp
, ct_udp
;
379 exclude
= nvram_safe_get("cstats_exclude");
381 // needs extra tweaks in the code before this works as it should
382 // so we'll stick to IPv4-only for now...
383 // #if defined(TCONFIG_IPV6) && defined(LINUX26)
384 // const char conntrack[] = "/proc/net/nf_conntrack";
386 const char conntrack
[] = "/proc/net/ip_conntrack";
389 if ((b
= fopen(conntrack
, "r")) == NULL
) return;
392 char name
[] = "/proc/net/ipt_account/lanX";
394 web_puts("\n\niptraffic=[");
397 for(br
=0 ; br
<=3 ; br
++) {
398 char bridge
[2] = "0";
404 sprintf(name
, "/proc/net/ipt_account/lan%s", bridge
);
406 if ((a
= fopen(name
, "r")) == NULL
) continue;
408 fgets(sa
, sizeof(sa
), a
); // network
409 while (fgets(sa
, sizeof(sa
), a
)) {
412 "ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %lu %lu %lu %*u bytes_dst = %lu %*u %*u %*u %*u packets_dst = %*u %lu %lu %lu %*u time = %*u",
414 "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",
416 ip
, &tx_bytes
, &tp_tcp
, &tp_udp
, &tp_icmp
, &rx_bytes
, &rp_tcp
, &rp_udp
, &rp_icmp
) != 9 ) continue;
418 if (find_word(exclude
, ip
)) continue ;
420 if ((tx_bytes
> 0) || (rx_bytes
> 0)){
424 while (fgets(sb
, sizeof(sb
), b
)) {
425 if ((p
= strstr(sb
, ip
)) == NULL
) continue;
426 if (strncmp(sb
, "tcp", 3) == 0) ct_tcp
++;
427 if (strncmp(sb
, "udp", 3) == 0) ct_udp
++;
429 web_printf("%c['%s', %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu]",
430 comma
, ip
, rx_bytes
, tx_bytes
, rp_tcp
, tp_tcp
, rp_udp
, tp_udp
, rp_icmp
, tp_icmp
, ct_tcp
, ct_udp
);