Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

inet_ntop.c

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

Generated on Tue Apr 16 12:37:36 2002 for rsync by doxygen1.2.15