callback.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package router
  4. import (
  5. "sync"
  6. "tailscale.com/net/dns"
  7. )
  8. // CallbackRouter is an implementation of both Router and dns.OSConfigurator.
  9. // When either network or DNS settings are changed, SetBoth is called with both configs.
  10. // Mainly used as a shim for OSes that want to set both network and
  11. // DNS configuration simultaneously (Mac, iOS, Android).
  12. type CallbackRouter struct {
  13. SetBoth func(rcfg *Config, dcfg *dns.OSConfig) error
  14. SplitDNS bool
  15. // GetBaseConfigFunc optionally specifies a function to return the current DNS
  16. // config in response to GetBaseConfig.
  17. //
  18. // If nil, reading the current config isn't supported and GetBaseConfig()
  19. // will return ErrGetBaseConfigNotSupported.
  20. GetBaseConfigFunc func() (dns.OSConfig, error)
  21. // InitialMTU is the MTU the tun should be initialized with.
  22. // Zero means don't change the MTU from the default. This MTU
  23. // is applied only once, shortly after the TUN is created, and
  24. // ignored thereafter.
  25. InitialMTU uint32
  26. mu sync.Mutex // protects all the following
  27. didSetMTU bool // if we set the MTU already
  28. rcfg *Config // last applied router config
  29. dcfg *dns.OSConfig // last applied DNS config
  30. }
  31. // Up implements Router.
  32. func (r *CallbackRouter) Up() error {
  33. return nil // TODO: check that all callers have no need for initialization
  34. }
  35. // Set implements Router.
  36. func (r *CallbackRouter) Set(rcfg *Config) error {
  37. r.mu.Lock()
  38. defer r.mu.Unlock()
  39. if r.rcfg.Equal(rcfg) {
  40. return nil
  41. }
  42. if r.didSetMTU == false {
  43. r.didSetMTU = true
  44. rcfg.NewMTU = int(r.InitialMTU)
  45. }
  46. r.rcfg = rcfg
  47. return r.SetBoth(r.rcfg, r.dcfg)
  48. }
  49. // SetDNS implements dns.OSConfigurator.
  50. func (r *CallbackRouter) SetDNS(dcfg dns.OSConfig) error {
  51. r.mu.Lock()
  52. defer r.mu.Unlock()
  53. if r.dcfg != nil && r.dcfg.Equal(dcfg) {
  54. return nil
  55. }
  56. r.dcfg = &dcfg
  57. return r.SetBoth(r.rcfg, r.dcfg)
  58. }
  59. // SupportsSplitDNS implements dns.OSConfigurator.
  60. func (r *CallbackRouter) SupportsSplitDNS() bool {
  61. return r.SplitDNS
  62. }
  63. func (r *CallbackRouter) GetBaseConfig() (dns.OSConfig, error) {
  64. if r.GetBaseConfigFunc == nil {
  65. return dns.OSConfig{}, dns.ErrGetBaseConfigNotSupported
  66. }
  67. return r.GetBaseConfigFunc()
  68. }
  69. func (r *CallbackRouter) Close() error {
  70. return r.SetBoth(nil, nil) // TODO: check if makes sense
  71. }