cmdline: new build system
[libcmdline.git] / src / lib / cmdline_parse_ipaddr.c
1 /*-
2  * Copyright (c) <2010>, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the
15  *   distribution.
16  *
17  * - Neither the name of Intel Corporation nor the names of its
18  *   contributors may be used to endorse or promote products derived
19  *   from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*
36  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37  * All rights reserved.
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions are met:
40  *
41  *     * Redistributions of source code must retain the above copyright
42  *       notice, this list of conditions and the following disclaimer.
43  *     * Redistributions in binary form must reproduce the above copyright
44  *       notice, this list of conditions and the following disclaimer in the
45  *       documentation and/or other materials provided with the distribution.
46  *     * Neither the name of the University of California, Berkeley nor the
47  *       names of its contributors may be used to endorse or promote products
48  *       derived from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  */
61
62 /*
63  * For inet_ntop() functions:
64  *
65  * Copyright (c) 1996 by Internet Software Consortium.
66  *
67  * Permission to use, copy, modify, and distribute this software for any
68  * purpose with or without fee is hereby granted, provided that the above
69  * copyright notice and this permission notice appear in all copies.
70  *
71  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
72  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
73  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
74  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
75  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
76  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
77  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
78  * SOFTWARE.
79  */
80
81
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <inttypes.h>
85 #include <ctype.h>
86 #include <string.h>
87 #include <errno.h>
88 #include <netinet/in.h>
89 #ifndef __linux__
90 #include <net/socket.h>
91 #endif
92
93 #include "cmdline_parse.h"
94 #include "cmdline_parse_ipaddr.h"
95
96 #define INADDRSZ 4
97 #define IN6ADDRSZ 16
98
99 /*
100  * WARNING: Don't even consider trying to compile this on a system where
101  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
102  */
103
104 static int inet_pton4(const char *src, unsigned char *dst);
105 static int inet_pton6(const char *src, unsigned char *dst);
106
107 /* int
108  * inet_pton(af, src, dst)
109  *      convert from presentation format (which usually means ASCII printable)
110  *      to network format (which is usually some kind of binary format).
111  * return:
112  *      1 if the address was valid for the specified address family
113  *      0 if the address wasn't valid (`dst' is untouched in this case)
114  *      -1 if some other error occurred (`dst' is untouched in this case, too)
115  * author:
116  *      Paul Vixie, 1996.
117  */
118 static int
119 my_inet_pton(int af, const char *src, void *dst)
120 {
121         switch (af) {
122                 case AF_INET:
123                         return (inet_pton4(src, dst));
124                 case AF_INET6:
125                         return (inet_pton6(src, dst));
126                 default:
127                         errno = EAFNOSUPPORT;
128                         return (-1);
129         }
130         /* NOTREACHED */
131 }
132
133 /* int
134  * inet_pton4(src, dst)
135  *      like inet_aton() but without all the hexadecimal and shorthand.
136  * return:
137  *      1 if `src' is a valid dotted quad, else 0.
138  * notice:
139  *      does not touch `dst' unless it's returning 1.
140  * author:
141  *      Paul Vixie, 1996.
142  */
143 static int
144 inet_pton4(const char *src, unsigned char *dst)
145 {
146         static const char digits[] = "0123456789";
147         int saw_digit, octets, ch;
148         unsigned char tmp[INADDRSZ], *tp;
149
150         saw_digit = 0;
151         octets = 0;
152         *(tp = tmp) = 0;
153         while ((ch = *src++) != '\0') {
154                 const char *pch;
155
156                 if ((pch = strchr(digits, ch)) != NULL) {
157                         unsigned int new = *tp * 10 + (pch - digits);
158
159                         if (new > 255)
160                                 return (0);
161                         if (! saw_digit) {
162                                 if (++octets > 4)
163                                         return (0);
164                                 saw_digit = 1;
165                         }
166                         *tp = (unsigned char)new;
167                 } else if (ch == '.' && saw_digit) {
168                         if (octets == 4)
169                                 return (0);
170                         *++tp = 0;
171                         saw_digit = 0;
172                 } else
173                         return (0);
174         }
175         if (octets < 4)
176                 return (0);
177
178         memcpy(dst, tmp, INADDRSZ);
179         return (1);
180 }
181
182 /* int
183  * inet_pton6(src, dst)
184  *      convert presentation level address to network order binary form.
185  * return:
186  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
187  * notice:
188  *      (1) does not touch `dst' unless it's returning 1.
189  *      (2) :: in a full address is silently ignored.
190  * credit:
191  *      inspired by Mark Andrews.
192  * author:
193  *      Paul Vixie, 1996.
194  */
195 static int
196 inet_pton6(const char *src, unsigned char *dst)
197 {
198         static const char xdigits_l[] = "0123456789abcdef",
199                 xdigits_u[] = "0123456789ABCDEF";
200         unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
201         const char *xdigits, *curtok;
202         int ch, saw_xdigit, count_xdigit;
203         unsigned int val;
204
205         memset((tp = tmp), '\0', IN6ADDRSZ);
206         endp = tp + IN6ADDRSZ;
207         colonp = NULL;
208         /* Leading :: requires some special handling. */
209         if (*src == ':')
210                 if (*++src != ':')
211                         return (0);
212         curtok = src;
213         saw_xdigit = count_xdigit = 0;
214         val = 0;
215         while ((ch = *src++) != '\0') {
216                 const char *pch;
217
218                 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
219                         pch = strchr((xdigits = xdigits_u), ch);
220                 if (pch != NULL) {
221                         if (count_xdigit >= 4)
222                                 return (0);
223                         val <<= 4;
224                         val |= (pch - xdigits);
225                         if (val > 0xffff)
226                                 return (0);
227                         saw_xdigit = 1;
228                         count_xdigit++;
229                         continue;
230                 }
231                 if (ch == ':') {
232                         curtok = src;
233                         if (!saw_xdigit) {
234                                 if (colonp)
235                                         return (0);
236                                 colonp = tp;
237                                 continue;
238                         } else if (*src == '\0') {
239                                 return (0);
240                         }
241                         if (tp + sizeof(int16_t) > endp)
242                                 return (0);
243                         *tp++ = (unsigned char) ((val >> 8) & 0xff);
244                         *tp++ = (unsigned char) (val & 0xff);
245                         saw_xdigit = 0;
246                         count_xdigit = 0;
247                         val = 0;
248                         continue;
249                 }
250                 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
251                     inet_pton4(curtok, tp) > 0) {
252                         tp += INADDRSZ;
253                         saw_xdigit = 0;
254                         count_xdigit = 0;
255                         break;  /* '\0' was seen by inet_pton4(). */
256                 }
257                 return (0);
258         }
259         if (saw_xdigit) {
260                 if (tp + sizeof(int16_t) > endp)
261                         return (0);
262                 *tp++ = (unsigned char) ((val >> 8) & 0xff);
263                 *tp++ = (unsigned char) (val & 0xff);
264         }
265         if (colonp != NULL) {
266                 /*
267                  * Since some memmove()'s erroneously fail to handle
268                  * overlapping regions, we'll do the shift by hand.
269                  */
270                 const int n = tp - colonp;
271                 int i;
272
273                 for (i = 1; i <= n; i++) {
274                         endp[- i] = colonp[n - i];
275                         colonp[n - i] = 0;
276                 }
277                 tp = endp;
278         }
279         if (tp != endp)
280                 return (0);
281         memcpy(dst, tmp, IN6ADDRSZ);
282         return (1);
283 }
284
285 static int
286 cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
287                      unsigned ressize)
288 {
289         struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
290         unsigned int token_len = 0;
291         char ip_str[INET6_ADDRSTRLEN+4]; /* '+4' is for prefixlen (if any) */
292         cmdline_ipaddr_t ipaddr;
293         char *prefix, *prefix_end;
294         long prefixlen;
295
296         if (res && ressize < sizeof(cmdline_ipaddr_t))
297                 return -1;
298
299         memset(&ipaddr, 0, sizeof(ipaddr));
300
301         /* if token is too big... */
302         token_len = snprintf(ip_str, sizeof(ip_str), "%s", buf);
303         if (token_len >= sizeof(ip_str))
304                 return -1;
305
306         /* convert the network prefix */
307         if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
308                 prefix = strrchr(ip_str, '/');
309                 if (prefix == NULL)
310                         return -1;
311                 *prefix = '\0';
312                 prefix ++;
313                 errno = 0;
314                 prefixlen = strtol(prefix, &prefix_end, 10);
315                 if (errno || (*prefix_end != '\0') )
316                         return -1;
317                 ipaddr.prefixlen = prefixlen;
318         }
319         else {
320                 ipaddr.prefixlen = 0;
321         }
322
323         /* convert the IP addr */
324         if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
325             my_inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1) {
326                 ipaddr.family = AF_INET;
327                 if (res != NULL)
328                         memcpy(res, &ipaddr, sizeof(ipaddr));
329                 return 0;
330         }
331         if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
332             my_inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
333                 ipaddr.family = AF_INET6;
334                 if (res != NULL)
335                         memcpy(res, &ipaddr, sizeof(ipaddr));
336                 return 0;
337         }
338         return -1;
339
340 }
341
342 static int
343 cmdline_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
344                     unsigned int size)
345 {
346         struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
347
348         switch (tk2->ipaddr_data.flags) {
349         case CMDLINE_IPADDR_V4:
350                 snprintf(dstbuf, size, "IPv4");
351                 break;
352         case CMDLINE_IPADDR_V6:
353                 snprintf(dstbuf, size, "IPv6");
354                 break;
355         case CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
356                 snprintf(dstbuf, size, "IPv4/IPv6");
357                 break;
358         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4:
359                 snprintf(dstbuf, size, "IPv4 network");
360                 break;
361         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V6:
362                 snprintf(dstbuf, size, "IPv6 network");
363                 break;
364         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
365                 snprintf(dstbuf, size, "IPv4/IPv6 network");
366                 break;
367         default:
368                 snprintf(dstbuf, size, "IPaddr (bad flags)");
369                 break;
370         }
371         return 0;
372 }
373
374 struct cmdline_token_ops cmdline_token_ipaddr_ops = {
375         .parse = cmdline_parse_ipaddr,
376         .complete_start = NULL,
377         .complete_iterate = NULL,
378         .complete_end = NULL,
379         .help = cmdline_help_ipaddr,
380 };
381