Skip to content

Fix assign{Ips,Macs} #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions nix/net-extensions.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let
;

inherit (lib.trivial)
flip
min
warnIf
;
Expand All @@ -35,10 +36,6 @@ let
assertMsg
;

inherit (lib.debug)
flip
;

# NOTE: from <https://github.com/oddlama/nixos-extra-modules/blob/ba53e1a0becde25b0644fbf886234669c9b285e3/lib/misc.nix>

# Calculates base^exp, but careful, this overflows for results > 2^62
Expand Down Expand Up @@ -242,6 +239,8 @@ in {
#
# > net.cidr.assignIps "192.168.100.1/24" [202 "192.168.100.74"] ["a" "b" "c"]
# { a = "192.168.100.203"; b = "192.168.100.75"; c = "192.168.100.226"; }
#
# WARN: Does not work on IPv6 addresses with Lix 2.91+ and Nix.25+ as they ban integer overflow.
assignIps = net: reserved: hosts: let
cidrSize = libNet.cidr.size net;
capacity = libNet.cidr.capacity net;
Expand Down Expand Up @@ -269,7 +268,8 @@ in {
# Generates a hash (i.e. offset value) for a given hostname
hashElem = x:
builtins.bitAnd (capacity - 1)
(hexToDec (builtins.substring 0 16 (builtins.hashString "sha256" x)));
# 15 characters is as many hexToDec can handle (60 bits)
(hexToDec (builtins.substring 0 15 (builtins.hashString "sha256" x)));
# Do linear probing. Returns the first unused value at or after the given value.
probe = avoid: value:
if elem value avoid
Expand Down Expand Up @@ -336,6 +336,8 @@ in {
#
# > net.mac.assignMacs "11:22:33:00:00:00" 24 ["11:22:33:1b:bd:ca"] ["a" "b" "c"]
# { a = "11:22:33:1b:bd:cb"; b = "11:22:33:39:59:4a"; c = "11:22:33:50:7a:e2"; }
#
# WARN: Does not work with Lix 2.91+ and Nix.25+ as they ban integer overflow.
assignMacs = base: size: reserved: hosts: let
capacity = pow 2 size;
baseAsInt = libNet.mac.diff base "00:00:00:00:00:00";
Expand All @@ -356,7 +358,8 @@ in {
# Generates a hash (i.e. offset value) for a given hostname
hashElem = x:
builtins.bitAnd (capacity - 1)
(hexToDec (substring 0 16 (builtins.hashString "sha256" x)));
# 15 characters is as many hexToDec can handle (60 bits)
(hexToDec (substring 0 15 (builtins.hashString "sha256" x)));
# Do linear probing. Returns the first unused value at or after the given value.
probe = avoid: value:
if elem value avoid
Expand Down