本文整理汇总了Golang中net.IPNet.Contains方法的典型用法代码示例。如果您正苦于以下问题:Golang IPNet.Contains方法的具体用法?Golang IPNet.Contains怎么用?Golang IPNet.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.IPNet
的用法示例。
在下文中一共展示了IPNet.Contains方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAllocate
func TestAllocate(t *testing.T) {
subnet := net.IPNet{
IP: net.IPv4(0xab, 0xcd, 0xe0, 0x00),
Mask: net.CIDRMask(20, 32),
}
conflicts := map[string]struct{}{}
ipSet := map[string]struct{}{}
// Only 4k IPs, in 0xfffff000. Guaranteed a collision
for i := 0; i < 5000; i++ {
ip, err := allocateIP(ipSet, subnet)
if err != nil {
continue
}
if _, ok := conflicts[ip]; ok {
t.Fatalf("IP Double allocation: 0x%x", ip)
}
require.True(t, subnet.Contains(net.ParseIP(ip)),
fmt.Sprintf("\"%s\" is not in %s", ip, subnet))
conflicts[ip] = struct{}{}
}
assert.Equal(t, len(conflicts), len(ipSet))
if len(conflicts) < 2500 || len(conflicts) > 4096 {
// If the code's working, this is possible but *extremely* unlikely.
// Probably a bug.
t.Errorf("Too few conflicts: %d", len(conflicts))
}
}
示例2: GetIndexedIP
// GetIndexedIP returns a net.IP that is subnet.IP + index in the contiguous IP space.
func GetIndexedIP(subnet *net.IPNet, index int) (net.IP, error) {
ip := addIPOffset(bigForIP(subnet.IP), index)
if !subnet.Contains(ip) {
return nil, fmt.Errorf("can't generate IP with index %d from subnet. subnet too small. subnet: %q", index, subnet)
}
return ip, nil
}
示例3: getSuitableAddrs
func getSuitableAddrs(addrs []*address, v4, v6, linklocal, loopback bool, re *regexp.Regexp, mask *net.IPNet) ([]*address, error) {
ret := []*address{}
for _, a := range addrs {
if a.IsLoopback() && !loopback {
continue
}
if !v6 && a.IsV6() {
continue
}
if !v4 && a.IsV4() {
continue
}
if !linklocal && a.IsLinkLocalUnicast() {
continue
}
if !loopback && a.IsLoopback() {
continue
}
if re != nil {
if !re.MatchString(a.String()) {
continue
}
}
if mask != nil {
if !mask.Contains(a.IP) {
continue
}
}
ret = append(ret, a)
}
if len(ret) == 0 {
return nil, errors.New("unable to find suitable address")
}
return ret, nil
}
示例4: NextSubnetIP
// NextSubnetIP returns the next available IP address in a given subnet.
func NextSubnetIP(subnet *net.IPNet, ipsInUse []net.IP) (net.IP, error) {
ones, bits := subnet.Mask.Size()
subnetMaskUint32 := ipUint32(net.IP(subnet.Mask))
inUse := big.NewInt(0)
for _, ip := range ipsInUse {
if !subnet.Contains(ip) {
continue
}
index := ipIndex(ip, subnetMaskUint32)
inUse = inUse.SetBit(inUse, index, 1)
}
// Now iterate through all addresses in the subnet and return the
// first address that is not in use. We start at the first non-
// reserved address, and stop short of the last address in the
// subnet (i.e. all non-mask bits set), which is the broadcast
// address for the subnet.
n := ipUint32(subnet.IP)
for i := reservedAddressRangeEnd + 1; i < (1<<uint64(bits-ones) - 1); i++ {
ip := uint32IP(n + uint32(i))
if !ip.IsGlobalUnicast() {
continue
}
index := ipIndex(ip, subnetMaskUint32)
if inUse.Bit(index) == 0 {
return ip, nil
}
}
return nil, errors.Errorf("no addresses available in %s", subnet)
}
示例5: hasIP6Connected
// hasIP6Connected parses the list of remote addresses in /proc/net/{tcp,udp}6 or in
// /proc/<pid>/net/{tcp,udp}6 and returns addresses that are contained within
// the ipnet submitted. It always uses CIDR inclusion, even when only
// searching for a single IP (but assuming a /128 bitmask).
// Remote addresses exposed in /proc are in hexadecimal notation, and converted into byte slices
// to use in ipnet.Contains()
func hasIP6Connected(ip net.IP, ipnet *net.IPNet) (found bool, elements []element, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("hasIP6Connected(): %v", e)
}
}()
lns, err := procIP6Entries()
if err != nil {
panic(err)
}
// if the ipnet is nil, assume that its a full 128bits mask
if ipnet == nil {
ipnet = new(net.IPNet)
ipnet.IP = ip
ipnet.Mask = net.CIDRMask(net.IPv6len*8, net.IPv6len*8)
}
for _, ipent := range lns {
fields := strings.Fields(ipent.line)
if len(fields) < 4 {
panic("/proc doesn't respect the expected format")
}
remote := strings.Split(fields[2], ":")
if len(remote) != 2 {
panic("remote isn't in the form <ip>:<port>")
}
remoteIP := hexToIP6(remote[0])
if remoteIP == nil {
panic("failed to convert remote IP")
}
// if we've got a match, store the element
if ipnet.Contains(remoteIP) {
var el element
el.RemoteAddr = remoteIP.String()
remotePort, err := strconv.ParseUint(remote[1], 16, 16)
if err != nil {
panic("failed to convert remote port")
}
el.RemotePort = float64(remotePort)
local := strings.Split(fields[1], ":")
if len(local) != 2 {
panic("local isn't in the form <ip>:<port>")
}
localAddr := hexToIP6(local[0])
if localAddr == nil {
panic("failed to convert local ip")
}
el.LocalAddr = localAddr.String()
localPort, err := strconv.ParseUint(local[1], 16, 16)
if err != nil {
panic("failed to convert local port")
}
el.LocalPort = float64(localPort)
el.Namespace = ipent.nsIdentifier
elements = append(elements, el)
found = true
}
stats.Examined++
}
return
}
示例6: getBeginingAndEndIndices
func (s *cidrSet) getBeginingAndEndIndices(cidr *net.IPNet) (begin, end int, err error) {
begin, end = 0, s.maxCIDRs
cidrMask := cidr.Mask
maskSize, _ := cidrMask.Size()
if !s.clusterCIDR.Contains(cidr.IP.Mask(s.clusterCIDR.Mask)) && !cidr.Contains(s.clusterCIDR.IP.Mask(cidr.Mask)) {
return -1, -1, fmt.Errorf("cidr %v is out the range of cluster cidr %v", cidr, s.clusterCIDR)
}
if s.clusterMaskSize < maskSize {
subNetMask := net.CIDRMask(s.subNetMaskSize, 32)
begin, err = s.getIndexForCIDR(&net.IPNet{
IP: cidr.IP.To4().Mask(subNetMask),
Mask: subNetMask,
})
if err != nil {
return -1, -1, err
}
ip := make([]byte, 4)
ipInt := binary.BigEndian.Uint32(cidr.IP) | (^binary.BigEndian.Uint32(cidr.Mask))
binary.BigEndian.PutUint32(ip, ipInt)
end, err = s.getIndexForCIDR(&net.IPNet{
IP: net.IP(ip).To4().Mask(subNetMask),
Mask: subNetMask,
})
if err != nil {
return -1, -1, err
}
}
return begin, end, nil
}
示例7: networkAddressForSubnet
func networkAddressForSubnet(subnet *net.IPNet) (net.IP, string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return net.IP{}, "", err
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
if subnet.Contains(ip) {
return ip, iface.Name, nil
}
}
}
return net.IP{}, "", fmt.Errorf("No address found in subnet")
}
示例8: NetList
// NetList : subnet helper function
func NetList(ip net.IP, subnet net.IP) (IPlist []net.IP) {
//ip, ipnet, err := net.ParseCIDR(cidrNet)
mask := net.IPv4Mask(subnet[0], subnet[1], subnet[2], subnet[3])
ipnet := net.IPNet{ip, mask}
for ip := ip.Mask(mask); ipnet.Contains(ip); incIP(ip) {
IPlist = append(IPlist, net.IP{ip[0], ip[1], ip[2], ip[3]})
}
return
}
示例9: IPNetEqual
// IPNetEqual checks if the two input IPNets are representing the same subnet.
// For example,
// 10.0.0.1/24 and 10.0.0.0/24 are the same subnet.
// 10.0.0.1/24 and 10.0.0.0/25 are not the same subnet.
func IPNetEqual(ipnet1, ipnet2 *net.IPNet) bool {
if ipnet1 == nil || ipnet2 == nil {
return false
}
if reflect.DeepEqual(ipnet1.Mask, ipnet2.Mask) && ipnet1.Contains(ipnet2.IP) && ipnet2.Contains(ipnet1.IP) {
return true
}
return false
}
示例10: NetworkOverlaps
// Detects overlap between one IPNet and another
func NetworkOverlaps(netX *net.IPNet, netY *net.IPNet) bool {
if firstIP, _ := NetworkRange(netX); netY.Contains(firstIP) {
return true
}
if firstIP, _ := NetworkRange(netY); netX.Contains(firstIP) {
return true
}
return false
}
示例11: contains
// Checks whether the passed subnet is a superset or subset of any of the subset in this config db
func (aSpace *addrSpace) contains(space string, nw *net.IPNet) bool {
for k, v := range aSpace.subnets {
if space == k.AddressSpace && k.ChildSubnet == "" {
if nw.Contains(v.Pool.IP) || v.Pool.Contains(nw.IP) {
return true
}
}
}
return false
}
示例12: SpanningCIDR
// SpanningCIDR computes network covers given IP addresses
func SpanningCIDR(first, last net.IP) *net.IPNet {
_, bits := last.DefaultMask().Size()
var network net.IPNet
for ones := bits; !network.Contains(first); ones-- {
network.Mask = net.CIDRMask(ones, bits)
network.IP = last.Mask(network.Mask)
}
return &network
}
示例13: networkOverlaps
// Detects overlap between one IPNet and another
func networkOverlaps(netX *net.IPNet, netY *net.IPNet) bool {
firstIP, _ := networkRange(netX)
if netY.Contains(firstIP) {
return true
}
firstIP, _ = networkRange(netY)
if netX.Contains(firstIP) {
return true
}
return false
}
示例14: InNetwork
func (d *Device) InNetwork(network net.IPNet) bool {
if network.Contains(d.IP) {
return true
}
for _, a := range d.Reverse {
if !network.Contains(a) {
continue
}
return true
}
return false
}
示例15: filterOutServiceRange
// Marks all CIDRs with subNetMaskSize that belongs to serviceCIDR as used,
// so that they won't be assignable.
func (r *rangeAllocator) filterOutServiceRange(serviceCIDR *net.IPNet) {
// Checks if service CIDR has a nonempty intersection with cluster CIDR. It is the case if either
// clusterCIDR contains serviceCIDR with clusterCIDR's Mask applied (this means that clusterCIDR contains serviceCIDR)
// or vice versa (which means that serviceCIDR contains clusterCIDR).
if !r.clusterCIDR.Contains(serviceCIDR.IP.Mask(r.clusterCIDR.Mask)) && !serviceCIDR.Contains(r.clusterCIDR.IP.Mask(serviceCIDR.Mask)) {
return
}
if err := r.cidrs.occupy(serviceCIDR); err != nil {
glog.Errorf("Error filtering out service cidr %v: %v", serviceCIDR, err)
}
}