本文整理汇总了Golang中net.IPNet类的典型用法代码示例。如果您正苦于以下问题:Golang IPNet类的具体用法?Golang IPNet怎么用?Golang IPNet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IPNet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ensureBridgeAddr
func ensureBridgeAddr(br *netlink.Bridge, ipn *net.IPNet, forceAddress bool) error {
addrs, err := netlink.AddrList(br, syscall.AF_INET)
if err != nil && err != syscall.ENOENT {
return fmt.Errorf("could not get list of IP addresses: %v", err)
}
// if there're no addresses on the bridge, it's ok -- we'll add one
if len(addrs) > 0 {
ipnStr := ipn.String()
for _, a := range addrs {
// string comp is actually easiest for doing IPNet comps
if a.IPNet.String() == ipnStr {
return nil
}
// If forceAddress is set to true then reconfigure IP address otherwise throw error
if forceAddress {
if err = deleteBridgeAddr(br, a.IPNet); err != nil {
return err
}
} else {
return fmt.Errorf("%q already has an IP address different from %v", br.Name, ipn.String())
}
}
}
addr := &netlink.Addr{IPNet: ipn, Label: ""}
if err := netlink.AddrAdd(br, addr); err != nil {
return fmt.Errorf("could not add IP address to %q: %v", br.Name, err)
}
return nil
}
示例2: 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))
}
}
示例3: generateCMD
func generateCMD(dst net.IPNet, gw net.IP, is_delete bool) []string {
var netmask_str string = net.IP(dst.Mask).String()
var ipaddr_str string = dst.IP.String()
var gateway_str string = gw.String()
var network_str string = dst.String()
switch runtime.GOOS {
case "darwin":
if is_delete {
return []string{"route", "delete", "-net",
ipaddr_str, gateway_str, netmask_str}
} else {
return []string{"route", "add", "-net",
ipaddr_str, gateway_str, netmask_str}
}
case "linux":
if is_delete {
return []string{"ip", "route", "del", network_str}
} else {
return []string{"ip", "route", "add", network_str, "via", gateway_str}
}
default:
return nil
}
return 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: insertBitMask
func (a *Allocator) insertBitMask(key SubnetKey, pool *net.IPNet) error {
log.Debugf("Inserting bitmask (%s, %s)", key.String(), pool.String())
store := a.getStore(key.AddressSpace)
if store == nil {
return fmt.Errorf("could not find store for address space %s while inserting bit mask", key.AddressSpace)
}
ipVer := getAddressVersion(pool.IP)
ones, bits := pool.Mask.Size()
numAddresses := uint32(1 << uint(bits-ones))
if ipVer == v4 {
// Do not let broadcast address be reserved
numAddresses--
}
// Generate the new address masks. AddressMask content may come from datastore
h, err := bitseq.NewHandle(dsDataKey, store, key.String(), numAddresses)
if err != nil {
return err
}
if ipVer == v4 {
// Do not let network identifier address be reserved
h.Set(0)
}
a.Lock()
a.addresses[key] = h
a.Unlock()
return nil
}
示例6: 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
}
示例7: getNextIp
// return an available ip if one is currently available. If not,
// return the next available ip for the nextwork
func getNextIp(address *net.IPNet) (*net.IP, error) {
var (
ownIP = ipToInt(&address.IP)
allocated = allocatedIPs[address.String()]
first, _ = networkdriver.NetworkRange(address)
base = ipToInt(&first)
size = int(networkdriver.NetworkSize(address.Mask))
max = int32(size - 2) // size -1 for the broadcast address, -1 for the gateway address
pos = atomic.LoadInt32(&allocated.last)
)
var (
firstNetIP = address.IP.To4().Mask(address.Mask)
firstAsInt = ipToInt(&firstNetIP) + 1
)
for i := int32(0); i < max; i++ {
pos = pos%max + 1
next := int32(base + pos)
if next == ownIP || next == firstAsInt {
continue
}
if !allocated.Exists(int(pos)) {
ip := intToIP(next)
allocated.Push(int(pos))
atomic.StoreInt32(&allocated.last, pos)
return ip, nil
}
}
return nil, ErrNoAvailableIPs
}
示例8: createCBR0
func createCBR0(wantCIDR *net.IPNet) error {
// recreate cbr0 with wantCIDR
if err := exec.Command("brctl", "addbr", "cbr0").Run(); err != nil {
glog.Error(err)
return err
}
if err := exec.Command("ip", "addr", "add", wantCIDR.String(), "dev", "cbr0").Run(); err != nil {
glog.Error(err)
return err
}
if err := exec.Command("ip", "link", "set", "dev", "cbr0", "mtu", "1460", "up").Run(); err != nil {
glog.Error(err)
return err
}
// restart docker
// For now just log the error. The containerRuntime check will catch docker failures.
// TODO (dawnchen) figure out what we should do for rkt here.
if util.UsingSystemdInitSystem() {
if err := exec.Command("systemctl", "restart", "docker").Run(); err != nil {
glog.Error(err)
}
} else {
if err := exec.Command("service", "docker", "restart").Run(); err != nil {
glog.Error(err)
}
}
glog.V(2).Info("Recreated cbr0 and restarted docker")
return nil
}
示例9: Setup
func (mgr *filterChain) Setup(instanceChain, bridgeName string, ip net.IP, network *net.IPNet) error {
commands := []*exec.Cmd{
// Create filter instance chain
exec.Command("iptables", "--wait", "-N", instanceChain),
// Allow intra-subnet traffic (Linux ethernet bridging goes through ip stack)
exec.Command("iptables", "--wait", "-A", instanceChain, "-s", network.String(), "-d", network.String(), "-j", "ACCEPT"),
// Otherwise, use the default filter chain
exec.Command("iptables", "--wait", "-A", instanceChain, "--goto", mgr.cfg.DefaultChain),
// Bind filter instance chain to filter forward chain
exec.Command("iptables", "--wait", "-I", mgr.cfg.ForwardChain, "2", "--in-interface", bridgeName, "--source", ip.String(), "--goto", instanceChain),
}
for _, cmd := range commands {
buffer := &bytes.Buffer{}
cmd.Stderr = buffer
logger := mgr.logger.Session("setup", lager.Data{"cmd": cmd})
logger.Debug("starting")
if err := mgr.runner.Run(cmd); err != nil {
stderr, _ := ioutil.ReadAll(buffer)
logger.Error("failed", err, lager.Data{"stderr": string(stderr)})
return fmt.Errorf("iptables_manager: filter: %s", err)
}
logger.Debug("ended")
}
return nil
}
示例10: setAddr4
// sets IP4 addr on link removing any existing ones first
func setAddr4(link *netlink.Vxlan, ipn *net.IPNet) error {
addrs, err := netlink.AddrList(link, syscall.AF_INET)
if err != nil {
return err
}
addr := netlink.Addr{ipn, ""}
existing := false
for _, old := range addrs {
if old.IPNet.String() == addr.IPNet.String() {
existing = true
continue
}
if err = netlink.AddrDel(link, &old); err != nil {
return fmt.Errorf("failed to delete IPv4 addr %s from %s", old.String(), link.Attrs().Name)
}
}
if !existing {
if err = netlink.AddrAdd(link, &addr); err != nil {
return fmt.Errorf("failed to add IP address %s to %s: %s", ipn.String(), link.Attrs().Name, err)
}
}
return nil
}
示例11: 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
}
示例12: checkAddress
func checkAddress(address *net.IPNet) {
key := address.String()
if _, exists := allocatedIPs[key]; !exists {
allocatedIPs[key] = collections.NewOrderedIntSet()
availableIPS[key] = collections.NewOrderedIntSet()
}
}
示例13: generateRandomNetwork
func generateRandomNetwork(address *net.IPNet) string {
tick := float64(time.Now().UnixNano() / 1000000)
ones, bits := address.Mask.Size()
zeros := bits - ones
uniqIPsAmount := math.Pow(2.0, float64(zeros))
rawIP := math.Mod(tick, uniqIPsAmount)
remainder := rawIP
remainder, octet4 := math.Modf(remainder / 255.0)
remainder, octet3 := math.Modf(remainder / 255.0)
remainder, octet2 := math.Modf(remainder / 255.0)
base := address.IP
address.IP = net.IPv4(
byte(remainder)|base[0],
byte(octet2*255)|base[1],
byte(octet3*255)|base[2],
byte(octet4*255)|base[3],
)
address.IP.Mask(address.Mask)
return address.String()
}
示例14: initConfigWrite
// Initialize the repo to be used to announce/write config.
// A seperate repo is initialized to read incoming announcements
func initConfigWrite(networkCidr *net.IPNet, hostIface, gitRepoURL string) {
var err error
if !pathExists(EndpointPushSubDir) {
log.Debugf("[ %s ] dir not found, creating it..", EndpointPushSubDir)
if err = CreatePaths(EndpointPushSubDir); err != nil {
log.Fatalf("Could not create the directory [ %s ]: %s", EndpointPushSubDir, err)
} else {
log.Warnf("Succesfully created the config path [ %s ]", EndpointPushSubDir)
}
}
// Create the cache subdirectories
time.Sleep(1 * time.Second)
localEndpointIP, _ := getIfaceAddrStr(hostIface)
// Fun Go fact: using a + with sprintf is faster then %s since it uses reflection
endpointFile := fmt.Sprintf(localEndpointIP + dotjson)
log.Debugf("The endpoint file name is [ %s ] ", endpointFile)
log.Debugf("Anouncing this endpoint using the source [ %s ] and advertsing network [ %s ] to datastore file [ %s ]", networkCidr, localEndpointIP, endpointFile)
endpointConfig := &LocalEndpoint{
Endpoint: localEndpointIP,
Network: networkCidr.String(),
Meta: "",
}
var configAnnounce []LocalEndpoint
configAnnounce = append(configAnnounce, *endpointConfig)
marshallConfig(configAnnounce, configFormat, endpointFile)
if log.GetLevel().String() == "debug" {
printPretty(configAnnounce, "json")
}
// Parse the repo name
defer gitPushConfig()
}
示例15: checkAddress
func checkAddress(address *net.IPNet) {
key := address.String()
if _, exists := allocatedIPs[key]; !exists {
allocatedIPs[key] = &iPSet{}
availableIPS[key] = &iPSet{}
}
}