+#include <simgear/threads/SGThread.hxx>
+
+using std::string;
+
+namespace {
+
+class Resolver : public SGThread
+{
+public:
+ static Resolver* instance()
+ {
+ if (!static_instance) {
+ static_instance = new Resolver;
+ atexit(&Resolver::cleanup);
+ static_instance->start();
+ }
+
+ return static_instance;
+ }
+
+ static void cleanup()
+ {
+ static_instance->shutdown();
+ static_instance->join();
+ }
+
+ Resolver() :
+ _done(false)
+ {
+ }
+
+ void shutdown()
+ {
+ _lock.lock();
+ _done = true;
+ _wait.signal();
+ _lock.unlock();
+ }
+
+ simgear::IPAddress* lookup(const string& host)
+ {
+ simgear::IPAddress* result = NULL;
+ _lock.lock();
+ AddressCache::iterator it = _cache.find(host);
+ if (it == _cache.end()) {
+ _cache[host] = NULL; // mark as needing looked up
+ _wait.signal(); // if the thread was sleeping, poke it
+ } else {
+ result = it->second;
+ }
+ _lock.unlock();
+ return result;
+ }
+
+ simgear::IPAddress* lookupSync(const string& host)
+ {
+ simgear::IPAddress* result = NULL;
+ _lock.lock();
+ AddressCache::iterator it = _cache.find(host);
+ if (it == _cache.end()) {
+ _lock.unlock();
+ result = new simgear::IPAddress;
+ bool ok = lookupHost(host.c_str(), *result);
+ _lock.lock();
+ if (ok) {
+ _cache[host] = result; // mark as needing looked up
+ } else {
+ delete result;
+ result = NULL;
+ }
+ } else { // found in cache, easy
+ result = it->second;
+ }
+ _lock.unlock();
+ return result;
+ }
+protected:
+ /**
+ * run method waits on a condition (_wait), and when awoken,
+ * finds any unresolved entries in _cache, resolves them, and goes
+ * back to sleep.
+ */
+ virtual void run()
+ {
+ _lock.lock();
+ while (!_done) {
+ AddressCache::iterator it;
+
+ for (it = _cache.begin(); it != _cache.end(); ++it) {
+ if (it->second == NULL) {
+ string h = it->first;
+
+ _lock.unlock();
+ simgear::IPAddress* addr = new simgear::IPAddress;
+ // may take seconds or even minutes!
+ lookupHost(h.c_str(), *addr);
+ _lock.lock();
+
+ // cahce may have changed while we had the lock released -
+ // so iterators may be invalid: restart the traversal
+ it = _cache.begin();
+ _cache[h] = addr;
+ } // of found un-resolved entry
+ } // of un-resolved address iteration
+ _wait.wait(_lock);
+ } // of thread run loop
+ _lock.unlock();
+ }
+private:
+ static Resolver* static_instance;
+
+ /**
+ * The actual synchronous, blocking host lookup function
+ * do *not* call this with any locks (mutexs) held, since depending
+ * on local system configuration / network availability, it
+ * may block for seconds or minutes.
+ */
+ bool lookupHost(const char* host, simgear::IPAddress& addr)
+ {
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_family = AF_INET;
+ bool ok = false;
+
+ struct addrinfo* result0 = NULL;
+ int err = getaddrinfo(host, NULL, &hints, &result0);
+ if (err) {
+ SG_LOG(SG_IO, SG_WARN, "getaddrinfo failed for '" << host << "' : " << gai_strerror(err));
+ return false;
+ } else {
+ struct addrinfo* result;
+ for (result = result0; result != NULL; result = result->ai_next) {
+ if (result->ai_family != AF_INET) { // only accept IP4 for the moment
+ continue;
+ }
+
+ if (result->ai_addrlen != addr.getAddrLen()) {
+ SG_LOG(SG_IO, SG_ALERT, "mismatch in socket address sizes: got " <<
+ result->ai_addrlen << ", expected " << addr.getAddrLen());
+ continue;
+ }
+
+ memcpy(addr.getAddr(), result->ai_addr, result->ai_addrlen);
+ ok = true;
+ break;
+ } // of getaddrinfo results iteration
+ } // of getaddrinfo succeeded
+
+ freeaddrinfo(result0);
+ return ok;
+ }
+
+ SGMutex _lock;
+ SGWaitCondition _wait;
+
+ typedef std::map<string, simgear::IPAddress*> AddressCache;
+ AddressCache _cache;
+ bool _done;
+};
+
+Resolver* Resolver::static_instance = NULL;
+
+} // of anonymous namespace