plugin.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 router
  5. import (
  6. "errors"
  7. "net"
  8. "github.com/coredhcp/coredhcp/handler"
  9. "github.com/coredhcp/coredhcp/logger"
  10. "github.com/coredhcp/coredhcp/plugins"
  11. "github.com/insomniacslk/dhcp/dhcpv4"
  12. "github.com/insomniacslk/dhcp/dhcpv6"
  13. )
  14. var log = logger.GetLogger("plugins/router")
  15. // Plugin wraps plugin registration information
  16. var Plugin = plugins.Plugin{
  17. Name: "router",
  18. Setup6: setup6,
  19. Setup4: setup4,
  20. }
  21. var (
  22. routers []net.IP
  23. )
  24. func setup6(args ...string) (handler.Handler6, error) {
  25. // TODO setup function for IPv6
  26. log.Warning("Not implemented for IPv6")
  27. return Handler6, nil
  28. }
  29. func setup4(args ...string) (handler.Handler4, error) {
  30. log.Printf("Loaded plugin for DHCPv4.")
  31. if len(args) < 1 {
  32. return nil, errors.New("need at least one router IP address")
  33. }
  34. for _, arg := range args {
  35. router := net.ParseIP(arg)
  36. if router.To4() == nil {
  37. return Handler4, errors.New("expected an router IP address, got: " + arg)
  38. }
  39. routers = append(routers, router)
  40. }
  41. log.Infof("loaded %d router IP addresses.", len(routers))
  42. return Handler4, nil
  43. }
  44. // Handler6 not implemented only IPv4
  45. func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  46. // TODO add router IPv6 addresses to the response
  47. return resp, false
  48. }
  49. //Handler4 handles DHCPv4 packets for the router plugin
  50. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  51. resp.Options.Update(dhcpv4.OptRouter(routers...))
  52. return resp, false
  53. }