Skip to content

Commit

Permalink
Merge pull request #3 from dj-nlx/main
Browse files Browse the repository at this point in the history
IPAM functionality fixes
  • Loading branch information
UltraInstinct14 authored Jan 20, 2023
2 parents 22352b0 + 0fcaf60 commit 73ed815
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
22 changes: 18 additions & 4 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ func (C *Counter) ReserveCounter(id uint64) error {
return errors.New("Overflow")
}

tmp := C.start
C.start = C.counters[id]
C.end = tmp
C.counters[id] = ^uint64(0)
rid := id - C.begin
if C.counters[rid] == ^uint64(0) {
return errors.New("Already exists")
}

for i := uint64(0); i < C.len; i++ {
if C.counters[i] == rid {
C.counters[i] = C.counters[rid]
}
}

if C.start == rid {
C.start = C.counters[rid]
}
if C.end == rid {
C.end = C.counters[rid]
}
C.counters[rid] = ^uint64(0)
C.cap--

return nil
Expand Down
12 changes: 9 additions & 3 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,20 @@ func TestIPAlloc(t *testing.T) {
ipa := IpAllocatorNew()

ipa.AddIPRange(IPClusterDefault, "123.123.123.0/24")
ipa.ReserveIP(IPClusterDefault, "123.123.123.0/24", 0, "123.123.123.2")
for i := 0; i < 255; i++ {
ip, err := ipa.AllocateNewIP(IPClusterDefault, "123.123.123.0/24", uint32(0))
if i >= 254 && err == nil {
if i >= 253 && err == nil {
t.Fatal("Failed IP Alloc for 123.123.123.0/24 - Check Alloc Algo")
} else if i < 254 && err != nil {
} else if i < 253 && err != nil {
t.Fatalf("Failed IP Alloc for 123.123.123.0/24 : %d:%s", i, err)
} else if err == nil {
expected := fmt.Sprintf("123.123.123.%d", i+1)
expected := ""
if i < 1 {
expected = fmt.Sprintf("123.123.123.%d", i+1)
} else {
expected = fmt.Sprintf("123.123.123.%d", i+2)
}
if ip.String() != expected {
t.Fatalf("Failed IP Alloc for 123.123.123.0/24: %s:%s", ip.String(), expected)
}
Expand Down

0 comments on commit 73ed815

Please sign in to comment.