LCOV - differential code coverage report
Current view: top level - src/port - inet_net_ntop.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB
Current: Differential Code Coverage HEAD vs 15 Lines: 82.5 % 97 80 3 6 8 4 42 34 5 43
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 4 4 3 1 3
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 82.5 % 97 80 3 6 8 4 42 34 5 43
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 57.1 % 7 4 3 1 3

 Age         Owner                  TLA  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/port/inet_net_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                 : #ifndef FRONTEND
                                 25                 : #include "postgres.h"
                                 26                 : #else
                                 27                 : #include "postgres_fe.h"
                                 28                 : #endif
                                 29                 : 
                                 30                 : #include <sys/socket.h>
                                 31                 : #include <netinet/in.h>
                                 32                 : #include <arpa/inet.h>
                                 33                 : 
                                 34                 : #ifndef FRONTEND
                                 35                 : #include "utils/inet.h"
                                 36                 : #else
                                 37                 : /*
                                 38                 :  * In a frontend build, we can't include inet.h, but we still need to have
                                 39                 :  * sensible definitions of these two constants.  Note that pg_inet_net_ntop()
                                 40                 :  * assumes that PGSQL_AF_INET is equal to AF_INET.
                                 41                 :  */
                                 42                 : #define PGSQL_AF_INET   (AF_INET + 0)
                                 43                 : #define PGSQL_AF_INET6  (AF_INET + 1)
                                 44                 : #endif
                                 45                 : 
                                 46                 : 
                                 47                 : #define NS_IN6ADDRSZ 16
                                 48                 : #define NS_INT16SZ 2
                                 49                 : 
                                 50                 : #ifdef SPRINTF_CHAR
                                 51                 : #define SPRINTF(x) strlen(sprintf/**/x)
                                 52                 : #else
                                 53                 : #define SPRINTF(x) ((size_t)sprintf x)
                                 54                 : #endif
                                 55                 : 
                                 56                 : static char *inet_net_ntop_ipv4(const u_char *src, int bits,
                                 57                 :                                 char *dst, size_t size);
                                 58                 : static char *inet_net_ntop_ipv6(const u_char *src, int bits,
                                 59                 :                                 char *dst, size_t size);
                                 60                 : 
                                 61                 : 
                                 62                 : /*
                                 63                 :  * char *
                                 64                 :  * pg_inet_net_ntop(af, src, bits, dst, size)
                                 65                 :  *  convert host/network address from network to presentation format.
                                 66                 :  *  "src"'s size is determined from its "af".
                                 67                 :  * return:
                                 68                 :  *  pointer to dst, or NULL if an error occurred (check errno).
                                 69                 :  * note:
                                 70                 :  *  192.5.5.1/28 has a nonzero host part, which means it isn't a network
                                 71                 :  *  as called for by pg_inet_net_pton() but it can be a host address with
                                 72                 :  *  an included netmask.
                                 73                 :  * author:
                                 74                 :  *  Paul Vixie (ISC), October 1998
                                 75                 :  */
                                 76                 : char *
 1330 tgl                        77 CBC        7429 : pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
                                 78                 : {
                                 79                 :     /*
                                 80                 :      * We need to cover both the address family constants used by the PG inet
                                 81                 :      * type (PGSQL_AF_INET and PGSQL_AF_INET6) and those used by the system
                                 82                 :      * libraries (AF_INET and AF_INET6).  We can safely assume PGSQL_AF_INET
                                 83                 :      * == AF_INET, but the INET6 constants are very likely to be different.
 4517 tgl                        84 ECB             :      */
 8935 bruce                      85 GIC        7429 :     switch (af)
 8935 bruce                      86 ECB             :     {
 7229 bruce                      87 CBC        6046 :         case PGSQL_AF_INET:
 8935                            88            6046 :             return (inet_net_ntop_ipv4(src, bits, dst, size));
 7229 bruce                      89 GIC        1383 :         case PGSQL_AF_INET6:
                                 90                 : #if AF_INET6 != PGSQL_AF_INET6
                                 91                 :         case AF_INET6:
 4517 tgl                        92 ECB             : #endif
 7229 bruce                      93 GBC        1383 :             return (inet_net_ntop_ipv6(src, bits, dst, size));
 8935 bruce                      94 UBC           0 :         default:
                                 95               0 :             errno = EAFNOSUPPORT;
 8935 bruce                      96 UIC           0 :             return (NULL);
                                 97                 :     }
                                 98                 : }
                                 99                 : 
                                100                 : /*
                                101                 :  * static char *
                                102                 :  * inet_net_ntop_ipv4(src, bits, dst, size)
                                103                 :  *  convert IPv4 network address from network to presentation format.
                                104                 :  *  "src"'s size is determined from its "af".
                                105                 :  * return:
                                106                 :  *  pointer to dst, or NULL if an error occurred (check errno).
                                107                 :  * note:
                                108                 :  *  network byte order assumed.  this means 192.5.5.240/28 has
                                109                 :  *  0b11110000 in its fourth octet.
                                110                 :  * author:
                                111                 :  *  Paul Vixie (ISC), October 1998
                                112                 :  */
 8935 bruce                     113 ECB             : static char *
 8935 bruce                     114 GIC        6046 : inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
 8935 bruce                     115 ECB             : {
 8720 bruce                     116 GIC        6046 :     char       *odst = dst;
 8720 bruce                     117 ECB             :     char       *t;
 8185 tgl                       118 GIC        6046 :     int         len = 4;
                                119                 :     int         b;
 8935 bruce                     120 ECB             : 
 8935 bruce                     121 GIC        6046 :     if (bits < 0 || bits > 32)
 8935 bruce                     122 EUB             :     {
 8935 bruce                     123 UBC           0 :         errno = EINVAL;
 8935 bruce                     124 UIC           0 :         return (NULL);
                                125                 :     }
                                126                 : 
 8185 tgl                       127 ECB             :     /* Always format all four octets, regardless of mask length. */
 8185 tgl                       128 GIC       30230 :     for (b = len; b > 0; b--)
 8935 bruce                     129 ECB             :     {
 6641 tgl                       130 GBC       24184 :         if (size <= sizeof ".255")
 8935 bruce                     131 LBC           0 :             goto emsgsize;
 8935 bruce                     132 CBC       24184 :         t = dst;
 8185 tgl                       133           24184 :         if (dst != odst)
 8935 bruce                     134           18138 :             *dst++ = '.';
 8185 tgl                       135           24184 :         dst += SPRINTF((dst, "%u", *src++));
 8720 bruce                     136 GIC       24184 :         size -= (size_t) (dst - t);
                                137                 :     }
                                138                 : 
 8935 bruce                     139 ECB             :     /* don't print masklen if 32 bits */
 8185 tgl                       140 GIC        6046 :     if (bits != 32)
 8185 tgl                       141 ECB             :     {
 6641 tgl                       142 GBC        4961 :         if (size <= sizeof "/32")
 8185 tgl                       143 LBC           0 :             goto emsgsize;
 8185 tgl                       144 GIC        4961 :         dst += SPRINTF((dst, "/%u", bits));
                                145                 :     }
 8935 bruce                     146 ECB             : 
 8935 bruce                     147 GIC        6046 :     return (odst);
 8935 bruce                     148 EUB             : 
 8720 bruce                     149 UBC           0 : emsgsize:
 8935                           150               0 :     errno = EMSGSIZE;
 8935 bruce                     151 UIC           0 :     return (NULL);
                                152                 : }
                                153                 : 
 7229 bruce                     154 ECB             : static int
 7188 bruce                     155 GIC         137 : decoct(const u_char *src, int bytes, char *dst, size_t size)
 7188 bruce                     156 ECB             : {
 7188 bruce                     157 GIC         137 :     char       *odst = dst;
                                158                 :     char       *t;
                                159                 :     int         b;
 7229 bruce                     160 ECB             : 
 7188 bruce                     161 GIC         685 :     for (b = 1; b <= bytes; b++)
 7188 bruce                     162 ECB             :     {
 6641 tgl                       163 GBC         548 :         if (size <= sizeof "255.")
 7229 bruce                     164 LBC           0 :             return (0);
 7229 bruce                     165 CBC         548 :         t = dst;
                                166             548 :         dst += SPRINTF((dst, "%u", *src++));
 7188 bruce                     167 GIC         548 :         if (b != bytes)
 7188 bruce                     168 ECB             :         {
 7229 bruce                     169 CBC         411 :             *dst++ = '.';
 7229 bruce                     170 GIC         411 :             *dst = '\0';
 7229 bruce                     171 ECB             :         }
 7188 bruce                     172 GIC         548 :         size -= (size_t) (dst - t);
 7229 bruce                     173 ECB             :     }
 7229 bruce                     174 GIC         137 :     return (dst - odst);
                                175                 : }
                                176                 : 
 7229 bruce                     177 ECB             : static char *
 7229 bruce                     178 GIC        1383 : inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
                                179                 : {
                                180                 :     /*
                                181                 :      * Note that int32_t and int16_t need only be "at least" large enough to
                                182                 :      * contain a value of the specified size.  On some systems, like Crays,
                                183                 :      * there is no such thing as an integer variable with 16 bits. Keep this
                                184                 :      * in mind if you think this function should have been coded to use
                                185                 :      * pointer overlays.  All the world's not a VAX.
                                186                 :      */
                                187                 :     char        tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
                                188                 :     char       *tp;
                                189                 :     struct
                                190                 :     {
                                191                 :         int         base,
                                192                 :                     len;
                                193                 :     }           best, cur;
                                194                 :     u_int       words[NS_IN6ADDRSZ / NS_INT16SZ];
                                195                 :     int         i;
 7229 bruce                     196 ECB             : 
 7188 bruce                     197 GIC        1383 :     if ((bits < -1) || (bits > 128))
 7188 bruce                     198 EUB             :     {
 7229 bruce                     199 UBC           0 :         errno = EINVAL;
 7229 bruce                     200 UIC           0 :         return (NULL);
                                201                 :     }
                                202                 : 
                                203                 :     /*
                                204                 :      * Preprocess: Copy the input (bytewise) array into a wordwise array. Find
                                205                 :      * the longest run of 0x00's in src[] for :: shorthanding.
 7229 bruce                     206 ECB             :      */
 7229 bruce                     207 CBC        1383 :     memset(words, '\0', sizeof words);
                                208           23511 :     for (i = 0; i < NS_IN6ADDRSZ; i++)
                                209           22128 :         words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
                                210            1383 :     best.base = -1;
                                211            1383 :     cur.base = -1;
 6406 tgl                       212            1383 :     best.len = 0;
                                213            1383 :     cur.len = 0;
 7188 bruce                     214 GIC       12447 :     for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
 7188 bruce                     215 ECB             :     {
 7188 bruce                     216 GIC       11064 :         if (words[i] == 0)
 7188 bruce                     217 ECB             :         {
 7229 bruce                     218 CBC        5182 :             if (cur.base == -1)
 7229 bruce                     219 GIC        1301 :                 cur.base = i, cur.len = 1;
 7229 bruce                     220 ECB             :             else
 7229 bruce                     221 GIC        3881 :                 cur.len++;
                                222                 :         }
                                223                 :         else
 7188 bruce                     224 ECB             :         {
 7188 bruce                     225 GIC        5882 :             if (cur.base != -1)
 7188 bruce                     226 ECB             :             {
 7229 bruce                     227 CBC        1219 :                 if (best.base == -1 || cur.len > best.len)
                                228            1219 :                     best = cur;
 7229 bruce                     229 GIC        1219 :                 cur.base = -1;
                                230                 :             }
                                231                 :         }
 7229 bruce                     232 ECB             :     }
 7188 bruce                     233 GIC        1383 :     if (cur.base != -1)
 7188 bruce                     234 ECB             :     {
 7229 bruce                     235 CBC          82 :         if (best.base == -1 || cur.len > best.len)
 7229 bruce                     236 GIC          79 :             best = cur;
 7229 bruce                     237 ECB             :     }
 7229 bruce                     238 CBC        1383 :     if (best.base != -1 && best.len < 2)
 7229 bruce                     239 GIC           9 :         best.base = -1;
                                240                 : 
                                241                 :     /*
                                242                 :      * Format the result.
 7229 bruce                     243 ECB             :      */
 7229 bruce                     244 CBC        1383 :     tp = tmp;
 7188 bruce                     245 GIC       12173 :     for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
                                246                 :     {
 7229 bruce                     247 ECB             :         /* Are we inside the best run of 0x00's? */
 7229 bruce                     248 CBC       10927 :         if (best.base != -1 && i >= best.base &&
 7188 bruce                     249 GIC        8718 :             i < (best.base + best.len))
 7188 bruce                     250 ECB             :         {
 7229 bruce                     251 CBC        5170 :             if (i == best.base)
                                252            1289 :                 *tp++ = ':';
 7229 bruce                     253 GIC        5170 :             continue;
                                254                 :         }
 7229 bruce                     255 ECB             :         /* Are we following an initial run of 0x00s or any real hex? */
 7229 bruce                     256 CBC        5757 :         if (i != 0)
 7229 bruce                     257 GIC        4544 :             *tp++ = ':';
 7229 bruce                     258 ECB             :         /* Is this address an encapsulated IPv4? */
 7229 bruce                     259 CBC        5757 :         if (i == 6 && best.base == 0 && (best.len == 6 ||
 2118 tgl                       260              73 :                                          (best.len == 7 && words[7] != 0x0001) ||
 2118 tgl                       261 GIC          73 :                                          (best.len == 5 && words[5] == 0xffff)))
                                262                 :         {
                                263                 :             int         n;
 7188 bruce                     264 ECB             : 
 7188 bruce                     265 CBC         137 :             n = decoct(src + 12, 4, tp, sizeof tmp - (tp - tmp));
 7188 bruce                     266 GIC         137 :             if (n == 0)
 7188 bruce                     267 EUB             :             {
 7229 bruce                     268 UBC           0 :                 errno = EMSGSIZE;
 7229 bruce                     269 UIC           0 :                 return (NULL);
 7229 bruce                     270 ECB             :             }
 7229 bruce                     271 CBC         137 :             tp += strlen(tp);
 7229 bruce                     272 GIC         137 :             break;
 7229 bruce                     273 ECB             :         }
 7229 bruce                     274 GIC        5620 :         tp += SPRINTF((tp, "%x", words[i]));
                                275                 :     }
                                276                 : 
 7229 bruce                     277 ECB             :     /* Was it a trailing run of 0x00's? */
 7188 bruce                     278 GIC        1383 :     if (best.base != -1 && (best.base + best.len) ==
 7188 bruce                     279 ECB             :         (NS_IN6ADDRSZ / NS_INT16SZ))
 7229 bruce                     280 CBC          73 :         *tp++ = ':';
 7229 bruce                     281 GIC        1383 :     *tp = '\0';
 7229 bruce                     282 ECB             : 
 7229 bruce                     283 CBC        1383 :     if (bits != -1 && bits != 128)
 7229 bruce                     284 GIC         403 :         tp += SPRINTF((tp, "/%u", bits));
                                285                 : 
                                286                 :     /*
                                287                 :      * Check for overflow, copy, and we're done.
 7229 bruce                     288 ECB             :      */
 7188 bruce                     289 GIC        1383 :     if ((size_t) (tp - tmp) > size)
 7188 bruce                     290 EUB             :     {
 7229 bruce                     291 UBC           0 :         errno = EMSGSIZE;
 7229 bruce                     292 UIC           0 :         return (NULL);
 7229 bruce                     293 ECB             :     }
 7229 bruce                     294 CBC        1383 :     strcpy(dst, tmp);
 7229 bruce                     295 GIC        1383 :     return (dst);
                                296                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a