本文整理汇总了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")
}
}
示例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
}
}
}
示例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)
}
示例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
}
示例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()
}
}
示例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])
}
示例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")
}
}
示例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())
}
}
示例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)
}
}
示例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])
}
示例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)
}
}
示例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
}
示例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
}
示例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)
}
}
示例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))
}