Current File : //usr/include/libnetcfg.h
/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 */

#ifndef	_LIBNETCFG_H
#define	_LIBNETCFG_H

#ifdef __cplusplus
extern "C" {
#endif

#include <libnvpair.h>
#include <paths.h>
#include <sys/param.h>
#include <sys/types.h>

/* Name of directory containing the door */
#define	NETCFG_DOOR_DIR		_PATH_SYSVOL "/netcfg"

/*
 * libnetcfg error codes
 */
typedef enum {
	NETCFG_SUCCESS,		/* No error occurred */
	NETCFG_EPERM,		/* Permission denied */
	NETCFG_INVALID_ARG,	/* Invalid argument */
	NETCFG_EXISTS,		/* Object exists */
	NETCFG_NOT_FOUND,	/* Object not found */
	NETCFG_WALK_HALTED,	/* Callback halted walk */
	NETCFG_ERROR_BIND,	/* Could not bind to daemon */
	NETCFG_NO_MEMORY,	/* Insufficient memory */
	NETCFG_FAILURE		/* Failure */
} netcfg_error_t;

#define	NETCFG_OBJECT_ID_LIST	"object-id-list"
#define	NETCFG_OBJECT_DB_NAME	"object-db-name"
#define	NETCFG_OBJECT_DB_ZONE	"object-db-zone"
#define	NETCFG_OBJECT_RAW_BUFS	"object-raw-bufs"

#define	NETCFG_DB_RAW_FLAG	0x00000001

#define	NETCFG_PROFILE_LEN		MAXNAMELEN

#define	NETADM_ACTIVE_PROFILE		"current-profile"
#define	S10_BRAND_DUMMY_PROFILE		"s10c-non-profile"

/*
 * Return the currently active profile.
 */
extern netcfg_error_t	netcfg_active_profile(char *, size_t);

/*
 * netcfg_*_object() functions:
 *
 * Arguments:
 *    - nvlist of object identifiers, including a NETCFG_OBJECT_DB_NAME value
 *    - flags
 *    - nvlist specifiying object properties (excluded for remove function)
 *
 * netcfg_read_object: returns the object specified in the idlist in the
 *   passed-in object list pointer.  If idlist only contains a db name,
 *   returns an nvlist with just one pair, named NETCFG_OBJECT_ID_LIST,
 *   whose value is an array of nvlists.  Each nvlist is the idlist of an
 *   object in the db.
 *
 * netcfg_update_object: writes the object identified by idlist and specified
 *   in objlist to the specified db.  Replaces an existing entry, if present.
 *
 * netcfg_rename_object: in this case, the second nvlist is not an objlist;
 *   instead, it is the new idlist.  Finds the object identified by idlist,
 *   and rewrites it in the file with its existing object list and the new
 *   idlist.
 *
 * netcfg_remove_object: removes the object identified by idlist from the db.
 *
 * netcvg_get_object_prop: reads the object identified by idlist and fetches
 *   the value[s] of the property or properties included in the object list,
 *   updating the object list with the values.  If a particular property is
 *   not present, it is removed from the object list.
 */
extern netcfg_error_t	netcfg_read_object(nvlist_t **, uint64_t, nvlist_t **);
extern netcfg_error_t	netcfg_update_object(nvlist_t **, uint64_t,
			    nvlist_t **);
extern netcfg_error_t	netcfg_rename_object(nvlist_t **, uint64_t,
			    nvlist_t **);
extern netcfg_error_t	netcfg_remove_object(nvlist_t **, uint64_t);
extern netcfg_error_t	netcfg_get_object_props(nvlist_t **, uint64_t,
			    nvlist_t **);

/*
 * netcfg_*_db() functions:
 *
 * Common arguments:
 *    - nvlist containing NETCFG_OBJECT_DB_NAME and, optionally,
 *        NETCFG_OBJECT_DB_ZONE.
 *    - flags
 *
 * netcfg_walk_db: first calls the select callback for each entry in the
 *   specified db, passing the idlist and objlist for the entry, as well
 *   as the flags passed to it.  Select function returns 0 if the callback
 *   should be called for this object, non-zero otherwise.  Select function
 *   may also return an object argument which will be passed to the callback;
 *   if this argument is not specified, the object list will be passed. To
 *   free the object argument returned by the select function, netcfg_freecb_t
 *   will be called.
 *
 * netcfg_walk_db_raw: calls the specified callback for each entry in the
 *   specified db, passing a string buffer containing the "raw" line from
 *   the db file.
 *
 * netcfg_create_db: creates the specified database, and populates it with
 *   the provided object list.  The object list should be an nvlist with one
 *   pair for each database entry.  For each pair, the name is the string
 *   equivalent of its idlist (produced by calling netcfg_idl2idstr()), and
 *   the value is the object nvlist.  If the specified database already exists,
 *   it will be overwritten.
 *
 * netcfg_destroy_db: destroys the specified database, by unlinking the file.
 *
 */
typedef int	(netcfg_walkcb_t)(void *, void *);
typedef int	(netcfg_selectcb_t)(nvlist_t *, nvlist_t *, uint64_t, void **);
typedef void	(netcfg_freecb_t)(void **);
typedef int 	(netcfg_rawcb_t)(char *, void *);

extern netcfg_error_t	netcfg_walk_db(nvlist_t **, uint64_t, netcfg_walkcb_t *,
			    void *, netcfg_selectcb_t *, netcfg_freecb_t *,
			    int *);
extern netcfg_error_t	netcfg_walk_db_raw(nvlist_t **, uint64_t,
			    netcfg_rawcb_t *, void *);
extern netcfg_error_t	netcfg_create_db(nvlist_t **, uint64_t, nvlist_t **);
extern netcfg_error_t	netcfg_destroy_db(nvlist_t **);
extern netcfg_error_t	netcfg_backup_db(nvlist_t **);
extern netcfg_error_t	netcfg_restore_db(nvlist_t **);
extern netcfg_error_t	netcfg_destroy_backup_db(nvlist_t **);

/*
 * Functions to manipulate idlists:
 *
 * netcfg_init_idlist: allocates an nvlist, and adds a NETCFG_OBJECT_DB_NAME
 *   pair to it with the specified name string.
 *
 * netcfg_add_idlist: adds the name-value pair specified by the two string
 *   arguments (name followed by value) to the given idlist.
 *
 * netcfg_idl2idstr: produces the string representation of the given idlist.
 *   Memory is allocated for the string; it is the caller's responsibility
 *   to free that memory.
 *
 * dbname_from_idlist: returns a pointer to the string value of the
 *   NETCFG_OBJECT_DB_NAME pair, if present; NULL if not.
 *
 * zoneid_from_idlist: returns a pointer to the string value of the
 *   NETCFG_OBJECT_DB_ZONE pair, if present; NULL if not.
 */
extern netcfg_error_t	netcfg_init_idlist(nvlist_t **, const char *);
extern netcfg_error_t	netcfg_add_idlist(nvlist_t *, const char *,
			    const char *);
extern netcfg_error_t	netcfg_idl2idstr(nvlist_t *, char **);
extern char		*dbname_from_idlist(nvlist_t *);
extern zoneid_t		zoneid_from_idlist(nvlist_t *);

extern netcfg_error_t	check_s10_brand(boolean_t *);

/*
 * Check if the caller is authorized to write into the given db.
 */
extern netcfg_error_t	netcfg_check_write_auths(const char *);

/*
 * All properties are stored as arrays, regardless of the number of elements.
 * These functions allow callers to extract or add one value from/to an
 * nvpair in cases where there is known to be a single value in the array.
 */
extern int	netcfg_nvl_get_one_boolean(nvlist_t *, const char *,
			    boolean_t *);
extern int	netcfg_nvl_get_one_int32(nvlist_t *, const char *, int32_t *);
extern int	netcfg_nvl_get_one_int64(nvlist_t *, const char *, int64_t *);
extern int	netcfg_nvl_get_one_uint32(nvlist_t *, const char *, uint32_t *);
extern int	netcfg_nvl_get_one_uint64(nvlist_t *, const char *, uint64_t *);
extern int	netcfg_nvl_get_one_string(nvlist_t *, const char *, char **);

extern int	netcfg_nvp_get_one_boolean(nvpair_t *, boolean_t *);
extern int	netcfg_nvp_get_one_int32(nvpair_t *, int32_t *);
extern int	netcfg_nvp_get_one_int64(nvpair_t *, int64_t *);
extern int	netcfg_nvp_get_one_uint32(nvpair_t *, uint32_t *);
extern int	netcfg_nvp_get_one_uint64(nvpair_t *, uint64_t *);
extern int	netcfg_nvp_get_one_string(nvpair_t *, char **);

extern int	netcfg_nvl_add_one_boolean(nvlist_t *, const char *, boolean_t);
extern int	netcfg_nvl_add_one_int32(nvlist_t *, const char *, int32_t);
extern int	netcfg_nvl_add_one_int64(nvlist_t *, const char *, int64_t);
extern int	netcfg_nvl_add_one_uint32(nvlist_t *, const char *, uint32_t);
extern int	netcfg_nvl_add_one_uint64(nvlist_t *, const char *, uint64_t);
extern int	netcfg_nvl_add_one_string(nvlist_t *, const char *, char *);

extern const char	*netcfg_strerror(netcfg_error_t);
extern int		netcfg_error2errno(netcfg_error_t);
extern char		*netcfg_tokenize_by_unescaped_delim(char *, char,
			    char **);
extern netcfg_error_t	netcfg_parse_ntv(char *, nvlist_t **);

#ifdef __cplusplus
}
#endif

#endif /* _LIBNETCFG_H */