2023-09-13 18:49:57 +02:00
|
|
|
(local { : system } (require :anoia))
|
2023-07-08 23:15:56 +02:00
|
|
|
(local svc (require :anoia.svc))
|
2023-07-02 01:52:18 +02:00
|
|
|
|
2023-09-13 18:49:57 +02:00
|
|
|
(fn changes [old-addresses new-addresses]
|
2023-07-02 01:52:18 +02:00
|
|
|
(let [added {}
|
2023-09-13 18:49:57 +02:00
|
|
|
deleted {}]
|
|
|
|
(each [n address (pairs new-addresses)]
|
|
|
|
(if (not (. old-addresses n))
|
|
|
|
(table.insert added address)))
|
|
|
|
(each [n address (pairs old-addresses)]
|
|
|
|
(if (not (. new-addresses n))
|
|
|
|
(table.insert deleted address)))
|
2023-07-02 01:52:18 +02:00
|
|
|
(values added deleted)))
|
2023-06-01 00:29:05 +02:00
|
|
|
|
2023-09-13 18:49:57 +02:00
|
|
|
(fn update-prefixes [device prefixes new-prefixes]
|
|
|
|
(let [(added deleted) (changes prefixes new-prefixes)]
|
2024-02-13 13:57:41 +01:00
|
|
|
;; if some address has changed (e.g. preferred/valid lifetime)
|
|
|
|
;; then we don't want to delete it before re-adding it because
|
|
|
|
;; the kernel will drop any routes that go through it. On the
|
|
|
|
;; other hand, we can't add it _before_ deleting it as we'll
|
|
|
|
;; get an error that it already exists. Therefore, use "change"
|
|
|
|
;; instead of "add", it works a bit more like an upsert
|
2023-09-13 18:49:57 +02:00
|
|
|
(each [_ p (ipairs added)]
|
|
|
|
(system
|
2024-02-13 13:57:41 +01:00
|
|
|
(.. "ip address change " p.address "1/" p.len " dev " device
|
|
|
|
" valid_lft " p.valid
|
|
|
|
" preferred_lft " p.preferred
|
|
|
|
)))
|
2023-09-13 18:49:57 +02:00
|
|
|
(each [_ p (ipairs deleted)]
|
|
|
|
(system
|
|
|
|
(.. "ip address del " p.address "1/" p.len " dev " device)))))
|
|
|
|
|
|
|
|
(fn run []
|
|
|
|
(let [[state-directory lan-device] arg
|
|
|
|
dir (svc.open state-directory)]
|
|
|
|
(accumulate [addresses []
|
|
|
|
v (dir:events)]
|
|
|
|
(update-prefixes lan-device addresses (v:output "prefix")))))
|
|
|
|
|
|
|
|
{ : changes : run }
|