<%#
LuCI - Lua Configuration Interface
Copyright 2008-2009 Steven Barth <steven@midlink.org>
Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$

-%>

<%-
	local fs = require "nixio.fs"
	local bit = require "nixio".bit
	local sys = require "luci.sys"
	local uci = require "luci.model.uci"
	local inst = uci.inst
	local state = uci.inst_state
	local http = require "luci.http"
	local style = true


	local ifc = {__index = function(self, key)
		local net = key
		state:foreach("network", "interface", function(s)
			if s.ifname == key then
				net = s[".name"]
			end
		end)
		rawset(self, key, net) 
		return net
	end}
	setmetatable(ifc, ifc)
	
	if http.formvalue("toggle_rtable") then
		local cursor = uci.cursor()
		local rt = cursor:get("network", "lan", "_showrtable") or "1"
		cursor:set("network", "lan", "_showrtable", rt == "1" and "0" or "1")
		cursor:save("network")
		cursor:unload("network")
		inst:unload("network")
	end
-%>

<div><a href="?toggle_rtable=1"> &gt; <%:Toggle display of Routing Information%> &lt; </a></div>
<br />

<% if inst:get("network", "lan", "_showrtable") ~= "0" then %>
<div class="cbi-map" id="x-cbi-network">

	<fieldset class="cbi-section" id="x-cbi-table-table">
		<legend><%_Active <abbr title="Internet Protocol Version 4">IPv4</abbr>-Routes%></legend>

		<div class="cbi-section-node">
			<table class="cbi-section-table">
				<tr class="cbi-section-table-titles">
					<th class="cbi-section-table-cell"><%:Network%></th>
					<th class="cbi-section-table-cell"><%:Target%></th>
					<th class="cbi-section-table-cell"><%_<abbr title="Internet Protocol Version 4">IPv4</abbr>-Netmask%></th>
					<th class="cbi-section-table-cell"><%_<abbr title="Internet Protocol Version 4">IPv4</abbr>-Gateway%></th>
					<th class="cbi-section-table-cell"><%:Metric%></th>
				</tr>
				<% luci.sys.net.routes(function(rt) %>
				<tr class="cbi-section-table-row cbi-rowstyle-<%=(style and 1 or 2)%>">
					<td class="cbi-value-field"><%=ifc[rt.device]%></td>
					<td class="cbi-value-field"><%=rt.dest:network():string()%></td>
					<td class="cbi-value-field"><%=rt.dest:mask():string()%></td>
					<td class="cbi-value-field"><%=rt.gateway:string()%></td>
					<td class="cbi-value-field"><%=rt.metric%></td>
				</tr>
				<% style = not style; end) %>
			</table>
		</div>
	</fieldset>
	<br />

	<% if fs.access("/proc/net/ipv6_route") then style = true %>
	<fieldset class="cbi-section" id="x-cbi-table-table-2">
		<legend><%_Active <abbr title="Internet Protocol Version 6">IPv6</abbr>-Routes%></legend>

		<div class="cbi-section-node">
			<table class="cbi-section-table">
				<tr class="cbi-section-table-titles">
					<th class="cbi-section-table-cell"><%:Network%></th>
					<th class="cbi-section-table-cell"><%:Target%></th>
					<th class="cbi-section-table-cell"><%_<abbr title="Internet Protocol Version 6">IPv6</abbr>-Gateway%></th>
					<th class="cbi-section-table-cell"><%:Metric%></th>
				</tr>
				<% luci.sys.net.routes6(function(rt) %>
				<tr class="cbi-section-table-row cbi-rowstyle-<%=(style and 1 or 2)%>">
					<td class="cbi-value-field"><%=ifc[rt.device]%></td>
					<td class="cbi-value-field"><%=rt.dest:string()%></td>
					<td class="cbi-value-field"><%=rt.source:string()%></td>
					<td class="cbi-value-field"><%- 
						local metr   = rt.metric
						local lower  = bit.band(metr, 0xffff)
						local higher = bit.rshift(bit.band(metr, 0xffff0000), 16)
						write(string.format("%04X%04X", higher, lower))
					-%></td>
				</tr>
				<% style = not style; end) %>
			</table>
		</div>
	</fieldset>
	<br />
	<% end %>
</div>
<% end %>
