plugin.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 plugins
  5. import (
  6. "github.com/coredhcp/coredhcp/handler"
  7. "github.com/coredhcp/coredhcp/logger"
  8. )
  9. var log = logger.GetLogger("plugins")
  10. // Plugin represents a plugin object.
  11. // Setup6 and Setup4 are the setup functions for DHCPv6 and DHCPv4 handlers
  12. // respectively. Both setup functions can be nil.
  13. type Plugin struct {
  14. Name string
  15. Setup6 SetupFunc6
  16. Setup4 SetupFunc4
  17. }
  18. // RegisteredPlugins maps a plugin name to a Plugin instance.
  19. var RegisteredPlugins = make(map[string]*Plugin)
  20. // SetupFunc6 defines a plugin setup function for DHCPv6
  21. type SetupFunc6 func(args ...string) (handler.Handler6, error)
  22. // SetupFunc4 defines a plugin setup function for DHCPv6
  23. type SetupFunc4 func(args ...string) (handler.Handler4, error)
  24. // RegisterPlugin registers a plugin by its name and setup functions.
  25. func RegisterPlugin(name string, setup6 SetupFunc6, setup4 SetupFunc4) {
  26. log.Printf("Registering plugin \"%s\"", name)
  27. if _, ok := RegisteredPlugins[name]; ok {
  28. // TODO this highlights that asking the plugins to register themselves
  29. // is not the right approach. Need to register them in the main program.
  30. log.Panicf("Plugin '%s' is already registered", name)
  31. }
  32. plugin := Plugin{
  33. Name: name,
  34. Setup6: setup6,
  35. Setup4: setup4,
  36. }
  37. RegisteredPlugins[name] = &plugin
  38. }