PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
inet_cidr_ntop.c File Reference
#include "postgres.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "utils/builtins.h"
#include "utils/inet.h"
Include dependency graph for inet_cidr_ntop.c:

Go to the source code of this file.

Macros

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

Functions

static char * inet_cidr_ntop_ipv4 (const u_char *src, int bits, char *dst, size_t size)
 
static char * inet_cidr_ntop_ipv6 (const u_char *src, int bits, char *dst, size_t size)
 
char * inet_cidr_ntop (int af, const void *src, int bits, char *dst, size_t size)
 

Macro Definition Documentation

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

Definition at line 37 of file inet_cidr_ntop.c.

Referenced by inet_cidr_ntop_ipv4(), and inet_cidr_ntop_ipv6().

Function Documentation

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

Definition at line 56 of file inet_cidr_ntop.c.

References EAFNOSUPPORT, inet_cidr_ntop_ipv4(), inet_cidr_ntop_ipv6(), NULL, PGSQL_AF_INET, and PGSQL_AF_INET6.

Referenced by cidr_abbrev().

57 {
58  switch (af)
59  {
60  case PGSQL_AF_INET:
61  return (inet_cidr_ntop_ipv4(src, bits, dst, size));
62  case PGSQL_AF_INET6:
63  return (inet_cidr_ntop_ipv6(src, bits, dst, size));
64  default:
65  errno = EAFNOSUPPORT;
66  return (NULL);
67  }
68 }
#define PGSQL_AF_INET
Definition: inet.h:39
static char * inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
#define PGSQL_AF_INET6
Definition: inet.h:40
static char * inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
#define EAFNOSUPPORT
Definition: win32.h:289
#define NULL
Definition: c.h:229
static char * inet_cidr_ntop_ipv4 ( const u_char *  src,
int  bits,
char *  dst,
size_t  size 
)
static

Definition at line 85 of file inet_cidr_ntop.c.

References EMSGSIZE, NULL, and SPRINTF.

Referenced by inet_cidr_ntop().

86 {
87  char *odst = dst;
88  char *t;
89  u_int m;
90  int b;
91 
92  if (bits < 0 || bits > 32)
93  {
94  errno = EINVAL;
95  return (NULL);
96  }
97 
98  if (bits == 0)
99  {
100  if (size < sizeof "0")
101  goto emsgsize;
102  *dst++ = '0';
103  size--;
104  *dst = '\0';
105  }
106 
107  /* Format whole octets. */
108  for (b = bits / 8; b > 0; b--)
109  {
110  if (size <= sizeof "255.")
111  goto emsgsize;
112  t = dst;
113  dst += SPRINTF((dst, "%u", *src++));
114  if (b > 1)
115  {
116  *dst++ = '.';
117  *dst = '\0';
118  }
119  size -= (size_t) (dst - t);
120  }
121 
122  /* Format partial octet. */
123  b = bits % 8;
124  if (b > 0)
125  {
126  if (size <= sizeof ".255")
127  goto emsgsize;
128  t = dst;
129  if (dst != odst)
130  *dst++ = '.';
131  m = ((1 << b) - 1) << (8 - b);
132  dst += SPRINTF((dst, "%u", *src & m));
133  size -= (size_t) (dst - t);
134  }
135 
136  /* Format CIDR /width. */
137  if (size <= sizeof "/32")
138  goto emsgsize;
139  dst += SPRINTF((dst, "/%u", bits));
140  return (odst);
141 
142 emsgsize:
143  errno = EMSGSIZE;
144  return (NULL);
145 }
#define SPRINTF(x)
#define EMSGSIZE
Definition: win32.h:287
#define NULL
Definition: c.h:229
static char * inet_cidr_ntop_ipv6 ( const u_char *  src,
int  bits,
char *  dst,
size_t  size 
)
static

Definition at line 165 of file inet_cidr_ntop.c.

References EMSGSIZE, i, NULL, and SPRINTF.

Referenced by inet_cidr_ntop().

166 {
167  u_int m;
168  int b;
169  int p;
170  int zero_s,
171  zero_l,
172  tmp_zero_s,
173  tmp_zero_l;
174  int i;
175  int is_ipv4 = 0;
176  unsigned char inbuf[16];
177  char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
178  char *cp;
179  int words;
180  u_char *s;
181 
182  if (bits < 0 || bits > 128)
183  {
184  errno = EINVAL;
185  return (NULL);
186  }
187 
188  cp = outbuf;
189 
190  if (bits == 0)
191  {
192  *cp++ = ':';
193  *cp++ = ':';
194  *cp = '\0';
195  }
196  else
197  {
198  /* Copy src to private buffer. Zero host part. */
199  p = (bits + 7) / 8;
200  memcpy(inbuf, src, p);
201  memset(inbuf + p, 0, 16 - p);
202  b = bits % 8;
203  if (b != 0)
204  {
205  m = ~0 << (8 - b);
206  inbuf[p - 1] &= m;
207  }
208 
209  s = inbuf;
210 
211  /* how many words need to be displayed in output */
212  words = (bits + 15) / 16;
213  if (words == 1)
214  words = 2;
215 
216  /* Find the longest substring of zero's */
217  zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
218  for (i = 0; i < (words * 2); i += 2)
219  {
220  if ((s[i] | s[i + 1]) == 0)
221  {
222  if (tmp_zero_l == 0)
223  tmp_zero_s = i / 2;
224  tmp_zero_l++;
225  }
226  else
227  {
228  if (tmp_zero_l && zero_l < tmp_zero_l)
229  {
230  zero_s = tmp_zero_s;
231  zero_l = tmp_zero_l;
232  tmp_zero_l = 0;
233  }
234  }
235  }
236 
237  if (tmp_zero_l && zero_l < tmp_zero_l)
238  {
239  zero_s = tmp_zero_s;
240  zero_l = tmp_zero_l;
241  }
242 
243  if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
244  ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
245  ((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
246  is_ipv4 = 1;
247 
248  /* Format whole words. */
249  for (p = 0; p < words; p++)
250  {
251  if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l)
252  {
253  /* Time to skip some zeros */
254  if (p == zero_s)
255  *cp++ = ':';
256  if (p == words - 1)
257  *cp++ = ':';
258  s++;
259  s++;
260  continue;
261  }
262 
263  if (is_ipv4 && p > 5)
264  {
265  *cp++ = (p == 6) ? ':' : '.';
266  cp += SPRINTF((cp, "%u", *s++));
267  /* we can potentially drop the last octet */
268  if (p != 7 || bits > 120)
269  {
270  *cp++ = '.';
271  cp += SPRINTF((cp, "%u", *s++));
272  }
273  }
274  else
275  {
276  if (cp != outbuf)
277  *cp++ = ':';
278  cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
279  s += 2;
280  }
281  }
282  }
283  /* Format CIDR /width. */
284  (void) SPRINTF((cp, "/%u", bits));
285  if (strlen(outbuf) + 1 > size)
286  goto emsgsize;
287  strcpy(dst, outbuf);
288 
289  return (dst);
290 
291 emsgsize:
292  errno = EMSGSIZE;
293  return (NULL);
294 }
#define SPRINTF(x)
#define EMSGSIZE
Definition: win32.h:287
#define NULL
Definition: c.h:229
int i