gluon-core: enhance mac_to_ip() beyond RFC 4291

mac_to_ip() calculates an ipv6 address from a mac address according to
RFC 4291. For wireguard we have to use specially crafted addresses that
must be unique. This allows calculating such unique mac-based addresses
by allowing to optionally specifying the bytes to be inserted into the
address.
This commit is contained in:
Christof Schulze 2019-08-23 14:35:24 +02:00
parent 087f14be80
commit 731facd200

View File

@ -74,10 +74,10 @@ function M.IPv6(address)
end
end
function M.mac_to_ip(prefix, mac)
function M.mac_to_ip(prefix, mac, firstbyte, secondbyte)
local m1, m2, m3, m6, m7, m8 = string.match(mac, '(%x%x):(%x%x):(%x%x):(%x%x):(%x%x):(%x%x)')
local m4 = 0xff
local m5 = 0xfe
local m4 = firstbyte or 0xff
local m5 = secondbyte or 0xfe
m1 = bit.bxor(tonumber(m1, 16), 0x02)
local h1 = 0x100 * m1 + tonumber(m2, 16)