summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdhcpsvc/modules/util/util.c
blob: 4b50304f427c47aaeefbce1fbc0d9969ea8c82e8 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Internal libdhcpsvc public module utility functions: a collection of
 * general-purpose routines that are used by assorted public modules.
 * Someday we should integrate this into the build process a bit more
 * intelligently.
 */

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/isa_defs.h>
#include <dhcp_svc_public.h>
#include <assert.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include <ctype.h>

#include "util.h"

/*
 * Open a file at path `pathname'; depending on the flags passed in through
 * `dsvc_flags', this file may be optionally created or opened read-only.
 * On success, DSVC_SUCCESS is returned and `fdp' points to the opened file
 * descriptor.  On failure, a DSVC_* error code is returned.
 */
int
open_file(const char *pathname, unsigned int dsvc_flags, int *fdp)
{
	int open_flags;

	/*
	 * Note that we always open with read access, independent of
	 * dsvc_flags, because an update operation (add, delete, modify)
	 * needs to lookup records to detect collisions.
	 */
	open_flags = O_RDONLY;
	if (dsvc_flags & DSVC_WRITE)
		open_flags = O_RDWR;
	if (dsvc_flags & DSVC_CREATE)
		open_flags |= O_CREAT|O_EXCL;

	*fdp = open(pathname, open_flags, 0644);
	if (*fdp == -1)
		return (syserr_to_dsvcerr(errno));

	return (DSVC_SUCCESS);
}

/*
 * Read input a chunk at a time, avoiding as much copying as possible.  To
 * this end, we don't read into a temporary buffer, but rather read
 * directly into dynamically reallocated storage (on the assumption that
 * most of the time we will have to return something).  Returns NULL either
 * on failure or EOF; use feof(3C) on `fp' to determine which condition
 * occurred.
 */
char *
read_entry(FILE *fp)
{
	char		*newline, *new_result, *result = NULL;
	unsigned int	len = 0, size = 0, chunksize = BUFSIZ;

	for (;;) {
		/*
		 * See if we need to grow the buffer; we always try to read
		 * `chunksize' bytes, so we need at least `chunksize' around;
		 * grab a little more just to avoid constant realloc'ing
		 */
		if (len + chunksize > size) {
			size = len + (chunksize * 2);
			new_result = realloc(result, size);
			if (new_result == NULL) {
				free(result);
				return (NULL);
			}
		}

		if (fgets(&new_result[len], chunksize, fp) == NULL) {
			/*
			 * Hit EOF; if we never read any data, then free
			 * `new_result' and return NULL.  If we are
			 * returning data, it's in `new_result', not
			 * `result'.
			 */
			if (result == NULL)
				free(new_result);
			else
				result = new_result;
			break;
		}
		result = new_result;

		/*
		 * If we read a newline, then see if the preceding
		 * character was an escape.  If so, remove the escape and
		 * continue; otherwise we're done.  Note that we need to
		 * do the strrchr() on `&result[len]' so that NUL's that
		 * may be lurking elsewhere on the line don't confuse us.
		 */
		newline = strrchr(&result[len], '\n');
		if (newline != NULL) {
			len = newline - result;
			if (newline == result || newline[-1] != '\\') {
				newline[0] = '\0';
				break;
			}
			newline[-1] = '\0';
			len -= 2;
		} else {
			/*
			 * We either `chunksize' worth of data or we hit a
			 * NUL somewhere in the data stream.  If we hit a
			 * NUL, then we can't "see" beyond the NUL; just
			 * advance to the NUL itself and continue.
			 */
			len += strlen(&result[len]);
		}
	}
	return (result);
}

/*
 * Given a buffer `buf' of words separated by one or more of the characters
 * in `seps', split it into at most `nfields' fields, by changing the
 * separator character following a field to a NUL character.  Set
 * `fields[i]' to point to the beginning of field i in `buf'.  Return the
 * number of fields set in `fields[]'.  This routine is quite similar to
 * bufsplit(3G), but less general, faster, and also handles multiple
 * multiple whitespace separator characters differently.
 */
