Add Paul Vixie's implementation of inet_ntop and inet_pton for
[rsync/rsync.git] / lib / inet_pton.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 #if defined(LIBC_SCCS) && !defined(lint)
19 static char rcsid[] =
20         "$Id$";
21 #endif /* LIBC_SCCS and not lint */
22
23 #include <config.h>
24
25 #include <errno.h>
26 #include <string.h>
27
28 #define NS_INT16SZ       2
29 #define NS_INADDRSZ      4
30 #define NS_IN6ADDRSZ    16
31
32 /*
33  * WARNING: Don't even consider trying to compile this on a system where
34  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
35  */
36
37 static int inet_pton4(const char *src, unsigned char *dst);
38 static int inet_pton6(const char *src, unsigned char *dst);
39
40 /* int
41  * isc_net_pton(af, src, dst)
42  *      convert from presentation format (which usually means ASCII printable)
43  *      to network format (which is usually some kind of binary format).
44  * return:
45  *      1 if the address was valid for the specified address family
46  *      0 if the address wasn't valid (`dst' is untouched in this case)
47  *      -1 if some other error occurred (`dst' is untouched in this case, too)
48  * author:
49  *      Paul Vixie, 1996.
50  */
51 int
52 isc_net_pton(af, src, dst)
53         int af;
54         const char *src;
55         void *dst;
56 {
57         switch (af) {
58         case AF_INET:
59                 return (inet_pton4(src, dst));
60         case AF_INET6:
61                 return (inet_pton6(src, dst));
62         default:
63                 errno = EAFNOSUPPORT;
64                 return (-1);
65         }
66         /* NOTREACHED */
67 }
68
69 /* int
70  * inet_pton4(src, dst)
71  *      like inet_aton() but without all the hexadecimal and shorthand.
72  * return:
73  *      1 if `src' is a valid dotted quad, else 0.
74  * notice:
75  *      does not touch `dst' unless it's returning 1.
76  * author:
77  *      Paul Vixie, 1996.
78  */
79 static int
80 inet_pton4(src, dst)
81         const char *src;
82         unsigned char *dst;
83 {
84         static const char digits[] = "0123456789";
85         int saw_digit, octets, ch;
86         unsigned char tmp[NS_INADDRSZ], *tp;
87
88         saw_digit = 0;
89         octets = 0;
90         *(tp = tmp) = 0;
91         while ((ch = *src++) != '\0') {
92                 const char *pch;
93
94                 if ((pch = strchr(digits, ch)) != NULL) {
95                         unsigned int new = *tp * 10 + (pch - digits);
96
97                         if (new > 255)
98                                 return (0);
99                         *tp = new;
100                         if (! saw_digit) {
101                                 if (++octets > 4)
102                                         return (0);
103                                 saw_digit = 1;
104                         }
105                 } else if (ch == '.' && saw_digit) {
106                         if (octets == 4)
107                                 return (0);
108                         *++tp = 0;
109                         saw_digit = 0;
110                 } else
111                         return (0);
112         }
113         if (octets < 4)
114                 return (0);
115         memcpy(dst, tmp, NS_INADDRSZ);
116         return (1);
117 }
118
119 /* int
120  * inet_pton6(src, dst)
121  *      convert presentation level address to network order binary form.
122  * return:
123  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
124  * notice:
125  *      (1) does not touch `dst' unless it's returning 1.
126  *      (2) :: in a full address is silently ignored.
127  * credit:
128  *      inspired by Mark Andrews.
129  * author:
130  *      Paul Vixie, 1996.
131  */
132 static int
133 inet_pton6(src, dst)
134         const char *src;
135         unsigned char *dst;
136 {
137         static const char xdigits_l[] = "0123456789abcdef",
138                           xdigits_u[] = "0123456789ABCDEF";
139         unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
140         const char *xdigits, *curtok;
141         int ch, saw_xdigit;
142         unsigned int val;
143
144         memset((tp = tmp), '\0', NS_IN6ADDRSZ);
145         endp = tp + NS_IN6ADDRSZ;
146         colonp = NULL;
147         /* Leading :: requires some special handling. */
148         if (*src == ':')
149                 if (*++src != ':')
150                         return (0);
151         curtok = src;
152         saw_xdigit = 0;
153         val = 0;
154         while ((ch = *src++) != '\0') {
155                 const char *pch;
156
157                 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
158                         pch = strchr((xdigits = xdigits_u), ch);
159                 if (pch != NULL) {
160                         val <<= 4;
161                         val |= (pch - xdigits);
162                         if (val > 0xffff)
163                                 return (0);
164                         saw_xdigit = 1;
165                         continue;
166                 }
167                 if (ch == ':') {
168                         curtok = src;
169                         if (!saw_xdigit) {
170                                 if (colonp)
171                                         return (0);
172                                 colonp = tp;
173                                 continue;
174                         }
175                         if (tp + NS_INT16SZ > endp)
176                                 return (0);
177                         *tp++ = (unsigned char) (val >> 8) & 0xff;
178                         *tp++ = (unsigned char) val & 0xff;
179                         saw_xdigit = 0;
180                         val = 0;
181                         continue;
182                 }
183                 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
184                     inet_pton4(curtok, tp) > 0) {
185                         tp += NS_INADDRSZ;
186                         saw_xdigit = 0;
187                         break;  /* '\0' was seen by inet_pton4(). */
188                 }
189                 return (0);
190         }
191         if (saw_xdigit) {
192                 if (tp + NS_INT16SZ > endp)
193                         return (0);
194                 *tp++ = (unsigned char) (val >> 8) & 0xff;
195                 *tp++ = (unsigned char) val & 0xff;
196         }
197         if (colonp != NULL) {
198                 /*
199                  * Since some memmove()'s erroneously fail to handle
200                  * overlapping regions, we'll do the shift by hand.
201                  */
202                 const int n = tp - colonp;
203                 int i;
204
205                 for (i = 1; i <= n; i++) {
206                         endp[- i] = colonp[n - i];
207                         colonp[n - i] = 0;
208                 }
209                 tp = endp;
210         }
211         if (tp != endp)
212                 return (0);
213         memcpy(dst, tmp, NS_IN6ADDRSZ);
214         return (1);
215 }