Ver Fonte

Initial commit of a proposed default lease time plugin. (#66)

* Initial commit of a proposed default lease time plugin.

Signed-off-by: kevin <kworm@missouri-telecom.com>
kworm83 há 6 anos atrás
pai
commit
6e2a8c4506
3 ficheiros alterados com 55 adições e 0 exclusões
  1. 1 0
      cmds/coredhcp/config.yml.example
  2. 1 0
      cmds/coredhcp/main.go
  3. 53 0
      plugins/lease_time/plugin.go

+ 1 - 0
cmds/coredhcp/config.yml.example

@@ -10,6 +10,7 @@ server4:
     listen: '0.0.0.0:67'
     interface: "eth0"
     plugins:
+        # - lease_time: 60s
         # - server_id: 10.10.10.1
         # - dns: 8.8.8.8 8.8.4.4
         # - router: 10.10.10.1

+ 1 - 0
cmds/coredhcp/main.go

@@ -13,6 +13,7 @@ import (
 	"github.com/coredhcp/coredhcp/logger"
 	_ "github.com/coredhcp/coredhcp/plugins/dns"
 	_ "github.com/coredhcp/coredhcp/plugins/file"
+	_ "github.com/coredhcp/coredhcp/plugins/lease_time"
 	_ "github.com/coredhcp/coredhcp/plugins/netmask"
 	_ "github.com/coredhcp/coredhcp/plugins/range"
 	_ "github.com/coredhcp/coredhcp/plugins/redis"

+ 53 - 0
plugins/lease_time/plugin.go

@@ -0,0 +1,53 @@
+// Copyright 2018-present the CoreDHCP Authors. All rights reserved
+// This source code is licensed under the MIT license found in the
+// LICENSE file in the root directory of this source tree.
+
+package leasetime
+
+import (
+	"errors"
+	"time"
+
+	"github.com/coredhcp/coredhcp/handler"
+	"github.com/coredhcp/coredhcp/logger"
+	"github.com/coredhcp/coredhcp/plugins"
+	"github.com/insomniacslk/dhcp/dhcpv4"
+)
+
+func init() {
+	plugins.RegisterPlugin("lease_time", nil, setupLeaseTime4)
+}
+
+var (
+	log         = logger.GetLogger("plugins/lease_time")
+	v4LeaseTime time.Duration
+)
+
+// Handler4 handles DHCPv4 packets for the lease_time plugin.
+func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
+	if req.OpCode != dhcpv4.OpcodeBootRequest {
+		return resp, false
+	}
+	// Set lease time unless it has already been set
+	if !resp.Options.Has(dhcpv4.OptionIPAddressLeaseTime) {
+		resp.Options.Update(dhcpv4.OptIPAddressLeaseTime(v4LeaseTime))
+	}
+	return resp, false
+}
+
+func setupLeaseTime4(args ...string) (handler.Handler4, error) {
+	log.Print("loading `lease_time` plugin for DHCPv4")
+	if len(args) < 1 {
+		log.Error("No default lease time provided")
+		return nil, errors.New("lease_time failed to initialize")
+	}
+
+	leaseTime, err := time.ParseDuration(args[0])
+	if err != nil {
+		log.Errorf("invalid duration: %v", args[0])
+		return nil, errors.New("lease_time failed to initialize")
+	}
+	v4LeaseTime = leaseTime
+
+	return Handler4, nil
+}