Skip to content

Commit

Permalink
bring back functions generate_host(), generate_address() and helpers,…
Browse files Browse the repository at this point in the history
… killed by Gio with 'Human understandable address generation' and 'Begin code razionalization and cleaning'
  • Loading branch information
altergui committed Sep 2, 2014
1 parent 213efd0 commit 237ec97
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/lime-system/files/usr/lib/lua/lime/network.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

network = {}

local bit = require("bit")
local ip = require("luci.ip")
local libuci = require("uci")
local fs = require("nixio.fs")
Expand Down Expand Up @@ -34,6 +35,43 @@ function network.primary_address()
return ip.IPv4(ipv4_template), ip.IPv6(ipv6_template)
end

function network.generate_host(ipprefix, hexsuffix)
-- use only the 8 rightmost nibbles for IPv4, or 32 nibbles for IPv6
hexsuffix = hexsuffix:sub((ipprefix[1] == 4) and -8 or -32)

-- convert hexsuffix into a cidr instance, using same prefix and family of ipprefix
local ipsuffix = ip.Hex(hexsuffix, ipprefix:prefix(), ipprefix[1])

local ipaddress = ipprefix
-- if it's a network prefix, fill in host bits with ipsuffix
if ipprefix:equal(ipprefix:network()) then
for i in ipairs(ipprefix[2]) do
-- reset ipsuffix netmask bits to 0
ipsuffix[2][i] = bit.bxor(ipsuffix[2][i],ipsuffix:network()[2][i])
-- fill in ipaddress host part, with ipsuffix bits
ipaddress[2][i] = bit.bor(ipaddress[2][i],ipsuffix[2][i])
end
end

return ipaddress
end

function network.generate_address(n)
local id = n or 0
local m4, m5, m6 = utils.node_id()
local n1, n2, n3 = utils.network_id()
local ipv4_template = config.get("network", "main_ipv4_address")
local ipv6_template = config.get("network", "main_ipv6_address")

local hex = utils.hex
ipv6_template = ipv6_template:gsub("N1", hex(n1)):gsub("N2", hex(n2)):gsub("N3", hex(n3))
ipv4_template = ipv4_template:gsub("N1", n1):gsub("N2", n2):gsub("N3", n3)

hexsuffix = hex((m4 * 256*256 + m5 * 256 + m6) + id)
return network.generate_host(ip.IPv4(ipv4_template), hexsuffix),
network.generate_host(ip.IPv6(ipv6_template), hexsuffix)
end

function network.setup_rp_filter()
local sysctl_file_path = "/etc/sysctl.conf";
local sysctl_options = "";
Expand Down
21 changes: 21 additions & 0 deletions packages/lime-system/files/usr/lib/lua/lime/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

utils = {}

local config = require("lime.config")

function utils.split(string, sep)
local ret = {}
for token in string.gmatch(string, "[^"..sep.."]+") do table.insert(ret, token) end
Expand Down Expand Up @@ -41,4 +43,23 @@ function utils.applyMacTemplate10(template, mac)
return template
end

function utils.node_id()
local m = network.primary_mac()
return tonumber(m[4], 16), tonumber(m[5], 16), tonumber(m[6], 16)
end

function utils.network_id()
local network_essid = config.get("wifi", "ap_ssid")
local n1, n2, n3
local fd = io.popen('echo "' .. network_essid .. '" | md5sum')
if fd then
local md5 = fd:read("*a")
n1 = tonumber(md5:match("^(..)"), 16)
n2 = tonumber(md5:match("^..(..)"), 16)
n3 = tonumber(md5:match("^....(..)"), 16)
fd:close()
end
return n1, n2, n3
end

return utils

0 comments on commit 237ec97

Please sign in to comment.