Even my lecturer can't figure this 1 out.... help! Any resolver gurus
know what's happening?
I've got 2 samples. 1st sample was written by my lecturer to display the
contents of hosent structure. The 2nd sample is my game with his code
(juz that 1 line with gethostbyname()) ported over. It works perfectly
fine with the 1st sample - any text string gets accepted by
gethostbyname().
gethostbyname() causes segmentation fault in the 2nd sample though.
I've tried other alternative ways like assigning to a new char ptr,
copying to a new buffer space, & even hardcoding the string into the
function call. all will still gimme segmentation fault. The error
pattern I noticed is strings above 11 or 12 chars will produce the fault
:
"brady.sgt.g" will be accepted & actually queries the DNS server (I can
see it thru the modem). This is generate the "Server host invalid" msg
of course.
"brady.sgt.gm" will cause segmentation fault immediately, without
sending any DNS query packet.
if I try "131.181.112.1" gethostbyname() accepts it, but returns invalid
host. I thought gethostbyname() is able to return a hostent struct with
the same addr? giving "131.181.112.2" will produce segmentation fault
again, & "131.181.112.3" will get invalid host, & so on.
Anybody knows how gethostbyname manipulates the string?
#1
int main(int argc, char *argv[])
{
char *ptr, **pptr, str[INET_ADDRSTRLEN];
struct hostent *hptr;
while (--argc > 0)
{
ptr = *++argv;
printf("%s", ptr);
fflush(stdout);
if ((hptr = gethostbyname(ptr)) == NULL)
{
fprintf(stderr, " not valid hostname?\n", ptr);
continue;
}
printf(" official hostname : %s\n", hptr->h_name);
...........
...........
#2
void start_player(char name[NAME + 1], char *servername, uint16_t port,
int pos[2])
{
int sockd;
struct sockaddr_in serversock;
struct hostent *serverhost;
/* test param string */
printf("HMS %s to enter %s:%d at %d long %d lat\n", name, servername,
port, pos[0], pos[1]);
if ((serverhost = gethostbyname(servername)) == NULL)
{
fprintf(stderr, "Server host invalid.\n");
exit(PARAM_FAULT);
}
/* test string */
printf("Set server socket now...\n");
bzero(&serversock, sizeof(serversock));
inet_pton(AF_INET, serverhost->h_addr_list[0], &serversock.sin_addr);
serversock.sin_family = AF_INET;
serversock.sin_port = htons(port);
..........
..........
trapped,
Aaron