summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix8
-rw-r--r--lib/krebs/genipv6.nix61
2 files changed, 55 insertions, 14 deletions
diff --git a/lib/default.nix b/lib/default.nix
index bf8c65e21..e352c7be9 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -29,8 +29,6 @@ let
listToAttrs (map (name: nameValuePair name set.${name})
(filter (flip hasAttr set) names));
- setAttr = name: value: set: set // { ${name} = value; };
-
test = re: x: isString x && testString re x;
testString = re: x: match re x != null;
@@ -109,7 +107,11 @@ let
in
a: concatStringsSep ":" (map f (splitString ":" a));
in
- a: toLower (group-zeros (drop-leading-zeros a));
+ a:
+ toLower
+ (if test ".*::.*" a
+ then a
+ else group-zeros (drop-leading-zeros a));
};
in
diff --git a/lib/krebs/genipv6.nix b/lib/krebs/genipv6.nix
index 27df8bf55..bf3ebab38 100644
--- a/lib/krebs/genipv6.nix
+++ b/lib/krebs/genipv6.nix
@@ -1,20 +1,18 @@
lib:
with lib;
let {
- body = netname: subnetname: suffix: rec {
+ body = netname: subnetname: suffixSpec: rec {
address = let
- suffix' =
- if hasEmptyGroup (parseAddress suffix)
- then suffix
- else joinAddress "::" suffix;
+ suffix' = prependZeros suffixLength suffix;
in
- checkAddress addressLength (joinAddress subnetPrefix suffix');
+ normalize-ip6-addr
+ (checkAddress addressLength (joinAddress subnetPrefix suffix'));
addressCIDR = "${address}/${toString addressLength}";
addressLength = 128;
inherit netname;
netCIDR = "${netAddress}/${toString netPrefixLength}";
- netAddress = joinAddress netPrefix "::";
+ netAddress = appendZeros netPrefixLength netPrefix;
netHash = toString {
retiolum = 0;
wirelum = 1;
@@ -27,16 +25,57 @@ let {
inherit subnetname;
subnetCIDR = "${subnetAddress}/${toString subnetPrefixLength}";
- subnetAddress = joinAddress subnetPrefix "::";
- subnetHash = hash subnetname;
+ subnetAddress = appendZeros subnetPrefixLength subnetPrefix;
+ subnetHash = hash 4 subnetname;
subnetPrefix = joinAddress netPrefix subnetHash;
subnetPrefixLength = netPrefixLength + 16;
- inherit suffix;
+ suffix = getAttr (typeOf suffixSpec) {
+ set =
+ concatStringsSep
+ ":"
+ (stringToGroupsOf 4 (hash (suffixLength / 4) suffixSpec.hostName));
+ string = suffixSpec;
+ };
suffixLength = addressLength - subnetPrefixLength;
};
- hash = s: head (match "0*(.+)" (substring 0 4 (hashString "sha256" s)));
+ appendZeros = n: s: let
+ n' = n / 16;
+ zeroCount = n' - length parsedaddr;
+ parsedaddr = parseAddress s;
+ in
+ formatAddress (parsedaddr ++ map (const "0") (range 1 zeroCount));
+
+ prependZeros = n: s: let
+ n' = n / 16;
+ zeroCount = n' - length parsedaddr;
+ parsedaddr = parseAddress s;
+ in
+ formatAddress (map (const "0") (range 1 zeroCount) ++ parsedaddr);
+
+ # Split string into list of chunks where each chunk is at most n chars long.
+ # The leftmost chunk might shorter.
+ # Example: stringToGroupsOf "123456" -> ["12" "3456"]
+ stringToGroupsOf = n: s: let
+ acc =
+ foldl'
+ (acc: c: if stringLength acc.chunk < n then {
+ chunk = acc.chunk + c;
+ chunks = acc.chunks;
+ } else {
+ chunk = c;
+ chunks = acc.chunks ++ [acc.chunk];
+ })
+ {
+ chunk = "";
+ chunks = [];
+ }
+ (stringToCharacters s);
+ in
+ filter (x: x != []) ([acc.chunk] ++ acc.chunks);
+
+ hash = n: s: substring 0 n (hashString "sha256" s);
dropLast = n: xs: reverseList (drop n (reverseList xs));
takeLast = n: xs: reverseList (take n (reverseList xs));