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