BackgroundResolver.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_BACKGROUNDRESOLVER_HPP
  28. #define ZT_BACKGROUNDRESOLVER_HPP
  29. #include <vector>
  30. #include <string>
  31. #include "../node/Constants.hpp"
  32. #include "../node/Mutex.hpp"
  33. #include "../node/InetAddress.hpp"
  34. #include "../node/NonCopyable.hpp"
  35. #include "Thread.hpp"
  36. namespace ZeroTier {
  37. class BackgroundResolverJob;
  38. /**
  39. * A simple background resolver
  40. */
  41. class BackgroundResolver : NonCopyable
  42. {
  43. friend class BackgroundResolverJob;
  44. public:
  45. /**
  46. * Construct a new resolver
  47. *
  48. * resolveNow() must be called to actually initiate background resolution.
  49. *
  50. * @param name Name to resolve
  51. */
  52. BackgroundResolver(const char *name);
  53. ~BackgroundResolver();
  54. /**
  55. * @return Most recent resolver results or empty vector if none
  56. */
  57. std::vector<InetAddress> get() const;
  58. /**
  59. * Launch a background resolve job now
  60. *
  61. * If a resolve job is currently in progress, it is aborted and another
  62. * job is started.
  63. *
  64. * Note that jobs can't actually be aborted due to the limitations of the
  65. * ancient synchronous OS resolver APIs. As a result, in progress jobs
  66. * that are aborted are simply abandoned. Don't call this too frequently
  67. * or background threads might pile up.
  68. *
  69. * @param callback Callback function to receive notification or NULL if none
  70. * @praam arg Second argument to callback function
  71. */
  72. void resolveNow(void (*callback)(BackgroundResolver *,void *) = 0,void *arg = 0);
  73. /**
  74. * Abort (abandon) any current resolve jobs
  75. */
  76. void abort();
  77. /**
  78. * @return True if a background job is in progress
  79. */
  80. inline bool running() const
  81. {
  82. Mutex::Lock _l(_lock);
  83. return (_job != (BackgroundResolverJob *)0);
  84. }
  85. /**
  86. * Wait for pending job to complete (if any)
  87. */
  88. inline void wait() const
  89. {
  90. Thread t;
  91. {
  92. Mutex::Lock _l(_lock);
  93. if (!_job)
  94. return;
  95. t = _jobThread;
  96. }
  97. Thread::join(t);
  98. }
  99. private:
  100. void _postResult(const std::vector<InetAddress> &ips);
  101. std::string _name;
  102. BackgroundResolverJob *_job;
  103. Thread _jobThread;
  104. void (*_callback)(BackgroundResolver *,void *);
  105. void *_arg;
  106. std::vector<InetAddress> _ips;
  107. Mutex _lock;
  108. };
  109. } // namespace ZeroTier
  110. #endif