| 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 License.txt |
| 7 |
** |
| 8 |
** Unicode support by Jim Park -- 08/24/2007 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Jim Park: For Unicode Support, all string functions must explicitly use |
| 12 |
// ANSI versions if UNICODE is defined. |
| 13 |
|
| 14 |
#include "netinc.h" |
| 15 |
#include "util.h" |
| 16 |
#include "asyncdns.h" |
| 17 |
|
| 18 |
JNL_AsyncDNS::JNL_AsyncDNS() |
| 19 |
{ |
| 20 |
m_thread=0; |
| 21 |
m_addr=0; |
| 22 |
m_hostname[0]=0; |
| 23 |
} |
| 24 |
|
| 25 |
JNL_AsyncDNS::~JNL_AsyncDNS() |
| 26 |
{ |
| 27 |
wait_for_thread_death(); |
| 28 |
} |
| 29 |
|
| 30 |
DWORD WINAPI JNL_AsyncDNS::_threadfunc(LPVOID _d) |
| 31 |
{ |
| 32 |
JNL_AsyncDNS *_this=(JNL_AsyncDNS*)_d; |
| 33 |
struct hostent *hostentry; |
| 34 |
hostentry=::gethostbyname(_this->m_hostname); |
| 35 |
if (hostentry) |
| 36 |
{ |
| 37 |
_this->m_addr=*((int*)hostentry->h_addr); |
| 38 |
} |
| 39 |
else |
| 40 |
_this->m_addr=INADDR_NONE; |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
int JNL_AsyncDNS::resolve(char *hostname, unsigned long *addr) |
| 45 |
{ |
| 46 |
// return 0 on success, 1 on wait, -1 on unresolvable |
| 47 |
unsigned long ip=inet_addr(hostname); |
| 48 |
if (ip != INADDR_NONE) |
| 49 |
{ |
| 50 |
*addr=ip; |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
if (lstrcmpiA(m_hostname,hostname)) m_addr=0; |
| 55 |
else if (m_addr == INADDR_NONE) |
| 56 |
{ |
| 57 |
wait_for_thread_death(); |
| 58 |
return -1; |
| 59 |
} |
| 60 |
else if (m_addr) |
| 61 |
{ |
| 62 |
*addr=m_addr; |
| 63 |
wait_for_thread_death(); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
lstrcpyA(m_hostname,hostname); |
| 67 |
|
| 68 |
if (!m_thread) |
| 69 |
{ |
| 70 |
DWORD id; |
| 71 |
m_thread=CreateThread(NULL,0,_threadfunc,(LPVOID)this,0,&id); |
| 72 |
if (!m_thread) return -1; |
| 73 |
} |
| 74 |
return 1; |
| 75 |
} |
| 76 |
|
| 77 |
void JNL_AsyncDNS::wait_for_thread_death() |
| 78 |
{ |
| 79 |
if (m_thread) |
| 80 |
{ |
| 81 |
WaitForSingleObject(m_thread,INFINITE); |
| 82 |
CloseHandle(m_thread); |
| 83 |
} |
| 84 |
|
| 85 |
m_thread=0; |
| 86 |
} |