@@ -70,10 +70,35 @@ static int add_mailname_host(struct strbuf *buf)
70
70
return 0 ;
71
71
}
72
72
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
+
73
99
static void add_domainname (struct strbuf * out )
74
100
{
75
101
char buf [1024 ];
76
- struct hostent * he ;
77
102
78
103
if (gethostname (buf , sizeof (buf ))) {
79
104
warning ("cannot get host name: %s" , strerror (errno ));
@@ -82,9 +107,7 @@ static void add_domainname(struct strbuf *out)
82
107
}
83
108
if (strchr (buf , '.' ))
84
109
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 )
88
111
strbuf_addf (out , "%s.(none)" , buf );
89
112
}
90
113
0 commit comments