Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

timeout on gethostbyaddr() function



I got the following "dns lookup with timeout" code from:

http://orange.kame.net/dev/cvsweb.cgi/wwwoffle/ sockets4.c?rev=1.1&cvsroot=apps-obsoleted

If my connection to the internet is ok, then it works great, but if I disrupt my connection to check that the timeout is working, my app dies with signal 10.

Anyone see the problem or know another way to specify a timeout on gethostbyaddr()?

Thanks.

- Philip


static void sigalarm(int signum) {
longjmp(dns_jmp_env,1);
}

- (NSString *)dnsLookupForIP:(NSString *)ip {
struct sigaction action;

start:

if(!timeout_dns) {
host = gethostbyaddr((const void*)&MYaddr, sizeof(struct in_addr), AF_INET);
if (host) {
return [NSString stringWithCString:host->h_name];
} else {
return @"n/a";
}
}

action.sa_handler = sigalarm;
sigemptyset (&action.sa_mask);
action.sa_flags = 0;
if (sigaction(SIGALRM, &action, NULL) != 0) {
printf("Failed to set SIGALRM; cancelling timeout for DNS.\n");
timeout_dns=0;
goto start;
}

alarm(timeout_dns);

if (setjmp(dns_jmp_env)) {
host=NULL;
errno=ETIMEDOUT;
} else {
inet_aton([ip cString], &MYaddr);
host = gethostbyaddr((const void*)&MYaddr, sizeof(struct in_addr), AF_INET);
}

alarm(0);
action.sa_handler = SIG_IGN;
sigemptyset (&action.sa_mask);
action.sa_flags = 0;
if (sigaction(SIGALRM, &action, NULL) != 0)
printf("Failed to clear SIGALRM.\n");

if (host) {
return [NSString stringWithCString:host->h_name];
} else {
return @"n/a";
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.