Skip to content

Commit

Permalink
kvm2 driver: add static ip
Browse files Browse the repository at this point in the history
  • Loading branch information
prezha committed Mar 27, 2021
1 parent 7218c9e commit 69925c5
Showing 1 changed file with 0 additions and 34 deletions.
34 changes: 0 additions & 34 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,37 +264,3 @@ func reserveSubnet(subnet string, period time.Duration) bool {
reservedSubnets.Store(subnet, reservation{createdAt: time.Now()})
return true
}

// IncIP returns address calculated by adding (positive or negative) val to addr.
// If addr is not in CIDR format it will use its default mask and set "out-of-network" if calculated address doesn't belong to same network as addr.
func IncIP(addr string, val int) (calc string, oon bool, err error) {
ip, network, err := net.ParseCIDR(addr)
if err != nil { // addr not in CIDR format - use default mask for IP addr
ip = net.ParseIP(addr)
if ip == nil {
return "", false, fmt.Errorf("failed parsing address %s: %w", addr, err)
}
prefix, _ := ip.DefaultMask().Size() // assume default network mask
_, network, err = net.ParseCIDR(fmt.Sprintf("%s/%d", addr, prefix))
if err != nil {
return "", false, fmt.Errorf("failed parsing CIDR %s/%d: %w", addr, prefix, err)
}
}

ip0 := ip.To4() // convert to 4-byte representation
n0 := binary.BigEndian.Uint32(ip0)

n1 := int(n0) + val
if n1 < 0 {
return "", true, fmt.Errorf("failed decrementing IP %s: value %d too big", addr, val)
}

ip1 := make(net.IP, 4)
binary.BigEndian.PutUint32(ip1, uint32(n1))

if !network.Contains(ip1) {
return "", true, fmt.Errorf("failed incrementing IP %s: value %d too big (%s would cross network boundary)", addr, val, ip)
}

return ip1.String(), false, nil
}

0 comments on commit 69925c5

Please sign in to comment.