curl-library
Re: Windows people -- help needed!
Date: Tue, 28 Jan 2003 13:51:57 -0500
Here's my test of #2 with some modificatiions to get it to compile. I
added a few comments along the way. Seems to work ok for me. You would
still need to address the TODO in reagrds to the returned hostent, probably
what curl already has to clone these structs.
#include <windows.h>
#include <stdio.h>
// a structure passed to gethostbyname_tp containing
// name resolution info
typedef struct dns_info
{
char *name;
struct hostent *dnsinfo;
HANDLE hEventDns;
} dns_info;
// The thread proc that will run the gethostname resolutions
DWORD WINAPI gethostbyname_tp(LPVOID* p)
{
dns_info* info = (dns_info*)p;
info->dnsinfo = gethostbyname(info->name);
SetEvent(info->hEventDns);
return 0;
}
// Get hostbyname with a timeout
// returns < 0 if timeout occurred (or some other initialization issue)
// or zero if gethostbyname() completed (note: does not imply that name
// was resolved, only that gethostbyname() completed). Returns an
allocated
// hostent struct in pph (if applicable)
int gethostbyname_to(char *name, int timeout_millis, struct hostent** pph)
{
int rc = 0;
struct hostent* h = NULL;
DWORD hThreadDns = 0;
HANDLE hThread = INVALID_HANDLE_VALUE;
int eventresult = 0;
dns_info myInfo;
myInfo.name = strdup(name);
myInfo.dnsinfo = NULL;
// Create the event for the threadproc to signal upon completion
myInfo.hEventDns = CreateEvent(NULL, FALSE, FALSE, "DNS Event");
// start the name resolution thread
// NOTE: if the threadproc ever includes and C runtime calls this
// should be changes to using _beginthreadex()
hThread = CreateThread(NULL, 0, gethostbyname_tp, &myInfo, 0,
&hThreadDns);
if(hThread != INVALID_HANDLE_VALUE)
{
// wait for name resolution to either complete or timeout
eventresult = WaitForSingleObject(myInfo.hEventDns,
timeout_millis);
if ((eventresult==WAIT_TIMEOUT) || (eventresult==WAIT_FAILED))
{
// if timedout, kill the pending opertation
TerminateThread(hThread, -1);
rc = -1;
myInfo.dnsinfo = NULL;
}
CloseHandle(hThread);
}
CloseHandle(myInfo.hEventDns);
// TODO: need to copy the hostent* returned if not NULL
// don't just return the gethostbyname allocated one
*pph = myInfo.dnsinfo;
return rc;
}
int main(int argc, char *argv[])
{
int i = 0;
WSADATA data;
struct hostent* h = NULL;
char* names[] = {
"foo",
"bar",
"www.uml.edu",
"www.unh.edu",
};
int timeouts[] = {
100,
2000,
100,
2000,
};
WSAStartup(0x0101, &data);
for(i=0; i<4; i++)
{
if(gethostbyname_to(names[i], timeouts[i], &h) < 0)
{
printf("gethostbyname timed out.\n");
}
else
{
printf("%s (%d)\n", h?"name resolved":"name not
resolved", WSAGetLastError());
}
}
WSACleanup();
return 0;
}
Daniel Stenberg
<daniel_at_haxx.se> To: libcurl Mailing list <curl-library_at_lists.sourceforge.net>
Sent by: cc:
curl-library-admin_at_lists.sour Subject: Windows people -- help needed!
ceforge.net
01/28/2003 11:22 AM
Please respond to
curl-library
1)
Bug report #607604 was fixed a long time ago for non-windows platforms, and
I
just want to ask if there's anyone out there who could possibly consider
helping us closing the report by fixing this in the windows version as
well?
http://sourceforge.net/tracker/index.php?func=detail&aid=607604&group_id=976&atid=100976
2)
There is also this interesting patch in bug report #674095 submitted by
aschiffler, that offers a gethostbyname() that can be aborted with a
timeout
(for Windows). I would like someone to test it/comment it to become subject
for inclusion in an upcoming libcurl version:
http://sourceforge.net/tracker/index.php?func=detail&aid=674095&group_id=976&atid=100976
-- Daniel Stenberg -- curl, cURL, Curl, CURL. Groks URLs. ------------------------------------------------------- This SF.NET email is sponsored by: SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See! http://www.vasoftware.com ------------------------------------------------------- This SF.NET email is sponsored by: SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See! http://www.vasoftware.comReceived on 2003-01-28