implemented api handlers

This commit is contained in:
ston1th 2021-04-08 00:34:54 +02:00
commit 372915e24d
15 changed files with 302 additions and 73 deletions

View file

@ -36,22 +36,26 @@ func (a *Alloc) UpdateDB(db *db.DB) {
a.Unlock()
}
func (a *Alloc) cidr(ip net.IP) string {
gw := new(net.IPNet)
*gw = *a.gateway
gw.IP = ip
return gw.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 pos := c.First(); pos != nil; pos = c.Next() {
ip := pos.IP.String()
if !a.db.IPExists(ip) {
err = a.db.SetIP(ip, name)
cidr := a.cidr(pos.IP)
if !a.db.IPExists(cidr) {
err = a.db.SetIP(cidr, name)
if err != nil {
return
}
var gw *net.IPNet
*gw = *a.gateway
gw.IP = pos.IP
addr = gw.String()
addr = pos.IP.String()
return
}
}