lib: ip: add 'nthInRange4'
And use it to refactor 'rangeIp4'
This commit is contained in:
parent
509332270e
commit
1968285d0a
22
lib/ip.nix
22
lib/ip.nix
|
@ -86,15 +86,8 @@ rec {
|
||||||
# Pretty print a parsed subnet into a human readable form
|
# Pretty print a parsed subnet into a human readable form
|
||||||
prettySubnet4 = { baseIp, cidr, ... }: "${prettyIp4 baseIp}/${toString cidr}";
|
prettySubnet4 = { baseIp, cidr, ... }: "${prettyIp4 baseIp}/${toString cidr}";
|
||||||
|
|
||||||
# Convert an IPv4 range into a list of all its constituent addresses
|
# Get the nth address from an IPv4 range, without checking if it is in range
|
||||||
rangeIp4 =
|
nthInRange4 = { from, to }: n:
|
||||||
{ from, to }:
|
|
||||||
let
|
|
||||||
numAddresses =
|
|
||||||
builtins.foldl' (acc: rhs: acc * 256 + rhs)
|
|
||||||
0
|
|
||||||
(zipListsWith (lhs: rhs: lhs - rhs) to from);
|
|
||||||
addToBase = n:
|
|
||||||
let
|
let
|
||||||
carry = lhs: { carry, acc }:
|
carry = lhs: { carry, acc }:
|
||||||
let
|
let
|
||||||
|
@ -107,6 +100,15 @@ rec {
|
||||||
carried = foldr carry { carry = n; acc = [ ]; } from;
|
carried = foldr carry { carry = n; acc = [ ]; } from;
|
||||||
in
|
in
|
||||||
carried.acc;
|
carried.acc;
|
||||||
|
|
||||||
|
# Convert an IPv4 range into a list of all its constituent addresses
|
||||||
|
rangeIp4 =
|
||||||
|
{ from, to } @ arg:
|
||||||
|
let
|
||||||
|
numAddresses =
|
||||||
|
builtins.foldl' (acc: rhs: acc * 256 + rhs)
|
||||||
|
0
|
||||||
|
(zipListsWith (lhs: rhs: lhs - rhs) to from);
|
||||||
in
|
in
|
||||||
map addToBase (range 0 numAddresses);
|
map (nthInRange4 arg) (range 0 numAddresses);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue