本文整理匯總了Golang中net.CIDRMask函數的典型用法代碼示例。如果您正苦於以下問題:Golang CIDRMask函數的具體用法?Golang CIDRMask怎麽用?Golang CIDRMask使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了CIDRMask函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestSetupFixedCIDRv4
func TestSetupFixedCIDRv4(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
AddressIPv4: &net.IPNet{IP: net.ParseIP("192.168.1.1"), Mask: net.CIDRMask(16, 32)},
FixedCIDR: &net.IPNet{IP: net.ParseIP("192.168.2.0"), Mask: net.CIDRMask(24, 32)}}
br := &bridgeInterface{}
if err := setupDevice(config, br); err != nil {
t.Fatalf("Bridge creation failed: %v", err)
}
if err := setupBridgeIPv4(config, br); err != nil {
t.Fatalf("Assign IPv4 to bridge failed: %v", err)
}
if err := setupFixedCIDRv4(config, br); err != nil {
t.Fatalf("Failed to setup bridge FixedCIDRv4: %v", err)
}
if ip, err := ipAllocator.RequestIP(config.FixedCIDR, nil); err != nil {
t.Fatalf("Failed to request IP to allocator: %v", err)
} else if expected := "192.168.2.1"; ip.String() != expected {
t.Fatalf("Expected allocated IP %s, got %s", expected, ip)
}
}
示例2: validateRoutes
func validateRoutes(t *testing.T, routes []plugin.Route) {
expected := []plugin.Route{
plugin.Route{
Dst: net.IPNet{
IP: net.IPv4(10, 0, 0, 0),
Mask: net.CIDRMask(8, 32),
},
GW: net.IPv4(10, 1, 2, 3),
},
plugin.Route{
Dst: net.IPNet{
IP: net.IPv4(192, 168, 1, 0),
Mask: net.CIDRMask(24, 32),
},
GW: net.IPv4(192, 168, 2, 3),
},
}
if len(routes) != len(expected) {
t.Fatalf("wrong length slice; expected %v, got %v", len(expected), len(routes))
}
for i := 0; i < len(routes); i++ {
a := routes[i]
e := expected[i]
if a.Dst.String() != e.Dst.String() {
t.Errorf("route.Dst mismatch: expected %v, got %v", e.Dst, a.Dst)
}
if !a.GW.Equal(e.GW) {
t.Errorf("route.GW mismatch: expected %v, got %v", e.GW, a.GW)
}
}
}
示例3: prepareNetworks
func prepareNetworks(addrs string) (networks []*net.IPNet) {
for _, n := range strings.Fields(addrs) {
n = strings.TrimSpace(n)
if strings.Contains(n, "/") {
if _, network, err := net.ParseCIDR(n); err == nil {
networks = append(networks, network)
} else {
log.Warn("authenticationMW prepare networks error", "err", err, "net", n)
}
} else {
if ip := net.ParseIP(n); ip != nil {
var mask net.IPMask
if len(ip) == 4 { // ipv4
mask = net.CIDRMask(32, 32)
} else {
mask = net.CIDRMask(128, 128)
}
network := &net.IPNet{ip, mask}
networks = append(networks, network)
} else {
log.Warn("authenticationMW prepare ip error", "net", n)
}
}
}
log.Debug("prepareNetworks", "networks", networks, "inp", addrs)
return
}
示例4: TestSetupBadFixedCIDRv4
func TestSetupBadFixedCIDRv4(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
AddressIPv4: &net.IPNet{IP: net.ParseIP("192.168.1.1"), Mask: net.CIDRMask(24, 32)},
FixedCIDR: &net.IPNet{IP: net.ParseIP("192.168.2.0"), Mask: net.CIDRMask(24, 32)}}
br := &bridgeInterface{}
if err := setupDevice(config, br); err != nil {
t.Fatalf("Bridge creation failed: %v", err)
}
if err := setupBridgeIPv4(config, br); err != nil {
t.Fatalf("Assign IPv4 to bridge failed: %v", err)
}
err := setupFixedCIDRv4(config, br)
if err == nil {
t.Fatal("Setup bridge FixedCIDRv4 should have failed")
}
if _, ok := err.(*FixedCIDRv4Error); !ok {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
}
示例5: ToIPNet
func (x *XfrmAddress) ToIPNet(prefixlen uint8) *net.IPNet {
ip := x.ToIP()
if GetIPFamily(ip) == FAMILY_V4 {
return &net.IPNet{IP: ip, Mask: net.CIDRMask(int(prefixlen), 32)}
}
return &net.IPNet{IP: ip, Mask: net.CIDRMask(int(prefixlen), 128)}
}
示例6: pack
func (e *EDNS0_SUBNET) pack() ([]byte, error) {
b := make([]byte, 4)
binary.BigEndian.PutUint16(b[0:], e.Family)
b[2] = e.SourceNetmask
b[3] = e.SourceScope
switch e.Family {
case 1:
if e.SourceNetmask > net.IPv4len*8 {
return nil, errors.New("dns: bad netmask")
}
if len(e.Address.To4()) != net.IPv4len {
return nil, errors.New("dns: bad address")
}
ip := e.Address.To4().Mask(net.CIDRMask(int(e.SourceNetmask), net.IPv4len*8))
needLength := (e.SourceNetmask + 8 - 1) / 8 // division rounding up
b = append(b, ip[:needLength]...)
case 2:
if e.SourceNetmask > net.IPv6len*8 {
return nil, errors.New("dns: bad netmask")
}
if len(e.Address) != net.IPv6len {
return nil, errors.New("dns: bad address")
}
ip := e.Address.Mask(net.CIDRMask(int(e.SourceNetmask), net.IPv6len*8))
needLength := (e.SourceNetmask + 8 - 1) / 8 // division rounding up
b = append(b, ip[:needLength]...)
default:
return nil, errors.New("dns: bad address family")
}
return b, nil
}
示例7: parseRouteAddr
// parseRouteAddr parses a string route address and returns the resulting
// RouteAddr struct
func parseRouteAddr(text string) (RouteAddr, error) {
r := RouteAddr{}
// detect if this is a remote addr
if strings.HasSuffix(text, "C") {
text = strings.TrimSuffix(text, "C")
r.Remote = true
}
// for some reason ipm.IP is 16 bytes for an IPv4, but ip is 4 bytes
// so we use ip instead
ip, ipm, err := net.ParseCIDR(text)
if err == nil {
r.IP = ip
r.Mask = ipm.Mask
return r, nil
}
//it might not be a CIDR and instead just an IP
r.IP = net.ParseIP(text)
if r.IP == nil {
return r, err
}
r.Mask = net.CIDRMask(32, 8*net.IPv4len)
if r.IP.To4() == nil {
r.Mask = net.CIDRMask(128, 8*net.IPv6len)
}
return r, nil
}
示例8: UnmarshalBinary
func (c *CIDRs) UnmarshalBinary(b []byte) error {
if len(b) < 1 {
return fmt.Errorf("compare: missing type byte")
}
if b[0] != byte(TypeCIDRs) {
return fmt.Errorf("compare: expected type %d, got %d", TypeCIDRs, b[0])
}
b = b[1:]
*c = make(CIDRs, 0, len(b)/ipv4CIDRLen)
for len(b) > 0 {
var n net.IPNet
if b[0]&(1<<7) == 0 { // check top bit of ones byte, if 0, IPv4, if 1, IPv6
if len(b) < ipv4CIDRLen {
return fmt.Errorf("compare: unexpected end of buffer decoding IPv4 CIDR")
}
n.Mask = net.CIDRMask(int(b[0]), ipv4Len*8)
n.IP = net.IPv4(b[1], b[2], b[3], b[4])
b = b[5:]
} else {
if len(b) < ipv6CIDRLen {
return fmt.Errorf("compare: unexpected end of buffer decoding IPv6 CIDR")
}
n.Mask = net.CIDRMask(int(b[0]&^(1<<7)), ipv6Len*8)
n.IP = make([]byte, ipv6Len)
copy(n.IP, b[1:])
b = b[ipv6CIDRLen:]
}
*c = append(*c, n)
}
return nil
}
示例9: setupHostVeth
func setupHostVeth(vethName string, ipConf *types.IPConfig) error {
// hostVeth moved namespaces and may have a new ifindex
veth, err := netlink.LinkByName(vethName)
if err != nil {
return fmt.Errorf("failed to lookup %q: %v", vethName, err)
}
// TODO(eyakubovich): IPv6
ipn := &net.IPNet{
IP: ipConf.Gateway,
Mask: net.CIDRMask(32, 32),
}
addr := &netlink.Addr{IPNet: ipn, Label: ""}
if err = netlink.AddrAdd(veth, addr); err != nil {
return fmt.Errorf("failed to add IP addr (%#v) to veth: %v", ipn, err)
}
ipn = &net.IPNet{
IP: ipConf.IP.IP,
Mask: net.CIDRMask(32, 32),
}
// dst happens to be the same as IP/net of host veth
if err = ip.AddHostRoute(ipn, nil, veth); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to add route on host: %v", err)
}
return nil
}
示例10: TestRuleAddDel
func TestRuleAddDel(t *testing.T) {
srcNet := &net.IPNet{IP: net.IPv4(172, 16, 0, 1), Mask: net.CIDRMask(16, 32)}
dstNet := &net.IPNet{IP: net.IPv4(172, 16, 1, 1), Mask: net.CIDRMask(24, 32)}
rules_begin, err := RuleList(syscall.AF_INET)
if err != nil {
t.Fatal(err)
}
rule := NewRule()
rule.Table = syscall.RT_TABLE_MAIN
rule.Src = srcNet
rule.Dst = dstNet
rule.Priority = 5
rule.OifName = "lo"
rule.IifName = "lo"
if err := RuleAdd(rule); err != nil {
t.Fatal(err)
}
rules, err := RuleList(syscall.AF_INET)
if err != nil {
t.Fatal(err)
}
if len(rules) != len(rules_begin)+1 {
t.Fatal("Rule not added properly")
}
// find this rule
var found bool
for i := range rules {
if rules[i].Table == rule.Table &&
rules[i].Src != nil && rules[i].Src.String() == srcNet.String() &&
rules[i].Dst != nil && rules[i].Dst.String() == dstNet.String() &&
rules[i].OifName == rule.OifName &&
rules[i].Priority == rule.Priority &&
rules[i].IifName == rule.IifName {
found = true
}
}
if !found {
t.Fatal("Rule has diffrent options than one added")
}
if err := RuleDel(rule); err != nil {
t.Fatal(err)
}
rules_end, err := RuleList(syscall.AF_INET)
if err != nil {
t.Fatal(err)
}
if len(rules_end) != len(rules_begin) {
t.Fatal("Rule not removed properly")
}
}
示例11: splitToHostNetwork
func splitToHostNetwork(domain string) (bool, string, *net.IPMask, *net.IPMask) {
var host string
const v4Len = 32
n4s := "32"
const v6Len = 128
n6s := "128"
line := strings.SplitN(domain, "/", 3)
if len(line) == 3 {
host, n4s, n6s = line[0], line[1], line[2]
} else if len(line) == 2 {
host, n4s = line[0], line[1]
} else {
host = line[0]
}
if !IsDomainName(host) {
return false, host, nil, nil
}
if isEmpty(&n4s) {
// empty values default to maximum netmask
n4s = "32"
}
if isEmpty(&n6s) {
// empty values default to maximum netmask
n6s = "128"
}
var err error
var n4 int
var n6 int
var v4Network net.IPMask
var v6Network net.IPMask
if n4, err = strconv.Atoi(n4s); err != nil {
return false, host, nil, nil
} else if n4 < 0 || n4 > v4Len {
return false, host, nil, nil
} else {
v4Network = net.CIDRMask(n4, v4Len)
}
if n6, err = strconv.Atoi(n6s); err != nil {
return false, host, nil, nil
} else if n6 < 0 || n6 > v6Len {
return false, host, nil, nil
} else {
v6Network = net.CIDRMask(n6, v6Len)
}
return true, host, &v4Network, &v6Network
}
示例12: testConfig
func testConfig() *Configuration {
return &Configuration{
source: extraconfig.MapSource(map[string]string{}),
sink: extraconfig.MapSink(map[string]string{}),
BridgeLink: &mockLink{},
Network: config.Network{
BridgeNetwork: "bridge",
ContainerNetworks: map[string]*executor.ContainerNetwork{
"bridge": {
Common: executor.Common{
Name: "bridge",
},
Type: constants.BridgeScopeType,
},
"bar7": {
Common: executor.Common{
Name: "external",
},
Gateway: net.IPNet{IP: net.ParseIP("10.13.0.1"), Mask: net.CIDRMask(16, 32)},
Nameservers: []net.IP{net.ParseIP("10.10.1.1")},
Pools: []ip.Range{*ip.ParseRange("10.13.1.0-255"), *ip.ParseRange("10.13.2.0-10.13.2.15")},
Type: constants.ExternalScopeType,
},
"bar71": {
Common: executor.Common{
Name: "external",
},
Gateway: net.IPNet{IP: net.ParseIP("10.131.0.1"), Mask: net.CIDRMask(16, 32)},
Nameservers: []net.IP{net.ParseIP("10.131.0.1"), net.ParseIP("10.131.0.2")},
Pools: []ip.Range{*ip.ParseRange("10.131.1.0/16")},
Type: constants.ExternalScopeType,
},
"bar72": {
Common: executor.Common{
Name: "external",
},
Type: constants.ExternalScopeType,
},
"bar73": {
Common: executor.Common{
Name: "external",
},
Gateway: net.IPNet{IP: net.ParseIP("10.133.0.1"), Mask: net.CIDRMask(16, 32)},
Type: constants.ExternalScopeType,
},
},
},
PortGroups: map[string]object.NetworkReference{
"bridge": testBridgeNetwork,
"bar7": testExternalNetwork,
"bar71": testExternalNetwork,
"bar72": testExternalNetwork,
"bar73": testExternalNetwork,
},
}
}
示例13: TestMain
func TestMain(m *testing.M) {
n := object.NewNetwork(nil, types.ManagedObjectReference{})
n.InventoryPath = "testBridge"
testBridgeNetwork = n
n = object.NewNetwork(nil, types.ManagedObjectReference{})
n.InventoryPath = "testExternal"
testExternalNetwork := n
Config = Configuration{
BridgeLink: &mockLink{},
BridgeNetwork: "bridge",
ContainerNetworks: map[string]*ContainerNetwork{
"bridge": &ContainerNetwork{
PortGroup: testBridgeNetwork,
},
"bar7": &ContainerNetwork{
Common: executor.Common{
Name: "external",
},
Gateway: net.IPNet{IP: net.ParseIP("10.13.0.1"), Mask: net.CIDRMask(16, 32)},
Nameservers: []net.IP{net.ParseIP("10.10.1.1")},
Pools: []ip.Range{*ip.ParseRange("10.13.1.0-255"), *ip.ParseRange("10.13.2.0-10.13.2.15")},
PortGroup: testExternalNetwork,
},
"bar71": &ContainerNetwork{
Common: executor.Common{
Name: "external",
},
Gateway: net.IPNet{IP: net.ParseIP("10.131.0.1"), Mask: net.CIDRMask(16, 32)},
Nameservers: []net.IP{net.ParseIP("10.131.0.1"), net.ParseIP("10.131.0.2")},
Pools: []ip.Range{*ip.ParseRange("10.131.1.0/16")},
PortGroup: testExternalNetwork,
},
"bar72": &ContainerNetwork{
Common: executor.Common{
Name: "external",
},
PortGroup: testExternalNetwork,
},
"bar73": &ContainerNetwork{
Common: executor.Common{
Name: "external",
},
Gateway: net.IPNet{IP: net.ParseIP("10.133.0.1"), Mask: net.CIDRMask(16, 32)},
PortGroup: testExternalNetwork,
},
},
}
rc := m.Run()
os.Exit(rc)
}
示例14: newInterface
func newInterface(s, i byte) *Interface {
return &Interface{
HardwareAddr: net.HardwareAddr{0x02, s, 0, 0, 0, i},
DHCPv4: []net.IPNet{{
IP: net.IP{10, s, 0, i},
Mask: net.CIDRMask(24, 32)}},
DHCPv6: []net.IPNet{{
IP: net.IP{0xfd, s, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, i},
Mask: net.CIDRMask(64, 128)}},
}
}
示例15: ExampleCIDRMask
func ExampleCIDRMask() {
// This mask corresponds to a /31 subnet for IPv4.
fmt.Println(net.CIDRMask(31, 32))
// This mask corresponds to a /64 subnet for IPv6.
fmt.Println(net.CIDRMask(64, 128))
// Output:
// fffffffe
// ffffffffffffffff0000000000000000
}