plugin.go 1.1 KB

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