first public release
[dpdk.git] / lib / librte_cmdline / cmdline_parse_ipaddr.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 /*
37  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
38  * All rights reserved.
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  *     * Redistributions of source code must retain the above copyright
43  *       notice, this list of conditions and the following disclaimer.
44  *     * Redistributions in binary form must reproduce the above copyright
45  *       notice, this list of conditions and the following disclaimer in the
46  *       documentation and/or other materials provided with the distribution.
47  *     * Neither the name of the University of California, Berkeley nor the
48  *       names of its contributors may be used to endorse or promote products
49  *       derived from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
52  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
55  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  */
62
63 /*
64  * For inet_ntop() functions:
65  *
66  * Copyright (c) 1996 by Internet Software Consortium.
67  *
68  * Permission to use, copy, modify, and distribute this software for any
69  * purpose with or without fee is hereby granted, provided that the above
70  * copyright notice and this permission notice appear in all copies.
71  *
72  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
73  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
74  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
75  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
76  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
77  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
78  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
79  * SOFTWARE.
80  */
81
82
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <stdarg.h>
86 #include <inttypes.h>
87 #include <ctype.h>
88 #include <string.h>
89 #include <errno.h>
90 #include <netinet/in.h>
91 #ifndef __linux__
92 #include <net/socket.h>
93 #endif
94
95 #include <rte_string_fns.h>
96
97 #include "cmdline_parse.h"
98 #include "cmdline_parse_ipaddr.h"
99
100 struct cmdline_token_ops cmdline_token_ipaddr_ops = {
101         .parse = cmdline_parse_ipaddr,
102         .complete_get_nb = NULL,
103         .complete_get_elt = NULL,
104         .get_help = cmdline_get_help_ipaddr,
105 };
106
107 #define INADDRSZ 4
108 #define IN6ADDRSZ 16
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, *endp, *colonp;
212         const char *xdigits, *curtok;
213         int ch, saw_xdigit, count_xdigit;
214         unsigned int val;
215
216         memset((tp = tmp), '\0', IN6ADDRSZ);
217         endp = tp + IN6ADDRSZ;
218         colonp = NULL;
219         /* Leading :: requires some special handling. */
220         if (*src == ':')
221                 if (*++src != ':')
222                         return (0);
223         curtok = src;
224         saw_xdigit = count_xdigit = 0;
225         val = 0;
226         while ((ch = *src++) != '\0') {
227                 const char *pch;
228
229                 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
230                         pch = strchr((xdigits = xdigits_u), ch);
231                 if (pch != NULL) {
232                         if (count_xdigit >= 4)
233                                 return (0);
234                         val <<= 4;
235                         val |= (pch - xdigits);
236                         if (val > 0xffff)
237                                 return (0);
238                         saw_xdigit = 1;
239                         count_xdigit++;
240                         continue;
241                 }
242                 if (ch == ':') {
243                         curtok = src;
244                         if (!saw_xdigit) {
245                                 if (colonp)
246                                         return (0);
247                                 colonp = tp;
248                                 continue;
249                         } else if (*src == '\0') {
250                                 return (0);
251                         }
252                         if (tp + sizeof(int16_t) > endp)
253                                 return (0);
254                         *tp++ = (unsigned char) ((val >> 8) & 0xff);
255                         *tp++ = (unsigned char) (val & 0xff);
256                         saw_xdigit = 0;
257                         count_xdigit = 0;
258                         val = 0;
259                         continue;
260                 }
261                 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
262                     inet_pton4(curtok, tp) > 0) {
263                         tp += INADDRSZ;
264                         saw_xdigit = 0;
265                         count_xdigit = 0;
266                         break;  /* '\0' was seen by inet_pton4(). */
267                 }
268                 return (0);
269         }
270         if (saw_xdigit) {
271                 if (tp + sizeof(int16_t) > endp)
272                         return (0);
273                 *tp++ = (unsigned char) ((val >> 8) & 0xff);
274                 *tp++ = (unsigned char) (val & 0xff);
275         }
276         if (colonp != NULL) {
277                 /*
278                  * Since some memmove()'s erroneously fail to handle
279                  * overlapping regions, we'll do the shift by hand.
280                  */
281                 const int n = tp - colonp;
282                 int i;
283
284                 for (i = 1; i <= n; i++) {
285                         endp[- i] = colonp[n - i];
286                         colonp[n - i] = 0;
287                 }
288                 tp = endp;
289         }
290         if (tp != endp)
291                 return (0);
292         memcpy(dst, tmp, IN6ADDRSZ);
293         return (1);
294 }
295
296 int
297 cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
298 {
299         struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
300         unsigned int token_len = 0;
301         char ip_str[INET6_ADDRSTRLEN+4+1]; /* '+4' is for prefixlen (if any) */
302         cmdline_ipaddr_t ipaddr;
303         char *prefix, *prefix_end;
304         long prefixlen;
305
306         if (! *buf)
307                 return -1;
308
309         while (!cmdline_isendoftoken(buf[token_len]))
310                 token_len++;
311
312         /* if token is too big... */
313         if (token_len >= INET6_ADDRSTRLEN+4)
314                 return -1;
315
316         rte_snprintf(ip_str, token_len+1, "%s", buf);
317
318         /* convert the network prefix */
319         if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
320                 prefix = strrchr(ip_str, '/');
321                 if (prefix == NULL)
322                         return -1;
323                 *prefix = '\0';
324                 prefix ++;
325                 errno = 0;
326                 prefixlen = strtol(prefix, &prefix_end, 10);
327                 if (errno || (*prefix_end != '\0') )
328                         return -1;
329                 ipaddr.prefixlen = prefixlen;
330         }
331         else {
332                 ipaddr.prefixlen = 0;
333         }
334
335         /* convert the IP addr */
336         if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
337             my_inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1) {
338                 ipaddr.family = AF_INET;
339                 if (res != NULL)
340                         memcpy(res, &ipaddr, sizeof(ipaddr));
341                 return token_len;
342         }
343         if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
344             my_inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
345                 ipaddr.family = AF_INET6;
346                 if (res != NULL)
347                         memcpy(res, &ipaddr, sizeof(ipaddr));
348                 return token_len;
349         }
350         return -1;
351
352 }
353
354 int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
355                             unsigned int size)
356 {
357         struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
358
359         switch (tk2->ipaddr_data.flags) {
360         case CMDLINE_IPADDR_V4:
361                 rte_snprintf(dstbuf, size, "IPv4");
362                 break;
363         case CMDLINE_IPADDR_V6:
364                 rte_snprintf(dstbuf, size, "IPv6");
365                 break;
366         case CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
367                 rte_snprintf(dstbuf, size, "IPv4/IPv6");
368                 break;
369         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4:
370                 rte_snprintf(dstbuf, size, "IPv4 network");
371                 break;
372         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V6:
373                 rte_snprintf(dstbuf, size, "IPv6 network");
374                 break;
375         case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
376                 rte_snprintf(dstbuf, size, "IPv4/IPv6 network");
377                 break;
378         default:
379                 rte_snprintf(dstbuf, size, "IPaddr (bad flags)");
380                 break;
381         }
382         return 0;
383 }