当前位置: 首页>>代码示例>>Golang>>正文


Golang net.ParseCIDR函数代码示例

本文整理汇总了Golang中net.ParseCIDR函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseCIDR函数的具体用法?Golang ParseCIDR怎么用?Golang ParseCIDR使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ParseCIDR函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: GetAvailableGwAddress

func GetAvailableGwAddress(bridgeIP string) (gwaddr string, err error) {
	if len(bridgeIP) != 0 {
		_, _, err = net.ParseCIDR(bridgeIP)
		if err != nil {
			return
		}
		gwaddr = bridgeIP
	} else {
		for _, addr := range gatewayAddrs {
			_, dockerNetwork, err := net.ParseCIDR(addr)
			if err != nil {
				return "", err
			}
			if err = CheckRouteOverlaps(dockerNetwork); err != nil {
				continue
			}
			gwaddr = addr
			break
		}
	}
	if gwaddr == "" {
		return "", errors.New("No available gateway addresses")
	}
	return gwaddr, nil
}
开发者ID:anlaneg,项目名称:socketplane,代码行数:25,代码来源:bridge.go

示例2: init

func init() {
	// Add each private block
	privateBlocks = make([]*net.IPNet, 3)
	_, block, err := net.ParseCIDR("10.0.0.0/8")
	if err != nil {
		panic(fmt.Sprintf("Bad cidr. Got %v", err))
	}
	privateBlocks[0] = block

	_, block, err = net.ParseCIDR("172.16.0.0/12")
	if err != nil {
		panic(fmt.Sprintf("Bad cidr. Got %v", err))
	}
	privateBlocks[1] = block

	_, block, err = net.ParseCIDR("192.168.0.0/16")
	if err != nil {
		panic(fmt.Sprintf("Bad cidr. Got %v", err))
	}
	privateBlocks[2] = block

	var n int64
	binary.Read(crand.Reader, binary.BigEndian, &n)

	randSrc = rand.NewSource(n)

	randGen = rand.New(randSrc)
}
开发者ID:40a,项目名称:vega,代码行数:28,代码来源:util.go

示例3: getIp

// determine the IP address of the container
func getIp(usePublic bool) string {
	interfaces, _ := net.Interfaces()
	var ips []net.IP
	_, loopback, _ := net.ParseCIDR("127.0.0.0/8")
	for _, intf := range interfaces {
		ipAddrs, _ := intf.Addrs()
		// We're assuming each interface has one IP here because neither Docker
		// nor Triton sets up IP aliasing.
		ipAddr, _, _ := net.ParseCIDR(ipAddrs[0].String())
		if !loopback.Contains(ipAddr) {
			ips = append(ips, ipAddr)
		}
	}
	var ip string
	for _, ipAddr := range ips {
		isPublic := isPublicIp(ipAddr)
		if isPublic && usePublic {
			ip = ipAddr.String()
			break
		} else if !isPublic && !usePublic {
			ip = ipAddr.String()
			break
		}
	}
	return ip
}
开发者ID:frascuchon,项目名称:containerbuddy,代码行数:27,代码来源:config.go

示例4: ExamplePrefix_subnettingAndAggregation

func ExamplePrefix_subnettingAndAggregation() {
	_, n, err := net.ParseCIDR("192.168.0.0/24")
	if err != nil {
		log.Fatal(err)
	}
	p := ipaddr.NewPrefix(n)
	fmt.Println(p.IP, p.Last(), p.Len(), p.Mask, p.Hostmask())
	ps := p.Subnets(2)
	for _, p := range ps {
		fmt.Println(p)
	}
	_, n, err = net.ParseCIDR("192.168.100.0/24")
	if err != nil {
		log.Fatal(err)
	}
	ps = append(ps, *ipaddr.NewPrefix(n))
	fmt.Println(ipaddr.Aggregate(ps[2:]))
	// Output:
	// 192.168.0.0 192.168.0.255 24 ffffff00 000000ff
	// 192.168.0.0/26
	// 192.168.0.64/26
	// 192.168.0.128/26
	// 192.168.0.192/26
	// [192.168.0.128/25 192.168.100.0/24]
}
开发者ID:ijayasin,项目名称:ipaddr,代码行数:25,代码来源:example_test.go

示例5: parseIpv4RoutingLine

