"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/inet_ntop.c" (7 Sep 2020, 4866 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "inet_ntop.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /*! \file */
   13 
   14 #include <config.h>
   15 
   16 #include <errno.h>
   17 #include <stdio.h>
   18 #include <inttypes.h>
   19 #include <stdlib.h>
   20 #include <string.h>
   21 
   22 #include <isc/net.h>
   23 #include <isc/print.h>
   24 #include <isc/string.h>
   25 #include <isc/util.h>
   26 
   27 #define NS_INT16SZ   2
   28 #define NS_IN6ADDRSZ    16
   29 
   30 /*
   31  * WARNING: Don't even consider trying to compile this on a system where
   32  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
   33  */
   34 
   35 static const char *inet_ntop4(const unsigned char *src, char *dst,
   36                   size_t size);
   37 
   38 #ifdef AF_INET6
   39 static const char *inet_ntop6(const unsigned char *src, char *dst,
   40                   size_t size);
   41 #endif
   42 
   43 /*! char *
   44  * isc_net_ntop(af, src, dst, size)
   45  *  convert a network format address to presentation format.
   46  * \return
   47  *  pointer to presentation format address (`dst'), or NULL (see errno).
   48  * \author
   49  *  Paul Vixie, 1996.
   50  */
   51 const char *
   52 isc_net_ntop(int af, const void *src, char *dst, size_t size)
   53 {
   54     switch (af) {
   55     case AF_INET:
   56         return (inet_ntop4(src, dst, size));
   57 #ifdef AF_INET6
   58     case AF_INET6:
   59         return (inet_ntop6(src, dst, size));
   60 #endif
   61     default:
   62         errno = EAFNOSUPPORT;
   63         return (NULL);
   64     }
   65     /* NOTREACHED */
   66 }
   67 
   68 /*! const char *
   69  * inet_ntop4(src, dst, size)
   70  *  format an IPv4 address
   71  * \return
   72  *  `dst' (as a const)
   73  * \note
   74  *  (1) uses no statics
   75  * \note
   76  *  (2) takes a unsigned char* not an in_addr as input
   77  * \author
   78  *  Paul Vixie, 1996.
   79  */
   80 static const char *
   81 inet_ntop4(const unsigned char *src, char *dst, size_t size)
   82 {
   83     static const char *fmt = "%u.%u.%u.%u";
   84     char tmp[sizeof("255.255.255.255")];
   85     int n;
   86 
   87 
   88     n = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
   89     if (n < 0 || (size_t)n >= size) {
   90         errno = ENOSPC;
   91         return (NULL);
   92     }
   93     strlcpy(dst, tmp, size);
   94 
   95     return (dst);
   96 }
   97 
   98 /*! const char *
   99  * isc_inet_ntop6(src, dst, size)
  100  *  convert IPv6 binary address into presentation (printable) format
  101  * \author
  102  *  Paul Vixie, 1996.
  103  */
  104 #ifdef AF_INET6
  105 static const char *
  106 inet_ntop6(const unsigned char *src, char *dst, size_t size)
  107 {
  108     /*
  109      * Note that int32_t and int16_t need only be "at least" large enough
  110      * to contain a value of the specified size.  On some systems, like
  111      * Crays, there is no such thing as an integer variable with 16 bits.
  112      * Keep this in mind if you think this function should have been coded
  113      * to use pointer overlays.  All the world's not a VAX.
  114      */
  115     char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
  116     struct { int base, len; } best, cur;
  117     unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
  118     int i;
  119 
  120     /*
  121      * Preprocess:
  122      *  Copy the input (bytewise) array into a wordwise array.
  123      *  Find the longest run of 0x00's in src[] for :: shorthanding.
  124      */
  125     memset(words, '\0', sizeof(words));
  126     for (i = 0; i < NS_IN6ADDRSZ; i++)
  127         words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
  128     best.base = -1;
  129     best.len = 0;   /* silence compiler */
  130     cur.base = -1;
  131     cur.len = 0;    /* silence compiler */
  132     for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
  133         if (words[i] == 0) {
  134             if (cur.base == -1)
  135                 cur.base = i, cur.len = 1;
  136             else
  137                 cur.len++;
  138         } else {
  139             if (cur.base != -1) {
  140                 if (best.base == -1 || cur.len > best.len)
  141                     best = cur;
  142                 cur.base = -1;
  143             }
  144         }
  145     }
  146     if (cur.base != -1) {
  147         if (best.base == -1 || cur.len > best.len)
  148             best = cur;
  149     }
  150     if (best.base != -1 && best.len < 2)
  151         best.base = -1;
  152 
  153     /*
  154      * Format the result.
  155      */
  156     tp = tmp;
  157     for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
  158         /* Are we inside the best run of 0x00's? */
  159         if (best.base != -1 && i >= best.base &&
  160             i < (best.base + best.len)) {
  161             if (i == best.base)
  162                 *tp++ = ':';
  163             continue;
  164         }
  165         /* Are we following an initial run of 0x00s or any real hex? */
  166         if (i != 0)
  167             *tp++ = ':';
  168         /* Is this address an encapsulated IPv4? */
  169         if (i == 6 && best.base == 0 && (best.len == 6 ||
  170             (best.len == 7 && words[7] != 0x0001) ||
  171             (best.len == 5 && words[5] == 0xffff))) {
  172             if (!inet_ntop4(src+12, tp,
  173                     sizeof(tmp) - (tp - tmp)))
  174                 return (NULL);
  175             tp += strlen(tp);
  176             break;
  177         }
  178         INSIST((size_t)(tp - tmp) < sizeof(tmp));
  179         tp += snprintf(tp, sizeof(tmp) - (tp - tmp), "%x", words[i]);
  180     }
  181     /* Was it a trailing run of 0x00's? */
  182     if (best.base != -1 && (best.base + best.len) ==
  183         (NS_IN6ADDRSZ / NS_INT16SZ))
  184         *tp++ = ':';
  185     *tp++ = '\0';
  186 
  187     /*
  188      * Check for overflow, copy, and we're done.
  189      */
  190     if ((size_t)(tp - tmp) > size) {
  191         errno = ENOSPC;
  192         return (NULL);
  193     }
  194     strlcpy(dst, tmp, size);
  195     return (dst);
  196 }
  197 #endif /* AF_INET6 */