luci-mod-network: improve static DHCP lease validation
[project/luci.git] / modules / luci-mod-network / htdocs / luci-static / resources / view / network / dhcp.js
1 'use strict';
2 'require view';
3 'require dom';
4 'require poll';
5 'require rpc';
6 'require uci';
7 'require form';
8 'require network';
9 'require validation';
10
11 var callHostHints, callDUIDHints, callDHCPLeases, CBILeaseStatus, CBILease6Status;
12
13 callHostHints = rpc.declare({
14 object: 'luci-rpc',
15 method: 'getHostHints',
16 expect: { '': {} }
17 });
18
19 callDUIDHints = rpc.declare({
20 object: 'luci-rpc',
21 method: 'getDUIDHints',
22 expect: { '': {} }
23 });
24
25 callDHCPLeases = rpc.declare({
26 object: 'luci-rpc',
27 method: 'getDHCPLeases',
28 expect: { '': {} }
29 });
30
31 CBILeaseStatus = form.DummyValue.extend({
32 renderWidget: function(section_id, option_id, cfgvalue) {
33 return E([
34 E('h4', _('Active DHCP Leases')),
35 E('table', { 'id': 'lease_status_table', 'class': 'table' }, [
36 E('tr', { 'class': 'tr table-titles' }, [
37 E('th', { 'class': 'th' }, _('Hostname')),
38 E('th', { 'class': 'th' }, _('IPv4 address')),
39 E('th', { 'class': 'th' }, _('MAC address')),
40 E('th', { 'class': 'th' }, _('Lease time remaining'))
41 ]),
42 E('tr', { 'class': 'tr placeholder' }, [
43 E('td', { 'class': 'td' }, E('em', _('Collecting data...')))
44 ])
45 ])
46 ]);
47 }
48 });
49
50 CBILease6Status = form.DummyValue.extend({
51 renderWidget: function(section_id, option_id, cfgvalue) {
52 return E([
53 E('h4', _('Active DHCPv6 Leases')),
54 E('table', { 'id': 'lease6_status_table', 'class': 'table' }, [
55 E('tr', { 'class': 'tr table-titles' }, [
56 E('th', { 'class': 'th' }, _('Host')),
57 E('th', { 'class': 'th' }, _('IPv6 address')),
58 E('th', { 'class': 'th' }, _('DUID')),
59 E('th', { 'class': 'th' }, _('Lease time remaining'))
60 ]),
61 E('tr', { 'class': 'tr placeholder' }, [
62 E('td', { 'class': 'td' }, E('em', _('Collecting data...')))
63 ])
64 ])
65 ]);
66 }
67 });
68
69 function calculateNetwork(addr, mask) {
70 addr = validation.parseIPv4(addr);
71
72 if (!isNaN(mask))
73 mask = validation.parseIPv4(network.prefixToMask(+mask));
74 else
75 mask = validation.parseIPv4(mask);
76
77 if (addr == null || mask == null)
78 return null;
79
80 return [
81 [
82 addr[0] & (mask[0] >>> 0 & 255),
83 addr[1] & (mask[1] >>> 0 & 255),
84 addr[2] & (mask[2] >>> 0 & 255),
85 addr[3] & (mask[3] >>> 0 & 255)
86 ].join('.'),
87 mask.join('.')
88 ];
89 }
90
91 function getDHCPPools() {
92 return uci.load('dhcp').then(function() {
93 let sections = uci.sections('dhcp', 'dhcp'),
94 tasks = [], pools = [];
95
96 for (var i = 0; i < sections.length; i++) {
97 if (sections[i].ignore == '1' || !sections[i].interface)
98 continue;
99
100 tasks.push(network.getNetwork(sections[i].interface).then(L.bind(function(section_id, net) {
101 var cidr = (net.getIPAddrs()[0] || '').split('/');
102
103 if (cidr.length == 2) {
104 var net_mask = calculateNetwork(cidr[0], cidr[1]);
105
106 pools.push({
107 section_id: section_id,
108 network: net_mask[0],
109 netmask: net_mask[1]
110 });
111 }
112 }, null, sections[i]['.name'])));
113 }
114
115 return Promise.all(tasks).then(function() {
116 return pools;
117 });
118 });
119 }
120
121 function validateHostname(sid, s) {
122 if (s == null || s == '')
123 return true;
124
125 if (s.length > 256)
126 return _('Expecting: %s').format(_('valid hostname'));
127
128 var labels = s.replace(/^\.+|\.$/g, '').split(/\./);
129
130 for (var i = 0; i < labels.length; i++)
131 if (!labels[i].match(/^[a-z0-9_](?:[a-z0-9-]{0,61}[a-z0-9])?$/i))
132 return _('Expecting: %s').format(_('valid hostname'));
133
134 return true;
135 }
136
137 function validateAddressList(sid, s) {
138 if (s == null || s == '')
139 return true;
140
141 var m = s.match(/^\/(.+)\/$/),
142 names = m ? m[1].split(/\//) : [ s ];
143
144 for (var i = 0; i < names.length; i++) {
145 var res = validateHostname(sid, names[i]);
146
147 if (res !== true)
148 return res;
149 }
150
151 return true;
152 }
153
154 function validateServerSpec(sid, s) {
155 if (s == null || s == '')
156 return true;
157
158 var m = s.match(/^(?:\/(.+)\/)?(.*)$/);
159 if (!m)
160 return _('Expecting: %s').format(_('valid hostname'));
161
162 var res = validateAddressList(sid, m[1]);
163 if (res !== true)
164 return res;
165
166 if (m[2] == '' || m[2] == '#')
167 return true;
168
169 // ipaddr%scopeid#srvport@source@interface#srcport
170
171 m = m[2].match(/^([0-9a-f:.]+)(?:%[^#@]+)?(?:#(\d+))?(?:@([0-9a-f:.]+)(?:@[^#]+)?(?:#(\d+))?)?$/);
172
173 if (!m)
174 return _('Expecting: %s').format(_('valid IP address'));
175
176 if (validation.parseIPv4(m[1])) {
177 if (m[3] != null && !validation.parseIPv4(m[3]))
178 return _('Expecting: %s').format(_('valid IPv4 address'));
179 }
180 else if (validation.parseIPv6(m[1])) {
181 if (m[3] != null && !validation.parseIPv6(m[3]))
182 return _('Expecting: %s').format(_('valid IPv6 address'));
183 }
184 else {
185 return _('Expecting: %s').format(_('valid IP address'));
186 }
187
188 if ((m[2] != null && +m[2] > 65535) || (m[4] != null && +m[4] > 65535))
189 return _('Expecting: %s').format(_('valid port value'));
190
191 return true;
192 }
193
194 function validateMACAddr(pools, sid, s) {
195 if (s == null || s == '')
196 return true;
197
198 var leases = uci.sections('dhcp', 'host'),
199 this_macs = L.toArray(s).map(function(m) { return m.toUpperCase() });
200
201 for (var i = 0; i < pools.length; i++) {
202 var this_net_mask = calculateNetwork(uci.get('dhcp', sid, 'ip'), pools[i].netmask);
203
204 if (!this_net_mask)
205 continue;
206
207 for (var j = 0; j < leases.length; j++) {
208 if (leases[j]['.name'] == sid || !leases[j].ip)
209 continue;
210
211 var lease_net_mask = calculateNetwork(leases[j].ip, pools[i].netmask);
212
213 if (!lease_net_mask || this_net_mask[0] != lease_net_mask[0])
214 continue;
215
216 var lease_macs = L.toArray(leases[j].mac).map(function(m) { return m.toUpperCase() });
217
218 for (var k = 0; k < lease_macs.length; k++)
219 for (var l = 0; l < this_macs.length; l++)
220 if (lease_macs[k] == this_macs[l])
221 return _('The MAC address %h is already used by another static lease in the same DHCP pool').format(this_macs[l]);
222 }
223 }
224
225 return true;
226 }
227
228 return view.extend({
229 load: function() {
230 return Promise.all([
231 callHostHints(),
232 callDUIDHints(),
233 getDHCPPools()
234 ]);
235 },
236
237 render: function(hosts_duids_pools) {
238 var has_dhcpv6 = L.hasSystemFeature('dnsmasq', 'dhcpv6') || L.hasSystemFeature('odhcpd'),
239 hosts = hosts_duids_pools[0],
240 duids = hosts_duids_pools[1],
241 pools = hosts_duids_pools[2],
242 m, s, o, ss, so;
243
244 console.debug(pools);
245
246 m = new form.Map('dhcp', _('DHCP and DNS'), _('Dnsmasq is a combined <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr>-Server and <abbr title="Domain Name System">DNS</abbr>-Forwarder for <abbr title="Network Address Translation">NAT</abbr> firewalls'));
247
248 s = m.section(form.TypedSection, 'dnsmasq', _('Server Settings'));
249 s.anonymous = true;
250 s.addremove = false;
251
252 s.tab('general', _('General Settings'));
253 s.tab('files', _('Resolv and Hosts Files'));
254 s.tab('tftp', _('TFTP Settings'));
255 s.tab('advanced', _('Advanced Settings'));
256 s.tab('leases', _('Static Leases'));
257
258 s.taboption('general', form.Flag, 'domainneeded',
259 _('Domain required'),
260 _('Don\'t forward <abbr title="Domain Name System">DNS</abbr>-Requests without <abbr title="Domain Name System">DNS</abbr>-Name'));
261
262 s.taboption('general', form.Flag, 'authoritative',
263 _('Authoritative'),
264 _('This is the only <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr> in the local network'));
265
266
267 s.taboption('files', form.Flag, 'readethers',
268 _('Use <code>/etc/ethers</code>'),
269 _('Read <code>/etc/ethers</code> to configure the <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr>-Server'));
270
271 s.taboption('files', form.Value, 'leasefile',
272 _('Leasefile'),
273 _('file where given <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr>-leases will be stored'));
274
275 s.taboption('files', form.Flag, 'noresolv',
276 _('Ignore resolve file')).optional = true;
277
278 o = s.taboption('files', form.Value, 'resolvfile',
279 _('Resolve file'),
280 _('local <abbr title="Domain Name System">DNS</abbr> file'));
281
282 o.depends('noresolv', '0');
283 o.placeholder = '/tmp/resolv.conf.d/resolv.conf.auto';
284 o.optional = true;
285
286
287 s.taboption('files', form.Flag, 'nohosts',
288 _('Ignore <code>/etc/hosts</code>')).optional = true;
289
290 s.taboption('files', form.DynamicList, 'addnhosts',
291 _('Additional Hosts files')).optional = true;
292
293 o = s.taboption('advanced', form.Flag, 'quietdhcp',
294 _('Suppress logging'),
295 _('Suppress logging of the routine operation of these protocols'));
296 o.optional = true;
297
298 o = s.taboption('advanced', form.Flag, 'sequential_ip',
299 _('Allocate IP sequentially'),
300 _('Allocate IP addresses sequentially, starting from the lowest available address'));
301 o.optional = true;
302
303 o = s.taboption('advanced', form.Flag, 'boguspriv',
304 _('Filter private'),
305 _('Do not forward reverse lookups for local networks'));
306 o.default = o.enabled;
307
308 s.taboption('advanced', form.Flag, 'filterwin2k',
309 _('Filter useless'),
310 _('Do not forward requests that cannot be answered by public name servers'));
311
312
313 s.taboption('advanced', form.Flag, 'localise_queries',
314 _('Localise queries'),
315 _('Localise hostname depending on the requesting subnet if multiple IPs are available'));
316
317 if (L.hasSystemFeature('dnsmasq', 'dnssec')) {
318 o = s.taboption('advanced', form.Flag, 'dnssec',
319 _('DNSSEC'));
320 o.optional = true;
321
322 o = s.taboption('advanced', form.Flag, 'dnsseccheckunsigned',
323 _('DNSSEC check unsigned'),
324 _('Requires upstream supports DNSSEC; verify unsigned domain responses really come from unsigned domains'));
325 o.default = o.enabled;
326 o.optional = true;
327 }
328
329 s.taboption('general', form.Value, 'local',
330 _('Local server'),
331 _('Local domain specification. Names matching this domain are never forwarded and are resolved from DHCP or hosts files only'));
332
333 s.taboption('general', form.Value, 'domain',
334 _('Local domain'),
335 _('Local domain suffix appended to DHCP names and hosts file entries'));
336
337 s.taboption('advanced', form.Flag, 'expandhosts',
338 _('Expand hosts'),
339 _('Add local domain suffix to names served from hosts files'));
340
341 s.taboption('advanced', form.Flag, 'nonegcache',
342 _('No negative cache'),
343 _('Do not cache negative replies, e.g. for not existing domains'));
344
345 s.taboption('advanced', form.Value, 'serversfile',
346 _('Additional servers file'),
347 _('This file may contain lines like \'server=/domain/1.2.3.4\' or \'server=1.2.3.4\' for domain-specific or full upstream <abbr title="Domain Name System">DNS</abbr> servers.'));
348
349 s.taboption('advanced', form.Flag, 'strictorder',
350 _('Strict order'),
351 _('<abbr title="Domain Name System">DNS</abbr> servers will be queried in the order of the resolvfile')).optional = true;
352
353 s.taboption('advanced', form.Flag, 'allservers',
354 _('All Servers'),
355 _('Query all available upstream <abbr title="Domain Name System">DNS</abbr> servers')).optional = true;
356
357 o = s.taboption('advanced', form.DynamicList, 'bogusnxdomain', _('Bogus NX Domain Override'),
358 _('List of hosts that supply bogus NX domain results'));
359
360 o.optional = true;
361 o.placeholder = '67.215.65.132';
362
363
364 s.taboption('general', form.Flag, 'logqueries',
365 _('Log queries'),
366 _('Write received DNS requests to syslog')).optional = true;
367
368 o = s.taboption('general', form.DynamicList, 'server', _('DNS forwardings'),
369 _('List of <abbr title="Domain Name System">DNS</abbr> servers to forward requests to'));
370
371 o.optional = true;
372 o.placeholder = '/example.org/10.1.2.3';
373 o.validate = validateServerSpec;
374
375
376 o = s.taboption('general', form.DynamicList, 'address', _('Addresses'),
377 _('List of domains to force to an IP address.'));
378
379 o.optional = true;
380 o.placeholder = '/router.local/192.168.0.1';
381
382
383 o = s.taboption('general', form.Flag, 'rebind_protection',
384 _('Rebind protection'),
385 _('Discard upstream RFC1918 responses'));
386
387 o.rmempty = false;
388
389
390 o = s.taboption('general', form.Flag, 'rebind_localhost',
391 _('Allow localhost'),
392 _('Allow upstream responses in the 127.0.0.0/8 range, e.g. for RBL services'));
393
394 o.depends('rebind_protection', '1');
395
396
397 o = s.taboption('general', form.DynamicList, 'rebind_domain',
398 _('Domain whitelist'),
399 _('List of domains to allow RFC1918 responses for'));
400 o.optional = true;
401
402 o.depends('rebind_protection', '1');
403 o.placeholder = 'ihost.netflix.com';
404 o.validate = validateAddressList;
405
406
407 o = s.taboption('advanced', form.Value, 'port',
408 _('<abbr title="Domain Name System">DNS</abbr> server port'),
409 _('Listening port for inbound DNS queries'));
410
411 o.optional = true;
412 o.datatype = 'port';
413 o.placeholder = 53;
414
415
416 o = s.taboption('advanced', form.Value, 'queryport',
417 _('<abbr title="Domain Name System">DNS</abbr> query port'),
418 _('Fixed source port for outbound DNS queries'));
419
420 o.optional = true;
421 o.datatype = 'port';
422 o.placeholder = _('any');
423
424
425 o = s.taboption('advanced', form.Value, 'dhcpleasemax',
426 _('<abbr title="maximal">Max.</abbr> <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr> leases'),
427 _('Maximum allowed number of active DHCP leases'));
428
429 o.optional = true;
430 o.datatype = 'uinteger';
431 o.placeholder = _('unlimited');
432
433
434 o = s.taboption('advanced', form.Value, 'ednspacket_max',
435 _('<abbr title="maximal">Max.</abbr> <abbr title="Extension Mechanisms for Domain Name System">EDNS0</abbr> packet size'),
436 _('Maximum allowed size of EDNS.0 UDP packets'));
437
438 o.optional = true;
439 o.datatype = 'uinteger';
440 o.placeholder = 1280;
441
442
443 o = s.taboption('advanced', form.Value, 'dnsforwardmax',
444 _('<abbr title="maximal">Max.</abbr> concurrent queries'),
445 _('Maximum allowed number of concurrent DNS queries'));
446
447 o.optional = true;
448 o.datatype = 'uinteger';
449 o.placeholder = 150;
450
451 o = s.taboption('advanced', form.Value, 'cachesize',
452 _('Size of DNS query cache'),
453 _('Number of cached DNS entries (max is 10000, 0 is no caching)'));
454 o.optional = true;
455 o.datatype = 'range(0,10000)';
456 o.placeholder = 150;
457
458 s.taboption('tftp', form.Flag, 'enable_tftp',
459 _('Enable TFTP server')).optional = true;
460
461 o = s.taboption('tftp', form.Value, 'tftp_root',
462 _('TFTP server root'),
463 _('Root directory for files served via TFTP'));
464
465 o.optional = true;
466 o.depends('enable_tftp', '1');
467 o.placeholder = '/';
468
469
470 o = s.taboption('tftp', form.Value, 'dhcp_boot',
471 _('Network boot image'),
472 _('Filename of the boot image advertised to clients'));
473
474 o.optional = true;
475 o.depends('enable_tftp', '1');
476 o.placeholder = 'pxelinux.0';
477
478 o = s.taboption('general', form.Flag, 'localservice',
479 _('Local Service Only'),
480 _('Limit DNS service to subnets interfaces on which we are serving DNS.'));
481 o.optional = false;
482 o.rmempty = false;
483
484 o = s.taboption('general', form.Flag, 'nonwildcard',
485 _('Non-wildcard'),
486 _('Bind dynamically to interfaces rather than wildcard address (recommended as linux default)'));
487 o.default = o.enabled;
488 o.optional = false;
489 o.rmempty = true;
490
491 o = s.taboption('general', form.DynamicList, 'interface',
492 _('Listen Interfaces'),
493 _('Limit listening to these interfaces, and loopback.'));
494 o.optional = true;
495
496 o = s.taboption('general', form.DynamicList, 'notinterface',
497 _('Exclude interfaces'),
498 _('Prevent listening on these interfaces.'));
499 o.optional = true;
500
501 o = s.taboption('leases', form.SectionValue, '__leases__', form.GridSection, 'host', null,
502 _('Static leases are used to assign fixed IP addresses and symbolic hostnames to DHCP clients. They are also required for non-dynamic interface configurations where only hosts with a corresponding lease are served.') + '<br />' +
503 _('Use the <em>Add</em> Button to add a new lease entry. The <em>MAC address</em> identifies the host, the <em>IPv4 address</em> specifies the fixed address to use, and the <em>Hostname</em> is assigned as a symbolic name to the requesting host. The optional <em>Lease time</em> can be used to set non-standard host-specific lease time, e.g. 12h, 3d or infinite.'));
504
505 ss = o.subsection;
506
507 ss.addremove = true;
508 ss.anonymous = true;
509
510 so = ss.option(form.Value, 'name', _('Hostname'));
511 so.validate = validateHostname;
512 so.rmempty = true;
513 so.write = function(section, value) {
514 uci.set('dhcp', section, 'name', value);
515 uci.set('dhcp', section, 'dns', '1');
516 };
517 so.remove = function(section) {
518 uci.unset('dhcp', section, 'name');
519 uci.unset('dhcp', section, 'dns');
520 };
521
522 so = ss.option(form.Value, 'mac', _('<abbr title="Media Access Control">MAC</abbr>-Address'));
523 so.datatype = 'list(macaddr)';
524 so.rmempty = true;
525 so.cfgvalue = function(section) {
526 var macs = L.toArray(uci.get('dhcp', section, 'mac')),
527 result = [];
528
529 for (var i = 0, mac; (mac = macs[i]) != null; i++)
530 if (/^([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2})$/.test(mac))
531 result.push('%02X:%02X:%02X:%02X:%02X:%02X'.format(
532 parseInt(RegExp.$1, 16), parseInt(RegExp.$2, 16),
533 parseInt(RegExp.$3, 16), parseInt(RegExp.$4, 16),
534 parseInt(RegExp.$5, 16), parseInt(RegExp.$6, 16)));
535
536 return result.length ? result.join(' ') : null;
537 };
538 so.renderWidget = function(section_id, option_index, cfgvalue) {
539 var node = form.Value.prototype.renderWidget.apply(this, [section_id, option_index, cfgvalue]),
540 ipopt = this.section.children.filter(function(o) { return o.option == 'ip' })[0];
541
542 node.addEventListener('cbi-dropdown-change', L.bind(function(ipopt, section_id, ev) {
543 var mac = ev.detail.value.value;
544 if (mac == null || mac == '' || !hosts[mac])
545 return;
546
547 var iphint = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0];
548 if (iphint == null)
549 return;
550
551 var ip = ipopt.formvalue(section_id);
552 if (ip != null && ip != '')
553 return;
554
555 var node = ipopt.map.findElement('id', ipopt.cbid(section_id));
556 if (node)
557 dom.callClassMethod(node, 'setValue', iphint);
558 }, this, ipopt, section_id));
559
560 return node;
561 };
562 so.validate = validateMACAddr.bind(so, pools);
563 Object.keys(hosts).forEach(function(mac) {
564 var hint = hosts[mac].name || L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0];
565 so.value(mac, hint ? '%s (%s)'.format(mac, hint) : mac);
566 });
567
568 so = ss.option(form.Value, 'ip', _('<abbr title="Internet Protocol Version 4">IPv4</abbr>-Address'));
569 so.datatype = 'or(ip4addr,"ignore")';
570 so.validate = function(section, value) {
571 var mac = this.map.lookupOption('mac', section),
572 name = this.map.lookupOption('name', section),
573 m = mac ? mac[0].formvalue(section) : null,
574 n = name ? name[0].formvalue(section) : null;
575
576 if ((m == null || m == '') && (n == null || n == ''))
577 return _('One of hostname or mac address must be specified!');
578
579 if (value == null || value == '' || value == 'ignore')
580 return true;
581
582 var leases = uci.sections('dhcp', 'host');
583
584 for (var i = 0; i < leases.length; i++)
585 if (leases[i]['.name'] != section && leases[i].ip == value)
586 return _('The IP address %h is already used by another static lease').format(value);
587
588
589 for (var i = 0; i < pools.length; i++) {
590 var net_mask = calculateNetwork(value, pools[i].netmask);
591
592 if (net_mask && net_mask[0] == pools[i].network)
593 return true;
594 }
595
596 return _('The IP address is outside of any DHCP pool address range');
597 };
598
599 var ipaddrs = {};
600
601 Object.keys(hosts).forEach(function(mac) {
602 var addrs = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4);
603
604 for (var i = 0; i < addrs.length; i++)
605 ipaddrs[addrs[i]] = hosts[mac].name;
606 });
607
608 L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) {
609 so.value(ipv4, ipaddrs[ipv4] ? '%s (%s)'.format(ipv4, ipaddrs[ipv4]) : ipv4);
610 });
611
612 so = ss.option(form.Value, 'leasetime', _('Lease time'));
613 so.rmempty = true;
614
615 so = ss.option(form.Value, 'duid', _('<abbr title="The DHCP Unique Identifier">DUID</abbr>'));
616 so.datatype = 'and(rangelength(20,36),hexstring)';
617 Object.keys(duids).forEach(function(duid) {
618 so.value(duid, '%s (%s)'.format(duid, duids[duid].hostname || duids[duid].macaddr || duids[duid].ip6addr || '?'));
619 });
620
621 so = ss.option(form.Value, 'hostid', _('<abbr title="Internet Protocol Version 6">IPv6</abbr>-Suffix (hex)'));
622
623 o = s.taboption('leases', CBILeaseStatus, '__status__');
624
625 if (has_dhcpv6)
626 o = s.taboption('leases', CBILease6Status, '__status6__');
627
628 return m.render().then(function(mapEl) {
629 poll.add(function() {
630 return callDHCPLeases().then(function(leaseinfo) {
631 var leases = Array.isArray(leaseinfo.dhcp_leases) ? leaseinfo.dhcp_leases : [],
632 leases6 = Array.isArray(leaseinfo.dhcp6_leases) ? leaseinfo.dhcp6_leases : [];
633
634 cbi_update_table(mapEl.querySelector('#lease_status_table'),
635 leases.map(function(lease) {
636 var exp;
637
638 if (lease.expires === false)
639 exp = E('em', _('unlimited'));
640 else if (lease.expires <= 0)
641 exp = E('em', _('expired'));
642 else
643 exp = '%t'.format(lease.expires);
644
645 return [
646 lease.hostname || '?',
647 lease.ipaddr,
648 lease.macaddr,
649 exp
650 ];
651 }),
652 E('em', _('There are no active leases')));
653
654 if (has_dhcpv6) {
655 cbi_update_table(mapEl.querySelector('#lease6_status_table'),
656 leases6.map(function(lease) {
657 var exp;
658
659 if (lease.expires === false)
660 exp = E('em', _('unlimited'));
661 else if (lease.expires <= 0)
662 exp = E('em', _('expired'));
663 else
664 exp = '%t'.format(lease.expires);
665
666 var hint = lease.macaddr ? hosts[lease.macaddr] : null,
667 name = hint ? (hint.name || L.toArray(hint.ipaddrs || hint.ipv4)[0] || L.toArray(hint.ip6addrs || hint.ipv6)[0]) : null,
668 host = null;
669
670 if (name && lease.hostname && lease.hostname != name && lease.ip6addr != name)
671 host = '%s (%s)'.format(lease.hostname, name);
672 else if (lease.hostname)
673 host = lease.hostname;
674 else if (name)
675 host = name;
676
677 return [
678 host || '-',
679 lease.ip6addrs ? lease.ip6addrs.join(' ') : lease.ip6addr,
680 lease.duid,
681 exp
682 ];
683 }),
684 E('em', _('There are no active leases')));
685 }
686 });
687 });
688
689 return mapEl;
690 });
691 }
692 });