PostgreSQL Source Code  git master
inet_net_ntop.c File Reference
#include "postgres.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "utils/inet.h"
Include dependency graph for inet_net_ntop.c:

Go to the source code of this file.

Macros

#define NS_IN6ADDRSZ   16
 
#define NS_INT16SZ   2
 
#define SPRINTF(x)   ((size_t)sprintf x)
 

Functions

static char * inet_net_ntop_ipv4 (const u_char *src, int bits, char *dst, size_t size)
 
static char * inet_net_ntop_ipv6 (const u_char *src, int bits, char *dst, size_t size)
 
char * pg_inet_net_ntop (int af, const void *src, int bits, char *dst, size_t size)
 
static int decoct (const u_char *src, int bytes, char *dst, size_t size)
 

Macro Definition Documentation

◆ NS_IN6ADDRSZ

#define NS_IN6ADDRSZ   16

Definition at line 47 of file inet_net_ntop.c.

◆ NS_INT16SZ

#define NS_INT16SZ   2

Definition at line 48 of file inet_net_ntop.c.

◆ SPRINTF

#define SPRINTF (   x)    ((size_t)sprintf x)

Definition at line 53 of file inet_net_ntop.c.

Function Documentation

◆ decoct()

static int decoct ( const u_char *  src,
int  bytes,
char *  dst,
size_t  size 
)
static

Definition at line 155 of file inet_net_ntop.c.

156 {
157  char *odst = dst;
158  char *t;
159  int b;
160 
161  for (b = 1; b <= bytes; b++)
162  {
163  if (size <= sizeof "255.")
164  return (0);
165  t = dst;
166  dst += SPRINTF((dst, "%u", *src++));
167  if (b != bytes)
168  {
169  *dst++ = '.';
170  *dst = '\0';
171  }
172  size -= (size_t) (dst - t);
173  }
174  return (dst - odst);
175 }
#define SPRINTF(x)
Definition: inet_net_ntop.c:53
int b
Definition: isn.c:69
static pg_noinline void Size size
Definition: slab.c:607

References b, size, and SPRINTF.

Referenced by inet_net_ntop_ipv6().

◆ inet_net_ntop_ipv4()

static char * inet_net_ntop_ipv4 ( const u_char *  src,
int  bits,
char *  dst,
size_t  size 
)
static

Definition at line 114 of file inet_net_ntop.c.

115 {
116  char *odst = dst;
117  char *t;
118  int len = 4;
119  int b;
120 
121  if (bits < 0 || bits > 32)
122  {
123  errno = EINVAL;
124  return (NULL);
125  }
126 
127  /* Always format all four octets, regardless of mask length. */
128  for (b = len; b > 0; b--)
129  {
130  if (size <= sizeof ".255")
131  goto emsgsize;
132  t = dst;
133  if (dst != odst)
134  *dst++ = '.';
135  dst += SPRINTF((dst, "%u", *src++));
136  size -= (size_t) (dst - t);
137  }
138 
139  /* don't print masklen if 32 bits */
140  if (bits != 32)
141  {
142  if (size <= sizeof "/32")
143  goto emsgsize;
144  dst += SPRINTF((dst, "/%u", bits));
145  }
146 
147  return (odst);
148 
149 emsgsize:
150  errno = EMSGSIZE;
151  return (NULL);
152 }
const void size_t len
#define EMSGSIZE
Definition: win32_port.h:376

References b, EMSGSIZE, len, size, and SPRINTF.

Referenced by pg_inet_net_ntop().

◆ inet_net_ntop_ipv6()

static char * inet_net_ntop_ipv6 ( const u_char *  src,
int  bits,
char *  dst,
size_t  size 
)
static

