Discussion:
Uses gethostbyname to get FQDN
(too old to reply)
h***@gmail.com
2007-01-12 21:15:19 UTC
Permalink
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!
Arkady Frenkel
2007-01-13 06:07:35 UTC
Permalink
Look at networks NG for my answer and use cross-posting and not multiposting
next time
Arkady
Post by h***@gmail.com
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).
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!
Continue reading on narkive:
Search results for 'Uses gethostbyname to get FQDN' (Questions and Answers)
4
replies
unix machine name?
started 2007-09-29 17:19:01 UTC
programming & design
Loading...