func parseIpv4RoutingLine(line string) (route Route, err error) {
	t := strings.Fields(line)

	if strings.HasPrefix(t[1], "link") {
		err = errors.New("Link-local route")
		return
	}

	raw_dest := t[0]
	if raw_dest == "default" {
		raw_dest = "0.0.0.0/0"
	}

	_, destination, err := net.ParseCIDR(raw_dest)
	if err != nil {
		return
	}

	raw_gateway := t[1]
	gateway, _, err := net.ParseCIDR(raw_gateway + "/32")
	if err != nil {
		return
	}

	route.Destination = *destination
	route.Gateway = gateway

	return
}
开发者ID:r3boot,项目名称:rlib,代码行数:29,代码来源:rib_freebsd.go

示例6: newTestServerWithDefaults

func newTestServerWithDefaults() (*Server, *MockRingBuilderThings) {
	b := ring.NewBuilder(64)
	b.SetReplicaCount(3)
	b.AddNode(true, 1, []string{"server1", "zone1"}, []string{"1.2.3.4:56789"}, "server1|meta one", []byte("Conf Thing1"))
	b.AddNode(true, 1, []string{"dummy1", "zone42"}, []string{"1.42.42.42:56789"}, "dummy1|meta one", []byte("Dummy Conf"))
	ring := b.Ring()

	rbytes := []byte("imnotaring")
	bbytes := []byte("imnotbuilder")

	mock := &MockRingBuilderThings{
		builderPath:  "/tmp/test.builder",
		ringPath:     "/tmp/test.ring",
		ring:         ring,
		ringbytes:    &rbytes,
		builder:      b,
		builderbytes: &bbytes,
		managedNodes: make(map[uint64]ManagedNode, 0),
		slaves:       make([]*RingSlave, 0),
		changeChan:   make(chan *changeMsg, 1),
	}
	s := newTestServer(&Config{}, "test", mock)
	_, netblock, _ := net.ParseCIDR("10.0.0.0/24")
	s.netlimits = append(s.netlimits, netblock)
	_, netblock, _ = net.ParseCIDR("1.2.3.0/24")
	s.netlimits = append(s.netlimits, netblock)
	return s, mock
}
开发者ID:pandemicsyn,项目名称:syndicate,代码行数:28,代码来源:syndicate_test.go

示例7: TestServer_ValidNodeIP

func TestServer_ValidNodeIP(t *testing.T) {
	s, _ := newTestServerWithDefaults()

	_, netblock, _ := net.ParseCIDR("10.0.0.0/24")
	s.netlimits = append(s.netlimits, netblock)

	loopback := "127.0.0.1/32"
	multicast := "224.0.0.1/32"
	inlimit := "10.0.0.1/32"
	badips := []string{
		loopback,
		multicast,
		"2.2.2.2/32",
	}
	for _, v := range badips {
		i, _, _ := net.ParseCIDR(v)
		if s.validNodeIP(i) {
			t.Errorf("validNodeIP(%s) should have been false but was true", v)
		}
	}
	log.Println(inlimit)
	okip, _, err := net.ParseCIDR(inlimit)
	if err != nil {
		panic(err)
	}
	if !s.validNodeIP(okip) {
		t.Errorf("validNodeIP(%s) should have been true but was false", okip)
	}
}
开发者ID:pandemicsyn,项目名称:syndicate,代码行数:29,代码来源:syndicate_test.go

示例8: Subset

// Subset checks if the IPs belonging to a given CIDR block is a subset of IPs
// belonging to another CIDR block.
func Subset(cidr1, cidr2 string) (bool, error) {
	if cidr1 == "" {
		return false, fmt.Errorf("missing CIDR to be checked against")
	}

	if cidr2 == "" {
		return false, fmt.Errorf("missing CIDR that needs to be checked")
	}

	ip1, net1, err := net.ParseCIDR(cidr1)
	if err != nil {
		return false, fmt.Errorf("failed to parse the CIDR to be checked against: %q", err)
	}

	zeroAddr := false
	if ip := ip1.To4(); ip != nil && ip.Equal(net.IPv4zero) {
		zeroAddr = true
	}
	if ip := ip1.To16(); ip != nil && ip.Equal(net.IPv6zero) {
		zeroAddr = true
	}

	maskLen1, _ := net1.Mask.Size()
	if !zeroAddr && maskLen1 == 0 {
		return false, fmt.Errorf("CIDR to be checked against is not in its canonical form")
	}

	ip2, net2, err := net.ParseCIDR(cidr2)
	if err != nil {
		return false, fmt.Errorf("failed to parse the CIDR that needs to be checked: %q", err)
	}

	zeroAddr = false
	if ip := ip2.To4(); ip != nil && ip.Equal(net.IPv4zero) {
		zeroAddr = true
	}
	if ip := ip2.To16(); ip != nil && ip.Equal(net.IPv6zero) {
		zeroAddr = true
	}

	maskLen2, _ := net2.Mask.Size()
	if !zeroAddr && maskLen2 == 0 {
		return false, fmt.Errorf("CIDR that needs to be checked is not in its canonical form")
	}

	// If the mask length of the CIDR that needs to be checked is smaller
	// then the mask length of the CIDR to be checked against, then the
	// former will encompass more IPs than the latter, and hence can't be a
	// subset of the latter.
	if maskLen2 < maskLen1 {
		return false, nil
	}

	belongs, err := IPBelongsToCIDR(net2.IP.String(), cidr1)
	if err != nil {
		return false, err
	}

	return belongs, nil
}
开发者ID:quixoten,项目名称:vault,代码行数:62,代码来源:cidr.go

