r23118: Make inet_pton and inet_ntop available through lib/replace.
[sfrench/samba-autobuild/.git] / source4 / lib / replace / inet_ntop.c
1 /*
2  * Copyright (C) 1996-2001  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
9  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11  * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* Adapted for Samba lib/replace by Michael Adam */
19
20 #include "replace.h"
21 #include "system/network.h"
22
23 #define NS_INT16SZ       2
24 #define NS_IN6ADDRSZ    16
25
26 /*
27  * WARNING: Don't even consider trying to compile this on a system where
28  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
29  */
30
31 static const char *inet_ntop4(const unsigned char *src, char *dst,
32                               size_t size);
33
34 #ifdef AF_INET6
35 static const char *inet_ntop6(const unsigned char *src, char *dst,
36                               size_t size);
37 #endif
38
39 /* char *
40  * isc_net_ntop(af, src, dst, size)
41  *      convert a network format address to presentation format.
42  * return:
43  *      pointer to presentation format address (`dst'), or NULL (see errno).
44  * author:
45  *      Paul Vixie, 1996.
46  */
47 const char *
48 rep_inet_ntop(int af, const void *src, char *dst, size_t size)
49 {
50         switch (af) {
51         case AF_INET:
52                 return (inet_ntop4(src, dst, size));
53 #ifdef AF_INET6
54         case AF_INET6:
55                 return (inet_ntop6(src, dst, size));
56 #endif
57         default:
58                 errno = EAFNOSUPPORT;
59                 return (NULL);
60         }
61         /* NOTREACHED */
62 }
63
64 /* const char *
65  * inet_ntop4(src, dst, size)
66  *      format an IPv4 address
67  * return:
68  *      `dst' (as a const)
69  * notes:
70  *      (1) uses no statics
71  *      (2) takes a unsigned char* not an in_addr as input
72  * author:
73  *      Paul Vixie, 1996.
74  */
75 static const char *
76 inet_ntop4(const unsigned char *src, char *dst, size_t size)
77 {
78         static const char *fmt = "%u.%u.%u.%u";
79         char tmp[sizeof "255.255.255.255"];
80         size_t len;
81
82         len = snprintf(tmp, sizeof tmp, fmt, src[0], src[1], src[2], src[3]);
83         if (len >= size) {
84                 errno = ENOSPC;
85                 return (NULL);
86         }
87         memcpy(dst, tmp, len + 1);
88
89         return (dst);
90 }
91
92 /* const char *
93  * isc_inet_ntop6(src, dst, size)
94  *      convert IPv6 binary address into presentation (printable) format
95  * author:
96  *      Paul Vixie, 1996.
97  */
98 #ifdef AF_INET6
99 static const char *
100 inet_ntop6(const unsigned char *src, char *dst, size_t size)
101 {
102         /*
103          * Note that int32_t and int16_t need only be "at least" large enough
104          * to contain a value of the specified size.  On some systems, like
105          * Crays, there is no such thing as an integer variable with 16 bits.
106          * Keep this in mind if you think this function should have been coded
107          * to use pointer overlays.  All the world's not a VAX.
108          */
109         char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
110         struct { int base, len; } best, cur;
111         unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
112         int i, inc;
113
114         /*
115          * Preprocess:
116          *      Copy the input (bytewise) array into a wordwise array.
117          *      Find the longest run of 0x00's in src[] for :: shorthanding.
118          */
119         memset(words, '\0', sizeof words);
120         for (i = 0; i < NS_IN6ADDRSZ; i++)
121                 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
122         best.base = -1;
123         cur.base = -1;
124         for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
125                 if (words[i] == 0) {
126                         if (cur.base == -1)
127                                 cur.base = i, cur.len = 1;
128                         else
129                                 cur.len++;
130                 } else {
131                         if (cur.base != -1) {
132                                 if (best.base == -1 || cur.len > best.len)
133                                         best = cur;
134                                 cur.base = -1;
135                         }
136                 }
137         }
138         if (cur.base != -1) {
139                 if (best.base == -1 || cur.len > best.len)
140                         best = cur;
141         }
142         if (best.base != -1 && best.len < 2)
143                 best.base = -1;
144
145         /*
146          * Format the result.
147          */
148         tp = tmp;
149         for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
150                 /* Are we inside the best run of 0x00's? */
151                 if (best.base != -1 && i >= best.base &&
152                     i < (best.base + best.len)) {
153                         if (i == best.base)
154                                 *tp++ = ':';
155                         continue;
156                 }
157                 /* Are we following an initial run of 0x00s or any real hex? */
158                 if (i != 0)
159                         *tp++ = ':';
160                 /* Is this address an encapsulated IPv4? */
161                 if (i == 6 && best.base == 0 &&
162                     (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
163                         if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
164                                 return (NULL);
165                         tp += strlen(tp);
166                         break;
167                 }
168                 inc = snprintf(tp, 5, "%x", words[i]);
169                 assert(inc < 5);
170                 tp += inc;
171         }
172         /* Was it a trailing run of 0x00's? */
173         if (best.base != -1 && (best.base + best.len) ==
174             (NS_IN6ADDRSZ / NS_INT16SZ))
175                 *tp++ = ':';
176         *tp++ = '\0';
177
178         /*
179          * Check for overflow, copy, and we're done.
180          */
181         if ((size_t)(tp - tmp) > size) {
182                 errno = ENOSPC;
183                 return (NULL);
184         }
185         memcpy(dst, tmp, tp - tmp);
186         return (dst);
187 }
188 #endif /* AF_INET6 */