unsigned int
field_split(char *buf, unsigned int nfields, char *fields[], const char *seps)
{
	unsigned int	i = 0;
	char		*ch;

	for (;;) {
		fields[i] = buf;

		/*
		 * Look for the field separator, byte-at-a-time; ignore
		 * separators that have been escaped.  Believe it or not,
		 * strchr(3C) will match `seps' if `*buf' is the NUL byte
		 * (which indicates we're done).
		 */
		for (;;) {
			ch = strchr(seps, *buf);
			if (ch != NULL && *ch == '\0')
				return (i + 1);
			if (ch != NULL && (buf == fields[i] || buf[-1] != '\\'))
				break;
			buf++;
		}

		/*
		 * If this is the last field, then consider any remaining
		 * text on the line part of the last field.  This is
		 * similar to how `read' in sh(1) works.
		 */
		if (++i == nfields)
			return (i);

		if (*buf == '\0')
			return (i);

		*buf = '\0';

		/*
		 * If separator is whitespace, then skip all consecutive
		 * pieces of whitespace.
		 */
		while (ch != NULL && isspace(*ch)) {
			ch = strchr(seps, buf[1]);
			if (ch != NULL && isspace(*ch))
				buf++;
		}
		buf++;
	}
}

/*
 * Map a standard errno into a corresponding DSVC_* code.  If there
 * is no translation, default to DSVC_INTERNAL.
 */
int
syserr_to_dsvcerr(int error)
{
	switch (error) {

	case EEXIST:
		return (DSVC_TABLE_EXISTS);

	case ENOMEM:
		return (DSVC_NO_MEMORY);

	case ENOSPC:
		return (DSVC_NO_RESOURCES);

	case EROFS:
	case EPERM:
	case EACCES:
		return (DSVC_ACCESS);

	case ENOENT:
		return (DSVC_NO_TABLE);

	default:
		break;
	}

	return (DSVC_INTERNAL);
}

/*
 * Convert an object of `len' bytes pointed to by `srcraw' between
 * network-order and host-order and store in `dstraw'.  The length `len'
 * must be the actual length of the objects pointed to by `srcraw' and
 * `dstraw' (or zero) or the results are undefined.  Note that `srcraw' and
 * `dstraw' may be the same, in which case the object is converted
 * in-place.  This routine will convert from host-order to network-order or
 * network-order to host-order, since the conversion is the same.
 */
void
nhconvert(void *dstraw, const void *srcraw, size_t len)
{
#ifdef	_LITTLE_ENDIAN
	uint8_t	b1, b2;
	uint8_t *dst, *src;
	size_t i;

	/*
	 * If both `srcraw' and `dstraw' are 32-bit aligned and `len' is 4,
	 * then use ntohl() to do the byteswap, since it's hand-tuned.
	 */
	if (IS_P2ALIGNED(dstraw, 4) && IS_P2ALIGNED(srcraw, 4) && len == 4) {
		*(uint32_t *)dstraw = ntohl(*(uint32_t *)srcraw);
		return;
	}

	dst = (uint8_t *)dstraw;
	src = (uint8_t *)srcraw;

	for (i = 0; i < len / 2; i++) {
		b1 = src[i];
		b2 = src[len - i - 1];
		dst[i] = b2;
		dst[len - i - 1] = b1;
	}
#else
	if (srcraw != dstraw)
		(void) memmove(dstraw, srcraw, len);
#endif
}

/*
 * Positioned n-byte read: read `buflen' bytes at offset `off' at open file
 * `fd' into `buffer', or "read" none at all.  Returns -1 if all `buflen'
 * bytes cannot be read; otherwise, returns 0.
 */
int
pnread(int fd, void *buffer, size_t buflen, off_t off)
{
	size_t	nread;
	ssize_t	nbytes;
	char	*buf = buffer;

	for (nread = 0; nread < buflen; nread += nbytes) {
		nbytes = pread(fd, &buf[nread], buflen - nread, off + nread);
		if (nbytes == -1)
			return (-1);
		if (nbytes == 0) {
			errno = EIO;
			return (-1);
		}
	}

	assert(nread == buflen);
	return (0);
}

/*
 * Positioned n-byte write: write `buflen' bytes from `buffer' to offset
 * `off' in open file `fd'.  Tries to write all `buflen' bytes, but does
 * not attempt to "undo" what it has done in the case of failure.  Returns
 * -1 if all `buflen' bytes cannot be written, otherwise returns 0.
 */
