forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetfullyqualifiedname.cpp
More file actions
54 lines (44 loc) · 1.47 KB
/
Copy pathgetfullyqualifiedname.cpp
File metadata and controls
54 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//! @brief Implements GetFullyQualifiedName on Linux
#include "getcomputername.h"
#include "getfullyqualifiedname.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
//! @brief GetFullyQualifiedName retrieves the fully qualified dns name of the host
//!
//! @retval username as UTF-8 string, or null if unsuccessful
char *GetFullyQualifiedName()
{
errno = 0;
char *computerName = GetComputerName();
if (computerName == NULL)
{
return NULL;
}
struct addrinfo hints, *info;
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.
*/
char *fullName = NULL;
if (getaddrinfo(computerName, "http", &hints, &info) != 0)
{
goto exit;
}
// return the first canonical name in the list
fullName = strndup(info->ai_canonname, strnlen(info->ai_canonname, NI_MAXHOST));
// only free info if getaddrinfo was successful
freeaddrinfo(info);
exit:
free(computerName);
return fullName;
}