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>
33 // to javascript-safe string
34 char *js_string(const char *s
)
40 if ((buffer
= malloc((strlen(s
) * 4) + 1)) != NULL
) {
42 while ((c
= *s
++) != 0) {
43 if ((c
== '"') || (c
== '\'') || (c
== '\\') || (!isprint(c
))) {
44 b
+= sprintf(b
, "\\x%02x", c
);
55 // to html-safe string
56 char *html_string(const char *s
)
62 if ((buffer
= malloc((strlen(s
) * 6) + 1)) != NULL
) {
64 while ((c
= *s
++) != 0) {
65 if ((c
== '&') || (c
== '<') || (c
== '>') || (c
== '"') || (c
== '\'') || (!isprint(c
))) {
66 b
+= sprintf(b
, "&#%d;", c
);
78 char *unix_string(const char *s
)
84 if ((buffer
= malloc(strlen(s
) + 1)) != NULL
) {
86 while ((c
= *s
++) != 0)
87 if (c
!= '\r') *b
++ = c
;
94 char *reltime(char *buf
, time_t t
)
102 sprintf(buf
, "%d day%s, %02d:%02d:%02d", days
, ((days
==1) ? "" : "s"), ((m
/ 60) % 24), (m
% 60), (int)(t
% 60));
106 int get_client_info(char *mac
, char *ifname
)
111 char ip
[INET6_ADDRSTRLEN
];
113 char ip
[INET_ADDRSTRLEN
];
117 # ip neigh show fe80:0:0::201:02ff:fe03:0405
118 fe80::201:2ff:fe3:405 dev br0 lladdr 00:01:02:03:04:05 REACHABLE
120 if (clientsai
.ss_family
== AF_INET
) {
121 inet_ntop(clientsai
.ss_family
, &(((struct sockaddr_in
*)&clientsai
)->sin_addr
), ip
, sizeof(ip
));
122 sprintf(s
, "ip neigh show %s", ip
);
125 else if (clientsai
.ss_family
== AF_INET6
) {
126 inet_ntop(clientsai
.ss_family
, &(((struct sockaddr_in6
*)&clientsai
)->sin6_addr
), ip
, sizeof(ip
));
127 if (IN6_IS_ADDR_V4MAPPED( &(((struct sockaddr_in6
*)&clientsai
)->sin6_addr
) ))
128 sprintf(s
, "ip neigh show %s", ip
+ 7); // chop off the ::ffff: to get the ipv4 bit
130 sprintf(s
, "ip neigh show %s", ip
);
134 if ((f
= popen(s
, "r")) != NULL
) {
135 while (fgets(s
, sizeof(s
), f
)) {
136 if (sscanf(s
, "%*s dev %16s lladdr %17s %*s", ifname
, mac
) == 2) {
147 // <% lanip(mode); %>
149 // 1 return first 3 octets (192.168.1)
150 // 2 return last octet (1)
151 // else return full (192.168.1.1)
153 void asp_lanip(int argc
, char **argv
)
159 mode
= argc
? *argv
[0] : 0;
161 if ((nv
= nvram_get("lan_ipaddr")) != NULL
) {
163 if ((p
= strrchr(s
, '.')) != NULL
) {
165 web_puts((mode
== '1') ? s
: (mode
== '2') ? (p
+ 1) : nv
);
170 void asp_lipp(int argc
, char **argv
)
176 // <% psup(process); %>
177 // returns 1 if process is running
179 void asp_psup(int argc
, char **argv
)
181 if (argc
== 1) web_printf("%d", pidof(argv
[0]) > 0);
184 void wo_vpn_status(char *url
)
186 #ifdef TCONFIG_OPENVPN
194 if ( (str
= webcgi_get("server")) )
196 else if ( (str
= webcgi_get("client")) )
199 num
= str
? atoi(str
): 0;
200 if ( type
&& num
> 0 )
202 // Trigger OpenVPN to update the status file
203 snprintf(&buf
[0], sizeof(buf
), "vpn%s%d", type
, num
);
204 killall(&buf
[0], SIGUSR2
);
206 // Give it a chance to update the file
209 // Read the status file and repeat it verbatim to the caller
210 snprintf(&buf
[0], sizeof(buf
), "/etc/openvpn/%s%d/status", type
, num
);
211 fp
= fopen(&buf
[0], "r");
214 while (fgets(&buf
[0], sizeof(buf
), fp
) != NULL
)
224 total: used: free: shared: buffers: cached:
225 Mem: 14872576 12877824 1994752 0 1236992 4837376
247 unsigned long shared
;
248 unsigned long buffers
;
249 unsigned long cached
;
250 unsigned long swaptotal
;
251 unsigned long swapfree
;
252 unsigned long maxfreeram
;
255 static int get_memory(meminfo_t
*m
)
261 memset(m
, 0, sizeof(*m
));
262 if ((f
= fopen("/proc/meminfo", "r")) != NULL
) {
263 while (fgets(s
, sizeof(s
), f
)) {
265 if (strncmp(s
, "MemTotal:", 9) == 0) {
266 m
->total
= strtoul(s
+ 12, NULL
, 10) * 1024;
269 else if (strncmp(s
, "MemFree:", 8) == 0) {
270 m
->free
= strtoul(s
+ 12, NULL
, 10) * 1024;
273 else if (strncmp(s
, "Buffers:", 8) == 0) {
274 m
->buffers
= strtoul(s
+ 12, NULL
, 10) * 1024;
277 else if (strncmp(s
, "Cached:", 7) == 0) {
278 m
->cached
= strtoul(s
+ 12, NULL
, 10) * 1024;
282 if (strncmp(s
, "Mem:", 4) == 0) {
283 if (sscanf(s
+ 6, "%ld %*d %ld %ld %ld %ld", &m
->total
, &m
->free
, &m
->shared
, &m
->buffers
, &m
->cached
) == 5)
287 else if (strncmp(s
, "SwapTotal:", 10) == 0) {
288 m
->swaptotal
= strtoul(s
+ 12, NULL
, 10) * 1024;
291 else if (strncmp(s
, "SwapFree:", 9) == 0) {
292 m
->swapfree
= strtoul(s
+ 11, NULL
, 10) * 1024;
304 m
->maxfreeram
= m
->free
;
305 if (nvram_match("t_cafree", "1")) m
->maxfreeram
+= (m
->cached
+ m
->buffers
);
310 #define IP6ADDR_MAX_CNT 3 // wan, lan, lan-ll
311 static void print_ipv6_addrs(void)
313 char buf
[INET6_ADDRSTRLEN
];
316 struct ifaddrs
*ifap
, *ifa
;
317 struct sockaddr_in6
*s6
;
319 if (!ipv6_enabled() || (getifaddrs(&ifap
) != 0))
323 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
324 if ((ifa
->ifa_addr
== NULL
) || (ifa
->ifa_addr
->sa_family
!= AF_INET6
))
327 s6
= (struct sockaddr_in6
*)(ifa
->ifa_addr
);
329 if (strncmp(ifa
->ifa_name
, nvram_safe_get("lan_ifname"), IFNAMSIZ
) == 0) {
330 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
))
335 else if (strncmp(ifa
->ifa_name
, get_wan6face(), IFNAMSIZ
) == 0) {
336 if (!IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
))
344 if (inet_ntop(ifa
->ifa_addr
->sa_family
, &(s6
->sin6_addr
), buf
, sizeof(buf
)) != NULL
) {
345 web_printf("\tip6_%s: '%s',\n",
347 if (++found
>= IP6ADDR_MAX_CNT
)
361 dev: size erasesize name
362 mtd0: 00020000 00010000 "pmon"
363 mtd1: 007d0000 00010000 "linux"
371 if ((f
= fopen("/proc/mtd", "r")) != NULL
) {
372 while (fgets(s
, sizeof(s
), f
)) {
373 if (sscanf(s
, "%*s %X %*s %16s", &size
, partname
) != 2) continue;
374 if (strcmp(partname
, "\"linux\"") == 0) {
382 if ((size
> 0x2000000) && (size
< 0x4000000)) return 64;
383 else if ((size
> 0x1000000) && (size
< 0x2000000)) return 32;
384 else if ((size
> 0x800000) && (size
< 0x1000000)) return 16;
385 else if ((size
> 0x400000) && (size
< 0x800000)) return 8;
386 else if ((size
> 0x200000) && (size
< 0x400000)) return 4;
387 else if ((size
> 0x100000) && (size
< 0x200000)) return 2;
396 void asp_sysinfo(int argc
, char **argv
)
401 char system_type
[64];
406 get_cpuinfo(system_type
, cpu_model
, bogomips
, cpuclk
);
408 web_puts("\nsysinfo = {\n");
417 "\tuptime_s: '%s',\n"
418 "\tloads: [%ld, %ld, %ld],\n"
422 "\tbufferram: %ld,\n"
424 "\ttotalswap: %ld,\n"
426 "\ttotalfreeram: %ld,\n"
429 "\tsystemtype: '%s',\n"
430 "\tcpumodel: '%s',\n"
431 "\tbogomips: '%s',\n"
434 reltime(s
, si
.uptime
),
435 si
.loads
[0], si
.loads
[1], si
.loads
[2],
437 mem
.shared
, mem
.buffers
, mem
.cached
,
438 mem
.swaptotal
, mem
.swapfree
,
449 void asp_activeroutes(int argc
, char **argv
)
455 unsigned long gateway
;
465 web_puts("\nactiveroutes = [");
467 if ((f
= fopen("/proc/net/route", "r")) != NULL
) {
468 while (fgets(s
, sizeof(s
), f
)) {
469 if (sscanf(s
, "%16s%lx%lx%lx%*s%*s%u%lx", dev
, &dest
, &gateway
, &flags
, &metric
, &mask
) != 6) continue;
470 if ((flags
& RTF_UP
) == 0) continue;
473 strcpy(s_dest
, inet_ntoa(ia
));
476 strcpy(s_dest
, "default");
480 strcpy(s_gateway
, inet_ntoa(ia
));
483 strcpy(s_gateway
, "*");
486 strcpy(s_mask
, inet_ntoa(ia
));
487 web_printf("%s['%s','%s','%s','%s',%u]", n
? "," : "", dev
, s_dest
, s_gateway
, s_mask
, metric
);
496 struct sockaddr_in6 snaddr6
;
497 char addr6
[40], nhop6
[40];
499 if ((ipv6_enabled()) &&
500 (f
= fopen("/proc/net/ipv6_route", "r")) != NULL
) {
501 while (fgets(s
, sizeof(s
), f
)) {
502 if (sscanf(s
, "%32s%x%*s%*s%32s%x%*s%*s%lx%s\n",
503 addr6x
+14, &pxlen
, addr6x
+40+7, &metric
, &flags
, dev
) != 6) continue;
505 if ((flags
& RTF_UP
) == 0) continue;
511 if (i
== 40) { // nul terminator for 1st address?
512 addr6x
[39] = 0; // Fixup... need 0 instead of ':'.
513 ++p
; // Skip and continue.
522 } while (i
< 40+28+7);
524 inet_pton(AF_INET6
, addr6x
, (struct sockaddr
*) &snaddr6
.sin6_addr
);
525 if (IN6_IS_ADDR_UNSPECIFIED(&snaddr6
.sin6_addr
))
526 strcpy(addr6
, "default");
528 inet_ntop(AF_INET6
, &snaddr6
.sin6_addr
, addr6
, sizeof(addr6
));
530 inet_pton(AF_INET6
, addr6x
+ 40, (struct sockaddr
*) &snaddr6
.sin6_addr
);
531 if (IN6_IS_ADDR_UNSPECIFIED(&snaddr6
.sin6_addr
))
534 inet_ntop(AF_INET6
, &snaddr6
.sin6_addr
, nhop6
, sizeof(nhop6
));
536 web_printf("%s['%s','%s','%s','%d',%u]", n
? "," : "", dev
, addr6
, nhop6
, pxlen
, metric
);
547 void asp_cgi_get(int argc
, char **argv
)
552 for (i
= 0; i
< argc
; ++i
) {
553 v
= webcgi_get(argv
[i
]);
558 void asp_time(int argc
, char **argv
)
565 web_puts("Not Available");
568 strftime(s
, sizeof(s
), "%a, %d %b %Y %H:%M:%S %z", localtime(&t
));
573 void asp_wanup(int argc
, char **argv
)
575 web_puts(check_wanup() ? "1" : "0");
578 void asp_wanstatus(int argc
, char **argv
)
582 if ((using_dhcpc()) && (f_exists("/var/lib/misc/dhcpc.renewing"))) {
585 else if (check_wanup()) {
588 else if (f_exists("/var/lib/misc/wan.connecting")) {
597 void asp_link_uptime(int argc
, char **argv
)
607 if (f_read("/var/lib/misc/wantime", &uptime
, sizeof(uptime
)) == sizeof(uptime
)) {
608 reltime(buf
, si
.uptime
- uptime
);
614 void asp_rrule(int argc
, char **argv
)
619 i
= nvram_get_int("rruleN");
620 sprintf(s
, "rrule%d", i
);
621 web_puts("\nrrule = '");
622 web_putj(nvram_safe_get(s
));
623 web_printf("';\nrruleN = %d;\n", i
);
626 void asp_compmac(int argc
, char **argv
)
631 if (get_client_info(mac
, ifname
)) {
636 void asp_ident(int argc
, char **argv
)
638 web_puth(nvram_safe_get("router_name"));
641 void asp_statfs(int argc
, char **argv
)
646 if (argc
!= 2) return;
648 // used for /cifs/, /jffs/... if it returns squashfs type, assume it's not mounted
649 if ((statfs(argv
[0], &sf
) != 0) || (sf
.f_type
== 0x73717368)) {
651 memset(&sf
, 0, sizeof(sf
));
653 // for jffs, try to get total size from mtd partition
654 if (strncmp(argv
[1], "jffs", 4) == 0) {
657 if (mtd_getinfo(argv
[1], &part
, (int *)&sf
.f_blocks
)) {
674 ((uint64_t)sf
.f_bsize
* sf
.f_blocks
),
675 ((uint64_t)sf
.f_bsize
* sf
.f_bfree
));
678 void asp_notice(int argc
, char **argv
)
683 if (argc
!= 1) return;
684 snprintf(s
, sizeof(s
), "/var/notice/%s", argv
[0]);
685 if (f_read_string(s
, buf
, sizeof(buf
)) <= 0) return;
689 void wo_wakeup(char *url
)
695 if ((mac
= webcgi_get("mac")) != NULL
) {
696 end
= mac
+ strlen(mac
);
698 while ((*mac
== ' ') || (*mac
== '\t') || (*mac
== '\r') || (*mac
== '\n')) ++mac
;
699 if (*mac
== 0) break;
702 while ((*p
!= 0) && (*p
!= ' ') && (*p
!= '\r') && (*p
!= '\n')) ++p
;
705 eval("ether-wake", "-b", "-i", nvram_safe_get("lan_ifname"), mac
);
712 void asp_dns(int argc
, char **argv
)
716 const dns_list_t
*dns
;
718 dns
= get_dns(); // static buffer
719 strcpy(s
, "\ndns = [");
720 for (i
= 0 ; i
< dns
->count
; ++i
) {
721 sprintf(s
+ strlen(s
), "%s'%s:%u'", i
? "," : "", inet_ntoa(dns
->dns
[i
].addr
), dns
->dns
[i
].port
);
727 int resolve_addr(const char *ip
, char *host
)
729 struct addrinfo hints
;
730 struct addrinfo
*res
;
733 memset(&hints
, 0, sizeof(hints
));
734 hints
.ai_family
= AF_UNSPEC
;
735 hints
.ai_socktype
= SOCK_STREAM
;
737 ret
= getaddrinfo(ip
, NULL
, &hints
, &res
);
739 ret
= getnameinfo(res
->ai_addr
, res
->ai_addrlen
, host
, NI_MAXHOST
, NULL
, 0, 0);
745 void wo_resolve(char *url
)
749 char host
[NI_MAXHOST
];
754 web_puts("\nresolve_data = [\n");
755 if ((p
= webcgi_get("ip")) != NULL
) {
756 while ((ip
= strsep(&p
, ",")) != NULL
) {
757 if (resolve_addr(ip
, host
) != 0) continue;
758 js
= js_string(host
);
759 web_printf("%c['%s','%s']", comma
, ip
, js
);