From 35918163333e86c30f393cad8ed9ca706c24fc16 Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Sat, 9 Apr 2022 10:03:30 -0700 Subject: [PATCH] Fix off-by-one in subnet calculation This was causing problems for people on many networks. Signed-off-by: Erik Hollensbe --- mgr.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mgr.go b/mgr.go index c8d9795..21bb0db 100644 --- a/mgr.go +++ b/mgr.go @@ -28,7 +28,7 @@ var networkTemplate string const ( magicComment = "--- Managed by zerotier-systemd-manager. Do not remove this comment. ---" networkDir = "/etc/systemd/network" - ipv4bits = net.IPv4len * 8 + ipv4bits = 32 ) // parameter list for multiple template operations @@ -158,14 +158,15 @@ func main() { } used, total := ipnet.Mask.Size() - bits := int(math.Ceil(float64(total) / float64(used))) - octets := make([]byte, bits) + bits := int(math.Trunc(math.Ceil(float64(total) / float64(used)))) + + octets := make([]byte, bits+1) if total == ipv4bits { ip = ip.To4() } - for i := 0; i < bits; i++ { + for i := 0; i <= bits; i++ { octets[i] = ip[i] }