In Windows 2000, call gethostbyaddr on additional ips returns the name of the fi
If someone can show me where I am wrong?
Regards,
Sam
Source Code:
// ws2000.cpp : to demonstrate the bug in Windows 2000
//
// call gethostbyaddr on additional ips returns the name of the first ip.
//
// A machine has more than one ip, each ip has different name in dns server,
// If OS is NT4, this program return:
/*
Hostname: beowolf
ip: 10.178.20.51 name: beowolf.eshare.com
ip: 10.178.20.52 name: beowolf1.eshare.com
ip: 10.178.20.53 name: beowolf2.eshare.com
ip: 10.178.20.54 name: beowolf3.eshare.com
ip: 10.178.20.55 name: beowolf4.eshare.com
Press ang key to continue.....
*/
// It is correct. However,
// If OS is Windows 2000 Server, this program return:
/*
Hostname: beowolf
ip: 10.178.20.51 name: beowolf.eshare.com
ip: 10.178.20.52 name: beowolf.eshare.com
ip: 10.178.20.53 name: beowolf.eshare.com
ip: 10.178.20.54 name: beowolf.eshare.com
ip: 10.178.20.55 name: beowolf.eshare.com
Press ang key to continue.....
*/
// It isn't expected.
#include
#include
#include
int main(int argc, char* argv[])
{
WSADATA wsData;
int status = WSAStartup(MAKEWORD(2,2),&wsData);
if (status != NO_ERROR)
{
cerr << "Fail to start socket!" << endl;
return -1;
}
//DoAllIPs();
char szHostName[256];
if( gethostname(szHostName,256) != 0 )
{
cerr << "Fail to get hostname!" << endl;
return -1;
}
cout << "Hostname: " << szHostName << endl;
hostent *ph = gethostbyname(szHostName);
if (ph == NULL)
{
cerr << "Fail to gethostbyname!" <h_addr_list[i]!= NULL&&ih_addr_list[i])[0],
(unsigned int)((unsigned char*)ph->h_addr_list[i])[1],
(unsigned int)((unsigned char*)ph->h_addr_list[i])[2],
(unsigned int)((unsigned char*)ph->h_addr_list[i])[3]);
}
nips = i;
for (i = 0; i < nips; ++i)
{
cout << "ip: " << ips[i];
unsigned long iaddr = inet_addr(ips[i]);
if (iaddr==INADDR_NONE)
{
cerr << "Error: " << ips[i] << " isn't ip address!" << endl;
return -1;
}
sockaddr_in addr;
memset(&addr, 0, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
memcpy(&addr.sin_addr, &iaddr, sizeof(iaddr));
ph = gethostbyaddr((char *)&addr.sin_addr,4,AF_INET);
if (ph)
{
cout << " name: " <h_name << endl;
}
else
{
cout << " name: host.unknown" << endl;
}
}
WSACleanup();
cout << "Press ang key to continue....." << endl;
getchar();
return 0;
}

