github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/libnetwork/ipamutils/utils.go (about)

     1  // Package ipamutils provides utility functions for ipam management
     2  package ipamutils
     3  
     4  import (
     5  	"net"
     6  	"sync"
     7  )
     8  
     9  var (
    10  	// PredefinedBroadNetworks contains a list of 31 IPv4 private networks with host size 16 and 12
    11  	// (172.17-31.x.x/16, 192.168.x.x/20) which do not overlap with the networks in `PredefinedGranularNetworks`
    12  	PredefinedBroadNetworks []*net.IPNet
    13  	// PredefinedGranularNetworks contains a list of 64K IPv4 private networks with host size 8
    14  	// (10.x.x.x/24) which do not overlap with the networks in `PredefinedBroadNetworks`
    15  	PredefinedGranularNetworks []*net.IPNet
    16  
    17  	initNetworksOnce sync.Once
    18  )
    19  
    20  // InitNetworks initializes the pre-defined networks used by the built-in IP allocator
    21  func InitNetworks() {
    22  	initNetworksOnce.Do(func() {
    23  		PredefinedBroadNetworks = initBroadPredefinedNetworks()
    24  		PredefinedGranularNetworks = initGranularPredefinedNetworks()
    25  	})
    26  }
    27  
    28  func initBroadPredefinedNetworks() []*net.IPNet {
    29  	pl := make([]*net.IPNet, 0, 31)
    30  	mask := []byte{255, 255, 0, 0}
    31  	for i := 17; i < 32; i++ {
    32  		pl = append(pl, &net.IPNet{IP: []byte{172, byte(i), 0, 0}, Mask: mask})
    33  	}
    34  	mask20 := []byte{255, 255, 240, 0}
    35  	for i := 0; i < 16; i++ {
    36  		pl = append(pl, &net.IPNet{IP: []byte{192, 168, byte(i << 4), 0}, Mask: mask20})
    37  	}
    38  	return pl
    39  }
    40  
    41  func initGranularPredefinedNetworks() []*net.IPNet {
    42  	pl := make([]*net.IPNet, 0, 256*256)
    43  	mask := []byte{255, 255, 255, 0}
    44  	for i := 0; i < 256; i++ {
    45  		for j := 0; j < 256; j++ {
    46  			pl = append(pl, &net.IPNet{IP: []byte{10, byte(i), byte(j), 0}, Mask: mask})
    47  		}
    48  	}
    49  	return pl
    50  }