h***@gmail.com
2007-01-12 21:15:19 UTC
I have a legacy application which uses gethostbyname to retrieve the
computer's Fully Qualified Domain Name (FQDN). On some computers with
static IP addresses, it suddently fails to return FQDN name. It only
returns a local machine name in hostent->h_name without errors
(WSAGetLastError() returns o).
A testing piece of code as following:
const int HostnameMaxLen = 1024;
int HostnameLength = HostnameMaxLen;
char hostName[HostnameMaxLen];
WSAData wsaData;
memset(&wsaData, 0, sizeof(WSAData));
int error = WSAStartup(MAKEWORD(2,2),&wsaData);
printf("WSAStartup error#:%ld\n", error);
if (gethostname(hostName, HostnameLength)==0)
{
if (WSAGetLastError() != 0)
printf("error#:%ld\n", WSAGetLastError());
printf("host Name:%s\n", hostName);
struct hostent *phe =
gethostbyname(hostName); // network order
if (phe)
{
printf("phe->h_name:%s\n", phe->h_name);
}
}
I should get FQDN from phe->h_name, but I only got a local host name
from phe->h_name on some specific machines. These machines use static
IP address. By the way I can use NSLOOKUP to see the DNS server, IP
address and FQDN on these machines.
According to MSDN, If using a local hosts file, phe->h_name is the
first entry after the IP address. But I changed the local hosts file,
it didn't return the first entry. How can i get the FQDN using
gethostbyname?
Hope an expert could help me out on it.
Thanks in advance!
computer's Fully Qualified Domain Name (FQDN). On some computers with
static IP addresses, it suddently fails to return FQDN name. It only
returns a local machine name in hostent->h_name without errors
(WSAGetLastError() returns o).
A testing piece of code as following:
const int HostnameMaxLen = 1024;
int HostnameLength = HostnameMaxLen;
char hostName[HostnameMaxLen];
WSAData wsaData;
memset(&wsaData, 0, sizeof(WSAData));
int error = WSAStartup(MAKEWORD(2,2),&wsaData);
printf("WSAStartup error#:%ld\n", error);
if (gethostname(hostName, HostnameLength)==0)
{
if (WSAGetLastError() != 0)
printf("error#:%ld\n", WSAGetLastError());
printf("host Name:%s\n", hostName);
struct hostent *phe =
gethostbyname(hostName); // network order
if (phe)
{
printf("phe->h_name:%s\n", phe->h_name);
}
}
I should get FQDN from phe->h_name, but I only got a local host name
from phe->h_name on some specific machines. These machines use static
IP address. By the way I can use NSLOOKUP to see the DNS server, IP
address and FQDN on these machines.
According to MSDN, If using a local hosts file, phe->h_name is the
first entry after the IP address. But I changed the local hosts file,
it didn't return the first entry. How can i get the FQDN using
gethostbyname?
Hope an expert could help me out on it.
Thanks in advance!