first working version

This commit is contained in:
ston1th 2021-04-09 22:38:03 +02:00
commit 6da132106f
18 changed files with 519 additions and 96 deletions

View file

@ -46,12 +46,12 @@ func (a *Alloc) cidr(ip net.IP) string {
func (a *Alloc) AllocIP(ctx context.Context, name string) (addr string, err error) {
a.Lock()
defer a.Unlock()
for _, cidr := range a.pool {
c := ipaddr.NewCursor([]ipaddr.Prefix{*ipaddr.NewPrefix(cidr)})
for _, p := range a.pool {
c := ipaddr.NewCursor([]ipaddr.Prefix{*ipaddr.NewPrefix(p)})
for pos := c.First(); pos != nil; pos = c.Next() {
cidr := a.cidr(pos.IP)
if !a.db.IPExists(cidr) {
err = a.db.SetIP(cidr, name)
err = a.db.SetIPName(cidr, name)
if err != nil {
return
}
@ -70,11 +70,11 @@ func (a *Alloc) FreeIP(ctx context.Context, name string) (err error) {
if name == "" {
return
}
ip, err := a.db.GetName(name)
ip, err := a.db.GetIP(name)
if err != nil {
return
}
return a.db.DeleteIP(ip, name)
return a.db.DeleteIPName(ip, name)
}
func parseRange(cidrs []string) (nets []*net.IPNet, err error) {