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


Golang net.IPv4Mask函数代码示例

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


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

示例1: TestRouteTableCMD

func TestRouteTableCMD(t *testing.T) {
	cmds := [][]string{
		generateCMD(net.IPNet{
			net.IPv4(166, 111, 8, 28),
			net.IPv4Mask(255, 255, 255, 0),
		}, net.IPv4(166, 111, 8, 1), true),
		generateCMD(net.IPNet{
			net.IPv4(0, 0, 0, 0),
			net.IPv4Mask(0, 0, 0, 0),
		}, net.IPv4(166, 111, 8, 1), false),
		generateCMD(net.IPNet{
			net.IPv4(10, 0, 0, 0),
			net.IPv4Mask(255, 255, 255, 255),
		}, net.IPv4(166, 111, 8, 1), false),
	}
	assert_cmd := func(cmd []string, str string) {
		if strings.Join(cmd, " ") != str {
			t.Errorf("Error route CMD, expect %s\n", str)
		}
	}
	if runtime.GOOS == "darwin" {
		assert_cmd(cmds[0], "route delete -net 166.111.8.28 166.111.8.1 255.255.255.0")
		assert_cmd(cmds[1], "route add -net 0.0.0.0 166.111.8.1 0.0.0.0")
		assert_cmd(cmds[2], "route add -net 10.0.0.0 166.111.8.1 255.255.255.255")
	} else if runtime.GOOS == "linux" {
		assert_cmd(cmds[0], "ip route del 166.111.8.28/24")
		assert_cmd(cmds[1], "ip route add 0.0.0.0/0 via 166.111.8.1")
		assert_cmd(cmds[2], "ip route add 10.0.0.0/32 via 166.111.8.1")
	}
}
开发者ID:blahgeek,项目名称:justvpn,代码行数:30,代码来源:route_table_test.go

示例2: TestWorkerDrop

// Make sure we drop a worker after a connection is severed
func TestWorkerDrop(t *testing.T) {
	// Start up server listening on our channel based connection
	s := NewServerState()

	conn := newChannelReadWriter()
	mc := NewMessageConn(conn, time.Duration(10)*time.Second)

	go s.handleConnection(mc)

	// Now lets connect a worker
	ws := WorkerState{
		Host: "smith",
		Addrs: []net.IPNet{
			{net.IPv4(192, 1, 1, 1), net.IPv4Mask(255, 255, 255, 0)},
		},
		Port:     56,
		Capacity: 1,
		Load:     0,
	}

	clientAddrs := []net.IPNet{
		{net.IPv4(192, 1, 1, 2), net.IPv4Mask(255, 255, 255, 0)},
	}

	mc.Send(ws)

	// We block until we are able to find a worker, which means our connection
	// was successful
	var wr WorkerResponse

	for {
		var err error

		wr, err = s.sch.findWorker(clientAddrs)

		if err == nil {
			break
		}
	}

	// Make sure we have the right host back
	if wr.Host != "smith" {
		t.Error("Bad worker")
	}

	// Now lets close the connection which should drop the worker
	conn.Close()

	// Now lets wait for that drop
	for {
		var err error

		_, err = s.sch.findWorker(clientAddrs)

		if err != nil {
			break
		}
	}
}
开发者ID:jlisee,项目名称:cbd,代码行数:60,代码来源:server_test.go

示例3: TestFilterBridgeAddresses

