BackgroundResolver.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_BACKGROUNDRESOLVER_HPP
  19. #define ZT_BACKGROUNDRESOLVER_HPP
  20. #include <vector>
  21. #include <string>
  22. #include "../node/Constants.hpp"
  23. #include "../node/Mutex.hpp"
  24. #include "../node/InetAddress.hpp"
  25. #include "../node/NonCopyable.hpp"
  26. #include "Thread.hpp"
  27. namespace ZeroTier {
  28. class BackgroundResolverJob;
  29. /**
  30. * A simple background resolver
  31. */
  32. class BackgroundResolver : NonCopyable
  33. {
  34. friend class BackgroundResolverJob;
  35. public:
  36. /**
  37. * Construct a new resolver
  38. *
  39. * resolveNow() must be called to actually initiate background resolution.
  40. *
  41. * @param name Name to resolve
  42. */
  43. BackgroundResolver(const char *name);
  44. ~BackgroundResolver();
  45. /**
  46. * @return Most recent resolver results or empty vector if none
  47. */
  48. std::vector<InetAddress> get() const;
  49. /**
  50. * Launch a background resolve job now
  51. *
  52. * If a resolve job is currently in progress, it is aborted and another
  53. * job is started.
  54. *
  55. * Note that jobs can't actually be aborted due to the limitations of the
  56. * ancient synchronous OS resolver APIs. As a result, in progress jobs
  57. * that are aborted are simply abandoned. Don't call this too frequently
  58. * or background threads might pile up.
  59. *
  60. * @param callback Callback function to receive notification or NULL if none
  61. * @praam arg Second argument to callback function
  62. */
  63. void resolveNow(void (*callback)(BackgroundResolver *,void *) = 0,void *arg = 0);
  64. /**
  65. * Abort (abandon) any current resolve jobs
  66. */
  67. void abort();
  68. /**
  69. * @return True if a background job is in progress
  70. */
  71. inline bool running() const
  72. {
  73. Mutex::Lock _l(_lock);
  74. return (_job != (BackgroundResolverJob *)0);
  75. }
  76. /**
  77. * Wait for pending job to complete (if any)
  78. */
  79. inline void wait() const
  80. {
  81. Thread t;
  82. {
  83. Mutex::Lock _l(_lock);
  84. if (!_job)
  85. return;
  86. t = _jobThread;
  87. }
  88. Thread::join(t);
  89. }
  90. private:
  91. void _postResult(const std::vector<InetAddress> &ips);
  92. std::string _name;
  93. BackgroundResolverJob *_job;
  94. Thread _jobThread;
  95. void (*_callback)(BackgroundResolver *,void *);
  96. void *_arg;
  97. std::vector<InetAddress> _ips;
  98. Mutex _lock;
  99. };
  100. } // namespace ZeroTier
  101. #endif