gethostname() works well enough.  There's no need to use getaddrinfo() to look up the canonical name as well.