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/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 47 of file inet_net_ntop.c.

Referenced by inet_net_ntop_ipv6().

#define NS_INT16SZ   2

Definition at line 48 of file inet_net_ntop.c.

Referenced by inet_net_ntop_ipv6().

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

Definition at line 53 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 156 of file inet_net_ntop.c.

References SPRINTF.

Referenced by inet_net_ntop_ipv6().

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

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().

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. If
84  * AF_INET6 isn't defined, silently ignore it.
85  */
86  switch (af)
87  {
88  case PGSQL_AF_INET:
89  return (inet_net_ntop_ipv4(src, bits, dst, size));
90  case PGSQL_AF_INET6:
91 #if defined(AF_INET6) && AF_INET6 != PGSQL_AF_INET6
92  case AF_INET6:
93 #endif
94  return (inet_net_ntop_ipv6(src, bits, dst, size));
95  default:
96  errno = EAFNOSUPPORT;
97  return (NULL);
98  }
99 }
#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:229
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 115 of file inet_net_ntop.c.

References EMSGSIZE, NULL, and SPRINTF.

Referenced by inet_net_ntop().

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

Definition at line 179 of file inet_net_ntop.c.

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

Referenced by inet_net_ntop().

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