Definition at line 178 of file inet_net_ntop.c.

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;
196 
197  if ((bits < -1) || (bits > 128))
198  {
199  errno = EINVAL;
200  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.
206  */
207  memset(words, '\0', sizeof words);
208  for (i = 0; i < NS_IN6ADDRSZ; i++)
209  words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
210  best.base = -1;
211  cur.base = -1;
212  best.len = 0;
213  cur.len = 0;
214  for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
215  {
216  if (words[i] == 0)
217  {
218  if (cur.base == -1)
219  cur.base = i, cur.len = 1;
220  else
221  cur.len++;
222  }
223  else
224  {
225  if (cur.base != -1)
226  {
227  if (best.base == -1 || cur.len > best.len)
228  best = cur;
229  cur.base = -1;
230  }
231  }
232  }
233  if (cur.base != -1)
234  {
235  if (best.base == -1 || cur.len > best.len)
236  best = cur;
237  }
238  if (best.base != -1 && best.len < 2)
239  best.base = -1;
240 
241  /*
242  * Format the result.
243  */
244  tp = tmp;
245  for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
246  {
247  /* Are we inside the best run of 0x00's? */
248  if (best.base != -1 && i >= best.base &&
249  i < (best.base + best.len))
250  {
251  if (i == best.base)
252  *tp++ = ':';
253  continue;
254  }
255  /* Are we following an initial run of 0x00s or any real hex? */
256  if (i != 0)
257  *tp++ = ':';
258  /* Is this address an encapsulated IPv4? */
259  if (i == 6 && best.base == 0 && (best.len == 6 ||
260  (best.len == 7 && words[7] != 0x0001) ||
261  (best.len == 5 && words[5] == 0xffff)))
262  {
263  int n;
264 
265  n = decoct(src + 12, 4, tp, sizeof tmp - (tp - tmp));
266  if (n == 0)
267  {
268  errno = EMSGSIZE;
269  return (NULL);
270  }
271  tp += strlen(tp);
272  break;
273  }
274  tp += SPRINTF((tp, "%x", words[i]));
275  }
276 
277  /* Was it a trailing run of 0x00's? */
278  if (best.base != -1 && (best.base + best.len) ==
280  *tp++ = ':';
281  *tp = '\0';
282 
283  if (bits != -1 && bits != 128)
284  tp += SPRINTF((tp, "/%u", bits));
285 
286  /*
287  * Check for overflow, copy, and we're done.
288  */
289  if ((size_t) (tp - tmp) > size)
290  {
291  errno = EMSGSIZE;
292  return (NULL);
293  }
294  strcpy(dst, tmp);
295  return (dst);
296 }
struct cursor * cur
Definition: ecpg.c:29
#define NS_IN6ADDRSZ
Definition: inet_net_ntop.c:47
#define NS_INT16SZ
Definition: inet_net_ntop.c:48
static int decoct(const u_char *src, int bytes, char *dst, size_t size)
int i
Definition: isn.c:72

References cur, decoct(), EMSGSIZE, i, len, NS_IN6ADDRSZ, NS_INT16SZ, size, and SPRINTF.

Referenced by pg_inet_net_ntop().

◆ pg_inet_net_ntop()

char* pg_inet_net_ntop ( int  af,
const void *  src,
int  bits,
char *  dst,
size_t  size 
)

Definition at line 77 of file inet_net_ntop.c.

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.
84  */
85  switch (af)
86  {
87  case PGSQL_AF_INET:
88  return (inet_net_ntop_ipv4(src, bits, dst, size));
89  case PGSQL_AF_INET6:
90 #if AF_INET6 != PGSQL_AF_INET6
91  case AF_INET6:
92 #endif
93  return (inet_net_ntop_ipv6(src, bits, dst, size));
94  default:
95  errno = EAFNOSUPPORT;
96  return (NULL);
97  }
98 }
static char * inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
static char * inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
#define PGSQL_AF_INET
Definition: inet.h:39
#define PGSQL_AF_INET6
Definition: inet.h:40
#define EAFNOSUPPORT
Definition: win32_port.h:378

References EAFNOSUPPORT, inet_net_ntop_ipv4(), inet_net_ntop_ipv6(), PGSQL_AF_INET, PGSQL_AF_INET6, and size.

Referenced by getHostaddr(), inet_abbrev(), network_host(), network_out(), network_show(), and pq_verify_peer_name_matches_certificate_ip().