summaryrefslogtreecommitdiff
path: root/accel-pppd/utils.c
blob: 018b6efa3b81db1ea24a7463890e16007a854717 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#include "triton.h"
#include "utils.h"

#include "memdebug.h"

extern int urandom_fd;

/* Convenient wrapper around inet_ntop() to print IPv6 addresses.
 * It stores a string representation of addr into buf, which must be at
 * least INET6_ADDRSTRLEN bytes long.
 *
 * Returns buf, which is guaranteed to contain a valid string even if an error
 * occured.
 */
char __export *u_ip6str(const struct in6_addr *addr, char *buf)
{
	if (!inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN))
		snprintf(buf, INET6_ADDRSTRLEN, "< ERROR! >");

	return buf;
}

/* Convenient wrapper around inet_ntop() to print IPv4 addresses.
 * It stores a string representation of addr into buf, which must be at
 * least INET_ADDRSTRLEN bytes long.
 *
 * Returns buf, which is guaranteed to contain a valid string even if an error
 * occured.
 */
char __export *u_ip4str(const struct in_addr *addr, char *buf)
{
	if (!inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN))
		snprintf(buf, INET_ADDRSTRLEN, "< ERROR! >");

	return buf;
}

void __export u_inet_ntoa(in_addr_t addr, char *str)
{
	addr = ntohl(addr);
	sprintf(str, "%i.%i.%i.%i", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
}

int __export u_readlong(long int *dst, const char *src,
                        long int min, long int max)
{
        char *src_stop = NULL;
        long int rv;

        if (dst == NULL || src == NULL || src[0] == '\0')
                return -1;

        errno = 0;
        rv = strtol(src, &src_stop, 0);
        if (errno != 0 || *src_stop != '\0' || rv < min || rv > max) {
                return -1;
        } else {
                *dst = rv;
                return 0;
        }
}

/* Parse spaces.
 * Returns the number of leading space characters in str.
 * This is a convenient function around strspn() which preserves the look and
 * feel of other u_parse_*() functions.
 */
size_t __export u_parse_spaces(const char *str)
{
	return strspn(str, " ");
}

/* Parse end of string.
 * Reads a sequence of space characters, followed by the end-of-string
 * mark ('\0').
 * Returns the number of characters parsed on success (that is, the number of
 * space characters plus one for '\0'). Beware that 'str + u_parse_endstr(str)'
 * points to the next byte after the end of the string in this case.
 * Returns 0 if parsing fails (that is, if unexpected characers are found
 * before the end of the string).
 */
size_t __export u_parse_endstr(const char *str)
{
	const char *end;

	end = str + strspn(str, " ");
	if (*end != '\0')
		return 0;

	++end;

	return end - str;
}

/* Parse an 8 bits unsigned integer in base 10.
 * Returns the number of bytes parsed on success.
 * Returns 0 if str doesn't start with a valid number or if this number doesn't
 * fit in 8 bits.
 */
size_t __export u_parse_u8(const char *str, uint8_t *val)
{
	char *endptr;
	unsigned long ul;

	/* strtoul() handles leading signs (+/-) and white spaces. Make sure we
	 * parse raw numbers.
	 */
	if (!isdigit(*str))
		return 0;

	ul = strtoul(str, &endptr, 10);
	if (ul > UINT8_MAX)
		return 0;

	*val = ul;

	return endptr - str;
}

/* Parse a 16 bits unsigned integer in base 10.
 * Returns the number of bytes parsed on success.
 * Returns 0 if str doesn't start with a valid number or if this number doesn't
 * fit in 16 bits.
 */
size_t __export u_parse_u16(const char *str, uint16_t *val)
{
	char *endptr;
	unsigned long ul;

	/* strtoul() handles leading signs (+/-) and white spaces. Make sure we
	 * parse raw numbers.
	 */
	if (!isdigit(*str))
		return 0;

	ul = strtoul(str, &endptr, 10);
	if (ul > UINT16_MAX)
		return 0;

	*val = ul;

	return endptr - str;
}

/* Parse a 32 bits unsigned integer in base 10.
 * Returns the number of bytes parsed on success.
 * Returns 0 if str doesn't start with a valid number or if this number doesn't
 * fit in 32 bits.
 */
size_t __export u_parse_u32(const char *str, uint32_t *val)
{
	char *endptr;
	unsigned long ul;

	/* strtoul() handles leading signs (+/-) and white spaces. Make sure we
	 * parse raw numbers.
	 */
	if (!isdigit(*str))
		return 0;

	errno = 0;
	ul = strtoul(str, &endptr, 10);
	/* On platforms where unsigned longs are 32 bits wide, overflows would
	 * return a valid UINT32_MAX value. So we need to check for ERANGE too.
	 */
	if (errno == ERANGE || ul > UINT32_MAX)
		return 0;

	*val = ul;

	return endptr - str;
}

/* Parse an IPv6 address (for example "2001:db8::1").
 * Returns the number of bytes parsed, or 0 if str doesn't start with an IPv6
 * address.
 */
size_t __export u_parse_ip6addr(const char *str, struct in6_addr *addr)
{
	char buf[INET6_ADDRSTRLEN];
	size_t len;

	len = strspn(str, ":0123456789abcdef");
	if (!len || len >= sizeof(buf))
		return 0;

	memcpy(buf, str, len);
	buf[len] = '\0';

	if (inet_pton(AF_INET6, buf, addr) != 1)
		return 0;

	return len;
}

/* Parse an IPv4 address in dotted-decimal format (for example "198.51.100.1").
 * Other formats (hex "0xc6.0x33.0x64.0x1", octal "0306.063.0144.01", mixed
 * "0xc6.51.0144.1", non dotted-quad "198.51.25601"...) are rejected.
 *
 * Returns the number of bytes parsed, or 0 if str doesn't start with an IPv4
 * address.
 */
size_t __export u_parse_ip4addr(const char *str, struct in_addr *addr)
{
	char buf[INET_ADDRSTRLEN];
	size_t len;

	len = strspn(str, ".0123456789");
	if (!len || len >= sizeof(buf))
		return 0;

	memcpy(buf, str, len);
	buf[len] = '\0';

	if (inet_pton(AF_INET, buf, addr) != 1)
		return 0;

	return len;
}

/* Parse an IPv6 network prefix in CIDR notation (for example "2001:db8::/32").
 * Returns the number of bytes parsed, or 0 if str doesn't start with an IPv6
 * network prefix.
 */
size_t __export u_parse_ip6cidr(const char *str, struct in6_addr *netp, uint8_t *plen)
{
	const char *ptr = str;
	size_t len;

	len = u_parse_ip6addr(ptr, netp);
	if (!len)
		return 0;

	ptr += len;
	if (*ptr != '/')
		return 0;

	len = u_parse_u8(++ptr, plen);
	if (!len)
		return 0;

	if (*plen > 128)
		return 0;

	ptr += len;

	return ptr - str;
}

/* Parse an IPv4 network prefix in CIDR notation (for example "192.0.2.0/24").
 * The IP address must be in dotted-decimal format.
 * Returns the number of bytes parsed, or 0 if str doesn't start with an IPv4
 * network prefix.
 */
size_t __export u_parse_ip4cidr(const char *str, struct in_addr *netp, uint8_t *plen)
{
	const char *ptr = str;
	size_t len;

	len = u_parse_ip4addr(ptr, netp);
	if (!len)
		return 0;

	ptr += len;
	if (*ptr != '/')
		return 0;

	len = u_parse_u8(++ptr, plen);
	if (!len)
		return 0;

	if (*plen > 32)
		return 0;

	ptr += len;

	return ptr - str;
}

/* Parse an IPv4 address range (for example "192.0.2.0-255").
 * The IP address must be in dotted-decimal format. The number following '-'
 * is the upper bound of the address' least significant byte (the lower bound
 * is given by the address itself). The upper bound must be bigger or equal
 * than the lower bound.
 *
 * Returns the number of bytes parsed, or 0 if str doesn't start with an IPv4
 * address range.
 */
size_t __export u_parse_ip4range(const char *str, struct in_addr *base_ip, uint8_t *max)
{
	const char *ptr = str;
	size_t len;

	len = u_parse_ip4addr(ptr, base_ip);
	if (!len)
		return 0;

	ptr += len;
	if (*ptr != '-')
		return 0;

	len = u_parse_u8(++ptr, max);
	if (!len)
		return 0;

	if (*max < (ntohl(base_ip->s_addr) & 0xff))
		return 0;

	ptr += len;

	return ptr - str;
}

int __export u_randbuf(void *buf, size_t buf_len, int *err)
{
	uint8_t *u8buf = buf;
	ssize_t rd_len;

	while (buf_len) {
		rd_len = read(urandom_fd, u8buf, buf_len);
		if (rd_len < 0) {
			if (errno == EINTR)
				rd_len = 0;
			else {
				if (err)
					*err = errno;
				return -1;
			}
		} else if (rd_len == 0) {
			if (err)
				*err = 0;
			return -1;
		}
		u8buf += rd_len;
		buf_len -= rd_len;
	}

	return 0;
}