root/maint/gnulib/lib/canon-host.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. canon_host
  2. canon_host_r
  3. ch_strerror

   1 /* Host name canonicalization
   2 
   3    Copyright (C) 2005-2021 Free Software Foundation, Inc.
   4 
   5    Written by Derek Price <derek@ximbiot.com>.
   6 
   7    This file is free software: you can redistribute it and/or modify
   8    it under the terms of the GNU Lesser General Public License as
   9    published by the Free Software Foundation; either version 3 of the
  10    License, or (at your option) any later version.
  11 
  12    This file is distributed in the hope that it will be useful,
  13    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15    GNU Lesser General Public License for more details.
  16 
  17    You should have received a copy of the GNU Lesser General Public License
  18    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  19 
  20 #include <config.h>
  21 
  22 #include "canon-host.h"
  23 
  24 #include <string.h>
  25 #include <netdb.h>
  26 
  27 /* Store the last error for the single-threaded version of this function.  */
  28 static int last_cherror;
  29 
  30 /* Single-threaded of wrapper for canon_host_r.  After a NULL return, error
  31    messages may be retrieved via ch_strerror().  */
  32 char *
  33 canon_host (const char *host)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35   return canon_host_r (host, &last_cherror);
  36 }
  37 
  38 /* Return a malloc'd string containing the canonical hostname associated with
  39    HOST, or NULL if a canonical name cannot be determined.  On NULL return,
  40    if CHERROR is not NULL, set *CHERROR to an error code as returned by
  41    getaddrinfo().  Use ch_strerror_r() or gai_strerror() to convert a *CHERROR
  42    value to a string suitable for error messages.
  43 
  44    WARNINGS
  45      HOST must be a string representation of a resolvable name for this host.
  46      Strings containing an IP address in dotted decimal notation will be
  47      returned as-is, without further resolution.
  48 
  49      The use of the word "canonical" in this context is unfortunate but
  50      entrenched.  The value returned by this function will be the end result
  51      of the resolution of any CNAME chains in the DNS.  There may only be one
  52      such value for any given hostname, though the actual IP address
  53      referenced by this value and the device using that IP address may each
  54      actually have any number of such "canonical" hostnames.  See the POSIX
  55      getaddrinfo spec
  56      <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html>,
  57      RFC 1034 <https://www.ietf.org/rfc/rfc1034.txt>, & RFC 2181
  58      <https://www.ietf.org/rfc/rfc2181.txt> for more on what this confusing
  59      term really refers to. */
  60 char *
  61 canon_host_r (char const *host, int *cherror)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63   char *retval = NULL;
  64   static struct addrinfo hints;
  65   struct addrinfo *res = NULL;
  66   int status;
  67 
  68   hints.ai_flags = AI_CANONNAME;
  69   status = getaddrinfo (host, NULL, &hints, &res);
  70   if (!status)
  71     {
  72       /* https://lists.gnu.org/r/bug-coreutils/2006-09/msg00300.html
  73          says Darwin 7.9.0 getaddrinfo returns 0 but sets
  74          res->ai_canonname to NULL.  */
  75       retval = strdup (res->ai_canonname ? res->ai_canonname : host);
  76       if (!retval && cherror)
  77         *cherror = EAI_MEMORY;
  78       freeaddrinfo (res);
  79     }
  80   else if (cherror)
  81     *cherror = status;
  82 
  83   return retval;
  84 }
  85 
  86 /* Return a string describing the last error encountered by canon_host.  */
  87 const char *
  88 ch_strerror (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90   return gai_strerror (last_cherror);
  91 }

/* [previous][next][first][last][top][bottom][index][help] */