plugin.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. package mtu
  5. import (
  6. "errors"
  7. "fmt"
  8. "strconv"
  9. "github.com/insomniacslk/dhcp/dhcpv4"
  10. "github.com/coredhcp/coredhcp/handler"
  11. "github.com/coredhcp/coredhcp/logger"
  12. "github.com/coredhcp/coredhcp/plugins"
  13. )
  14. var log = logger.GetLogger("plugins/mtu")
  15. // Plugin wraps the MTU plugin information.
  16. var Plugin = plugins.Plugin{
  17. Name: "mtu",
  18. Setup4: setup4,
  19. // No Setup6 since DHCPv6 does not have MTU-related options
  20. }
  21. var (
  22. mtu int
  23. )
  24. func setup4(args ...string) (handler.Handler4, error) {
  25. if len(args) != 1 {
  26. return nil, errors.New("need one mtu value")
  27. }
  28. var err error
  29. if mtu, err = strconv.Atoi(args[0]); err != nil {
  30. return nil, fmt.Errorf("invalid mtu: %v", args[0])
  31. }
  32. log.Infof("loaded mtu %d.", mtu)
  33. return Handler4, nil
  34. }
  35. // Handler4 handles DHCPv4 packets for the mtu plugin
  36. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  37. if req.IsOptionRequested(dhcpv4.OptionInterfaceMTU) {
  38. resp.Options.Update(dhcpv4.Option{Code: dhcpv4.OptionInterfaceMTU, Value: dhcpv4.Uint16(mtu)})
  39. }
  40. return resp, false
  41. }