4 Copyright (C) 2006-2008 Jonathan Zarate
11 #include <sys/sysinfo.h>
13 #include <sys/ioctl.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <sys/types.h>
22 #include <sys/statfs.h>
24 #include <net/route.h>
30 int wait_file_exists(const char *name
, int max
, int invert
)
33 if (f_exists(name
) ^ invert
) return 1;
39 // to javascript-safe string
40 char *js_string(const char *s
)
46 if ((buffer
= malloc((strlen(s
) * 4) + 1)) != NULL
) {
48 while ((c
= *s
++) != 0) {
49 if ((c
== '"') || (c
== '\'') || (c
== '\\') || (!isprint(c
))) {
50 b
+= sprintf(b
, "\\x%02x", c
);
61 // to html-safe string
62 char *html_string(const char *s
)
68 if ((buffer
= malloc((strlen(s
) * 6) + 1)) != NULL
) {
70 while ((c
= *s
++) != 0) {
71 if ((c
== '&') || (c
== '<') || (c
== '>') || (c
== '"') || (c
== '\'') || (!isprint(c
))) {
72 b
+= sprintf(b
, "&#%d;", c
);
84 char *unix_string(const char *s
)
90 if ((buffer
= malloc(strlen(s
) + 1)) != NULL
) {
92 while ((c
= *s
++) != 0)
93 if (c
!= '\r') *b
++ = c
;
100 char *reltime(char *buf
, time_t t
)
108 sprintf(buf
, "%d day%s, %02d:%02d:%02d", days
, ((days
==1) ? "" : "s"), ((m
/ 60) % 24), (m
% 60), (int)(t
% 60));
112 int get_client_info(char *mac
, char *ifname
)
120 IP address HW type Flags HW address Mask Device
121 192.168.0.1 0x1 0x2 00:01:02:03:04:05 * vlan1
122 192.168.1.5 0x1 0x2 00:05:06:07:08:09 * br0
125 if ((f
= fopen("/proc/net/arp", "r")) != NULL
) {
126 while (fgets(s
, sizeof(s
), f
)) {
127 if (sscanf(s
, "%15s %*s %*s %17s %*s %16s", ips
, mac
, ifname
) == 3) {
128 if (inet_addr(ips
) == clientsai
.sin_addr
.s_addr
) {
143 // <% lanip(mode); %>
145 // 1 return first 3 octets (192.168.1)
146 // 2 return last octet (1)
147 // else return full (192.168.1.1)
149 void asp_lanip(int argc
, char **argv
)
155 mode
= argc
? *argv
[0] : 0;
157 if ((nv
= nvram_get("lan_ipaddr")) != NULL
) {
159 if ((p
= strrchr(s
, '.')) != NULL
) {
161 web_puts((mode
== '1') ? s
: (mode
== '2') ? (p
+ 1) : nv
);
166 void asp_lipp(int argc
, char **argv
)
172 // <% psup(process); %>
173 // returns 1 if process is running
175 void asp_psup(int argc
, char **argv
)
177 if (argc
== 1) web_printf("%d", pidof(argv
[0]) > 0);
180 void asp_vpn_ciphers(int argc
, char **argv
)
182 #ifdef TCONFIG_OPENVPN
186 fp
=popen("/usr/sbin/openvpn --show-ciphers | grep -v \"^$\" | awk '{print($1)}'", "r");
190 while (fgets(&buffer
[0], 16, fp
) != NULL
)
191 web_printf(",['%s','%s']", strtok(&buffer
[0],"\n"), &buffer
[0]);
199 total: used: free: shared: buffers: cached:
200 Mem: 14872576 12877824 1994752 0 1236992 4837376
222 unsigned long shared
;
223 unsigned long buffers
;
224 unsigned long cached
;
225 unsigned long swaptotal
;
226 unsigned long swapfree
;
227 unsigned long maxfreeram
;
230 static int get_memory(meminfo_t
*m
)
236 if ((f
= fopen("/proc/meminfo", "r")) != NULL
) {
237 while (fgets(s
, sizeof(s
), f
)) {
238 if (strncmp(s
, "Mem:", 4) == 0) {
239 if (sscanf(s
+ 6, "%ld %*d %ld %ld %ld %ld", &m
->total
, &m
->free
, &m
->shared
, &m
->buffers
, &m
->cached
) == 5)
242 else if (strncmp(s
, "SwapTotal:", 10) == 0) {
243 m
->swaptotal
= strtoul(s
+ 12, NULL
, 10) * 1024;
246 else if (strncmp(s
, "SwapFree:", 9) == 0) {
247 m
->swapfree
= strtoul(s
+ 11, NULL
, 10) * 1024;
255 memset(m
, 0, sizeof(*m
));
258 m
->maxfreeram
= m
->free
;
259 if (nvram_match("t_cafree", "1")) m
->maxfreeram
+= m
->cached
;
263 void asp_sysinfo(int argc
, char **argv
)
269 web_puts("\nsysinfo = {\n");
274 "\tuptime_s: '%s',\n"
275 "\tloads: [%ld, %ld, %ld],\n"
279 "\tbufferram: %ld,\n"
281 "\ttotalswap: %ld,\n"
283 "\ttotalfreeram: %ld,\n"
286 reltime(s
, si
.uptime
),
287 si
.loads
[0], si
.loads
[1], si
.loads
[2],
289 mem
.shared
, mem
.buffers
, mem
.cached
,
290 mem
.swaptotal
, mem
.swapfree
,
296 void asp_activeroutes(int argc
, char **argv
)
302 unsigned long gateway
;
312 web_puts("\nactiveroutes = [");
314 if ((f
= fopen("/proc/net/route", "r")) != NULL
) {
315 while (fgets(s
, sizeof(s
), f
)) {
316 if (sscanf(s
, "%16s%lx%lx%lx%*s%*s%u%lx", dev
, &dest
, &gateway
, &flags
, &metric
, &mask
) != 6) continue;
317 if ((flags
& RTF_UP
) == 0) continue;
320 strcpy(s_dest
, inet_ntoa(ia
));
323 strcpy(s_dest
, "default");
327 strcpy(s_gateway
, inet_ntoa(ia
));
330 strcpy(s_gateway
, "*");
333 strcpy(s_mask
, inet_ntoa(ia
));
334 web_printf("%s['%s','%s','%s','%s',%u]", n
? "," : "", dev
, s_dest
, s_gateway
, s_mask
, metric
);
342 void asp_cgi_get(int argc
, char **argv
)
347 for (i
= 0; i
< argc
; ++i
) {
348 v
= webcgi_get(argv
[i
]);
353 void asp_time(int argc
, char **argv
)
360 web_puts("Not Available");
363 strftime(s
, sizeof(s
), "%a, %d %b %Y %H:%M:%S %z", localtime(&t
));
368 void asp_wanup(int argc
, char **argv
)
370 web_puts(check_wanup() ? "1" : "0");
373 void asp_wanstatus(int argc
, char **argv
)
377 if ((using_dhcpc()) && (f_exists("/var/lib/misc/dhcpc.renewing"))) {
380 else if (check_wanup()) {
383 else if (f_exists("/var/lib/misc/wan.connecting")) {
392 void asp_link_uptime(int argc
, char **argv
)
402 if (f_read("/var/lib/misc/wantime", &uptime
, sizeof(uptime
)) == sizeof(uptime
)) {
403 reltime(buf
, si
.uptime
- uptime
);
409 void asp_bandwidth(int argc
, char **argv
)
414 if ((nvram_match("rstats_enable", "1")) && (argc
== 1)) {
415 if (strcmp(argv
[0], "speed") == 0) {
417 name
= "/var/spool/rstats-speed.js";
421 name
= "/var/spool/rstats-history.js";
424 killall("rstats", sig
);
425 wait_file_exists(name
, 5, 0);
431 void asp_rrule(int argc
, char **argv
)
436 i
= nvram_get_int("rruleN");
437 sprintf(s
, "rrule%d", i
);
438 web_puts("\nrrule = '");
439 web_putj(nvram_safe_get(s
));
440 web_printf("';\nrruleN = %d;\n", i
);
443 void asp_compmac(int argc
, char **argv
)
448 if (get_client_info(mac
, ifname
)) {
453 void asp_ident(int argc
, char **argv
)
455 web_puts(nvram_safe_get("router_name"));
458 void asp_statfs(int argc
, char **argv
)
462 if (argc
!= 2) return;
464 // used for /cifs/, /jffs/... if it returns squashfs type, assume it's not mounted
465 if ((statfs(argv
[0], &sf
) != 0) || (sf
.f_type
== 0x73717368))
466 memset(&sf
, 0, sizeof(sf
));
474 ((uint64_t)sf
.f_bsize
* sf
.f_blocks
),
475 ((uint64_t)sf
.f_bsize
* sf
.f_bfree
));
478 void asp_notice(int argc
, char **argv
)
483 if (argc
!= 1) return;
484 snprintf(s
, sizeof(s
), "/var/notice/%s", argv
[0]);
485 if (f_read_string(s
, buf
, sizeof(buf
)) <= 0) return;
489 void wo_wakeup(char *url
)
495 if ((mac
= webcgi_get("mac")) != NULL
) {
496 end
= mac
+ strlen(mac
);
498 while ((*mac
== ' ') || (*mac
== '\t') || (*mac
== '\r') || (*mac
== '\n')) ++mac
;
499 if (*mac
== 0) break;
502 while ((*p
!= 0) && (*p
!= ' ') && (*p
!= '\r') && (*p
!= '\n')) ++p
;
505 eval("ether-wake", "-i", nvram_safe_get("lan_ifname"), mac
);
512 void asp_dns(int argc
, char **argv
)
516 const dns_list_t
*dns
;
518 dns
= get_dns(); // static buffer
519 strcpy(s
, "\ndns = [");
520 for (i
= 0 ; i
< dns
->count
; ++i
) {
521 sprintf(s
+ strlen(s
), "%s'%s'", i
? "," : "", inet_ntoa(dns
->dns
[i
]));
527 void wo_resolve(char *url
)
537 web_puts("\nresolve_data = [\n");
538 if ((p
= webcgi_get("ip")) != NULL
) {
539 while ((ip
= strsep(&p
, ",")) != NULL
) {
540 ia
.s_addr
= inet_addr(ip
);
541 he
= gethostbyaddr(&ia
, sizeof(ia
), AF_INET
);
542 js
= js_string(he
? he
->h_name
: "");
543 web_printf("%c['%s','%s']", comma
, inet_ntoa(ia
), js
);