mirror of
https://github.com/clearlinux/libnetwork.git
synced 2026-09-01 02:44:45 +00:00
bitseq to only handle and return unsigned types
Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
+11
-11
@@ -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<<uint(internalHostSize))
|
||||
addIntToIP(intIP, uint32(i<<uint(internalHostSize)))
|
||||
subnetList[i] = &net.IPNet{IP: intIP, Mask: intMask}
|
||||
}
|
||||
}
|
||||
@@ -431,7 +431,7 @@ func (a *Allocator) Release(addrSpace AddressSpace, address net.IP) {
|
||||
sub := subKey.canonicalChildSubnet()
|
||||
if sub.Contains(address) {
|
||||
// Retrieve correspondent ordinal in the subnet
|
||||
ordinal := ipToInt(getHostPortionIP(address, sub))
|
||||
ordinal := ipToUint32(getHostPortionIP(address, sub))
|
||||
// Release it
|
||||
for {
|
||||
var err error
|
||||
@@ -509,8 +509,8 @@ func (a *Allocator) getSubnetList(addrSpace AddressSpace, ver ipVersion) []subne
|
||||
|
||||
func (a *Allocator) getAddress(subnet *net.IPNet, bitmask *bitseq.Handle, prefAddress net.IP, ver ipVersion) (net.IP, error) {
|
||||
var (
|
||||
bytePos, bitPos int
|
||||
ordinal int
|
||||
bytePos, bitPos uint32
|
||||
ordinal uint32
|
||||
err error
|
||||
)
|
||||
|
||||
@@ -522,7 +522,7 @@ func (a *Allocator) getAddress(subnet *net.IPNet, bitmask *bitseq.Handle, prefAd
|
||||
if prefAddress == nil {
|
||||
bytePos, bitPos, err = bitmask.GetFirstAvailable()
|
||||
} else {
|
||||
ordinal = ipToInt(getHostPortionIP(prefAddress, subnet))
|
||||
ordinal = ipToUint32(getHostPortionIP(prefAddress, subnet))
|
||||
bytePos, bitPos, err = bitmask.CheckIfAvailable(ordinal)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -568,7 +568,7 @@ func (a *Allocator) DumpDatabase() {
|
||||
|
||||
// It generates the ip address in the passed subnet specified by
|
||||
// the passed host address ordinal
|
||||
func generateAddress(ordinal int, network *net.IPNet) net.IP {
|
||||
func generateAddress(ordinal uint32, network *net.IPNet) net.IP {
|
||||
var address [16]byte
|
||||
|
||||
// Get network portion of IP
|
||||
@@ -592,14 +592,14 @@ func getAddressVersion(ip net.IP) ipVersion {
|
||||
}
|
||||
|
||||
// .0 and .255 will return false
|
||||
func isValidIP(i int) bool {
|
||||
func isValidIP(i uint32) bool {
|
||||
lastByte := i & 0xff
|
||||
return lastByte != 0xff && lastByte != 0
|
||||
}
|
||||
|
||||
// Adds the ordinal IP to the current array
|
||||
// 192.168.0.0 + 53 => 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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user