func (s *NetworkSuite) TestFilterBridgeAddresses(c *gc.C) {
	lxcFakeNetConfig := filepath.Join(c.MkDir(), "lxc-net")
	// We create an LXC bridge named "foobar", and then put 10.0.3.1,
	// 10.0.3.4 and 10.0.3.5/24 on that bridge.
	// We also put 10.0.4.1 and 10.0.5.1/24 onto whatever bridge LXD is
	// configured to use.
	netConf := []byte(`
  # comments ignored
LXC_BR= ignored
LXC_ADDR = "fooo"
 LXC_BRIDGE = " foobar " # detected, spaces stripped
anything else ignored
LXC_BRIDGE="ignored"`[1:])
	err := ioutil.WriteFile(lxcFakeNetConfig, netConf, 0644)
	c.Assert(err, jc.ErrorIsNil)
	s.PatchValue(&network.InterfaceByNameAddrs, func(name string) ([]net.Addr, error) {
		if name == "foobar" {
			return []net.Addr{
				&net.IPAddr{IP: net.IPv4(10, 0, 3, 1)},
				&net.IPAddr{IP: net.IPv4(10, 0, 3, 4)},
				// Try a CIDR 10.0.3.5/24 as well.
				&net.IPNet{IP: net.IPv4(10, 0, 3, 5), Mask: net.IPv4Mask(255, 255, 255, 0)},
			}, nil
		} else if name == network.DefaultLXDBridge {
			return []net.Addr{
				&net.IPAddr{IP: net.IPv4(10, 0, 4, 1)},
				// Try a CIDR 10.0.5.1/24 as well.
				&net.IPNet{IP: net.IPv4(10, 0, 5, 1), Mask: net.IPv4Mask(255, 255, 255, 0)},
			}, nil
		}
		c.Fatalf("unknown bridge name: %q", name)
		return nil, nil
	})
	s.PatchValue(&network.LXCNetDefaultConfig, lxcFakeNetConfig)

	inputAddresses := network.NewAddresses(
		"127.0.0.1",
		"2001:db8::1",
		"10.0.0.1",
		"10.0.3.1",  // filtered (directly as IP)
		"10.0.3.3",  // filtered (by the 10.0.3.5/24 CIDR)
		"10.0.3.5",  // filtered (directly)
		"10.0.3.4",  // filtered (directly)
		"10.0.4.1",  // filtered (directly from LXD bridge)
		"10.0.5.10", // filtered (from LXD bridge, 10.0.5.1/24)
		"10.0.6.10", // unfiltered
		"192.168.123.42",
	)
	filteredAddresses := network.NewAddresses(
		"127.0.0.1",
		"2001:db8::1",
		"10.0.0.1",
		"10.0.6.10",
		"192.168.123.42",
	)
	c.Assert(network.FilterBridgeAddresses(inputAddresses), jc.DeepEquals, filteredAddresses)
}
开发者ID:bac,项目名称:juju,代码行数:57,代码来源:network_test.go

示例4: NetworkSize

// Given a netmask, calculates the number of available hosts
func NetworkSize(mask net.IPMask) int32 {
	m := net.IPv4Mask(0, 0, 0, 0)
	for i := 0; i < net.IPv4len; i++ {
		m[i] = ^mask[i]
	}
	return int32(binary.BigEndian.Uint32(m)) + 1
}
开发者ID:beginnor,项目名称:docker,代码行数:8,代码来源:utils.go

示例5: TestParseInterfaceStanzaStaticPostUp

func TestParseInterfaceStanzaStaticPostUp(t *testing.T) {
	options := []string{
		"address 192.168.1.100",
		"netmask 255.255.255.0",
		"post-up route add gw 192.168.1.1 -net 192.168.1.0 netmask 255.255.255.0",
	}
	expect := []route{
		{
			destination: net.IPNet{
				IP:   net.IPv4(192, 168, 1, 0),
				Mask: net.IPv4Mask(255, 255, 255, 0),
			},
			gateway: net.IPv4(192, 168, 1, 1),
		},
	}

	iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, options)
	if err != nil {
		t.FailNow()
	}
	static, ok := iface.configMethod.(configMethodStatic)
	if !ok {
		t.FailNow()
	}
	if !reflect.DeepEqual(static.routes, expect) {
		t.FailNow()
	}
}
开发者ID:RubanDeventhiran,项目名称:coreos-cloudinit,代码行数:28,代码来源:stanza_test.go

示例6: ParseIPv4Mask

