root/src/router/httpd/visuals/ejs.c

Revision 12361, 75.2 kB (checked in by eko, 5 months ago)

broadcom: display real power returned by driver

Line 
1 #define VISUALSOURCE 1
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <ctype.h>
9 #include <signal.h>
10
11 #include <sys/ioctl.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/socket.h>
15 #include <sys/statfs.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <broadcom.h>
19 #include <cymac.h>
20 #include <wlutils.h>
21 #include <bcmparams.h>
22 #include <dirent.h>
23 #include <netdb.h>
24 #include <utils.h>
25 #include <bcmnvram.h>
26 #include <revision.h>
27
28 void (*do_ej_buffer) (char *buffer, webs_t stream) = NULL;
29 int (*httpd_filter_name) (char *old_name, char *new_name, size_t size,
30                           int type) = NULL;
31 char *(*websGetVar) (webs_t wp, char *var, char *d) = NULL;
32 int (*websWrite) (webs_t wp, char *fmt, ...) = NULL;
33 struct wl_client_mac *wl_client_macs = NULL;
34 void (*do_ej) (struct mime_handler * handler, char *path, webs_t stream, char *query) = NULL;   // jimmy,
35                                                                         // https,
36                                                                         // 8/4/2003
37 int (*ejArgs) (int argc, char_t ** argv, char_t * fmt, ...) = NULL;
38 FILE *(*getWebsFile) (char *path) = NULL;
39 int (*wfflush) (webs_t fp) = NULL;
40 int (*wfputc) (char c, webs_t fp) = NULL;
41 int (*wfputs) (char *buf, webs_t fp) = NULL;
42 char *(*live_translate) (char *tran) = NULL;
43 websRomPageIndexType *websRomPageIndex = NULL;
44 char *(*GOZILA_GET) (webs_t wp, char *name) = NULL;
45
46 #ifdef HAVE_HTTPS
47 int do_ssl;
48 #endif
49
50 void initWeb(struct Webenvironment *env)
51 {
52
53         cprintf("set websgetwar %p:%p->%p:%p\n", env, env->PwebsGetVar,
54                 &websGetVar, websGetVar);
55         websGetVar = env->PwebsGetVar;
56         httpd_filter_name = env->Phttpd_filter_name;
57         cprintf("set wl_client_macs\n");
58         wl_client_macs = env->Pwl_client_macs;
59         cprintf("set webswrite\n");
60         websWrite = env->PwebsWrite;
61         cprintf("set do_ej_buffer\n");
62         do_ej_buffer = env->Pdo_ej_buffer;
63         cprintf("set do_ej\n");
64         do_ej = env->Pdo_ej;
65 #ifdef HAVE_HTTPS
66         cprintf("set do_ssl\n");
67         do_ssl = env->Pdo_ssl;
68 #endif
69         cprintf("set ejargs\n");
70         ejArgs = env->PejArgs;
71         cprintf("set getwebsfile\n");
72         getWebsFile = env->PgetWebsFile;
73         cprintf("set wwflush\n");
74         wfflush = env->Pwfflush;
75         cprintf("set wfputs\n");
76         wfputc = env->Pwfputc;
77         cprintf("set wfputs\n");
78         wfputs = env->Pwfputs;
79         cprintf("set websrompageindex\n");
80         websRomPageIndex = env->PwebsRomPageIndex;
81         cprintf("set live_translate\n");
82         live_translate = env->Plive_translate;
83         GOZILA_GET = env->PGOZILA_GET;
84 }
85
86 struct onload onloads[] = {
87         // { "Filters", filter_onload },
88         {"WL_ActiveTable-wl0", wl_active_onload},
89         {"WL_ActiveTable-wl1", wl_active_onload},
90         {"MACClone", macclone_onload},
91         {"FilterSummary", filtersummary_onload},
92         {"Ping", ping_onload},
93         // {"Traceroute", traceroute_onload},
94 };
95
96 void ej_onload(webs_t wp, int argc, char_t ** argv)
97 {
98         char *type, *arg;
99         struct onload *v;
100
101 #ifndef FASTWEB
102         if (argc < 2) {
103                 websError(wp, 400, "Insufficient args\n");
104                 return;
105         }
106 #endif
107         type = argv[0];
108         arg = argv[1];
109
110         for (v = onloads; v < &onloads[STRUCT_LEN(onloads)]; v++) {
111                 if (!strcmp(v->name, type)) {
112                         v->go(wp, arg);
113                         return;
114                 }
115         }
116
117         return;
118 }
119
120 /*
121  * Meta tag command that will no allow page cached by browsers. The will
122  * force the page to be refreshed when visited.
123  */
124 void ej_no_cache(webs_t wp, int argc, char_t ** argv)
125 {
126         websWrite(wp, "<meta http-equiv=\"expires\" content=\"0\">\n");
127         websWrite(wp,
128                   "<meta http-equiv=\"cache-control\" content=\"no-cache\">\n");
129         websWrite(wp, "<meta http-equiv=\"pragma\" content=\"no-cache\">\n");
130
131         return;
132 }
133
134 void prefix_ip_get(char *name, char *buf, int type)
135 {
136         if (type == 1)
137                 sprintf(buf, "%d.%d.%d.",
138                         get_single_ip(nvram_safe_get(name), 0),
139                         get_single_ip(nvram_safe_get(name), 1),
140                         get_single_ip(nvram_safe_get(name), 2));
141         if (type == 2)
142                 sprintf(buf, "%d.%d.", get_single_ip(nvram_safe_get(name), 0),
143                         get_single_ip(nvram_safe_get(name), 1));
144 }
145
146 /*
147  * Example:
148  * lan_ipaddr=192.168.1.1
149  * <% prefix_ip_get("lan_ipaddr",1); %> produces "192.168.1."
150  */
151 void ej_prefix_ip_get(webs_t wp, int argc, char_t ** argv)
152 {
153         char *name;
154         int type;
155
156 #ifndef FASTWEB
157         if (argc < 2) {
158                 websError(wp, 400, "Insufficient args\n");
159                 return;
160         }
161 #endif
162         name = argv[0];
163         type = atoi(argv[1]);
164
165         if (type == 1)
166                 websWrite(wp, "%d.%d.%d.",
167                           get_single_ip(nvram_safe_get(name), 0),
168                           get_single_ip(nvram_safe_get(name), 1),
169                           get_single_ip(nvram_safe_get(name), 2));
170         if (type == 2)
171                 websWrite(wp, "%d.%d.", get_single_ip(nvram_safe_get(name), 0),
172                           get_single_ip(nvram_safe_get(name), 1));
173
174         return;
175 }
176
177 /*
178  * Example:
179  * lan_ipaddr = 192.168.1.1
180  * <% nvram_get("lan_ipaddr"); %> produces "192.168.1.1"
181  */
182 void ej_nvram_get(webs_t wp, int argc, char_t ** argv)
183 {
184
185 #ifndef FASTWEB
186         if (argc < 1) {
187                 websError(wp, 400, "Insufficient args\n");
188                 return;
189         }
190 #endif
191
192 #if COUNTRY == JAPAN
193         websWrite(wp, "%s", nvram_safe_get(argv[0]));
194 #else
195         /*
196          * for (c = nvram_safe_get (name); *c; c++) { if (isprint ((int) *c)) //
197          * && // *c != '"' && *c != '&' && *c != '<' && *c != '>') ret +=
198          * websWrite (wp, "%c", *c); else { if (*c == '"') ret += websWrite (wp,
199          * "&quot;"); else if (*c == '&') ret += websWrite (wp, "&amp;"); else if
200          * (*c == '<') ret += websWrite (wp, "&lt;"); else if (*c == '>') ret +=
201          * websWrite (wp, "&gt;"); else if (*c == 13) continue; else ret +=
202          * websWrite (wp, "&#%d", *c); } }
203          */
204
205         tf_webWriteESCNV(wp, argv[0]);  // test: buffered version of above
206
207         return;
208 #endif
209
210         return;
211 }
212
213 void ej_nvram_real_get(webs_t wp, int argc, char_t ** argv)
214 {
215
216 #ifndef FASTWEB
217         if (argc < 1) {
218                 websError(wp, 400, "Insufficient args\n");
219                 return;
220         }
221 #endif
222         websWrite(wp, "%s", nvram_safe_get(argv[0]));
223
224         return;
225 }
226
227 /*
228  * Example:
229  * lan_ipaddr = 192.168.1.1, gozila_action = 0
230  * <% nvram_selget("lan_ipaddr"); %> produces "192.168.1.1"
231  * lan_ipaddr = 192.168.1.1, gozila_action = 1, websGetVar(wp, "lan_proto", NULL) = 192.168.1.2;
232  * <% nvram_selget("lan_ipaddr"); %> produces "192.168.1.2"
233  */
234 void ej_nvram_selget(webs_t wp, int argc, char_t ** argv)
235 {
236         char *name;
237
238 #ifndef FASTWEB
239         if (argc < 1) {
240                 websError(wp, 400, "Insufficient args\n");
241                 return;
242         }
243 #endif
244         name = argv[0];
245         if (nvram_match("gozila_action", "1")) {
246                 char *buf = websGetVar(wp, name, NULL);
247
248                 if (buf) {
249                         websWrite(wp, "%s", buf);
250                         return;
251                 }
252         }
253         tf_webWriteESCNV(wp, name);     // test: buffered version of above
254
255         return;
256 }
257
258 /*
259  * Example:
260  * wan_mac = 00:11:22:33:44:55
261  * <% nvram_mac_get("wan_mac"); %> produces "00-11-22-33-44-55"
262  */
263 void ej_nvram_mac_get(webs_t wp, int argc, char_t ** argv)
264 {
265         char *c;
266         char *mac;
267         int i;
268
269 #ifndef FASTWEB
270         if (argc < 1) {
271                 websError(wp, 400, "Insufficient args\n");
272                 return;
273         }
274 #endif
275         c = nvram_safe_get(argv[0]);
276
277         if (c) {
278                 mac = strdup(c);
279                 for (i = 0; *(mac + i); i++) {
280                         if (*(mac + i) == ':')
281                                 *(mac + i) = '-';
282                 }
283                 websWrite(wp, "%s", mac);
284                 free(mac);      // leak, thx tofu
285         }
286
287         return;
288
289 }
290
291 /*
292  * Example:
293  * wan_proto = dhcp; gozilla = 0;
294  * <% nvram_gozila_get("wan_proto"); %> produces "dhcp"
295  *
296  * wan_proto = dhcp; gozilla = 1; websGetVar(wp, "wan_proto", NULL) = static;
297  * <% nvram_gozila_get("wan_proto"); %> produces "static"
298  */
299 void ej_nvram_gozila_get(webs_t wp, int argc, char_t ** argv)
300 {
301         char *type;
302
303 #ifndef FASTWEB
304         if (argc < 1) {
305                 websError(wp, 400, "Insufficient args\n");
306                 return;
307         }
308 #endif
309         type = GOZILA_GET(wp, argv[0]);
310         if (type == NULL)
311                 type = nvram_safe_get(argv[0]);
312
313         websWrite(wp, "%s", type);
314 }
315
316 void ej_webs_get(webs_t wp, int argc, char_t ** argv)
317 {
318         char *value;
319
320 #ifndef FASTWEB
321         if (argc < 1) {
322                 websError(wp, 400, "Insufficient args\n");
323                 return;
324         }
325 #endif
326
327         value = websGetVar(wp, argv[0], NULL);
328
329         if (value)
330                 websWrite(wp, "%s", value);
331
332         return;
333 }
334
335 /*
336  * Example:
337  * lan_ipaddr = 192.168.1.1
338  * <% get_single_ip("lan_ipaddr","1"); %> produces "168"
339  */
340 void ej_get_single_ip(webs_t wp, int argc, char_t ** argv)
341 {
342         char *c;
343
344 #ifndef FASTWEB
345         if (argc < 1) {
346                 websError(wp, 400, "Insufficient args\n");
347                 return;
348         }
349 #endif
350
351         c = nvram_safe_get(argv[0]);
352         if (c) {
353                 if (!strcmp(c, PPP_PSEUDO_IP) || !strcmp(c, PPP_PSEUDO_GW))
354                         c = "0.0.0.0";
355                 else if (!strcmp(c, PPP_PSEUDO_NM))
356                         c = "255.255.255.0";
357
358                 websWrite(wp, "%d", get_single_ip(c, atoi(argv[1])));
359         } else
360                 websWrite(wp, "0");
361
362         return;
363 }
364
365 /*
366  * Example: wan_mac = 00:11:22:33:44:55 get_single_mac("wan_mac", 1);
367  * produces "11"
368  */
369 int get_single_mac(char *macaddr, int which)
370 {
371         int mac[6] = { 0, 0, 0, 0, 0, 0 };
372         int ret;
373
374         ret =
375             sscanf(macaddr, "%2X:%2X:%2X:%2X:%2X:%2X", &mac[0], &mac[1],
376                    &mac[2], &mac[3], &mac[4], &mac[5]);
377         return mac[which];
378 }
379
380 /*
381  * Example:
382  * wan_mac = 00:11:22:33:44:55
383  * <% get_single_mac("wan_mac","1"); %> produces "11"
384  */
385 void ej_get_single_mac(webs_t wp, int argc, char_t ** argv)
386 {
387         char *c;
388         int mac;
389
390 #ifndef FASTWEB
391         if (argc < 2) {
392                 websError(wp, 400, "Insufficient args\n");
393                 return;
394         }
395 #endif
396
397         c = nvram_safe_get(argv[0]);
398         if (c) {
399                 mac = get_single_mac(c, atoi(argv[1]));
400                 websWrite(wp, "%02X", mac);
401         } else
402                 websWrite(wp, "00");
403
404         return;
405 }
406
407 /*
408  * Example:
409  * wan_proto = dhcp; gozilla = 0;
410  * <% nvram_selmatch("wan_proto", "dhcp", "selected"); %> produces "selected"
411  *
412  * wan_proto = dhcp; gozilla = 1; websGetVar(wp, "wan_proto", NULL) = static;
413  * <% nvram_selmatch("wan_proto", "static", "selected"); %> produces "selected"
414  */
415
416 int nvram_selmatch(webs_t wp, char *name, char *match)
417 {
418         char *type = GOZILA_GET(wp, name);
419
420         if (!type) {
421                 if (nvram_match(name, match)) {
422                         return 1;
423                 }
424         } else {
425                 if (!strcmp(type, match)) {
426                         return 1;
427                 }
428         }
429         return 0;
430 }
431
432 void ej_nvram_selmatch(webs_t wp, int argc, char_t ** argv)
433 {
434
435 #ifndef FASTWEB
436         if (argv < 3) {
437                 websError(wp, 400, "Insufficient args\n");
438                 return;
439         }
440 #endif
441         if (nvram_selmatch(wp, argv[0], argv[1])) {
442                 websWrite(wp, argv[2]);
443         }
444         return;
445 }
446
447 void ej_nvram_else_selmatch(webs_t wp, int argc, char_t ** argv)
448 {
449         char *type;
450
451 #ifndef FASTWEB
452         if (argc < 4) {
453                 websError(wp, 400, "Insufficient args\n");
454                 return;
455         }
456 #endif
457
458         type = GOZILA_GET(wp, argv[0]);
459
460         if (!type) {
461                 if (nvram_match(argv[0], argv[1])) {
462                         websWrite(wp, argv[2]);
463                 } else
464                         websWrite(wp, argv[3]);
465         } else {
466                 if (!strcmp(type, argv[1])) {
467                         websWrite(wp, argv[2]);
468                 } else
469                         websWrite(wp, argv[3]);
470         }
471
472         return;
473 }
474
475 /*
476  * Example:
477  * wan_proto=dhcp
478  * <% nvram_else_match("wan_proto", "dhcp", "0","1"); %> produces "0"
479  * <% nvram_else_match("wan_proto", "static", "0","1"); %> produces "1"
480  */
481 void ej_nvram_else_match(webs_t wp, int argc, char_t ** argv)
482 {
483
484 #ifndef FASTWEB
485         if (argc < 4) {
486                 websError(wp, 400, "Insufficient args\n");
487                 return;
488         }
489 #endif
490
491         if (nvram_match(argv[0], argv[1]))
492                 websWrite(wp, argv[2]);
493         else
494                 websWrite(wp, argv[3]);
495
496         return;
497 }
498
499 void ej_startswith(webs_t wp, int argc, char_t ** argv)
500 {
501
502 #ifndef FASTWEB
503         if (argc < 3) {
504                 websError(wp, 400, "Insufficient args\n");
505                 return;
506         }
507 #endif
508         if (startswith(nvram_safe_get(argv[0]), argv[1]))
509                 websWrite(wp, argv[2]);
510
511         return;
512 }
513
514 void ej_ifdef(webs_t wp, int argc, char_t ** argv)
515 {
516         char *name, *output;
517
518 #ifndef FASTWEB
519         if (argc < 2) {
520                 websError(wp, 400, "Insufficient args\n");
521                 return;
522         }
523 #endif
524         name = argv[0];
525         output = argv[1];
526 #ifdef HAVE_MICRO
527         if (!strcmp(name, "MICRO")) {
528                 websWrite(wp, output);
529                 return;
530         }
531 #endif
532 #ifdef HAVE_EXTHELP
533         if (!strcmp(name, "EXTHELP")) {
534                 websWrite(wp, output);
535                 return;
536         }
537 #endif
538         if (!strcmp(name, "MINI"))      // to include mini + mini-special
539         {
540                 if (startswith(nvram_safe_get("dist_type"), "mini")) {
541                         websWrite(wp, output);
542                         return;
543                 }
544         }
545         if (!strcmp(name, "VPN"))       // to include vpn + vpn-special
546         {
547                 if (startswith(nvram_safe_get("dist_type"), "vpn")) {
548                         websWrite(wp, output);
549                         return;
550                 }
551         }
552 #ifdef HAVE_MULTICAST
553         if (!strcmp(name, "MULTICAST")) {
554                 websWrite(wp, output);
555                 return;
556         }
557 #endif
558 #ifdef HAVE_WIVIZ
559         if (!strcmp(name, "WIVIZ")) {
560                 websWrite(wp, output);
561                 return;
562         }
563 #endif
564 #ifdef HAVE_RSTATS
565         if (!strcmp(name, "RSTATS")) {
566                 websWrite(wp, output);
567                 return;
568         }
569 #endif
570 #ifdef HAVE_ACK
571         if (!strcmp(name, "ACK")) {
572                 websWrite(wp, output);
573                 return;
574         }
575 #endif
576 #ifdef HAVE_SSHD
577         if (!strcmp(name, "SSHD")) {
578                 websWrite(wp, output);
579                 return;
580         }
581 #endif
582         if (!strcmp(name, "WET")) {
583                 if (getWET())
584                         websWrite(wp, output);
585                 return;
586         }
587         if (!strcmp(name, "STA")) {
588                 if (getSTA())
589                         websWrite(wp, output);
590                 return;
591         }
592
593         return;
594 }
595
596 void ej_ifndef(webs_t wp, int argc, char_t ** argv)
597 {
598         char *name, *output;
599
600 #ifndef FASTWEB
601         if (argc < 2) {
602                 websError(wp, 400, "Insufficient args\n");
603                 return;
604         }
605 #endif
606         name = argv[0];
607         output = argv[1];
608
609 #ifdef HAVE_MICRO
610         if (!strcmp(name, "MICRO"))
611                 return;
612 #endif
613 #ifdef HAVE_MULTICAST
614         if (!strcmp(name, "MULTICAST"))
615                 return;
616 #endif
617 #ifdef HAVE_WIVIZ
618         if (!strcmp(name, "WIVIZ"))
619                 return;
620 #endif
621 #ifdef HAVE_RSTATS
622         if (!strcmp(name, "RSTATS"))
623                 return;
624 #endif
625 #ifdef HAVE_ACK
626         if (!strcmp(name, "ACK"))
627                 return;
628 #endif
629 #ifdef HAVE_SAMBA
630         if (!strcmp(name, "SAMBA"))
631                 return;
632 #endif
633 #ifdef HAVE_JFFS2
634         if (!strcmp(name, "JFFS2"))
635                 return;
636 #endif
637 #ifdef HAVE_GPSI
638         if (!strcmp(name, "GPSI"))
639                 return;
640 #endif
641 #ifdef HAVE_MMC
642         if (!strcmp(name, "MMC"))
643                 return;
644 #endif
645 #ifdef HAVE_SPUTNIK_APD
646         if (!strcmp(name, "SPUTNIK_APD"))
647                 return;
648 #endif
649 #ifdef HAVE_RFLOW
650         if (!strcmp(name, "RFLOW"))
651                 return;
652 #endif
653 #ifdef HAVE_USB
654         if (!strcmp(name, "USB"))
655                 return;
656 #endif
657 #ifdef HAVE_SSHD
658         if (!strcmp(name, "SSHD"))
659                 return;
660 #endif
661 #ifdef HAVE_PPPOESERVER
662         if (!strcmp(name, "PPPOESERVER"))
663                 return;
664 #endif
665 #ifdef HAVE_MILKFISH
666         if (!strcmp(name, "MILKFISH"))
667                 return;
668 #endif
669         // HAVE_AFTERBURNER
670         if (!strncmp(name, "AFTERBURNER", 11)) {
671 #if defined(HAVE_MADWIFI) || defined(HAVE_RT2880)
672                 return;
673 #else
674                 int afterburner = 0;
675                 char cap[WLC_IOCTL_SMLEN];
676                 char caps[WLC_IOCTL_SMLEN];
677                 char *ifname;
678                 name = name + 11;
679                 if (!strncmp(name, "_wl0", 4))
680                         ifname = nvram_safe_get("wl0_ifname");
681                 else            // "_wl1"
682                         ifname = nvram_safe_get("wl1_ifname");
683                 char *next;
684
685                 if (wl_iovar_get(ifname, "cap", (void *)caps, WLC_IOCTL_SMLEN)
686                     == 0) {
687                         foreach(cap, caps, next) {
688                                 if (!strcmp(cap, "afterburner"))
689                                         afterburner = 1;
690                         }
691
692                         if (afterburner)
693                                 return;
694                 }
695 #endif
696         }
697         // end HAVE_AFTERBURNER
698         // HAVE_HASWIFI
699         if (!strcmp(name, "HASWIFI")) {
700                 if (haswifi())
701                         return;
702         }
703         // end HAVE_HASWIFI
704         if (!strcmp(name, "WET")) {
705                 if (getWET())
706                         return;
707         }
708         if (!strcmp(name, "STA")) {
709                 if (getSTA())
710                         return;
711         }
712        
713         websWrite(wp, output);
714
715         return;
716 }
717
718 /*
719  * Example:
720  * wan_proto=dhcp
721  * <% nvram_match("wan_proto", "dhcp", "selected"); %> produces "selected"
722  * <% nvram_match("wan_proto", "static", "selected"); %> does not produce
723  */
724 void ej_nvram_match(webs_t wp, int argc, char_t ** argv)
725 {
726
727 #ifndef FASTWEB
728         if (argc < 3) {
729                 websError(wp, 400, "Insufficient args\n");
730                 return;
731         }
732 #endif
733
734         if (nvram_match(argv[0], argv[1]))
735                 websWrite(wp, argv[2]);
736
737         return;
738 }
739
740 /*
741  * Example:
742  * wan_proto=dhcp
743  * <% nvram_invmatch("wan_proto", "dhcp", "disabled"); %> does not produce
744  * <% nvram_invmatch("wan_proto", "static", "disabled"); %> produces "disabled"
745  */
746 void ej_nvram_invmatch(webs_t wp, int argc, char_t ** argv)
747 {
748         char *name, *invmatch, *output;
749
750 #ifdef FASTWEB
751         ejArgs(argc, argv, "%s %s %s", &name, &invmatch, &output);
752 #else
753         if (ejArgs(argc, argv, "%s %s %s", &name, &invmatch, &output) < 3) {
754                 websError(wp, 400, "Insufficient args\n");
755                 return;
756         }
757 #endif
758
759         if (!nvram_match(name, invmatch))
760                 websWrite(wp, output);
761
762         return;
763 }
764
765 /*
766  * static void ej_scroll (webs_t wp, int argc, char_t ** argv) { char *type;
767  * int y;
768  *
769  * #ifdef FASTWEB ejArgs (argc, argv, "%s %d", &type, &y); #else if (ejArgs
770  * (argc, argv, "%s %d", &type, &y) < 2) { websError (wp, 400, "Insufficient
771  * args\n"); return; } #endif if (gozila_action) websWrite (wp, "%d", y);
772  * else websWrite (wp, "0");
773  *
774  * return; }
775  */
776 /*
777  * Example:
778  * filter_mac=00:12:34:56:78:00 00:87:65:43:21:00
779  * <% nvram_list("filter_mac", 1); %> produces "00:87:65:43:21:00"
780  * <% nvram_list("filter_mac", 100); %> produces ""
781  */
782 void ej_nvram_list(webs_t wp, int argc, char_t ** argv)
783 {
784         int which;
785         char word[256], *next;
786
787 #ifndef FASTWEB
788         if (argc < 2) {
789                 websError(wp, 400, "Insufficient args\n");
790                 return;
791         }
792 #endif
793         which = atoi(argv[1]);
794         char *list = nvram_safe_get(argv[0]);
795
796         foreach(word, list, next) {
797                 if (which-- == 0)
798                         websWrite(wp, word);
799         }
800
801         return;
802 }
803
804 /*
805  * Example: wan_dns = 1.2.3.4 10.20.30.40 15.25.35.45 get_dns_ip("wan_dns",
806  * 1, 2); produces "20"
807  */
808 int get_dns_ip(char *name, int which, int count)
809 {
810         static char word[256];
811         char *next;
812         int ip;
813         char *list = nvram_safe_get(name);
814
815         foreach(word, list, next) {
816                 if (which-- == 0) {
817                         ip = get_single_ip(word, count);
818                         return ip;
819                 }
820         }
821         return 0;
822 }
823
824 /*
825  * Example: wan_dns = 168.95.1.1 210.66.161.125 168.95.192.1 <%
826  * get_dns_ip("wan_dns", "1", "2"); %> produces "161" <%
827  * get_dns_ip("wan_dns", "2", "3"); %> produces "1"
828  */
829 void ej_get_dns_ip(webs_t wp, int argc, char_t ** argv)
830 {
831         int which;
832         char word[256], *next;
833
834 #ifndef FASTWEB
835         if (argc < 3) {
836                 websError(wp, 400, "Insufficient args\n");
837                 return;
838         }
839 #endif
840         which = atoi(argv[1]);
841         char *list = nvram_safe_get(argv[0]);
842
843         foreach(word, list, next) {
844                 if (which-- == 0) {
845                         websWrite(wp, "%d", get_single_ip(word, atoi(argv[2])));
846                         return;
847                 }
848         }
849
850         websWrite(wp, "0");     // not find
851 }
852
853 void ej_get_http_prefix(webs_t wp, int argc, char_t ** argv)
854 {
855         char http[10];
856         char ipaddr[20];
857         char port[10];
858
859         char *http_enable = websGetVar(wp, "http_enable", NULL);
860
861 #ifdef HAVE_HTTPS
862         char *https_enable = websGetVar(wp, "https_enable", NULL);
863
864         if (do_ssl && http_enable == NULL && https_enable == NULL) {
865                 strcpy(http, "https");
866         } else if (do_ssl && http_enable && https_enable) {
867                 if (atoi(https_enable) && atoi(http_enable))
868                         strcpy(http, "https");
869                 else if (atoi(https_enable) && !atoi(http_enable))
870                         strcpy(http, "https");
871                 else            // !atoi(https_enable) && atoi(http_enable)
872                         strcpy(http, "http");
873         } else if (do_ssl && !http_enable && !https_enable) {
874                 strcpy(http, "http");
875         } else if (!do_ssl && http_enable && https_enable) {
876                 if (atoi(https_enable) && atoi(http_enable))
877                         strcpy(http, "http");
878                 else if (atoi(https_enable) && !atoi(http_enable))
879                         strcpy(http, "https");
880                 else            // !atoi(https_enable) && atoi(http_enable)
881                         strcpy(http, "http");
882         } else
883 #endif
884                 strcpy(http, "http");
885
886         if (nvram_match("browser_method", "USE_LAN")) { // Use LAN to browser
887                 if (nvram_match("restore_defaults", "1")
888                     || nvram_match("sv_restore_defaults", "1")) {
889
890                         strcpy(http, "http");
891                 } else
892                         strcpy(ipaddr, nvram_safe_get("lan_ipaddr"));
893                 strcpy(port, "");
894         } else {
895
896                 if (nvram_match("wan_proto", "pptp"))
897                         strcpy(ipaddr, nvram_safe_get("pptp_get_ip"));
898                 else if (nvram_match("wan_proto", "l2tp"))
899                         strcpy(ipaddr, nvram_safe_get("l2tp_get_ip"));
900                 else
901                         strcpy(ipaddr, nvram_safe_get("wan_ipaddr"));
902
903                 sprintf(port, ":%s", nvram_safe_get("http_wanport"));
904         }
905
906         websWrite(wp, "%s://%s%s/", http, ipaddr, port);
907
908         return;
909 }
910
911 void ej_get_mtu(webs_t wp, int argc, char_t ** argv)
912 {
913         struct mtu_lists *mtu_list;
914         char *type;
915         char *proto = GOZILA_GET(wp, "wan_proto");
916
917         type = argv[1];
918 #ifndef FASTWEB
919         if (argc < 1) {
920                 websError(wp, 400, "Insufficient args\n");
921                 return;
922         }
923 #endif
924         if (proto == NULL)
925                 proto = nvram_safe_get("wan_proto");
926         mtu_list = get_mtu(proto);
927
928         if (!strcmp(type, "min"))
929                 websWrite(wp, "%s", mtu_list->min);
930         else if (!strcmp(type, "max"))
931                 websWrite(wp, "%s", mtu_list->max);
932
933         return;
934 }
935
936 void ej_show_forward(webs_t wp, int argc, char_t ** argv)
937 // ej_show_forward(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg,
938 // char_t *url, char_t *path, char_t *query)
939 {
940         int i;
941         char *count;
942         int c = 0;
943
944         count = nvram_safe_get("forward_entries");
945         if (count == NULL || strlen(count) == 0 || (c = atoi(count)) <= 0) {
946                 // return -1; botho 07/03/06 add "- None -" if empty
947                 websWrite(wp, "<tr>\n");
948                 websWrite(wp,
949                           "<td colspan=\"6\" align=\"center\" valign=\"middle\">- <script type=\"text/javascript\">Capture(share.none)</script> -</td>\n");
950                 websWrite(wp, "</tr>\n");
951         }
952         for (i = 0; i < c; i++) {
953                 websWrite(wp, "<tr><td>\n");
954                 websWrite(wp,
955                           "<input maxlength=\"12\" size=\"12\" name=\"name%d\" onblur=\"valid_name(this,'Name')\" value=\"",
956                           i);
957                 port_forward_table(wp, "name", i);
958                 websWrite(wp, "\" /></td>\n");
959                 websWrite(wp, "<td>\n");
960                 websWrite(wp,
961                           "<input class=\"num\" maxlength=\"5\" size=\"5\" name=\"from%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
962                           i);
963                 port_forward_table(wp, "from", i);
964                 websWrite(wp, "\" /></td>\n");
965                 websWrite(wp, "<td>\n");
966                 websWrite(wp,
967                           "<input class=\"num\" maxlength=\"5\" size=\"5\" name=\"to%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
968                           i);
969                 port_forward_table(wp, "to", i);
970                 websWrite(wp, "\"/></td>\n");
971                 websWrite(wp, "<td><select size=\"1\" name=\"pro%d\">\n", i);
972                 websWrite(wp, "<option value=\"tcp\" ");
973                 port_forward_table(wp, "sel_tcp", i);
974                 websWrite(wp, ">TCP</option>\n");
975                 websWrite(wp, "<option value=\"udp\" ");
976                 port_forward_table(wp, "sel_udp", i);
977                 websWrite(wp, ">UDP</option>\n");
978                 websWrite(wp, "<script type=\"text/javascript\">\n//<![CDATA[\n\
979                                 document.write(\"<option value=\\\"both\\\" ");
980                 port_forward_table(wp, "sel_both", i);
981                 websWrite(wp, " >\" + share.both + \"</option>\");\n\
982         \n//]]>\n</script>\n");
983                 websWrite(wp, "</select></td>\n");
984                 websWrite(wp, "<td>\n");
985                 websWrite(wp,
986                           "<input class=\"num\" maxlength=\"15\" size=\"15\" name=\"ip%d\" value=\"",
987                           i);
988                 port_forward_table(wp, "ip", i);
989                 websWrite(wp, "\" /></td>\n");
990                 websWrite(wp, "<td>\n");
991                 websWrite(wp,
992                           "<input type=\"checkbox\" value=\"on\" name=\"enable%d\" ",
993                           i);
994                 port_forward_table(wp, "enable", i);
995                 websWrite(wp, " /></td>\n");
996                 websWrite(wp, "</tr>\n");
997         }
998         return;
999 }
1000
1001 void ej_show_forward_spec(webs_t wp, int argc, char_t ** argv)
1002 // ej_show_forward(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg,
1003 // char_t *url, char_t *path, char_t *query)
1004 {
1005         int i;
1006         char *count;
1007         int c = 0;
1008
1009         count = nvram_safe_get("forwardspec_entries");
1010         if (count == NULL || strlen(count) == 0 || (c = atoi(count)) <= 0) {
1011                 // return -1; botho 07/03/06 add "- None -" if empty
1012                 // websWrite (wp, "<tr></tr><tr></tr>\n");
1013                 websWrite(wp, "<tr>\n");
1014                 websWrite(wp,
1015                           "<td colspan=\"6\" align=\"center\" valign=\"middle\">- <script type=\"text/javascript\">Capture(share.none)</script> -</td>\n");
1016                 websWrite(wp, "</tr>\n");
1017         }
1018         for (i = 0; i < c; i++) {
1019                 websWrite(wp, "<tr><td>\n");
1020                 websWrite(wp,
1021                           "<input maxlength=\"12\" size=\"12\" name=\"name%d\" onblur=\"valid_name(this,'Name')\" value=\"",
1022                           i);
1023                 port_forward_spec(wp, "name", i);
1024                 websWrite(wp, "\" /></td>\n");
1025                 websWrite(wp, "<td>\n");
1026                 websWrite(wp,
1027                           "<input class=\"num\" maxlength=\"5\" size=\"5\" name=\"from%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1028                           i);
1029                 port_forward_spec(wp, "from", i);
1030                 websWrite(wp, "\" /></td>\n");
1031                 websWrite(wp, "<td><select size=\"1\" name=\"pro%d\">\n", i);
1032                 websWrite(wp, "<option value=\"tcp\" ");
1033                 port_forward_spec(wp, "sel_tcp", i);
1034                 websWrite(wp, ">TCP</option>\n");
1035                 websWrite(wp, "<option value=\"udp\" ");
1036                 port_forward_spec(wp, "sel_udp", i);
1037                 websWrite(wp, ">UDP</option>\n");
1038                 websWrite(wp, "<script type=\"text/javascript\">\n//<![CDATA[\n\
1039                                 document.write(\"<option value=\\\"both\\\" ");
1040                 port_forward_spec(wp, "sel_both", i);
1041                 websWrite(wp, " >\" + share.both + \"</option>\");\n\
1042                 \n//]]>\n</script>\n");
1043                 websWrite(wp, "</select></td>\n");
1044                 websWrite(wp, "<td>\n");
1045                 websWrite(wp,
1046                           "<input class=\"num\" maxlength=\"15\" size=\"15\" name=\"ip%d\" value=\"",
1047                           i);
1048                 port_forward_spec(wp, "ip", i);
1049                 websWrite(wp, "\" /></td>\n");
1050                 websWrite(wp, "<td>\n");
1051                 websWrite(wp,
1052                           "<input class=\"num\" maxlength=\"5\" size=\"5\" name=\"to%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1053                           i);
1054                 port_forward_spec(wp, "to", i);
1055                 websWrite(wp, "\" /></td>\n");
1056                 websWrite(wp, "<td>\n");
1057                 websWrite(wp,
1058                           "<input type=\"checkbox\" value=\"on\" name=\"enable%d\" ",
1059                           i);
1060                 port_forward_spec(wp, "enable", i);
1061                 websWrite(wp, " /></td>\n");
1062                 websWrite(wp, "</tr>\n");
1063         }
1064         return;
1065 }
1066
1067 void ej_show_triggering(webs_t wp, int argc, char_t ** argv)
1068 {
1069         int i;
1070         char *count;
1071         int c = 0;
1072
1073         count = nvram_safe_get("trigger_entries");
1074         if (count == NULL || strlen(count) == 0 || (c = atoi(count)) <= 0) {
1075                 websWrite(wp, "<tr>\n");
1076                 websWrite(wp,
1077                           "<td colspan=\"6\" align=\"center\" valign=\"middle\">- <script type=\"text/javascript\">Capture(share.none)</script> -</td>\n");
1078                 websWrite(wp, "</tr>\n");
1079         }
1080         for (i = 0; i < c; i++) {
1081                 websWrite(wp, "<tr>\n");
1082                 websWrite(wp,
1083                           "<td><input maxlength=\"12\" size=\"12\" name=\"name%d\" onblur=\"valid_name(this,'Name')\" value=\"",
1084                           i);
1085                 port_trigger_table(wp, "name", i);
1086                 websWrite(wp, "\" /></td>\n");
1087                 websWrite(wp,
1088                           "<td><input class=\"num\" maxlength=\"5\" size=\"5\" name=\"i_from%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1089                           i);
1090                 port_trigger_table(wp, "i_from", i);
1091                 websWrite(wp, "\" /></td>\n");
1092                 websWrite(wp,
1093                           "<td><input class=\"num\" maxlength=\"5\" size=\"5\" name=\"i_to%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1094                           i);
1095                 port_trigger_table(wp, "i_to", i);
1096                 websWrite(wp, "\" /></td>\n");
1097                 websWrite(wp, "<td><select size=\"1\" name=\"pro%d\">\n", i);
1098                 websWrite(wp, "<option value=\"tcp\" ");
1099                 port_trigger_table(wp, "sel_tcp", i);
1100                 websWrite(wp, ">TCP</option>\n");
1101                 websWrite(wp, "<option value=\"udp\" ");
1102                 port_trigger_table(wp, "sel_udp", i);
1103                 websWrite(wp, ">UDP</option>\n");
1104                 websWrite(wp, "<script type=\"text/javascript\">\n//<![CDATA[\n\
1105                                 document.write(\"<option value=\\\"both\\\" ");
1106                 port_trigger_table(wp, "sel_both", i);
1107                 websWrite(wp, " >\" + share.both + \"</option>\");\n\
1108                 \n//]]>\n</script>\n");
1109                 websWrite(wp, "</select></td>\n");
1110                 websWrite(wp,
1111                           "<td><input class=\"num\" maxlength=\"5\" size=\"5\" name=\"o_from%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1112                           i);
1113                 port_trigger_table(wp, "o_from", i);
1114                 websWrite(wp, "\" /></td>\n");
1115                 websWrite(wp,
1116                           "<td><input class=\"num\" maxlength=\"5\" size=\"5\" name=\"o_to%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1117                           i);
1118                 port_trigger_table(wp, "o_to", i);
1119                 websWrite(wp, "\" /></td>\n");
1120                 websWrite(wp,
1121                           "<td><input type=\"checkbox\" value=\"on\" name=\"enable%d\" ",
1122                           i);
1123                 port_trigger_table(wp, "enable", i);
1124                 websWrite(wp, " /></td>\n");
1125                 websWrite(wp, "</tr>\n");
1126         }
1127         return;
1128 }
1129
1130 // SEG DD-WRT addition
1131 void ej_show_styles(webs_t wp, int argc, char_t ** argv)
1132 {
1133         // <option value="blue" <% nvram_selected("router_style", "blue");
1134         // %>>Blue</option>
1135         DIR *directory;
1136         char buf[256];
1137
1138         directory = opendir("/www/style");
1139         struct dirent *entry;
1140
1141         while ((entry = readdir(directory)) != NULL) {
1142                 sprintf(buf, "style/%s/style.css", entry->d_name);
1143                 FILE *web = getWebsFile(buf);
1144
1145                 if (web == NULL) {
1146                         sprintf(buf, "/www/style/%s/style.css", entry->d_name);
1147                         FILE *test = fopen(buf, "rb");
1148
1149                         if (test == NULL)
1150                                 continue;
1151                         fclose(test);
1152                 }
1153                 fclose(web);
1154
1155                 websWrite(wp, "<option value=\"%s\" %s>%s</option>\n",
1156                           entry->d_name, nvram_match("router_style",
1157                                                      entry->d_name) ?
1158                           "selected=\"selected\"" : "", entry->d_name);
1159         }
1160         closedir(directory);
1161         return;
1162 }
1163
1164 #ifdef HAVE_LANGUAGE
1165 // extern websRomPageIndexType websRomPageIndex[];
1166 void ej_show_languages(webs_t wp, int argc, char_t ** argv)
1167 {
1168         char buf[256];
1169
1170         websWrite(wp, "<script type=\"text/javascript\">\n//<![CDATA[\n");
1171         int i = 0;
1172
1173         while (websRomPageIndex[i].path != NULL) {
1174                 cprintf("checking %s\n", websRomPageIndex[i].path);
1175                 if (!strncmp
1176                     (websRomPageIndex[i].path, "lang_pack/",
1177                      strlen("lang_pack/"))) {
1178                         cprintf("found language\n");
1179                         if (strlen(websRomPageIndex[i].path) < 14)
1180                                 continue;
1181                         strcpy(buf, websRomPageIndex[i].path);
1182                         char *mybuf = &buf[strlen("lang_pack/")];
1183
1184                         mybuf[strlen(mybuf) - 3] = 0;   // strip .js
1185                         websWrite(wp,
1186                                   "document.write(\"<option value=\\\"%s\\\" %s >\" + management.lang_%s + \"</option>\");\n",
1187                                   mybuf, nvram_match("language",
1188                                                      mybuf) ?
1189                                   "selected=\\\"selected\\\"" : "", mybuf);
1190                 }
1191                 i++;
1192         }
1193         websWrite(wp, "//]]>\n</script>\n");
1194         return;
1195 }
1196 #endif
1197
1198 static char *directories[] = {
1199         "/etc/config",
1200         "/jffs/etc/config",
1201         "/mmc/etc/config"
1202 };
1203
1204 void ej_show_modules(webs_t wp, int argc, char_t ** argv)
1205 {
1206         char buf[256];
1207         struct dirent *entry;
1208         DIR *directory;
1209
1210         // display modules
1211         int idx;
1212
1213         for (idx = 0; idx < 3; idx++) {
1214                 directory = opendir(directories[idx]);
1215                 if (directory == NULL)
1216                         continue;
1217                 // list all files in this directory
1218                 while ((entry = readdir(directory)) != NULL) {
1219                         if (argc > 0) {
1220                                 if (endswith(entry->d_name, argv[0])) {
1221 #ifdef HAVE_ERC
1222                                         if (strcmp(entry->d_name, "base.webconfig") && !wp->userid)     //show only base.webconfig for this user and nothing else
1223                                         {
1224                                                 continue;
1225                                         }
1226 #endif
1227                                         sprintf(buf, "%s/%s", directories[idx],
1228                                                 entry->d_name);
1229                                         do_ej(NULL, buf, wp, NULL);
1230                                 }
1231                         } else {
1232                                 if (endswith(entry->d_name, ".webconfig")) {
1233                                         sprintf(buf, "%s/%s", directories[idx],
1234                                                 entry->d_name);
1235                                         do_ej(NULL, buf, wp, NULL);
1236                                 }
1237                         }
1238                 }
1239                 closedir(directory);
1240         }
1241         return;
1242 }
1243
1244 void ej_get_sysmodel(webs_t wp, int argc, char_t ** argv)
1245 {
1246 #ifdef HAVE_XIOCOM
1247         websWrite(wp, "XWR");
1248 #else
1249         websWrite(wp, "%s", nvram_safe_get("DD_BOARD"));
1250 #endif
1251 }
1252
1253 void ej_get_totaltraff(webs_t wp, int argc, char_t ** argv)
1254 {
1255         char *type;
1256         static char wanface[32];
1257         char line[256];
1258         unsigned long rcvd, sent, megcounti, megcounto;
1259         FILE *in;
1260
1261 #ifndef FASTWEB
1262         if (argc < 1) {
1263                 websError(wp, 400, "Insufficient args\n");
1264                 return;
1265         }
1266 #endif
1267         type = argv[0];
1268
1269         if (!nvram_match("ttraff_enable", "1"))
1270                 return;
1271
1272         if (nvram_match("ttraff_iface", "") || !nvram_get("ttraff_iface"))
1273                 strncpy(wanface, get_wan_face(), sizeof(wanface));
1274         else
1275                 strncpy(wanface, nvram_safe_get("ttraff_iface"),
1276                         sizeof(wanface));
1277
1278         in = fopen("/proc/net/dev", "rb");
1279         if (in == NULL)
1280                 return;
1281
1282         while (fgets(line, sizeof(line), in) != NULL) {
1283                 int ifl = 0;
1284
1285                 if (!strchr(line, ':'))
1286                         continue;
1287                 while (line[ifl] != ':')
1288                         ifl++;
1289                 line[ifl] = 0;  /* interface */
1290                 if (strstr(line, wanface)) {
1291                         sscanf(line + ifl + 1,
1292                                "%lu %*ld %*ld %*ld %*ld %*ld %*ld %*ld %lu %*ld %*ld %*ld %*ld %*ld %*ld %*ld",
1293                                &rcvd, &sent);
1294
1295                 }
1296         }
1297
1298         fclose(in);
1299
1300         rcvd >>= 20;            // output in MBytes
1301         sent >>= 20;
1302
1303         if ((in = fopen("/tmp/.megc", "r")) != NULL) {
1304                 fgets(line, sizeof(line), in);
1305                 sscanf(line, "%lu:%lu", &megcounti, &megcounto);
1306                 rcvd += megcounti;
1307                 sent += megcounto;
1308                 fclose(in);
1309         }
1310
1311         if (!strcmp(type, "in")) {
1312                 websWrite(wp, "%lu", rcvd);     // output in MBytes
1313         } else if (!strcmp(type, "out")) {
1314                 websWrite(wp, "%lu", sent);
1315         }
1316         return;
1317 }
1318
1319 void show_bwif(webs_t wp, char *ifname, char *name)
1320 {
1321         websWrite(wp, "<h2>%s - %s</h2>\n", live_translate("status_band.h2"),
1322                   name);
1323         websWrite(wp, "<fieldset>\n");
1324         websWrite(wp,
1325                   "<iframe src=\"/graph_if.svg?%s\" width=\"555\" height=\"275\" frameborder=\"0\" type=\"image/svg+xml\">\n",
1326                   ifname);
1327         websWrite(wp, "</iframe>\n");
1328         websWrite(wp, "</fieldset>\n");
1329         websWrite(wp, "<br />\n");
1330 }
1331
1332 void ej_show_bandwidth(webs_t wp, int argc, char_t ** argv)
1333 {
1334         show_bwif(wp, nvram_safe_get("lan_ifname"), "LAN");
1335         if (!nvram_match("wan_proto", "disabled")) {
1336                 if (strlen(nvram_safe_get("wan_iface")) > 0) {
1337                         show_bwif(wp, nvram_safe_get("wan_iface"), "WAN");
1338                 } else {
1339                         if (strlen(nvram_safe_get("wan_ifname")) > 0) {
1340                                 show_bwif(wp, nvram_safe_get("wan_ifname"),
1341                                           "WAN");
1342                         }
1343                 }
1344                 if (nvram_match("dtag_vlan8", "1"))
1345                         show_bwif(wp, "eth0.0008", "IPTV");
1346
1347         }
1348 #ifdef HAVE_MADWIFI
1349         char var[80];
1350         char *next;
1351         int c = getdevicecount();
1352         int i;
1353
1354         for (i = 0; i < c; i++) {
1355                 char dev[32];
1356
1357                 sprintf(dev, "ath%d", i);
1358                 char name[32];
1359
1360                 sprintf(name, "%s (%s)", live_translate("share.wireless"), dev);
1361                 show_bwif(wp, dev, name);
1362                 char *vifs = nvram_nget("%s_vifs", dev);
1363
1364                 if (vifs == NULL)
1365                         continue;
1366                 foreach(var, vifs, next) {
1367                         sprintf(name, "%s (%s)",
1368                                 live_translate("share.wireless"), var);
1369                         show_bwif(wp, var, name);
1370                 }
1371                 int s;
1372
1373                 for (s = 1; s <= 10; s++) {
1374                         char *wdsdev;
1375
1376                         wdsdev = nvram_nget("%s_wds%d_if", dev, s);
1377                         if (strlen(wdsdev) == 0)
1378                                 continue;
1379                         if (nvram_nmatch("0", "%s_wds%d_enable", dev, s))
1380                                 continue;
1381                         sprintf(name, "%s (%s)",
1382                                 live_translate("share.wireless"), wdsdev);
1383                         show_bwif(wp, wdsdev, name);
1384                 }
1385
1386         }
1387
1388 #else
1389         char name[32];
1390         int cnt = get_wl_instances();
1391         int c;
1392
1393         for (c = 0; c < cnt; c++) {
1394                 sprintf(name, "%s (wl%d)", live_translate("share.wireless"), c);
1395                 show_bwif(wp, get_wl_instance_name(c), name);
1396         }
1397 #endif
1398 #ifdef HAVE_WAVESAT
1399         char name[32];
1400
1401         sprintf(name, "%s", live_translate("wl_wimax.titl"));
1402         show_bwif(wp, "ofdm", name);
1403 #endif
1404 }
1405
1406 void ej_do_menu(webs_t wp, int argc, char_t ** argv)
1407 {
1408         char *mainmenu, *submenu;
1409
1410 #ifndef FASTWEB
1411         if (argc < 2) {
1412                 websError(wp, 400, "Insufficient args\n");
1413                 return;
1414         }
1415 #endif
1416         mainmenu = argv[0];
1417         submenu = argv[1];
1418
1419         int vlan_supp = check_vlan_support();
1420
1421 #ifdef HAVE_SPUTNIK_APD
1422         int sputnik = nvram_match("apd_enable", "1");
1423 #else
1424         int sputnik = 0;
1425 #endif
1426         int openvpn = nvram_match("openvpn_enable",
1427                                   "1") | nvram_match("openvpncl_enable",
1428                                                      "1");
1429         int auth = nvram_match("status_auth", "1");
1430         int registered = 1;
1431 #ifdef HAVE_REGISTER
1432         if (!isregistered_real())
1433                 registered = 0;
1434 #endif
1435 #ifdef HAVE_MADWIFI
1436 #ifdef HAVE_NOWIFI
1437         int wifi = 0;
1438 #else
1439         int wifi = haswifi();
1440 #endif
1441 #endif
1442 #ifdef HAVE_MADWIFI
1443         int wimaxwifi = 0;
1444 #endif
1445 #ifdef HAVE_ERC
1446         static char menu_s[8][11][32] =
1447             { {"index.asp", "", "", "", "", "", "", "", "", "", ""},
1448         {"Wireless_Basic.asp", "WL_WPATable.asp", "", "", "", "", "", "", "",
1449          "", ""},
1450         {"ForwardSpec.asp", "", "", "", "", "", "", "", "", "", ""},
1451         {"Management.asp", "", "", "", "", "", "", "", "", "", ""},
1452         {"", "", "", "", "", "", "", "", "", "", ""},
1453         {"", "", "", "", "", "", "", "", "", "", ""},
1454         {"", "", "", "", "", "", "", "", "", "", ""},
1455         {"", "", "", "", "", "", "", "", "", "", ""}
1456         };
1457         /*
1458          * real name is bmenu.menuname[i][j]
1459          */
1460         static char menuname_s[8][12][32] =
1461             { {"setup", "setupbasic", "", "", "", "", "", "", "", "", "", ""},
1462         {"wireless", "wirelessBasic", "wirelessSecurity", "", "", "", "", "",
1463          "", "", "", ""},
1464         {"applications", "applicationspforwarding", "", "", "", "", "", "", "",
1465          "", "", ""},
1466         {"admin", "adminManagement", "", "", "", "", "", "", "", "", "", ""},
1467         {"", "", "", "", "", "", "", "", "", "", "", ""},
1468         {"", "", "", "", "", "", "", "", "", "", "", ""},
1469         {"", "", "", "", "", "", "", "", "", "", "", ""},
1470         {"", "", "", "", "", "", "", "", "", "", "", ""}
1471         };
1472
1473 #endif
1474
1475         static char menu_t[8][11][32] =
1476             { {"index.asp", "DDNS.asp", "WanMAC.asp", "Routing.asp", "Vlan.asp",
1477                "Networking.asp", "", "", "", "", ""},
1478         {"Wireless_Basic.asp", "SuperChannel.asp", "WiMAX.asp",
1479          "Wireless_radauth.asp", "WL_WPATable.asp", "Wireless_MAC.asp",
1480          "Wireless_Advanced.asp", "Wireless_WDS.asp", "", "", ""},
1481         {"Services.asp", "PPPoE_Server.asp", "PPTP.asp", "USB.asp", "NAS.asp",
1482          "Hotspot.asp", "Milkfish.asp", "eop-tunnel.asp", "AnchorFree.asp", "",
1483          ""},
1484         {"Firewall.asp", "VPN.asp", "", "", "", "", "", "", "", "", ""},
1485         {"Filters.asp", "", "", "", "", "", "", "", "", "", ""},
1486         {"ForwardSpec.asp", "Forward.asp", "Triggering.asp", "UPnP.asp",
1487          "DMZ.asp", "QoS.asp", "P2P.asp", "", "", "", ""},
1488         {"Management.asp", "Alive.asp", "Diagnostics.asp", "Wol.asp",
1489          "Factory_Defaults.asp", "Upgrade.asp", "config.asp", "", "", "", ""},
1490         {"Status_Router.asp", "Status_Internet.asp", "Status_Lan.asp",
1491          "Status_Wireless.asp", "Status_SputnikAPD.asp", "Status_OpenVPN.asp",
1492          "Status_Bandwidth.asp", "Info.htm", "register.asp", "", ""}
1493         };
1494         /*
1495          * real name is bmenu.menuname[i][j]
1496          */
1497         static char menuname_t[8][12][32] =
1498             { {"setup", "setupbasic", "setupddns", "setupmacclone",
1499                "setuprouting", "setupvlan", "networking", "", "", "", "", ""},
1500         {"wireless", "wirelessBasic", "wirelessSuperchannel", "wimax",
1501          "wirelessRadius", "wirelessSecurity", "wirelessMac",
1502          "wirelessAdvanced", "wirelessWds", "", "", ""},
1503         {"services", "servicesServices", "servicesPppoesrv", "servicesPptp",
1504          "servicesUSB", "servicesNAS", "servicesHotspot", "servicesMilkfish",
1505          "setupeop", "servicesAnchorFree", "", ""},
1506         {"security", "firwall", "vpn", "", "", "", "", "", "", "", "", ""},
1507         {"accrestriction", "webaccess", "", "", "", "", "", "", "", "", "", ""},
1508         {"applications", "applicationspforwarding", "applicationsprforwarding",
1509          "applicationsptriggering", "applicationsUpnp", "applicationsDMZ",
1510          "applicationsQoS", "applicationsP2P", "", "", "", ""},
1511         {"admin", "adminManagement", "adminAlive", "adminDiag", "adminWol",
1512          "adminFactory", "adminUpgrade", "adminBackup", "", "", "", ""},
1513         {"statu", "statuRouter", "statuInet", "statuLAN", "statuWLAN",
1514          "statuSputnik", "statuVPN", "statuBand", "statuSysInfo",
1515          "statuActivate", "", ""}
1516         };
1517
1518         static char menu[8][11][32];
1519         static char menuname[8][12][32];
1520         memcpy(menu, menu_t, 8 * 11 * 32);
1521         memcpy(menuname, menuname_t, 8 * 12 * 32);
1522 #ifdef HAVE_ERC
1523         if (!wp->userid) {
1524                 memcpy(menu, menu_s, 8 * 11 * 32);
1525                 memcpy(menuname, menuname_s, 8 * 12 * 32);
1526         }
1527 #endif
1528 //fprintf(stderr,"generate menu content\n");
1529 #ifdef HAVE_MADWIFI
1530         // fill up WDS
1531         int ifcount = getifcount("wifi");
1532         int a;
1533
1534         for (a = 0; a < ifcount; a++) {
1535                 sprintf(&menu[1][a + 7][0], "Wireless_WDS-ath%d.asp", a);
1536                 if (ifcount == 1)
1537                         sprintf(&menuname[1][a + 8][0], "wirelessWds");
1538                 else
1539                         sprintf(&menuname[1][a + 8][0], "wirelessWds%d", a);
1540         }
1541 #else
1542 #ifdef HAVE_ERC
1543         if (wp->userid) {
1544 #endif
1545
1546                 int ifcount = get_wl_instances();
1547                 int a;
1548
1549                 for (a = 0; a < ifcount; a++) {
1550                         sprintf(&menu[1][a * 2 + 6][0],
1551                                 "Wireless_Advanced-wl%d.asp", a);
1552                         sprintf(&menu[1][a * 2 + 7][0], "Wireless_WDS-wl%d.asp",
1553                                 a);
1554                         if (ifcount == 1) {
1555                                 sprintf(&menuname[1][a * 2 + 7][0],
1556                                         "wirelessAdvanced");
1557                                 sprintf(&menuname[1][a * 2 + 8][0],
1558                                         "wirelessWds");
1559                         } else {
1560                                 sprintf(&menuname[1][a * 2 + 7][0],
1561                                         "wirelessAdvancedwl%d", a);
1562                                 sprintf(&menuname[1][a * 2 + 8][0],
1563                                         "wirelessWdswl%d", a);
1564                         }
1565                 }
1566 #ifdef HAVE_ERC
1567         }
1568 #endif
1569 #endif
1570 //fprintf(stderr,"generate menu header content\n");
1571
1572         int i, j;
1573
1574         websWrite(wp, "<div id=\"menu\">\n");
1575         websWrite(wp, " <div id=\"menuMain\">\n");
1576         websWrite(wp, "  <ul id=\"menuMainList\">\n");
1577 #ifdef HAVE_WAVESAT
1578         wimaxwifi = 1;
1579 #endif
1580         for (i = 0; i < 8; i++) {
1581                 if (strlen(menu[i][0]) == 0)
1582                         continue;
1583 #ifdef HAVE_MADWIFI
1584                 if (!wifi && !wimaxwifi
1585                     && !strcmp(menu[i][0], "Wireless_Basic.asp"))
1586                         i++;
1587 #endif
1588 //fprintf(stderr,"generate menu %s\n",menu[i][0]);
1589                 if (!strcmp(menu[i][0], mainmenu)) {
1590 #ifdef HAVE_MADWIFI
1591                         if (!wifi && wimaxwifi
1592                             && !strcmp(menu[i][0], "Wireless_Basic.asp"))
1593                                 websWrite(wp,
1594                                           "   <li class=\"current\"><span><script type=\"text/javascript\">Capture(bmenu.wimax)</script></span>\n");
1595                         else
1596 #endif
1597                                 websWrite(wp,
1598                                           "   <li class=\"current\"><span><script type=\"text/javascript\">Capture(bmenu.%s)</script></span>\n",
1599                                           menuname[i][0]);
1600                         websWrite(wp, "    <div id=\"menuSub\">\n");
1601                         websWrite(wp, "     <ul id=\"menuSubList\">\n");
1602
1603                         for (j = 0; j < 11; j++) {
1604
1605 #ifdef HAVE_MADWIFI
1606                                 if (!wifi
1607                                     && !strncmp(menu[i][j],
1608                                                 "Wireless_Basic.asp", 8))
1609                                         j++;
1610 #ifndef HAVE_SUPERCHANNEL
1611                                 if (!strcmp(menu[i][j], "SuperChannel.asp"))    // jump over
1612                                         // PPTP in
1613                                         // micro
1614                                         // build
1615                                         j++;
1616 #else
1617                                 if (!strcmp(menu[i][j], "SuperChannel.asp") && (issuperchannel() || !wifi))     // jump
1618                                         // over
1619                                         // PPTP
1620                                         // in
1621                                         // micro
1622                                         // build
1623                                         j++;
1624 #endif
1625 #else
1626                                 if (!strcmp(menu[i][j], "SuperChannel.asp"))    // jump over
1627                                         // PPTP in
1628                                         // micro
1629                                         // build
1630                                         j++;
1631 #endif
1632 #ifndef HAVE_WAVESAT
1633                                 if (!strcmp(menu[i][j], "WiMAX.asp"))   // jump over
1634                                         // WiMAX
1635                                         j++;
1636 #else
1637                                 if (!wimaxwifi && !strcmp(menu[i][j], "WiMAX.asp"))     // jump
1638                                         // over
1639                                         // WiMAX
1640                                         j++;
1641 #endif
1642 #ifdef HAVE_MADWIFI
1643                                 if (!wifi && !strcmp(menu[i][j], "WL_WPATable.asp"))    // jump
1644                                         // over
1645                                         // PPTP
1646                                         // in
1647                                         // micro
1648                                         // build
1649                                         j++;
1650                                 if (!strcmp(menu[i][j], "Wireless_radauth.asp"))
1651                                         j++;
1652                                 if (!wifi
1653                                     && !strncmp(menu[i][j], "Wireless_MAC.asp",
1654                                                 8))
1655                                         j++;
1656                                 if (!strncmp
1657                                     (menu[i][j], "Wireless_Advanced", 17))
1658                                         j++;
1659                                 if (!wifi
1660                                     && !strncmp(menu[i][j], "Wireless_WDS", 12))
1661                                         j++;
1662                                 if (!wifi
1663                                     && !strcmp(menu[i][j],
1664                                                "Status_Wireless.asp"))
1665                                         j++;
1666
1667 #endif
1668                                 if ((!vlan_supp) && !strcmp(menu[i][j], "Vlan.asp"))    // jump
1669                                         // over
1670                                         // VLANs
1671                                         // if
1672                                         // vlan
1673                                         // not
1674                                         // supported
1675                                         j++;
1676
1677 #ifndef HAVE_PPPOESERVER
1678                                 if (!strcmp(menu[i][j], "PPPoE_Server.asp"))
1679                                         j++;
1680 #endif
1681 #ifdef HAVE_MICRO
1682                                 if (!strcmp(menu[i][j], "PPTP.asp"))    // jump over PPTP in
1683                                         // micro build
1684                                         j++;
1685 #endif
1686 #ifndef HAVE_USB
1687                                 if (!strcmp(menu[i][j], "USB.asp"))     // jump over USB
1688                                         j++;
1689 #endif
1690 #ifndef HAVE_NAS_SERVER
1691                                 if (!strcmp(menu[i][j], "NAS.asp"))     // jump over NAS
1692                                         j++;
1693 #endif
1694 #ifdef HAVE_GLAUCO
1695                                 if (!strcmp(menu[i][j], "Factory_Defaults.asp"))
1696                                         j++;
1697                                 if (!strcmp(menu[i][j], "Upgrade.asp"))
1698                                         j++;
1699 #endif
1700 #ifndef HAVE_MILKFISH
1701                                 if (!strcmp(menu[i][j], "Milkfish.asp"))
1702                                         j++;
1703 #endif
1704 #ifndef HAVE_WOL
1705                                 if (!strcmp(menu[i][j], "Wol.asp"))
1706                                         j++;
1707 #endif
1708 #ifndef HAVE_EOP_TUNNEL
1709                                 if (!strcmp(menu[i][j], "eop-tunnel.asp"))
1710                                         j++;
1711 #endif
1712 #ifndef HAVE_VLANTAGGING
1713                                 if (!strcmp(menu[i][j], "Networking.asp"))
1714                                         j++;
1715 #endif
1716 #ifndef HAVE_CTORRENT
1717                                 if (!strcmp(menu[i][j], "P2P.asp"))
1718                                         j++;
1719 #endif
1720                                 if ((!sputnik) && !strcmp(menu[i][j], "Status_SputnikAPD.asp")) // jump
1721                                         // over
1722                                         // Sputnik
1723                                         j++;
1724                                 if ((!openvpn) && !strcmp(menu[i][j], "Status_OpenVPN.asp"))    // jump
1725                                         // over
1726                                         // OpenVPN
1727                                         j++;
1728                                 if ((!auth) && !strcmp(menu[i][j], "Info.htm")) // jump
1729                                         // over
1730                                         // Sys-Info
1731                                         j++;
1732
1733                                 if ((registered) && !strcmp(menu[i][j], "register.asp"))        // jump
1734                                         // over
1735                                         // Sys-Info
1736                                         j++;
1737 #ifdef HAVE_MADWIFI
1738                                 if (!strcmp(menu[i][j], submenu)
1739                                     && (strlen(menu[i][j])
1740                                         && !strcmp(menu[i][j],
1741                                                    "Wireless_Basic.asp")
1742                                         && !wifi && wimaxwifi)) {
1743                                         websWrite(wp,
1744                                                   "      <li><span><script type=\"text/javascript\">Capture(bmenu.wimax)</script></span></li>\n");
1745                                 }
1746 #endif
1747                                 else if (!strcmp(menu[i][j], submenu)
1748                                          && (strlen(menu[i][j]))) {
1749                                         websWrite(wp,
1750                                                   "      <li><span><script type=\"text/javascript\">Capture(bmenu.%s)</script></span></li>\n",
1751                                                   menuname[i][j + 1]);
1752                                 }
1753 #ifdef HAVE_HTTPS               // until https will allow upgrade and backup
1754 #ifdef HAVE_MATRIXSSL
1755                                 else if ((strlen(menu[i][j]) != 0) && (do_ssl)
1756                                          && ((!strcmp(menu[i][j], "Upgrade.asp")
1757                                               ||
1758                                               (!strcmp
1759                                                (menu[i][j], "config.asp"))))) {
1760                                         websWrite(wp,
1761                                                   "      <script type=\"text/javascript\">\n//<![CDATA[\n");
1762                                         websWrite(wp,
1763                                                   "      document.write(\"<li><a style=\\\"cursor:pointer\\\" title=\\\"\" + errmsg.err46 + \"\\\" onclick=\\\"alert(errmsg.err45)\\\" ><em>\" + bmenu.%s + \"</em></a></li>\");\n",
1764                                                   menuname[i][j + 1]);
1765                                         websWrite(wp,
1766                                                   "      \n//]]>\n</script>\n");
1767                                 }
1768 #endif
1769 #endif
1770 #ifdef HAVE_MADWIFI
1771                                 else if (strlen(menu[i][j])
1772                                          && !strcmp(menu[i][j],
1773                                                     "Wireless_Basic.asp")
1774                                          && !wifi && wimaxwifi) {
1775                                         websWrite(wp,
1776                                                   "      <li><a href=\"WiMAX.asp\"><script type=\"text/javascript\">Capture(bmenu.wimax)</script></a></li>\n");
1777                                 }
1778 #endif
1779                                 else if (strlen(menu[i][j])) {
1780                                         websWrite(wp,
1781                                                   "      <li><a href=\"%s\"><script type=\"text/javascript\">Capture(bmenu.%s)</script></a></li>\n",
1782                                                   menu[i][j],
1783                                                   menuname[i][j + 1]);
1784                                 }
1785                         }
1786                         websWrite(wp, "     </ul>\n");
1787                         websWrite(wp, "    </div>\n");
1788                         websWrite(wp, "    </li>\n");
1789                 }
1790 #ifdef HAVE_MADWIFI
1791                 else if (!strcmp(menu[i][0], "Wireless_Basic.asp") && !wifi
1792                          && wimaxwifi) {
1793                         websWrite(wp,
1794                                   "      <li><a href=\"WiMAX.asp\"><script type=\"text/javascript\">Capture(bmenu.wimax)</script></a></li>\n");
1795                 }
1796 #endif
1797                 else {
1798                         websWrite(wp,
1799                                   "   <li><a href=\"%s\"><script type=\"text/javascript\">Capture(bmenu.%s)</script></a></li>\n",
1800                                   menu[i][0], menuname[i][0]);
1801                 }
1802         }
1803         websWrite(wp, "  </ul>\n");
1804         websWrite(wp, " </div>\n");
1805         websWrite(wp, "</div>\n");
1806
1807         return;
1808 }
1809
1810 void ej_do_pagehead(webs_t wp, int argc, char_t ** argv)        // Eko
1811 {
1812         char *style = nvram_get("router_style");
1813
1814 #ifndef FASTWEB
1815         if (argc < 1) {
1816                 websError(wp, 400, "Insufficient args\n");
1817                 return;
1818         }
1819 #endif
1820
1821         /*
1822          * websWrite (wp, "<\?xml version=\"1.0\" encoding=\"%s\"\?>\n",
1823          * live_translate("lang_charset.set")); IE Problem ...
1824          */
1825         websWrite(wp,
1826                   "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
1827         websWrite(wp, "<html>\n");
1828         websWrite(wp, "\t<head>\n");
1829         websWrite(wp,
1830                   "\t\t<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=%s\" />\n",
1831                   live_translate("lang_charset.set"));
1832 #ifndef HAVE_MICRO
1833         websWrite(wp,
1834                   "\t\t<link rel=\"icon\" href=\"images/favicon.ico\" type=\"image/x-icon\" />\n");
1835         websWrite(wp,
1836                   "\t\t<link rel=\"shortcut icon\" href=\"images/favicon.ico\" type=\"image/x-icon\" />\n");
1837 #endif
1838         websWrite(wp,
1839                   "\t\t<script type=\"text/javascript\" src=\"common.js\"></script>\n");
1840         websWrite(wp,
1841                   "\t\t<script type=\"text/javascript\" src=\"lang_pack/english.js\"></script>\n");
1842 #ifdef HAVE_LANGUAGE
1843         websWrite(wp,
1844                   "\t\t<script type=\"text/javascript\" src=\"lang_pack/language.js\"></script>\n");
1845 #endif
1846         websWrite(wp,
1847                   "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"style/%s/style.css\" />\n",
1848                   style);
1849         websWrite(wp,
1850                   "\t\t<!--[if IE]><link type=\"text/css\" rel=\"stylesheet\" href=\"style/%s/style_ie.css\" /><![endif]-->\n",
1851                   style);
1852
1853 #ifdef HAVE_PWC
1854         websWrite(wp,
1855                   "\t\t<script type=\"text/javascript\" src=\"js/prototype.js\"></script>\n");
1856         websWrite(wp,
1857                   "\t\t<script type=\"text/javascript\" src=\"js/effects.js\"></script>\n");
1858         websWrite(wp,
1859                   "\t\t<script type=\"text/javascript\" src=\"js/window.js\"></script>\n");
1860         websWrite(wp,
1861                   "\t\t<script type=\"text/javascript\" src=\"js/window_effects.js\"></script>\n");
1862         websWrite(wp,
1863                   "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"style/pwc/default.css\" />\n");
1864         websWrite(wp,
1865                   "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"style/pwc/ddwrt.css\" />\n");
1866 #endif
1867         websWrite(wp, "\t\t<title>%s (build %s)", nvram_get("router_name"),SVN_REVISION);
1868         if (strlen(argv[0]) != 0) {
1869                 websWrite(wp, " - %s", live_translate(argv[0]));
1870         }
1871         websWrite(wp, "</title>\n");
1872
1873 }
1874
1875 void ej_do_hpagehead(webs_t wp, int argc, char_t ** argv)       // Eko
1876 {
1877         char *htitle;
1878
1879 #ifdef FASTWEB
1880         ejArgs(argc, argv, "%s", &htitle);
1881 #else
1882         if (ejArgs(argc, argv, "%s", &htitle) < 1) {
1883                 websError(wp, 400, "Insufficient args\n");
1884                 return;
1885         }
1886 #endif
1887         websWrite(wp,
1888                   "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
1889         if (!strcmp(htitle, "doctype_only"))
1890                 return;         // stop here, for About.htm
1891         websWrite(wp, "<html>\n");
1892         websWrite(wp, "\t<head>\n");
1893         websWrite(wp,
1894                   "\t\t<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=%s\" />\n",
1895                   live_translate("lang_charset.set"));
1896         websWrite(wp,
1897                   "\t\t<script type=\"text/javascript\" src=\"../common.js\"></script>\n");
1898         websWrite(wp,
1899                   "\t\t<script type=\"text/javascript\" src=\"../lang_pack/english.js\"></script>\n");
1900 #ifdef HAVE_LANGUAGE
1901         websWrite(wp,
1902                   "\t\t<script type=\"text/javascript\" src=\"../lang_pack/language.js\"></script>\n");
1903 #endif
1904         websWrite(wp,
1905                   "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"help.css\">\n");
1906         websWrite(wp, "\t\t<title>%s (build %s)", live_translate("share.help"),SVN_REVISION);
1907         websWrite(wp, " - %s</title>\n", live_translate(htitle));
1908         websWrite(wp, "\t</head>\n");
1909
1910 }
1911
1912 void ej_show_timeoptions(webs_t wp, int argc, char_t ** argv)   // Eko
1913 {
1914
1915         char timediffs[39][8] =
1916             { "-12", "-11", "-10", "-09.5", "-09", "-08", "-07", "-06", "-05",
1917                 "-04.5", "-04",
1918                 "-03.5", "-03", "-02", "-01", "+00",
1919                 "+01", "+02", "+03", "+03.5", "+04", "+04.5", "+05", "+05.5",
1920                 "+05.75",
1921                 "+06",
1922                 "+06.5", "+07", "+08", "+09", "+09.5", "+10", "+10.5", "+11",
1923                     "+11.5",
1924                 "+12", "+12.75", "+13", "+14"
1925         };
1926
1927         char timezones[39][8] =
1928             { "-12:00", "-11:00", "-10:00", "-09:30", "-09:00", "-08:00",
1929                 "-07:00",
1930                 "-06:00", "-05:00", "-04:30", "-04:00", "-03:30", "-03:00",
1931                     "-02:00",
1932                 "-01:00", "",
1933                 "+01:00", "+02:00", "+03:00", "+03:30", "+04:00", "+04:30",
1934                     "+05:00",
1935                 "+05:30",
1936                 "+05:45", "+06:00", "+06:30", "+07:00", "+08:00", "+09:00",
1937                     "+09:30",
1938                 "+10:00", "+10:30", "+11:00", "+11:30", "+12:00", "+12:45",
1939                     "+13:00",
1940                 "+14:00"
1941         };
1942
1943         int i;
1944
1945         for (i = 0; i < 39; i++) {
1946                 websWrite(wp,
1947                           "<option value=\"%s\" %s>UTC%s</option>\");\n",
1948                           timediffs[i], nvram_match("time_zone",
1949                                                     timediffs[i]) ?
1950                           "selected=\"selected\"" : "", timezones[i]);
1951
1952         }
1953
1954 }
1955
1956 void ej_show_wanipinfo(webs_t wp, int argc, char_t ** argv)     // Eko
1957 {
1958         char *wan_ipaddr;
1959         int wan_link;
1960
1961         if (getWET() || nvram_match("wan_proto", "disabled")) {
1962                 websWrite(wp, ": %s", live_translate("share.disabled"));
1963                 return;
1964         }
1965
1966         wan_link = check_wan_link(0);
1967
1968         if (nvram_match("wan_proto", "pptp")) {
1969                 wan_ipaddr =
1970                     wan_link ? nvram_safe_get("pptp_get_ip") :
1971                     nvram_safe_get("wan_ipaddr");
1972         } else if (!strcmp(nvram_safe_get("wan_proto"), "pppoe")) {
1973                 wan_ipaddr =
1974                     wan_link ? nvram_safe_get("wan_ipaddr") : "0.0.0.0";
1975         } else if (!strcmp(nvram_safe_get("wan_proto"), "3g")) {
1976                 wan_ipaddr =
1977                     wan_link ? nvram_safe_get("wan_ipaddr") : "0.0.0.0";
1978         } else if (nvram_match("wan_proto", "l2tp")) {
1979                 wan_ipaddr =
1980                     wan_link ? nvram_safe_get("l2tp_get_ip") :
1981                     nvram_safe_get("wan_ipaddr");
1982         } else {
1983                 wan_ipaddr = nvram_safe_get("wan_ipaddr");
1984         }
1985
1986         websWrite(wp, "&nbsp;IP: %s", wan_ipaddr);
1987
1988         return;
1989 }
1990
1991 /*
1992  * Example:
1993  * wan_proto=dhcp
1994  * <% nvram_selected("wan_proto", "dhcp",); %> produces: selected="selected"
1995  * <% nvram_selected_js("wan_proto", "dhcp"); %> produces: selected=\"selected\"
1996  * <% nvram_selected("wan_proto", "static"); %> does not produce
1997  */
1998 void ej_nvram_selected(webs_t wp, int argc, char_t ** argv)
1999 {
2000
2001 #ifndef FASTWEB
2002         if (argc < 2) ;
2003         {
2004                 websError(wp, 400, "Insufficient args\n");
2005                 return;
2006         }
2007 #endif
2008
2009         if (nvram_match(argv[0], argv[1])) {
2010                 websWrite(wp, "selected=\"selected\"");
2011         }
2012         return;
2013 }
2014
2015 void ej_nvram_selected_js(webs_t wp, int argc, char_t ** argv)
2016 {
2017
2018 #ifndef FASTWEB
2019         if (argc < 2) ;
2020         {
2021                 websError(wp, 400, "Insufficient args\n");
2022                 return;
2023         }
2024 #endif
2025
2026         if (nvram_match(argv[0], argv[1])) {
2027                 websWrite(wp, "selected=\\\"selected\\\"");
2028         }
2029         return;
2030 }
2031
2032 void ej_getrebootflags(webs_t wp, int argc, char_t ** argv)
2033 {
2034 #ifdef HAVE_RB500
2035         websWrite(wp, "1");
2036 #elif HAVE_MAGICBOX
2037         websWrite(wp, "2");
2038 #elif HAVE_FONERA
2039         websWrite(wp, "2");
2040 #elif HAVE_MERAKI
2041         websWrite(wp, "2");
2042 #elif HAVE_LS2
2043         websWrite(wp, "2");
2044 #elif HAVE_LS5
2045         websWrite(wp, "2");
2046 #elif HAVE_WHRAG108
2047         websWrite(wp, "2");
2048 #elif HAVE_TW6600
2049         websWrite(wp, "2");
2050 #elif HAVE_CA8
2051         websWrite(wp, "2");
2052 #elif HAVE_GATEWORX
2053         websWrite(wp, "1");
2054 #elif HAVE_X86
2055         websWrite(wp, "1");
2056 #else
2057         websWrite(wp, "0");
2058 #endif
2059 }
2060
2061 void ej_tran(webs_t wp, int argc, char_t ** argv)
2062 {
2063
2064 #ifndef FASTWEB
2065         if (argc != 1)
2066                 return;
2067 #endif
2068         websWrite(wp, "<script type=\"text/javascript\">Capture(%s)</script>",
2069                   argv[0]);
2070         return;
2071 }
2072
2073 /*
2074  * Example:
2075  * wan_proto=dhcp
2076  * <% nvram_checked("wan_proto", "dhcp"); %> produces: checked="checked"
2077  * <% nvram_checked_js("wan_proto", "dhcp"); %> produces: checked=\"checked\"
2078  * <% nvram_checked("wan_proto", "static"); %> does not produce
2079  */
2080 void ej_nvram_checked(webs_t wp, int argc, char_t ** argv)
2081 {
2082
2083 #ifdef FASTWEB
2084         if (argc < 2) {
2085                 websError(wp, 400, "Insufficient args\n");
2086                 return;
2087         }
2088 #endif
2089
2090         if (nvram_match(argv[0], argv[1])) {
2091                 websWrite(wp, "checked=\"checked\"");
2092         }
2093
2094         return;
2095 }
2096
2097 void ej_nvram_checked_js(webs_t wp, int argc, char_t ** argv)
2098 {
2099
2100 #ifndef FASTWEB
2101         if (argc < 2) {
2102                 websError(wp, 400, "Insufficient args\n");
2103                 return;
2104         }
2105 #endif
2106
2107         if (nvram_match(argv[0], argv[1])) {
2108                 websWrite(wp, "checked=\\\"checked\\\"");
2109         }
2110
2111         return;
2112 }
2113
2114 void ej_make_time_list(webs_t wp, int argc, char_t ** argv)
2115 {
2116         int i, st, en;
2117         char ic[16];
2118
2119 #ifndef FASTWEB
2120         if (argc < 3) {
2121                 websError(wp, 400, "Insufficient args\n");
2122                 return;
2123         }
2124 #endif
2125
2126         st = atoi(argv[1]);
2127         en = atoi(argv[2]);
2128
2129         for (i = st; i <= en; i++) {
2130                 sprintf(ic, "%d", i);
2131                 websWrite(wp, "<option value=\"%d\" %s >%02d</option>\n", i,
2132                           nvram_match(argv[0],
2133                                       ic) ? "selected=\"selected\"" : "", i);
2134         }
2135
2136         return;
2137 }
2138
2139 #ifdef HAVE_CPUTEMP
2140 void ej_get_cputemp(webs_t wp, int argc, char_t ** argv)
2141 {
2142 #ifdef HAVE_GATEWORX
2143         int TEMP_MUL = 100;
2144
2145         if (getRouterBrand() == ROUTER_BOARD_GATEWORX_SWAP)
2146                 TEMP_MUL = 200;
2147
2148         FILE *fp =
2149             fopen
2150             ("/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0028/temp_input",
2151              "rb");
2152 #else
2153 #define TEMP_MUL 1000
2154 #ifdef HAVE_X86
2155         FILE *fp =
2156             fopen("/sys/devices/platform/i2c-1/1-0048/temp1_input", "rb");
2157 #else
2158         FILE *fp =
2159             fopen("/sys/devices/platform/i2c-0/0-0048/temp1_input", "rb");
2160 #endif
2161 #endif
2162
2163         if (fp == NULL) {
2164                 websWrite(wp, "%s", live_translate("status_router.notavail"));  // no
2165                 // i2c
2166                 // lm75
2167                 // found
2168                 return;
2169         }
2170         int temp;
2171
2172         fscanf(fp, "%d", &temp);
2173         fclose(fp);
2174         int high = temp / TEMP_MUL;
2175         int low = (temp - (high * TEMP_MUL)) / (TEMP_MUL / 10);
2176
2177         websWrite(wp, "%d.%d &#176;C", high, low);      // no i2c lm75 found
2178 }
2179
2180 void ej_show_cpu_temperature(webs_t wp, int argc, char_t ** argv)
2181 {
2182         websWrite(wp, "<div class=\"setting\">\n");
2183         websWrite(wp,
2184                   "<div class=\"label\"><script type=\"text/javascript\">Capture(status_router.cputemp)</script></div>\n");
2185         websWrite(wp, "<span id=\"cpu_temp\">");
2186         ej_get_cputemp(wp, argc, argv);
2187         websWrite(wp, "</span>&nbsp;\n");
2188         websWrite(wp, "</div>\n");
2189 }
2190 #else
2191 void ej_get_cputemp(webs_t wp, int argc, char_t ** argv)
2192 {
2193         return;
2194 }
2195
2196 void ej_show_cpu_temperature(webs_t wp, int argc, char_t ** argv)
2197 {
2198         return;
2199 }
2200 #endif
2201
2202 #ifdef HAVE_VOLT
2203 void ej_get_voltage(webs_t wp, int argc, char_t ** argv)
2204 {
2205         FILE *fp =
2206             fopen
2207             ("/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0028/volt",
2208              "rb");
2209
2210         if (fp == NULL) {
2211                 websWrite(wp, "%s", live_translate("status_router.notavail"));  // no
2212                 // i2c
2213                 // lm75
2214                 // found
2215                 return;
2216         }
2217         int temp;
2218
2219         fscanf(fp, "%d", &temp);
2220         fclose(fp);
2221         // temp*=564;
2222         int high = temp / 1000;
2223         int low = (temp - (high * 1000)) / 100;
2224
2225         websWrite(wp, "%d.%d V", high, low);    // no i2c lm75 found
2226 }
2227
2228 void ej_show_voltage(webs_t wp, int argc, char_t ** argv)
2229 {
2230         websWrite(wp, "<div class=\"setting\">\n");
2231         websWrite(wp,
2232                   "<div class=\"label\"><script type=\"text/javascript\">Capture(status_router.inpvolt)</script></div>\n");
2233         websWrite(wp, "<span id=\"voltage\">");
2234         ej_get_voltage(wp, argc, argv);
2235         websWrite(wp, "</span>&nbsp;\n");
2236         websWrite(wp, "</div>\n");
2237 }
2238 #else
2239 void ej_get_voltage(webs_t wp, int argc, char_t ** argv)
2240 {
2241         return;
2242 }
2243
2244 void ej_show_voltage(webs_t wp, int argc, char_t ** argv)
2245 {
2246         return;
2247 }
2248 #endif
2249
2250 static void showencstatus(webs_t wp, char *prefix)
2251 {
2252         char akm[64];
2253
2254         sprintf(akm, "%s_akm", prefix);
2255         websWrite(wp, "<div class=\"setting\">\n");
2256         websWrite(wp,
2257                   "<div class=\"label\"><script type=\"text/javascript\">Capture(share.encrypt)</script>&nbsp;-&nbsp;<script type=\"text/javascript\">Capture(share.intrface)</script>&nbsp;%s</div>\n",
2258                   prefix);
2259         websWrite(wp, "<script type=\"text/javascript\">");
2260         if (nvram_match(akm, "disabled")) {
2261                 websWrite(wp, "Capture(share.disabled)");
2262                 websWrite(wp, "</script>");
2263         } else {
2264                 websWrite(wp, "Capture(share.enabled)");
2265                 websWrite(wp, "</script>,&nbsp;");
2266
2267                 if (nvram_match(akm, "psk"))
2268                         websWrite(wp, "WPA Personal");
2269                 if (nvram_match(akm, "wpa"))
2270                         websWrite(wp, "WPA Enterprise");
2271                 if (nvram_match(akm, "psk2"))
2272                         websWrite(wp, "WPA2 Personal");
2273                 if (nvram_match(akm, "wpa2"))
2274                         websWrite(wp, "WPA2 Enterprise");
2275                 if (nvram_match(akm, "psk psk2"))
2276                         websWrite(wp, "WPA2 Personal Mixed");
2277                 if (nvram_match(akm, "wpa wpa2"))
2278                         websWrite(wp, "WPA Enterprise Mixed");
2279                 if (nvram_match(akm, "radius"))
2280                         websWrite(wp, "RADIUS");
2281                 if (nvram_match(akm, "wep"))
2282                         websWrite(wp, "WEP");
2283         }
2284
2285         websWrite(wp, "\n</div>\n");
2286         return;
2287 }
2288
2289 void ej_get_txpower(webs_t wp, int argc, char_t ** argv)
2290 {
2291         char txpwr[32];
2292         char m[32];
2293
2294         strncpy(m, nvram_safe_get("wifi_display"), 4);
2295         m[4] = 0;
2296         sprintf(txpwr, "%s_txpwr", m);
2297 #ifdef HAVE_MADWIFI
2298         websWrite(wp, "%d dBm", wifi_gettxpower(m));
2299 #elif HAVE_RT2880
2300         websWrite(wp, "%s mW", nvram_safe_get(txpwr));
2301 #else //broadcom
2302         websWrite(wp, "%d mW", bcm_gettxpower(m));
2303 #endif
2304 }
2305
2306 void ej_getencryptionstatus(webs_t wp, int argc, char_t ** argv)
2307 {
2308         char *mode = nvram_safe_get("wifi_display");
2309
2310         showencstatus(wp, mode);
2311 }
2312
2313 void ej_getwirelessstatus(webs_t wp, int argc, char_t ** argv)
2314 {
2315         char mode[32];
2316         char m[32];
2317
2318         strncpy(m, nvram_safe_get("wifi_display"), 4);
2319         m[4] = 0;
2320         sprintf(mode, "%s_mode", m);
2321
2322         if (nvram_match(mode, "wet") || nvram_match(mode, "sta")
2323             || nvram_match(mode, "infra"))
2324                 websWrite(wp,
2325                           "<script type=\"text/javascript\">Capture(info.ap)</script>");
2326         else if (nvram_match(mode, "apsta") || nvram_match(mode, "apstawet")) {
2327                 websWrite(wp,
2328                           "<script type=\"text/javascript\">Capture(info.ap)</script>");
2329                 websWrite(wp, " & ");
2330                 websWrite(wp,
2331                           "<script type=\"text/javascript\">Capture(status_wireless.legend3)</script>");
2332         } else
2333                 websWrite(wp,
2334                           "<script type=\"text/javascript\">Capture(status_wireless.legend3)</script>");
2335 }
2336
2337 void ej_getwirelessssid(webs_t wp, int argc, char_t ** argv)
2338 {
2339         char ssid[32];
2340
2341         sprintf(ssid, "%s_ssid", nvram_safe_get("wifi_display"));
2342         tf_webWriteESCNV(wp, ssid);
2343
2344 }
2345
2346 void ej_getwirelessmode(webs_t wp, int argc, char_t ** argv)
2347 {
2348         char mode[32];
2349         char m[32];
2350
2351         strncpy(m, nvram_safe_get("wifi_display"), 4);
2352         m[4] = 0;
2353         sprintf(mode, "%s_mode", m);
2354
2355         websWrite(wp, "<script type=\"text/javascript\">");
2356         if (nvram_match(mode, "wet"))
2357                 websWrite(wp, "Capture(wl_basic.clientBridge)");
2358         if (nvram_match(mode, "ap"))
2359                 websWrite(wp, "Capture(wl_basic.ap)");
2360         if (nvram_match(mode, "sta"))
2361                 websWrite(wp, "Capture(wl_basic.client)");
2362         if (nvram_match(mode, "infra"))
2363                 websWrite(wp, "Capture(wl_basic.adhoc)");
2364         if (nvram_match(mode, "apsta"))
2365                 websWrite(wp, "Capture(wl_basic.repeater)");
2366         if (nvram_match(mode, "apstawet"))
2367                 websWrite(wp, "Capture(wl_basic.repeaterbridge)");
2368         if (nvram_match(mode, "wdssta"))
2369                 websWrite(wp, "Capture(wl_basic.wdssta)");
2370         if (nvram_match(mode, "wdsap"))
2371                 websWrite(wp, "Capture(wl_basic.wdsap)");
2372         websWrite(wp, "</script>&nbsp;\n");
2373 }
2374
2375 void ej_getwirelessnetmode(webs_t wp, int argc, char_t ** argv)
2376 {
2377
2378         char mode[32];
2379         char m[32];
2380
2381         strncpy(m, nvram_safe_get("wifi_display"), 4);
2382         m[4] = 0;
2383         sprintf(mode, "%s_net_mode", m);
2384
2385         websWrite(wp, "<script type=\"text/javascript\">");
2386         if (nvram_match(mode, "disabled"))
2387                 websWrite(wp, "Capture(share.disabled)");
2388         if (nvram_match(mode, "mixed"))
2389                 websWrite(wp, "Capture(wl_basic.mixed)");
2390         if (nvram_match(mode, "bg-mixed"))
2391                 websWrite(wp, "Capture(wl_basic.bg)");
2392         if (nvram_match(mode, "g-only"))
2393                 websWrite(wp, "Capture(wl_basic.g)");
2394         if (nvram_match(mode, "b-only"))
2395                 websWrite(wp, "Capture(wl_basic.b)");
2396         if (nvram_match(mode, "n-only"))
2397                 websWrite(wp, "Capture(wl_basic.n)");
2398         if (nvram_match(mode, "a-only"))
2399                 websWrite(wp, "Capture(wl_basic.a)");
2400         if (nvram_match(mode, "na-only"))
2401                 websWrite(wp, "Capture(wl_basic.na)");
2402         websWrite(wp, "</script>&nbsp;\n");
2403 }
2404
2405 void ej_show_openvpn_status(webs_t wp, int argc, char_t ** argv)
2406 {
2407         websWrite(wp,
2408                   "<fieldset>\n<legend><script type=\"text/javascript\">Capture(share.state)</script></legend>\n");
2409
2410         system2("/etc/openvpnstate.sh > /tmp/.temp");
2411         FILE *in = fopen("/tmp/.temp", "r");
2412
2413         while (!feof(in)) {
2414                 int b = getc(in);
2415
2416                 if (b != EOF)
2417                         wfputc(b, wp);
2418         }
2419         fclose(in);
2420         websWrite(wp, "</fieldset><br />");
2421         websWrite(wp,
2422                   "<fieldset>\n<legend><script type=\"text/javascript\">Capture(share.statu)</script></legend>\n");
2423         system2("/etc/openvpnstatus.sh > /tmp/.temp");
2424         in = fopen("/tmp/.temp", "r");
2425         while (!feof(in)) {
2426                 int b = getc(in);
2427
2428                 if (b != EOF)
2429                         wfputc(b, wp);
2430         }
2431         fclose(in);
2432         websWrite(wp, "</fieldset><br />");
2433         websWrite(wp,
2434                   "<fieldset>\n<legend><script type=\"text/javascript\">Capture(log.legend)</script></legend>\n");
2435         system2("/etc/openvpnlog.sh > /tmp/.temp");
2436         in = fopen("/tmp/.temp", "r");
2437         while (!feof(in)) {
2438                 int b = getc(in);
2439
2440                 if (b != EOF)
2441                         wfputc(b, wp);
2442         }
2443         fclose(in);
2444         websWrite(wp, "</fieldset><br />");
2445
2446 }
2447
2448 void ej_radio_on(webs_t wp, int argc, char_t ** argv)
2449 {
2450         int radiooff = -1;
2451
2452 #ifdef HAVE_MADWIFI
2453         char *ifname = nvram_safe_get("wifi_display");
2454
2455         if (strlen(ifname) > 0) {
2456                 int state = get_radiostate(ifname);
2457
2458                 switch (state) {
2459                 case 1:
2460                         websWrite(wp, "1");
2461                         break;
2462                 default:
2463                         websWrite(wp, "0");
2464                         break;
2465                 }
2466         } else {
2467                 websWrite(wp, "0");
2468         }
2469 #elif HAVE_RT2880
2470
2471         int state = get_radiostate("wl0");
2472
2473         switch (state) {
2474         case 1:
2475                 websWrite(wp, "1");
2476                 break;
2477         default:
2478                 websWrite(wp, "0");
2479                 break;
2480         }
2481
2482 #else
2483         char name[32];
2484         sprintf(name, "%s_ifname", nvram_safe_get("wifi_display"));
2485
2486         char *ifname = nvram_safe_get(name);
2487
2488         wl_ioctl(ifname, WLC_GET_RADIO, &radiooff, sizeof(int));
2489
2490         switch ((radiooff & WL_RADIO_SW_DISABLE)) {
2491         case 0:
2492                 websWrite(wp, "1");
2493                 break;
2494         default:
2495                 websWrite(wp, "0");
2496                 break;
2497         }
2498 #endif
2499 }
2500
2501 void ej_get_radio_state(webs_t wp, int argc, char_t ** argv)
2502 {
2503         int radiooff = -1;
2504
2505 #ifdef HAVE_MADWIFI
2506         char *ifname = nvram_safe_get("wifi_display");
2507
2508         if (strlen(ifname) > 0) {
2509                 int state = get_radiostate(ifname);
2510
2511                 switch (state) {
2512                 case 1:
2513                         websWrite(wp, "%s",
2514                                   live_translate("wl_basic.radio_on"));
2515                         break;
2516                 case -1:
2517                         websWrite(wp, "%s", live_translate("share.unknown"));
2518                         break;
2519                 default:        // 1: software disabled, 2: hardware
2520                         // disabled, 3: both are disabled
2521                         websWrite(wp, "%s",
2522                                   live_translate("wl_basic.radio_off"));
2523                         break;
2524                 }
2525         } else {
2526                 websWrite(wp, "%s", live_translate("share.unknown"));
2527         }
2528 #elif HAVE_RT2880
2529
2530         int state = get_radiostate("wl0");
2531
2532         switch (state) {
2533         case 1:
2534                 websWrite(wp, "%s", live_translate("wl_basic.radio_on"));
2535                 break;
2536         case -1:
2537                 websWrite(wp, "%s", live_translate("share.unknown"));
2538                 break;
2539         default:                // 1: software disabled, 2: hardware
2540                 // disabled, 3: both are disabled
2541                 websWrite(wp, "%s", live_translate("wl_basic.radio_off"));
2542                 break;
2543         }
2544
2545 #else
2546         char name[32];
2547         sprintf(name, "%s_ifname", nvram_safe_get("wifi_display"));
2548
2549         char *ifname = nvram_safe_get(name);
2550
2551         wl_ioctl(ifname, WLC_GET_RADIO, &radiooff, sizeof(int));
2552
2553         switch ((radiooff & WL_RADIO_SW_DISABLE)) {
2554         case 0:
2555                 websWrite(wp, "%s", live_translate("wl_basic.radio_on"));
2556                 break;
2557         case -1:
2558                 websWrite(wp, "%s", live_translate("share.unknown"));
2559                 break;
2560         default:                // 1: software disabled, 2: hardware
2561                 // disabled, 3: both are disabled
2562                 websWrite(wp, "%s", live_translate("wl_basic.radio_off"));
2563                 break;
2564         }
2565 #endif
2566 }
2567
2568 void ej_dumparptable(webs_t wp, int argc, char_t ** argv)
2569 {
2570         FILE *f;
2571         FILE *host;
2572         FILE *conn;
2573         char buf[256];
2574         char hostname[128];
2575         char ip[16];
2576         char ip2[20];
2577         char fullip[18];
2578         char mac[18];
2579         char landev[16];
2580         int count = 0;
2581         int conn_count = 0;
2582
2583         if ((f = fopen("/proc/net/arp", "r")) != NULL) {
2584                 while (fgets(buf, sizeof(buf), f)) {
2585                         if (sscanf
2586                             (buf, "%15s %*s %*s %17s %*s %s", ip, mac,
2587                              landev) != 3)
2588                                 continue;
2589                         if ((strlen(mac) != 17)
2590                             || (strcmp(mac, "00:00:00:00:00:00") == 0))
2591                                 continue;
2592                         if (strcmp(landev, nvram_safe_get("wan_iface")) == 0)
2593                                 continue;       // skip all but LAN arp entries
2594                         strcpy(hostname, "*");  // set name to *
2595
2596                         /*
2597                          * count open connections per IP
2598                          */
2599                         if ((conn =
2600                              fopen("/proc/net/ip_conntrack", "r")) != NULL) {
2601                                 strcpy(ip2, ip);
2602                                 strcat(ip2, " ");
2603
2604                                 while (fgets(buf, sizeof(buf), conn)) {
2605                                         if (strstr(buf, ip2))
2606                                                 conn_count++;
2607                                 }
2608                                 fclose(conn);
2609                         }
2610
2611                         /*
2612                          * end count
2613                          */
2614
2615                         /*
2616                          * do nslookup
2617                          */
2618
2619                         // struct servent *servp;
2620                         // char buf1[256];
2621                         //
2622                         // getHostName (buf1, ip);
2623                         // if (strcmp(buf1, "unknown"))
2624                         // strcpy (hostname, buf1);
2625                         /*
2626                          * end nslookup
2627                          */
2628
2629                         /*
2630                          * look into hosts file for hostnames (static leases)
2631                          */
2632                         if ((host = fopen("/tmp/hosts", "r")) != NULL
2633                             && !strcmp(hostname, "*")) {
2634                                 while (fgets(buf, sizeof(buf), host)) {
2635                                         sscanf(buf, "%15s %*s", fullip);
2636
2637                                         if (!strcmp(ip, fullip)) {
2638                                                 sscanf(buf, "%*15s %s",
2639                                                        hostname);
2640                                         }
2641                                 }
2642                                 fclose(host);
2643                         }
2644                         /*
2645                          * end hosts file lookup
2646                          */
2647
2648                         /*
2649                          * check for dnsmasq leases in /tmp/dnsmasq.leases and /jffs/ if
2650                          * hostname is still unknown
2651                          */
2652
2653                         if (!strcmp(hostname, "*")
2654                             && nvram_match("dhcp_dnsmasq", "1")
2655                             && nvram_match("dhcpd_usenvram", "0")) {
2656                                 if (!(host = fopen("/tmp/dnsmasq.leases", "r")))
2657                                         host =
2658                                             fopen("/jffs/dnsmasq.leases", "r");
2659
2660                                 if (host) {
2661
2662                                         while (fgets(buf, sizeof(buf), host)) {
2663                                                 sscanf(buf, "%*s %*s %15s %*s",
2664                                                        fullip);
2665
2666                                                 if (strcmp(ip, fullip) == 0) {
2667                                                         sscanf(buf,
2668                                                                "%*s %*s %*s %s",
2669                                                                hostname);
2670                                                 }
2671                                         }
2672                                         fclose(host);
2673                                 }
2674                         }
2675                         /*
2676                          * end dnsmasq.leases check
2677                          */
2678
2679                         /*
2680                          * check nvram for dnsmasq leases in nvram if hostname is still
2681                          * unknown
2682                          */
2683
2684                         if (!strcmp(hostname, "*")
2685                             && nvram_match("dhcp_dnsmasq", "1")
2686                             && nvram_match("dhcpd_usenvram", "1")) {
2687                                 sscanf(nvram_nget("dnsmasq_lease_%s", ip),
2688                                        "%*s %*s %*s %s", hostname);
2689                         }
2690                         /*
2691                          * end nvram check
2692                          */
2693
2694                         websWrite(wp, "%c'%s','%s','%s','%d'",
2695                                   (count ? ',' : ' '), hostname, ip, mac,
2696                                   conn_count);
2697                         ++count;
2698                         conn_count = 0;
2699                 }
2700                 fclose(f);
2701         }
2702 }
2703
2704 #ifdef HAVE_EOP_TUNNEL
2705
2706 void ej_show_eop_tunnels(webs_t wp, int argc, char_t ** argv)
2707 {
2708
2709         int tun;
2710         char temp[32];
2711
2712         for (tun = 1; tun < 11; tun++) {
2713
2714                 websWrite(wp, "<fieldset>\n");
2715                 websWrite(wp,
2716                           "<legend><script type=\"text/javascript\">Capture(eoip.tunnel)</script> %d</legend>\n",
2717                           tun);
2718                 websWrite(wp, "<div class=\"setting\">\n");
2719                 websWrite(wp,
2720                           "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.srv)</script></div>\n");
2721                 sprintf(temp, "oet%d_en", tun);
2722                 websWrite(wp,
2723                           "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s onclick=\"show_layer_ext(this, 'idoet%d', true)\" /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
2724                           temp,
2725                           (nvram_match(temp, "1") ? "checked=\"checked\"" : ""),
2726                           tun);
2727                 websWrite(wp,
2728                           "<input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s onclick=\"show_layer_ext(this, 'idoet%d', false)\" /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
2729                           temp,
2730                           (nvram_match(temp, "0") ? "checked=\"checked\"" : ""),
2731                           tun);
2732                 websWrite(wp, "</div>\n");
2733                 websWrite(wp, "<div id=\"idoet%d\">\n", tun);
2734                 websWrite(wp, "<div class=\"setting\">\n");
2735                 websWrite(wp,
2736                           "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.remoteIP)</script></div>\n");
2737                 websWrite(wp,
2738                           "<input type=\"hidden\" name=\"oet%d_rem\" value=\"0.0.0.0\"/>\n",
2739                           tun);
2740                 sprintf(temp, "oet%d_rem", tun);
2741                 websWrite(wp,
2742                           "<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_0\" onblur=\"valid_range(this,0,255,eoip.remoteIP)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_1\" onblur=\"valid_range(this,0,255,eoip.tunnelID)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_2\" onblur=\"valid_range(this,0,255,eoip.tunnelID)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_3\" onblur=\"valid_range(this,1,254,eoip.tunnelID)\" value=\"%d\" />\n",
2743                           temp, get_single_ip(nvram_safe_get(temp), 0), temp,
2744                           get_single_ip(nvram_safe_get(temp), 1), temp,
2745                           get_single_ip(nvram_safe_get(temp), 2), temp,
2746                           get_single_ip(nvram_safe_get(temp), 3));
2747                 websWrite(wp, "</div>\n");
2748 /*
2749         websWrite( wp, "<div class=\"setting\">\n" );
2750         websWrite( wp,
2751                    "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.tunnelID)</script></div>\n" );
2752                         sprintf( temp, "oet%d_id", tun );
2753         websWrite( wp,
2754                    "<input size=\"4\" maxlength=\"3\" class=\"num\" name=\"%s\" onblur=\"valid_range(this,0,999,eoip.tunnelID)\" value=\"%s\" />\n",
2755                    temp, nvram_get( temp ) );
2756         websWrite( wp, "</div>\n" );
2757
2758         websWrite( wp, "<div class=\"setting\">\n" );
2759         websWrite( wp,
2760                    "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.comp)</script></div>\n" );
2761         sprintf( temp, "oet%d_comp", tun );
2762         websWrite( wp,
2763                    "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
2764                    temp,
2765                    ( nvram_match( temp, "1" ) ? "checked=\"checked\"" :
2766                      "" ) );
2767         websWrite( wp,
2768                    "<input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
2769                    temp,
2770                    ( nvram_match( temp, "0" ) ? "checked=\"checked\"" :
2771                      "" ) );
2772         websWrite( wp, "</div>\n" );
2773         websWrite( wp, "<div class=\"setting\">\n" );
2774         websWrite( wp,
2775                    "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.passtos)</script></div>\n" );
2776         sprintf( temp, "oet%d_pt", tun );
2777         websWrite( wp,
2778                    "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
2779                    temp,
2780                    ( nvram_match( temp, "1" ) ? "checked=\"checked\"" :
2781                      "" ) );
2782         websWrite( wp,
2783                    "<input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
2784                    temp,
2785                    ( nvram_match( temp, "0" ) ? "checked=\"checked\"" :
2786                      "" ) );
2787         websWrite( wp, "</div>\n" );
2788         websWrite( wp, "<div class=\"setting\">\n" );
2789         websWrite( wp,
2790                    "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.frag)</script></div>\n" );
2791         sprintf( temp, "oet%d_fragment", tun );
2792         websWrite( wp,
2793                    "<input size=\"4\" maxlength=\"4\" class=\"num\" name=\"%s\" onblur=\"valid_range(this,0,1500,eoip.frag)\" value=\"%s\" />\n",
2794                    temp, nvram_get( temp ) );
2795         websWrite( wp, "</div>\n" );
2796         websWrite( wp, "<div class=\"setting\">\n" );
2797         websWrite( wp,
2798                    "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.mssfix)</script></div>\n" );
2799         sprintf( temp, "oet%d_mssfix", tun );
2800         websWrite( wp,
2801                    "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
2802                    temp,
2803                    ( nvram_match( temp, "1" ) ? "checked=\"checked\"" :
2804                      "" ) );
2805         websWrite( wp,
2806                    "<input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
2807                    temp,
2808                    ( nvram_match( temp, "0" ) ? "checked=\"checked\"" :
2809                      "" ) );
2810         websWrite( wp, "</div>\n" );
2811         websWrite( wp, "<div class=\"setting\">\n" );
2812         websWrite( wp,
2813                    "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.shaper)</script></div>\n" );
2814         sprintf( temp, "oet%d_shaper", tun );
2815         websWrite( wp,
2816                    "<input size=\"6\" maxlength=\"6\" class=\"num\" name=\"%s\" onblur=\"valid_range(this,0,100000,eoip.shaper)\" value=\"%s\" />\n",
2817                    temp, nvram_get( temp ) );
2818         websWrite( wp, "</div>\n" );
2819 */
2820                 websWrite(wp, "<div class=\"setting\">\n");
2821                 websWrite(wp,
2822                           "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.bridging)</script></div>\n");
2823                 sprintf(temp, "oet%d_bridged", tun);
2824                 websWrite(wp,
2825                           "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s onclick=\"show_layer_ext(this, 'idbridged%d', false)\" /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
2826                           temp,
2827                           (nvram_match(temp, "1") ? "checked=\"checked\"" : ""),
2828                           tun);
2829                 websWrite(wp,
2830                           " <input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s onclick=\"show_layer_ext(this, 'idbridged%d', true)\" /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
2831                           temp,
2832                           (nvram_match(temp, "0") ? "checked=\"checked\"" : ""),
2833                           tun);
2834                 websWrite(wp, "</div>\n");
2835                 websWrite(wp, "<div id=\"idbridged%d\">\n", tun);
2836                 websWrite(wp, "<div class=\"setting\">\n");
2837                 websWrite(wp,
2838                           "<div class=\"label\"><script type=\"text/javascript\">Capture(share.ip)</script></div>\n");
2839                 websWrite(wp,
2840                           "<input type=\"hidden\" name=\"oet%d_ipaddr\" value=\"0.0.0.0\"/>\n",
2841                           tun);
2842                 sprintf(temp, "oet%d_ipaddr", tun);
2843                 websWrite(wp,
2844                           "<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_0\" onblur=\"valid_range(this,0,255,share.ip)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_1\" onblur=\"valid_range(this,0,255,share.ip)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_2\" onblur=\"valid_range(this,0,255,share.ip)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_3\" onblur=\"valid_range(this,1,254,share.ip)\" value=\"%d\" />\n",
2845                           temp, get_single_ip(nvram_safe_get(temp), 0), temp,
2846                           get_single_ip(nvram_safe_get(temp), 1), temp,
2847                           get_single_ip(nvram_safe_get(temp), 2), temp,
2848                           get_single_ip(nvram_safe_get(temp), 3));
2849                 websWrite(wp, "</div>\n");
2850                 websWrite(wp, "<div class=\"setting\">\n");
2851                 websWrite(wp,
2852                           "<div class=\"label\"><script type=\"text/javascript\">Capture(share.subnet)</script></div>\n");
2853                 websWrite(wp,
2854                           "<input type=\"hidden\" name=\"oet%d_netmask\" value=\"0.0.0.0\"/>\n",
2855                           tun);
2856                 sprintf(temp, "oet%d_netmask", tun);
2857                 websWrite(wp,
2858                           "<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_0\" onblur=\"valid_range(this,0,255,share.subnet)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_1\" onblur=\"valid_range(this,0,255,share.subnet)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_2\" onblur=\"valid_range(this,0,255,share.subnet)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_3\" onblur=\"valid_range(this,0,254,share.subnet)\" value=\"%d\" />\n",
2859                           temp, get_single_ip(nvram_safe_get(temp), 0), temp,
2860                           get_single_ip(nvram_safe_get(temp), 1), temp,
2861                           get_single_ip(nvram_safe_get(temp), 2), temp,
2862                           get_single_ip(nvram_safe_get(temp), 3));
2863                 websWrite(wp, "</div>\n");
2864                 websWrite(wp, "</div>\n");
2865                 websWrite(wp, "</div>\n");
2866                 websWrite(wp, "</fieldset><br/>\n");
2867         }
2868 }
2869
2870 #endif
2871
2872 // web-writes html escaped (&#xxx;) nvram entries / test buffered
2873 int tf_webWriteESCNV(webs_t wp, const char *nvname)
2874 {
2875         char buf[512];
2876         int n;
2877         int r;
2878         const char *c;
2879
2880         n = 0;
2881         r = 0;
2882         for (c = nvram_safe_get(nvname); *c; c++) {
2883                 if ((isprint(*c)) && (*c != '"') && (*c != '&')
2884                     && (*c != '<') && (*c != '>') && (*c != '\'')
2885                     && (*c != '\\')) {
2886                         buf[n++] = *c;
2887                 } else {
2888                         sprintf(buf + n, "&#%d;", *c);
2889                         n += strlen(buf + n);
2890                 }
2891                 if (n > (sizeof(buf) - 10)) {   // ! extra space for &...
2892                         buf[n] = 0;
2893                         n = 0;
2894                         r += wfputs(buf, wp);
2895                 }
2896         }
2897         if (n > 0) {
2898                 buf[n] = 0;
2899                 r += wfputs(buf, wp);
2900         }
2901         wfflush(wp);
2902         return r;
2903 }
2904
2905 int tf_webWriteJS(webs_t wp, const char *s)
2906 {
2907         char buf[512];
2908         int n;
2909         int r;
2910
2911         n = 0;
2912         r = 0;
2913         for (; *s; s++) {
2914                 if (*s == '<') {
2915                         sprintf(buf + n, "&lt;");
2916                         n += 4;
2917                 } else if (*s == '>') {
2918                         sprintf(buf + n, "&gt;");
2919                         n += 4;
2920                 } else if ((*s != '"') && (*s != '\\') && (*s != '/')
2921                            && (*s != '*') && (*s != '\'') && (isprint(*s))) {
2922                         buf[n++] = *s;
2923                 } else {
2924                         sprintf(buf + n, "\\x%02x", *s);
2925                         n += 4;
2926                 }
2927                 if (n > (sizeof(buf) - 10)) {   // ! extra space for \xHH
2928                         buf[n] = 0;
2929                         n = 0;
2930                         r += wfputs(buf, wp);
2931                 }
2932         }
2933         if (n > 0) {
2934                 buf[n] = 0;
2935                 r += wfputs(buf, wp);
2936         }
2937         wfflush(wp);
2938         return r;
2939 }
2940
2941 #ifdef HAVE_UPNP
2942 // changed by steve
2943 // writes javascript-string safe text
2944
2945 // <% tf_upnp(); %>
2946 // returns all "forward_port#" nvram entries containing upnp port forwardings
2947 void ej_tf_upnp(webs_t wp, int argc, char_t ** argv)
2948 {
2949         int i;
2950         int len, pos, count;
2951         char *temp;
2952
2953         if (nvram_match("upnp_enable", "1")) {
2954                 for (i = 0; i < 50; i++) {
2955                         websWrite(wp, (i > 0) ? ",'" : "'");
2956
2957                         // fix: some entries are missing the desc. - this breaks the
2958                         // upnp.asp page, so we add ,*
2959                         temp = nvram_nget("forward_port%d", i);
2960                         count = 0;
2961                         len = strlen(temp);
2962
2963                         for (pos = len; pos != 0; pos--) {
2964                                 if (temp[pos] == ',')
2965                                         count++;
2966                         }
2967
2968                         tf_webWriteJS(wp, temp);
2969                         if (count == 2)
2970                                 websWrite(wp, ",*");
2971
2972                         websWrite(wp, "'");
2973                 }
2974         }
2975 }
2976
2977 // end changed by steve
2978 #endif
Note: See TracBrowser for help on using the browser.