net-snmp 5.7
inet_ntop.c
00001 /*      Id: inet_ntop.c,v 1.4 2001/04/17 07:53:47 lukem Exp     */
00002 /*      $NetBSD: inet_ntop.c,v 1.9 2000/01/22 22:19:16 mycroft Exp $    */
00003 
00004 /* Copyright (c) 1996 by Internet Software Consortium.
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00011  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00012  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00013  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00014  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00015  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00016  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00017  * SOFTWARE.
00018  */
00019 
00020 #include <net-snmp/net-snmp-config.h>
00021 
00022 #if HAVE_ARPA_NAMESER_H
00023 #include <arpa/nameser.h>
00024 #endif
00025 
00026 #include <errno.h>
00027 #include <stdio.h>
00028 #if HAVE_STRING_H
00029 #include <string.h>
00030 #else
00031 #include <strings.h>
00032 #endif
00033 
00034 #include <net-snmp/types.h>
00035 
00036 #include "inet_ntop.h"
00037 
00038 #ifndef EAFNOSUPPORT
00039 #define EAFNOSUPPORT            WSAEAFNOSUPPORT
00040 #endif
00041 
00042 #ifndef IN6ADDRSZ
00043 #define IN6ADDRSZ       16
00044 #endif
00045 
00046 #ifndef INT16SZ
00047 #define INT16SZ         2
00048 #endif
00049 
00050 #ifdef SPRINTF_CHAR
00051 # define SPRINTF(x) strlen(sprintfx)
00052 #else
00053 # define SPRINTF(x) ((size_t)sprintf x)
00054 #endif
00055 
00056 /*
00057  * WARNING: Don't even consider trying to compile this on a system where
00058  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
00059  */
00060 
00061 static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
00062 #ifdef NETSNMP_ENABLE_IPV6
00063 static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
00064 #endif /* NETSNMP_ENABLE_IPV6 */
00065 
00066 /* char *
00067  * inet_ntop(af, src, dst, size)
00068  *      convert a network format address to presentation format.
00069  * return:
00070  *      pointer to presentation format address (`dst'), or NULL (see errno).
00071  * author:
00072  *      Paul Vixie, 1996.
00073  */
00074 const char *
00075 inet_ntop(int af, const void *src, char *dst, size_t size)
00076 {
00077 
00078         switch (af) {
00079         case AF_INET:
00080                 return (inet_ntop4(src, dst, size));
00081 #ifdef NETSNMP_ENABLE_IPV6
00082         case AF_INET6:
00083                 return (inet_ntop6(src, dst, size));
00084 #endif
00085         default:
00086                 errno = EAFNOSUPPORT;
00087                 return (NULL);
00088         }
00089         /* NOTREACHED */
00090 }
00091 
00092 /* const char *
00093  * inet_ntop4(src, dst, size)
00094  *      format an IPv4 address, more or less like inet_ntoa()
00095  * return:
00096  *      `dst' (as a const)
00097  * notes:
00098  *      (1) uses no statics
00099  *      (2) takes a u_char* not an in_addr as input
00100  * author:
00101  *      Paul Vixie, 1996.
00102  */
00103 static const char *
00104 inet_ntop4(const u_char *src, char *dst, size_t size)
00105 {
00106         static const char fmt[] = "%u.%u.%u.%u";
00107         char tmp[sizeof "255.255.255.255"];
00108 
00109         if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
00110                 errno = ENOSPC;
00111                 return (NULL);
00112         }
00113         strcpy(dst, tmp);
00114         return (dst);
00115 }
00116 
00117 #ifdef NETSNMP_ENABLE_IPV6
00118 /* const char *
00119  * inet_ntop6(src, dst, size)
00120  *      convert IPv6 binary address into presentation (printable) format
00121  * author:
00122  *      Paul Vixie, 1996.
00123  */
00124 static const char *
00125 inet_ntop6(const u_char *src, char *dst, size_t size)
00126 {
00127         /*
00128          * Note that int32_t and int16_t need only be "at least" large enough
00129          * to contain a value of the specified size.  On some systems, like
00130          * Crays, there is no such thing as an integer variable with 16 bits.
00131          * Keep this in mind if you think this function should have been coded
00132          * to use pointer overlays.  All the world's not a VAX.
00133          */
00134         char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
00135         struct { int base, len; } best, cur;
00136         u_int words[IN6ADDRSZ / INT16SZ];
00137         int i;
00138 
00139         /*
00140          * Preprocess:
00141          *      Copy the input (bytewise) array into a wordwise array.
00142          *      Find the longest run of 0x00's in src[] for :: shorthanding.
00143          */
00144         memset(words, '\0', sizeof words);
00145         for (i = 0; i < IN6ADDRSZ; i++)
00146                 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
00147         best.base = -1;
00148         cur.base = -1;
00149         for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
00150                 if (words[i] == 0) {
00151                         if (cur.base == -1)
00152                                 cur.base = i, cur.len = 1;
00153                         else
00154                                 cur.len++;
00155                 } else {
00156                         if (cur.base != -1) {
00157                                 if (best.base == -1 || cur.len > best.len)
00158                                         best = cur;
00159                                 cur.base = -1;
00160                         }
00161                 }
00162         }
00163         if (cur.base != -1) {
00164                 if (best.base == -1 || cur.len > best.len)
00165                         best = cur;
00166         }
00167         if (best.base != -1 && best.len < 2)
00168                 best.base = -1;
00169 
00170         /*
00171          * Format the result.
00172          */
00173         tp = tmp;
00174         for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
00175                 /* Are we inside the best run of 0x00's? */
00176                 if (best.base != -1 && i >= best.base &&
00177                     i < (best.base + best.len)) {
00178                         if (i == best.base)
00179                                 *tp++ = ':';
00180                         continue;
00181                 }
00182                 /* Are we following an initial run of 0x00s or any real hex? */
00183                 if (i != 0)
00184                         *tp++ = ':';
00185                 /* Is this address an encapsulated IPv4? */
00186                 if (i == 6 && best.base == 0 &&
00187                     (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
00188                         if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
00189                                 return (NULL);
00190                         tp += strlen(tp);
00191                         break;
00192                 }
00193                 tp += SPRINTF((tp, "%x", words[i]));
00194         }
00195         /* Was it a trailing run of 0x00's? */
00196         if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
00197                 *tp++ = ':';
00198         *tp++ = '\0';
00199 
00200         /*
00201          * Check for overflow, copy, and we're done.
00202          */
00203         if ((size_t)(tp - tmp) > size) {
00204                 errno = ENOSPC;
00205                 return (NULL);
00206         }
00207         strcpy(dst, tmp);
00208         return (dst);
00209 }
00210 #endif