Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - gethostbyname_r fails after connected: (3 Items)
   
gethostbyname_r fails after connected  
I have created a program that repeatedly calls gethostbyname_r.  In the case where I connect the target to the internet 
then run the program, as expected it prints the IP address.  Also, if I start the program while disconnected to the 
internet, as expected no IP address is received.  However, if I start the program while disconnected to the internet, 
then while the program is running establish a connection to the internet, gethostbyname_r continues to fail even though 
it's connected to the internet.  At this point, if I terminate the program restart it, gethostbyname_r will resolve the 
name.
Attachment: Text repeatResolve.c 656 bytes
Re: gethostbyname_r fails after connected  
This has been fixed.

Regards,

-seanb

On Wed, Oct 26, 2011 at 10:25:48AM -0400, Andy Naegle wrote:
> I have created a program that repeatedly calls gethostbyname_r.  In the case where I connect the target to the 
internet then run the program, as expected it prints the IP address.  Also, if I start the program while disconnected to
 the internet, as expected no IP address is received.  However, if I start the program while disconnected to the 
internet, then while the program is running establish a connection to the internet, gethostbyname_r continues to fail 
even though it's connected to the internet.  At this point, if I terminate the program restart it, gethostbyname_r will 
resolve the name.
> 
> 
> 
> _______________________________________________
> 
> OSTech
> http://community.qnx.com/sf/go/post89647

> #include <netdb.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <arpa/inet.h>
> 
> int main(int argc, char **argv) {
> 
> 	if (argc < 2) {
> 		fprintf(stderr, "Usage: %s hostname\n", argv[0]);
> 		exit(1);
> 	}
> 
> 	struct hostent *host = malloc(sizeof(struct hostent));
> 	char buffer[1024];
> 	int error;
> 
> 	while(1) {
> 		if (gethostbyname_r(argv[1],host,buffer,sizeof(buffer),&error) != NULL) {
> 			int i=0;
> 			printf("%s IPs: \n", host->h_name);
> 
> 			while (host->h_addr_list[i] != NULL) {
> 				printf("%s \n", inet_ntoa(*(struct in_addr*)(host->h_addr_list[i])));
> 				i++;
> 			}
> 		} else {
> 			printf("gethostbyname_r returned null\n");
> 		}
> 	}
> }
Re: gethostbyname_r fails after connected  
Could you please provide me more info on this fix?  Is there a patch/hotfix available?