Skip to content

Commit

Permalink
Removing _encode internal function as it was only used by subnet mask…
Browse files Browse the repository at this point in the history
… which has been removed for now.
  • Loading branch information
djacu committed Apr 12, 2024
1 parent b7c93e1 commit a8ab014
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 46 deletions.
39 changes: 0 additions & 39 deletions lib/network/internal.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,45 +55,6 @@ rec {
};

ipv4 = {
/**
Encodes an integer into a valid IPv4 address.
# Example
```nix
_encode 0
=> "0.0.0.0"
_encode 3232235521
=> "192.168.0.1"
```
# Type
```
_encode :: Int -> String
```
# Arguments
- [num] A decoded integer representation of an IPv4 address.
# Throws
- If the argument is less than zero.
- If the argument is greater than or equal to 2^32.
*/
_encode =
num:
if num < 0 then
throw "lib.network.ipv4._encode: ${toString num} cannot be encoded into an IPv4 address."
# ipv4 only has 4*8 = 32 bits, so 2^32 addresses
else if num >= 4294967296 then
throw "lib.network.ipv4._encode: ${toString num} is too large to encode into an IPv4 address."
else
concatStringsSep "." (
map (x: toString (mod (num / x) 256)) (reverseList (genList (x: common.pow 2 (x * 8)) 4))
);

/**
Extracts a prefix length from a CIDR and verifies it is valid.
Expand Down
7 changes: 0 additions & 7 deletions lib/network/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,6 @@ expectEqual 'internal.common.pow 2 0' '1'
expectEqual 'internal.common.pow 2 3' '8'
expectFailure 'internal.common.pow 2 (-1)' 'lib.network.pow: Exponent cannot be negative.'

# Test basic cases for encoding IPv4 address.
expectEqual 'internal.ipv4._encode 0' '"0.0.0.0"'
expectEqual 'internal.ipv4._encode 4294967295' '"255.255.255.255"'
expectEqual 'internal.ipv4._encode 3232235521' '"192.168.0.1"'
expectFailure 'internal.ipv4._encode 4294967296' 'lib.network.ipv4._encode: [[:digit:]]+ is too large to encode into an IPv4 address.'
expectFailure 'internal.ipv4._encode (-1)' 'lib.network.ipv4._encode: -[[:digit:]]+ cannot be encoded into an IPv4 address.'

# Test basic cases for verifying an address.
expectEqual 'internal.ipv4._verifyAddress "192.168.0.1/24"' '"192.168.0.1"'
expectFailure 'internal.ipv4._verifyAddress "192.168.0./24"' 'lib.network.ipv4: CIDR 192.168.0./24 has an empty octet.'
Expand Down

0 comments on commit a8ab014

Please sign in to comment.