acquire-wan-address uses parsed addresses from odhcp

This commit is contained in:
Daniel Barlow 2023-09-11 00:07:11 +01:00
parent 3ff55d3aad
commit 7683ed69de

View file

@ -1,26 +1,6 @@
(local { : merge : split : file-exists? : system } (require :anoia)) (local { : system } (require :anoia))
(local svc (require :anoia.svc)) (local svc (require :anoia.svc))
;; structurally this is remarkably similar to
;; acquire-lan-prefix.fnl. maybe they should be merged: if not then
;; we could at least extract some common code
;; (alternatively we could move all the parsing code into the thing in
;; the odhcp service that *writes* this stuff)
; (parse-address "2001:8b0:1111:1111:0:ffff:51bb:4cf2/128,3600,7200")
(fn parse-address [str]
(fn parse-extra [s]
(let [out {}]
(each [name val (string.gmatch s ",(.-)=([^,]+)")]
(tset out name val))
out))
(let [(address len preferred valid extra)
(string.match str "(.-)/(%d+),(%d+),(%d+)(.*)$")]
(merge {: address : len : preferred : valid} (parse-extra extra))))
(local bound-states (local bound-states
{ :bound true { :bound true
:rebound true :rebound true
@ -31,30 +11,37 @@
(fn changes [old-addresses new-addresses] (fn changes [old-addresses new-addresses]
(let [added {} (let [added {}
deleted {} deleted {}]
old-set (collect [_ v (ipairs old-addresses)] (values v true)) (each [n address (pairs new-addresses)]
new-set (collect [_ v (ipairs new-addresses)] (values v true))] (if (not (. old-addresses n))
(each [_ address (ipairs new-addresses)] (table.insert added address)))
(if (not (. old-set address)) (each [n address (pairs old-addresses)]
(table.insert added (parse-address address)))) (if (not (. new-addresses n))
(each [_ address (ipairs old-addresses)] (table.insert deleted address)))
(if (not (. new-set address))
(table.insert deleted (parse-address address))))
(values added deleted))) (values added deleted)))
(let [[state-directory wan-device] arg (fn update-addresses [wan-device addresses new-addresses]
dir (svc.open state-directory)] (let [(added deleted) (changes addresses new-addresses)]
(var addresses []) (each [_ p (ipairs added)]
(while true (system
(while (not (dir:ready?)) (dir:wait)) (.. "ip address add " p.address "/" p.len " dev " wan-device)))
(if (. bound-states (dir:output "state")) (each [_ p (ipairs deleted)]
(let [new-addresses (split " " (dir:output "/addresses")) (system
(added deleted) (changes addresses new-addresses)] (.. "ip address del " p.address "/" p.len " dev " wan-device)))
(each [_ p (ipairs added)] new-addresses))
(system
(.. "ip address add " p.address "/" p.len " dev " wan-device))) (fn run [state-directory wan-device]
(each [_ p (ipairs deleted)] (let [dir (svc.open state-directory)]
(system (var addresses [])
(.. "ip address del " p.address "/" p.len " dev " wan-device))) (while true
(set addresses new-addresses))) (while (not (dir:ready?)) (dir:wait))
(dir:wait))) (when (. bound-states (dir:output "state"))
(set addresses
(update-addresses wan-device addresses (dir:output "address"))))
(dir:wait))))
(if (os.getenv "RUN_TESTS")
{ : update-addresses : changes : run }
(let [[state-directory wan-device] arg]
(run state-directory wan-device)))