When compiling, I get several warnings. I use
gcc -Wall myfile.c -o myfile
First of all, you forgot an # before include on the first line (might have been a copy error). Next
1) malloc() unknow (line 10); include stdlib.h; this will make that the compiler knows the malloc() function
2) malloc returns a pointer; on fail it returns NULL and not -1 (line 12)
3) error() is not defined (line 14); should probably perror
4) exit() unknown (line 15); include stdlin.h
5) inet_aton unknown (line 19); include arpa/inet.h (and maybe sys/socket.h , netinet/in.h)
6) printf() unknown (line 23); include stdio.h
7) i and err are not used
8) main is supposed to return a value
It runs on my system without segfaults. However, there are some issues that are not right.
1) You don't need malloc.
gethostbyname returns a pointer that already points to a hostent structure and you can / have to directly assign it to hostinfo. The way you use it makes that you loose the pointer to the allocated memory and that you will never be able to free that memory (except by terminating the program).
BTW you don't free the allocated memory
2) I always battle with lines like
ptr=(char*)"127.0.0.1"; (it might be correct). Personally I would declare a variable to hold the string
Code:
char myaddr[16];
strcpy(myaddr,"127.0.0.1");
inet_aton(myaddr,cp);
3) Although you pass a pointer (cp) as the second argument of inet_aton, this pointer points to nowhere. Either you have to use malloc or define a variable that can hold the info (as shown below).
Code:
struct in_addr cp; // not *cp
char myaddr[16];
strcpy(myaddr,"127.0.0.1");
inet_aton(myaddr,&cp);
By using the
&cp, you pass the address of cp (which is the pointer to cp). This is more than likely the cause of the segmentation fault.
4) Last but not least, you must check return values. If gethostbyaddr retuns NULL, your last printf statement will give a segmentation fault.
With regards to debugging. Either learn to use the debugger (gdb if you are using linux) or use printf statements. Just add printf before and afterlines that you don't trust. In your situation, this will be after nearly every statement. Something like
Code:
include<netdb.h>
int main()
{
struct hostent * hostinfo;
int i,err;
struct in_addr * cp;
char * ptr;
hostinfo = (struct hostent *)malloc(sizeof(struct hostent));
printf("1\n");
if(hostinfo == -1)
{
error("malloc");
exit(1);
}
ptr = (char *)"127.0.0.1";
printf("2\n");
inet_aton(ptr,cp);
printf("3\n");
hostinfo = gethostbyaddr(cp,4,2);
printf("4\n");
printf("host mane is %s\n",hostinfo->h_name);
}
If you see 2 in your output, but not 3, you know where (one of) the problems are.
Just for the fun a little utility where you can pass the IP address as an argument to the executable and it will give you the hostname.
Code:
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct hostent * hostinfo;
struct in_addr addr;
if(argc!=2)
{
printf("Use: %s IP address\n",argv[0]);
return 1;
}
if(inet_aton(argv[1],&addr)==0)
{
printf("inet_aton returned 0\n");
return 2;
}
hostinfo = gethostbyaddr(&addr,4,2);
if(hostinfo==NULL)
{
printf("gethostbyaddr returned NULL\n");
return 2;
}
printf("host name is %s\n",hostinfo->h_name);
return 0;
}