OpenVAS Libraries  7.0.7
Macros | Functions
kb.c File Reference
#include <stdlib.h>
#include <string.h>
#include <fnmatch.h>
#include <glib.h>
#include "arglists.h"
#include "kb.h"
#include "system_internal.h"

Macros

#define HASH_MAX   65537
 

Functions

struct kb_item ** kb_new ()
 Allocates memory for an array of kb_items with max length of HASH_MAX. More...
 
struct kb_itemkb_item_get_single (struct kb_item **kb, char *name, int type)
 READ the knowledge base. More...
 
char * kb_item_get_str (struct kb_item **kb, char *name)
 Get the value of a kb_item with type KB_TYPE_STR and name name. More...
 
int kb_item_get_int (struct kb_item **kb, char *name)
 Get the value of a kb_item with type KB_TYPE_INT and name name. More...
 
struct kb_itemkb_item_get_all (struct kb_item **kb, char *name)
 Returns a list of copies of kb_items with name name in a knowledge base. More...
 
struct kb_itemkb_item_get_pattern (struct kb_item **kb, char *expr)
 Returns a list of copies of kb_items that match a pattern. More...
 
void kb_item_get_all_free (struct kb_item *items)
 Frees a list of kb_items. More...
 
int kb_item_add_str (struct kb_item **kb, char *name, char *value)
 Adds a string to the knowledge base. In contrast to kb_item_set_str the item will not be replaced (useful for list creation). More...
 
int kb_item_set_str (struct kb_item **kb, char *name, char *value)
 
int kb_item_set_int (struct kb_item **kb, char *name, int value)
 
int kb_item_add_int (struct kb_item **kb, char *name, int value)
 
void kb_item_rm_all (struct kb_item **kb, char *name)
 

Detailed Description

Knowledge base management API.
Knowledge bases collect information and can be used to share information between NVTs.
A knowledge base is an array of knowledge base items (kb_item). An item is defined by its name and has a value (either int or char*), a type flag (indicating whether the value shall be interpreted as int or char*) and a pointer to the "next" item.
A knowledge base (kb_item**) stores single items at a position according to a hash of the items name (function mkkey). Because of that, a knowledge base has a fixed size of 65537 items and kb_items are implemented as lists.

Macro Definition Documentation

#define HASH_MAX   65537

Function Documentation

int kb_item_add_int ( struct kb_item **  kb,
char *  name,
int  value 
)
int kb_item_add_str ( struct kb_item **  kb,
char *  name,
char *  value 
)

Adds a string to the knowledge base. In contrast to kb_item_set_str the item will not be replaced (useful for list creation).

Parameters
kbThe knowledge base.
nameKey of the entry.
valueValue of the entry.
struct kb_item* kb_item_get_all ( struct kb_item **  kb,
char *  name 
)

Returns a list of copies of kb_items with name name in a knowledge base.

The result has to be freed (kb_item_get_all_free). Use kb_item_get_pattern if you want to get all items matching a pattern, rather than a single name.

Parameters
kbThe knowledge base.
nameName of the item(s) of interest.
Returns
A kb_item list (has to be freed) with kb_items of name name.
void kb_item_get_all_free ( struct kb_item items)

Frees a list of kb_items.

Can be used to free the results of querying the kb with kb_item_get_all() or kb_item_get_pattern().

Parameters
itemsThe list of kb_items to free.
int kb_item_get_int ( struct kb_item **  kb,
char *  name 
)

Get the value of a kb_item with type KB_TYPE_INT and name name.

Returns
Value of the kb_item name with type KB_TYPE_INT or -1 if it does not exist.
struct kb_item* kb_item_get_pattern ( struct kb_item **  kb,
char *  expr 
)

Returns a list of copies of kb_items that match a pattern.

The items have to be freed, e.g. with kb_item_get_all_free.

Parameters
kbThe knowledge base.
exprA pattern that can be used with fnmatch (e.g. "www/serv*").
Returns
A list of kb_items (has to be freed) whose name matches the pattern exp.
struct kb_item* kb_item_get_single ( struct kb_item **  kb,
char *  name,
int  type 
)

READ the knowledge base.

Returns
kb_item in knowledge base with name name and type type or NULL if none found.
char* kb_item_get_str ( struct kb_item **  kb,
char *  name 
)

Get the value of a kb_item with type KB_TYPE_STR and name name.

Returns
(char*) value of the kb_item name with type KB_TYPE_STR.
void kb_item_rm_all ( struct kb_item **  kb,
char *  name 
)
int kb_item_set_int ( struct kb_item **  kb,
char *  name,
int  value 
)
int kb_item_set_str ( struct kb_item **  kb,
char *  name,
char *  value 
)
struct kb_item** kb_new ( )

Allocates memory for an array of kb_items with max length of HASH_MAX.

Returns
Pointer to first item in knowledge base item array.