2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/endian.h>
36 #include <bitstring.h>
52 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
55 #define PJDLOG_ABORT(...) abort()
58 #define NV_TYPE_NONE 0
60 #define NV_TYPE_INT8 1
61 #define NV_TYPE_UINT8 2
62 #define NV_TYPE_INT16 3
63 #define NV_TYPE_UINT16 4
64 #define NV_TYPE_INT32 5
65 #define NV_TYPE_UINT32 6
66 #define NV_TYPE_INT64 7
67 #define NV_TYPE_UINT64 8
68 #define NV_TYPE_INT8_ARRAY 9
69 #define NV_TYPE_UINT8_ARRAY 10
70 #define NV_TYPE_INT16_ARRAY 11
71 #define NV_TYPE_UINT16_ARRAY 12
72 #define NV_TYPE_INT32_ARRAY 13
73 #define NV_TYPE_UINT32_ARRAY 14
74 #define NV_TYPE_INT64_ARRAY 15
75 #define NV_TYPE_UINT64_ARRAY 16
76 #define NV_TYPE_STRING 17
78 #define NV_TYPE_MASK 0x7f
79 #define NV_TYPE_FIRST NV_TYPE_INT8
80 #define NV_TYPE_LAST NV_TYPE_STRING
82 #define NV_ORDER_NETWORK 0x00
83 #define NV_ORDER_HOST 0x80
85 #define NV_ORDER_MASK 0x80
87 #define NV_MAGIC 0xaea1e
100 #define NVH_DATA(nvh) ((unsigned char *)nvh + NVH_HSIZE(nvh))
101 #define NVH_HSIZE(nvh) \
102 (sizeof(struct nvhdr) + roundup2((nvh)->nvh_namesize, 8))
103 #define NVH_DSIZE(nvh) \
104 (((nvh)->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST ? \
106 le32toh((nvh)->nvh_dsize))
107 #define NVH_SIZE(nvh) (NVH_HSIZE(nvh) + roundup2(NVH_DSIZE(nvh), 8))
109 #define NV_CHECK(nv) do { \
110 PJDLOG_ASSERT((nv) != NULL); \
111 PJDLOG_ASSERT((nv)->nv_magic == NV_MAGIC); \
114 static void nv_add(struct nv *nv, const unsigned char *value, size_t vsize,
115 int type, const char *name);
116 static void nv_addv(struct nv *nv, const unsigned char *value, size_t vsize,
117 int type, const char *namefmt, va_list nameap);
118 static struct nvhdr *nv_find(struct nv *nv, int type, const char *namefmt,
120 static void nv_swap(struct nvhdr *nvh, bool tohost);
123 * Allocate and initialize new nv structure.
124 * Return NULL in case of malloc(3) failure.
131 nv = malloc(sizeof(*nv));
134 nv->nv_ebuf = ebuf_alloc(0);
135 if (nv->nv_ebuf == NULL) {
140 nv->nv_magic = NV_MAGIC;
145 * Free the given nv structure.
148 nv_free(struct nv *nv)
157 ebuf_free(nv->nv_ebuf);
162 * Return error for the given nv structure.
165 nv_error(const struct nv *nv)
173 return (nv->nv_error);
177 * Set error for the given nv structure and return previous error.
180 nv_set_error(struct nv *nv, int error)
189 preverr = nv->nv_error;
190 nv->nv_error = error;
195 * Validate correctness of the entire nv structure and all its elements.
196 * If extrap is not NULL, store number of extra bytes at the end of the buffer.
199 nv_validate(struct nv *nv, size_t *extrap)
202 unsigned char *data, *ptr;
203 size_t dsize, size, vsize;
212 PJDLOG_ASSERT(nv->nv_error == 0);
214 /* TODO: Check that names are unique? */
217 ptr = ebuf_data(nv->nv_ebuf, &size);
220 * Zeros at the end of the buffer are acceptable.
225 * Minimum size at this point is size of nvhdr structure, one
226 * character long name plus terminating '\0'.
228 if (size < sizeof(*nvh) + 2) {
232 nvh = (struct nvhdr *)ptr;
233 if (size < NVH_HSIZE(nvh)) {
237 if (nvh->nvh_name[nvh->nvh_namesize - 1] != '\0') {
241 if (strlen(nvh->nvh_name) !=
242 (size_t)(nvh->nvh_namesize - 1)) {
246 if ((nvh->nvh_type & NV_TYPE_MASK) < NV_TYPE_FIRST ||
247 (nvh->nvh_type & NV_TYPE_MASK) > NV_TYPE_LAST) {
251 dsize = NVH_DSIZE(nvh);
256 if (size < NVH_SIZE(nvh)) {
261 switch (nvh->nvh_type & NV_TYPE_MASK) {
281 if (dsize != vsize) {
286 case NV_TYPE_INT8_ARRAY:
287 case NV_TYPE_UINT8_ARRAY:
289 case NV_TYPE_INT16_ARRAY:
290 case NV_TYPE_UINT16_ARRAY:
294 case NV_TYPE_INT32_ARRAY:
295 case NV_TYPE_UINT32_ARRAY:
299 case NV_TYPE_INT64_ARRAY:
300 case NV_TYPE_UINT64_ARRAY:
303 if ((dsize % vsize) != 0) {
309 data = NVH_DATA(nvh);
310 if (data[dsize - 1] != '\0') {
314 if (strlen((char *)data) != dsize - 1) {
320 PJDLOG_ABORT("invalid condition");
324 ptr += NVH_SIZE(nvh);
325 size -= NVH_SIZE(nvh);
329 if (nv->nv_error == 0)
330 nv->nv_error = error;
339 * Convert the given nv structure to network byte order and return ebuf
343 nv_hton(struct nv *nv)
350 PJDLOG_ASSERT(nv->nv_error == 0);
352 ptr = ebuf_data(nv->nv_ebuf, &size);
355 * Minimum size at this point is size of nvhdr structure,
356 * one character long name plus terminating '\0'.
358 PJDLOG_ASSERT(size >= sizeof(*nvh) + 2);
359 nvh = (struct nvhdr *)ptr;
360 PJDLOG_ASSERT(NVH_SIZE(nvh) <= size);
362 ptr += NVH_SIZE(nvh);
363 size -= NVH_SIZE(nvh);
366 return (nv->nv_ebuf);
370 * Create nv structure based on ebuf received from the network.
373 nv_ntoh(struct ebuf *eb)
379 PJDLOG_ASSERT(eb != NULL);
381 nv = malloc(sizeof(*nv));
386 nv->nv_magic = NV_MAGIC;
388 if (nv_validate(nv, &extra) == -1) {
396 * Remove extra zeros at the end of the buffer.
398 ebuf_del_tail(eb, extra);
403 #define NV_DEFINE_ADD(type, TYPE) \
405 nv_add_##type(struct nv *nv, type##_t value, const char *namefmt, ...) \
409 va_start(nameap, namefmt); \
410 nv_addv(nv, (unsigned char *)&value, sizeof(value), \
411 NV_TYPE_##TYPE, namefmt, nameap); \
415 NV_DEFINE_ADD(int8, INT8)
416 NV_DEFINE_ADD(uint8, UINT8)
417 NV_DEFINE_ADD(int16, INT16)
418 NV_DEFINE_ADD(uint16, UINT16)
419 NV_DEFINE_ADD(int32, INT32)
420 NV_DEFINE_ADD(uint32, UINT32)
421 NV_DEFINE_ADD(int64, INT64)
422 NV_DEFINE_ADD(uint64, UINT64)
426 #define NV_DEFINE_ADD_ARRAY(type, TYPE) \
428 nv_add_##type##_array(struct nv *nv, const type##_t *value, \
429 size_t nsize, const char *namefmt, ...) \
433 va_start(nameap, namefmt); \
434 nv_addv(nv, (const unsigned char *)value, \
435 sizeof(value[0]) * nsize, NV_TYPE_##TYPE##_ARRAY, namefmt, \
440 NV_DEFINE_ADD_ARRAY(int8, INT8)
441 NV_DEFINE_ADD_ARRAY(uint8, UINT8)
442 NV_DEFINE_ADD_ARRAY(int16, INT16)
443 NV_DEFINE_ADD_ARRAY(uint16, UINT16)
444 NV_DEFINE_ADD_ARRAY(int32, INT32)
445 NV_DEFINE_ADD_ARRAY(uint32, UINT32)
446 NV_DEFINE_ADD_ARRAY(int64, INT64)
447 NV_DEFINE_ADD_ARRAY(uint64, UINT64)
449 #undef NV_DEFINE_ADD_ARRAY
452 nv_add_string(struct nv *nv, const char *value, const char *namefmt, ...)
457 size = strlen(value) + 1;
459 va_start(nameap, namefmt);
460 nv_addv(nv, (const unsigned char *)value, size, NV_TYPE_STRING,
466 nv_add_stringf(struct nv *nv, const char *name, const char *valuefmt, ...)
470 va_start(valueap, valuefmt);
471 nv_add_stringv(nv, name, valuefmt, valueap);
476 nv_add_stringv(struct nv *nv, const char *name, const char *valuefmt,
482 size = vasprintf(&value, valuefmt, valueap);
484 if (nv->nv_error == 0)
485 nv->nv_error = ENOMEM;
489 nv_add(nv, (const unsigned char *)value, size, NV_TYPE_STRING, name);
493 #define NV_DEFINE_GET(type, TYPE) \
495 nv_get_##type(struct nv *nv, const char *namefmt, ...) \
501 va_start(nameap, namefmt); \
502 nvh = nv_find(nv, NV_TYPE_##TYPE, namefmt, nameap); \
506 PJDLOG_ASSERT((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST);\
507 PJDLOG_ASSERT(sizeof(value) == nvh->nvh_dsize); \
508 bcopy(NVH_DATA(nvh), &value, sizeof(value)); \
513 NV_DEFINE_GET(int8, INT8)
514 NV_DEFINE_GET(uint8, UINT8)
515 NV_DEFINE_GET(int16, INT16)
516 NV_DEFINE_GET(uint16, UINT16)
517 NV_DEFINE_GET(int32, INT32)
518 NV_DEFINE_GET(uint32, UINT32)
519 NV_DEFINE_GET(int64, INT64)
520 NV_DEFINE_GET(uint64, UINT64)
524 #define NV_DEFINE_GET_ARRAY(type, TYPE) \
526 nv_get_##type##_array(struct nv *nv, size_t *sizep, \
527 const char *namefmt, ...) \
532 va_start(nameap, namefmt); \
533 nvh = nv_find(nv, NV_TYPE_##TYPE##_ARRAY, namefmt, nameap); \
537 PJDLOG_ASSERT((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST);\
538 PJDLOG_ASSERT((nvh->nvh_dsize % sizeof(type##_t)) == 0); \
540 *sizep = nvh->nvh_dsize / sizeof(type##_t); \
541 return ((type##_t *)(void *)NVH_DATA(nvh)); \
544 NV_DEFINE_GET_ARRAY(int8, INT8)
545 NV_DEFINE_GET_ARRAY(uint8, UINT8)
546 NV_DEFINE_GET_ARRAY(int16, INT16)
547 NV_DEFINE_GET_ARRAY(uint16, UINT16)
548 NV_DEFINE_GET_ARRAY(int32, INT32)
549 NV_DEFINE_GET_ARRAY(uint32, UINT32)
550 NV_DEFINE_GET_ARRAY(int64, INT64)
551 NV_DEFINE_GET_ARRAY(uint64, UINT64)
553 #undef NV_DEFINE_GET_ARRAY
556 nv_get_string(struct nv *nv, const char *namefmt, ...)
562 va_start(nameap, namefmt);
563 nvh = nv_find(nv, NV_TYPE_STRING, namefmt, nameap);
567 PJDLOG_ASSERT((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST);
568 PJDLOG_ASSERT(nvh->nvh_dsize >= 1);
569 str = (char *)NVH_DATA(nvh);
570 PJDLOG_ASSERT(str[nvh->nvh_dsize - 1] == '\0');
571 PJDLOG_ASSERT(strlen(str) == nvh->nvh_dsize - 1);
576 nv_vexists(struct nv *nv, const char *namefmt, va_list nameap)
579 int snverror, serrno;
585 snverror = nv->nv_error;
587 nvh = nv_find(nv, NV_TYPE_NONE, namefmt, nameap);
590 nv->nv_error = snverror;
592 return (nvh != NULL);
596 nv_exists(struct nv *nv, const char *namefmt, ...)
601 va_start(nameap, namefmt);
602 ret = nv_vexists(nv, namefmt, nameap);
609 nv_assert(struct nv *nv, const char *namefmt, ...)
613 va_start(nameap, namefmt);
614 PJDLOG_ASSERT(nv_vexists(nv, namefmt, nameap));
619 * Dump content of the nv structure.
622 nv_dump(struct nv *nv)
625 unsigned char *data, *ptr;
630 if (nv_validate(nv, NULL) == -1) {
631 printf("error: %d\n", errno);
636 PJDLOG_ASSERT(nv->nv_error == 0);
638 ptr = ebuf_data(nv->nv_ebuf, &size);
640 PJDLOG_ASSERT(size >= sizeof(*nvh) + 2);
641 nvh = (struct nvhdr *)ptr;
642 PJDLOG_ASSERT(size >= NVH_SIZE(nvh));
643 swap = ((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_NETWORK);
644 dsize = NVH_DSIZE(nvh);
645 data = NVH_DATA(nvh);
646 printf(" %s", nvh->nvh_name);
647 switch (nvh->nvh_type & NV_TYPE_MASK) {
649 printf("(int8): %jd", (intmax_t)(*(int8_t *)data));
652 printf("(uint8): %ju", (uintmax_t)(*(uint8_t *)data));
655 printf("(int16): %jd", swap ?
656 (intmax_t)le16toh(*(int16_t *)(void *)data) :
657 (intmax_t)*(int16_t *)(void *)data);
660 printf("(uint16): %ju", swap ?
661 (uintmax_t)le16toh(*(uint16_t *)(void *)data) :
662 (uintmax_t)*(uint16_t *)(void *)data);
665 printf("(int32): %jd", swap ?
666 (intmax_t)le32toh(*(int32_t *)(void *)data) :
667 (intmax_t)*(int32_t *)(void *)data);
670 printf("(uint32): %ju", swap ?
671 (uintmax_t)le32toh(*(uint32_t *)(void *)data) :
672 (uintmax_t)*(uint32_t *)(void *)data);
675 printf("(int64): %jd", swap ?
676 (intmax_t)le64toh(*(int64_t *)(void *)data) :
677 (intmax_t)*(int64_t *)(void *)data);
680 printf("(uint64): %ju", swap ?
681 (uintmax_t)le64toh(*(uint64_t *)(void *)data) :
682 (uintmax_t)*(uint64_t *)(void *)data);
684 case NV_TYPE_INT8_ARRAY:
685 printf("(int8 array):");
686 for (ii = 0; ii < dsize; ii++)
687 printf(" %jd", (intmax_t)((int8_t *)data)[ii]);
689 case NV_TYPE_UINT8_ARRAY:
690 printf("(uint8 array):");
691 for (ii = 0; ii < dsize; ii++)
692 printf(" %ju", (uintmax_t)((uint8_t *)data)[ii]);
694 case NV_TYPE_INT16_ARRAY:
695 printf("(int16 array):");
696 for (ii = 0; ii < dsize / 2; ii++) {
697 printf(" %jd", swap ?
698 (intmax_t)le16toh(((int16_t *)(void *)data)[ii]) :
699 (intmax_t)((int16_t *)(void *)data)[ii]);
702 case NV_TYPE_UINT16_ARRAY:
703 printf("(uint16 array):");
704 for (ii = 0; ii < dsize / 2; ii++) {
705 printf(" %ju", swap ?
706 (uintmax_t)le16toh(((uint16_t *)(void *)data)[ii]) :
707 (uintmax_t)((uint16_t *)(void *)data)[ii]);
710 case NV_TYPE_INT32_ARRAY:
711 printf("(int32 array):");
712 for (ii = 0; ii < dsize / 4; ii++) {
713 printf(" %jd", swap ?
714 (intmax_t)le32toh(((int32_t *)(void *)data)[ii]) :
715 (intmax_t)((int32_t *)(void *)data)[ii]);
718 case NV_TYPE_UINT32_ARRAY:
719 printf("(uint32 array):");
720 for (ii = 0; ii < dsize / 4; ii++) {
721 printf(" %ju", swap ?
722 (uintmax_t)le32toh(((uint32_t *)(void *)data)[ii]) :
723 (uintmax_t)((uint32_t *)(void *)data)[ii]);
726 case NV_TYPE_INT64_ARRAY:
727 printf("(int64 array):");
728 for (ii = 0; ii < dsize / 8; ii++) {
729 printf(" %ju", swap ?
730 (uintmax_t)le64toh(((uint64_t *)(void *)data)[ii]) :
731 (uintmax_t)((uint64_t *)(void *)data)[ii]);
734 case NV_TYPE_UINT64_ARRAY:
735 printf("(uint64 array):");
736 for (ii = 0; ii < dsize / 8; ii++) {
737 printf(" %ju", swap ?
738 (uintmax_t)le64toh(((uint64_t *)(void *)data)[ii]) :
739 (uintmax_t)((uint64_t *)(void *)data)[ii]);
743 printf("(string): %s", (char *)data);
746 PJDLOG_ABORT("invalid condition");
749 ptr += NVH_SIZE(nvh);
750 size -= NVH_SIZE(nvh);
755 * Local routines below.
759 nv_add(struct nv *nv, const unsigned char *value, size_t vsize, int type,
762 static unsigned char align[7];
773 namesize = strlen(name) + 1;
775 nvh = malloc(sizeof(*nvh) + roundup2(namesize, 8));
777 if (nv->nv_error == 0)
778 nv->nv_error = ENOMEM;
781 nvh->nvh_type = NV_ORDER_HOST | type;
782 nvh->nvh_namesize = (uint8_t)namesize;
783 nvh->nvh_dsize = (uint32_t)vsize;
784 bcopy(name, nvh->nvh_name, namesize);
786 /* Add header first. */
787 if (ebuf_add_tail(nv->nv_ebuf, nvh, NVH_HSIZE(nvh)) == -1) {
788 PJDLOG_ASSERT(errno != 0);
789 if (nv->nv_error == 0)
790 nv->nv_error = errno;
795 /* Add the actual data. */
796 if (ebuf_add_tail(nv->nv_ebuf, value, vsize) == -1) {
797 PJDLOG_ASSERT(errno != 0);
798 if (nv->nv_error == 0)
799 nv->nv_error = errno;
802 /* Align the data (if needed). */
803 vsize = roundup2(vsize, 8) - vsize;
806 PJDLOG_ASSERT(vsize > 0 && vsize <= sizeof(align));
807 if (ebuf_add_tail(nv->nv_ebuf, align, vsize) == -1) {
808 PJDLOG_ASSERT(errno != 0);
809 if (nv->nv_error == 0)
810 nv->nv_error = errno;
816 nv_addv(struct nv *nv, const unsigned char *value, size_t vsize, int type,
817 const char *namefmt, va_list nameap)
822 namesize = vsnprintf(name, sizeof(name), namefmt, nameap);
823 PJDLOG_ASSERT(namesize > 0 && namesize < sizeof(name));
825 nv_add(nv, value, vsize, type, name);
828 static struct nvhdr *
829 nv_find(struct nv *nv, int type, const char *namefmt, va_list nameap)
834 size_t size, namesize;
843 namesize = vsnprintf(name, sizeof(name), namefmt, nameap);
844 PJDLOG_ASSERT(namesize > 0 && namesize < sizeof(name));
847 ptr = ebuf_data(nv->nv_ebuf, &size);
849 PJDLOG_ASSERT(size >= sizeof(*nvh) + 2);
850 nvh = (struct nvhdr *)ptr;
851 PJDLOG_ASSERT(size >= NVH_SIZE(nvh));
853 if (strcmp(nvh->nvh_name, name) == 0) {
854 if (type != NV_TYPE_NONE &&
855 (nvh->nvh_type & NV_TYPE_MASK) != type) {
857 if (nv->nv_error == 0)
858 nv->nv_error = EINVAL;
863 ptr += NVH_SIZE(nvh);
864 size -= NVH_SIZE(nvh);
867 if (nv->nv_error == 0)
868 nv->nv_error = ENOENT;
873 nv_swap(struct nvhdr *nvh, bool tohost)
875 unsigned char *data, *end, *p;
878 data = NVH_DATA(nvh);
880 if ((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST)
882 nvh->nvh_dsize = le32toh(nvh->nvh_dsize);
883 end = data + nvh->nvh_dsize;
884 nvh->nvh_type &= ~NV_ORDER_MASK;
885 nvh->nvh_type |= NV_ORDER_HOST;
887 if ((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_NETWORK)
889 end = data + nvh->nvh_dsize;
890 nvh->nvh_dsize = htole32(nvh->nvh_dsize);
891 nvh->nvh_type &= ~NV_ORDER_MASK;
892 nvh->nvh_type |= NV_ORDER_NETWORK;
897 switch (nvh->nvh_type & NV_TYPE_MASK) {
900 case NV_TYPE_INT8_ARRAY:
901 case NV_TYPE_UINT8_ARRAY:
905 case NV_TYPE_INT16_ARRAY:
906 case NV_TYPE_UINT16_ARRAY:
912 case NV_TYPE_INT32_ARRAY:
913 case NV_TYPE_UINT32_ARRAY:
919 case NV_TYPE_INT64_ARRAY:
920 case NV_TYPE_UINT64_ARRAY:
923 for (p = data; p < end; p += vsize) {
927 *(uint16_t *)(void *)p =
928 le16toh(*(uint16_t *)(void *)p);
931 *(uint32_t *)(void *)p =
932 le32toh(*(uint32_t *)(void *)p);
935 *(uint64_t *)(void *)p =
936 le64toh(*(uint64_t *)(void *)p);
939 PJDLOG_ABORT("invalid condition");
944 *(uint16_t *)(void *)p =
945 htole16(*(uint16_t *)(void *)p);
948 *(uint32_t *)(void *)p =
949 htole32(*(uint32_t *)(void *)p);
952 *(uint64_t *)(void *)p =
953 htole64(*(uint64_t *)(void *)p);
956 PJDLOG_ABORT("invalid condition");
964 PJDLOG_ABORT("unrecognized type");