plugin.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package plugins
  2. import (
  3. "fmt"
  4. "github.com/coredhcp/coredhcp/handler"
  5. "github.com/coredhcp/coredhcp/logger"
  6. )
  7. var log = logger.GetLogger()
  8. // Plugin represents a plugin object.
  9. // Setup6 and Setup4 are the setup functions for DHCPv6 and DHCPv4 handlers
  10. // respectively. Both setup functions can be nil.
  11. type Plugin struct {
  12. Name string
  13. Setup6 SetupFunc6
  14. Setup4 SetupFunc4
  15. }
  16. // RegisteredPlugins maps a plugin name to a Plugin instance.
  17. var RegisteredPlugins = make(map[string]*Plugin, 0)
  18. // SetupFunc6 defines a plugin setup function for DHCPv6
  19. type SetupFunc6 func(args ...string) (handler.Handler6, error)
  20. // SetupFunc4 defines a plugin setup function for DHCPv6
  21. type SetupFunc4 func(args ...string) (handler.Handler4, error)
  22. // RegisterPlugin registers a plugin by its name and setup functions.
  23. func RegisterPlugin(name string, setup6 SetupFunc6, setup4 SetupFunc4) error {
  24. log.Printf("Registering plugin \"%s\"", name)
  25. if _, ok := RegisteredPlugins[name]; ok {
  26. return fmt.Errorf("Plugin \"%s\" already registered", name)
  27. }
  28. plugin := Plugin{
  29. Name: name,
  30. Setup6: setup6,
  31. Setup4: setup4,
  32. }
  33. RegisteredPlugins[name] = &plugin
  34. return nil
  35. }