Merge commit 'origin/tomato-RT' into tomato-shibby
[tomato.git] / release / src / router / www / basic-static.asp
blob735d33e4036a541112d9b1bd3702ca5271139c38
1 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0//EN'>
2 <!--
3 Tomato GUI
4 Copyright (C) 2006-2010 Jonathan Zarate
5 http://www.polarcloud.com/tomato/
7 For use with Tomato Firmware only.
8 No part of this file may be used without permission.
9 -->
10 <html>
11 <head>
12 <meta http-equiv='content-type' content='text/html;charset=utf-8'>
13 <meta name='robots' content='noindex,nofollow'>
14 <title>[<% ident(); %>] Basic: Static DHCP</title>
15 <link rel='stylesheet' type='text/css' href='tomato.css'>
16 <link rel='stylesheet' type='text/css' href='color.css'>
17 <script type='text/javascript' src='tomato.js'></script>
19 <!-- / / / -->
20 <style type='text/css'>
21 #bs-grid {
22 width: 600px;
24 #bs-grid .co1,
25 #bs-grid .co2 {
26 width: 130px;
28 #bs-grid .co3 {
29 width: 340px;
31 </style>
33 <script type='text/javascript' src='debug.js'></script>
35 <script type='text/javascript'>
37 // <% nvram("lan_ipaddr,lan_netmask,dhcpd_static,dhcpd_startip"); %>
39 if (nvram.lan_ipaddr.match(/^(\d+\.\d+\.\d+)\.(\d+)$/)) ipp = RegExp.$1 + '.';
40 else ipp = '?.?.?.';
42 autonum = aton(nvram.lan_ipaddr) & aton(nvram.lan_netmask);
44 var sg = new TomatoGrid();
46 sg.exist = function(f, v)
48 var data = this.getAllData();
49 for (var i = 0; i < data.length; ++i) {
50 if (data[i][f] == v) return true;
52 return false;
55 sg.existMAC = function(mac)
57 if (isMAC0(mac)) return false;
58 return this.exist(0, mac) || this.exist(1, mac);
61 sg.existName = function(name)
63 return this.exist(3, name);
66 sg.inStatic = function(n)
68 return this.exist(2, n);
71 sg.dataToView = function(data) {
72 var v = [];
74 var s = data[0];
75 if (!isMAC0(data[1])) s += '<br>' + data[1];
76 v.push(s);
78 for (var i = 2; i < data.length; ++i)
79 v.push(escapeHTML('' + data[i]));
81 return v;
84 sg.sortCompare = function(a, b) {
85 var da = a.getRowData();
86 var db = b.getRowData();
87 var r = 0;
88 switch (this.sortColumn) {
89 case 0:
90 r = cmpText(da[0], db[0]);
91 break;
92 case 1:
93 r = cmpIP(da[2], db[2]);
94 break;
96 if (r == 0) r = cmpText(da[3], db[3]);
97 return this.sortAscending ? r : -r;
100 sg.verifyFields = function(row, quiet)
102 var f, s, i;
104 f = fields.getAll(row);
106 if (!v_macz(f[0], quiet)) return 0;
107 if (!v_macz(f[1], quiet)) return 0;
108 if (isMAC0(f[0].value)) {
109 f[0].value = f[1].value;
110 f[1].value = '00:00:00:00:00:00';
112 else if (f[0].value == f[1].value) {
113 f[1].value = '00:00:00:00:00:00';
115 else if ((!isMAC0(f[1].value)) && (f[0].value > f[1].value)) {
116 s = f[1].value;
117 f[1].value = f[0].value;
118 f[0].value = s;
120 for (i = 0; i < 2; ++i) {
121 if (this.existMAC(f[i].value)) {
122 ferror.set(f[i], 'Duplicate MAC address', quiet);
123 return 0;
127 if (f[2].value.indexOf('.') == -1) {
128 s = parseInt(f[2].value, 10)
129 if (isNaN(s) || (s <= 0) || (s >= 255)) {
130 ferror.set(f[2], 'Invalid IP address', quiet);
131 return 0;
133 f[2].value = ipp + s;
136 if ((!isMAC0(f[0].value)) && (this.inStatic(f[2].value))) {
137 ferror.set(f[2], 'Duplicate IP address', quiet);
138 return 0;
141 if (!v_hostname(f[3], quiet, 5)) return 0;
142 if (!v_nodelim(f[3], quiet, 'Hostname', 1)) return 0;
143 s = f[3].value;
144 if (s.length > 0) {
145 if (this.existName(s)) {
146 ferror.set(f[3], 'Duplicate name.', quiet);
147 return 0;
151 if (isMAC0(f[0].value)) {
152 if (s == '') {
153 s = 'Both MAC address and name fields must not be empty.';
154 ferror.set(f[0], s, 1);
155 ferror.set(f[3], s, quiet);
156 return 0;
160 return 1;
163 sg.resetNewEditor = function() {
164 var f, c, n;
166 f = fields.getAll(this.newEditor);
167 ferror.clearAll(f);
169 if ((c = cookie.get('addstatic')) != null) {
170 cookie.set('addstatic', '', 0);
171 c = c.split(',');
172 if (c.length == 3) {
173 f[0].value = c[0];
174 f[1].value = '00:00:00:00:00:00';
175 f[2].value = c[1];
176 f[3].value = c[2];
177 return;
181 f[0].value = '00:00:00:00:00:00';
182 f[1].value = '00:00:00:00:00:00';
183 f[3].value = '';
185 n = 10;
186 do {
187 if (--n < 0) {
188 f[2].value = '';
189 return;
191 autonum++;
192 } while (((c = fixIP(ntoa(autonum), 1)) == null) || (c == nvram.lan_ipaddr) || (this.inStatic(c)));
194 f[2].value = c;
197 sg.setup = function()
199 this.init('bs-grid', 'sort', 140, [
200 { multi: [ { type: 'text', maxlen: 17 }, { type: 'text', maxlen: 17 } ] },
201 { type: 'text', maxlen: 15 },
202 { type: 'text', maxlen: 63 } ] );
204 this.headerSet(['MAC Address', 'IP Address', 'Hostname']);
205 var s = nvram.dhcpd_static.split('>');
206 for (var i = 0; i < s.length; ++i) {
207 var t = s[i].split('<');
208 if (t.length == 3) {
209 var d = t[0].split(',');
210 this.insertData(-1, [d[0], (d.length >= 2) ? d[1] : '00:00:00:00:00:00',
211 (t[1].indexOf('.') == -1) ? (ipp + t[1]) : t[1], t[2]]);
214 this.sort(2);
215 this.showNewEditor();
216 this.resetNewEditor();
219 function save()
221 if (sg.isEditing()) return;
223 var data = sg.getAllData();
224 var sdhcp = '';
225 var i;
227 for (i = 0; i < data.length; ++i) {
228 var d = data[i];
229 sdhcp += d[0];
230 if (!isMAC0(d[1])) sdhcp += ',' + d[1];
231 sdhcp += '<' + d[2] + '<' + d[3] + '>';
234 var fom = E('_fom');
235 fom.dhcpd_static.value = sdhcp;
236 form.submit(fom, 1);
239 function init()
241 sg.recolor();
243 </script>
244 </head>
245 <body onload='init()'>
246 <form id='_fom' method='post' action='tomato.cgi'>
247 <table id='container' cellspacing=0>
248 <tr><td colspan=2 id='header'>
249 <div class='title'>Tomato</div>
250 <div class='version'>Version <% version(); %></div>
251 </td></tr>
252 <tr id='body'><td id='navi'><script type='text/javascript'>navi()</script></td>
253 <td id='content'>
254 <div id='ident'><% ident(); %></div>
256 <!-- / / / -->
258 <input type='hidden' name='_nextpage' value='basic-static.asp'>
259 <input type='hidden' name='_service' value='dhcpd-restart'>
261 <input type='hidden' name='dhcpd_static'>
263 <div class='section-title'>Static DHCP</div>
264 <div class='section'>
265 <table class='tomato-grid' id='bs-grid'></table>
266 </div>
268 <div>
269 <small>To specify multiple hostnames per device, separate them with spaces.</small>
270 </div>
272 <!-- / / / -->
274 </td></tr>
275 <tr><td id='footer' colspan=2>
276 <span id='footer-msg'></span>
277 <input type='button' value='Save' id='save-button' onclick='save()'>
278 <input type='button' value='Cancel' id='cancel-button' onclick='javascript:reloadPage();'>
279 </td></tr>
280 </table>
281 </form>
282 <script type='text/javascript'>sg.setup();</script>
283 </body>
284 </html>