I haven't heard any objections to my initial AI_FQDN diff, so here's
the diff again with updated man page bits.  See the original thread
for a proof-of-concept diff from djm@ to use this in OpenSSH.

The man page mostly only uses "hostname" when referring to the formal
argument, so I changed the AI_CANONNAME descriptive text to use "host
name" to be consistent with the rest of the file.  The AI_FQDN text is
based on the AI_CANONNAME text + the MSDN description of Windows's
flag.

ok?

Index: lib/libc/net/getaddrinfo.3
===================================================================
RCS file: /cvs/src/lib/libc/net/getaddrinfo.3,v
retrieving revision 1.47
diff -u -p lib/libc/net/getaddrinfo.3
--- lib/libc/net/getaddrinfo.3  9 Jul 2009 10:14:41 -0000       1.47
+++ lib/libc/net/getaddrinfo.3  4 Apr 2011 17:53:40 -0000
@@ -126,11 +126,33 @@ If the
 bit is set, a successful call to
 .Fn getaddrinfo
 will return a NUL-terminated string containing the canonical name
-of the specified hostname in the
+of the specified host name in the
 .Fa ai_canonname
 element of the first
 .Li addrinfo
 structure returned.
+.It Dv AI_FQDN
+If the
+.Dv AI_FQDN
+bit is set, a successful call to
+.Fn getaddrinfo
+will return a NUL-terminated string containing the fully qualified domain name
+of the specified host name in the
+.Fa ai_canonname
+element of the first
+.Li addrinfo
+structure returned.
+.Pp
+This is different than the
+.Dv AI_CANONNAME
+bit flag that returns the canonical name registered in DNS,
+which may be different than the fully qualified domain name
+that the host name resolved to.
+Only one of the
+.Dv AI_FQDN
+and
+.DV AI_CANONNAME
+bits can be set.
 .It Dv AI_NUMERICHOST
 If the
 .Dv AI_NUMERICHOST
@@ -438,6 +460,10 @@ function is defined by the
 draft specification and documented in
 .Dv "RFC 3493" ,
 .Dq Basic Socket Interface Extensions for IPv6 .
+.Pp
+The
+.Dv AI_FQDN
+flag bit first appeared in Windows 7.
 .Sh BUGS
 The implementation of
 .Fn getaddrinfo
Index: include/netdb.h
===================================================================
RCS file: /cvs/src/include/netdb.h,v
retrieving revision 1.27
diff -u -p include/netdb.h
--- include/netdb.h     2 Jun 2009 16:47:50 -0000       1.27
+++ include/netdb.h     4 Apr 2011 17:53:40 -0000
@@ -155,9 +155,10 @@ struct     protoent {
 #define AI_NUMERICHOST 4       /* don't ever try hostname lookup */
 #define AI_EXT         8       /* enable non-portable extensions */
 #define AI_NUMERICSERV 16      /* don't ever try servname lookup */
+#define AI_FQDN                32      /* return the FQDN that was resolved */
 /* valid flags for addrinfo */
 #define AI_MASK \
-    (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV)
+    (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV | AI_FQDN)
 
 #define NI_NUMERICHOST 1       /* return the host address, not the name */
 #define NI_NUMERICSERV 2       /* return the service address, not the name */
Index: lib/libc/net/getaddrinfo.c
===================================================================
RCS file: /cvs/src/lib/libc/net/getaddrinfo.c,v
retrieving revision 1.71
diff -u -p lib/libc/net/getaddrinfo.c
--- lib/libc/net/getaddrinfo.c  18 Nov 2009 07:43:22 -0000      1.71
+++ lib/libc/net/getaddrinfo.c  4 Apr 2011 17:53:40 -0000
@@ -309,7 +309,9 @@ getaddrinfo(const char *hostname, const char *servname
                if (hints->ai_addrlen || hints->ai_canonname ||
                    hints->ai_addr || hints->ai_next)
                        ERR(EAI_BADHINTS); /* xxx */
-               if (hints->ai_flags & ~AI_MASK)
+               if ((hints->ai_flags & ~AI_MASK) != 0 ||
+                   (hints->ai_flags & (AI_CANONNAME | AI_FQDN)) ==
+                   (AI_CANONNAME | AI_FQDN))
                        ERR(EAI_BADFLAGS);
                switch (hints->ai_family) {
                case PF_UNSPEC:
@@ -671,14 +673,13 @@ explore_numeric(const struct addrinfo *pai, const char
                    pai->ai_family == PF_UNSPEC /*?*/) {
                        GET_AI(cur->ai_next, afd, pton);
                        GET_PORT(cur->ai_next, servname);
-                       if ((pai->ai_flags & AI_CANONNAME)) {
-                               /*
-                                * Set the numeric address itself as
-                                * the canonical name, based on a
-                                * clarification in rfc2553bis-03.
-                                */
-                               GET_CANONNAME(cur->ai_next, canonname);
-                       }
+                       /*
+                        * Set the numeric address itself as
+                        * the canonical name, based on a
+                        * clarification in rfc2553bis-03.
+                        */
+                       GET_CANONNAME(cur->ai_next, canonname);
+
                        while (cur && cur->ai_next)
                                cur = cur->ai_next;
                } else
@@ -764,7 +765,7 @@ explore_numeric_scope(const struct addrinfo *pai, cons
 static int
 get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
 {
-       if ((pai->ai_flags & AI_CANONNAME) != 0) {
+       if ((pai->ai_flags & (AI_CANONNAME | AI_FQDN)) != 0) {
                ai->ai_canonname = strdup(str);
                if (ai->ai_canonname == NULL)
                        return EAI_MEMORY;
@@ -1129,7 +1130,7 @@ getanswer(const querybuf *answer, int anslen, const ch
                        haveanswer++;
        }
        if (haveanswer) {
-               if (!canonname)
+               if (!canonname || (pai->ai_flags & AI_FQDN) != 0)
                        (void)get_canonname(pai, sentinel.ai_next, qname);
                else
                        (void)get_canonname(pai, sentinel.ai_next, canonname);
@@ -1275,11 +1276,9 @@ found:
                /* cover it up */
                res->ai_flags = pai->ai_flags;
 
-               if (pai->ai_flags & AI_CANONNAME) {
-                       if (get_canonname(pai, res, cname) != 0) {
-                               freeaddrinfo(res0);
-                               goto again;
-                       }
+               if (get_canonname(pai, res, cname) != 0) {
+                       freeaddrinfo(res0);
+                       goto again;
                }
        }
        return res0;
@@ -1369,8 +1368,7 @@ nextline:
                        /* cover it up */
                        res->ai_flags = pai->ai_flags;
 
-                       if (pai->ai_flags & AI_CANONNAME)
-                               (void)get_canonname(pai, res, canonname);
+                       (void)get_canonname(pai, res, canonname);
                }
        } else
                res0 = NULL;

Reply via email to