gluon-core: enhance mac_to_ip() beyond RFC 4291 (#1798)
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:
		
							parent
							
								
									087f14be80
								
							
						
					
					
						commit
						bd543db86b
					
				| @ -74,10 +74,10 @@ function M.IPv6(address) | |||||||
| 	end | 	end | ||||||
| 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 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 m4 = firstbyte or 0xff | ||||||
| 	local m5 = 0xfe | 	local m5 = secondbyte or 0xfe | ||||||
| 	m1 = bit.bxor(tonumber(m1, 16), 0x02) | 	m1 = bit.bxor(tonumber(m1, 16), 0x02) | ||||||
| 
 | 
 | ||||||
| 	local h1 = 0x100 * m1 + tonumber(m2, 16) | 	local h1 = 0x100 * m1 + tonumber(m2, 16) | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user