1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
| | // SPDX-License-Identifier: GPL-2.0-or-later
/* PASST - Plug A Simple Socket Transport
* for qemu/UNIX domain socket mode
*
* PASTA - Pack A Subtle Tap Abstraction
* for network namespace/tap device mode
*
* parse.c - Composable parsing helpers
*
* Copyright Red Hat
* Author: David Gibson <david@gibson.dropbear.id.au>
*/
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <arpa/inet.h>
#include <net/if.h>
#include "common.h"
#include "parse.h"
#include "inany.h"
/**
* DOC: Theory of Operation
*
* These are a number of primitives which can be combined into parsers for
* moderately complex input. For simpler composability, they have common
* conventions.
*
* - Functions return a bool indicating whether they successfully parsed.
* - Any specific output from the parse is as output parameters
* - First argument is always @cursor, a const char **.
* - On entry, *@cursor has the point to start parsing.
* - On successful exit, *@cursor is updated to the next character after the
parsed portion of the input
* - On failure, *@cursor and any output arguments are not modified
*
* For brevity the common parameters and return information are omitted from the
* individual function documentation comments.
*/
/**
* parse_literal() - Parse a specified literal string
* @lit: Keyword to accept
*/
bool parse_literal(const char **cursor, const char *lit)
{
size_t len = strlen(lit);
if (strlen(*cursor) < len || memcmp(*cursor, lit, len))
return false;
*cursor += len;
return true;
}
/**
* parse_eoi() - Parse end of input
* @cursor: Current parse pointer
*
* Return: true if @p is at End of Input (\0), false otherwise
*/
bool parse_eoi(const char *cursor)
{
return !(*cursor);
}
/*
* parse_unsigned() - Parse an unsigned integer
* @base: Numeric base for string as strtoul(3)
* @valp: On success, updated with parsed value
*/
bool parse_unsigned(const char **cursor, int base, unsigned long *valp)
{
const char *p = *cursor;
unsigned long val;
errno = 0;
val = strtoul(p, (char **)&p, base);
if (errno || p == *cursor)
return false;
*valp = val;
*cursor = p;
return true;
}
/**
* parse_port_range() - Parse a range of port numbers '<first>[-<last>]'
* @range: Update with the parsed values on success
*/
bool parse_port_range(const char **cursor, struct port_range *range)
{
unsigned long first, last;
const char *p = *cursor;
if (!parse_unsigned(&p, 10, &first))
return false;
last = first;
if (parse_literal(&p, "-"))
if (!parse_unsigned(&p, 10, &last))
return false;
if ((last < first) || (last >= NUM_PORTS))
return false;
range->first = first;
range->last = last;
*cursor = p;
return true;
}
/**
* parse_ipv4() - Parse an IPv4 address from a string
* @abuf: On success, updated with parsed address
*/
bool parse_ipv4(const char **cursor, struct in_addr *abuf)
{
/* Brackets are not typical on IPv4, but allow for consistency */
const char *p = *cursor;
bool bracket = parse_literal(&p, "[");
char buf[INET_ADDRSTRLEN];
struct in_addr addr;
size_t len;
if (bracket)
len = strcspn(p, "]");
else
len = strspn(p, "0123456789.");
if (len >= sizeof(buf))
return false;
memcpy(buf, p, len);
buf[len] = '\0';
p += len;
if (!inet_pton(AF_INET, buf, &addr))
return false;
if (bracket && !parse_literal(&p, "]"))
return false;
*cursor = p;
*abuf = addr;
return true;
}
/**
* parse_ipv6() - Parse an IPv6 address from a string
* @abuf: On success, updated with parsed address
*/
static bool parse_ipv6(const char **cursor, struct in6_addr *abuf)
{
const char *p = *cursor;
bool bracket = parse_literal(&p, "[");
char buf[INET6_ADDRSTRLEN];
struct in6_addr addr;
size_t len;
if (bracket)
len = strcspn(p, "]");
else
len = strspn(p, "0123456789aAbBcCdDeEfF:.");
if (len >= sizeof(buf))
return false;
memcpy(buf, p, len);
buf[len] = '\0';
p += len;
if (!inet_pton(AF_INET6, buf, &addr))
return false;
if (bracket && !parse_literal(&p, "]"))
return false;
*cursor = p;
*abuf = addr;
return true;
}
/**
* parse_inany_() - Parse an IPv4 or IPv6 address from a string
* @addr: On success, updated with parsed address
* @parseaf: On success, updated with the format of the parsed address
*
* @parseaf is updated to reflect the string format, not the final address
* family. So "::ffff:192.0.1.1", will set @parseaf to AF_INET6, despite being
* a IPv4-mapped address.
*/
bool parse_inany_(const char **cursor, union inany_addr *addr,
sa_family_t *parseaf)
{
struct in_addr a4;
if (parse_ipv6(cursor, &addr->a6)) {
if (parseaf)
*parseaf = AF_INET6;
return true;
}
if (parse_ipv4(cursor, &a4)) {
*addr = inany_from_v4(a4);
if (parseaf)
*parseaf = AF_INET;
return true;
}
return false;
}
/**
* parse_ifspec() - Parse a interface name specifier (starting with %)
* @ifname: On success updated with parsed name (must have IFNAMSIZ space)
*
* This will accept a missing specifier (empty string), setting ifname to ""
*/
bool parse_ifspec(const char **cursor, char *ifname)
{
const char *p = *cursor;
size_t len;
if (!parse_literal(&p, "%")) {
/* No interface specifier */
ifname[0] = '\0';
return true;
}
/* ifnames can have anything that's not '/' or whitespace */
len = strcspn(p, "/ \f\n\r\t\v");
if (!len || len >= IFNAMSIZ)
return false;
memcpy(ifname, p, len);
ifname[len] = '\0';
*cursor = p + len;
return true;
}
|