8 var callLuciDHCPLeases
= rpc
.declare({
10 method
: 'getDHCPLeases',
14 return baseclass
.extend({
23 network
.getHostHints(),
24 L
.resolveDefault(uci
.load('dhcp'))
28 handleCreateStaticLease: function(lease
, ev
) {
29 ev
.currentTarget
.classList
.add('spinning');
30 ev
.currentTarget
.disabled
= true;
31 ev
.currentTarget
.blur();
33 var cfg
= uci
.add('dhcp', 'host');
34 uci
.set('dhcp', cfg
, 'name', lease
.hostname
);
35 uci
.set('dhcp', cfg
, 'ip', lease
.ipaddr
);
36 uci
.set('dhcp', cfg
, 'mac', lease
.macaddr
.toUpperCase());
39 .then(L
.bind(L
.ui
.changes
.init
, L
.ui
.changes
))
40 .then(L
.bind(L
.ui
.changes
.displayChanges
, L
.ui
.changes
));
43 handleCreateStaticLease6: function(lease
, ev
) {
44 ev
.currentTarget
.classList
.add('spinning');
45 ev
.currentTarget
.disabled
= true;
46 ev
.currentTarget
.blur();
48 var cfg
= uci
.add('dhcp', 'host'),
49 ip6arr
= lease
.ip6addrs
[0] ? validation
.parseIPv6(lease
.ip6addrs
[0]) : null;
51 uci
.set('dhcp', cfg
, 'name', lease
.hostname
);
52 uci
.set('dhcp', cfg
, 'duid', lease
.duid
.toUpperCase());
53 uci
.set('dhcp', cfg
, 'mac', lease
.macaddr
);
55 uci
.set('dhcp', cfg
, 'hostid', (ip6arr
[6] * 0xFFFF + ip6arr
[7]).toString(16));
58 .then(L
.bind(L
.ui
.changes
.init
, L
.ui
.changes
))
59 .then(L
.bind(L
.ui
.changes
.displayChanges
, L
.ui
.changes
));
62 renderLeases: function(data
) {
63 var leases
= Array
.isArray(data
[0].dhcp_leases
) ? data
[0].dhcp_leases
: [],
64 leases6
= Array
.isArray(data
[0].dhcp6_leases
) ? data
[0].dhcp6_leases
: [],
65 machints
= data
[1].getMACHints(false),
66 hosts
= uci
.sections('dhcp', 'host'),
67 isReadonlyView
= !L
.hasViewPermission();
69 for (var i
= 0; i
< hosts
.length
; i
++) {
73 var macs
= L
.toArray(host
.mac
);
74 for (var j
= 0; j
< macs
.length
; j
++) {
75 var mac
= macs
[j
].toUpperCase();
76 this.isMACStatic
[mac
] = true;
80 var duid
= host
.duid
.toUpperCase();
81 this.isDUIDStatic
[duid
] = true;
85 var table
= E('table', { 'id': 'status_leases', 'class': 'table lases' }, [
86 E('tr', { 'class': 'tr table-titles' }, [
87 E('th', { 'class': 'th' }, _('Hostname')),
88 E('th', { 'class': 'th' }, _('IPv4 address')),
89 E('th', { 'class': 'th' }, _('MAC address')),
90 E('th', { 'class': 'th' }, _('Lease time remaining')),
91 isReadonlyView
? E([]) : E('th', { 'class': 'th cbi-section-actions' }, _('Static Lease'))
95 cbi_update_table(table
, leases
.map(L
.bind(function(lease
) {
98 if (lease
.expires
=== false)
99 exp
= E('em', _('unlimited'));
100 else if (lease
.expires
<= 0)
101 exp
= E('em', _('expired'));
103 exp
= '%t'.format(lease
.expires
);
105 var hint
= lease
.macaddr
? machints
.filter(function(h
) { return h
[0] == lease
.macaddr
})[0] : null,
108 if (hint
&& lease
.hostname
&& lease
.hostname
!= hint
[1])
109 host
= '%s (%s)'.format(lease
.hostname
, hint
[1]);
110 else if (lease
.hostname
)
111 host
= lease
.hostname
;
120 if (!isReadonlyView
&& lease
.macaddr
!= null) {
121 var mac
= lease
.macaddr
.toUpperCase();
122 rows
.push(E('button', {
123 'class': 'cbi-button cbi-button-apply',
124 'click': L
.bind(this.handleCreateStaticLease
, this, lease
),
125 'disabled': this.isMACStatic
[mac
]
126 }, [ _('Set Static') ]));
130 }, this)), E('em', _('There are no active leases')));
132 var table6
= E('table', { 'id': 'status_leases6', 'class': 'table leases6' }, [
133 E('tr', { 'class': 'tr table-titles' }, [
134 E('th', { 'class': 'th' }, _('Host')),
135 E('th', { 'class': 'th' }, _('IPv6 address')),
136 E('th', { 'class': 'th' }, _('DUID')),
137 E('th', { 'class': 'th' }, _('Lease time remaining')),
138 isReadonlyView
? E([]) : E('th', { 'class': 'th cbi-section-actions' }, _('Static Lease'))
142 cbi_update_table(table6
, leases6
.map(L
.bind(function(lease
) {
145 if (lease
.expires
=== false)
146 exp
= E('em', _('unlimited'));
147 else if (lease
.expires
<= 0)
148 exp
= E('em', _('expired'));
150 exp
= '%t'.format(lease
.expires
);
152 var hint
= lease
.macaddr
? machints
.filter(function(h
) { return h
[0] == lease
.macaddr
})[0] : null,
155 if (hint
&& lease
.hostname
&& lease
.hostname
!= hint
[1] && lease
.ip6addr
!= hint
[1])
156 host
= '%s (%s)'.format(lease
.hostname
, hint
[1]);
157 else if (lease
.hostname
)
158 host
= lease
.hostname
;
164 lease
.ip6addrs
? lease
.ip6addrs
.join(' ') : lease
.ip6addr
,
169 if (!isReadonlyView
&& lease
.duid
!= null) {
170 var duid
= lease
.duid
.toUpperCase();
171 rows
.push(E('button', {
172 'class': 'cbi-button cbi-button-apply',
173 'click': L
.bind(this.handleCreateStaticLease6
, this, lease
),
174 'disabled': this.isDUIDStatic
[duid
]
175 }, [ _('Set Static') ]));
179 }, this)), E('em', _('There are no active leases')));
182 E('h3', _('Active DHCP Leases')),
184 E('h3', _('Active DHCPv6 Leases')),
189 render: function(data
) {
190 if (L
.hasSystemFeature('dnsmasq') || L
.hasSystemFeature('odhcpd'))
191 return this.renderLeases(data
);