caching gethostbyname()

Atomical
Unregistered
 
Post: #1
It runs through strcmp() a few times and then gives me a segfault on the strcmp() line.

Quote:hostent * lookup_host ( char *hname , hostname **hostdatabase) {
int i;


for ( i =0; hostdatabase[i]; i++ ){
if ( strcmp(hostdatabase[i]->name,hname) == 0 )
return hostdatabase[i]->info;
}


hostdatabase = (hostname **)realloc(hostdatabase,(i*sizeof(hostname))+sizeof(hostname));

hostdatabase[i]->name = (char *)malloc(strlen(hname)*sizeof(char)+1);

strcpy(hostdatabase[i]->name, hname);

hostent* hostptr = gethostbyname(hname);

if ( host_error ( ) == NULL ) {

hostdatabase[i]->info = hostptr;

} else {

hostdatabase[i]->info = NULL;

free( hostptr );

return NULL;

}

return hostptr;


}
Quote this message in a reply
Post Reply