// Parse IPv4 netmask written in IP form (e.g. 255.255.255.0).
// This function should really belong to the net package.
func ParseIPv4Mask(s string) net.IPMask {
	mask := net.ParseIP(s)
	if mask == nil {
		return nil
	}
	return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15])
}
开发者ID:carriercomm,项目名称:empire,代码行数:9,代码来源:ipmask.go

示例7: TestAddRouteSourceSelection

func TestAddRouteSourceSelection(t *testing.T) {
	tstIp := "127.1.1.1"
	tl := testLink{name: "tstEth", linkType: "dummy"}

	addLink(t, tl.name, tl.linkType)
	defer deleteLink(t, tl.name)

	ip := net.ParseIP(tstIp)
	mask := net.IPv4Mask(255, 255, 255, 255)
	ipNet := &net.IPNet{IP: ip, Mask: mask}

	iface, err := net.InterfaceByName(tl.name)
	if err != nil {
		t.Fatalf("Lost created link %#v", tl)
	}

	if err := NetworkLinkAddIp(iface, ip, ipNet); err != nil {
		t.Fatalf("Could not add IP address %s to interface %#v: %s", ip.String(), iface, err)
	}

	upLink(t, tl.name)
	defer downLink(t, tl.name)

	if err := AddRoute("127.0.0.0/8", tstIp, "", tl.name); err != nil {
		t.Fatalf("Failed to add route with source address")
	}
}
开发者ID:NERSC,项目名称:docker,代码行数:27,代码来源:netlink_linux_test.go

示例8: TestAddDelNetworkIp

func TestAddDelNetworkIp(t *testing.T) {
	if testing.Short() {
		return
	}

	ifaceName := "lo"
	ip := net.ParseIP("127.0.1.1")
	mask := net.IPv4Mask(255, 255, 255, 255)
	ipNet := &net.IPNet{IP: ip, Mask: mask}

	iface, err := net.InterfaceByName(ifaceName)
	if err != nil {
		t.Skip("No 'lo' interface; skipping tests")
	}

	if err := NetworkLinkAddIp(iface, ip, ipNet); err != nil {
		t.Fatalf("Could not add IP address %s to interface %#v: %s", ip.String(), iface, err)
	}

	if !ipAssigned(iface, ip) {
		t.Fatalf("Could not locate address '%s' in lo address list.", ip.String())
	}

	if err := NetworkLinkDelIp(iface, ip, ipNet); err != nil {
		t.Fatalf("Could not delete IP address %s from interface %#v: %s", ip.String(), iface, err)
	}

	if ipAssigned(iface, ip) {
		t.Fatalf("Located address '%s' in lo address list after removal.", ip.String())
	}
}
开发者ID:NERSC,项目名称:docker,代码行数:31,代码来源:netlink_linux_test.go

示例9: TestUnmarshalRoundtrip

func (S) TestUnmarshalRoundtrip(c *C) {
	for i, t := range []encoding.BinaryMarshaler{
		Bool(true),
		Bool(false),
		Integers{Integer{IntegerOpGt, 1}},
		Integers{},
		Strings{"foo", "bar"},
		Strings{},
		(*Regexp)(regexp.MustCompile(`\d`)),
		(*Regexp)(regexp.MustCompile("")),
		CIDRs{
			net.IPNet{IP: net.IPv4(127, 0, 0, 1), Mask: net.IPv4Mask(255, 255, 255, 0)},
			net.IPNet{IP: net.IPv6loopback, Mask: net.CIDRMask(8, 16*8)},
		},
		CIDRs{},
	} {
		c.Log(i)
		info := Commentf("i = %d", i)

		b, err := t.MarshalBinary()
		c.Assert(err, IsNil, info)

		parsed, err := UnmarshalBinary(b)
		c.Assert(err, IsNil, info)
		c.Assert(parsed, DeepEquals, t)
	}
}
开发者ID:ably-forks,项目名称:flynn,代码行数:27,代码来源:compare_test.go

示例10: ParseIPv4Mask

