2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2009-2010 The FreeBSD Foundation
7 * This software was developed by Pawel Jakub Dawidek under sponsorship from
8 * the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
33 #include <sys/endian.h>
35 #include <bitstring.h>
51 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
54 #define PJDLOG_ABORT(...) abort()
57 #define NV_TYPE_NONE 0
59 #define NV_TYPE_INT8 1
60 #define NV_TYPE_UINT8 2
61 #define NV_TYPE_INT16 3
62 #define NV_TYPE_UINT16 4
63 #define NV_TYPE_INT32 5
64 #define NV_TYPE_UINT32 6
65 #define NV_TYPE_INT64 7
66 #define NV_TYPE_UINT64 8
67 #define NV_TYPE_INT8_ARRAY 9
68 #define NV_TYPE_UINT8_ARRAY 10
69 #define NV_TYPE_INT16_ARRAY 11
70 #define NV_TYPE_UINT16_ARRAY 12
71 #define NV_TYPE_INT32_ARRAY 13
72 #define NV_TYPE_UINT32_ARRAY 14
73 #define NV_TYPE_INT64_ARRAY 15
74 #define NV_TYPE_UINT64_ARRAY 16
75 #define NV_TYPE_STRING 17
77 #define NV_TYPE_MASK 0x7f
78 #define NV_TYPE_FIRST NV_TYPE_INT8
79 #define NV_TYPE_LAST NV_TYPE_STRING
81 #define NV_ORDER_NETWORK 0x00
82 #define NV_ORDER_HOST 0x80
84 #define NV_ORDER_MASK 0x80
86 #define NV_MAGIC 0xaea1e
99 #define NVH_DATA(nvh) ((unsigned char *)nvh + NVH_HSIZE(nvh))
100 #define NVH_HSIZE(nvh) \
101 (sizeof(struct nvhdr) + roundup2((nvh)->nvh_namesize, 8))
102 #define NVH_DSIZE(nvh) \
103 (((nvh)->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST ? \
105 le32toh((nvh)->nvh_dsize))
106 #define NVH_SIZE(nvh) (NVH_HSIZE(nvh) + roundup2(NVH_DSIZE(nvh), 8))
108 #define NV_CHECK(nv) do { \
109 PJDLOG_ASSERT((nv) != NULL); \
110 PJDLOG_ASSERT((nv)->nv_magic == NV_MAGIC); \
113 static void nv_add(struct nv *nv, const unsigned char *value, size_t vsize,
114 int type, const char *name);
115 static void nv_addv(struct nv *nv, const unsigned char *value, size_t vsize,
116 int type, const char *namefmt, va_list nameap);
117 static struct nvhdr *nv_find(struct nv *nv, int type, const char *namefmt,
119 static void nv_swap(struct nvhdr *nvh, bool tohost);
122 * Allocate and initialize new nv structure.
123 * Return NULL in case of malloc(3) failure.
130 nv = malloc(sizeof(*nv));
133 nv->nv_ebuf = ebuf_alloc(0);
134 if (nv->nv_ebuf == NULL) {
139 nv->nv_magic = NV_MAGIC;
144 * Free the given nv structure.
147 nv_free(struct nv *nv)
156 ebuf_free(nv->nv_ebuf);
161 * Return error for the given nv structure.
164 nv_error(const struct nv *nv)
172 return (nv->nv_error);
176 * Set error for the given nv structure and return previous error.
179 nv_set_error(struct nv *nv, int error)
188 preverr = nv->nv_error;
189 nv->nv_error = error;
194 * Validate correctness of the entire nv structure and all its elements.
195 * If extrap is not NULL, store number of extra bytes at the end of the buffer.
198 nv_validate(struct nv *nv, size_t *extrap)
201 unsigned char *data, *ptr;
202 size_t dsize, size, vsize;
211 PJDLOG_ASSERT(nv->nv_error == 0);
213 /* TODO: Check that names are unique? */
216 ptr = ebuf_data(nv->nv_ebuf, &size);
219 * Zeros at the end of the buffer are acceptable.
224 * Minimum size at this point is size of nvhdr structure, one
225 * character long name plus terminating '\0'.
227 if (size < sizeof(*nvh) + 2) {
231 nvh = (struct nvhdr *)ptr;
232 if (size < NVH_HSIZE(nvh)) {
236 if (nvh->nvh_name[nvh->nvh_namesize - 1] != '\0') {
240 if (strlen(nvh->nvh_name) !=
241 (size_t)(nvh->nvh_namesize - 1)) {
245 if ((nvh->nvh_type & NV_TYPE_MASK) < NV_TYPE_FIRST ||
246 (nvh->nvh_type & NV_TYPE_MASK) > NV_TYPE_LAST) {
250 dsize = NVH_DSIZE(nvh);
255 if (size < NVH_SIZE(nvh)) {
260 switch (nvh->nvh_type & NV_TYPE_MASK) {
280 if (dsize != vsize) {
285 case NV_TYPE_INT8_ARRAY:
286 case NV_TYPE_UINT8_ARRAY:
288 case NV_TYPE_INT16_ARRAY:
289 case NV_TYPE_UINT16_ARRAY:
293 case NV_TYPE_INT32_ARRAY:
294 case NV_TYPE_UINT32_ARRAY:
298 case NV_TYPE_INT64_ARRAY:
299 case NV_TYPE_UINT64_ARRAY:
302 if ((dsize % vsize) != 0) {
308 data = NVH_DATA(nvh);
309 if (data[dsize - 1] != '\0') {
313 if (strlen((char *)data) != dsize - 1) {
319 PJDLOG_ABORT("invalid condition");
323 ptr += NVH_SIZE(nvh);
324 size -= NVH_SIZE(nvh);
328 if (nv->nv_error == 0)
329 nv->nv_error = error;
338 * Convert the given nv structure to network byte order and return ebuf
342 nv_hton(struct nv *nv)
349 PJDLOG_ASSERT(nv->nv_error == 0);
351 ptr = ebuf_data(nv->nv_ebuf, &size);
354 * Minimum size at this point is size of nvhdr structure,
355 * one character long name plus terminating '\0'.
357 PJDLOG_ASSERT(size >= sizeof(*nvh) + 2);
358 nvh = (struct nvhdr *)ptr;
359 PJDLOG_ASSERT(NVH_SIZE(nvh) <= size);
361 ptr += NVH_SIZE(nvh);
362 size -= NVH_SIZE(nvh);
365 return (nv->nv_ebuf);
369 * Create nv structure based on ebuf received from the network.
372 nv_ntoh(struct ebuf *eb)
378 PJDLOG_ASSERT(eb != NULL);
380 nv = malloc(sizeof(*nv));
385 nv->nv_magic = NV_MAGIC;
387 if (nv_validate(nv, &extra) == -1) {
395 * Remove extra zeros at the end of the buffer.
397 ebuf_del_tail(eb, extra);
402 #define NV_DEFINE_ADD(type, TYPE) \
404 nv_add_##type(struct nv *nv, type##_t value, const char *namefmt, ...) \
408 va_start(nameap, namefmt); \
409 nv_addv(nv, (unsigned char *)&value, sizeof(value), \
410 NV_TYPE_##TYPE, namefmt, nameap); \
414 NV_DEFINE_ADD(int8, INT8)
415 NV_DEFINE_ADD(uint8, UINT8)
416 NV_DEFINE_ADD(int16, INT16)
417 NV_DEFINE_ADD(uint16, UINT16)
418 NV_DEFINE_ADD(int32, INT32)
419 NV_DEFINE_ADD(uint32, UINT32)
420 NV_DEFINE_ADD(int64, INT64)
421 NV_DEFINE_ADD(uint64, UINT64)
425 #define NV_DEFINE_ADD_ARRAY(type, TYPE) \
427 nv_add_##type##_array(struct nv *nv, const type##_t *value, \
428 size_t nsize, const char *namefmt, ...) \
432 va_start(nameap, namefmt); \
433 nv_addv(nv, (const unsigned char *)value, \
434 sizeof(value[0]) * nsize, NV_TYPE_##TYPE##_ARRAY, namefmt, \
439 NV_DEFINE_ADD_ARRAY(int8, INT8)
440 NV_DEFINE_ADD_ARRAY(uint8, UINT8)
441 NV_DEFINE_ADD_ARRAY(int16, INT16)
442 NV_DEFINE_ADD_ARRAY(uint16, UINT16)
443 NV_DEFINE_ADD_ARRAY(int32, INT32)
444 NV_DEFINE_ADD_ARRAY(uint32, UINT32)
445 NV_DEFINE_ADD_ARRAY(int64, INT64)
446 NV_DEFINE_ADD_ARRAY(uint64, UINT64)
448 #undef NV_DEFINE_ADD_ARRAY
451 nv_add_string(struct nv *nv, const char *value, const char *namefmt, ...)
456 size = strlen(value) + 1;
458 va_start(nameap, namefmt);
459 nv_addv(nv, (const unsigned char *)value, size, NV_TYPE_STRING,
465 nv_add_stringf(struct nv *nv, const char *name, const char *valuefmt, ...)
469 va_start(valueap, valuefmt);
470 nv_add_stringv(nv, name, valuefmt, valueap);
475 nv_add_stringv(struct nv *nv, const char *name, const char *valuefmt,
481 size = vasprintf(&value, valuefmt, valueap);
483 if (nv->nv_error == 0)
484 nv->nv_error = ENOMEM;
488 nv_add(nv, (const unsigned char *)value, size, NV_TYPE_STRING, name);
492 #define NV_DEFINE_GET(type, TYPE) \
494 nv_get_##type(struct nv *nv, const char *namefmt, ...) \
500 va_start(nameap, namefmt); \
501 nvh = nv_find(nv, NV_TYPE_##TYPE, namefmt, nameap); \
505 PJDLOG_ASSERT((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST);\
506 PJDLOG_ASSERT(sizeof(value) == nvh->nvh_dsize); \
507 bcopy(NVH_DATA(nvh), &value, sizeof(value)); \
512 NV_DEFINE_GET(int8, INT8)
513 NV_DEFINE_GET(uint8, UINT8)
514 NV_DEFINE_GET(int16, INT16)
515 NV_DEFINE_GET(uint16, UINT16)
516 NV_DEFINE_GET(int32, INT32)
517 NV_DEFINE_GET(uint32, UINT32)
518 NV_DEFINE_GET(int64, INT64)
519 NV_DEFINE_GET(uint64, UINT64)
523 #define NV_DEFINE_GET_ARRAY(type, TYPE) \
525 nv_get_##type##_array(struct nv *nv, size_t *sizep, \
526 const char *namefmt, ...) \
531 va_start(nameap, namefmt); \
532 nvh = nv_find(nv, NV_TYPE_##TYPE##_ARRAY, namefmt, nameap); \
536 PJDLOG_ASSERT((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST);\
537 PJDLOG_ASSERT((nvh->nvh_dsize % sizeof(type##_t)) == 0); \
539 *sizep = nvh->nvh_dsize / sizeof(type##_t); \
540 return ((type##_t *)(void *)NVH_DATA(nvh)); \
543 NV_DEFINE_GET_ARRAY(int8, INT8)
544 NV_DEFINE_GET_ARRAY(uint8, UINT8)
545 NV_DEFINE_GET_ARRAY(int16, INT16)
546 NV_DEFINE_GET_ARRAY(uint16, UINT16)
547 NV_DEFINE_GET_ARRAY(int32, INT32)
548 NV_DEFINE_GET_ARRAY(uint32, UINT32)
549 NV_DEFINE_GET_ARRAY(int64, INT64)
550 NV_DEFINE_GET_ARRAY(uint64, UINT64)
552 #undef NV_DEFINE_GET_ARRAY
555 nv_get_string(struct nv *nv, const char *namefmt, ...)
561 va_start(nameap, namefmt);
562 nvh = nv_find(nv, NV_TYPE_STRING, namefmt, nameap);
566 PJDLOG_ASSERT((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST);
567 PJDLOG_ASSERT(nvh->nvh_dsize >= 1);
568 str = (char *)NVH_DATA(nvh);
569 PJDLOG_ASSERT(str[nvh->nvh_dsize - 1] == '\0');
570 PJDLOG_ASSERT(strlen(str) == nvh->nvh_dsize - 1);
575 nv_vexists(struct nv *nv, const char *namefmt, va_list nameap)
578 int snverror, serrno;
584 snverror = nv->nv_error;
586 nvh = nv_find(nv, NV_TYPE_NONE, namefmt, nameap);
589 nv->nv_error = snverror;
591 return (nvh != NULL);
595 nv_exists(struct nv *nv, const char *namefmt, ...)
600 va_start(nameap, namefmt);
601 ret = nv_vexists(nv, namefmt, nameap);
608 nv_assert(struct nv *nv, const char *namefmt, ...)
612 va_start(nameap, namefmt);
613 PJDLOG_ASSERT(nv_vexists(nv, namefmt, nameap));
618 * Dump content of the nv structure.
621 nv_dump(struct nv *nv)
624 unsigned char *data, *ptr;
629 if (nv_validate(nv, NULL) == -1) {
630 printf("error: %d\n", errno);
635 PJDLOG_ASSERT(nv->nv_error == 0);
637 ptr = ebuf_data(nv->nv_ebuf, &size);
639 PJDLOG_ASSERT(size >= sizeof(*nvh) + 2);
640 nvh = (struct nvhdr *)ptr;
641 PJDLOG_ASSERT(size >= NVH_SIZE(nvh));
642 swap = ((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_NETWORK);
643 dsize = NVH_DSIZE(nvh);
644 data = NVH_DATA(nvh);
645 printf(" %s", nvh->nvh_name);
646 switch (nvh->nvh_type & NV_TYPE_MASK) {
648 printf("(int8): %jd", (intmax_t)(*(int8_t *)data));
651 printf("(uint8): %ju", (uintmax_t)(*(uint8_t *)data));
654 printf("(int16): %jd", swap ?
655 (intmax_t)le16toh(*(int16_t *)(void *)data) :
656 (intmax_t)*(int16_t *)(void *)data);
659 printf("(uint16): %ju", swap ?
660 (uintmax_t)le16toh(*(uint16_t *)(void *)data) :
661 (uintmax_t)*(uint16_t *)(void *)data);
664 printf("(int32): %jd", swap ?
665 (intmax_t)le32toh(*(int32_t *)(void *)data) :
666 (intmax_t)*(int32_t *)(void *)data);
669 printf("(uint32): %ju", swap ?
670 (uintmax_t)le32toh(*(uint32_t *)(void *)data) :
671 (uintmax_t)*(uint32_t *)(void *)data);
674 printf("(int64): %jd", swap ?
675 (intmax_t)le64toh(*(int64_t *)(void *)data) :
676 (intmax_t)*(int64_t *)(void *)data);
679 printf("(uint64): %ju", swap ?
680 (uintmax_t)le64toh(*(uint64_t *)(void *)data) :
681 (uintmax_t)*(uint64_t *)(void *)data);
683 case NV_TYPE_INT8_ARRAY:
684 printf("(int8 array):");
685 for (ii = 0; ii < dsize; ii++)
686 printf(" %jd", (intmax_t)((int8_t *)data)[ii]);
688 case NV_TYPE_UINT8_ARRAY:
689 printf("(uint8 array):");
690 for (ii = 0; ii < dsize; ii++)
691 printf(" %ju", (uintmax_t)((uint8_t *)data)[ii]);
693 case NV_TYPE_INT16_ARRAY:
694 printf("(int16 array):");
695 for (ii = 0; ii < dsize / 2; ii++) {
696 printf(" %jd", swap ?
697 (intmax_t)le16toh(((int16_t *)(void *)data)[ii]) :
698 (intmax_t)((int16_t *)(void *)data)[ii]);
701 case NV_TYPE_UINT16_ARRAY:
702 printf("(uint16 array):");
703 for (ii = 0; ii < dsize / 2; ii++) {
704 printf(" %ju", swap ?
705 (uintmax_t)le16toh(((uint16_t *)(void *)data)[ii]) :
706 (uintmax_t)((uint16_t *)(void *)data)[ii]);
709 case NV_TYPE_INT32_ARRAY:
710 printf("(int32 array):");
711 for (ii = 0; ii < dsize / 4; ii++) {
712 printf(" %jd", swap ?
713 (intmax_t)le32toh(((int32_t *)(void *)data)[ii]) :
714 (intmax_t)((int32_t *)(void *)data)[ii]);
717 case NV_TYPE_UINT32_ARRAY:
718 printf("(uint32 array):");
719 for (ii = 0; ii < dsize / 4; ii++) {
720 printf(" %ju", swap ?
721 (uintmax_t)le32toh(((uint32_t *)(void *)data)[ii]) :
722 (uintmax_t)((uint32_t *)(void *)data)[ii]);
725 case NV_TYPE_INT64_ARRAY:
726 printf("(int64 array):");
727 for (ii = 0; ii < dsize / 8; ii++) {
728 printf(" %ju", swap ?
729 (uintmax_t)le64toh(((uint64_t *)(void *)data)[ii]) :
730 (uintmax_t)((uint64_t *)(void *)data)[ii]);
733 case NV_TYPE_UINT64_ARRAY:
734 printf("(uint64 array):");
735 for (ii = 0; ii < dsize / 8; ii++) {
736 printf(" %ju", swap ?
737 (uintmax_t)le64toh(((uint64_t *)(void *)data)[ii]) :
738 (uintmax_t)((uint64_t *)(void *)data)[ii]);
742 printf("(string): %s", (char *)data);
745 PJDLOG_ABORT("invalid condition");
748 ptr += NVH_SIZE(nvh);
749 size -= NVH_SIZE(nvh);
754 * Local routines below.
758 nv_add(struct nv *nv, const unsigned char *value, size_t vsize, int type,
761 static unsigned char align[7];
772 namesize = strlen(name) + 1;
774 nvh = malloc(sizeof(*nvh) + roundup2(namesize, 8));
776 if (nv->nv_error == 0)
777 nv->nv_error = ENOMEM;
780 nvh->nvh_type = NV_ORDER_HOST | type;
781 nvh->nvh_namesize = (uint8_t)namesize;
782 nvh->nvh_dsize = (uint32_t)vsize;
783 bcopy(name, nvh->nvh_name, namesize);
785 /* Add header first. */
786 if (ebuf_add_tail(nv->nv_ebuf, nvh, NVH_HSIZE(nvh)) == -1) {
787 PJDLOG_ASSERT(errno != 0);
788 if (nv->nv_error == 0)
789 nv->nv_error = errno;
794 /* Add the actual data. */
795 if (ebuf_add_tail(nv->nv_ebuf, value, vsize) == -1) {
796 PJDLOG_ASSERT(errno != 0);
797 if (nv->nv_error == 0)
798 nv->nv_error = errno;
801 /* Align the data (if needed). */
802 vsize = roundup2(vsize, 8) - vsize;
805 PJDLOG_ASSERT(vsize > 0 && vsize <= sizeof(align));
806 if (ebuf_add_tail(nv->nv_ebuf, align, vsize) == -1) {
807 PJDLOG_ASSERT(errno != 0);
808 if (nv->nv_error == 0)
809 nv->nv_error = errno;
815 nv_addv(struct nv *nv, const unsigned char *value, size_t vsize, int type,
816 const char *namefmt, va_list nameap)
821 namesize = vsnprintf(name, sizeof(name), namefmt, nameap);
822 PJDLOG_ASSERT(namesize > 0 && namesize < sizeof(name));
824 nv_add(nv, value, vsize, type, name);
827 static struct nvhdr *
828 nv_find(struct nv *nv, int type, const char *namefmt, va_list nameap)
833 size_t size, namesize;
842 namesize = vsnprintf(name, sizeof(name), namefmt, nameap);
843 PJDLOG_ASSERT(namesize > 0 && namesize < sizeof(name));
846 ptr = ebuf_data(nv->nv_ebuf, &size);
848 PJDLOG_ASSERT(size >= sizeof(*nvh) + 2);
849 nvh = (struct nvhdr *)ptr;
850 PJDLOG_ASSERT(size >= NVH_SIZE(nvh));
852 if (strcmp(nvh->nvh_name, name) == 0) {
853 if (type != NV_TYPE_NONE &&
854 (nvh->nvh_type & NV_TYPE_MASK) != type) {
856 if (nv->nv_error == 0)
857 nv->nv_error = EINVAL;
862 ptr += NVH_SIZE(nvh);
863 size -= NVH_SIZE(nvh);
866 if (nv->nv_error == 0)
867 nv->nv_error = ENOENT;
872 nv_swap(struct nvhdr *nvh, bool tohost)
874 unsigned char *data, *end, *p;
877 data = NVH_DATA(nvh);
879 if ((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_HOST)
881 nvh->nvh_dsize = le32toh(nvh->nvh_dsize);
882 end = data + nvh->nvh_dsize;
883 nvh->nvh_type &= ~NV_ORDER_MASK;
884 nvh->nvh_type |= NV_ORDER_HOST;
886 if ((nvh->nvh_type & NV_ORDER_MASK) == NV_ORDER_NETWORK)
888 end = data + nvh->nvh_dsize;
889 nvh->nvh_dsize = htole32(nvh->nvh_dsize);
890 nvh->nvh_type &= ~NV_ORDER_MASK;
891 nvh->nvh_type |= NV_ORDER_NETWORK;
896 switch (nvh->nvh_type & NV_TYPE_MASK) {
899 case NV_TYPE_INT8_ARRAY:
900 case NV_TYPE_UINT8_ARRAY:
904 case NV_TYPE_INT16_ARRAY:
905 case NV_TYPE_UINT16_ARRAY:
911 case NV_TYPE_INT32_ARRAY:
912 case NV_TYPE_UINT32_ARRAY:
918 case NV_TYPE_INT64_ARRAY:
919 case NV_TYPE_UINT64_ARRAY:
922 for (p = data; p < end; p += vsize) {
926 *(uint16_t *)(void *)p =
927 le16toh(*(uint16_t *)(void *)p);
930 *(uint32_t *)(void *)p =
931 le32toh(*(uint32_t *)(void *)p);
934 *(uint64_t *)(void *)p =
935 le64toh(*(uint64_t *)(void *)p);
938 PJDLOG_ABORT("invalid condition");
943 *(uint16_t *)(void *)p =
944 htole16(*(uint16_t *)(void *)p);
947 *(uint32_t *)(void *)p =
948 htole32(*(uint32_t *)(void *)p);
951 *(uint64_t *)(void *)p =
952 htole64(*(uint64_t *)(void *)p);
955 PJDLOG_ABORT("invalid condition");
963 PJDLOG_ABORT("unrecognized type");