hi,
I was trying to get the corresponding hostname/canonical name for an
IP address.
I tried using both getaddrinfo and getnameinfo, but i am not getting
proper results.
For example
getaddrinfo:
Usage:
hints.ai_socktype = 0;
hints.ai_protocol = 0;
hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_INET;
hints.ai_addrlen = 0;
hints.ai_addr = 0;
hints.ai_canonname = 0;
if (error = getaddrinfo("203.200.202.207", NULL, &hints, &result))
{
cout <<"ERROR USING getaddrinfo \n";
}
Output:
IP address 0 = 203.200.202.207 Canonical name 0 = 203.200.202.207
You will see that canonical name is being shown same as that of IP
address.
Example: I tried getnameinfo:
struct sockaddr_in a;
int error=0;
char hostname[NI_MAXHOST];
memset((void*)&a, 0 , sizeof(a));
a.sin_family = AF_INET;
if(inet_aton("203.27.235.25",
&(a.sin_addr)) == 0)
{
cout <<"inet_aton returned zero \n";
return;
}
if((error = getnameinfo((struct sockaddr*)&a, sizeof(struct
sockaddr),
hostname, sizeof(hostname), NULL,0,0)))
{
cout <<"getnameinfo returned error \n";
}
cout <<"Hostname obtained is >> \n";
cout <<hostname;
Observed Output is:
Hostname obtained is >> 203.27.235.25
Expected Output was:
Hostname obtained is >> icicibank.com
Can anyone tell me if my code is proper?
If anyone has code for converting ip address to hostname, can you
please forward me the code?
|