BackgroundResolver.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include "OSUtils.hpp"
  28. #include "Thread.hpp"
  29. #include "BackgroundResolver.hpp"
  30. namespace ZeroTier {
  31. /*
  32. * We can't actually abort a job. This is a legacy characteristic of the
  33. * ancient synchronous resolver APIs. So to abort jobs, we just abandon
  34. * them by setting their parent to null.
  35. */
  36. class BackgroundResolverJob
  37. {
  38. public:
  39. std::string name;
  40. BackgroundResolver *volatile parent;
  41. Mutex lock;
  42. void threadMain()
  43. throw()
  44. {
  45. std::vector<InetAddress> ips;
  46. try {
  47. ips = OSUtils::resolve(name.c_str());
  48. } catch ( ... ) {}
  49. {
  50. Mutex::Lock _l(lock);
  51. BackgroundResolver *p = parent;
  52. if (p)
  53. p->_postResult(ips);
  54. }
  55. delete this;
  56. }
  57. };
  58. BackgroundResolver::BackgroundResolver(const char *name) :
  59. _name(name),
  60. _job((BackgroundResolverJob *)0),
  61. _callback(0),
  62. _arg((void *)0),
  63. _ips(),
  64. _lock()
  65. {
  66. }
  67. BackgroundResolver::~BackgroundResolver()
  68. {
  69. abort();
  70. }
  71. std::vector<InetAddress> BackgroundResolver::get() const
  72. {
  73. Mutex::Lock _l(_lock);
  74. return _ips;
  75. }
  76. void BackgroundResolver::resolveNow(void (*callback)(BackgroundResolver *,void *),void *arg)
  77. {
  78. Mutex::Lock _l(_lock);
  79. if (_job) {
  80. Mutex::Lock _l2(_job->lock);
  81. _job->parent = (BackgroundResolver *)0;
  82. _job = (BackgroundResolverJob *)0;
  83. }
  84. BackgroundResolverJob *j = new BackgroundResolverJob();
  85. j->name = _name;
  86. j->parent = this;
  87. _job = j;
  88. _callback = callback;
  89. _arg = arg;
  90. _jobThread = Thread::start(j);
  91. }
  92. void BackgroundResolver::abort()
  93. {
  94. Mutex::Lock _l(_lock);
  95. if (_job) {
  96. Mutex::Lock _l2(_job->lock);
  97. _job->parent = (BackgroundResolver *)0;
  98. _job = (BackgroundResolverJob *)0;
  99. }
  100. }
  101. void BackgroundResolver::_postResult(const std::vector<InetAddress> &ips)
  102. {
  103. void (*cb)(BackgroundResolver *,void *);
  104. void *a;
  105. {
  106. Mutex::Lock _l(_lock);
  107. _job = (BackgroundResolverJob *)0;
  108. cb = _callback;
  109. a = _arg;
  110. _ips = ips;
  111. }
  112. if (cb)
  113. cb(this,a);
  114. }
  115. } // namespace ZeroTier