plugin.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/config"
  8. "github.com/coredhcp/coredhcp/handler"
  9. "github.com/coredhcp/coredhcp/logger"
  10. )
  11. var log = logger.GetLogger("plugins")
  12. // Plugin represents a plugin object.
  13. // Setup6 and Setup4 are the setup functions for DHCPv6 and DHCPv4 handlers
  14. // respectively. Both setup functions can be nil.
  15. type Plugin struct {
  16. Name string
  17. Setup6 SetupFunc6
  18. Setup4 SetupFunc4
  19. }
  20. // RegisteredPlugins maps a plugin name to a Plugin instance.
  21. var RegisteredPlugins = make(map[string]*Plugin)
  22. // SetupFunc6 defines a plugin setup function for DHCPv6
  23. type SetupFunc6 func(args ...string) (handler.Handler6, error)
  24. // SetupFunc4 defines a plugin setup function for DHCPv6
  25. type SetupFunc4 func(args ...string) (handler.Handler4, error)
  26. // RegisterPlugin registers a plugin.
  27. func RegisterPlugin(plugin *Plugin) error {
  28. if plugin == nil {
  29. return errors.New("cannot register nil plugin")
  30. }
  31. log.Printf("Registering plugin '%s'", plugin.Name)
  32. if _, ok := RegisteredPlugins[plugin.Name]; ok {
  33. // TODO this highlights that asking the plugins to register themselves
  34. // is not the right approach. Need to register them in the main program.
  35. log.Panicf("Plugin '%s' is already registered", plugin.Name)
  36. }
  37. RegisteredPlugins[plugin.Name] = plugin
  38. return nil
  39. }
  40. // LoadPlugins reads a Config object and loads the plugins as specified in the
  41. // `plugins` section, in order. For a plugin to be available, it must have been
  42. // previously registered with plugins.RegisterPlugin. This is normally done at
  43. // plugin import time.
  44. // This function returns the list of loaded v6 plugins, the list of loaded v4
  45. // plugins, and an error if any.
  46. func LoadPlugins(conf *config.Config) ([]handler.Handler4, []handler.Handler6, error) {
  47. log.Print("Loading plugins...")
  48. handlers4 := make([]handler.Handler4, 0)
  49. handlers6 := make([]handler.Handler6, 0)
  50. if conf.Server6 == nil && conf.Server4 == nil {
  51. return nil, nil, errors.New("no configuration found for either DHCPv6 or DHCPv4")
  52. }
  53. // now load the plugins. We need to call its setup function with
  54. // the arguments extracted above. The setup function is mapped in
  55. // plugins.RegisteredPlugins .
  56. // Load DHCPv6 plugins.
  57. if conf.Server6 != nil {
  58. for _, pluginConf := range conf.Server6.Plugins {
  59. if plugin, ok := RegisteredPlugins[pluginConf.Name]; ok {
  60. log.Printf("DHCPv6: loading plugin `%s`", pluginConf.Name)
  61. if plugin.Setup6 == nil {
  62. log.Warningf("DHCPv6: plugin `%s` has no setup function for DHCPv6", pluginConf.Name)
  63. continue
  64. }
  65. h6, err := plugin.Setup6(pluginConf.Args...)
  66. if err != nil {
  67. return nil, nil, err
  68. } else if h6 == nil {
  69. return nil, nil, config.ConfigErrorFromString("no DHCPv6 handler for plugin %s", pluginConf.Name)
  70. }
  71. handlers6 = append(handlers6, h6)
  72. } else {
  73. return nil, nil, config.ConfigErrorFromString("DHCPv6: unknown plugin `%s`", pluginConf.Name)
  74. }
  75. }
  76. }
  77. // Load DHCPv4 plugins. Yes, duplicated code, there's not really much that
  78. // can be deduplicated here.
  79. if conf.Server4 != nil {
  80. for _, pluginConf := range conf.Server4.Plugins {
  81. if plugin, ok := RegisteredPlugins[pluginConf.Name]; ok {
  82. log.Printf("DHCPv4: loading plugin `%s`", pluginConf.Name)
  83. if plugin.Setup4 == nil {
  84. log.Warningf("DHCPv4: plugin `%s` has no setup function for DHCPv4", pluginConf.Name)
  85. continue
  86. }
  87. h4, err := plugin.Setup4(pluginConf.Args...)
  88. if err != nil {
  89. return nil, nil, err
  90. } else if h4 == nil {
  91. return nil, nil, config.ConfigErrorFromString("no DHCPv4 handler for plugin %s", pluginConf.Name)
  92. }
  93. handlers4 = append(handlers4, h4)
  94. } else {
  95. return nil, nil, config.ConfigErrorFromString("DHCPv4: unknown plugin `%s`", pluginConf.Name)
  96. }
  97. }
  98. }
  99. return handlers4, handlers6, nil
  100. }