diff options
| author | Jeff Carr <[email protected]> | 2025-03-22 08:30:15 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-03-22 08:30:15 -0500 |
| commit | 3dd6affd6fde2f0af23b35e7014ba59b6233320a (patch) | |
| tree | bf9eb38cb1072af2119c4c3c9d0acd797ed9a1cb /common.go | |
| parent | 57706020c1a01b16ff37ad4bb33aa4e80e327714 (diff) | |
more hostname thingsv0.0.2
Diffstat (limited to 'common.go')
| -rw-r--r-- | common.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/common.go b/common.go new file mode 100644 index 0000000..ae4b7ff --- /dev/null +++ b/common.go @@ -0,0 +1,47 @@ +package hostname + +import ( + "errors" + "fmt" + "net" + "strings" +) + +var ErrorEmptyDomainName error = errors.New("domain name is empty") +var ErrorDomainNameMisconfigured error = errors.New("your OS domain name is not configured correctly") + +// returns 'hostname', 'domainname' +func Split(s string) (string, string) { + parts := strings.Fields(s) + if len(parts) == 0 { + return "", "" + } + if len(parts) == 1 { + return parts[0], "" + } + return parts[0], strings.Join(parts[1:], ".") +} + +// checks to make sure there is a domainname +func ValidDomainname(s string) error { + _, domainname := Split(s) + if domainname == "" { + return ErrorEmptyDomainName + } + return nil +} + +func ReverseLookupFQDN(hostname string) (string, error) { + addrs, err := net.LookupIP(hostname) + if err != nil || len(addrs) == 0 { + return hostname, fmt.Errorf("failed to lookup IP for hostname %s: %w", hostname, err) + } + + // Try reverse lookup on the first IP address + names, err := net.LookupAddr(addrs[0].String()) + if err != nil || len(names) == 0 { + return hostname, fmt.Errorf("reverse DNS lookup failed for IP %s: %w", addrs[0].String(), err) + } + + return names[0], nil +} |