示例9: TestCompareIPNet

func TestCompareIPNet(t *testing.T) {
	if CompareIPNet(nil, nil) == false {
		t.Fatalf("Failed to detect two nil net.IPNets are equal")
	}

	_, net1, _ := net.ParseCIDR("192.168.30.22/24")
	if CompareIPNet(net1, net1) == false {
		t.Fatalf("Failed to detect same net.IPNet pointers equality")
	}

	_, net2, _ := net.ParseCIDR("192.168.30.22/24")
	if CompareIPNet(net1, net2) == false {
		t.Fatalf("Failed to detect same net.IPNet object equality")
	}

	_, net3, _ := net.ParseCIDR("192.168.30.33/24")
	if CompareIPNet(net1, net3) == false {
		t.Fatalf("Failed to detect semantically equivalent net.IPNets")
	}

	_, net3, _ = net.ParseCIDR("192.168.31.33/24")
	if CompareIPNet(net2, net3) == true {
		t.Fatalf("Failed to detect different net.IPNets")
	}
}
开发者ID:choldrim,项目名称:docker,代码行数:25,代码来源:utils_test.go

示例10: RunNodeController

// RunNodeController starts the node controller
// TODO: handle node CIDR and route allocation
func (c *MasterConfig) RunNodeController() {
	s := c.ControllerManager

	// this cidr has been validated already
	_, clusterCIDR, _ := net.ParseCIDR(s.ClusterCIDR)
	_, serviceCIDR, _ := net.ParseCIDR(s.ServiceCIDR)

	controller, err := nodecontroller.NewNodeController(
		c.CloudProvider,
		clientadapter.FromUnversionedClient(c.KubeClient),
		s.PodEvictionTimeout.Duration,

		flowcontrol.NewTokenBucketRateLimiter(s.DeletingPodsQps, int(s.DeletingPodsBurst)),
		flowcontrol.NewTokenBucketRateLimiter(s.DeletingPodsQps, int(s.DeletingPodsBurst)), // upstream uses the same ones too

		s.NodeMonitorGracePeriod.Duration,
		s.NodeStartupGracePeriod.Duration,
		s.NodeMonitorPeriod.Duration,

		clusterCIDR,

		serviceCIDR,
		int(s.NodeCIDRMaskSize),

		s.AllocateNodeCIDRs,
	)
	if err != nil {
		glog.Fatalf("Unable to start node controller: %v", err)
	}

	controller.Run(s.NodeSyncPeriod.Duration)
}
开发者ID:rawlingsj,项目名称:gofabric8,代码行数:34,代码来源:master.go

示例11: validateClusterNetwork

func (oc *OvsController) validateClusterNetwork(networkCIDR string, subnetsInUse []string, hostIPNets []*net.IPNet) error {
	clusterIP, clusterIPNet, err := net.ParseCIDR(networkCIDR)
	if err != nil {
		return fmt.Errorf("Failed to parse network address: %s", networkCIDR)
	}

	errList := []error{}
	for _, ipNet := range hostIPNets {
		if ipNet.Contains(clusterIP) {
			errList = append(errList, fmt.Errorf("Error: Cluster IP: %s conflicts with host network: %s", clusterIP.String(), ipNet.String()))
		}
		if clusterIPNet.Contains(ipNet.IP) {
			errList = append(errList, fmt.Errorf("Error: Host network with IP: %s conflicts with cluster network: %s", ipNet.IP.String(), networkCIDR))
		}
	}

	for _, netStr := range subnetsInUse {
		subnetIP, _, err := net.ParseCIDR(netStr)
		if err != nil {
			errList = append(errList, fmt.Errorf("Failed to parse network address: %s", netStr))
			continue
		}
		if !clusterIPNet.Contains(subnetIP) {
			errList = append(errList, fmt.Errorf("Error: Existing node subnet: %s is not part of cluster network: %s", netStr, networkCIDR))
		}
	}
	return kerrors.NewAggregate(errList)
}
开发者ID:vikaslaad,项目名称:origin,代码行数:28,代码来源:common.go

