add all files from Hong

This commit is contained in:
zhangsz
2025-06-30 09:23:28 +08:00
parent ceb1fe2640
commit 9b7d32fbd9
69 changed files with 7280 additions and 0 deletions

50
plugins/mtu/plugin.go Normal file
View File

@@ -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
}

View File

@@ -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)
}
}