-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentity_windows.go
42 lines (36 loc) · 1.11 KB
/
identity_windows.go
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
package go_dnsdiscover
import (
"golang.org/x/sys/windows"
"os"
"syscall"
)
func hostname(format int) (string, error) {
n := uint32(64)
for {
b := make([]uint16, n)
err := windows.GetComputerNameEx(uint32(format), &b[0], &n)
if err == nil {
return syscall.UTF16ToString(b[:n]), nil
}
if err != syscall.ERROR_MORE_DATA {
return "", os.NewSyscallError("ComputerNameEx", err)
}
// If we received an ERROR_MORE_DATA, but n doesn't get larger,
// something has gone wrong, and we may be in an infinite loop
if n <= uint32(len(b)) {
return "", os.NewSyscallError("ComputerNameEx", err)
}
}
}
// HostnameFQDN returns the FQDN of the current host (hostname.domain).
func HostnameFQDN() (string, error) {
return hostname(windows.ComputerNamePhysicalDnsFullyQualified)
}
// HostnameShort returns the hostname part of the hostname,
func HostnameShort() (string, error) {
return hostname(windows.ComputerNamePhysicalDnsHostname)
}
// HostnameLinkLocal returns the hostname used for link-local service discovery.
func HostnameLinkLocal() (string, error) {
return hostname(windows.ComputerNameNetBIOS)
}