示例12: TestCheckRouteOverlaps

func TestCheckRouteOverlaps(t *testing.T) {
	teardown := setUp(t)
	defer teardown()

	_, bad, _ := net.ParseCIDR("10.254.44.1/24")
	_, good, _ := net.ParseCIDR("1.1.1.1/24")

	var err error

	if err = SetInterfaceIp(testIface, "10.254.44.1/24"); err != nil {
		t.Fatal(err)
	}
	if err = InterfaceUp(testIface); err != nil {
		t.Fatal(err)
	}

	err = CheckRouteOverlaps(bad)
	if err != ErrNetworkOverlaps {
		t.Fatal("network should be overlapping")
	}

	err = CheckRouteOverlaps(good)
	if err != nil {
		t.Fatal("There should be no errors here")
	}
}
开发者ID:anlaneg,项目名称:socketplane,代码行数:26,代码来源:utils_test.go

示例13: RequestAddress

func (i *ipam) RequestAddress(rq *ipamapi.RequestAddressRequest) (res *ipamapi.RequestAddressResponse, err error) {
	Log.Debugln("RequestAddress %v", rq)
	defer func() { Log.Debugln("RequestAddress returned res: %v, err: %v", res, err) }()
	options := rq.Options
	macAddr, err := net.ParseMAC(options[netlabel.MacAddress])
	if err != nil {
		err = fmt.Errorf("Mac address not understood %v", options[netlabel.MacAddress])
		return
	}
	parts := strings.Split(rq.PoolID, "-")
	if len(parts) != 3 || parts[0] != PoolName {
		err = fmt.Errorf("Unrecognized pool ID: %s", rq.PoolID)
		return
	}
	var subnet, iprange *net.IPNet
	if _, subnet, err = net.ParseCIDR(parts[1]); err != nil {
		return
	}
	if _, iprange, err = net.ParseCIDR(parts[2]); err != nil {
		return
	}
	Log.Debugf("We should query DHCP with: mac %v, subnet %v, iprange %v", macAddr, subnet, iprange)
	res = &ipamapi.RequestAddressResponse{
		Address: "172.13.0.84/24",
		Data: map[string]string{
			"ohay": "Good day to you too, sir!",
		},
	}
	return
}
开发者ID:xytis,项目名称:polyp,代码行数:30,代码来源:driver.go

示例14: AssertNoOverlap

func AssertNoOverlap(CIDRx string, CIDRy string, t *testing.T) {
	_, netX, _ := net.ParseCIDR(CIDRx)
	_, netY, _ := net.ParseCIDR(CIDRy)
	if NetworkOverlaps(netX, netY) {
		t.Errorf("%v and %v should not overlap", netX, netY)
	}
}
开发者ID:curtisz,项目名称:libnetwork,代码行数:7,代码来源:utils_test.go

示例15: init

func init() {
	// Add each private block
	privateBlocks = make([]*net.IPNet, 4)

	_, block, err := net.ParseCIDR("10.0.0.0/8")
	if err != nil {
		panic(fmt.Sprintf("Bad cidr. Got %v", err))
	}
	privateBlocks[0] = block

	_, block, err = net.ParseCIDR("172.16.0.0/12")
	if err != nil {
		panic(fmt.Sprintf("Bad cidr. Got %v", err))
	}
	privateBlocks[1] = block

	_, block, err = net.ParseCIDR("192.168.0.0/16")
	if err != nil {
		panic(fmt.Sprintf("Bad cidr. Got %v", err))
	}
	privateBlocks[2] = block

	_, block, err = net.ParseCIDR("100.64.0.0/10")
	if err != nil {
		panic(fmt.Sprintf("Bad cidr. Got %v", err))
	}
	privateBlocks[3] = block
}
开发者ID:ffung,项目名称:consul,代码行数:28,代码来源:util.go


注:本文中的net.ParseCIDR函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。