update copyright date to 2013
[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 /*
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 <stdarg.h>
85 #include <inttypes.h>
86 #include <ctype.h>
87 #include <string.h>
88 #include <errno.h>
89 #include <netinet/in.h>
90 #ifndef __linux__
91 #include <net/socket.h>
92 #endif
93
94 #include <rte_string_fns.h>
95
96 #include "cmdline_parse.h"
97 #include "cmdline_parse_ipaddr.h"
98
99 struct cmdline_token_ops cmdline_token_ipaddr_ops = {
100         .parse = cmdline_parse_ipaddr,
101         .complete_get_nb = NULL,
102         .complete_get_elt = NULL,
103         .get_help = cmdline_get_help_ipaddr,
104 };
105
106 #define INADDRSZ 4
107 #define IN6ADDRSZ 16
108 #define PREFIXMAX 128
109 #define V4PREFIXMAX 32
110
111 /*
112  * WARNING: Don't even consider trying to compile this on a system where
113  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
114  */
115
116 static int inet_pton4(const char *src, unsigned char *dst);
117 static int inet_pton6(const char *src, unsigned char *dst);
118
119 /* int
120  * inet_pton(af, src, dst)
121  *      convert from presentation format (which usually means ASCII printable)
122  *      to network format (which is usually some kind of binary format).
123  * return:
124  *      1 if the address was valid for the specified address family
125  *      0 if the address wasn't valid (`dst' is untouched in this case)
126  *      -1 if some other error occurred (`dst' is untouched in this case, too)
127  * author:
128  *      Paul Vixie, 1996.
129  */
130 static int
131 my_inet_pton(int af, const char *src, void *dst)
132 {
133         switch (af) {
134                 case AF_INET:
135                         return (inet_pton4(src, dst));
136                 case AF_INET6:
137                         return (inet_pton6(src, dst));
138                 default:
139                         errno = EAFNOSUPPORT;
140                         return (-1);
141         }
142         /* NOTREACHED */
143 }
144
145 /* int
146  * inet_pton4(src, dst)
147  *      like inet_aton() but without all the hexadecimal and shorthand.
148  * return:
149  *      1 if `src' is a valid dotted quad, else 0.
150  * notice:
151  *      does not touch `dst' unless it's returning 1.
152  * author:
153  *      Paul Vixie, 1996.
154  */
155 static int
156 inet_pton4(const char *src, unsigned char *dst)
157 {
158         static const char digits[] = "0123456789";
159         int saw_digit, octets, ch;
160         unsigned char tmp[INADDRSZ], *tp;
161
162         saw_digit = 0;
163         octets = 0;
164         *(tp = tmp) = 0;
165         while ((ch = *src++) != '\0') {
166                 const char *pch;
167
168                 if ((pch = strchr(digits, ch)) != NULL) {
169                         unsigned int new = *tp * 10 + (pch - digits);
170
171                         if (new > 255)
172                                 return (0);
173                         if (! saw_digit) {
174                                 if (++octets > 4)
175                                         return (0);
176                                 saw_digit = 1;
177                         }
178                         *tp = (unsigned char)new;
179                 } else if (ch == '.' && saw_digit) {
180                         if (octets == 4)
181                                 return (0);
182                         *++tp = 0;
183                         saw_digit = 0;
184                 } else
185                         return (0);
186         }
187         if (octets < 4)
188                 return (0);
189
190         memcpy(dst, tmp, INADDRSZ);
191         return (1);
192 }
193
194 /* int
195  * inet_pton6(src, dst)
196  *      convert presentation level address to network order binary form.
197  * return:
198  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
199  * notice:
200  *      (1) does not touch `dst' unless it's returning 1.
201  *      (2) :: in a full address is silently ignored.
202  * credit:
203  *      inspired by Mark Andrews.
204  * author:
205  *      Paul Vixie, 1996.
206  */
207 static int
208 inet_pton6(const char *src, unsigned char *dst)
209 {
210         static const char xdigits_l[] = "0123456789abcdef",
211                 xdigits_u[] = "0123456789ABCDEF";
212         unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
213         const char *xdigits = 0, *curtok = 0;
214         int ch = 0, saw_xdigit = 0, count_xdigit = 0;
215         unsigned int val = 0;
216         unsigned dbloct_count = 0;
217
218         memset((tp = tmp), '\0', IN6ADDRSZ);
219         endp = tp + IN6ADDRSZ;
220         colonp = NULL;
221         /* Leading :: requires some special handling. */
222         if (*src == ':')
223                 if (*++src != ':')
224                         return (0);
225         curtok = src;
226         saw_xdigit = count_xdigit = 0;
227         val = 0;
228
229         while ((ch = *src++) != '\0') {
230                 const char *pch;
231
232                 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
233                         pch = strchr((xdigits = xdigits_u), ch);
234                 if (pch != NULL) {
235                         if (count_xdigit >= 4)
236                                 return (0);
237                         val <<= 4;
238                         val |= (pch - xdigits);
239                         if (val > 0xffff)
240                                 return (0);
241                         saw_xdigit = 1;
242                         count_xdigit++;
243                         continue;
244                 }
245                 if (ch == ':') {
246                         curtok = src;
247                         if (!saw_xdigit) {
248                                 if (colonp)
249                                         return (0);
250                                 colonp = tp;
251                                 continue;
252                         } else if (*src == '\0') {
253                                 return (0);
254                         }
255                         if (tp + sizeof(int16_t) > endp)
256                                 return (0);
257                         *tp++ = (unsigned char) ((val >> 8) & 0xff);
258                         *tp++ = (unsigned char) (val & 0xff);
259                         saw_xdigit = 0;
260                         count_xdigit = 0;
261                         val = 0;
262                         dbloct_count++;
263                         continue;
264                 }
265                 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
266                     inet_pton4(curtok, tp) > 0) {
267                         tp += INADDRSZ;
268                         saw_xdigit = 0;
269                         dbloct_count += 2;
270                         break;  /* '\0' was seen by inet_pton4(). */
271                 }
272                 return (0);
273         }
274         if (saw_xdigit) {
275                 if (tp + sizeof(int16_t) > endp)
276                         return (0);
277                 *tp++ = (unsigned char) ((val >> 8) & 0xff);
278                 *tp++ = (unsigned char) (val & 0xff);
279                 dbloct_count++;
280         }
281         if (colonp != NULL) {
282                 /* if we already have 8 double octets, having a colon means error */
283                 if (dbloct_count == 8)
284                         return 0;
285
286                 /*
287                  * Since some memmove()'s erroneously fail to handle
288                  * overlapping regions, we'll do the shift by hand.
289                  */
290                 const int n = tp - colonp;
291                 int i;
292
293                 for (i = 1; i <= n; i++) {
294                         endp[- i] = colonp[n - i];
295                         colonp[n - i] = 0;
296                 }
297                 tp = endp;
298         }
299         if (tp != endp)
300                 return (0);
301         memcpy(dst, tmp, IN6ADDRSZ);
302         return (1);
303 }
304
305 int
306 cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
307 {
308         struct cmdline_token_ipaddr *tk2;
309         unsigned int token_len = 0;
310         char ip_str[INET6_ADDRSTRLEN+4+1]; /* '+4' is for prefixlen (if any) */
311         cmdline_ipaddr_t ipaddr;
312         char *prefix, *prefix_end;
313         long prefixlen = 0;
314
315         if (!buf || !tk || ! *buf)
316                 return -1;
317
318         tk2 = (struct cmdline_token_ipaddr *)tk;
319
320         while (!cmdline_isendoftoken(buf[token_len]))
321                 token_len++;
322
323         /* if token is too big... */
324         if (token_len >= INET6_ADDRSTRLEN+4)
325                 return -1;
326
327         rte_snprintf(ip_str, token_len+1, "%s", buf);
328
329         /* convert the network prefix */
330         if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
331                 prefix = strrchr(ip_str, '/');
332                 if (prefix == NULL)
333                         return -1;
334                 *prefix = '\0';
335                 prefix ++;
336                 errno = 0;
337                 prefixlen = strtol(prefix, &prefix_end, 10);
338                 if (errno || (*prefix_end != '\0')
339                         || prefixlen < 0 || prefixlen > PREFIXMAX)
340                         return -1;
341                 ipaddr.prefixlen = prefixlen;
342         }
343         else {
344                 ipaddr.prefixlen = 0;
345         }
346
347         /* convert the IP addr */
348         if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
349             my_inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1 &&
350                 prefixlen <= V4PREFIXMAX) {
351                 ipaddr.family = AF_INET;
352                 if (res)
353                         memcpy(res, &ipaddr, sizeof(ipaddr));
354                 return token_len;
355         }
356         if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
357             my_inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
358                 ipaddr.family = AF_INET6;
359                 if (res)
360                         memcpy(res, &ipaddr, sizeof(ipaddr));
361                 return token_len;
362         }
363         return -1;
364
365 }
366
367 int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
368                             unsigned int size)
369 {
370         struct cmdline_token_ipaddr *tk2;
371
372         if (!tk || !dstbuf)
373                 return -1;
374
375         tk2 = (struct cmdline_token_ipaddr *)tk;
376
377         switch (tk2->ipaddr_data.flags) {
378         case CMDLINE_IPADDR_V4:
379                 rte_snprintf(dstbuf, size, "IPv4");
380                 break;
381         case CMDLINE_IPADDR_V6:
382                 rte_snprintf(dstbuf, size, "IPv6");
383                 break;
384         case CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
385                 rte_snprintf(dstbuf, size, "IPv4/IPv6");
386                 break;
387         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4:
388                 rte_snprintf(dstbuf, size, "IPv4 network");
389                 break;
390         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V6:
391                 rte_snprintf(dstbuf, size, "IPv6 network");
392                 break;
393         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
394                 rte_snprintf(dstbuf, size, "IPv4/IPv6 network");
395                 break;
396         default:
397                 rte_snprintf(dstbuf, size, "IPaddr (bad flags)");
398                 break;
399         }
400         return 0;
401 }