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
)
107 char ip
[INET6_ADDRSTRLEN
];
109 char ip
[INET_ADDRSTRLEN
];
113 # ip neigh show fe80:0:0::201:02ff:fe03:0405
114 fe80::201:2ff:fe3:405 dev br0 lladdr 00:01:02:03:04:05 REACHABLE
116 if (clientsai
.ss_family
== AF_INET
) {
117 inet_ntop(clientsai
.ss_family
, &(((struct sockaddr_in
*)&clientsai
)->sin_addr
), ip
, sizeof(ip
));
118 sprintf(s
, "ip neigh show %s", ip
);
121 else if (clientsai
.ss_family
== AF_INET6
) {
122 inet_ntop(clientsai
.ss_family
, &(((struct sockaddr_in6
*)&clientsai
)->sin6_addr
), ip
, sizeof(ip
));
123 if (IN6_IS_ADDR_V4MAPPED( &(((struct sockaddr_in6
*)&clientsai
)->sin6_addr
) ))
124 sprintf(s
, "ip neigh show %s", ip
+ 7); // chop off the ::ffff: to get the ipv4 bit
126 sprintf(s
, "ip neigh show %s", ip
);
130 if ((f
= popen(s
, "r")) != NULL
) {
131 while (fgets(s
, sizeof(s
), f
)) {
132 if (sscanf(s
, "%*s dev %16s lladdr %17s %*s", ifname
, mac
) == 2) {
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 wo_vpn_status(char *url
)
182 #ifdef TCONFIG_OPENVPN
190 if ( (str
= webcgi_get("server")) )
192 else if ( (str
= webcgi_get("client")) )
195 num
= str
? atoi(str
): 0;
196 if ( type
&& num
> 0 )
198 // Trigger OpenVPN to update the status file
199 snprintf(&buf
[0], sizeof(buf
), "vpn%s%d", type
, num
);
200 killall(&buf
[0], SIGUSR2
);
202 // Give it a chance to update the file
205 // Read the status file and repeat it verbatim to the caller
206 snprintf(&buf
[0], sizeof(buf
), "/etc/openvpn/%s%d/status", type
, num
);
207 fp
= fopen(&buf
[0], "r");
210 while (fgets(&buf
[0], sizeof(buf
), fp
) != NULL
)
220 total: used: free: shared: buffers: cached:
221 Mem: 14872576 12877824 1994752 0 1236992 4837376
243 unsigned long shared
;
244 unsigned long buffers
;
245 unsigned long cached
;
246 unsigned long swaptotal
;
247 unsigned long swapfree
;
248 unsigned long maxfreeram
;
251 static int get_memory(meminfo_t
*m
)
257 memset(m
, 0, sizeof(*m
));
258 if ((f
= fopen("/proc/meminfo", "r")) != NULL
) {
259 while (fgets(s
, sizeof(s
), f
)) {
261 if (strncmp(s
, "MemTotal:", 9) == 0) {
262 m
->total
= strtoul(s
+ 12, NULL
, 10) * 1024;
265 else if (strncmp(s
, "MemFree:", 8) == 0) {
266 m
->free
= strtoul(s
+ 12, NULL
, 10) * 1024;
269 else if (strncmp(s
, "Buffers:", 8) == 0) {
270 m
->buffers
= strtoul(s
+ 12, NULL
, 10) * 1024;
273 else if (strncmp(s
, "Cached:", 7) == 0) {
274 m
->cached
= strtoul(s
+ 12, NULL
, 10) * 1024;
278 if (strncmp(s
, "Mem:", 4) == 0) {
279 if (sscanf(s
+ 6, "%ld %*d %ld %ld %ld %ld", &m
->total
, &m
->free
, &m
->shared
, &m
->buffers
, &m
->cached
) == 5)
283 else if (strncmp(s
, "SwapTotal:", 10) == 0) {
284 m
->swaptotal
= strtoul(s
+ 12, NULL
, 10) * 1024;
287 else if (strncmp(s
, "SwapFree:", 9) == 0) {
288 m
->swapfree
= strtoul(s
+ 11, NULL
, 10) * 1024;
300 m
->maxfreeram
= m
->free
;
301 if (nvram_match("t_cafree", "1")) m
->maxfreeram
+= (m
->cached
+ m
->buffers
);
305 void asp_sysinfo(int argc
, char **argv
)
311 web_puts("\nsysinfo = {\n");
316 "\tuptime_s: '%s',\n"
317 "\tloads: [%ld, %ld, %ld],\n"
321 "\tbufferram: %ld,\n"
323 "\ttotalswap: %ld,\n"
325 "\ttotalfreeram: %ld,\n"
328 reltime(s
, si
.uptime
),
329 si
.loads
[0], si
.loads
[1], si
.loads
[2],
331 mem
.shared
, mem
.buffers
, mem
.cached
,
332 mem
.swaptotal
, mem
.swapfree
,
338 void asp_activeroutes(int argc
, char **argv
)
344 unsigned long gateway
;
354 web_puts("\nactiveroutes = [");
356 if ((f
= fopen("/proc/net/route", "r")) != NULL
) {
357 while (fgets(s
, sizeof(s
), f
)) {
358 if (sscanf(s
, "%16s%lx%lx%lx%*s%*s%u%lx", dev
, &dest
, &gateway
, &flags
, &metric
, &mask
) != 6) continue;
359 if ((flags
& RTF_UP
) == 0) continue;
362 strcpy(s_dest
, inet_ntoa(ia
));
365 strcpy(s_dest
, "default");
369 strcpy(s_gateway
, inet_ntoa(ia
));
372 strcpy(s_gateway
, "*");
375 strcpy(s_mask
, inet_ntoa(ia
));
376 web_printf("%s['%s','%s','%s','%s',%u]", n
? "," : "", dev
, s_dest
, s_gateway
, s_mask
, metric
);
385 struct sockaddr_in6 snaddr6
;
386 char addr6
[40], nhop6
[40];
388 if (nvram_invmatch("ipv6_service", "")) &&
389 (f
= fopen("/proc/net/ipv6_route", "r")) != NULL
) {
390 while (fgets(s
, sizeof(s
), f
)) {
391 if (sscanf(s
, "%32s%x%*s%*s%32s%x%*s%*s%lx%s\n",
392 addr6x
+14, &pxlen
, addr6x
+40+7, &metric
, &flags
, dev
) != 6) continue;
394 if ((flags
& RTF_UP
) == 0) continue;
400 if (i
== 40) { // nul terminator for 1st address?
401 addr6x
[39] = 0; // Fixup... need 0 instead of ':'.
402 ++p
; // Skip and continue.
411 } while (i
< 40+28+7);
413 inet_pton(AF_INET6
, addr6x
, (struct sockaddr
*) &snaddr6
.sin6_addr
);
414 inet_ntop(AF_INET6
, &snaddr6
.sin6_addr
, addr6
, sizeof(addr6
));
415 inet_pton(AF_INET6
, addr6x
+ 40, (struct sockaddr
*) &snaddr6
.sin6_addr
);
416 inet_ntop(AF_INET6
, &snaddr6
.sin6_addr
, nhop6
, sizeof(nhop6
));
418 web_printf("%s['%s','%s/%d','%s','',%u]", n
? "," : "", dev
, addr6
, pxlen
, nhop6
, metric
);
429 void asp_cgi_get(int argc
, char **argv
)
434 for (i
= 0; i
< argc
; ++i
) {
435 v
= webcgi_get(argv
[i
]);
440 void asp_time(int argc
, char **argv
)
447 web_puts("Not Available");
450 strftime(s
, sizeof(s
), "%a, %d %b %Y %H:%M:%S %z", localtime(&t
));
455 void asp_wanup(int argc
, char **argv
)
457 web_puts(check_wanup() ? "1" : "0");
460 void asp_wanstatus(int argc
, char **argv
)
464 if ((using_dhcpc()) && (f_exists("/var/lib/misc/dhcpc.renewing"))) {
467 else if (check_wanup()) {
470 else if (f_exists("/var/lib/misc/wan.connecting")) {
479 void asp_link_uptime(int argc
, char **argv
)
489 if (f_read("/var/lib/misc/wantime", &uptime
, sizeof(uptime
)) == sizeof(uptime
)) {
490 reltime(buf
, si
.uptime
- uptime
);
496 void asp_rrule(int argc
, char **argv
)
501 i
= nvram_get_int("rruleN");
502 sprintf(s
, "rrule%d", i
);
503 web_puts("\nrrule = '");
504 web_putj(nvram_safe_get(s
));
505 web_printf("';\nrruleN = %d;\n", i
);
508 void asp_compmac(int argc
, char **argv
)
513 if (get_client_info(mac
, ifname
)) {
518 void asp_ident(int argc
, char **argv
)
520 web_puth(nvram_safe_get("router_name"));
523 void asp_statfs(int argc
, char **argv
)
528 if (argc
!= 2) return;
530 // used for /cifs/, /jffs/... if it returns squashfs type, assume it's not mounted
531 if ((statfs(argv
[0], &sf
) != 0) || (sf
.f_type
== 0x73717368)) {
533 memset(&sf
, 0, sizeof(sf
));
535 // for jffs, try to get total size from mtd partition
536 if (strncmp(argv
[1], "jffs", 4) == 0) {
539 if (mtd_getinfo(argv
[1], &part
, (int *)&sf
.f_blocks
)) {
556 ((uint64_t)sf
.f_bsize
* sf
.f_blocks
),
557 ((uint64_t)sf
.f_bsize
* sf
.f_bfree
));
560 void asp_notice(int argc
, char **argv
)
565 if (argc
!= 1) return;
566 snprintf(s
, sizeof(s
), "/var/notice/%s", argv
[0]);
567 if (f_read_string(s
, buf
, sizeof(buf
)) <= 0) return;
571 void wo_wakeup(char *url
)
577 if ((mac
= webcgi_get("mac")) != NULL
) {
578 end
= mac
+ strlen(mac
);
580 while ((*mac
== ' ') || (*mac
== '\t') || (*mac
== '\r') || (*mac
== '\n')) ++mac
;
581 if (*mac
== 0) break;
584 while ((*p
!= 0) && (*p
!= ' ') && (*p
!= '\r') && (*p
!= '\n')) ++p
;
587 eval("ether-wake", "-b", "-i", nvram_safe_get("lan_ifname"), mac
);
594 void asp_dns(int argc
, char **argv
)
598 const dns_list_t
*dns
;
600 dns
= get_dns(); // static buffer
601 strcpy(s
, "\ndns = [");
602 for (i
= 0 ; i
< dns
->count
; ++i
) {
603 sprintf(s
+ strlen(s
), "%s'%s:%u'", i
? "," : "", inet_ntoa(dns
->dns
[i
].addr
), dns
->dns
[i
].port
);
609 void wo_resolve(char *url
)
614 char host
[NI_MAXHOST
];
615 struct addrinfo hints
;
616 struct addrinfo
*res
;
618 memset(&hints
, 0, sizeof hints
);
619 hints
.ai_family
= AF_UNSPEC
;
620 hints
.ai_socktype
= SOCK_STREAM
;
626 web_puts("\nresolve_data = [\n");
627 if ((p
= webcgi_get("ip")) != NULL
) {
628 while ((ip
= strsep(&p
, ",")) != NULL
) {
629 if (getaddrinfo(ip
, NULL
, &hints
, &res
) != 0) {
633 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
, host
, sizeof(host
), NULL
, 0, 0) != 0) continue;
634 js
= js_string(host
);
635 web_printf("%c['%s','%s']", comma
, ip
, js
);