@@ -70,10 +70,35 @@ static int add_mailname_host(struct strbuf *buf)
7070 return 0 ;
7171}
7272
73+ static int canonical_name (const char * host , struct strbuf * out )
74+ {
75+ int status = -1 ;
76+
77+ #ifndef NO_IPV6
78+ struct addrinfo hints , * ai ;
79+ memset (& hints , '\0' , sizeof (hints ));
80+ hints .ai_flags = AI_CANONNAME ;
81+ if (!getaddrinfo (host , NULL , & hints , & ai )) {
82+ if (ai && strchr (ai -> ai_canonname , '.' )) {
83+ strbuf_addstr (out , ai -> ai_canonname );
84+ status = 0 ;
85+ }
86+ freeaddrinfo (ai );
87+ }
88+ #else
89+ struct hostent * he = gethostbyname (host );
90+ if (he && strchr (he -> h_name , '.' )) {
91+ strbuf_addstr (out , he -> h_name );
92+ status = 0 ;
93+ }
94+ #endif /* NO_IPV6 */
95+
96+ return status ;
97+ }
98+
7399static void add_domainname (struct strbuf * out )
74100{
75101 char buf [1024 ];
76- struct hostent * he ;
77102
78103 if (gethostname (buf , sizeof (buf ))) {
79104 warning ("cannot get host name: %s" , strerror (errno ));
@@ -82,9 +107,7 @@ static void add_domainname(struct strbuf *out)
82107 }
83108 if (strchr (buf , '.' ))
84109 strbuf_addstr (out , buf );
85- else if ((he = gethostbyname (buf )) && strchr (he -> h_name , '.' ))
86- strbuf_addstr (out , he -> h_name );
87- else
110+ else if (canonical_name (buf , out ) < 0 )
88111 strbuf_addf (out , "%s.(none)" , buf );
89112}
90113
0 commit comments