2023-03-14 17:21:49 +01:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-04-03 10:03:52 +02:00
|
|
|
"errors"
|
2023-04-04 16:43:45 +02:00
|
|
|
"math"
|
2023-04-03 10:03:52 +02:00
|
|
|
"net/netip"
|
|
|
|
"time"
|
2023-03-14 17:21:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Subnet struct {
|
2023-04-03 10:03:52 +02:00
|
|
|
Subnet netip.Prefix `json:"subnet"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Vlan string `json:"vlan"`
|
|
|
|
ChangedAt time.Time `json:"changedat,omitempty"`
|
|
|
|
ChangedBy string `json:"changedby,omitempty"`
|
|
|
|
Addresses []Address `json:"addresses"`
|
2023-03-14 17:21:49 +01:00
|
|
|
}
|
|
|
|
|
2023-04-04 16:43:45 +02:00
|
|
|
// GetIPCount gets the IP count for the Subnet
|
|
|
|
//
|
|
|
|
// Returns the IP count or -1 if it's a IPv6 prefix
|
|
|
|
func (s Subnet) GetIPCount() int {
|
|
|
|
if !s.Subnet.Addr().Is4() {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
hostbits := float64(32 - s.Subnet.Bits())
|
|
|
|
if s.Subnet.Bits() == 31 {
|
|
|
|
return 2
|
|
|
|
} else {
|
|
|
|
return int(math.Pow(2, hostbits)) - 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 17:21:49 +01:00
|
|
|
// HasIP checks if a Subnet already contains given netip.Addr.
|
|
|
|
// Returns true if the IP already is present, false otherwise.
|
|
|
|
func (s Subnet) HasIP(ip netip.Addr) bool {
|
2023-04-03 10:03:52 +02:00
|
|
|
iscontained := false
|
2023-03-14 17:21:49 +01:00
|
|
|
|
2023-04-03 10:03:52 +02:00
|
|
|
for _, element := range s.Addresses {
|
|
|
|
if element.IP.Compare(ip) == 0 {
|
|
|
|
iscontained = true
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 17:21:49 +01:00
|
|
|
|
2023-04-03 10:03:52 +02:00
|
|
|
return iscontained
|
2023-03-14 17:21:49 +01:00
|
|
|
}
|
|
|
|
|
2023-04-04 16:43:45 +02:00
|
|
|
// FindFirstFreeIP finds and returns the next free netip.Addr
|
|
|
|
// or an invalid netip.Addr if no free IP was found
|
|
|
|
func (s Subnet) FindFirstFreeIP() netip.Addr {
|
|
|
|
var ip netip.Addr
|
|
|
|
|
|
|
|
if s.Subnet.Addr().Is4() {
|
|
|
|
subnetips := s.GetIPCount()
|
|
|
|
|
|
|
|
// handling /31 prefixes
|
|
|
|
if subnetips == 2 {
|
|
|
|
ip = s.Subnet.Addr()
|
|
|
|
} else {
|
|
|
|
ip = s.Subnet.Addr().Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
// start at first free IP
|
|
|
|
for count := 0; count < subnetips; count++ {
|
|
|
|
|
|
|
|
if s.HasIP(ip) {
|
|
|
|
ip = ip.Next()
|
|
|
|
} else {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ip = s.Subnet.Addr().Next()
|
|
|
|
for ; s.Subnet.Contains(ip); ip = ip.Next() {
|
|
|
|
if !s.HasIP(ip) {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return netip.Addr{}
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:43:20 +01:00
|
|
|
// RemoveIP removes the Address object for given ip from
|
|
|
|
// the Address list of the subnet.
|
|
|
|
//
|
|
|
|
// Returns the changed Subnet and nil if delete was
|
|
|
|
// successful, or an empty Subnet and an error if
|
|
|
|
// ip could not be deleted.
|
|
|
|
func (s Subnet) RemoveIP(ip netip.Addr) (Subnet, error) {
|
2023-04-03 10:03:52 +02:00
|
|
|
var addrlist []Address
|
2023-03-15 16:43:20 +01:00
|
|
|
|
2023-04-03 10:03:52 +02:00
|
|
|
if !s.HasIP(ip) {
|
2023-03-15 16:43:20 +01:00
|
|
|
|
2023-04-03 10:03:52 +02:00
|
|
|
return Subnet{}, errors.New("IP " + ip.String() + " wasn't found in subnet " + s.Subnet.String())
|
|
|
|
}
|
2023-03-15 16:43:20 +01:00
|
|
|
|
2023-04-03 10:03:52 +02:00
|
|
|
for _, item := range s.Addresses {
|
|
|
|
if item.IP.Compare(ip) != 0 {
|
|
|
|
addrlist = append(addrlist, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Addresses = addrlist
|
|
|
|
return s, nil
|
2023-03-14 17:21:49 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:43:20 +01:00
|
|
|
// GetIP returns the Address object for the subnet with
|
|
|
|
// netip.Addr ip.
|
|
|
|
//
|
|
|
|
// Returns the Address object and true if a corresponding
|
|
|
|
// object was found, an empty Address and false otherwise.
|
|
|
|
func (s Subnet) GetIP(ip netip.Addr) (Address, bool) {
|
2023-04-03 10:03:52 +02:00
|
|
|
for _, item := range s.Addresses {
|
|
|
|
if item.IP.Compare(ip) == 0 {
|
|
|
|
return item, true
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 16:43:20 +01:00
|
|
|
|
2023-04-03 10:03:52 +02:00
|
|
|
return Address{}, false
|
2023-03-14 17:21:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Address struct {
|
2023-04-03 10:03:52 +02:00
|
|
|
IP netip.Addr `json:"ip"`
|
|
|
|
FQDN string `json:"fqdn"`
|
|
|
|
ChangedAt time.Time `json:"changedat,omitempty"`
|
|
|
|
ChangedBy string `json:"changedby,omitempty"`
|
2023-03-14 17:21:49 +01:00
|
|
|
}
|