The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/rpc/inet_ntop.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
    3  * Copyright (c) 1996-1999 by Internet Software Consortium.
    4  *
    5  * Permission to use, copy, modify, and distribute this software for any
    6  * purpose with or without fee is hereby granted, provided that the above
    7  * copyright notice and this permission notice appear in all copies.
    8  *
    9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
   10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
   12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
   15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   16  */
   17 
   18 #if defined(LIBC_SCCS) && !defined(lint)
   19 static const char rcsid[] = "$Id: inet_ntop.c,v 1.3.18.2 2005/11/03 23:02:22 marka Exp $";
   20 #endif /* LIBC_SCCS and not lint */
   21 #include <sys/cdefs.h>
   22 __FBSDID("$FreeBSD$");
   23 
   24 #include <sys/param.h>
   25 #include <sys/types.h>
   26 #include <sys/socket.h>
   27 #include <sys/systm.h>
   28 
   29 #include <rpc/types.h>
   30 #include <rpc/rpc_com.h>
   31 
   32 /*%
   33  * WARNING: Don't even consider trying to compile this on a system where
   34  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
   35  */
   36 
   37 static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
   38 static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
   39 
   40 /* char *
   41  * inet_ntop(af, src, dst, size)
   42  *      convert a network format address to presentation format.
   43  * return:
   44  *      pointer to presentation format address (`dst'), or NULL (see errno).
   45  * author:
   46  *      Paul Vixie, 1996.
   47  */
   48 const char *
   49 __rpc_inet_ntop(int af, const void * __restrict src, char * __restrict dst,
   50     socklen_t size)
   51 {
   52         switch (af) {
   53         case AF_INET:
   54                 return (inet_ntop4(src, dst, size));
   55         case AF_INET6:
   56                 return (inet_ntop6(src, dst, size));
   57         default:
   58                 return (NULL);
   59         }
   60         /* NOTREACHED */
   61 }
   62 
   63 /* const char *
   64  * inet_ntop4(src, dst, size)
   65  *      format an IPv4 address
   66  * return:
   67  *      `dst' (as a const)
   68  * notes:
   69  *      (1) uses no statics
   70  *      (2) takes a u_char* not an in_addr as input
   71  * author:
   72  *      Paul Vixie, 1996.
   73  */
   74 static const char *
   75 inet_ntop4(const u_char *src, char *dst, socklen_t size)
   76 {
   77         static const char fmt[] = "%u.%u.%u.%u";
   78         char tmp[sizeof "255.255.255.255"];
   79         int l;
   80 
   81         l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
   82         if (l <= 0 || (socklen_t) l >= size) {
   83                 return (NULL);
   84         }
   85         strlcpy(dst, tmp, size);
   86         return (dst);
   87 }
   88 
   89 /* const char *
   90  * inet_ntop6(src, dst, size)
   91  *      convert IPv6 binary address into presentation (printable) format
   92  * author:
   93  *      Paul Vixie, 1996.
   94  */
   95 static const char *
   96 inet_ntop6(const u_char *src, char *dst, socklen_t size)
   97 {
   98         /*
   99          * Note that int32_t and int16_t need only be "at least" large enough
  100          * to contain a value of the specified size.  On some systems, like
  101          * Crays, there is no such thing as an integer variable with 16 bits.
  102          * Keep this in mind if you think this function should have been coded
  103          * to use pointer overlays.  All the world's not a VAX.
  104          */
  105         char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  106         struct { int base, len; } best, cur;
  107 #define NS_IN6ADDRSZ    16
  108 #define NS_INT16SZ      2
  109         u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
  110         int i;
  111 
  112         /*
  113          * Preprocess:
  114          *      Copy the input (bytewise) array into a wordwise array.
  115          *      Find the longest run of 0x00's in src[] for :: shorthanding.
  116          */
  117         memset(words, '\0', sizeof words);
  118         for (i = 0; i < NS_IN6ADDRSZ; i++)
  119                 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
  120         best.base = -1;
  121         best.len = 0;
  122         cur.base = -1;
  123         cur.len = 0;
  124         for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
  125                 if (words[i] == 0) {
  126                         if (cur.base == -1)
  127                                 cur.base = i, cur.len = 1;
  128                         else
  129                                 cur.len++;
  130                 } else {
  131                         if (cur.base != -1) {
  132                                 if (best.base == -1 || cur.len > best.len)
  133                                         best = cur;
  134                                 cur.base = -1;
  135                         }
  136                 }
  137         }
  138         if (cur.base != -1) {
  139                 if (best.base == -1 || cur.len > best.len)
  140                         best = cur;
  141         }
  142         if (best.base != -1 && best.len < 2)
  143                 best.base = -1;
  144 
  145         /*
  146          * Format the result.
  147          */
  148         tp = tmp;
  149         for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
  150                 /* Are we inside the best run of 0x00's? */
  151                 if (best.base != -1 && i >= best.base &&
  152                     i < (best.base + best.len)) {
  153                         if (i == best.base)
  154                                 *tp++ = ':';
  155                         continue;
  156                 }
  157                 /* Are we following an initial run of 0x00s or any real hex? */
  158                 if (i != 0)
  159                         *tp++ = ':';
  160                 /* Is this address an encapsulated IPv4? */
  161                 if (i == 6 && best.base == 0 && (best.len == 6 ||
  162                     (best.len == 7 && words[7] != 0x0001) ||
  163                     (best.len == 5 && words[5] == 0xffff))) {
  164                         if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
  165                                 return (NULL);
  166                         tp += strlen(tp);
  167                         break;
  168                 }
  169                 tp += sprintf(tp, "%x", words[i]);
  170         }
  171         /* Was it a trailing run of 0x00's? */
  172         if (best.base != -1 && (best.base + best.len) == 
  173             (NS_IN6ADDRSZ / NS_INT16SZ))
  174                 *tp++ = ':';
  175         *tp++ = '\0';
  176 
  177         /*
  178          * Check for overflow, copy, and we're done.
  179          */
  180         if ((socklen_t)(tp - tmp) > size) {
  181                 return (NULL);
  182         }
  183         strcpy(dst, tmp);
  184         return (dst);
  185 }
  186 
  187 /*! \file */

Cache object: 0c12733a5c4648165e5f261f86816567


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.