summaryrefslogtreecommitdiff
path: root/osdep/BackgroundResolver.hpp
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2015-05-21 13:44:33 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2015-05-21 13:44:33 -0700
commit2ad69237cfc1f716351464dd05839699c066b80a (patch)
tree1612605cfb7f4bb5875c6b9ab5671a93cf99be99 /osdep/BackgroundResolver.hpp
parent82f6b3f550fe7612bf5043e1c3361f74f1fa9244 (diff)
downloadinfinitytier-2ad69237cfc1f716351464dd05839699c066b80a.tar.gz
infinitytier-2ad69237cfc1f716351464dd05839699c066b80a.zip
Background resolver.
Diffstat (limited to 'osdep/BackgroundResolver.hpp')
-rw-r--r--osdep/BackgroundResolver.hpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/osdep/BackgroundResolver.hpp b/osdep/BackgroundResolver.hpp
new file mode 100644
index 00000000..7909d271
--- /dev/null
+++ b/osdep/BackgroundResolver.hpp
@@ -0,0 +1,127 @@
+/*
+ * ZeroTier One - Network Virtualization Everywhere
+ * Copyright (C) 2011-2015 ZeroTier, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * --
+ *
+ * ZeroTier may be used and distributed under the terms of the GPLv3, which
+ * are available at: http://www.gnu.org/licenses/gpl-3.0.html
+ *
+ * If you would like to embed ZeroTier into a commercial application or
+ * redistribute it in a modified binary form, please contact ZeroTier Networks
+ * LLC. Start here: http://www.zerotier.com/
+ */
+
+#ifndef ZT_BACKGROUNDRESOLVER_HPP
+#define ZT_BACKGROUNDRESOLVER_HPP
+
+#include <vector>
+#include <string>
+
+#include "../node/Constants.hpp"
+#include "../node/Mutex.hpp"
+#include "../node/InetAddress.hpp"
+#include "../node/NonCopyable.hpp"
+#include "Thread.hpp"
+
+namespace ZeroTier {
+
+class BackgroundResolverJob;
+
+/**
+ * A simple background resolver
+ */
+class BackgroundResolver : NonCopyable
+{
+ friend class BackgroundResolverJob;
+
+public:
+ /**
+ * Construct a new resolver
+ *
+ * resolveNow() must be called to actually initiate background resolution.
+ *
+ * @param name Name to resolve
+ */
+ BackgroundResolver(const char *name);
+
+ ~BackgroundResolver();
+
+ /**
+ * @return Most recent resolver results or empty vector if none
+ */
+ std::vector<InetAddress> get() const;
+
+ /**
+ * Launch a background resolve job now
+ *
+ * If a resolve job is currently in progress, it is aborted and another
+ * job is started.
+ *
+ * Note that jobs can't actually be aborted due to the limitations of the
+ * ancient synchronous OS resolver APIs. As a result, in progress jobs
+ * that are aborted are simply abandoned. Don't call this too frequently
+ * or background threads might pile up.
+ *
+ * @param callback Callback function to receive notification or NULL if none
+ * @praam arg Second argument to callback function
+ */
+ void resolveNow(void (*callback)(BackgroundResolver *,void *),void *arg);
+
+ /**
+ * Abort (abandon) any current resolve jobs
+ */
+ void abort();
+
+ /**
+ * @return True if a background job is in progress
+ */
+ inline bool running() const
+ {
+ Mutex::Lock _l(_lock);
+ return (_job != (BackgroundResolverJob *)0);
+ }
+
+ /**
+ * Wait for pending job to complete (if any)
+ */
+ inline void wait() const
+ {
+ Thread t;
+ {
+ Mutex::Lock _l(_lock);
+ if (!_job)
+ return;
+ t = _jobThread;
+ }
+ Thread::join(t);
+ }
+
+private:
+ void _postResult(const std::vector<InetAddress> &ips);
+
+ std::string _name;
+ BackgroundResolverJob *_job;
+ Thread _jobThread;
+ void (*_callback)(BackgroundResolver *,void *);
+ void *_arg;
+ std::vector<InetAddress> _ips;
+ Mutex _lock;
+};
+
+} // namespace ZeroTier
+
+#endif