Skip to content

Commit

Permalink
Remove subnet mask from the library for the initial PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
djacu committed Apr 12, 2024
1 parent f28ca5a commit b7c93e1
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 34 deletions.
31 changes: 1 addition & 30 deletions lib/network/internal.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ let
inherit (lib)
concatStringsSep
elemAt
foldl
genList
mod
range
Expand Down Expand Up @@ -55,33 +54,7 @@ rec {
foldl' (acc: _: acc * base) 1 (range 1 exponent);
};

ipv4 = rec {
/**
Given a prefix length, returns the equivalent subnet mask.
# Example
```nix
_prefixToSubnetMask "24"
=> "255.255.255.0"
```
# Type
```
_prefixToSubnetMask :: String -> String
# Arguments
- [prefixLength] A prefix length
*/
_prefixToSubnetMask =
prefixLength:
let
prefixLength' = toInt prefixLength;
in
_encode ((foldl (x: y: 2 * x + 1) 0 (range 1 prefixLength')) * (common.pow 2 (32 - prefixLength')));

ipv4 = {
/**
Encodes an integer into a valid IPv4 address.
Expand Down Expand Up @@ -198,7 +171,6 @@ rec {
address = "192.168.0.1";
cidr = "192.168.0.1/24";
prefixLength = "24";
subnetMask = "255.255.255.0";
}
```
Expand All @@ -219,7 +191,6 @@ rec {
prefixLength
];
inherit address prefixLength;
subnetMask = _prefixToSubnetMask prefixLength;
};
};
}
4 changes: 0 additions & 4 deletions lib/network/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ expectFailure() {
expectEqual '(ipv4.fromCidrString "192.168.0.1/24").cidr' '"192.168.0.1/24"'
expectEqual '(ipv4.fromCidrString "192.168.0.1/24").address' '"192.168.0.1"'
expectEqual '(ipv4.fromCidrString "192.168.0.1/24").prefixLength' '"24"'
expectEqual '(ipv4.fromCidrString "192.168.0.1/24").subnetMask' '"255.255.255.0"'

# Test pow function
expectEqual 'internal.common.pow 2 0' '1'
Expand Down Expand Up @@ -131,6 +130,3 @@ expectFailure 'internal.ipv4._verifyPrefixLength "192.168.0.1/"' 'lib.network.ip
expectFailure 'internal.ipv4._verifyPrefixLength "192.168.0.1/33"' 'lib.network.ipv4: CIDR 192.168.0.1/33 has an out of bounds prefix length, 33.'
expectFailure 'internal.ipv4._verifyPrefixLength "192.168.0.1/-1"' 'lib.network.ipv4: CIDR 192.168.0.1/-1 has an out of bounds prefix length, -1.'
expectFailure 'internal.ipv4._verifyPrefixLength "192.168.0.1/24/bad"' 'lib.network.ipv4: Could not verify prefix length for CIDR 192.168.0.1/24/bad'

# Test prefix length to subnet mask conversion.
expectEqual 'internal.ipv4._prefixToSubnetMask "24"' '"255.255.255.0"'

0 comments on commit b7c93e1

Please sign in to comment.