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
|
||||
prettySubnet4 = { baseIp, cidr, ... }: "${prettyIp4 baseIp}/${toString cidr}";
|
||||
|
||||
# Convert an IPv4 range into a list of all its constituent addresses
|
||||
rangeIp4 =
|
||||
{ from, to }:
|
||||
let
|
||||
numAddresses =
|
||||
builtins.foldl' (acc: rhs: acc * 256 + rhs)
|
||||
0
|
||||
(zipListsWith (lhs: rhs: lhs - rhs) to from);
|
||||
addToBase = n:
|
||||
# Get the nth address from an IPv4 range, without checking if it is in range
|
||||
nthInRange4 = { from, to }: n:
|
||||
let
|
||||
carry = lhs: { carry, acc }:
|
||||
let
|
||||
|
@ -107,6 +100,15 @@ rec {
|
|||
carried = foldr carry { carry = n; acc = [ ]; } from;
|
||||
in
|
||||
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
|
||||
map addToBase (range 0 numAddresses);
|
||||
map (nthInRange4 arg) (range 0 numAddresses);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue