1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009-2013 The FreeBSD Foundation
5 * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
6 * All rights reserved.
7 *
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef _NV_H_
34#define _NV_H_
35
36#include <sys/cdefs.h>
37#include <sys/_nv.h>
38
39#ifndef _KERNEL
40#include <stdarg.h>
41#include <stdbool.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <sys/nv_namespace.h>
45#endif
46
47#define NV_NAME_MAX 2048
48
49#define NV_TYPE_NONE 0
50
51#define NV_TYPE_NULL 1
52#define NV_TYPE_BOOL 2
53#define NV_TYPE_NUMBER 3
54#define NV_TYPE_STRING 4
55#define NV_TYPE_NVLIST 5
56#define NV_TYPE_DESCRIPTOR 6
57#define NV_TYPE_BINARY 7
58#define NV_TYPE_BOOL_ARRAY 8
59#define NV_TYPE_NUMBER_ARRAY 9
60#define NV_TYPE_STRING_ARRAY 10
61#define NV_TYPE_NVLIST_ARRAY 11
62#define NV_TYPE_DESCRIPTOR_ARRAY 12
63
64/*
65 * Perform case-insensitive lookups of provided names.
66 */
67#define NV_FLAG_IGNORE_CASE 0x01
68/*
69 * Names don't have to be unique.
70 */
71#define NV_FLAG_NO_UNIQUE 0x02
72
73#if defined(_KERNEL) && defined(MALLOC_DECLARE)
74MALLOC_DECLARE(M_NVLIST);
75#endif
76
77__BEGIN_DECLS
78
79nvlist_t *nvlist_create(int flags);
80void nvlist_destroy(nvlist_t *nvl);
81int nvlist_error(const nvlist_t *nvl);
82bool nvlist_empty(const nvlist_t *nvl);
83int nvlist_flags(const nvlist_t *nvl);
84void nvlist_set_error(nvlist_t *nvl, int error);
85
86nvlist_t *nvlist_clone(const nvlist_t *nvl);
87
88#ifndef _KERNEL
89void nvlist_dump(const nvlist_t *nvl, int fd);
90void nvlist_fdump(const nvlist_t *nvl, FILE *fp);
91#endif
92
93size_t nvlist_size(const nvlist_t *nvl);
94void *nvlist_pack(const nvlist_t *nvl, size_t *sizep);
95nvlist_t *nvlist_unpack(const void *buf, size_t size, int flags);
96
97int nvlist_send(int sock, const nvlist_t *nvl);
98nvlist_t *nvlist_recv(int sock, int flags);
99nvlist_t *nvlist_xfer(int sock, nvlist_t *nvl, int flags);
100
101const char *nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep);
102
103const nvlist_t *nvlist_get_parent(const nvlist_t *nvl, void **cookiep);
104
105const nvlist_t *nvlist_get_array_next(const nvlist_t *nvl);
106bool nvlist_in_array(const nvlist_t *nvl);
107
108const nvlist_t *nvlist_get_pararr(const nvlist_t *nvl, void **cookiep);
109
110/*
111 * The nvlist_exists functions check if the given name (optionally of the given
112 * type) exists on nvlist.
113 */
114
115bool nvlist_exists(const nvlist_t *nvl, const char *name);
116bool nvlist_exists_type(const nvlist_t *nvl, const char *name, int type);
117
118bool nvlist_exists_null(const nvlist_t *nvl, const char *name);
119bool nvlist_exists_bool(const nvlist_t *nvl, const char *name);
120bool nvlist_exists_number(const nvlist_t *nvl, const char *name);
121bool nvlist_exists_string(const nvlist_t *nvl, const char *name);
122bool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name);
123bool nvlist_exists_binary(const nvlist_t *nvl, const char *name);
124bool nvlist_exists_bool_array(const nvlist_t *nvl, const char *name);
125bool nvlist_exists_number_array(const nvlist_t *nvl, const char *name);
126bool nvlist_exists_string_array(const nvlist_t *nvl, const char *name);
127bool nvlist_exists_nvlist_array(const nvlist_t *nvl, const char *name);
128#ifndef _KERNEL
129bool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name);
130bool nvlist_exists_descriptor_array(const nvlist_t *nvl, const char *name);
131#endif
132
133/*
134 * The nvlist_add functions add the given name/value pair.
135 * If a pointer is provided, nvlist_add will internally allocate memory for the
136 * given data (in other words it won't consume provided buffer).
137 */
138
139void nvlist_add_null(nvlist_t *nvl, const char *name);
140void nvlist_add_bool(nvlist_t *nvl, const char *name, bool value);
141void nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value);
142void nvlist_add_string(nvlist_t *nvl, const char *name, const char *value);
143void nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4);
144#if !defined(_KERNEL) || defined(_VA_LIST_DECLARED)
145void nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0);
146#endif
147void nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value);
148void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size);
149void nvlist_add_bool_array(nvlist_t *nvl, const char *name, const bool *value, size_t nitems);
150void nvlist_add_number_array(nvlist_t *nvl, const char *name, const uint64_t *value, size_t nitems);
151void nvlist_add_string_array(nvlist_t *nvl, const char *name, const char * const *value, size_t nitems);
152void nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, const nvlist_t * const *value, size_t nitems);
153#ifndef _KERNEL
154void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value);
155void nvlist_add_descriptor_array(nvlist_t *nvl, const char *name, const int *value, size_t nitems);
156#endif
157
158void nvlist_append_bool_array(nvlist_t *nvl, const char *name, const bool value);
159void nvlist_append_number_array(nvlist_t *nvl, const char *name, const uint64_t value);
160void nvlist_append_string_array(nvlist_t *nvl, const char *name, const char * const value);
161void nvlist_append_nvlist_array(nvlist_t *nvl, const char *name, const nvlist_t * const value);
162#ifndef _KERNEL
163void nvlist_append_descriptor_array(nvlist_t *nvl, const char *name, int value);
164#endif
165
166/*
167 * The nvlist_move functions add the given name/value pair.
168 * The functions consumes provided buffer.
169 */
170
171void nvlist_move_string(nvlist_t *nvl, const char *name, char *value);
172void nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value);
173void nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size);
174void nvlist_move_bool_array(nvlist_t *nvl, const char *name, bool *value, size_t nitems);
175void nvlist_move_string_array(nvlist_t *nvl, const char *name, char **value, size_t nitems);
176void nvlist_move_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **value, size_t nitems);
177void nvlist_move_number_array(nvlist_t *nvl, const char *name, uint64_t *value, size_t nitems);
178#ifndef _KERNEL
179void nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value);
180void nvlist_move_descriptor_array(nvlist_t *nvl, const char *name, int *value, size_t nitems);
181#endif
182
183/*
184 * The nvlist_get functions returns value associated with the given name.
185 * If it returns a pointer, the pointer represents internal buffer and should
186 * not be freed by the caller.
187 */
188
189bool nvlist_get_bool(const nvlist_t *nvl, const char *name);
190uint64_t nvlist_get_number(const nvlist_t *nvl, const char *name);
191const char *nvlist_get_string(const nvlist_t *nvl, const char *name);
192const nvlist_t *nvlist_get_nvlist(const nvlist_t *nvl, const char *name);
193const void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep);
194const bool *nvlist_get_bool_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
195const uint64_t *nvlist_get_number_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
196const char * const *nvlist_get_string_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
197const nvlist_t * const *nvlist_get_nvlist_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
198#ifndef _KERNEL
199int nvlist_get_descriptor(const nvlist_t *nvl, const char *name);
200const int *nvlist_get_descriptor_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
201#endif
202
203/*
204 * The nvlist_take functions returns value associated with the given name and
205 * remove the given entry from the nvlist.
206 * The caller is responsible for freeing received data.
207 */
208
209bool nvlist_take_bool(nvlist_t *nvl, const char *name);
210uint64_t nvlist_take_number(nvlist_t *nvl, const char *name);
211char *nvlist_take_string(nvlist_t *nvl, const char *name);
212nvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name);
213void *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep);
214bool *nvlist_take_bool_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
215uint64_t *nvlist_take_number_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
216char **nvlist_take_string_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
217nvlist_t **nvlist_take_nvlist_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
218#ifndef _KERNEL
219int nvlist_take_descriptor(nvlist_t *nvl, const char *name);
220int *nvlist_take_descriptor_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
221#endif
222
223/*
224 * The nvlist_free functions removes the given name/value pair from the nvlist
225 * and frees memory associated with it.
226 */
227
228void nvlist_free(nvlist_t *nvl, const char *name);
229void nvlist_free_type(nvlist_t *nvl, const char *name, int type);
230
231void nvlist_free_null(nvlist_t *nvl, const char *name);
232void nvlist_free_bool(nvlist_t *nvl, const char *name);
233void nvlist_free_number(nvlist_t *nvl, const char *name);
234void nvlist_free_string(nvlist_t *nvl, const char *name);
235void nvlist_free_nvlist(nvlist_t *nvl, const char *name);
236void nvlist_free_binary(nvlist_t *nvl, const char *name);
237void nvlist_free_bool_array(nvlist_t *nvl, const char *name);
238void nvlist_free_number_array(nvlist_t *nvl, const char *name);
239void nvlist_free_string_array(nvlist_t *nvl, const char *name);
240void nvlist_free_nvlist_array(nvlist_t *nvl, const char *name);
241void nvlist_free_binary_array(nvlist_t *nvl, const char *name);
242#ifndef _KERNEL
243void nvlist_free_descriptor(nvlist_t *nvl, const char *name);
244void nvlist_free_descriptor_array(nvlist_t *nvl, const char *name);
245#endif
246
247__END_DECLS
248
249#endif /* !_NV_H_ */