LCOV - code coverage report
Current view: top level - src/backend/utils/adt - inet_cidr_ntop.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 77.8 % 135 105
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 3 3
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 64.0 % 89 57

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
       3                 :             :  * Copyright (c) 1996,1999 by Internet Software Consortium.
       4                 :             :  *
       5                 :             :  * Permission to use, copy, modify, and distribute this software for any
       6                 :             :  * purpose with or without fee is hereby granted, provided that the above
       7                 :             :  * copyright notice and this permission notice appear in all copies.
       8                 :             :  *
       9                 :             :  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
      10                 :             :  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      11                 :             :  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
      12                 :             :  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
      13                 :             :  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
      14                 :             :  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
      15                 :             :  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      16                 :             :  *
      17                 :             :  *        src/backend/utils/adt/inet_cidr_ntop.c
      18                 :             :  */
      19                 :             : 
      20                 :             : #if defined(LIBC_SCCS) && !defined(lint)
      21                 :             : static const char rcsid[] = "Id: inet_net_ntop.c,v 1.1.2.2 2004/03/09 09:17:27 marka Exp $";
      22                 :             : #endif
      23                 :             : 
      24                 :             : #include "postgres.h"
      25                 :             : 
      26                 :             : #include <sys/socket.h>
      27                 :             : #include <netinet/in.h>
      28                 :             : #include <arpa/inet.h>
      29                 :             : 
      30                 :             : #include "utils/builtins.h"
      31                 :             : #include "utils/inet.h"
      32                 :             : 
      33                 :             : 
      34                 :             : #ifdef SPRINTF_CHAR
      35                 :             : #define SPRINTF(x) strlen(sprintf/**/x)
      36                 :             : #else
      37                 :             : #define SPRINTF(x) ((size_t)sprintf x)
      38                 :             : #endif
      39                 :             : 
      40                 :             : static char *inet_cidr_ntop_ipv4(const u_char *src, int bits,
      41                 :             :                                                                  char *dst, size_t size);
      42                 :             : static char *inet_cidr_ntop_ipv6(const u_char *src, int bits,
      43                 :             :                                                                  char *dst, size_t size);
      44                 :             : 
      45                 :             : /*
      46                 :             :  * char *
      47                 :             :  * pg_inet_cidr_ntop(af, src, bits, dst, size)
      48                 :             :  *      convert network number from network to presentation format.
      49                 :             :  *      generates CIDR style result always.
      50                 :             :  * return:
      51                 :             :  *      pointer to dst, or NULL if an error occurred (check errno).
      52                 :             :  * author:
      53                 :             :  *      Paul Vixie (ISC), July 1996
      54                 :             :  */
      55                 :             : char *
      56                 :          17 : pg_inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
      57                 :             : {
      58      [ +  -  + ]:          17 :         switch (af)
      59                 :             :         {
      60                 :             :                 case PGSQL_AF_INET:
      61                 :          14 :                         return inet_cidr_ntop_ipv4(src, bits, dst, size);
      62                 :             :                 case PGSQL_AF_INET6:
      63                 :           3 :                         return inet_cidr_ntop_ipv6(src, bits, dst, size);
      64                 :             :                 default:
      65                 :           0 :                         errno = EAFNOSUPPORT;
      66                 :           0 :                         return NULL;
      67                 :             :         }
      68                 :          17 : }
      69                 :             : 
      70                 :             : 
      71                 :             : /*
      72                 :             :  * static char *
      73                 :             :  * inet_cidr_ntop_ipv4(src, bits, dst, size)
      74                 :             :  *      convert IPv4 network number from network to presentation format.
      75                 :             :  *      generates CIDR style result always.
      76                 :             :  * return:
      77                 :             :  *      pointer to dst, or NULL if an error occurred (check errno).
      78                 :             :  * note:
      79                 :             :  *      network byte order assumed.  this means 192.5.5.240/28 has
      80                 :             :  *      0b11110000 in its fourth octet.
      81                 :             :  * author:
      82                 :             :  *      Paul Vixie (ISC), July 1996
      83                 :             :  */
      84                 :             : static char *
      85                 :          14 : inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
      86                 :             : {
      87                 :          14 :         char       *odst = dst;
      88                 :          14 :         char       *t;
      89                 :          14 :         u_int           m;
      90                 :          14 :         int                     b;
      91                 :             : 
      92   [ +  -  -  + ]:          14 :         if (bits < 0 || bits > 32)
      93                 :             :         {
      94                 :           0 :                 errno = EINVAL;
      95                 :           0 :                 return NULL;
      96                 :             :         }
      97                 :             : 
      98         [ +  - ]:          14 :         if (bits == 0)
      99                 :             :         {
     100         [ #  # ]:           0 :                 if (size < sizeof "0")
     101                 :           0 :                         goto emsgsize;
     102                 :           0 :                 *dst++ = '0';
     103                 :           0 :                 size--;
     104                 :           0 :                 *dst = '\0';
     105                 :           0 :         }
     106                 :             : 
     107                 :             :         /* Format whole octets. */
     108         [ +  + ]:          49 :         for (b = bits / 8; b > 0; b--)
     109                 :             :         {
     110         [ -  + ]:          35 :                 if (size <= sizeof "255.")
     111                 :           0 :                         goto emsgsize;
     112                 :          35 :                 t = dst;
     113                 :          35 :                 dst += SPRINTF((dst, "%u", *src++));
     114         [ +  + ]:          35 :                 if (b > 1)
     115                 :             :                 {
     116                 :          21 :                         *dst++ = '.';
     117                 :          21 :                         *dst = '\0';
     118                 :          21 :                 }
     119                 :          35 :                 size -= (size_t) (dst - t);
     120                 :          35 :         }
     121                 :             : 
     122                 :             :         /* Format partial octet. */
     123                 :          14 :         b = bits % 8;
     124         [ +  + ]:          14 :         if (b > 0)
     125                 :             :         {
     126         [ -  + ]:           1 :                 if (size <= sizeof ".255")
     127                 :           0 :                         goto emsgsize;
     128                 :           1 :                 t = dst;
     129         [ -  + ]:           1 :                 if (dst != odst)
     130                 :           1 :                         *dst++ = '.';
     131                 :           1 :                 m = ((1 << b) - 1) << (8 - b);
     132                 :           1 :                 dst += SPRINTF((dst, "%u", *src & m));
     133                 :           1 :                 size -= (size_t) (dst - t);
     134                 :           1 :         }
     135                 :             : 
     136                 :             :         /* Format CIDR /width. */
     137         [ -  + ]:          14 :         if (size <= sizeof "/32")
     138                 :           0 :                 goto emsgsize;
     139                 :          14 :         dst += SPRINTF((dst, "/%u", bits));
     140                 :          14 :         return odst;
     141                 :             : 
     142                 :             : emsgsize:
     143                 :           0 :         errno = EMSGSIZE;
     144                 :           0 :         return NULL;
     145                 :          14 : }
     146                 :             : 
     147                 :             : /*
     148                 :             :  * static char *
     149                 :             :  * inet_cidr_ntop_ipv6(src, bits, dst, size)
     150                 :             :  *      convert IPv6 network number from network to presentation format.
     151                 :             :  *      generates CIDR style result always. Picks the shortest representation
     152                 :             :  *      unless the IP is really IPv4.
     153                 :             :  *      always prints specified number of bits (bits).
     154                 :             :  * return:
     155                 :             :  *      pointer to dst, or NULL if an error occurred (check errno).
     156                 :             :  * note:
     157                 :             :  *      network byte order assumed.  this means 192.5.5.240/28 has
     158                 :             :  *      0x11110000 in its fourth octet.
     159                 :             :  * author:
     160                 :             :  *      Vadim Kogan (UCB), June 2001
     161                 :             :  *      Original version (IPv4) by Paul Vixie (ISC), July 1996
     162                 :             :  */
     163                 :             : 
     164                 :             : static char *
     165                 :           3 : inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
     166                 :             : {
     167                 :           3 :         u_int           m;
     168                 :           3 :         int                     b;
     169                 :           3 :         int                     p;
     170                 :           3 :         int                     zero_s,
     171                 :             :                                 zero_l,
     172                 :             :                                 tmp_zero_s,
     173                 :             :                                 tmp_zero_l;
     174                 :           3 :         int                     i;
     175                 :           3 :         int                     is_ipv4 = 0;
     176                 :           3 :         unsigned char inbuf[16];
     177                 :           3 :         char            outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
     178                 :           3 :         char       *cp;
     179                 :           3 :         int                     words;
     180                 :           3 :         u_char     *s;
     181                 :             : 
     182   [ +  -  -  + ]:           3 :         if (bits < 0 || bits > 128)
     183                 :             :         {
     184                 :           0 :                 errno = EINVAL;
     185                 :           0 :                 return NULL;
     186                 :             :         }
     187                 :             : 
     188                 :           3 :         cp = outbuf;
     189                 :             : 
     190         [ +  - ]:           3 :         if (bits == 0)
     191                 :             :         {
     192                 :           0 :                 *cp++ = ':';
     193                 :           0 :                 *cp++ = ':';
     194                 :           0 :                 *cp = '\0';
     195                 :           0 :         }
     196                 :             :         else
     197                 :             :         {
     198                 :             :                 /* Copy src to private buffer.  Zero host part. */
     199                 :           3 :                 p = (bits + 7) / 8;
     200                 :           3 :                 memcpy(inbuf, src, p);
     201                 :           3 :                 memset(inbuf + p, 0, 16 - p);
     202                 :           3 :                 b = bits % 8;
     203         [ +  + ]:           3 :                 if (b != 0)
     204                 :             :                 {
     205                 :           1 :                         m = ((u_int) ~0) << (8 - b);
     206                 :           1 :                         inbuf[p - 1] &= m;
     207                 :           1 :                 }
     208                 :             : 
     209                 :           3 :                 s = inbuf;
     210                 :             : 
     211                 :             :                 /* how many words need to be displayed in output */
     212                 :           3 :                 words = (bits + 15) / 16;
     213         [ +  - ]:           3 :                 if (words == 1)
     214                 :           0 :                         words = 2;
     215                 :             : 
     216                 :             :                 /* Find the longest substring of zero's */
     217                 :           3 :                 zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
     218         [ +  + ]:          27 :                 for (i = 0; i < (words * 2); i += 2)
     219                 :             :                 {
     220         [ +  + ]:          24 :                         if ((s[i] | s[i + 1]) == 0)
     221                 :             :                         {
     222         [ +  + ]:          15 :                                 if (tmp_zero_l == 0)
     223                 :           3 :                                         tmp_zero_s = i / 2;
     224                 :          15 :                                 tmp_zero_l++;
     225                 :          15 :                         }
     226                 :             :                         else
     227                 :             :                         {
     228   [ +  +  -  + ]:           9 :                                 if (tmp_zero_l && zero_l < tmp_zero_l)
     229                 :             :                                 {
     230                 :           3 :                                         zero_s = tmp_zero_s;
     231                 :           3 :                                         zero_l = tmp_zero_l;
     232                 :           3 :                                         tmp_zero_l = 0;
     233                 :           3 :                                 }
     234                 :             :                         }
     235                 :          24 :                 }
     236                 :             : 
     237   [ -  +  #  # ]:           3 :                 if (tmp_zero_l && zero_l < tmp_zero_l)
     238                 :             :                 {
     239                 :           0 :                         zero_s = tmp_zero_s;
     240                 :           0 :                         zero_l = tmp_zero_l;
     241                 :           0 :                 }
     242                 :             : 
     243   [ +  -  +  +  :           3 :                 if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
             +  -  #  # ]
     244   [ +  -  +  - ]:           1 :                                                                                            ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
     245   [ #  #  #  # ]:           0 :                                                                                                 ((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
     246                 :           1 :                         is_ipv4 = 1;
     247                 :             : 
     248                 :             :                 /* Format whole words. */
     249         [ +  + ]:          27 :                 for (p = 0; p < words; p++)
     250                 :             :                 {
     251   [ +  -  +  +  :          24 :                         if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l)
                   +  + ]
     252                 :             :                         {
     253                 :             :                                 /* Time to skip some zeros */
     254         [ +  + ]:          15 :                                 if (p == zero_s)
     255                 :           3 :                                         *cp++ = ':';
     256         [ +  - ]:          15 :                                 if (p == words - 1)
     257                 :           0 :                                         *cp++ = ':';
     258                 :          15 :                                 s++;
     259                 :          15 :                                 s++;
     260                 :          15 :                                 continue;
     261                 :             :                         }
     262                 :             : 
     263   [ +  +  +  + ]:           9 :                         if (is_ipv4 && p > 5)
     264                 :             :                         {
     265                 :           2 :                                 *cp++ = (p == 6) ? ':' : '.';
     266                 :           2 :                                 cp += SPRINTF((cp, "%u", *s++));
     267                 :             :                                 /* we can potentially drop the last octet */
     268   [ +  +  +  - ]:           2 :                                 if (p != 7 || bits > 120)
     269                 :             :                                 {
     270                 :           2 :                                         *cp++ = '.';
     271                 :           2 :                                         cp += SPRINTF((cp, "%u", *s++));
     272                 :           2 :                                 }
     273                 :           2 :                         }
     274                 :             :                         else
     275                 :             :                         {
     276         [ +  + ]:           7 :                                 if (cp != outbuf)
     277                 :           5 :                                         *cp++ = ':';
     278                 :           7 :                                 cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
     279                 :           7 :                                 s += 2;
     280                 :             :                         }
     281                 :           9 :                 }
     282                 :             :         }
     283                 :             :         /* Format CIDR /width. */
     284                 :           3 :         (void) SPRINTF((cp, "/%u", bits));
     285         [ -  + ]:           3 :         if (strlen(outbuf) + 1 > size)
     286                 :           0 :                 goto emsgsize;
     287                 :           3 :         strcpy(dst, outbuf);
     288                 :             : 
     289                 :           3 :         return dst;
     290                 :             : 
     291                 :             : emsgsize:
     292                 :           0 :         errno = EMSGSIZE;
     293                 :           0 :         return NULL;
     294                 :           3 : }
        

Generated by: LCOV version 2.3.2-1