···
+
/* See: https://github.com/djacu/nixpkgs/blob/adb22cf/lib/network.nix */
+
Converts an IP address from a list of ints to a string.
+
Type: prettyIp :: [ Int ] -> String
+
prettyIp [ 192 168 70 9 ]
+
lib.concatStringsSep "." (builtins.map builtins.toString addr);
+
Given a bit mask, return the associated subnet mask.
+
Type: bitMaskToSubnetMask :: Int -> [ Int ]
+
bitMaskToSubnetMask = bitMask: let
+
# How many initial parts of the mask are full (=255)
+
fullParts = bitMask / octetBits;
+
# Fill up initial full parts
+
# If we're above the first non-full part, fill with 0
+
else if fullParts < idx
+
# First non-full part generation
+
else _genPartialMask (lib.mod bitMask octetBits)
+
Generate a the partial portion of a subnet mask.
+
Type: _genPartialMask :: Int -> Int
+
else _genPartialMask (n - 1) / 2 + 128;
+
Given a subnet mask, return the associated bit mask.
+
Type: subnetMaskToBitMask :: [ Int ] -> Int
+
subnetMaskToBitMask [ 255 254 0 0 ]
+
subnetMaskToBitMask [ 255 255 255 0 ]
+
subnetMaskToBitMask = subnetMask: let
+
else (lib.mod octet 2) + partialBits (octet / 2);
+
(builtins.map partialBits subnetMask);
+
Given a CIDR, return the IP Address.
+
Type: cidrToIpAddress :: String -> [ Int ]
+
cidrToIpAddress "192.168.70.9/15"
+
cidrToIpAddress = cidr: let
+
splitParts = lib.splitString "/" cidr;
+
addr = lib.elemAt splitParts 0;
+
(builtins.match "([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)" addr);
+
(octet >= 0) && (octet <= 255);
+
if (builtins.all checkBounds parsed)
+
else builtins.throw "IP ${prettyIp addr} has out of bounds octet(s)";
+
Given a CIDR, return the bitmask.
+
Type: cidrToBitMask :: String -> Int
+
cidrToBitMask "192.168.70.9/15"
+
cidrToBitMask = cidr: let
+
splitParts = lib.splitString "/" cidr;
+
mask = lib.toInt (lib.elemAt splitParts 1);
+
(mask >= 0) && (mask <= 32);
+
else builtins.throw "Bitmask ${builtins.toString mask} is invalid.";
+
Given a CIDR, return the associated subnet mask.
+
Type: cidrToSubnetMask :: String -> [ Int ]
+
cidrToSubnetMask "192.168.70.9/15"
+
cidrToSubnetMask = cidr:
+
bitMaskToSubnetMask (cidrToBitMask cidr);
+
Given a CIDR, return the associated network ID.
+
Type: cidrToNetworkId :: String -> [ Int ]
+
cidrToNetworkId "192.168.70.9/15"
+
cidrToNetworkId = cidr: let
+
ip = cidrToIpAddress cidr;
+
subnetMask = cidrToSubnetMask cidr;
+
lib.zipListsWith lib.bitAnd ip subnetMask;
+
Given a CIDR, return the associated first usable IP address.
+
Type: cidrToFirstUsableIp :: String -> [ Int ]
+
cidrToFirstUsableIp "192.168.70.9/15"
+
cidrToFirstUsableIp = cidr: let
+
networkId = cidrToNetworkId cidr;
+
incrementIp networkId 1;
+
Given a CIDR, return the associated broadcast address.
+
Type: cidrToBroadcastAddress :: String -> [ Int ]
+
cidrToBroadcastAddress "192.168.70.9/15"
+
cidrToBroadcastAddress = cidr: let
+
subnetMask = cidrToSubnetMask cidr;
+
networkId = cidrToNetworkId cidr;
+
getBroadcastAddress networkId subnetMask;
+
Given a network ID and subnet mask, return the associated broadcast address.
+
Type: getBroadcastAddress :: [ Int ] -> [ Int ] -> [ Int ]
+
getBroadcastAddress [ 192 168 0 0 ] [ 255 254 0 0 ]
+
getBroadcastAddress = networkId: subnetMask:
+
lib.zipListsWith (nid: mask: 255 - mask + nid) networkId subnetMask;
+
Given a CIDR, return the associated last usable IP address.
+
Type: cidrToLastUsableIp :: String -> [ Int ]
+
cidrToLastUsableIp "192.168.70.9/15"
+
cidrToLastUsableIp = cidr: let
+
broadcast = cidrToBroadcastAddress cidr;
+
incrementIp broadcast (-1);
+
Increment the last octet of a given IP address.
+
Type: incrementIp :: [ Int ] -> Int -> [ Int ]
+
incrementIp [ 192 168 70 9 ] 3
+
incrementIp [ 192 168 70 9 ] (-2)
+
incrementIp = addr: offset: let
+
lastOctet = lib.last addr;
+
firstThree = lib.init addr;
+
firstThree ++ [(lastOctet + offset)];
+
Given an IP address and bit mask, return the associated CIDR.
+
Type: ipAndBitMaskToCidr :: [ Int ] -> Int -> String
+
ipAndBitMaskToCidr [ 192 168 70 9 ] 15
+
ipAndBitMaskToCidr = addr: bitMask:
+
lib.concatStringsSep "/"
+
(builtins.toString bitMask)
+
Given an IP address and subnet mask, return the associated CIDR.
+
Type: ipAndSubnetMaskToCidr :: [ Int ] -> Int -> String
+
ipAndSubnetMaskToCidr [ 192 168 70 9 ] [ 255 254 0 0 ]
+
ipAndSubnetMaskToCidr = addr: subnetMask:
+
ipAndBitMaskToCidr addr (subnetMaskToBitMask subnetMask);
+
Given a CIDR, return an attribute set of:
+
the first usable IP address,
+
the last usable IP address,
+
Type: getNetworkProperties :: str -> attrset
+
getNetworkProperties "192.168.70.9/15"
+
broadcast = "192.169.255.255";
+
firstUsableIp = "192.168.0.1";
+
ipAddress = "192.168.70.9";
+
lastUsableIp = "192.169.255.254";
+
networkId = "192.168.0.0";
+
subnetMask = "255.254.0.0";
+
getNetworkProperties = cidr: let
+
ipAddress = prettyIp (cidrToIpAddress cidr);
+
bitMask = cidrToBitMask cidr;
+
firstUsableIp = prettyIp (cidrToFirstUsableIp cidr);
+
lastUsableIp = prettyIp (cidrToLastUsableIp cidr);
+
networkId = prettyIp (cidrToNetworkId cidr);
+
subnetMask = prettyIp (cidrToSubnetMask cidr);
+
broadcast = prettyIp (cidrToBroadcastAddress cidr);
+
in {inherit ipAddress bitMask firstUsableIp lastUsableIp networkId subnetMask broadcast;};