]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/inet_net_ntop.c
Fix for no platform NAN.
[postgresql] / src / backend / utils / adt / inet_net_ntop.c
1 /*
2  * Copyright (c) 1996 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.4 1999/01/01 04:17:13 momjian Exp $";
20
21 #endif
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include <postgres.h>
34 #include <utils/builtins.h>
35
36 #ifdef SPRINTF_CHAR
37 #define SPRINTF(x) strlen(sprintf/**/x)
38 #else
39 #define SPRINTF(x) ((size_t)sprintf x)
40 #endif
41
42 static char *inet_net_ntop_ipv4(const u_char *src, int bits,
43                                                                 char *dst, size_t size);
44 static char *inet_cidr_ntop_ipv4(const u_char *src, int bits,
45                                                                 char *dst, size_t size);
46
47 /*
48  * char *
49  * inet_cidr_ntop(af, src, bits, dst, size)
50  *      convert network number from network to presentation format.
51  *      generates CIDR style result always.
52  * return:
53  *      pointer to dst, or NULL if an error occurred (check errno).
54  * author:
55  *      Paul Vixie (ISC), July 1996
56  */
57 char *
58 inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
59 {
60         switch (af)
61         {
62                 case AF_INET:
63                         return (inet_cidr_ntop_ipv4(src, bits, dst, size));
64                 default:
65                         errno = EAFNOSUPPORT;
66                         return (NULL);
67         }
68 }
69
70
71 /*
72  * static char *
73  * inet_cidr_ntop_ipv4(src, bits, dst, size)
74  *      convert IPv4 network number from network to presentation format.
75  *      generates CIDR style result always.
76  * return:
77  *      pointer to dst, or NULL if an error occurred (check errno).
78  * note:
79  *      network byte order assumed.  this means 192.5.5.240/28 has
80  *      0x11110000 in its fourth octet.
81  * author:
82  *      Paul Vixie (ISC), July 1996
83  */
84 static char *
85 inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
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         if (bits == 0)
98         {
99                 if (size < sizeof "0")
100                         goto emsgsize;
101                 *dst++ = '0';
102                 *dst = '\0';
103         }
104
105         /* Format whole octets. */
106         for (b = bits / 8; b > 0; b--)
107         {
108                 if (size < sizeof "255.")
109                         goto emsgsize;
110                 t = dst;
111                 dst += SPRINTF((dst, "%u", *src++));
112                 if (b > 1)
113                 {
114                         *dst++ = '.';
115                         *dst = '\0';
116                 }
117                 size -= (size_t) (dst - t);
118         }
119
120         /* Format partial octet. */
121         b = bits % 8;
122         if (b > 0)
123         {
124                 if (size < sizeof ".255")
125                         goto emsgsize;
126                 t = dst;
127                 if (dst != odst)
128                         *dst++ = '.';
129                 m = ((1 << b) - 1) << (8 - b);
130                 dst += SPRINTF((dst, "%u", *src & m));
131                 size -= (size_t) (dst - t);
132         }
133
134         /* Format CIDR /width. */
135         if (size < sizeof "/32")
136                 goto emsgsize;
137         dst += SPRINTF((dst, "/%u", bits));
138         return (odst);
139
140 emsgsize:
141         errno = EMSGSIZE;
142         return (NULL);
143 }
144
145
146 /*
147  * char *
148  * inet_net_ntop(af, src, bits, dst, size)
149  *      convert host/network address from network to presentation format.
150  *      "src"'s size is determined from its "af".
151  * return:
152  *      pointer to dst, or NULL if an error occurred (check errno).
153  * note:
154  *      192.5.5.1/28 has a nonzero host part, which means it isn't a network
155  *      as called for by inet_net_pton() but it can be a host address with
156  *      an included netmask.
157  * author:
158  *      Paul Vixie (ISC), October 1998
159  */
160 char *
161 inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
162 {
163         switch (af)
164         {
165                 case AF_INET:
166                         return (inet_net_ntop_ipv4(src, bits, dst, size));
167                 default:
168                         errno = EAFNOSUPPORT;
169                         return (NULL);
170         }
171 }
172
173 /*
174  * static char *
175  * inet_net_ntop_ipv4(src, bits, dst, size)
176  *      convert IPv4 network address from network to presentation format.
177  *      "src"'s size is determined from its "af".
178  * return:
179  *      pointer to dst, or NULL if an error occurred (check errno).
180  * note:
181  *      network byte order assumed.  this means 192.5.5.240/28 has
182  *      0b11110000 in its fourth octet.
183  * author:
184  *      Paul Vixie (ISC), October 1998
185  */
186 static char *
187 inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
188 {
189         char *odst = dst;
190         char *t;
191         size_t len = 4;
192         int b, tb;
193
194         if (bits < 0 || bits > 32)
195         {
196                 errno = EINVAL;
197                 return (NULL);
198         }
199         if (bits == 0)
200         {
201                 if (size < sizeof "0")
202                         goto emsgsize;
203                 *dst++ = '0';
204                 size--;
205                 *dst = '\0';
206         }
207
208         /* Format whole octets plus nonzero trailing octets. */
209         tb = (bits == 32) ? 31 : bits;
210         for (b = 0; b <= (tb / 8) || (b < len && *src != 0); b++)
211         {
212                 if (size < sizeof "255.")
213                         goto emsgsize;
214                 t = dst;
215                 dst += SPRINTF((dst, "%u", *src++));
216                 if (b + 1 <= (tb / 8) || (b + 1 < len && *src != 0))
217                 {
218                         *dst++ = '.';
219                         *dst = '\0';
220                 }
221                 size -= (size_t)(dst - t);
222         }
223
224         /* don't print masklen if 32 bits */
225         if (bits == 32)
226                 return odst;
227
228         /* Format CIDR /width. */
229         if (size < sizeof "/32")
230                 goto emsgsize;
231         dst += SPRINTF((dst, "/%u", bits));
232
233         return (odst);
234
235  emsgsize:
236         errno = EMSGSIZE;
237         return (NULL);
238 }