PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
inet_net_ntop.c File Reference
#include "postgres.h"
#include <sys/types.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 * 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

#define NS_IN6ADDRSZ   16

Definition at line 48 of file inet_net_ntop.c.

Referenced by inet_net_ntop_ipv6().

#define NS_INT16SZ   2

Definition at line 49 of file inet_net_ntop.c.

Referenced by inet_net_ntop_ipv6().

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

Definition at line 54 of file inet_net_ntop.c.

Referenced by decoct(), inet_net_ntop_ipv4(), and inet_net_ntop_ipv6().

Function Documentation

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

Definition at line 157 of file inet_net_ntop.c.

References SPRINTF.

Referenced by inet_net_ntop_ipv6().

158 {
159  char *odst = dst;
160  char *t;
161  int b;
162 
163  for (b = 1; b <= bytes; b++)
164  {
165  if (size <= sizeof "255.")
166  return (0);
167  t = dst;
168  dst += SPRINTF((dst, "%u", *src++));
169  if (b != bytes)
170  {
171  *dst++ = '.';
172  *dst = '\0';
173  }
174  size -= (size_t) (dst - t);
175  }
176  return (dst - odst);
177 }
#define SPRINTF(x)
Definition: inet_net_ntop.c:54
char* inet_net_ntop ( int  af,
const void *  src,
int  bits,
char *  dst,
size_t  size 
)

Definition at line 78 of file inet_net_ntop.c.

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

Referenced by connectFailureMessage(), getnameinfo(), inet_abbrev(), network_host(), network_out(), and network_show().

79 {
80  /*
81  * We need to cover both the address family constants used by the PG inet
82  * type (PGSQL_AF_INET and PGSQL_AF_INET6) and those used by the system
83  * libraries (AF_INET and AF_INET6). We can safely assume PGSQL_AF_INET
84  * == AF_INET, but the INET6 constants are very likely to be different. If
85  * AF_INET6 isn't defined, silently ignore it.
86  */
87  switch (af)
88  {
89  case PGSQL_AF_INET:
90  return (inet_net_ntop_ipv4(src, bits, dst, size));
91  case PGSQL_AF_INET6:
92 #if defined(AF_INET6) && AF_INET6 != PGSQL_AF_INET6
93  case AF_INET6:
94 #endif
95  return (inet_net_ntop_ipv6(src, bits, dst, size));
96  default:
97  errno = EAFNOSUPPORT;
98  return (NULL);
99  }
100 }
#define PGSQL_AF_INET
Definition: inet.h:39
#define PGSQL_AF_INET6
Definition: inet.h:40
static char * inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
#define EAFNOSUPPORT
Definition: win32.h:299
#define NULL
Definition: c.h:226
static char * inet_net_ntop_ipv4(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 
)
static

Definition at line 116 of file inet_net_ntop.c.

References EMSGSIZE, NULL, and SPRINTF.

Referenced by inet_net_ntop().

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

Definition at line 180 of file inet_net_ntop.c.

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

Referenced by inet_net_ntop().

181 {
182  /*
183  * Note that int32_t and int16_t need only be "at least" large enough to
184  * contain a value of the specified size. On some systems, like Crays,
185  * there is no such thing as an integer variable with 16 bits. Keep this
186  * in mind if you think this function should have been coded to use
187  * pointer overlays. All the world's not a VAX.
188  */
189  char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
190  char *tp;
191  struct
192  {
193  int base,
194  len;
195  } best, cur;
196  u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
197  int i;
198 
199  if ((bits < -1) || (bits > 128))
200  {
201  errno = EINVAL;
202  return (NULL);
203  }
204 
205  /*
206  * Preprocess: Copy the input (bytewise) array into a wordwise array. Find
207  * the longest run of 0x00's in src[] for :: shorthanding.
208  */
209  memset(words, '\0', sizeof words);
210  for (i = 0; i < NS_IN6ADDRSZ; i++)
211  words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
212  best.base = -1;
213  cur.base = -1;
214  best.len = 0;
215  cur.len = 0;
216  for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
217  {
218  if (words[i] == 0)
219  {
220  if (cur.base == -1)
221  cur.base = i, cur.len = 1;
222  else
223  cur.len++;
224  }
225  else
226  {
227  if (cur.base != -1)
228  {
229  if (best.base == -1 || cur.len > best.len)
230  best = cur;
231  cur.base = -1;
232  }
233  }
234  }
235  if (cur.base != -1)
236  {
237  if (best.base == -1 || cur.len > best.len)
238  best = cur;
239  }
240  if (best.base != -1 && best.len < 2)
241  best.base = -1;
242 
243  /*
244  * Format the result.
245  */
246  tp = tmp;
247  for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
248  {
249  /* Are we inside the best run of 0x00's? */
250  if (best.base != -1 && i >= best.base &&
251  i < (best.base + best.len))
252  {
253  if (i == best.base)
254  *tp++ = ':';
255  continue;
256  }
257  /* Are we following an initial run of 0x00s or any real hex? */
258  if (i != 0)
259  *tp++ = ':';
260  /* Is this address an encapsulated IPv4? */
261  if (i == 6 && best.base == 0 && (best.len == 6 ||
262  (best.len == 7 && words[7] != 0x0001) ||
263  (best.len == 5 && words[5] == 0xffff)))
264  {
265  int n;
266 
267  n = decoct(src + 12, 4, tp, sizeof tmp - (tp - tmp));
268  if (n == 0)
269  {
270  errno = EMSGSIZE;
271  return (NULL);
272  }
273  tp += strlen(tp);
274  break;
275  }
276  tp += SPRINTF((tp, "%x", words[i]));
277  }
278 
279  /* Was it a trailing run of 0x00's? */
280  if (best.base != -1 && (best.base + best.len) ==
281  (NS_IN6ADDRSZ / NS_INT16SZ))
282  *tp++ = ':';
283  *tp = '\0';
284 
285  if (bits != -1 && bits != 128)
286  tp += SPRINTF((tp, "/%u", bits));
287 
288  /*
289  * Check for overflow, copy, and we're done.
290  */
291  if ((size_t) (tp - tmp) > size)
292  {
293  errno = EMSGSIZE;
294  return (NULL);
295  }
296  strcpy(dst, tmp);
297  return (dst);
298 }
#define NS_INT16SZ
Definition: inet_net_ntop.c:49
#define SPRINTF(x)
Definition: inet_net_ntop.c:54
static int decoct(const u_char *src, int bytes, char *dst, size_t size)
struct cursor * cur
Definition: ecpg.c:28
#define EMSGSIZE
Definition: win32.h:297
#define NS_IN6ADDRSZ
Definition: inet_net_ntop.c:48
#define NULL
Definition: c.h:226
int i