2018-12-07 12:17:16 +00:00
|
|
|
lib:
|
|
|
|
with lib;
|
|
|
|
let {
|
2018-12-11 20:37:52 +00:00
|
|
|
body = netname: subnetname: suffixSpec: rec {
|
2018-12-07 12:17:16 +00:00
|
|
|
address = let
|
2018-12-11 21:49:14 +00:00
|
|
|
suffix' = prependZeros suffixLength suffix;
|
2018-12-07 12:17:16 +00:00
|
|
|
in
|
2018-12-11 21:49:14 +00:00
|
|
|
normalize-ip6-addr
|
|
|
|
(checkAddress addressLength (joinAddress subnetPrefix suffix'));
|
2018-12-07 12:17:16 +00:00
|
|
|
addressCIDR = "${address}/${toString addressLength}";
|
|
|
|
addressLength = 128;
|
|
|
|
|
|
|
|
inherit netname;
|
|
|
|
netCIDR = "${netAddress}/${toString netPrefixLength}";
|
2018-12-14 19:03:26 +00:00
|
|
|
netAddress =
|
|
|
|
normalize-ip6-addr (appendZeros addressLength netPrefix);
|
2018-12-07 12:17:16 +00:00
|
|
|
netHash = toString {
|
|
|
|
retiolum = 0;
|
2018-12-16 15:11:02 +00:00
|
|
|
wiregrill = 1;
|
2018-12-07 12:17:16 +00:00
|
|
|
}.${netname};
|
|
|
|
netPrefix = "42:${netHash}";
|
|
|
|
netPrefixLength = {
|
|
|
|
retiolum = 32;
|
2018-12-16 15:11:02 +00:00
|
|
|
wiregrill = 32;
|
2018-12-07 12:17:16 +00:00
|
|
|
}.${netname};
|
|
|
|
|
|
|
|
inherit subnetname;
|
|
|
|
subnetCIDR = "${subnetAddress}/${toString subnetPrefixLength}";
|
2018-12-14 19:03:26 +00:00
|
|
|
subnetAddress =
|
|
|
|
normalize-ip6-addr (appendZeros addressLength subnetPrefix);
|
2018-12-11 22:21:42 +00:00
|
|
|
subnetHash = hashToLength 4 subnetname;
|
2018-12-07 12:17:16 +00:00
|
|
|
subnetPrefix = joinAddress netPrefix subnetHash;
|
|
|
|
subnetPrefixLength = netPrefixLength + 16;
|
|
|
|
|
2018-12-11 20:37:52 +00:00
|
|
|
suffix = getAttr (typeOf suffixSpec) {
|
|
|
|
set =
|
2018-12-11 21:49:14 +00:00
|
|
|
concatStringsSep
|
2018-12-11 20:37:52 +00:00
|
|
|
":"
|
2018-12-11 22:21:42 +00:00
|
|
|
(stringToGroupsOf
|
|
|
|
4
|
|
|
|
(hashToLength (suffixLength / 4) suffixSpec.hostName));
|
2018-12-11 20:37:52 +00:00
|
|
|
string = suffixSpec;
|
|
|
|
};
|
2018-12-07 12:17:16 +00:00
|
|
|
suffixLength = addressLength - subnetPrefixLength;
|
|
|
|
};
|
|
|
|
|
2018-12-11 21:49:14 +00:00
|
|
|
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);
|
|
|
|
|
2018-12-07 12:17:16 +00:00
|
|
|
hasEmptyPrefix = xs: take 2 xs == ["" ""];
|
|
|
|
hasEmptySuffix = xs: takeLast 2 xs == ["" ""];
|
|
|
|
hasEmptyInfix = xs: any (x: x == "") (trimEmpty 2 xs);
|
|
|
|
|
|
|
|
hasEmptyGroup = xs:
|
|
|
|
any (p: p xs) [hasEmptyPrefix hasEmptyInfix hasEmptySuffix];
|
|
|
|
|
|
|
|
ltrimEmpty = n: xs: if hasEmptyPrefix xs then drop n xs else xs;
|
|
|
|
rtrimEmpty = n: xs: if hasEmptySuffix xs then dropLast n xs else xs;
|
|
|
|
trimEmpty = n: xs: rtrimEmpty n (ltrimEmpty n xs);
|
|
|
|
|
|
|
|
parseAddress = splitString ":";
|
|
|
|
formatAddress = concatStringsSep ":";
|
|
|
|
|
|
|
|
check = s: c: if !c then throw "${s}" else true;
|
|
|
|
|
|
|
|
checkAddress = maxaddrlen: addr: let
|
|
|
|
parsedaddr = parseAddress addr;
|
|
|
|
normalizedaddr = trimEmpty 1 parsedaddr;
|
|
|
|
in
|
|
|
|
assert (check "address malformed; lone leading colon: ${addr}" (
|
|
|
|
head parsedaddr == "" -> tail (take 2 parsedaddr) == ""
|
|
|
|
));
|
|
|
|
assert (check "address malformed; lone trailing colon ${addr}" (
|
|
|
|
last parsedaddr == "" -> head (takeLast 2 parsedaddr) == ""
|
|
|
|
));
|
|
|
|
assert (check "address malformed; too many successive colons: ${addr}" (
|
|
|
|
length (filter (x: x == "") normalizedaddr) > 1 -> addr == [""]
|
|
|
|
));
|
|
|
|
assert (check "address malformed: ${addr}" (
|
|
|
|
all (test "[0-9a-f]{0,4}") parsedaddr
|
|
|
|
));
|
|
|
|
assert (check "address is too long: ${addr}" (
|
|
|
|
length normalizedaddr * 16 <= maxaddrlen
|
|
|
|
));
|
|
|
|
addr;
|
|
|
|
|
|
|
|
joinAddress = prefix: suffix: let
|
|
|
|
parsedPrefix = parseAddress prefix;
|
|
|
|
parsedSuffix = parseAddress suffix;
|
|
|
|
normalizePrefix = rtrimEmpty 2 parsedPrefix;
|
|
|
|
normalizeSuffix = ltrimEmpty 2 parsedSuffix;
|
|
|
|
delimiter =
|
|
|
|
optional (length (normalizePrefix ++ normalizeSuffix) < 8 &&
|
|
|
|
(hasEmptySuffix parsedPrefix || hasEmptyPrefix parsedSuffix))
|
|
|
|
"";
|
|
|
|
in
|
|
|
|
formatAddress (normalizePrefix ++ delimiter ++ normalizeSuffix);
|
|
|
|
}
|