plugin.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 searchdomains
  5. // This is an searchdomains plugin that adds default DNS search domains.
  6. import (
  7. "github.com/coredhcp/coredhcp/handler"
  8. "github.com/coredhcp/coredhcp/logger"
  9. "github.com/coredhcp/coredhcp/plugins"
  10. "github.com/insomniacslk/dhcp/dhcpv4"
  11. "github.com/insomniacslk/dhcp/dhcpv6"
  12. "github.com/insomniacslk/dhcp/rfc1035label"
  13. )
  14. var log = logger.GetLogger("plugins/searchdomains")
  15. // Plugin wraps the default DNS search domain options.
  16. // Note that importing the plugin is not enough to use it: you have to
  17. // explicitly specify the intention to use it in the `config.yml` file, in the
  18. // plugins section. For searchdomains:
  19. //
  20. // server6:
  21. // listen: '[::]547'
  22. // - searchdomains: domain.a domain.b
  23. // - server_id: LL aa:bb:cc:dd:ee:ff
  24. // - file: "leases.txt"
  25. //
  26. var Plugin = plugins.Plugin{
  27. Name: "searchdomains",
  28. Setup6: setup6,
  29. Setup4: setup4,
  30. }
  31. // These are the DNS search domains that are set by the plugin.
  32. // Note that DHCPv4 and DHCPv6 options are totally independent.
  33. // If you need the same settings for both, you'll need to configure
  34. // this plugin once for the v4 and once for the v6 server.
  35. var v4SearchList []string
  36. var v6SearchList []string
  37. // copySlice creates a new copy of a string slice in memory.
  38. // This helps to ensure that downstream plugins can't corrupt
  39. // this plugin's configuration
  40. func copySlice(original []string) []string {
  41. copied := make([]string, len(original))
  42. copy(copied, original)
  43. return copied
  44. }
  45. func setup6(args ...string) (handler.Handler6, error) {
  46. v6SearchList = args
  47. log.Printf("Registered domain search list (DHCPv6) %s", v6SearchList)
  48. return domainSearchListHandler6, nil
  49. }
  50. func setup4(args ...string) (handler.Handler4, error) {
  51. v4SearchList = args
  52. log.Printf("Registered domain search list (DHCPv4) %s", v4SearchList)
  53. return domainSearchListHandler4, nil
  54. }
  55. func domainSearchListHandler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  56. resp.UpdateOption(dhcpv6.OptDomainSearchList(&rfc1035label.Labels{
  57. Labels: copySlice(v6SearchList),
  58. }))
  59. return resp, false
  60. }
  61. func domainSearchListHandler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  62. resp.UpdateOption(dhcpv4.OptDomainSearch(&rfc1035label.Labels{
  63. Labels: copySlice(v4SearchList),
  64. }))
  65. return resp, false
  66. }