Back to index

nagios-plugins  1.4.16
inet_ntop.c
Go to the documentation of this file.
00001 /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
00002 
00003    Copyright (C) 2005-2006, 2008-2010 Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00018 
00019 /*
00020  * Copyright (c) 1996-1999 by Internet Software Consortium.
00021  *
00022  * Permission to use, copy, modify, and distribute this software for any
00023  * purpose with or without fee is hereby granted, provided that the above
00024  * copyright notice and this permission notice appear in all copies.
00025  *
00026  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00027  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00028  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00029  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00030  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00031  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00032  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00033  * SOFTWARE.
00034  */
00035 
00036 #include <config.h>
00037 
00038 /* Specification.  */
00039 #include <arpa/inet.h>
00040 
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <errno.h>
00044 
00045 #define NS_IN6ADDRSZ 16
00046 #define NS_INT16SZ 2
00047 
00048 /*
00049  * WARNING: Don't even consider trying to compile this on a system where
00050  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
00051  */
00052 typedef int verify_int_size[2 * sizeof (int) - 7];
00053 
00054 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
00055 #if HAVE_IPV6
00056 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
00057 #endif
00058 
00059 
00060 /* char *
00061  * inet_ntop(af, src, dst, size)
00062  *      convert a network format address to presentation format.
00063  * return:
00064  *      pointer to presentation format address (`dst'), or NULL (see errno).
00065  * author:
00066  *      Paul Vixie, 1996.
00067  */
00068 const char *
00069 inet_ntop (int af, const void *restrict src,
00070            char *restrict dst, socklen_t cnt)
00071 {
00072   switch (af)
00073     {
00074 #if HAVE_IPV4
00075     case AF_INET:
00076       return (inet_ntop4 (src, dst, cnt));
00077 #endif
00078 
00079 #if HAVE_IPV6
00080     case AF_INET6:
00081       return (inet_ntop6 (src, dst, cnt));
00082 #endif
00083 
00084     default:
00085       errno = EAFNOSUPPORT;
00086       return (NULL);
00087     }
00088   /* NOTREACHED */
00089 }
00090 
00091 /* const char *
00092  * inet_ntop4(src, dst, size)
00093  *      format an IPv4 address
00094  * return:
00095  *      `dst' (as a const)
00096  * notes:
00097  *      (1) uses no statics
00098  *      (2) takes a u_char* not an in_addr as input
00099  * author:
00100  *      Paul Vixie, 1996.
00101  */
00102 static const char *
00103 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
00104 {
00105   char tmp[sizeof "255.255.255.255"];
00106   int len;
00107 
00108   len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
00109   if (len < 0)
00110     return NULL;
00111 
00112   if (len > size)
00113     {
00114       errno = ENOSPC;
00115       return NULL;
00116     }
00117 
00118   return strcpy (dst, tmp);
00119 }
00120 
00121 #if HAVE_IPV6
00122 
00123 /* const char *
00124  * inet_ntop6(src, dst, size)
00125  *      convert IPv6 binary address into presentation (printable) format
00126  * author:
00127  *      Paul Vixie, 1996.
00128  */
00129 static const char *
00130 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
00131 {
00132   /*
00133    * Note that int32_t and int16_t need only be "at least" large enough
00134    * to contain a value of the specified size.  On some systems, like
00135    * Crays, there is no such thing as an integer variable with 16 bits.
00136    * Keep this in mind if you think this function should have been coded
00137    * to use pointer overlays.  All the world's not a VAX.
00138    */
00139   char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
00140   struct
00141   {
00142     int base, len;
00143   } best, cur;
00144   unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
00145   int i;
00146 
00147   /*
00148    * Preprocess:
00149    *      Copy the input (bytewise) array into a wordwise array.
00150    *      Find the longest run of 0x00's in src[] for :: shorthanding.
00151    */
00152   memset (words, '\0', sizeof words);
00153   for (i = 0; i < NS_IN6ADDRSZ; i += 2)
00154     words[i / 2] = (src[i] << 8) | src[i + 1];
00155   best.base = -1;
00156   cur.base = -1;
00157   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
00158     {
00159       if (words[i] == 0)
00160         {
00161           if (cur.base == -1)
00162             cur.base = i, cur.len = 1;
00163           else
00164             cur.len++;
00165         }
00166       else
00167         {
00168           if (cur.base != -1)
00169             {
00170               if (best.base == -1 || cur.len > best.len)
00171                 best = cur;
00172               cur.base = -1;
00173             }
00174         }
00175     }
00176   if (cur.base != -1)
00177     {
00178       if (best.base == -1 || cur.len > best.len)
00179         best = cur;
00180     }
00181   if (best.base != -1 && best.len < 2)
00182     best.base = -1;
00183 
00184   /*
00185    * Format the result.
00186    */
00187   tp = tmp;
00188   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
00189     {
00190       /* Are we inside the best run of 0x00's? */
00191       if (best.base != -1 && i >= best.base && i < (best.base + best.len))
00192         {
00193           if (i == best.base)
00194             *tp++ = ':';
00195           continue;
00196         }
00197       /* Are we following an initial run of 0x00s or any real hex? */
00198       if (i != 0)
00199         *tp++ = ':';
00200       /* Is this address an encapsulated IPv4? */
00201       if (i == 6 && best.base == 0 &&
00202           (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
00203         {
00204           if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
00205             return (NULL);
00206           tp += strlen (tp);
00207           break;
00208         }
00209       {
00210         int len = sprintf (tp, "%x", words[i]);
00211         if (len < 0)
00212           return NULL;
00213         tp += len;
00214       }
00215     }
00216   /* Was it a trailing run of 0x00's? */
00217   if (best.base != -1 && (best.base + best.len) ==
00218       (NS_IN6ADDRSZ / NS_INT16SZ))
00219     *tp++ = ':';
00220   *tp++ = '\0';
00221 
00222   /*
00223    * Check for overflow, copy, and we're done.
00224    */
00225   if ((socklen_t) (tp - tmp) > size)
00226     {
00227       errno = ENOSPC;
00228       return NULL;
00229     }
00230 
00231   return strcpy (dst, tmp);
00232 }
00233 
00234 #endif