472,958 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

gethostbyname and timeouts

Hello,

I'm writing a little program which has to get a FQDN on his stdin and
return the IP on stdout, we can do it easily with gethostbyname call
but I would like to set up a timeout to this blocking system call. So
I read texts about this and I planed to use a hack with alarm and
setjmp/longjmp. My program works until there is a timeout, when it
occurs, all the gethostbyname calls which follow fail, backtrace in
gdb give me this :
#0 0xb7eac469 in pthread_setcanceltype () from /lib/tls/libc.so.6
#1 0x080486d7 in dnslookup (request=0xbfe1dc9c "free.fr") at dnsserver.c:53
#2 0x080488aa in main (argc=1, argv=0xbfe1df24) at dnsserver.c:102

I give you the code :

/* $Id: dnsserver.c,v 1.5 2006-01-31 20:10:30 quentin Exp $ */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <setjmp.h>
#include <signal.h>

#define REQUEST_SIZE 512
#define DNS_TIMEOUT 2 /* Set here the timeout for the gethostbyname call */

static jmp_buf env;

static void sighandler (int signum)
{
longjmp (env, 1);
}

static void dnslookup (char *request)
{
struct hostent *host;

signal (SIGALRM, sighandler);
if (setjmp (env) == 0) {
alarm (DNS_TIMEOUT);
host = gethostbyname (request);
signal (SIGALRM, SIG_DFL);
alarm (0);

if (host == NULL) {
switch (h_errno) {
case HOST_NOT_FOUND:
case NO_ADDRESS:
case NO_RECOVERY:
puts ("$error");
break;
case TRY_AGAIN:
puts ("$tryagain");
break;
}
return;
}

puts (inet_ntoa (*((struct in_addr *) (host->h_addr)))); /* Display the address */
}
else { /* We come from the longjmp */
puts ("$timeout");
return;
}

}

int main (int argc, char **argv)
{
char req [REQUEST_SIZE];
char *p;

/* the stdout is not a buffered stream */
setvbuf (stdout, NULL, _IONBF, 0);

for (;;) { /* Main loop */
if (fgets (req, REQUEST_SIZE, stdin) == NULL) {
exit (EXIT_SUCCESS);
}
if (strncmp (req, "$goodbye", 8) == 0) {
exit (EXIT_SUCCESS);
} else if (strncmp (req, "$hello", 6) == 0) {
puts ("$olleh");
continue;
}
p = strrchr (req, '\n');
*p = '\0';
dnslookup (req);
}
/* NOTREACHED */
}
Feb 9 '06 #1
1 7046
Quentin Carbonneaux wrote:
Hello,

I'm writing a little program which has to get a FQDN on his stdin and
return the IP on stdout, we can do it easily with gethostbyname call
but I would like to set up a timeout to this blocking system call. So


Sorry, but you're asking in the wrong place (gethostbyname() is not
standard C -- it's POSIX).

Try news:comp.unix.programmer.
[snip]

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Feb 9 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: yawnmoth | last post by:
using the gethostbyname function seems to noticeably slow down pages. some of the comments in php.net's gethostbyname entry suggest using a version that caches the result, but those versions also...
0
by: Eric Brunel | last post by:
Hi all, I just compiled Python 2.3.2 on Linux Mandrake 8.0, upgrading from Python 2.1. I have one problem that I can't figure out: when I wanted to get "the" IP address of the current host with...
1
by: Fortepianissimo | last post by:
I've tried using socket.setdefaulttimeout(timeout) to set the default timeout to 'timeout' for all sockets. The part that's not clear to me, is that if this will affect...
5
by: ruroma | last post by:
Hello, I have one problem, and can think of two possible solutions but I can't manage to make them work. I'm open to other suggestions if you thik is better. The main function calls...
2
by: mircu | last post by:
Hi, I need a quick solution to make my application behave correctly when one of these timeouts occurs. I have some logic in session_start but when the authentication cookie timeouts the user is...
1
by: Glen Conway | last post by:
Hi, I'm trying to use the gethostbyname function from wsock32.dll and failing dismally Has anyone got a successful implementation of this in VB.NET? My ulitimate goal is to resolve NetBIOS names...
1
by: yawnmoth | last post by:
I seem to be getting conflicting gethostbyname behavior on different servers. Before going into detail, here's the script I'm using: <? $address = $HTTP_SERVER_VARS; $rev =...
2
by: Andrew DeFaria | last post by:
I've been having problems with my ISP. One way it seems to manifest itself is that I can not reach or contact my ISP's DNS servers. IOW a simply nslookup google.com will fail. So I tried writing...
18
by: aj | last post by:
I have the following snippet of code. On some platforms, the delete calls works, on Linux, it core dumps (memory dump) at the delete call. Am I responsible for deleting the memory that...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.