mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
dhcpd: support to handle DECLINE & RELEASE msg
This commit is contained in:
1 parent
3092d857ff
commit
41fee381d0
6 files changed
+83
-45
No files matched your search
+50
-28
@@ -5,12 +5,21 @@ import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Pool is a dhcp pool.
|
||||
type Pool struct {
|
||||
items []*item
|
||||
mutex sync.RWMutex
|
||||
lease time.Duration
|
||||
}
|
||||
|
||||
type item struct {
|
||||
ip net.IP
|
||||
mac net.HardwareAddr
|
||||
expire time.Time
|
||||
}
|
||||
|
||||
// NewPool returns a new dhcp ip pool.
|
||||
@@ -26,15 +35,32 @@ func NewPool(lease time.Duration, start, end net.IP) (*Pool, error) {
|
||||
|
||||
items := make([]*item, 0, e-s+1)
|
||||
for n := s; n <= e; n++ {
|
||||
items = append(items, &item{lease: lease, ip: num2ip(n)})
|
||||
items = append(items, &item{ip: num2ip(n)})
|
||||
}
|
||||
rand.Seed(time.Now().Unix())
|
||||
return &Pool{items: items}, nil
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// AssignIP assigns an ip to mac from dhco pool.
|
||||
func (p *Pool) AssignIP(mac net.HardwareAddr) (net.IP, error) {
|
||||
var ip net.IP
|
||||
// LeaseIP leases an ip to mac from dhcp pool.
|
||||
func (p *Pool) LeaseIP(mac net.HardwareAddr) (net.IP, error) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
for _, item := range p.items {
|
||||
if bytes.Equal(mac, item.mac) {
|
||||
return item.ip, nil
|
||||
@@ -43,39 +69,35 @@ func (p *Pool) AssignIP(mac net.HardwareAddr) (net.IP, error) {
|
||||
|
||||
idx := rand.Intn(len(p.items))
|
||||
for _, item := range p.items[idx:] {
|
||||
if ip = item.take(mac); ip != nil {
|
||||
return ip, nil
|
||||
if item.mac == nil {
|
||||
item.mac = mac
|
||||
item.expire = time.Now().Add(p.lease)
|
||||
return item.ip, nil
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range p.items {
|
||||
if ip = item.take(mac); ip != nil {
|
||||
return ip, nil
|
||||
if item.mac == nil {
|
||||
item.mac = mac
|
||||
item.expire = time.Now().Add(p.lease)
|
||||
return item.ip, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("no more ip can be assigned")
|
||||
|
||||
return nil, errors.New("no more ip can be leased")
|
||||
}
|
||||
|
||||
type item struct {
|
||||
taken bool
|
||||
ip net.IP
|
||||
lease time.Duration
|
||||
mac net.HardwareAddr
|
||||
}
|
||||
// ReleaseIP releases ip from pool according to the given mac.
|
||||
func (p *Pool) ReleaseIP(mac net.HardwareAddr) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
func (i *item) take(addr net.HardwareAddr) net.IP {
|
||||
if !i.taken {
|
||||
i.taken = true
|
||||
go func() {
|
||||
timer := time.NewTimer(i.lease)
|
||||
<-timer.C
|
||||
i.mac = nil
|
||||
i.taken = false
|
||||
}()
|
||||
i.mac = addr
|
||||
return i.ip
|
||||
for _, item := range p.items {
|
||||
if bytes.Equal(mac, item.mac) {
|
||||
item.mac = nil
|
||||
item.expire = time.Time{}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ip2num(ip net.IP) uint32 {
|
||||
|
||||
Reference in new issue
Block a user