X-Git-Url: https://git.rvb.name/lua-squid-acl-helper.git/blobdiff_plain/2e41ad1dc4bac0a74a64a14fabec46bd0aeb7fae..974503f46c01638519f9af457e849d88712c28bd:/src/arpcache.lua diff --git a/src/arpcache.lua b/src/arpcache.lua index 1ae8136..b4b8478 100644 --- a/src/arpcache.lua +++ b/src/arpcache.lua @@ -4,19 +4,25 @@ config = require "config" _arpcache = {} _ARPCACHE4 = '/sbin/ip -4 n' -_ARPCACHE6 = '/sbin/ip -6 n' function _match_v4(ip) return string.match(ip,"%d*%.%d*%.%d*%.%d*") end +_arpcache.cache = {} + function _arpcache.get_mac(ip) + local rec = _arpcache.cache[ip] + if rec and rec.timestamp+config.arp_ttl > os.time() then + return rec["mac"] + end + local cmd if _match_v4(ip) then cmd = _ARPCACHE4 else - cmd = _ARPCACHE6 + return "OK" end local f = io.popen(cmd) @@ -38,11 +44,16 @@ function _arpcache.get_mac(ip) if w[1]==ip then res = w[5] + break end end + if res then + _arpcache.cache[ip] = { mac = res, timestamp = os.time() } + end + f:close() return res