forked from DGNum/liminix
acquire-wan-address uses parsed addresses from odhcp
This commit is contained in:
parent
3ff55d3aad
commit
7683ed69de
1 changed files with 33 additions and 46 deletions
|
@ -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 [])
|
|
||||||
(while true
|
|
||||||
(while (not (dir:ready?)) (dir:wait))
|
|
||||||
(if (. bound-states (dir:output "state"))
|
|
||||||
(let [new-addresses (split " " (dir:output "/addresses"))
|
|
||||||
(added deleted) (changes addresses new-addresses)]
|
|
||||||
(each [_ p (ipairs added)]
|
(each [_ p (ipairs added)]
|
||||||
(system
|
(system
|
||||||
(.. "ip address add " p.address "/" p.len " dev " wan-device)))
|
(.. "ip address add " p.address "/" p.len " dev " wan-device)))
|
||||||
(each [_ p (ipairs deleted)]
|
(each [_ p (ipairs deleted)]
|
||||||
(system
|
(system
|
||||||
(.. "ip address del " p.address "/" p.len " dev " wan-device)))
|
(.. "ip address del " p.address "/" p.len " dev " wan-device)))
|
||||||
(set addresses new-addresses)))
|
new-addresses))
|
||||||
(dir:wait)))
|
|
||||||
|
(fn run [state-directory wan-device]
|
||||||
|
(let [dir (svc.open state-directory)]
|
||||||
|
(var addresses [])
|
||||||
|
(while true
|
||||||
|
(while (not (dir:ready?)) (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)))
|
||||||
|
|
Loading…
Reference in a new issue