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