BackgroundResolver.cpp 2.6 KB

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