Files
PowerShell/native/getfullyqualifiedname.cpp
T

66 lines
2.1 KiB
C++

//! @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>
#include "getcomputername.h"
#include "getfullyqualifiedname.h"
//! @brief GetFullyQualifiedName retrieves the fully qualifed dns name of the host
//!
//! @exception errno Passes these errors via errno to GetLastError:
//! - 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)
//! - ERROR_BAD_NET_NAME: Cannot determine network short name
//!
//! @retval username as UTF-8 string, or null if unsuccessful
//!
//! Note on testing: Since there's no easy way to override the domain name, no unit testing will be
//! provided. Instead, manual end-to-end testing should be done to verify domain name matches expected
//! value.
char* GetFullyQualifiedName()
{
errno = 0;
char *computerName = GetComputerName();
if (NULL == computerName)
{
return NULL;
}
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;
/* 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.
*/
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;
}