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