int
pnwrite(int fd, const void *buffer, size_t buflen, off_t off)
{
	size_t		nwritten;
	ssize_t		nbytes;
	const char	*buf = buffer;

	for (nwritten = 0; nwritten < buflen; nwritten += nbytes) {
		nbytes = pwrite(fd, &buf[nwritten], buflen - nwritten,
		    off + nwritten);
		if (nbytes == -1)
			return (-1);
		if (nbytes == 0) {
			errno = EIO;
			return (-1);
		}
	}

	assert(nwritten == buflen);
	return (0);
}

/*
 * Copy `nbytes' efficiently from offset `srcoff' in `srcfd' to offset
 * `dstoff' in `dstfd'; returns a DSVC_* return code.  Note that we make
 * `nbytes' a uint64_t (rather than a size_t) so that we can copy 2^64
 * bits even when compiled ILP32.
 */
int
copy_range(int srcfd, off_t srcoff, int dstfd, off_t dstoff, uint64_t nbytes)
{
	const size_t	chunksize = 16 * PAGESIZE;
	size_t		validsize;
	size_t		skip;
	uint64_t	nwritten = 0;
	int		mflags = MAP_PRIVATE;
	char		*buf = NULL;
	int		error;

	/*
	 * Handle trivial copy specially so we don't call munmap() below.
	 */
	if (nbytes == 0)
		return (DSVC_SUCCESS);

	/*
	 * The `off' argument to mmap(2) must be page-aligned, so align it;
	 * compute how many bytes we need to skip over in the mmap()'d
	 * buffer as a result.
	 */
	skip = srcoff % PAGESIZE;
	srcoff -= skip;

	while (nwritten < nbytes) {
		buf = mmap(buf, chunksize, PROT_READ, mflags, srcfd, srcoff);
		if (buf == MAP_FAILED)
			return (DSVC_INTERNAL);
		mflags |= MAP_FIXED;

		validsize = MIN(chunksize, nbytes - nwritten + skip);
		if (pnwrite(dstfd, &buf[skip], validsize - skip, dstoff)
		    == -1) {
			error = errno;
			(void) munmap(buf, chunksize);
			return (syserr_to_dsvcerr(error));
		}

		nwritten += validsize - skip;
		dstoff += validsize - skip;
		srcoff += validsize;
		skip = 0;
	}
	(void) munmap(buf, chunksize);

	return (DSVC_SUCCESS);
}

/*
 * Unescape all instances of `delimiter' in `buffer' and store result in
 * `unescaped', which is `size' bytes.  To guarantee that all data is
 * copied, `unescaped' should be at least as long as `buffer'.
 */
void
unescape(char delimiter, const char *buffer, char *unescaped, size_t size)
{
	int i, j;

	size--;
	for (i = 0, j = 0; buffer[i] != '\0' && j < size; i++, j++) {
		if (buffer[i] == '\\' && buffer[i + 1] == delimiter)
			i++;
		unescaped[j] = buffer[i];
	}
	unescaped[j] = '\0';
}

/*
 * Escape all instances of `delimiter' in `buffer' and store result in
 * `escaped', which is `size' bytes.  To guarantee that all data is
 * copied, `escaped' should be at least twice as long as `buffer'.
 */
void
escape(char delimiter, const char *buffer, char *escaped, size_t size)
{
	int i, j;

	size--;
	for (i = 0, j = 0; buffer[i] != '\0' && j < size; i++, j++) {
		if (buffer[i] == delimiter)
			escaped[j++] = '\\';
		escaped[j] = buffer[i];
	}
	escaped[j] = '\0';
}

/*
 * Generate a signature for a new record.  The signature is conceptually
 * divided into two pieces: a random 16-bit "generation number" and a
 * 48-bit monotonically increasing integer.  The generation number protects
 * against stale updates to records that have been deleted and since
 * recreated.
 */
uint64_t
gensig(void)
{
	static int seeded = 0;

	if (seeded == 0) {
		srand48((long)gethrtime());
		seeded++;
	}

	return ((uint64_t)lrand48() << 48 | 1);
}