plugin.go 1.4 KB

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