Sfoglia il codice sorgente

plugins: add mtu (#149)

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
Adphi 4 anni fa
parent
commit
0f369d7237

+ 1 - 0
cmds/coredhcp-generator/core-plugins.txt

@@ -1,6 +1,7 @@
 github.com/coredhcp/coredhcp/plugins/dns
 github.com/coredhcp/coredhcp/plugins/file
 github.com/coredhcp/coredhcp/plugins/leasetime
+github.com/coredhcp/coredhcp/plugins/mtu
 github.com/coredhcp/coredhcp/plugins/netmask
 github.com/coredhcp/coredhcp/plugins/nbp
 github.com/coredhcp/coredhcp/plugins/prefix

+ 2 - 0
cmds/coredhcp/main.go

@@ -20,6 +20,7 @@ import (
 	pl_dns "github.com/coredhcp/coredhcp/plugins/dns"
 	pl_file "github.com/coredhcp/coredhcp/plugins/file"
 	pl_leasetime "github.com/coredhcp/coredhcp/plugins/leasetime"
+	pl_mtu "github.com/coredhcp/coredhcp/plugins/mtu"
 	pl_nbp "github.com/coredhcp/coredhcp/plugins/nbp"
 	pl_netmask "github.com/coredhcp/coredhcp/plugins/netmask"
 	pl_prefix "github.com/coredhcp/coredhcp/plugins/prefix"
@@ -63,6 +64,7 @@ var desiredPlugins = []*plugins.Plugin{
 	&pl_dns.Plugin,
 	&pl_file.Plugin,
 	&pl_leasetime.Plugin,
+	&pl_mtu.Plugin,
 	&pl_nbp.Plugin,
 	&pl_netmask.Plugin,
 	&pl_prefix.Plugin,

+ 50 - 0
plugins/mtu/plugin.go

@@ -0,0 +1,50 @@
+// 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 mtu
+
+import (
+	"errors"
+	"fmt"
+	"strconv"
+
+	"github.com/insomniacslk/dhcp/dhcpv4"
+
+	"github.com/coredhcp/coredhcp/handler"
+	"github.com/coredhcp/coredhcp/logger"
+	"github.com/coredhcp/coredhcp/plugins"
+)
+
+var log = logger.GetLogger("plugins/mtu")
+
+// Plugin wraps the MTU plugin information.
+var Plugin = plugins.Plugin{
+	Name:   "mtu",
+	Setup4: setup4,
+	// No Setup6 since DHCPv6 does not have MTU-related options
+}
+
+var (
+	mtu int
+)
+
+func setup4(args ...string) (handler.Handler4, error) {
+	if len(args) != 1 {
+		return nil, errors.New("need one mtu value")
+	}
+	var err error
+	if mtu, err = strconv.Atoi(args[0]); err != nil {
+		return nil, fmt.Errorf("invalid mtu: %v", args[0])
+	}
+	log.Infof("loaded mtu %d.", mtu)
+	return Handler4, nil
+}
+
+// Handler4 handles DHCPv4 packets for the mtu plugin
+func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
+	if req.IsOptionRequested(dhcpv4.OptionInterfaceMTU) {
+		resp.Options.Update(dhcpv4.Option{Code: dhcpv4.OptionInterfaceMTU, Value: dhcpv4.Uint16(mtu)})
+	}
+	return resp, false
+}

+ 66 - 0
plugins/mtu/plugin_test.go

@@ -0,0 +1,66 @@
+// 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 mtu
+
+import (
+	"net"
+	"testing"
+
+	"github.com/insomniacslk/dhcp/dhcpv4"
+)
+
+func TestAddServer4(t *testing.T) {
+	req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, dhcpv4.WithRequestedOptions(dhcpv4.OptionInterfaceMTU))
+	if err != nil {
+		t.Fatal(err)
+	}
+	stub, err := dhcpv4.NewReplyFromRequest(req)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	mtu = 1500
+
+	resp, stop := Handler4(req, stub)
+	if resp == nil {
+		t.Fatal("plugin did not return a message")
+	}
+	if stop {
+		t.Error("plugin interrupted processing")
+	}
+	rMTU, err := dhcpv4.GetUint16(dhcpv4.OptionInterfaceMTU, resp.Options)
+	if err != nil {
+		t.Errorf("Failed to retrieve mtu from response")
+	}
+
+	if mtu != int(rMTU) {
+		t.Errorf("Found %d mtu, expected %d", rMTU, mtu)
+	}
+}
+
+func TestNotRequested4(t *testing.T) {
+	req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
+	if err != nil {
+		t.Fatal(err)
+	}
+	stub, err := dhcpv4.NewReplyFromRequest(req)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	mtu = 1500
+	req.UpdateOption(dhcpv4.OptParameterRequestList(dhcpv4.OptionBroadcastAddress))
+
+	resp, stop := Handler4(req, stub)
+	if resp == nil {
+		t.Fatal("plugin did not return a message")
+	}
+	if stop {
+		t.Error("plugin interrupted processing")
+	}
+	if mtu, err := dhcpv4.GetUint16(dhcpv4.OptionInterfaceMTU, resp.Options); err == nil {
+		t.Errorf("Retrieve mtu %d in response, expected none", mtu)
+	}
+}