2020-09-11 07:49:16 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2020 FreeIPA Contributors see COPYING for license
|
|
|
|
#
|
|
|
|
"""Get host's FQDN
|
|
|
|
"""
|
|
|
|
import socket
|
|
|
|
|
|
|
|
|
|
|
|
def gethostfqdn():
|
2020-09-17 05:48:37 -05:00
|
|
|
"""Get the fully qualified domain name of current host from glibc
|
2020-09-11 07:49:16 -05:00
|
|
|
|
2020-09-18 06:11:28 -05:00
|
|
|
This function may return an FQDN with up to MAXHOSTFQDNLEN characters
|
|
|
|
(253). The effective hostname is still limited to MAXHOSTNAMELEN (64).
|
|
|
|
|
2020-09-17 05:48:37 -05:00
|
|
|
:return: FQDN as str
|
|
|
|
"""
|
|
|
|
hostname = socket.gethostname()
|
2020-09-11 07:49:16 -05:00
|
|
|
# this call can never fail except for misconfigured nsswitch.conf
|
|
|
|
# without nss-myhostname provider. The myhostname provider translates
|
|
|
|
# gethostname() to local interfaces.
|
|
|
|
gai = socket.getaddrinfo(
|
|
|
|
hostname,
|
|
|
|
None, # service/port is irrelevant
|
|
|
|
family=socket.AF_UNSPEC, # IPv4 or IPv6
|
|
|
|
type=socket.SOCK_DGRAM, # optimization, TCP/RAW gives same result
|
|
|
|
# include canonical name in first addrinfo struct
|
|
|
|
# only use address family when at least one non-local interface
|
|
|
|
# is configured with that address family
|
|
|
|
flags=socket.AI_CANONNAME | socket.AI_ADDRCONFIG
|
|
|
|
)
|
|
|
|
# first addrinfo struct, fourth field is canonical name
|
2020-09-18 06:11:28 -05:00
|
|
|
# getaddrinfo() either raises an exception or returns at least one entry
|
2020-09-11 07:49:16 -05:00
|
|
|
return gai[0][3]
|