Home | History | Annotate | Line # | Download | only in dist
      1 /*
      2  * Copyright (c) 1999 Kungliga Tekniska Hgskolan
      3  * (Royal Institute of Technology, Stockholm, Sweden).
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  *
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by the Kungliga Tekniska
     20  *      Hgskolan and its contributors.
     21  *
     22  * 4. Neither the name of the Institute nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __RCSID("$NetBSD: addrtostr.c,v 1.7 2026/03/19 00:05:13 christos Exp $");
     42 #endif
     43 
     44 #include <config.h>
     45 
     46 #include "netdissect-stdinc.h"
     47 #include "addrtostr.h"
     48 
     49 #include <stdio.h>
     50 #include <string.h>
     51 
     52 /*
     53  *
     54  */
     55 
     56 #ifndef IN6ADDRSZ
     57 #define IN6ADDRSZ   16   /* IPv6 T_AAAA */
     58 #endif
     59 
     60 #ifndef INT16SZ
     61 #define INT16SZ     2    /* word size */
     62 #endif
     63 
     64 const char *
     65 addrtostr (const void *src, char *dst, size_t size)
     66 {
     67     const u_char *srcaddr = (const u_char *)src;
     68     const char digits[] = "0123456789";
     69     int i;
     70     const char *orig_dst = dst;
     71 
     72     if (size < INET_ADDRSTRLEN) {
     73 	errno = ENOSPC;
     74 	return NULL;
     75     }
     76     for (i = 0; i < 4; ++i) {
     77 	int n = *srcaddr++;
     78 	int non_zerop = 0;
     79 
     80 	if (non_zerop || n / 100 > 0) {
     81 	    *dst++ = digits[n / 100];
     82 	    n %= 100;
     83 	    non_zerop = 1;
     84 	}
     85 	if (non_zerop || n / 10 > 0) {
     86 	    *dst++ = digits[n / 10];
     87 	    n %= 10;
     88 	    non_zerop = 1;
     89 	}
     90 	*dst++ = digits[n];
     91 	if (i != 3)
     92 	    *dst++ = '.';
     93     }
     94     *dst++ = '\0';
     95     return orig_dst;
     96 }
     97 
     98 /*
     99  * Convert IPv6 binary address into presentation (printable) format.
    100  */
    101 const char *
    102 addrtostr6 (const void *src, char *dst, size_t size)
    103 {
    104   /*
    105    * Note that int32_t and int16_t need only be "at least" large enough
    106    * to contain a value of the specified size.  On some systems, like
    107    * Crays, there is no such thing as an integer variable with 16 bits.
    108    * Keep this in mind if you think this function should have been coded
    109    * to use pointer overlays.  All the world's not a VAX.
    110    */
    111   const u_char *srcaddr = (const u_char *)src;
    112   char *dp;
    113   size_t space_left, added_space;
    114   int snprintfed;
    115   struct {
    116     int base;
    117     int len;
    118   } best, cur;
    119   uint16_t words [IN6ADDRSZ / INT16SZ];
    120   int  i;
    121 
    122   /* Preprocess:
    123    *  Copy the input (bytewise) array into a wordwise array.
    124    *  Find the longest run of 0x00's in src[] for :: shorthanding.
    125    */
    126   for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
    127       words[i] = (srcaddr[2*i] << 8) | srcaddr[2*i + 1];
    128 
    129   best.len = 0;
    130   best.base = -1;
    131   cur.len = 0;
    132   cur.base  = -1;
    133   for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
    134     if (words[i] == 0) {
    135       if (cur.base == -1) {
    136          cur.base = i;
    137          cur.len = 1;
    138       } else
    139          cur.len++;
    140     } else if (cur.base != -1) {
    141       if (best.base == -1 || cur.len > best.len)
    142          best = cur;
    143       cur.base = -1;
    144     }
    145   }
    146   if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
    147      best = cur;
    148   if (best.base != -1 && best.len < 2)
    149      best.base = -1;
    150 
    151   /* Format the result.
    152    */
    153   dp = dst;
    154   space_left = size;
    155 #define APPEND_CHAR(c) \
    156     { \
    157         if (space_left == 0) { \
    158             errno = ENOSPC; \
    159             return (NULL); \
    160         } \
    161         *dp++ = c; \
    162         space_left--; \
    163     }
    164   for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
    165     /* Are we inside the best run of 0x00's?
    166      */
    167     if (best.base != -1 && i >= best.base && i < (best.base + best.len)) {
    168       if (i == best.base)
    169 	  APPEND_CHAR(':');
    170       continue;
    171     }
    172 
    173     /* Are we following an initial run of 0x00s or any real hex?
    174      */
    175     if (i != 0)
    176        APPEND_CHAR(':');
    177 
    178     /* Is this address an encapsulated IPv4?
    179      */
    180     if (i == 6 && best.base == 0 &&
    181         (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
    182     {
    183       if (!addrtostr(srcaddr+12, dp, space_left)) {
    184         errno = ENOSPC;
    185         return (NULL);
    186       }
    187       added_space = strlen(dp);
    188       dp += added_space;
    189       space_left -= added_space;
    190       break;
    191     }
    192     snprintfed = snprintf (dp, space_left, "%x", words[i]);
    193     if (snprintfed < 0)
    194         return (NULL);
    195     if ((size_t) snprintfed >= space_left) {
    196         errno = ENOSPC;
    197         return (NULL);
    198     }
    199     dp += snprintfed;
    200     space_left -= snprintfed;
    201   }
    202 
    203   /* Was it a trailing run of 0x00's?
    204    */
    205   if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
    206      APPEND_CHAR(':');
    207   APPEND_CHAR('\0');
    208 
    209   return (dst);
    210 }
    211