summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2017-04-17 13:15:13 +0200
committermakefu <github@syntax-fehler.de>2017-04-17 13:15:13 +0200
commit35c7e1c37545dd96c90d54191ba835eb0ceeac8e (patch)
tree65db0a2d770654f555fcac74c8e457261054df5d /lib
parent9d7e9bf4a9630bb763d7d7bff7880c70405c7ea3 (diff)
parentb6a4df7e398c7b4d7dc4f56d2eded22a79fee30f (diff)
Merge remote-tracking branch 'lass/master'
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix46
-rw-r--r--lib/types.nix1
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 7e61c9413..9399a0107 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -41,6 +41,52 @@ let
indent = replaceChars ["\n"] ["\n "];
+ # https://tools.ietf.org/html/rfc5952
+ normalize-ip6-addr =
+ let
+ max-run-0 =
+ let
+ both = v: { off = v; pos = v; };
+ gt = a: b: a.pos - a.off > b.pos - b.off;
+
+ chkmax = ctx: {
+ cur = both (ctx.cur.pos + 1);
+ max = if gt ctx.cur ctx.max then ctx.cur else ctx.max;
+ };
+
+ incpos = ctx: recursiveUpdate ctx {
+ cur.pos = ctx.cur.pos + 1;
+ };
+
+ f = ctx: blk: (if blk == "0" then incpos else chkmax) ctx;
+ z = { cur = both 0; max = both 0; };
+ in
+ blks: (chkmax (foldl' f z blks)).max;
+
+ group-zeros = a:
+ let
+ blks = splitString ":" a;
+ max = max-run-0 blks;
+ lhs = take max.off blks;
+ rhs = drop max.pos blks;
+ in
+ if max.pos == 0
+ then a
+ else "${concatStringsSep ":" lhs}::${concatStringsSep ":" rhs}";
+
+ drop-leading-zeros =
+ let
+ f = block:
+ let
+ res = match "0*(.+)" block;
+ in
+ if res == null
+ then block # empty block
+ else elemAt res 0;
+ in
+ a: concatStringsSep ":" (map f (splitString ":" a));
+ in
+ a: toLower (group-zeros (drop-leading-zeros a));
};
in
diff --git a/lib/types.nix b/lib/types.nix
index 8a3c76483..f511541bd 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -103,6 +103,7 @@ rec {
options = {
addr = mkOption {
type = addr6;
+ apply = lib.normalize-ip6-addr;
};
prefix = mkOption ({
type = str; # TODO routing prefix (CIDR)