hostdb

Name

hostdb -- Host Database, to associate persistant data with a connection.

Synopsis



struct      hostdb;
hostdb_t*   hostdb_search                   (const struct ip *ip);
hostdb_t*   hostdb_new                      (const struct ip *ip);
void        hostdb_del                      (hostdb_t *h,
                                             unsigned int pid);
#define     hostdb_get_plugin_data          (h, pid)
#define     hostdb_set_plugin_data          (h, pid, data)

Description

This interface permit to associate data with a connection, this allow Detection Plugin to easily maintain persistant information accross the various packet of a given connection. A hostdb entry representating a connection is shared accross plugin.

Here is an exemple about how to use the hostdb database:

Using the Host Database

static void handle_connection(struct ip *ipcur)
{
        hostdb_t *hdb;
	host_data_t *ptr;

        hdb = hostdb_search(ipcur);
        if ( ! hdb ) {
		/*
		 * No entry for this connection exist yet.
		 */
		hdb = hostdb_new(ipcur);
		if ( ! hdb )
			return;

		ptr = create_host_data(ipcur);
		if ( ! ptr ) 
		        return;

		hostdb_set_plugin_data(hdb, plug_id, (unsigned long) ptr);
        } else {
		ptr = hostdb_get_plugin_data(hdb, plug_id);
		if ( ! ptr ) {
		       	/*
			 * No data associated with this connection by this plugin (identified by plug_id).
			 */
			ptr = create_host_data(ipcur);
			if (! ptr )
			        return;

			hostdb_set_plugin_data(hdb, plug_id, (unsigned long) ptr);
	        }
	}

	[ Play with data ]
}
  

Details

struct hostdb

struct hostdb {
        const struct ip *ip;
        int key_cache;
        unsigned long *pdata;
        unsigned int refcount;
        struct _hostdb *prev;
        struct _hostdb *next;
};

const struct ip *ipThe connection identifier for this hostdb entry.
int key_cacheA cache for the hashed key, to avoid recalculating it everytime.
unsigned long *pdataAn array of pointer to plugins data.
unsigned int refcountThe number of plugins storing data with this hostdb entry.
struct _hostdb *prevList member.
struct _hostdb *nextList member.


hostdb_search ()

hostdb_t*   hostdb_search                   (const struct ip *ip);

Search for a host identified by the ip structure in the database.

ip : An ip header to match.
Returns : a pointer on the hostdb entry if host was found or NULL on error.


hostdb_new ()

hostdb_t*   hostdb_new                      (const struct ip *ip);

Create a new hostdb entry and allocate space for plugin to associate data with this entry.

ip : the ip header to create an entry for.
Returns : A new hostdb structure or NULL on error.


hostdb_del ()

void        hostdb_del                      (hostdb_t *h,
                                             unsigned int pid);

Delete a hostdb entry only if the refcount for this hostdb element is now 0.

h : The hostdb entry to delete.
pid : The id of the calling plugin.


hostdb_get_plugin_data()

#define hostdb_get_plugin_data(h, pid) (h)->pdata[(pid)]

Return a pointer on the data associated with a given hostdb entry.

h :The hostdb entry to get associated data from.
pid :The id of the plugin willing to get back it's data pointer.


hostdb_set_plugin_data()

#define hostdb_set_plugin_data(h, pid, data) (h)->pdata[(pid)] = (data); (h)->refcount++

Associate data with the given hostdb entry.

h :The hostdb entry to associate data with.
pid :The id of the plugin willing to associate data with this hostdb entry.
data :The data to associate to this hostdb entry.