Skip to content

Commit

Permalink
refactor network.primary_address(), abstracting applyNetTemplate() fu…
Browse files Browse the repository at this point in the history
…nctions into utils namespace
  • Loading branch information
altergui committed Sep 2, 2014
1 parent 058bea8 commit 5a970c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
17 changes: 9 additions & 8 deletions packages/lime-system/files/usr/lib/lua/lime/network.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ end
function network.primary_address(offset)
local offset = offset or 0
local pm = network.primary_mac()
local m4, m5, m6 = tonumber(pm[4], 16), tonumber(pm[5], 16), tonumber(pm[6], 16)
local n1, n2, n3 = utils.network_id()
local ipv4_template = utils.applyMacTemplate10(config.get("network", "main_ipv4_address"), pm)
local ipv6_template = utils.applyMacTemplate16(config.get("network", "main_ipv6_address"), pm)
local ipv4_template = config.get("network", "main_ipv4_address")
local ipv6_template = config.get("network", "main_ipv6_address")

ipv4_template = utils.applyMacTemplate10(ipv4_template, pm)
ipv6_template = utils.applyMacTemplate16(ipv6_template, pm)

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

hexsuffix = hex((m4 * 256*256 + m5 * 256 + m6) + offset)
local m4, m5, m6 = tonumber(pm[4], 16), tonumber(pm[5], 16), tonumber(pm[6], 16)
local hexsuffix = utils.hex((m4 * 256*256 + m5 * 256 + m6) + offset)
return network.generate_host(ip.IPv4(ipv4_template), hexsuffix),
network.generate_host(ip.IPv6(ipv6_template), hexsuffix)
end
Expand Down
22 changes: 17 additions & 5 deletions packages/lime-system/files/usr/lib/lua/lime/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,28 @@ end

function utils.network_id()
local network_essid = config.get("wifi", "ap_ssid")
local n1, n2, n3
local netid = {}
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)
netid[1] = md5:match("^(..)")
netid[2] = md5:match("^..(..)")
netid[3] = md5:match("^....(..)")
fd:close()
end
return n1, n2, n3
return netid
end

function utils.applyNetTemplate16(template)
local netid = utils.network_id()
for i=1,3,1 do template = template:gsub("%%N"..i, netid[i]) end
return template
end

function utils.applyNetTemplate10(template)
local netid = utils.network_id()
for i=1,3,1 do template = template:gsub("%%N"..i, tonumber(netid[i], 16)) end
return template
end

return utils

0 comments on commit 5a970c5

Please sign in to comment.