Skip to content

Commit

Permalink
send
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Jan 4, 2023
1 parent 267efeb commit e423d6c
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions providers/windows/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,17 @@ func (r *reader) hostname(h *host) {
h.info.Hostname = v
}
r.addErr(err)

v, err = fqdn()
if err == nil {
h.info.FQDN = v
}
r.addErr(err)
}

func fqdn() (name string, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("could not get windows FQDN: %w", err)
}
}()

func (r *reader) fqdn(h *host) {
size := uint32(64)
for {
buff := make([]uint16, size)
err := stdwindows.GetComputerNameEx(
stdwindows.ComputerNamePhysicalDnsFullyQualified, &buff[0], &size)
if err == nil {
return syscall.UTF16ToString(buff[:size]), nil
h.info.FQDN = syscall.UTF16ToString(buff[:size])
return
}

// ERROR_MORE_DATA means buff is too small and size is set to the
Expand All @@ -168,18 +157,19 @@ func fqdn() (name string, err error) {
if errors.Is(err, syscall.ERROR_MORE_DATA) {
if size <= uint32(len(buff)) {
// Safeguard to avoid an infinite loop.
return "", fmt.Errorf(
r.addErr(fmt.Errorf(
"windows.GetComputerNameEx returned ERROR_MORE_DATA, " +
"but data size should fit into buffer")
"but data size should fit into buffer"))
return
} else {
// grow the buffer and try again
// should we limit its grow?
// Grow the buffer and try again.
// Should we limit its growth?
buff = make([]uint16, size)
continue
}
}

return "", fmt.Errorf("could not get windows.ComputerNamePhysicalDnsFullyQualified: %w", err)
r.addErr(fmt.Errorf("could not get windows FQDN: could not get windows.ComputerNamePhysicalDnsFullyQualified: %w", err))
}
}

Expand Down

0 comments on commit e423d6c

Please sign in to comment.