mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
dhcpd: support static ip address for mac
This commit is contained in:
1 parent
41fee381d0
commit
d92e7f6191
6 files changed
+48
-11
No files matched your search
+24
-5
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/insomniacslk/dhcp/dhcpv4"
|
||||
@@ -24,29 +26,46 @@ type dpcpd struct{}
|
||||
|
||||
// Run runs the service.
|
||||
func (*dpcpd) Run(args ...string) {
|
||||
if len(args) < 3 {
|
||||
if len(args) < 4 {
|
||||
log.F("[dhcpd] not enough parameters, exiting")
|
||||
return
|
||||
}
|
||||
|
||||
iface := args[0]
|
||||
iface, startIP, endIP, leaseMin := args[0], args[1], args[2], args[3]
|
||||
if i, err := strconv.Atoi(leaseMin); err != nil {
|
||||
leaseTime = time.Duration(i) * time.Minute
|
||||
}
|
||||
|
||||
ip, mask, err := intfaceIP4(iface)
|
||||
if err != nil {
|
||||
log.F("[dhcpd] get ip of interface '%s' error: %s", iface, err)
|
||||
return
|
||||
}
|
||||
|
||||
if findExistServer(iface) {
|
||||
if existsServer(iface) {
|
||||
log.F("[dhcpd] found existing dhcp server on interface %s, service exiting", iface)
|
||||
return
|
||||
}
|
||||
|
||||
pool, err := NewPool(leaseTime, net.ParseIP(args[1]), net.ParseIP(args[2]))
|
||||
pool, err := NewPool(leaseTime, net.ParseIP(startIP), net.ParseIP(endIP))
|
||||
if err != nil {
|
||||
log.F("[dhcpd] error in pool init: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// static ips
|
||||
for _, host := range args[4:] {
|
||||
pair := strings.Split(host, "=")
|
||||
if len(pair) == 2 {
|
||||
mac, err := net.ParseMAC(pair[0])
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
ip := net.ParseIP(pair[1])
|
||||
pool.LeaseStaticIP(mac, ip)
|
||||
}
|
||||
}
|
||||
|
||||
laddr := net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 67}
|
||||
server, err := server4.NewServer(iface, &laddr, handleDHCP(ip, mask, pool))
|
||||
if err != nil {
|
||||
@@ -118,7 +137,7 @@ func handleDHCP(serverIP net.IP, mask net.IPMask, pool *Pool) server4.Handler {
|
||||
}
|
||||
}
|
||||
|
||||
func findExistServer(iface string) (exists bool) {
|
||||
func existsServer(iface string) (exists bool) {
|
||||
client, err := nclient4.New(iface)
|
||||
if err != nil {
|
||||
log.F("[dhcpd] failed in dhcp client creation: %s", err)
|
||||
|
||||
@@ -87,6 +87,19 @@ func (p *Pool) LeaseIP(mac net.HardwareAddr) (net.IP, error) {
|
||||
return nil, errors.New("no more ip can be leased")
|
||||
}
|
||||
|
||||
// LeaseStaticIP leases static ip from pool according to the given mac.
|
||||
func (p *Pool) LeaseStaticIP(mac net.HardwareAddr, ip net.IP) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
for _, item := range p.items {
|
||||
if item.ip.Equal(ip) {
|
||||
item.mac = mac
|
||||
item.expire = time.Now().Add(time.Hour * 24 * 365 * 50) // 50 years
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReleaseIP releases ip from pool according to the given mac.
|
||||
func (p *Pool) ReleaseIP(mac net.HardwareAddr) {
|
||||
p.mutex.Lock()
|
||||
|
||||
Reference in new issue
Block a user