home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / NSISdl / asyncdns.cpp next >
C/C++ Source or Header  |  2002-12-04  |  2KB  |  77 lines

  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2001 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: asyncdns.cpp - JNL portable asynchronous DNS implementation
  6. ** License: see jnetlib.h
  7. */
  8.  
  9.  
  10. #include "netinc.h"
  11. #include "util.h"
  12. #include "asyncdns.h"
  13.  
  14. JNL_AsyncDNS::JNL_AsyncDNS(int max_cache_entries)
  15. {
  16.   m_thread_kill=1;
  17.   m_thread=0;
  18.   m_addr=0;
  19.   m_hostname[0]=0;
  20. }
  21.  
  22. JNL_AsyncDNS::~JNL_AsyncDNS()
  23. {
  24.   m_thread_kill=1;
  25.  
  26.   if (m_thread)
  27.   {
  28.     WaitForSingleObject(m_thread,INFINITE);
  29.     CloseHandle(m_thread);
  30.   }
  31. }
  32.  
  33. unsigned long WINAPI JNL_AsyncDNS::_threadfunc(LPVOID _d)
  34. {
  35.   JNL_AsyncDNS *_this=(JNL_AsyncDNS*)_d;
  36.   struct hostent *hostentry;
  37.   hostentry=::gethostbyname(_this->m_hostname);
  38.   if (hostentry)
  39.   {
  40.     _this->m_addr=*((int*)hostentry->h_addr);
  41.   }
  42.   else
  43.     _this->m_addr=INADDR_NONE;
  44.   _this->m_thread_kill=1;
  45.   return 0;
  46. }
  47.  
  48. int JNL_AsyncDNS::resolve(char *hostname, unsigned long *addr)
  49. {
  50.   // return 0 on success, 1 on wait, -1 on unresolvable
  51.   unsigned long ip=inet_addr(hostname);
  52.   if (ip != INADDR_NONE) 
  53.   {
  54.     *addr=ip;
  55.     return 0;
  56.   }
  57.  
  58.   if (lstrcmpi(m_hostname,hostname)) m_addr=0;
  59.   else if (m_addr == INADDR_NONE) return -1;
  60.   else if (m_addr)
  61.   {
  62.     *addr=m_addr;
  63.     return 0;
  64.   }
  65.   lstrcpy(m_hostname,hostname);
  66.  
  67.   if (m_thread_kill)
  68.   {
  69.     DWORD id;
  70.     if (m_thread) return -1;
  71.     m_thread_kill=0;
  72.     m_thread=CreateThread(NULL,0,_threadfunc,(LPVOID)this,0,&id);
  73.     if (!m_thread) return -1;
  74.   }
  75.   return 1;
  76. }  
  77.