diff --git a/bitseq/sequence.go b/bitseq/sequence.go index 1d06e75..f8a2b18 100644 --- a/bitseq/sequence.go +++ b/bitseq/sequence.go @@ -14,10 +14,11 @@ import ( // block sequence constants // If needed we can think of making these configurable const ( - blockLen = 32 + blockLen = uint32(32) blockBytes = blockLen / 8 - blockMAX = 1< 0 && s.block&bitSel != 0 { bitSel >>= 1 bits++ } - return bits / 8, bits % 8 + return bits / 8, bits % 8, nil } // GetCopy returns a copy of the linked list rooted at this node @@ -168,7 +169,7 @@ func (s *sequence) fromByteArray(data []byte) error { } // GetFirstAvailable returns the byte and bit position of the first unset bit -func (h *Handle) GetFirstAvailable() (int, int, error) { +func (h *Handle) GetFirstAvailable() (uint32, uint32, error) { h.Lock() defer h.Unlock() return getFirstAvailable(h.head) @@ -176,14 +177,14 @@ func (h *Handle) GetFirstAvailable() (int, int, error) { // CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset // If the ordinal is beyond the sequence limits, a negative response is returned -func (h *Handle) CheckIfAvailable(ordinal int) (int, int, error) { +func (h *Handle) CheckIfAvailable(ordinal uint32) (uint32, uint32, error) { h.Lock() defer h.Unlock() return checkIfAvailable(h.head, ordinal) } // PushReservation pushes the bit reservation inside the bitmask. -func (h *Handle) PushReservation(bytePos, bitPos int, release bool) error { +func (h *Handle) PushReservation(bytePos, bitPos uint32, release bool) error { // Create a copy of the current handler h.Lock() nh := &Handle{ @@ -273,63 +274,62 @@ func (h *Handle) Unselected() uint32 { } // getFirstAvailable looks for the first unset bit in passed mask -func getFirstAvailable(head *sequence) (int, int, error) { - byteIndex := 0 +func getFirstAvailable(head *sequence) (uint32, uint32, error) { + byteIndex := uint32(0) current := head for current != nil { if current.block != blockMAX { - bytePos, bitPos := current.getAvailableBit() - return byteIndex + bytePos, bitPos, nil + bytePos, bitPos, err := current.getAvailableBit() + return byteIndex + bytePos, bitPos, err } - byteIndex += int(current.count * blockBytes) + byteIndex += current.count * blockBytes current = current.next } - return -1, -1, fmt.Errorf("no bit available") + return invalidPos, invalidPos, fmt.Errorf("no bit available") } // checkIfAvailable checks if the bit correspondent to the specified ordinal is unset // If the ordinal is beyond the sequence limits, a negative response is returned -func checkIfAvailable(head *sequence, ordinal int) (int, int, error) { +func checkIfAvailable(head *sequence, ordinal uint32) (uint32, uint32, error) { bytePos := ordinal / 8 bitPos := ordinal % 8 // Find the sequence containing this byte current, _, _, inBlockBytePos := findSequence(head, bytePos) - if current != nil { // Check whether the bit corresponding to the ordinal address is unset - bitSel := uint32(blockFirstBit >> uint(inBlockBytePos*8+bitPos)) + bitSel := blockFirstBit >> (inBlockBytePos*8 + bitPos) if current.block&bitSel == 0 { return bytePos, bitPos, nil } } - return -1, -1, fmt.Errorf("requested bit is not available") + return invalidPos, invalidPos, fmt.Errorf("requested bit is not available") } // Given the byte position and the sequences list head, return the pointer to the // sequence containing the byte (current), the pointer to the previous sequence, // the number of blocks preceding the block containing the byte inside the current sequence. -// If bytePos is outside of the list, function will return (nil, nil, 0, -1) -func findSequence(head *sequence, bytePos int) (*sequence, *sequence, uint32, int) { +// If bytePos is outside of the list, function will return (nil, nil, 0, invalidPos) +func findSequence(head *sequence, bytePos uint32) (*sequence, *sequence, uint32, uint32) { // Find the sequence containing this byte previous := head current := head n := bytePos - for current.next != nil && n >= int(current.count*blockBytes) { // Nil check for less than 32 addresses masks - n -= int(current.count * blockBytes) + for current.next != nil && n >= (current.count*blockBytes) { // Nil check for less than 32 addresses masks + n -= (current.count * blockBytes) previous = current current = current.next } // If byte is outside of the list, let caller know - if n >= int(current.count*blockBytes) { - return nil, nil, 0, -1 + if n >= (current.count * blockBytes) { + return nil, nil, 0, invalidPos } // Find the byte position inside the block and the number of blocks // preceding the block containing the byte inside this sequence - precBlocks := uint32(n / blockBytes) + precBlocks := n / blockBytes inBlockBytePos := bytePos % blockBytes return current, previous, precBlocks, inBlockBytePos @@ -352,7 +352,7 @@ func findSequence(head *sequence, bytePos int) (*sequence, *sequence, uint32, in // A) block is first in current: [prev seq] [new] [modified current seq] [next seq] // B) block is last in current: [prev seq] [modified current seq] [new] [next seq] // C) block is in the middle of current: [prev seq] [curr pre] [new] [curr post] [next seq] -func pushReservation(bytePos, bitPos int, head *sequence, release bool) *sequence { +func pushReservation(bytePos, bitPos uint32, head *sequence, release bool) *sequence { // Store list's head newHead := head diff --git a/bitseq/sequence_test.go b/bitseq/sequence_test.go index f8831dd..5370353 100644 --- a/bitseq/sequence_test.go +++ b/bitseq/sequence_test.go @@ -9,58 +9,58 @@ import ( func TestSequenceGetAvailableBit(t *testing.T) { input := []struct { head *sequence - bytePos int - bitPos int + bytePos uint32 + bitPos uint32 }{ - {&sequence{block: 0x0, count: 0}, -1, -1}, + {&sequence{block: 0x0, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0x0, count: 1}, 0, 0}, {&sequence{block: 0x0, count: 100}, 0, 0}, - {&sequence{block: 0x80000000, count: 0}, -1, -1}, + {&sequence{block: 0x80000000, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0x80000000, count: 1}, 0, 1}, {&sequence{block: 0x80000000, count: 100}, 0, 1}, - {&sequence{block: 0xFF000000, count: 0}, -1, -1}, + {&sequence{block: 0xFF000000, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0xFF000000, count: 1}, 1, 0}, {&sequence{block: 0xFF000000, count: 100}, 1, 0}, - {&sequence{block: 0xFF800000, count: 0}, -1, -1}, + {&sequence{block: 0xFF800000, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0xFF800000, count: 1}, 1, 1}, {&sequence{block: 0xFF800000, count: 100}, 1, 1}, - {&sequence{block: 0xFFC0FF00, count: 0}, -1, -1}, + {&sequence{block: 0xFFC0FF00, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0xFFC0FF00, count: 1}, 1, 2}, {&sequence{block: 0xFFC0FF00, count: 100}, 1, 2}, - {&sequence{block: 0xFFE0FF00, count: 0}, -1, -1}, + {&sequence{block: 0xFFE0FF00, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0xFFE0FF00, count: 1}, 1, 3}, {&sequence{block: 0xFFE0FF00, count: 100}, 1, 3}, - {&sequence{block: 0xFFFEFF00, count: 0}, -1, -1}, + {&sequence{block: 0xFFFEFF00, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0xFFFEFF00, count: 1}, 1, 7}, {&sequence{block: 0xFFFEFF00, count: 100}, 1, 7}, - {&sequence{block: 0xFFFFC0FF, count: 0}, -1, -1}, + {&sequence{block: 0xFFFFC0FF, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0xFFFFC0FF, count: 1}, 2, 2}, {&sequence{block: 0xFFFFC0FF, count: 100}, 2, 2}, - {&sequence{block: 0xFFFFFF00, count: 0}, -1, -1}, + {&sequence{block: 0xFFFFFF00, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0xFFFFFF00, count: 1}, 3, 0}, {&sequence{block: 0xFFFFFF00, count: 100}, 3, 0}, - {&sequence{block: 0xFFFFFFFE, count: 0}, -1, -1}, + {&sequence{block: 0xFFFFFFFE, count: 0}, invalidPos, invalidPos}, {&sequence{block: 0xFFFFFFFE, count: 1}, 3, 7}, {&sequence{block: 0xFFFFFFFE, count: 100}, 3, 7}, - {&sequence{block: 0xFFFFFFFF, count: 0}, -1, -1}, - {&sequence{block: 0xFFFFFFFF, count: 1}, -1, -1}, - {&sequence{block: 0xFFFFFFFF, count: 100}, -1, -1}, + {&sequence{block: 0xFFFFFFFF, count: 0}, invalidPos, invalidPos}, + {&sequence{block: 0xFFFFFFFF, count: 1}, invalidPos, invalidPos}, + {&sequence{block: 0xFFFFFFFF, count: 100}, invalidPos, invalidPos}, } for n, i := range input { - b, bb := i.head.getAvailableBit() + b, bb, err := i.head.getAvailableBit() if b != i.bytePos || bb != i.bitPos { - t.Fatalf("Error in sequence.getAvailableBit() (%d).\nExp: (%d, %d)\nGot: (%d, %d),", n, i.bytePos, i.bitPos, b, bb) + t.Fatalf("Error in sequence.getAvailableBit() (%d).\nExp: (%d, %d)\nGot: (%d, %d), err: %v", n, i.bytePos, i.bitPos, b, bb, err) } } } @@ -144,10 +144,10 @@ func TestSequenceCopy(t *testing.T) { func TestGetFirstAvailable(t *testing.T) { input := []struct { mask *sequence - bytePos int - bitPos int + bytePos uint32 + bitPos uint32 }{ - {&sequence{block: 0xffffffff, count: 2048}, -1, -1}, + {&sequence{block: 0xffffffff, count: 2048}, invalidPos, invalidPos}, {&sequence{block: 0x0, count: 8}, 0, 0}, {&sequence{block: 0x80000000, count: 8}, 0, 1}, {&sequence{block: 0xC0000000, count: 8}, 0, 2}, @@ -191,18 +191,18 @@ func TestGetFirstAvailable(t *testing.T) { func TestFindSequence(t *testing.T) { input := []struct { head *sequence - bytePos int + bytePos uint32 precBlocks uint32 - inBlockBytePos int + inBlockBytePos uint32 }{ - {&sequence{block: 0xffffffff, count: 0}, 0, 0, -1}, - {&sequence{block: 0xffffffff, count: 0}, 31, 0, -1}, - {&sequence{block: 0xffffffff, count: 0}, 100, 0, -1}, + {&sequence{block: 0xffffffff, count: 0}, 0, 0, invalidPos}, + {&sequence{block: 0xffffffff, count: 0}, 31, 0, invalidPos}, + {&sequence{block: 0xffffffff, count: 0}, 100, 0, invalidPos}, {&sequence{block: 0x0, count: 1}, 0, 0, 0}, {&sequence{block: 0x0, count: 1}, 1, 0, 1}, - {&sequence{block: 0x0, count: 1}, 31, 0, -1}, - {&sequence{block: 0x0, count: 1}, 60, 0, -1}, + {&sequence{block: 0x0, count: 1}, 31, 0, invalidPos}, + {&sequence{block: 0x0, count: 1}, 60, 0, invalidPos}, {&sequence{block: 0xffffffff, count: 10}, 0, 0, 0}, {&sequence{block: 0xffffffff, count: 10}, 3, 0, 3}, @@ -212,7 +212,7 @@ func TestFindSequence(t *testing.T) { {&sequence{block: 0xffffffff, count: 10}, 39, 9, 3}, {&sequence{block: 0xffffffff, count: 10, next: &sequence{block: 0xcc000000, count: 10}}, 79, 9, 3}, - {&sequence{block: 0xffffffff, count: 10, next: &sequence{block: 0xcc000000, count: 10}}, 80, 0, -1}, + {&sequence{block: 0xffffffff, count: 10, next: &sequence{block: 0xcc000000, count: 10}}, 80, 0, invalidPos}, } for n, i := range input { @@ -226,38 +226,38 @@ func TestFindSequence(t *testing.T) { func TestCheckIfAvailable(t *testing.T) { input := []struct { head *sequence - ordinal int - bytePos int - bitPos int + ordinal uint32 + bytePos uint32 + bitPos uint32 }{ - {&sequence{block: 0xffffffff, count: 0}, 0, -1, -1}, - {&sequence{block: 0xffffffff, count: 0}, 31, -1, -1}, - {&sequence{block: 0xffffffff, count: 0}, 100, -1, -1}, + {&sequence{block: 0xffffffff, count: 0}, 0, invalidPos, invalidPos}, + {&sequence{block: 0xffffffff, count: 0}, 31, invalidPos, invalidPos}, + {&sequence{block: 0xffffffff, count: 0}, 100, invalidPos, invalidPos}, {&sequence{block: 0x0, count: 1}, 0, 0, 0}, {&sequence{block: 0x0, count: 1}, 1, 0, 1}, {&sequence{block: 0x0, count: 1}, 31, 3, 7}, - {&sequence{block: 0x0, count: 1}, 60, -1, -1}, + {&sequence{block: 0x0, count: 1}, 60, invalidPos, invalidPos}, - {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x800000ff, count: 1}}, 31, -1, -1}, - {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x800000ff, count: 1}}, 32, -1, -1}, + {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x800000ff, count: 1}}, 31, invalidPos, invalidPos}, + {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x800000ff, count: 1}}, 32, invalidPos, invalidPos}, {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x800000ff, count: 1}}, 33, 4, 1}, - {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1}}, 33, -1, -1}, + {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1}}, 33, invalidPos, invalidPos}, {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1}}, 34, 4, 2}, {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 55, 6, 7}, - {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 56, -1, -1}, - {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 63, -1, -1}, + {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 56, invalidPos, invalidPos}, + {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 63, invalidPos, invalidPos}, {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 64, 8, 0}, {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 95, 11, 7}, - {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 96, -1, -1}, + {&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 96, invalidPos, invalidPos}, } for n, i := range input { - bytePos, bitPos, _ := checkIfAvailable(i.head, i.ordinal) + bytePos, bitPos, err := checkIfAvailable(i.head, i.ordinal) if bytePos != i.bytePos || bitPos != i.bitPos { - t.Fatalf("Error in (%d) checkIfAvailable(ord:%d). Expected (%d, %d). Got (%d, %d)", n, i.ordinal, i.bytePos, i.bitPos, bytePos, bitPos) + t.Fatalf("Error in (%d) checkIfAvailable(ord:%d). Expected (%d, %d). Got (%d, %d). err: %v", n, i.ordinal, i.bytePos, i.bitPos, bytePos, bitPos, err) } } } @@ -298,8 +298,8 @@ func TestMergeSequences(t *testing.T) { func TestPushReservation(t *testing.T) { input := []struct { mask *sequence - bytePos int - bitPos int + bytePos uint32 + bitPos uint32 newMask *sequence }{ // Create first sequence and fill in 8 addresses starting from address 0 diff --git a/idm/idm.go b/idm/idm.go index 6725275..2fa3cff 100644 --- a/idm/idm.go +++ b/idm/idm.go @@ -73,7 +73,7 @@ func (i *Idm) GetSpecificID(id uint32) error { } for { - bytePos, bitPos, err := i.handle.CheckIfAvailable(int(id - i.start)) + bytePos, bitPos, err := i.handle.CheckIfAvailable(id - i.start) if err != nil { return fmt.Errorf("requested id is not available") } @@ -90,5 +90,5 @@ func (i *Idm) GetSpecificID(id uint32) error { // Release releases the specified id func (i *Idm) Release(id uint32) { ordinal := id - i.start - i.handle.PushReservation(int(ordinal/8), int(ordinal%8), true) + i.handle.PushReservation(ordinal/8, ordinal%8, true) } diff --git a/ipam/allocator.go b/ipam/allocator.go index 6ae6c9d..2fee5ab 100644 --- a/ipam/allocator.go +++ b/ipam/allocator.go @@ -297,7 +297,7 @@ func getInternalSubnets(inSubnet *net.IPNet, internalHostSize int) ([]*net.IPNet for i := 0; i < numIntSubs; i++ { intIP := make([]byte, len(subnet.IP)) copy(intIP, subnet.IP) // IPv6 is too big, just work on the extra portion - addIntToIP(intIP, i< 192.168.53 -func addIntToIP(array []byte, ordinal int) { +func addIntToIP(array []byte, ordinal uint32) { for i := len(array) - 1; i >= 0; i-- { array[i] |= (byte)(ordinal & 0xff) ordinal >>= 8 @@ -607,11 +607,11 @@ func addIntToIP(array []byte, ordinal int) { } // Convert an ordinal to the respective IP address -func ipToInt(ip []byte) int { - value := 0 +func ipToUint32(ip []byte) uint32 { + value := uint32(0) for i := 0; i < len(ip); i++ { j := len(ip) - 1 - i - value += int(ip[i]) << uint(j*8) + value += uint32(ip[i]) << uint(j*8) } return value } diff --git a/ipam/allocator_test.go b/ipam/allocator_test.go index 8e063d7..25bd0b8 100644 --- a/ipam/allocator_test.go +++ b/ipam/allocator_test.go @@ -20,10 +20,10 @@ func getAllocator(t *testing.T, subnet *net.IPNet) *Allocator { } func TestInt2IP2IntConversion(t *testing.T) { - for i := 0; i < 256*256*256; i++ { + for i := uint32(0); i < 256*256*256; i++ { var array [4]byte // new array at each cycle addIntToIP(array[:], i) - j := ipToInt(array[:]) + j := ipToUint32(array[:]) if j != i { t.Fatalf("Failed to convert ordinal %d to IP % x and back to ordinal. Got %d", i, array, j) } @@ -31,14 +31,14 @@ func TestInt2IP2IntConversion(t *testing.T) { } func TestIsValid(t *testing.T) { - list := []int{0, 255, 256, 511, 512, 767, 768} + list := []uint32{0, 255, 256, 511, 512, 767, 768} for _, i := range list { if isValidIP(i) { t.Fatalf("Failed to detect invalid IPv4 ordinal: %d", i) } } - list = []int{1, 254, 257, 258, 510, 513, 769, 770} + list = []uint32{1, 254, 257, 258, 510, 513, 769, 770} for _, i := range list { if !isValidIP(i) { t.Fatalf("Marked valid ipv4 as invalid: %d", i) @@ -474,7 +474,7 @@ func assertInternalSubnet(t *testing.T, hostSize int, bigSubnet, firstSmall, las list, _ := getInternalSubnets(subnet, hostSize) count := 1 ones, bits := subnet.Mask.Size() - diff := bits - ones - hostSize + diff := bits - ones - int(hostSize) if diff > 0 { count <<= uint(diff) }