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