glider/service/dhcpd/pool.go

141 lines
2.9 KiB
Go
Raw Normal View History

2020-09-28 00:49:58 +08:00
package dhcpd
import (
"bytes"
"errors"
2020-09-29 00:38:35 +08:00
"math/rand"
2020-09-28 00:49:58 +08:00
"net"
2022-01-28 23:35:29 +08:00
"net/netip"
"sync"
2020-09-28 00:49:58 +08:00
"time"
)
2020-10-03 20:51:27 +08:00
// Pool is a dhcp pool.
2020-09-28 00:49:58 +08:00
type Pool struct {
items []*item
mutex sync.RWMutex
lease time.Duration
}
type item struct {
2022-01-28 23:35:29 +08:00
ip netip.Addr
mac net.HardwareAddr
expire time.Time
2020-09-28 00:49:58 +08:00
}
// NewPool returns a new dhcp ip pool.
2022-01-28 23:35:29 +08:00
func NewPool(lease time.Duration, start, end netip.Addr) (*Pool, error) {
2022-01-29 21:10:09 +08:00
if start.IsUnspecified() || end.IsUnspecified() || start.Is6() || end.Is6() {
return nil, errors.New("start ip or end ip is wrong/nil, please check your config, note only ipv4 is supported")
}
s, e := ipv4ToNum(start), ipv4ToNum(end)
2020-10-01 19:29:53 +08:00
if e < s {
return nil, errors.New("start ip larger than end ip")
}
items := make([]*item, 0, e-s+1)
for n := s; n <= e; n++ {
2022-01-29 21:10:09 +08:00
items = append(items, &item{ip: numToIPv4(n)})
2020-09-28 00:49:58 +08:00
}
p := &Pool{items: items, lease: lease}
go func() {
for now := range time.Tick(time.Second) {
p.mutex.Lock()
for i := 0; i < len(items); i++ {
if !items[i].expire.IsZero() && now.After(items[i].expire) {
items[i].mac = nil
items[i].expire = time.Time{}
}
}
p.mutex.Unlock()
}
}()
return p, nil
2020-09-28 00:49:58 +08:00
}
// LeaseIP leases an ip to mac from dhcp pool.
func (p *Pool) LeaseIP(mac net.HardwareAddr, ip netip.Addr) (netip.Addr, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
// static ip and leased ip
2020-09-28 00:49:58 +08:00
for _, item := range p.items {
2020-09-29 00:38:35 +08:00
if bytes.Equal(mac, item.mac) {
if !item.expire.IsZero() {
item.expire = time.Now().Add(p.lease)
}
return item.ip, nil
}
}
// requested ip
for _, item := range p.items {
if item.ip == ip && item.mac == nil {
item.mac = mac
item.expire = time.Now().Add(p.lease)
2020-09-28 00:49:58 +08:00
return item.ip, nil
}
}
2020-09-29 00:38:35 +08:00
// lease new ip
2020-09-29 00:38:35 +08:00
idx := rand.Intn(len(p.items))
for _, item := range p.items[idx:] {
if item.mac == nil {
item.mac = mac
item.expire = time.Now().Add(p.lease)
return item.ip, nil
2020-09-29 00:38:35 +08:00
}
}
2020-09-28 00:49:58 +08:00
for _, item := range p.items {
if item.mac == nil {
item.mac = mac
item.expire = time.Now().Add(p.lease)
return item.ip, nil
2020-09-28 00:49:58 +08:00
}
}
2022-01-28 23:35:29 +08:00
return netip.Addr{}, errors.New("no more ip can be leased")
2020-09-28 00:49:58 +08:00
}
// LeaseStaticIP leases static ip from pool according to the given mac.
2022-01-28 23:35:29 +08:00
func (p *Pool) LeaseStaticIP(mac net.HardwareAddr, ip netip.Addr) {
p.mutex.Lock()
defer p.mutex.Unlock()
for _, item := range p.items {
2022-01-28 23:35:29 +08:00
if item.ip == ip {
item.mac = mac
2022-12-19 00:01:14 +08:00
item.expire = time.Time{}
}
}
}
// ReleaseIP releases ip from pool according to the given mac.
func (p *Pool) ReleaseIP(mac net.HardwareAddr) {
p.mutex.Lock()
defer p.mutex.Unlock()
for _, item := range p.items {
2022-12-19 00:01:14 +08:00
// not static ip
if !item.expire.IsZero() && bytes.Equal(mac, item.mac) {
item.mac = nil
item.expire = time.Time{}
}
2020-09-28 00:49:58 +08:00
}
}
2022-01-29 21:10:09 +08:00
func ipv4ToNum(addr netip.Addr) uint32 {
ip := addr.AsSlice()
n := uint32(ip[0])<<24 + uint32(ip[1])<<16
return n + uint32(ip[2])<<8 + uint32(ip[3])
}
2022-01-29 21:10:09 +08:00
func numToIPv4(n uint32) netip.Addr {
ip := [4]byte{byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n)}
return netip.AddrFrom4(ip)
}