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

View File

@@ -0,0 +1,64 @@
// 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 ipv6only
// This plugin implements RFC8925: if the client has requested the
// IPv6-Only Preferred option, then add the option response and then
// terminate processing immediately.
//
// This module should be invoked *before* any IP address
// allocation has been done, so that the yiaddr is 0.0.0.0 and
// no pool addresses are consumed for compatible clients.
//
// The optional argument is the V6ONLY_WAIT configuration variable,
// described in RFC8925 section 3.2.
import (
"errors"
"time"
"github.com/coredhcp/coredhcp/handler"
"github.com/coredhcp/coredhcp/logger"
"github.com/coredhcp/coredhcp/plugins"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/sirupsen/logrus"
)
var log = logger.GetLogger("plugins/ipv6only")
var v6only_wait time.Duration
var Plugin = plugins.Plugin{
Name: "ipv6only",
Setup4: setup4,
}
func setup4(args ...string) (handler.Handler4, error) {
if len(args) > 0 {
dur, err := time.ParseDuration(args[0])
if err != nil {
log.Errorf("invalid duration: %v", args[0])
return nil, errors.New("ipv6only failed to initialize")
}
v6only_wait = dur
}
if len(args) > 1 {
return nil, errors.New("too many arguments")
}
return Handler4, nil
}
func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
v6pref := req.IsOptionRequested(dhcpv4.OptionIPv6OnlyPreferred)
log.WithFields(logrus.Fields{
"mac": req.ClientHWAddr.String(),
"ipv6only": v6pref,
}).Debug("ipv6only status")
if v6pref {
resp.UpdateOption(dhcpv4.OptIPv6OnlyPreferred(v6only_wait))
return resp, true
}
return resp, false
}

View File

@@ -0,0 +1,65 @@
// 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 ipv6only
import (
"bytes"
"net"
"testing"
"time"
"github.com/insomniacslk/dhcp/dhcpv4"
)
func TestOptionRequested(t *testing.T) {
req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
if err != nil {
t.Fatal(err)
}
req.UpdateOption(dhcpv4.OptParameterRequestList(dhcpv4.OptionBroadcastAddress, dhcpv4.OptionIPv6OnlyPreferred))
stub, err := dhcpv4.NewReplyFromRequest(req)
if err != nil {
t.Fatal(err)
}
v6only_wait = 0x1234 * time.Second
resp, stop := Handler4(req, stub)
if resp == nil {
t.Fatal("plugin did not return a message")
}
if !stop {
t.Error("plugin did not interrupt processing")
}
opt := resp.Options.Get(dhcpv4.OptionIPv6OnlyPreferred)
if opt == nil {
t.Fatal("plugin did not return the IPv6-Only Preferred option")
}
if !bytes.Equal(opt, []byte{0x00, 0x00, 0x12, 0x34}) {
t.Errorf("plugin gave wrong option response: %v", opt)
}
}
func TestNotRequested(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)
}
resp, stop := Handler4(req, stub)
if resp == nil {
t.Fatal("plugin did not return a message")
}
if stop {
t.Error("plugin interrupted processing")
}
if resp.Options.Get(dhcpv4.OptionIPv6OnlyPreferred) != nil {
t.Error("Found IPv6-Only Preferred option when not requested")
}
}