// ParseIPv4Mask written in IP form (e.g. 255.255.255.0).
// This function should really belong to the net package.
func ParseIPv4Mask(s string) net.IPMask {
	mask := net.ParseIP(s)
	if mask == nil {
		if len(s) != 8 {
			return nil
		}
		// net.IPMask.String() actually outputs things like ffffff00
		// so write a horrible parser for that as well  :-(
		m := []int{}
		for i := 0; i < 4; i++ {
			b := "0x" + s[2*i:2*i+2]
			d, err := strconv.ParseInt(b, 0, 0)
			if err != nil {
				return nil
			}
			m = append(m, int(d))
		}
		s := fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3])
		mask = net.ParseIP(s)
		if mask == nil {
			return nil
		}
	}
	return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15])
}
开发者ID:iamthemuffinman,项目名称:go-coreutils,代码行数:27,代码来源:ipmask.go

示例11: TestNetworkSize

func TestNetworkSize(t *testing.T) {
	mask := net.IPv4Mask(255, 255, 255, 0)
	result := NetworkSize(mask)
	expected := int32(256)
	if result != expected {
		t.Fatalf("got %v, expected %v\n", result, expected)
	}
}
开发者ID:anlaneg,项目名称:socketplane,代码行数:8,代码来源:utils_test.go

示例12: HexToIpv4Mask

func HexToIpv4Mask(hex_mask string) (mask net.IPMask, err error) {
	a, err := hex.DecodeString(hex_mask)
	if err != nil {
		return
	}

	mask = net.IPv4Mask(a[0], a[1], a[2], a[3])
	return
}
开发者ID:r3boot,项目名称:rlib,代码行数:9,代码来源:utils.go

示例13: 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
}
开发者ID:brianredbeard,项目名称:astralboot,代码行数:10,代码来源:store.go

示例14: main

func main() {
	const logDirEnvVar = "FAKE_LOG_DIR"
	logDir := os.Getenv(logDirEnvVar)
	if logDir == "" {
		log.Fatalf("missing required arg %q", logDirEnvVar)
	}

	stdin, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatalf("error reading stdin bytes: %s", err)
	}

	env, err := parseEnviron(os.Environ())
	if err != nil {
		log.Fatalf("unable to parse environment: %s", err)
	}

	args := os.Args

	logInfo := LogInfo{
		Args:  args,
		Env:   env,
		Stdin: string(stdin),
	}

	logBytes, err := json.Marshal(logInfo)
	if err != nil {
		log.Fatalf("unable to json marshal log info")
	}

	logFilePath := filepath.Join(logDir, filepath.Base(args[0])+".log")
	err = ioutil.WriteFile(logFilePath, logBytes, 0600)
	if err != nil {
		log.Fatalf("unable to write log file: %s", err)
	}

	result := types.Result{
		IP4: &types.IPConfig{
			IP: net.IPNet{
				IP:   net.ParseIP("169.254.1.2"),
				Mask: net.IPv4Mask(255, 255, 255, 0),
			},
		},
	}

	outputBytes, err := json.Marshal(result)
	if err != nil {
		log.Fatalf("unable to json marshal result data: %s", err)
	}

	_, err = os.Stdout.Write(outputBytes)
	if err != nil {
		log.Fatalf("unable to write result to stdout: %s", err)
	}

}
开发者ID:cloudfoundry-incubator,项目名称:guardian-cni-adapter,代码行数:56,代码来源:main.go

示例15: printFailoverIp

func printFailoverIp(failoverIp, localIp string) {
	var failover api.Failover
	if err := api.Get("/failover/"+failoverIp, &failover); err != nil {
		fatal(err)
	}

	a := net.ParseIP(failover.Netmask)
	_, bits := net.IPv4Mask(a[0], a[1], a[2], a[3]).Size()
	fmt.Printf(format, failover.Ip, bits, failover.Active_server_ip, failover.Server_ip, failover.Server_number, dutyMark(failover.Active_server_ip.String(), localIp))
}
开发者ID:mehulsbhatt,项目名称:hetzner-failover-ip,代码行数:10,代码来源:hetzner-failover-ip.go


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