2023-07-05 21:23:27 +02:00
|
|
|
(local { : merge : split : file-exists? : system } (require :anoia))
|
2023-07-08 23:15:56 +02:00
|
|
|
(local svc (require :anoia.svc))
|
2023-07-02 01:52:18 +02:00
|
|
|
|
|
|
|
(fn parse-prefix [str]
|
|
|
|
(fn parse-extra [s]
|
|
|
|
(let [out {}]
|
|
|
|
(each [name val (string.gmatch s ",(.-)=([^,]+)")]
|
|
|
|
(tset out name val))
|
|
|
|
out))
|
|
|
|
(let [(prefix len preferred valid extra)
|
|
|
|
(string.match str "(.-)::/(%d+),(%d+),(%d+)(.*)$")]
|
|
|
|
(merge {: prefix : len : preferred : valid} (parse-extra extra))))
|
2023-06-01 00:29:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
;; Format: <prefix>/<length>,preferred,valid[,excluded=<excluded-prefix>/<length>][,class=<prefix class #>]
|
|
|
|
|
2023-07-02 01:52:18 +02:00
|
|
|
;;(parse-prefix "2001:8b0:de3a:40dc::/64,7198,7198")
|
|
|
|
;;(parse-prefix "2001:8b0:de3a:1001::/64,7198,7188,excluded=1/2,thi=10")
|
2023-06-01 00:29:05 +02:00
|
|
|
|
2023-07-02 01:52:18 +02:00
|
|
|
|
|
|
|
(local bound-states
|
|
|
|
{ :bound true
|
|
|
|
:rebound true
|
|
|
|
:informed true
|
|
|
|
:updated true
|
|
|
|
:ra-updated true
|
|
|
|
})
|
|
|
|
|
|
|
|
; (local { : view } (require :fennel))
|
|
|
|
|
|
|
|
(fn changes [old-prefixes new-prefixes]
|
|
|
|
(let [added {}
|
|
|
|
deleted {}
|
|
|
|
old-set (collect [_ v (ipairs old-prefixes)] (values v true))
|
|
|
|
new-set (collect [_ v (ipairs new-prefixes)] (values v true))]
|
|
|
|
(each [_ prefix (ipairs new-prefixes)]
|
|
|
|
(if (not (. old-set prefix))
|
|
|
|
(table.insert added (parse-prefix prefix))))
|
|
|
|
(each [_ prefix (ipairs old-prefixes)]
|
|
|
|
(if (not (. new-set prefix))
|
|
|
|
(table.insert deleted (parse-prefix prefix))))
|
|
|
|
(values added deleted)))
|
2023-06-01 00:29:05 +02:00
|
|
|
|
|
|
|
(let [[state-directory lan-device] arg
|
2023-07-08 23:15:56 +02:00
|
|
|
dir (svc.open state-directory)]
|
2023-07-02 01:52:18 +02:00
|
|
|
(var prefixes [])
|
|
|
|
(while true
|
2023-07-08 23:15:56 +02:00
|
|
|
(while (not (dir:ready?)) (dir:wait))
|
|
|
|
(if (. bound-states (dir:output "state"))
|
|
|
|
(let [new-prefixes (split " " (dir:output "/prefixes"))
|
2023-07-02 01:52:18 +02:00
|
|
|
(added deleted) (changes prefixes new-prefixes)]
|
|
|
|
(each [_ p (ipairs added)]
|
2023-07-05 21:23:27 +02:00
|
|
|
(system
|
2023-07-02 01:52:18 +02:00
|
|
|
(.. "ip address add " p.prefix "::1/" p.len " dev " lan-device)))
|
|
|
|
(each [_ p (ipairs deleted)]
|
2023-07-05 21:23:27 +02:00
|
|
|
(system
|
2023-07-02 01:52:18 +02:00
|
|
|
(.. "ip address del " p.prefix "::1/" p.len " dev " lan-device)))
|
|
|
|
(set prefixes new-prefixes)))
|
2023-07-08 23:15:56 +02:00
|
|
|
(dir:wait)))
|