Skip to content

Commit 7edf477

Browse files
authored
Merge pull request #19 from erikh/fix-subnet-calculation
Fix off-by-one in subnet calculation
2 parents b4db027 + 3591816 commit 7edf477

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

mgr.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var networkTemplate string
2828
const (
2929
magicComment = "--- Managed by zerotier-systemd-manager. Do not remove this comment. ---"
3030
networkDir = "/etc/systemd/network"
31-
ipv4bits = net.IPv4len * 8
31+
ipv4bits = 32
3232
)
3333

3434
// parameter list for multiple template operations
@@ -158,14 +158,15 @@ func main() {
158158
}
159159

160160
used, total := ipnet.Mask.Size()
161-
bits := int(math.Ceil(float64(total) / float64(used)))
162161

163-
octets := make([]byte, bits)
162+
bits := int(math.Trunc(math.Ceil(float64(total) / float64(used))))
163+
164+
octets := make([]byte, bits+1)
164165
if total == ipv4bits {
165166
ip = ip.To4()
166167
}
167168

168-
for i := 0; i < bits; i++ {
169+
for i := 0; i <= bits; i++ {
169170
octets[i] = ip[i]
170171
}
171172

0 commit comments

Comments
 (0)