Basic USB Storage and Printing Support with GUI.
[tomato.git] / release / src / router / httpd / misc.c
blobab4e63e233aa6b458d66bd647d71fa61daf8a14c
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2008 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <ctype.h>
11 #include <sys/sysinfo.h>
12 #include <sys/stat.h>
13 #include <sys/ioctl.h>
14 #include <net/if.h>
15 #include <fcntl.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <sys/types.h>
20 #include <dirent.h>
21 #include <time.h>
22 #include <sys/statfs.h>
23 #include <netdb.h>
24 #include <net/route.h>
26 // !!TB
27 //#include <sys/mount.h>
28 #include <mntent.h>
30 #include <wlioctl.h>
31 #include <wlutils.h>
34 int wait_file_exists(const char *name, int max, int invert)
36 while (max-- > 0) {
37 if (f_exists(name) ^ invert) return 1;
38 sleep(1);
40 return 0;
43 // to javascript-safe string
44 char *js_string(const char *s)
46 unsigned char c;
47 char *buffer;
48 char *b;
50 if ((buffer = malloc((strlen(s) * 4) + 1)) != NULL) {
51 b = buffer;
52 while ((c = *s++) != 0) {
53 if ((c == '"') || (c == '\'') || (c == '\\') || (!isprint(c))) {
54 b += sprintf(b, "\\x%02x", c);
56 else {
57 *b++ = c;
60 *b = 0;
62 return buffer;
65 // to html-safe string
66 char *html_string(const char *s)
68 unsigned char c;
69 char *buffer;
70 char *b;
72 if ((buffer = malloc((strlen(s) * 6) + 1)) != NULL) {
73 b = buffer;
74 while ((c = *s++) != 0) {
75 if ((c == '&') || (c == '<') || (c == '>') || (c == '"') || (c == '\'') || (!isprint(c))) {
76 b += sprintf(b, "&#%d;", c);
78 else {
79 *b++ = c;
82 *b = 0;
84 return buffer;
87 // removes \r
88 char *unix_string(const char *s)
90 char *buffer;
91 char *b;
92 char c;
94 if ((buffer = malloc(strlen(s) + 1)) != NULL) {
95 b = buffer;
96 while ((c = *s++) != 0)
97 if (c != '\r') *b++ = c;
98 *b = 0;
100 return buffer;
103 // # days, ##:##:##
104 char *reltime(char *buf, time_t t)
106 int days;
107 int m;
109 if (t < 0) t = 0;
110 days = t / 86400;
111 m = t / 60;
112 sprintf(buf, "%d day%s, %02d:%02d:%02d", days, ((days==1) ? "" : "s"), ((m / 60) % 24), (m % 60), (int)(t % 60));
113 return buf;
116 int get_client_info(char *mac, char *ifname)
118 FILE *f;
119 char s[256];
120 char ips[16];
123 # cat /proc/net/arp
124 IP address HW type Flags HW address Mask Device
125 192.168.0.1 0x1 0x2 00:01:02:03:04:05 * vlan1
126 192.168.1.5 0x1 0x2 00:05:06:07:08:09 * br0
129 if ((f = fopen("/proc/net/arp", "r")) != NULL) {
130 while (fgets(s, sizeof(s), f)) {
131 if (sscanf(s, "%15s %*s %*s %17s %*s %16s", ips, mac, ifname) == 3) {
132 if (inet_addr(ips) == clientsai.sin_addr.s_addr) {
133 fclose(f);
134 return 1;
138 fclose(f);
140 return 0;
147 // <% lanip(mode); %>
148 // <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)
155 char *nv, *p;
156 char s[64];
157 char mode;
159 mode = argc ? *argv[0] : 0;
161 if ((nv = nvram_get("lan_ipaddr")) != NULL) {
162 strcpy(s, nv);
163 if ((p = strrchr(s, '.')) != NULL) {
164 *p = 0;
165 web_puts((mode == '1') ? s : (mode == '2') ? (p + 1) : nv);
170 void asp_lipp(int argc, char **argv)
172 char *one = "1";
173 asp_lanip(1, &one);
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);
185 # cat /proc/meminfo
186 total: used: free: shared: buffers: cached:
187 Mem: 14872576 12877824 1994752 0 1236992 4837376
188 Swap: 0 0 0
189 MemTotal: 14524 kB
190 MemFree: 1948 kB
191 MemShared: 0 kB
192 Buffers: 1208 kB
193 Cached: 4724 kB
194 SwapCached: 0 kB
195 Active: 4364 kB
196 Inactive: 2952 kB
197 HighTotal: 0 kB
198 HighFree: 0 kB
199 LowTotal: 14524 kB
200 LowFree: 1948 kB
201 SwapTotal: 0 kB
202 SwapFree: 0 kB
206 typedef struct {
207 unsigned long total;
208 unsigned long free;
209 unsigned long shared;
210 unsigned long buffers;
211 unsigned long cached;
212 unsigned long swaptotal;
213 unsigned long swapfree;
214 unsigned long maxfreeram;
215 } meminfo_t;
217 static int get_memory(meminfo_t *m)
219 FILE *f;
220 char s[128];
221 int ok = 0;
223 if ((f = fopen("/proc/meminfo", "r")) != NULL) {
224 while (fgets(s, sizeof(s), f)) {
225 if (strncmp(s, "Mem:", 4) == 0) {
226 if (sscanf(s + 6, "%ld %*d %ld %ld %ld %ld", &m->total, &m->free, &m->shared, &m->buffers, &m->cached) == 5)
227 ++ok;
229 else if (strncmp(s, "SwapTotal:", 10) == 0) {
230 m->swaptotal = strtoul(s + 12, NULL, 10) * 1024;
231 ++ok;
233 else if (strncmp(s, "SwapFree:", 9) == 0) {
234 m->swapfree = strtoul(s + 11, NULL, 10) * 1024;
235 ++ok;
236 break;
239 fclose(f);
241 if (ok != 3) {
242 memset(m, 0, sizeof(*m));
243 return 0;
245 m->maxfreeram = m->free;
246 if (nvram_match("t_cafree", "1")) m->maxfreeram += m->cached;
247 return 1;
250 void asp_sysinfo(int argc, char **argv)
252 struct sysinfo si;
253 char s[64];
254 meminfo_t mem;
256 web_puts("\nsysinfo = {\n");
257 sysinfo(&si);
258 get_memory(&mem);
259 web_printf(
260 "\tuptime: %ld,\n"
261 "\tuptime_s: '%s',\n"
262 "\tloads: [%ld, %ld, %ld],\n"
263 "\ttotalram: %ld,\n"
264 "\tfreeram: %ld,\n"
265 "\tshareram: %ld,\n"
266 "\tbufferram: %ld,\n"
267 "\tcached: %ld,\n"
268 "\ttotalswap: %ld,\n"
269 "\tfreeswap: %ld,\n"
270 "\ttotalfreeram: %ld,\n"
271 "\tprocs: %d\n",
272 si.uptime,
273 reltime(s, si.uptime),
274 si.loads[0], si.loads[1], si.loads[2],
275 mem.total, mem.free,
276 mem.shared, mem.buffers, mem.cached,
277 mem.swaptotal, mem.swapfree,
278 mem.maxfreeram,
279 si.procs);
280 web_puts("};\n");
283 void asp_activeroutes(int argc, char **argv)
285 FILE *f;
286 char s[512];
287 char dev[17];
288 unsigned long dest;
289 unsigned long gateway;
290 unsigned long flags;
291 unsigned long mask;
292 unsigned metric;
293 struct in_addr ia;
294 char s_dest[16];
295 char s_gateway[16];
296 char s_mask[16];
297 int n;
299 web_puts("\nactiveroutes = [");
300 n = 0;
301 if ((f = fopen("/proc/net/route", "r")) != NULL) {
302 while (fgets(s, sizeof(s), f)) {
303 if (sscanf(s, "%16s%lx%lx%lx%*s%*s%u%lx", dev, &dest, &gateway, &flags, &metric, &mask) != 6) continue;
304 if ((flags & RTF_UP) == 0) continue;
305 if (dest != 0) {
306 ia.s_addr = dest;
307 strcpy(s_dest, inet_ntoa(ia));
309 else {
310 strcpy(s_dest, "default");
312 if (gateway != 0) {
313 ia.s_addr = gateway;
314 strcpy(s_gateway, inet_ntoa(ia));
316 else {
317 strcpy(s_gateway, "*");
319 ia.s_addr = mask;
320 strcpy(s_mask, inet_ntoa(ia));
321 web_printf("%s['%s','%s','%s','%s',%u]", n ? "," : "", dev, s_dest, s_gateway, s_mask, metric);
322 ++n;
324 fclose(f);
326 web_puts("];\n");
329 void asp_cgi_get(int argc, char **argv)
331 const char *v;
332 int i;
334 for (i = 0; i < argc; ++i) {
335 v = webcgi_get(argv[i]);
336 if (v) web_puts(v);
340 void asp_time(int argc, char **argv)
342 time_t t;
343 char s[64];
345 t = time(NULL);
346 if (t < Y2K) {
347 web_puts("Not Available");
349 else {
350 strftime(s, sizeof(s), "%a, %d %b %Y %H:%M:%S %z", localtime(&t));
351 web_puts(s);
355 void asp_wanup(int argc, char **argv)
357 web_puts(check_wanup() ? "1" : "0");
360 void asp_wanstatus(int argc, char **argv)
362 const char *p;
364 if ((using_dhcpc()) && (f_exists("/var/lib/misc/dhcpc.renewing"))) {
365 p = "Renewing...";
367 else if (check_wanup()) {
368 p = "Connected";
370 else if (f_exists("/var/lib/misc/wan.connecting")) {
371 p = "Connecting...";
373 else {
374 p = "Disconnected";
376 web_puts(p);
379 void asp_link_uptime(int argc, char **argv)
381 struct sysinfo si;
382 char buf[64];
383 long uptime;
385 buf[0] = '-';
386 buf[1] = 0;
387 if (check_wanup()) {
388 sysinfo(&si);
389 if (f_read("/var/lib/misc/wantime", &uptime, sizeof(uptime)) == sizeof(uptime)) {
390 reltime(buf, si.uptime - uptime);
393 web_puts(buf);
396 void asp_bandwidth(int argc, char **argv)
398 char *name;
399 int sig;
401 if ((nvram_match("rstats_enable", "1")) && (argc == 1)) {
402 if (strcmp(argv[0], "speed") == 0) {
403 sig = SIGUSR1;
404 name = "/var/spool/rstats-speed.js";
406 else {
407 sig = SIGUSR2;
408 name = "/var/spool/rstats-history.js";
410 unlink(name);
411 killall("rstats", sig);
412 wait_file_exists(name, 5, 0);
413 do_file(name);
414 unlink(name);
418 void asp_rrule(int argc, char **argv)
420 char s[32];
421 int i;
423 i = nvram_get_int("rruleN");
424 sprintf(s, "rrule%d", i);
425 web_puts("\nrrule = '");
426 web_putj(nvram_safe_get(s));
427 web_printf("';\nrruleN = %d;\n", i);
430 void asp_compmac(int argc, char **argv)
432 char mac[32];
433 char ifname[32];
435 if (get_client_info(mac, ifname)) {
436 web_puts(mac);
440 void asp_ident(int argc, char **argv)
442 web_puth(nvram_safe_get("router_name"));
445 void asp_statfs(int argc, char **argv)
447 struct statfs sf;
449 if (argc != 2) return;
451 // used for /cifs/, /jffs/... if it returns squashfs type, assume it's not mounted
452 if ((statfs(argv[0], &sf) != 0) || (sf.f_type == 0x73717368))
453 memset(&sf, 0, sizeof(sf));
455 web_printf(
456 "\n%s = {\n"
457 "\tsize: %llu,\n"
458 "\tfree: %llu\n"
459 "};\n",
460 argv[1],
461 ((uint64_t)sf.f_bsize * sf.f_blocks),
462 ((uint64_t)sf.f_bsize * sf.f_bfree));
465 void asp_notice(int argc, char **argv)
467 char s[256];
468 char buf[2048];
470 if (argc != 1) return;
471 snprintf(s, sizeof(s), "/var/notice/%s", argv[0]);
472 if (f_read_string(s, buf, sizeof(buf)) <= 0) return;
473 web_putj(buf);
476 void wo_wakeup(char *url)
478 char *mac;
479 char *p;
480 char *end;
482 if ((mac = webcgi_get("mac")) != NULL) {
483 end = mac + strlen(mac);
484 while (mac < end) {
485 while ((*mac == ' ') || (*mac == '\t') || (*mac == '\r') || (*mac == '\n')) ++mac;
486 if (*mac == 0) break;
488 p = mac;
489 while ((*p != 0) && (*p != ' ') && (*p != '\r') && (*p != '\n')) ++p;
490 *p = 0;
492 eval("ether-wake", "-i", nvram_safe_get("lan_ifname"), mac);
493 mac = p + 1;
496 common_redirect();
499 void asp_dns(int argc, char **argv)
501 char s[128];
502 int i;
503 const dns_list_t *dns;
505 dns = get_dns(); // static buffer
506 strcpy(s, "\ndns = [");
507 for (i = 0 ; i < dns->count; ++i) {
508 sprintf(s + strlen(s), "%s'%s'", i ? "," : "", inet_ntoa(dns->dns[i]));
510 strcat(s, "];\n");
511 web_puts(s);
514 void wo_resolve(char *url)
516 char *p;
517 char *ip;
518 struct hostent *he;
519 struct in_addr ia;
520 char comma;
521 char *js;
523 comma = ' ';
524 web_puts("\nresolve_data = [\n");
525 if ((p = webcgi_get("ip")) != NULL) {
526 while ((ip = strsep(&p, ",")) != NULL) {
527 ia.s_addr = inet_addr(ip);
528 he = gethostbyaddr(&ia, sizeof(ia), AF_INET);
529 js = js_string(he ? he->h_name : "");
530 web_printf("%c['%s','%s']", comma, inet_ntoa(ia), js);
531 free(js);
532 comma = ',';
535 web_puts("];\n");
539 //!!TB - USB support
541 #define PROC_SCSI_ROOT "/proc/scsi"
542 #define USB_STORAGE "usb-storage"
544 int is_host_mounted(uint host_no)
546 DIR *usb_dev_part;
547 char usb_part[128], usb_disc[128];
548 struct dirent *dp_disc;
550 // check if at least one partition of the host is mounted
551 sprintf(usb_disc, "/dev/discs/disc%d", host_no);
553 int is_mounted = 0;
554 if ((usb_dev_part = opendir(usb_disc))) {
555 while (usb_dev_part && (dp_disc = readdir(usb_dev_part))) {
556 if (!strcmp(dp_disc->d_name, "..") || !strcmp(dp_disc->d_name, "."))
557 continue;
559 sprintf(usb_part, "%s/%s", usb_disc, dp_disc->d_name);
561 if (findmntent(usb_part)) {
562 is_mounted = 1;
563 break;
566 closedir(usb_dev_part);
569 return is_mounted;
572 void asp_usbdevices(int argc, char **argv)
574 DIR *usb_dev_disc;
575 struct dirent *dp;
576 uint host_no;
577 int i, attached;
578 FILE *fp;
579 char line[128];
580 char *tmp=NULL, g_usb_vendor[25], g_usb_product[20], g_usb_serial[20];
582 web_puts("\nusbdev = [");
584 if (!nvram_match("usb_enable", "1")) {
585 web_puts("];\n");
586 return;
589 i = 0;
590 usb_dev_disc = NULL;
592 #if 1
593 /* find all attached USB storage devices */
594 if ((usb_dev_disc = opendir("/dev/discs"))) {
595 while (usb_dev_disc && (dp = readdir(usb_dev_disc))) {
596 if (!strcmp(dp->d_name, "..") || !strcmp(dp->d_name, "."))
597 continue;
599 /* Host no. assigned by scsi driver for this UFD */
600 host_no = atoi(dp->d_name + 4);
601 sprintf(line, "%s/%s-%d/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no, host_no);
603 fp = fopen(line, "r");
604 if (!fp) {
605 sprintf(line, "%s/%s/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no);
606 fp = fopen(line, "r");
609 if (fp) {
610 attached = 0;
611 g_usb_vendor[0] = 0;
612 g_usb_product[0] = 0;
613 g_usb_serial[0] = 0;
614 tmp = NULL;
616 while (fgets(line, sizeof(line), fp) != NULL) {
617 if (strstr(line, "Attached: Yes")) {
618 attached = 1;
620 else if (strstr(line, "Vendor")) {
621 tmp = strtok(line, " ");
622 tmp = strtok(NULL, "\n");
623 strcpy(g_usb_vendor, tmp);
624 tmp = NULL;
626 else if (strstr(line, "Product")) {
627 tmp = strtok(line, " ");
628 tmp = strtok(NULL, "\n");
629 strcpy(g_usb_product, tmp);
630 tmp = NULL;
632 else if (strstr(line, "Serial Number")) {
633 tmp = strtok(line, " ");
634 tmp = strtok(NULL, " ");
635 tmp = strtok(NULL, "\n");
636 strcpy(g_usb_serial, tmp);
637 tmp = NULL;
640 fclose(fp);
642 if (attached) {
643 web_printf("%s['Storage','%d','%s','%s','%s', %d]", i ? "," : "",
644 host_no, g_usb_vendor, g_usb_product, g_usb_serial, is_host_mounted(host_no));
645 ++i;
649 closedir(usb_dev_disc);
652 #else
653 // this version of the code is for broken scsiglue implementation in some kernels
654 // do not use unless there're problems
656 DIR *scsi_dir=NULL, *usb_dir=NULL;
657 struct dirent *scsi_dirent;
659 /* find all attached USB storage devices */
660 scsi_dir = opendir(PROC_SCSI_ROOT);
661 while (scsi_dir && (scsi_dirent = readdir(scsi_dir)))
663 if (!strncmp(USB_STORAGE, scsi_dirent->d_name, strlen(USB_STORAGE)))
665 sprintf(line, "%s/%s", PROC_SCSI_ROOT, scsi_dirent->d_name);
666 usb_dir = opendir(line);
667 while (usb_dir && (dp = readdir(usb_dir)))
669 if (!strcmp(dp->d_name, "..") || !strcmp(dp->d_name, "."))
670 continue;
671 sprintf(line, "%s/%s/%s", PROC_SCSI_ROOT, scsi_dirent->d_name, dp->d_name);
673 fp = fopen(line, "r");
674 if (fp) {
675 attached = 0;
676 g_usb_vendor[0] = 0;
677 g_usb_product[0] = 0;
678 g_usb_serial[0] = 0;
679 tmp = NULL;
681 while (fgets(line, sizeof(line), fp) != NULL) {
682 if (strstr(line, "Attached: Yes")) {
683 attached = 1;
685 else if (strstr(line, "Vendor")) {
686 tmp = strtok(line, " ");
687 tmp = strtok(NULL, "\n");
688 strcpy(g_usb_vendor, tmp);
689 tmp = NULL;
691 else if (strstr(line, "Product")) {
692 tmp = strtok(line, " ");
693 tmp = strtok(NULL, "\n");
694 strcpy(g_usb_product, tmp);
695 tmp = NULL;
697 else if (strstr(line, "Serial Number")) {
698 tmp = strtok(line, " ");
699 tmp = strtok(NULL, " ");
700 tmp = strtok(NULL, "\n");
701 strcpy(g_usb_serial, tmp);
702 tmp = NULL;
705 fclose(fp);
706 if (attached) {
707 web_printf("%s['Storage','%d','%s','%s','%s', %d]", i ? "," : "",
708 host_no, g_usb_vendor, g_usb_product, g_usb_serial, is_host_mounted(host_no));
709 ++i;
714 if (usb_dir)
715 closedir(usb_dir);
718 if (scsi_dir)
719 closedir(scsi_dir);
720 #endif
722 /* now look for a printer */
723 if (f_exists("/dev/usb/lp0") && (fp = fopen("/proc/usblp/usblpid", "r"))) {
724 g_usb_vendor[0] = 0;
725 g_usb_product[0] = 0;
726 tmp = NULL;
728 while (fgets(line, sizeof(line), fp) != NULL) {
729 if (strstr(line, "Manufacturer")) {
730 tmp = strtok(line, "=");
731 tmp = strtok(NULL, "\n");
732 strcpy(g_usb_vendor, tmp);
733 tmp = NULL;
735 else if (strstr(line, "Model")) {
736 tmp = strtok(line, "=");
737 tmp = strtok(NULL, "\n");
738 strcpy(g_usb_product, tmp);
739 tmp = NULL;
742 if ((strlen(g_usb_product) > 0) || (strlen(g_usb_vendor) > 0)) {
743 web_printf("%s['Printer','','%s','%s','']", i ? "," : "",
744 g_usb_vendor, g_usb_product);
745 ++i;
748 fclose(fp);
751 web_puts("];\n");
755 void wo_usbcommand(char *url)
757 char *p;
759 web_puts("\nusb = [\n");
760 if ((p = webcgi_get("remove")) != NULL) {
761 nvram_set("usb_web_umount", p);
762 // wait for unmount
763 int i;
764 for (i = 0; i < 10; i++) {
765 sleep(1);
766 if (!nvram_invmatch("usb_web_umount", "")) break;
768 web_printf("%d", is_host_mounted(atoi(p)));
770 web_puts("];\n");