2 * Copyright (c) 2009-2013 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
35 #include <sys/cdefs.h>
44 #ifndef _NVLIST_T_DECLARED
45 #define _NVLIST_T_DECLARED
48 typedef struct nvlist nvlist_t;
51 #define NV_NAME_MAX 2048
53 #define NV_TYPE_NONE 0
55 #define NV_TYPE_NULL 1
56 #define NV_TYPE_BOOL 2
57 #define NV_TYPE_NUMBER 3
58 #define NV_TYPE_STRING 4
59 #define NV_TYPE_NVLIST 5
60 #define NV_TYPE_DESCRIPTOR 6
61 #define NV_TYPE_BINARY 7
64 * Perform case-insensitive lookups of provided names.
66 #define NV_FLAG_IGNORE_CASE 0x01
68 #if defined(_KERNEL) && defined(MALLOC_DECLARE)
69 MALLOC_DECLARE(M_NVLIST);
74 nvlist_t *nvlist_create(int flags);
75 void nvlist_destroy(nvlist_t *nvl);
76 int nvlist_error(const nvlist_t *nvl);
77 bool nvlist_empty(const nvlist_t *nvl);
78 int nvlist_flags(const nvlist_t *nvl);
79 void nvlist_set_error(nvlist_t *nvl, int error);
81 nvlist_t *nvlist_clone(const nvlist_t *nvl);
84 void nvlist_dump(const nvlist_t *nvl, int fd);
85 void nvlist_fdump(const nvlist_t *nvl, FILE *fp);
88 size_t nvlist_size(const nvlist_t *nvl);
89 void *nvlist_pack(const nvlist_t *nvl, size_t *sizep);
90 nvlist_t *nvlist_unpack(const void *buf, size_t size);
92 int nvlist_send(int sock, const nvlist_t *nvl);
93 nvlist_t *nvlist_recv(int sock);
94 nvlist_t *nvlist_xfer(int sock, nvlist_t *nvl);
96 const char *nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep);
98 const nvlist_t *nvlist_get_parent(const nvlist_t *nvl, void **cookiep);
101 * The nvlist_exists functions check if the given name (optionally of the given
102 * type) exists on nvlist.
105 bool nvlist_exists(const nvlist_t *nvl, const char *name);
106 bool nvlist_exists_type(const nvlist_t *nvl, const char *name, int type);
108 bool nvlist_exists_null(const nvlist_t *nvl, const char *name);
109 bool nvlist_exists_bool(const nvlist_t *nvl, const char *name);
110 bool nvlist_exists_number(const nvlist_t *nvl, const char *name);
111 bool nvlist_exists_string(const nvlist_t *nvl, const char *name);
112 bool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name);
114 bool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name);
116 bool nvlist_exists_binary(const nvlist_t *nvl, const char *name);
119 * The nvlist_add functions add the given name/value pair.
120 * If a pointer is provided, nvlist_add will internally allocate memory for the
121 * given data (in other words it won't consume provided buffer).
124 void nvlist_add_null(nvlist_t *nvl, const char *name);
125 void nvlist_add_bool(nvlist_t *nvl, const char *name, bool value);
126 void nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value);
127 void nvlist_add_string(nvlist_t *nvl, const char *name, const char *value);
128 void nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4);
129 #ifdef _VA_LIST_DECLARED
130 void nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0);
132 void nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value);
134 void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value);
136 void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size);
139 * The nvlist_move functions add the given name/value pair.
140 * The functions consumes provided buffer.
143 void nvlist_move_string(nvlist_t *nvl, const char *name, char *value);
144 void nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value);
146 void nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value);
148 void nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size);
151 * The nvlist_get functions returns value associated with the given name.
152 * If it returns a pointer, the pointer represents internal buffer and should
153 * not be freed by the caller.
156 bool nvlist_get_bool(const nvlist_t *nvl, const char *name);
157 uint64_t nvlist_get_number(const nvlist_t *nvl, const char *name);
158 const char *nvlist_get_string(const nvlist_t *nvl, const char *name);
159 const nvlist_t *nvlist_get_nvlist(const nvlist_t *nvl, const char *name);
161 int nvlist_get_descriptor(const nvlist_t *nvl, const char *name);
163 const void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep);
166 * The nvlist_take functions returns value associated with the given name and
167 * remove the given entry from the nvlist.
168 * The caller is responsible for freeing received data.
171 bool nvlist_take_bool(nvlist_t *nvl, const char *name);
172 uint64_t nvlist_take_number(nvlist_t *nvl, const char *name);
173 char *nvlist_take_string(nvlist_t *nvl, const char *name);
174 nvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name);
176 int nvlist_take_descriptor(nvlist_t *nvl, const char *name);
178 void *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep);
181 * The nvlist_free functions removes the given name/value pair from the nvlist
182 * and frees memory associated with it.
185 void nvlist_free(nvlist_t *nvl, const char *name);
186 void nvlist_free_type(nvlist_t *nvl, const char *name, int type);
188 void nvlist_free_null(nvlist_t *nvl, const char *name);
189 void nvlist_free_bool(nvlist_t *nvl, const char *name);
190 void nvlist_free_number(nvlist_t *nvl, const char *name);
191 void nvlist_free_string(nvlist_t *nvl, const char *name);
192 void nvlist_free_nvlist(nvlist_t *nvl, const char *name);
194 void nvlist_free_descriptor(nvlist_t *nvl, const char *name);
196 void nvlist_free_binary(nvlist_t *nvl, const char *name);