4 Copyright (C) 2006-2009 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>
29 // to javascript-safe string
30 char *js_string(const char *s
)
36 if ((buffer
= malloc((strlen(s
) * 4) + 1)) != NULL
) {
38 while ((c
= *s
++) != 0) {
39 if ((c
== '"') || (c
== '\'') || (c
== '\\') || (!isprint(c
))) {
40 b
+= sprintf(b
, "\\x%02x", c
);
51 // to html-safe string
52 char *html_string(const char *s
)
58 if ((buffer
= malloc((strlen(s
) * 6) + 1)) != NULL
) {
60 while ((c
= *s
++) != 0) {
61 if ((c
== '&') || (c
== '<') || (c
== '>') || (c
== '"') || (c
== '\'') || (!isprint(c
))) {
62 b
+= sprintf(b
, "&#%d;", c
);
74 char *unix_string(const char *s
)
80 if ((buffer
= malloc(strlen(s
) + 1)) != NULL
) {
82 while ((c
= *s
++) != 0)
83 if (c
!= '\r') *b
++ = c
;
90 char *reltime(char *buf
, time_t t
)
98 sprintf(buf
, "%d day%s, %02d:%02d:%02d", days
, ((days
==1) ? "" : "s"), ((m
/ 60) % 24), (m
% 60), (int)(t
% 60));
102 int get_client_info(char *mac
, char *ifname
)
110 IP address HW type Flags HW address Mask Device
111 192.168.0.1 0x1 0x2 00:01:02:03:04:05 * vlan1
112 192.168.1.5 0x1 0x2 00:05:06:07:08:09 * br0
115 if ((f
= fopen("/proc/net/arp", "r")) != NULL
) {
116 while (fgets(s
, sizeof(s
), f
)) {
117 if (sscanf(s
, "%15s %*s %*s %17s %*s %16s", ips
, mac
, ifname
) == 3) {
118 if (inet_addr(ips
) == clientsai
.sin_addr
.s_addr
) {
133 // <% lanip(mode); %>
135 // 1 return first 3 octets (192.168.1)
136 // 2 return last octet (1)
137 // else return full (192.168.1.1)
139 void asp_lanip(int argc
, char **argv
)
145 mode
= argc
? *argv
[0] : 0;
147 if ((nv
= nvram_get("lan_ipaddr")) != NULL
) {
149 if ((p
= strrchr(s
, '.')) != NULL
) {
151 web_puts((mode
== '1') ? s
: (mode
== '2') ? (p
+ 1) : nv
);
156 void asp_lipp(int argc
, char **argv
)
162 // <% psup(process); %>
163 // returns 1 if process is running
165 void asp_psup(int argc
, char **argv
)
167 if (argc
== 1) web_printf("%d", pidof(argv
[0]) > 0);
172 total: used: free: shared: buffers: cached:
173 Mem: 14872576 12877824 1994752 0 1236992 4837376
195 unsigned long shared
;
196 unsigned long buffers
;
197 unsigned long cached
;
198 unsigned long swaptotal
;
199 unsigned long swapfree
;
200 unsigned long maxfreeram
;
203 static int get_memory(meminfo_t
*m
)
209 if ((f
= fopen("/proc/meminfo", "r")) != NULL
) {
210 while (fgets(s
, sizeof(s
), f
)) {
211 if (strncmp(s
, "Mem:", 4) == 0) {
212 if (sscanf(s
+ 6, "%ld %*d %ld %ld %ld %ld", &m
->total
, &m
->free
, &m
->shared
, &m
->buffers
, &m
->cached
) == 5)
215 else if (strncmp(s
, "SwapTotal:", 10) == 0) {
216 m
->swaptotal
= strtoul(s
+ 12, NULL
, 10) * 1024;
219 else if (strncmp(s
, "SwapFree:", 9) == 0) {
220 m
->swapfree
= strtoul(s
+ 11, NULL
, 10) * 1024;
228 memset(m
, 0, sizeof(*m
));
231 m
->maxfreeram
= m
->free
;
232 if (nvram_match("t_cafree", "1")) m
->maxfreeram
+= m
->cached
;
236 void asp_sysinfo(int argc
, char **argv
)
242 web_puts("\nsysinfo = {\n");
247 "\tuptime_s: '%s',\n"
248 "\tloads: [%ld, %ld, %ld],\n"
252 "\tbufferram: %ld,\n"
254 "\ttotalswap: %ld,\n"
256 "\ttotalfreeram: %ld,\n"
259 reltime(s
, si
.uptime
),
260 si
.loads
[0], si
.loads
[1], si
.loads
[2],
262 mem
.shared
, mem
.buffers
, mem
.cached
,
263 mem
.swaptotal
, mem
.swapfree
,
269 void asp_activeroutes(int argc
, char **argv
)
275 unsigned long gateway
;
285 web_puts("\nactiveroutes = [");
287 if ((f
= fopen("/proc/net/route", "r")) != NULL
) {
288 while (fgets(s
, sizeof(s
), f
)) {
289 if (sscanf(s
, "%16s%lx%lx%lx%*s%*s%u%lx", dev
, &dest
, &gateway
, &flags
, &metric
, &mask
) != 6) continue;
290 if ((flags
& RTF_UP
) == 0) continue;
293 strcpy(s_dest
, inet_ntoa(ia
));
296 strcpy(s_dest
, "default");
300 strcpy(s_gateway
, inet_ntoa(ia
));
303 strcpy(s_gateway
, "*");
306 strcpy(s_mask
, inet_ntoa(ia
));
307 web_printf("%s['%s','%s','%s','%s',%u]", n
? "," : "", dev
, s_dest
, s_gateway
, s_mask
, metric
);
315 void asp_cgi_get(int argc
, char **argv
)
320 for (i
= 0; i
< argc
; ++i
) {
321 v
= webcgi_get(argv
[i
]);
326 void asp_time(int argc
, char **argv
)
333 web_puts("Not Available");
336 strftime(s
, sizeof(s
), "%a, %d %b %Y %H:%M:%S %z", localtime(&t
));
341 void asp_wanup(int argc
, char **argv
)
343 web_puts(check_wanup() ? "1" : "0");
346 void asp_wanstatus(int argc
, char **argv
)
350 if ((using_dhcpc()) && (f_exists("/var/lib/misc/dhcpc.renewing"))) {
353 else if (check_wanup()) {
356 else if (f_exists("/var/lib/misc/wan.connecting")) {
365 void asp_link_uptime(int argc
, char **argv
)
375 if (f_read("/var/lib/misc/wantime", &uptime
, sizeof(uptime
)) == sizeof(uptime
)) {
376 reltime(buf
, si
.uptime
- uptime
);
382 void asp_rrule(int argc
, char **argv
)
387 i
= nvram_get_int("rruleN");
388 sprintf(s
, "rrule%d", i
);
389 web_puts("\nrrule = '");
390 web_putj(nvram_safe_get(s
));
391 web_printf("';\nrruleN = %d;\n", i
);
394 void asp_compmac(int argc
, char **argv
)
399 if (get_client_info(mac
, ifname
)) {
404 void asp_ident(int argc
, char **argv
)
406 web_puth(nvram_safe_get("router_name"));
409 void asp_statfs(int argc
, char **argv
)
413 if (argc
!= 2) return;
415 // used for /cifs/, /jffs/... if it returns squashfs type, assume it's not mounted
416 if ((statfs(argv
[0], &sf
) != 0) || (sf
.f_type
== 0x73717368))
417 memset(&sf
, 0, sizeof(sf
));
425 ((uint64_t)sf
.f_bsize
* sf
.f_blocks
),
426 ((uint64_t)sf
.f_bsize
* sf
.f_bfree
));
429 void asp_notice(int argc
, char **argv
)
434 if (argc
!= 1) return;
435 snprintf(s
, sizeof(s
), "/var/notice/%s", argv
[0]);
436 if (f_read_string(s
, buf
, sizeof(buf
)) <= 0) return;
440 void wo_wakeup(char *url
)
446 if ((mac
= webcgi_get("mac")) != NULL
) {
447 end
= mac
+ strlen(mac
);
449 while ((*mac
== ' ') || (*mac
== '\t') || (*mac
== '\r') || (*mac
== '\n')) ++mac
;
450 if (*mac
== 0) break;
453 while ((*p
!= 0) && (*p
!= ' ') && (*p
!= '\r') && (*p
!= '\n')) ++p
;
456 eval("ether-wake", "-i", nvram_safe_get("lan_ifname"), mac
);
463 void asp_dns(int argc
, char **argv
)
467 const dns_list_t
*dns
;
469 dns
= get_dns(); // static buffer
470 strcpy(s
, "\ndns = [");
471 for (i
= 0 ; i
< dns
->count
; ++i
) {
472 sprintf(s
+ strlen(s
), "%s'%s'", i
? "," : "", inet_ntoa(dns
->dns
[i
]));
478 void wo_resolve(char *url
)
488 web_puts("\nresolve_data = [\n");
489 if ((p
= webcgi_get("ip")) != NULL
) {
490 while ((ip
= strsep(&p
, ",")) != NULL
) {
491 ia
.s_addr
= inet_addr(ip
);
492 he
= gethostbyaddr(&ia
, sizeof(ia
), AF_INET
);
493 js
= js_string(he
? he
->h_name
: "");
494 web_printf("%c['%s','%s']", comma
, inet_ntoa(ia
), js
);