plugin.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. func init() {
  16. plugins.RegisterPlugin("router", setupRouter6, setupRouter4)
  17. }
  18. var (
  19. routers []net.IP
  20. )
  21. func setupRouter6(args ...string) (handler.Handler6, error) {
  22. // TODO setup function for IPv6
  23. log.Warning("not implemented for IPv6")
  24. return Handler6, nil
  25. }
  26. func setupRouter4(args ...string) (handler.Handler4, error) {
  27. log.Printf("loaded plugin for DHCPv4.")
  28. if len(args) < 1 {
  29. return nil, errors.New("need at least one router IP address")
  30. }
  31. for _, arg := range args {
  32. router := net.ParseIP(arg)
  33. if router.To4() == nil {
  34. return Handler4, errors.New("expected an router IP address, got: " + arg)
  35. }
  36. routers = append(routers, router)
  37. }
  38. log.Infof("loaded %d router IP addresses.", len(routers))
  39. return Handler4, nil
  40. }
  41. // Handler6 not implemented only IPv4
  42. func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  43. // TODO add router IPv6 addresses to the response
  44. return resp, false
  45. }
  46. //Handler4 handles DHCPv4 packets for the router plugin
  47. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  48. resp.Options.Update(dhcpv4.OptRouter(routers...))
  49. return resp, false
  50. }