1258065Spjd/*- 2258065Spjd * Copyright (c) 2009-2013 The FreeBSD Foundation 3258065Spjd * All rights reserved. 4258065Spjd * 5258065Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from 6258065Spjd * the FreeBSD Foundation. 7258065Spjd * 8258065Spjd * Redistribution and use in source and binary forms, with or without 9258065Spjd * modification, are permitted provided that the following conditions 10258065Spjd * are met: 11258065Spjd * 1. Redistributions of source code must retain the above copyright 12258065Spjd * notice, this list of conditions and the following disclaimer. 13258065Spjd * 2. Redistributions in binary form must reproduce the above copyright 14258065Spjd * notice, this list of conditions and the following disclaimer in the 15258065Spjd * documentation and/or other materials provided with the distribution. 16258065Spjd * 17258065Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18258065Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19258065Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20258065Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21258065Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22258065Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23258065Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24258065Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25258065Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26258065Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27258065Spjd * SUCH DAMAGE. 28258065Spjd * 29258065Spjd * $FreeBSD$ 30258065Spjd */ 31258065Spjd 32258065Spjd#ifndef _NV_H_ 33258065Spjd#define _NV_H_ 34258065Spjd 35258065Spjd#include <sys/cdefs.h> 36258065Spjd 37279438Srstone#ifndef _KERNEL 38258065Spjd#include <stdarg.h> 39258065Spjd#include <stdbool.h> 40258065Spjd#include <stdint.h> 41258065Spjd#include <stdio.h> 42279438Srstone#endif 43258065Spjd 44258065Spjd#ifndef _NVLIST_T_DECLARED 45258065Spjd#define _NVLIST_T_DECLARED 46258065Spjdstruct nvlist; 47258065Spjd 48258065Spjdtypedef struct nvlist nvlist_t; 49258065Spjd#endif 50258065Spjd 51258065Spjd#define NV_NAME_MAX 2048 52258065Spjd 53258065Spjd#define NV_TYPE_NONE 0 54258065Spjd 55258065Spjd#define NV_TYPE_NULL 1 56258065Spjd#define NV_TYPE_BOOL 2 57258065Spjd#define NV_TYPE_NUMBER 3 58258065Spjd#define NV_TYPE_STRING 4 59258065Spjd#define NV_TYPE_NVLIST 5 60258065Spjd#define NV_TYPE_DESCRIPTOR 6 61258065Spjd#define NV_TYPE_BINARY 7 62258065Spjd 63258065Spjd/* 64258065Spjd * Perform case-insensitive lookups of provided names. 65258065Spjd */ 66258065Spjd#define NV_FLAG_IGNORE_CASE 0x01 67258065Spjd 68279438Srstone#if defined(_KERNEL) && defined(MALLOC_DECLARE) 69279438SrstoneMALLOC_DECLARE(M_NVLIST); 70279438Srstone#endif 71279438Srstone 72279421Srstone__BEGIN_DECLS 73279421Srstone 74258065Spjdnvlist_t *nvlist_create(int flags); 75258065Spjdvoid nvlist_destroy(nvlist_t *nvl); 76258065Spjdint nvlist_error(const nvlist_t *nvl); 77258065Spjdbool nvlist_empty(const nvlist_t *nvl); 78292637Sngieint nvlist_flags(const nvlist_t *nvl); 79279434Srstonevoid nvlist_set_error(nvlist_t *nvl, int error); 80258065Spjd 81258065Spjdnvlist_t *nvlist_clone(const nvlist_t *nvl); 82258065Spjd 83279438Srstone#ifndef _KERNEL 84258065Spjdvoid nvlist_dump(const nvlist_t *nvl, int fd); 85258065Spjdvoid nvlist_fdump(const nvlist_t *nvl, FILE *fp); 86279438Srstone#endif 87258065Spjd 88258065Spjdsize_t nvlist_size(const nvlist_t *nvl); 89258065Spjdvoid *nvlist_pack(const nvlist_t *nvl, size_t *sizep); 90258065Spjdnvlist_t *nvlist_unpack(const void *buf, size_t size); 91258065Spjd 92258065Spjdint nvlist_send(int sock, const nvlist_t *nvl); 93258065Spjdnvlist_t *nvlist_recv(int sock); 94258065Spjdnvlist_t *nvlist_xfer(int sock, nvlist_t *nvl); 95258065Spjd 96258065Spjdconst char *nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep); 97258065Spjd 98277921Spjdconst nvlist_t *nvlist_get_parent(const nvlist_t *nvl, void **cookiep); 99271579Spjd 100258065Spjd/* 101258065Spjd * The nvlist_exists functions check if the given name (optionally of the given 102258065Spjd * type) exists on nvlist. 103258065Spjd */ 104258065Spjd 105258065Spjdbool nvlist_exists(const nvlist_t *nvl, const char *name); 106258065Spjdbool nvlist_exists_type(const nvlist_t *nvl, const char *name, int type); 107258065Spjd 108258065Spjdbool nvlist_exists_null(const nvlist_t *nvl, const char *name); 109258065Spjdbool nvlist_exists_bool(const nvlist_t *nvl, const char *name); 110258065Spjdbool nvlist_exists_number(const nvlist_t *nvl, const char *name); 111258065Spjdbool nvlist_exists_string(const nvlist_t *nvl, const char *name); 112258065Spjdbool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name); 113279438Srstone#ifndef _KERNEL 114258065Spjdbool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name); 115279438Srstone#endif 116258065Spjdbool nvlist_exists_binary(const nvlist_t *nvl, const char *name); 117258065Spjd 118258065Spjd/* 119258065Spjd * The nvlist_add functions add the given name/value pair. 120258065Spjd * If a pointer is provided, nvlist_add will internally allocate memory for the 121258065Spjd * given data (in other words it won't consume provided buffer). 122258065Spjd */ 123258065Spjd 124258065Spjdvoid nvlist_add_null(nvlist_t *nvl, const char *name); 125258065Spjdvoid nvlist_add_bool(nvlist_t *nvl, const char *name, bool value); 126258065Spjdvoid nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value); 127258065Spjdvoid nvlist_add_string(nvlist_t *nvl, const char *name, const char *value); 128258065Spjdvoid nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4); 129279438Srstone#ifdef _VA_LIST_DECLARED 130258065Spjdvoid nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0); 131279438Srstone#endif 132258065Spjdvoid nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value); 133279438Srstone#ifndef _KERNEL 134258065Spjdvoid nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value); 135279438Srstone#endif 136258065Spjdvoid nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size); 137258065Spjd 138258065Spjd/* 139258065Spjd * The nvlist_move functions add the given name/value pair. 140258065Spjd * The functions consumes provided buffer. 141258065Spjd */ 142258065Spjd 143258065Spjdvoid nvlist_move_string(nvlist_t *nvl, const char *name, char *value); 144258065Spjdvoid nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value); 145279438Srstone#ifndef _KERNEL 146258065Spjdvoid nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value); 147279438Srstone#endif 148258065Spjdvoid nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size); 149258065Spjd 150258065Spjd/* 151258065Spjd * The nvlist_get functions returns value associated with the given name. 152258065Spjd * If it returns a pointer, the pointer represents internal buffer and should 153258065Spjd * not be freed by the caller. 154258065Spjd */ 155258065Spjd 156258065Spjdbool nvlist_get_bool(const nvlist_t *nvl, const char *name); 157258065Spjduint64_t nvlist_get_number(const nvlist_t *nvl, const char *name); 158258065Spjdconst char *nvlist_get_string(const nvlist_t *nvl, const char *name); 159258065Spjdconst nvlist_t *nvlist_get_nvlist(const nvlist_t *nvl, const char *name); 160279438Srstone#ifndef _KERNEL 161258065Spjdint nvlist_get_descriptor(const nvlist_t *nvl, const char *name); 162279438Srstone#endif 163258065Spjdconst void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep); 164258065Spjd 165258065Spjd/* 166258065Spjd * The nvlist_take functions returns value associated with the given name and 167258065Spjd * remove the given entry from the nvlist. 168258065Spjd * The caller is responsible for freeing received data. 169258065Spjd */ 170258065Spjd 171258065Spjdbool nvlist_take_bool(nvlist_t *nvl, const char *name); 172258065Spjduint64_t nvlist_take_number(nvlist_t *nvl, const char *name); 173258065Spjdchar *nvlist_take_string(nvlist_t *nvl, const char *name); 174258065Spjdnvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name); 175279438Srstone#ifndef _KERNEL 176258065Spjdint nvlist_take_descriptor(nvlist_t *nvl, const char *name); 177279438Srstone#endif 178258065Spjdvoid *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep); 179258065Spjd 180258065Spjd/* 181258065Spjd * The nvlist_free functions removes the given name/value pair from the nvlist 182258065Spjd * and frees memory associated with it. 183258065Spjd */ 184258065Spjd 185258065Spjdvoid nvlist_free(nvlist_t *nvl, const char *name); 186258065Spjdvoid nvlist_free_type(nvlist_t *nvl, const char *name, int type); 187258065Spjd 188258065Spjdvoid nvlist_free_null(nvlist_t *nvl, const char *name); 189258065Spjdvoid nvlist_free_bool(nvlist_t *nvl, const char *name); 190258065Spjdvoid nvlist_free_number(nvlist_t *nvl, const char *name); 191258065Spjdvoid nvlist_free_string(nvlist_t *nvl, const char *name); 192258065Spjdvoid nvlist_free_nvlist(nvlist_t *nvl, const char *name); 193279438Srstone#ifndef _KERNEL 194258065Spjdvoid nvlist_free_descriptor(nvlist_t *nvl, const char *name); 195279438Srstone#endif 196258065Spjdvoid nvlist_free_binary(nvlist_t *nvl, const char *name); 197258065Spjd 198279421Srstone__END_DECLS 199279421Srstone 200258065Spjd#endif /* !_NV_H_ */ 201