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