473,787 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_setcanc eltype () from /lib/tls/libc.so.6
#1 0x080486d7 in dnslookup (request=0xbfe1 dc9c "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 7225
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
6637
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 only speedup subsequent calls to gethostbyname - the first call is still as slow as ever. so is there anything i can do to speed this up? perhapes i can just implement a function equivalent to gethostbyname using fsockopen, or something?
0
6032
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 Python 2.1, I did: Python 2.1.1 (#12, Apr 10 2002, 17:52:08) on linux2 Type "copyright", "credits" or "license" for more information. >>> import socket
1
7148
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 socket.gethostbyname(). In my observation the timeout setting did not affect the DNS lookup. It might be my misunderstanding of the semantics here: would
5
2743
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 sendSocket, but I don't know how to tell it where to send the socket. ( I think I'm messing it up with pointers)
2
1660
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 redirected to login page and after successful login the session is not started. I'd like to have one timeout and when it occurs the user must login and then the new session is started. TIA. Regards, mircu
1
3815
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 to IP Addresses. I can use the framework DNS features if a DNS server is present on the network but this cannot use WINS if no DNS server is available. Any help or pointers are apprecatiated. Here is the code I've managed to do so far. I've...
1
1728
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 = implode('.',array_reverse(explode('.', $address))); $lookup = "$rev.l1.spews.dnsbl.sorbs.net"; echo gethostbyname($lookup); echo '<br />';
2
5579
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 a script that would monitor this. The script calls gethostbyname for google.com every 15 minutes and logs the status. When gethostbyname fails however it never comes back. My ISP and internet connection may come back and nslookup at the command...
18
3663
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 gethostbyname allocated? struct hostent *lHostInfo; lHostInfo = gethostbyname(ipHost.c_str()); memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list, lHostInfo- delete lHostInfo;
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.