2015-10-27 14:56:57 -07:00
|
|
|
//! @file getfullyqualifiedname.cpp
|
|
|
|
|
//! @author George Fleming <v-geflem@microsoft.com>
|
|
|
|
|
//! @brief Implements GetFullyQualifiedName on Linux
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netdb.h>
|
2015-10-27 16:43:43 -07:00
|
|
|
#include "getcomputername.h"
|
2015-10-27 14:56:57 -07:00
|
|
|
#include "getfullyqualifiedname.h"
|
|
|
|
|
|
2015-10-28 10:24:43 -07:00
|
|
|
//! @brief GetFullyQualifiedName retrieves the fully qualifed dns name of the host
|
2015-10-27 14:56:57 -07:00
|
|
|
//!
|
|
|
|
|
//! @exception errno Passes these errors via errno to GetLastError:
|
2015-10-28 10:24:43 -07:00
|
|
|
//! - ERROR_BAD_ENVIRONMENT: locale is not UTF-8 (from GetComputerName)
|
|
|
|
|
//! - ERROR_INVALID_FUNCTION: getlogin_r() returned an unrecognized error code (from GetComputerName)
|
|
|
|
|
//! - ERROR_INVALID_ADDRESS: buffer is an invalid address (from GetComputerName)
|
|
|
|
|
//! - ERROR_GEN_FAILURE: buffer not large enough (from GetComputerName)
|
2015-10-27 14:56:57 -07:00
|
|
|
//! - ERROR_BAD_NET_NAME: Cannot determine network short name
|
|
|
|
|
//!
|
|
|
|
|
//! @retval username as UTF-8 string, or null if unsuccessful
|
2015-10-28 10:24:43 -07:00
|
|
|
//!
|
2015-10-27 14:56:57 -07:00
|
|
|
|
|
|
|
|
char* GetFullyQualifiedName()
|
|
|
|
|
{
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
|
|
char *computerName = GetComputerName();
|
|
|
|
|
if (NULL == computerName)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-28 16:41:24 -07:00
|
|
|
if (strchr(computerName, '.') != NULL)
|
|
|
|
|
{
|
|
|
|
|
return computerName;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 14:56:57 -07:00
|
|
|
struct addrinfo hints, *info;
|
|
|
|
|
int gai_result;
|
|
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
|
hints.ai_family = AF_UNSPEC; /*either IPV4 or IPV6*/
|
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
|
|
|
|
|
2015-10-28 10:24:43 -07:00
|
|
|
/* There are several ways to get the domain name:
|
|
|
|
|
* uname(2), gethostbyname(3), resolver(3), getdomainname(2),
|
|
|
|
|
* and getaddrinfo(3). Some of these are not portable, some aren't
|
|
|
|
|
* POSIX compliant, and some are being deprecated. Getaddrinfo seems
|
|
|
|
|
* to be the best choice right now.
|
|
|
|
|
*/
|
2015-10-27 14:56:57 -07:00
|
|
|
if ((gai_result = getaddrinfo(computerName, "http", &hints, &info)) != 0)
|
|
|
|
|
{
|
|
|
|
|
errno = ERROR_BAD_NET_NAME;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// info is actually a link-list. We'll just return the first full name
|
|
|
|
|
|
|
|
|
|
char *fullName = strdup(info->ai_canonname);
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(info);
|
|
|
|
|
free(computerName);
|
|
|
|
|
return fullName;
|
|
|
|
|
}
|
|
|
|
|
|