plugin.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 example
  5. // This is an example plugin that inspects a packet and prints it out. The code
  6. // is commented in a way that should walk you through the implementation of your
  7. // own plugins.
  8. // Feedback is welcome!
  9. import (
  10. "github.com/coredhcp/coredhcp/handler"
  11. "github.com/coredhcp/coredhcp/logger"
  12. "github.com/coredhcp/coredhcp/plugins"
  13. "github.com/insomniacslk/dhcp/dhcpv4"
  14. "github.com/insomniacslk/dhcp/dhcpv6"
  15. )
  16. // We use a customizable logger, as part of the `logger` package. You can use
  17. // `logger.GetLogger()` to get a singleton instance of the logger. Then just use
  18. // it with the `logrus` interface (https://github.com/sirupsen/logrus). More
  19. // information in the docstring of the logger package.
  20. var log = logger.GetLogger("plugins/example")
  21. // In the main package, you need to register your plugin at import time. To do
  22. // this, just do a blank import of this package, e.g.
  23. // import (
  24. // _ "github.com/coredhcp/coredhcp/plugins/example"
  25. // )
  26. //
  27. // This guarantees that `init` will be called at import time, and your plugin
  28. // is correctly registered.
  29. //
  30. // The `init` function then should call `plugins.RegisterPlugin`, specifying the
  31. // plugin name, the setup function for DHCPv6 packets, and the setup function
  32. // for DHCPv4 packets. The setup functions must implement the
  33. // `plugin.SetupFunc6` and `plugin.Setup4` interfaces.
  34. // A `nil` setup function means that that protocol won't be handled by this
  35. // plugin.
  36. //
  37. // Note that importing the plugin is not enough: you have to explicitly specify
  38. // its use in the `config.yml` file, in the plugins section. For example:
  39. //
  40. // server6:
  41. // listen: '[::]547'
  42. // - example:
  43. // - server_id: LL aa:bb:cc:dd:ee:ff
  44. // - file: "leases.txt"
  45. //
  46. func init() {
  47. plugins.RegisterPlugin("example", setupExample6, setupExample4)
  48. }
  49. // setupExample6 is the setup function to initialize the handler for DHCPv6
  50. // traffic. This function implements the `plugin.SetupFunc6` interface.
  51. // This function returns a `handler.Handler6` function, and an error if any.
  52. // In this example we do very little in the setup function, and just return the
  53. // `exampleHandler6` function. Such function will be called for every DHCPv6
  54. // packet that the server receives. Remember that a handler may not be called
  55. // for each packet, if the handler chain is interrupted before reaching it.
  56. func setupExample6(args ...string) (handler.Handler6, error) {
  57. log.Printf("loaded plugin for DHCPv6.")
  58. return exampleHandler6, nil
  59. }
  60. // setupExample4 behaves like setupExample6, but for DHCPv4 packets. It
  61. // implements the `plugin.SetupFunc4` interface.
  62. func setupExample4(args ...string) (handler.Handler4, error) {
  63. log.Printf("loaded plugin for DHCPv4.")
  64. return exampleHandler4, nil
  65. }
  66. // exampleHandler6 handles DHCPv6 packets for the example plugin. It implements
  67. // the `handler.Handler6` interface. The input arguments are the request packet
  68. // that the server received from a client, and the response packet that has been
  69. // computed so far. This function returns the response packet to be sent back to
  70. // the client, and a boolean.
  71. // The response can be either the same response packet received as input, a
  72. // modified response packet, or nil. If nil, the server will not reply to the
  73. // client, basically dropping the request.
  74. // The returned boolean indicates to the server whether the chain of plugins
  75. // should continue or not. If `true`, the server will stop at this plugin, and
  76. // respond to the client (or drop the response, if nil). If `false`, the server
  77. // will call the next plugin in the chan, using the returned response packet as
  78. // input for the next plugin.
  79. func exampleHandler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  80. log.Printf("received DHCPv6 packet: %s", req.Summary())
  81. // return the unmodified response, and false. This means that the next
  82. // plugin in the chain will be called, and the unmodified response packet
  83. // will be used as its input.
  84. return resp, false
  85. }
  86. // exampleHandler4 behaves like exampleHandler6, but for DHCPv4 packets. It
  87. // implements the `handler.Handler4` interface.
  88. func exampleHandler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  89. log.Printf("received DHCPv4 packet: %s", req.Summary())
  90. // return the unmodified response, and false. This means that the next
  91. // plugin in the chain will be called, and the unmodified response packet
  92. // will be used as its input.
  93. return resp, false
  94. }