Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

hcache.c File Reference


Detailed Description

Host cache management.

Author:
Raphael Manfredi

Richard Eckart

Date:
2002-2003

Todo:
TODO:

#include "common.h"
#include "hosts.h"
#include "hcache.h"
#include "pcache.h"
#include "nodes.h"
#include "settings.h"
#include "bogons.h"
#include "hostiles.h"
#include "lib/bg.h"
#include "lib/file.h"
#include "lib/hashlist.h"
#include "lib/tm.h"
#include "lib/walloc.h"
#include "if/gnet_property.h"
#include "if/gnet_property_priv.h"
#include "lib/override.h"

Data Structures

struct  hostcache_entry
 An entry within the hostcache. More...

struct  hostcache
 A hostcache table. More...

struct  read_ctx

Defines

#define MIN_RESERVE_SIZE   1024 /**< we'd like that many pongs in reserve */
 we'd like that many pongs in reserve

#define NO_METADATA   GINT_TO_POINTER(1)
 No metadata for host.

#define HALF_PRUNE(x)
#define READ_MAGIC   0x3d00003d
#define HOST_READ_CNT   20 /**< Amount of hosts to read each tick */
 Amount of hosts to read each tick.


Typedefs

typedef hostcache_entry hostcache_entry_t
 An entry within the hostcache.

typedef hostcache hostcache_t
 A hostcache table.


Enumerations

enum  { HCACHE_ALREADY_CONNECTED, HCACHE_INVALID_HOST, HCACHE_LOCAL_INSTANCE, HCACHE_STATS_MAX }

Functions

 RCSID ("$Id:hcache.c, v 1.31 2005/11/09 19:16:53 cbiere Exp $")
void start_mass_update (hostcache_t *hc)
 Initiate mass update of host cache.

void stop_mass_update (hostcache_t *hc)
 End mass update of host cache.

void hcache_update_low_on_pongs (void)
hostcache_entry_thce_alloc (void)
void hce_free (struct hostcache_entry *hce)
void hcache_dump_info (const struct hostcache *hc, const gchar *what)
 Output contents information about a hostcache.

gboolean hcache_ht_get (const host_addr_t addr, guint16 port, gnet_host_t **h, hostcache_entry_t **e)
 Get information about the host entry, both the host and the metadata.

hostcache_entry_thcache_ht_add (hcache_type_t type, gnet_host_t *host)
 Add host to the hash table host cache.

void hcache_ht_remove (gnet_host_t *host)
 Remove host from the hash table host cache.

hostcache_entry_thcache_get_metadata (gnet_host_t *host)
 Get metadata for host.

gboolean hcache_node_is_bad (const host_addr_t addr)
void hcache_move_entries (hostcache_t *to, hostcache_t *from)
 Move entries from one hostcache to another.

gboolean hcache_require_caught (hostcache_t *hc)
 Make sure we have some host available in HCACHE_FRESH_ANY and HCACHE_FRESH_ULTRA.

void hcache_unlink_host (hostcache_t *hc, gnet_host_t *host)
 Remove host from a hostcache.

const gchar * hcache_type_to_string (hcache_type_t type)
 Convert host cache type to string.

const gchar * host_type_to_string (host_type_t type)
 Convert host type to string.

gint32 hcache_slots_left (hcache_type_t type)
gboolean hcache_add (hcache_type_t type, const host_addr_t addr, guint16 port, const gchar *what)
 Register a host.

gboolean hcache_add_caught (host_type_t type, const host_addr_t addr, guint16 port, const gchar *what)
 Add a caught (fresh) host to the right list depending on the host type.

gboolean hcache_add_valid (host_type_t type, const host_addr_t addr, guint16 port, const gchar *what)
 Add a valid host to the right list depending on the host type.

void hcache_remove (gnet_host_t *h)
 Remove host from cache.

gboolean hcache_is_low (host_type_t type)
 Do we have less that our mimumum amount of hosts in the cache?

void hcache_remove_all (hostcache_t *hc)
 Remove all entries from hostcache.

void hcache_clear_host_type (host_type_t type)
 Clear the whole host cache for a host type and the pong cache of the same type.

void hcache_clear (hcache_type_t type)
 Clear the whole host cache but does not clear the pong caches.

gint hcache_size (host_type_t type)
guint32 hcache_expire_cache (hostcache_t *hc, time_t now)
 Expire hosts from a single hostlist in a hostcache.

guint32 hcache_expire_all (time_t now)
 Expire hosts from the HL_BUSY, HL_TIMEOUT and HL_UNSTABLE lists and remove them from the hosts hashtable too.

void hcache_prune (hcache_type_t type)
 Remove hosts that exceed our maximum.

gint hcache_fill_caught_array (host_type_t type, gnet_host_t *hosts, gint hcount)
 Fill `hosts', an array of `hcount' hosts already allocated with at most `hcount' hosts from out caught list, without removing those hosts from the list.

gboolean hcache_find_nearby (host_type_t type, host_addr_t *addr, guint16 *port)
 Finds a host in either the pong_cache or the host_cache that is in one of the local networks.

void hcache_get_caught (host_type_t type, host_addr_t *addr, guint16 *port)
 Get host IP/port information from our caught host list, or from the recent pong cache, in alternance.

hostcache_thcache_alloc (hcache_type_t type, gnet_property_t reading, gnet_property_t catcher, gchar *name)
 Allocate hostcache of type `type'.

void hcache_free (hostcache_t *hc)
 Dispose of the hostcache.

void read_ctx_free (gpointer u)
 Dispose of the read context.

void read_done (hostcache_t *hc)
 Read is finished.

bgret_t read_step (gpointer unused_h, gpointer u, gint ticks)
 One reading step.

void bg_reader_done (gpointer unused_h, gpointer ctx, bgstatus_t unused_status, gpointer unused_arg)
 Invoked when the task is completed.

void hcache_retrieve (hostcache_t *hc, const gchar *filename)
 Loads caught hosts from text file.

void hcache_write (FILE *f, hostcache_t *hc)
 Write all data from cache to supplied file.

void hcache_store (hcache_type_t type, const gchar *filename, hcache_type_t extra)
 Persist hostcache to disk.

void hcache_get_stats (hcache_stats_t *s)
 Get statistical information about the caches.

void hcache_timer (time_t now)
 Host cache timer.

void hcache_init (void)
 Initialize host caches.

void hcache_retrieve_all (void)
 Load hostcache data from disk.

void hcache_store_if_dirty (host_type_t type)
 Save hostcache data to disk, for the relevant host type.

gboolean hcache_read_finished (void)
 Check whether the host caches are completely read.

void hcache_shutdown (void)
 Shutdown host caches.

void hcache_close (void)
 Destroy all host caches.


Variables

hostcache_tcaches [HCACHE_MAX]
gboolean hcache_close_running = FALSE
const gchar *const  names [HCACHE_MAX]
 Names of the host caches.

const gchar *const  host_type_names [HOST_MAX]
gpointer bg_reader [HCACHE_MAX]
guint stats [HCACHE_STATS_MAX]
GHashTable * ht_known_hosts = NULL
 Hashtable: IP/Port -> Metadata.


Define Documentation

#define HALF_PRUNE  ) 
 

Value:

do {        \
    if (hc->host_count < caches[x]->host_count) \
        hc = caches[x];         \
} while (0)

#define HOST_READ_CNT   20 /**< Amount of hosts to read each tick */
 

Amount of hosts to read each tick.

#define MIN_RESERVE_SIZE   1024 /**< we'd like that many pongs in reserve */
 

we'd like that many pongs in reserve

#define NO_METADATA   GINT_TO_POINTER(1)
 

No metadata for host.

#define READ_MAGIC   0x3d00003d
 


Typedef Documentation

typedef struct hostcache_entry hostcache_entry_t
 

An entry within the hostcache.

We don't really store the IP/port, as those are stored in the key of hash table recording all known hosts. Rather, we store "metadata" about the host.

typedef struct hostcache hostcache_t
 

A hostcache table.


Enumeration Type Documentation

anonymous enum
 

Enumeration values:
HCACHE_ALREADY_CONNECTED 
HCACHE_INVALID_HOST 
HCACHE_LOCAL_INSTANCE 
HCACHE_STATS_MAX 


Function Documentation

void bg_reader_done gpointer  unused_h,
gpointer  ctx,
bgstatus_t  unused_status,
gpointer  unused_arg
[static]
 

Invoked when the task is completed.

gboolean hcache_add hcache_type_t  type,
const host_addr_t  addr,
guint16  port,
const gchar *  what
 

Register a host.

If a host is already on the known hosts hashtable, it will not be registered. Otherwise it will be added to the hashtable of known hosts and added to one of the host lists as indicated by the "list" parameter. Sanity checks are only applied when the host is added to HL_CAUGHT, since when a host is added to any of the other lists it must have been in HL_CAUGHT or in the pong-cache before.

Returns:
TRUE when IP/port passed sanity checks, regardless of whether it was added to the cache. (See above)

gboolean hcache_add_caught host_type_t  type,
const host_addr_t  addr,
guint16  port,
const gchar *  what
 

Add a caught (fresh) host to the right list depending on the host type.

gboolean hcache_add_valid host_type_t  type,
const host_addr_t  addr,
guint16  port,
const gchar *  what
 

Add a valid host to the right list depending on the host type.

hostcache_t* hcache_alloc hcache_type_t  type,
gnet_property_t  reading,
gnet_property_t  catcher,
gchar *  name
[static]
 

Allocate hostcache of type `type'.

The `incache' property is what needs to be updated so that the GUI can display the proper amount of hosts we currently hold.

The `maxhosts' variable is the pointer to the variable giving the maximum amount of hosts we can store.

The `reading' variable is the property to update to signal whether we're reading the persisted file.

void hcache_clear hcache_type_t  type  ) 
 

Clear the whole host cache but does not clear the pong caches.

Use this to clear the "bad" host caches.

void hcache_clear_host_type host_type_t  type  ) 
 

Clear the whole host cache for a host type and the pong cache of the same type.

Use this to clear the "ultra" and "any" host caches.

void hcache_close void   ) 
 

Destroy all host caches.

void hcache_dump_info const struct hostcache hc,
const gchar *  what
[static]
 

Output contents information about a hostcache.

guint32 hcache_expire_all time_t  now  )  [static]
 

Expire hosts from the HL_BUSY, HL_TIMEOUT and HL_UNSTABLE lists and remove them from the hosts hashtable too.

Returns:
total number of expired entries

guint32 hcache_expire_cache hostcache_t hc,
time_t  now
[static]
 

Expire hosts from a single hostlist in a hostcache.

Also removes it from the host hashtable.

Returns:
total number of expired entries

gint hcache_fill_caught_array host_type_t  type,
gnet_host_t hosts,
gint  hcount
 

Fill `hosts', an array of `hcount' hosts already allocated with at most `hcount' hosts from out caught list, without removing those hosts from the list.

Returns:
amount of hosts filled

gboolean hcache_find_nearby host_type_t  type,
host_addr_t addr,
guint16 *  port
 

Finds a host in either the pong_cache or the host_cache that is in one of the local networks.

Returns:
TRUE if host is found

void hcache_free hostcache_t hc  )  [static]
 

Dispose of the hostcache.

void hcache_get_caught host_type_t  type,
host_addr_t addr,
guint16 *  port
 

Get host IP/port information from our caught host list, or from the recent pong cache, in alternance.

hostcache_entry_t* hcache_get_metadata gnet_host_t host  )  [static]
 

Get metadata for host.

Returns:
NULL if host was not found, NO_METADATA if no metadata was stored or a pointer to a hostcache_entry struct which hold the metadata.

void hcache_get_stats hcache_stats_t s  ) 
 

Get statistical information about the caches.

Parameters:
s must point to an hcache_stats_t[HCACHE_MAX] array.

hostcache_entry_t* hcache_ht_add hcache_type_t  type,
gnet_host_t host
[static]
 

Add host to the hash table host cache.

Also creates a metadata struct unless the host was added to HL_CAUGHT in which case we cannot know anything about the host. Yet we cannot assert that HL_CAUGHT never contains a host with metadata because when HL_CAUGHT becomes empty, move all hosts from HL_VALID to HL_CAUGHT. We can however assert that any host which does not have metadata is in HL_CAUGHT.

Returns:
Pointer to metadata struct for the added host or NO_METADATA if no metadata was added.

gboolean hcache_ht_get const host_addr_t  addr,
guint16  port,
gnet_host_t **  h,
hostcache_entry_t **  e
[static]
 

Get information about the host entry, both the host and the metadata.

Parameters:
addr the address of the host
port the port used by the host
h filled with the host entry in the table
e filled with the meta data of the host, as held in table
Returns:
FALSE if entry was not found in the cache.

void hcache_ht_remove gnet_host_t host  )  [static]
 

Remove host from the hash table host cache.

void hcache_init void   ) 
 

Initialize host caches.

gboolean hcache_is_low host_type_t  type  ) 
 

Do we have less that our mimumum amount of hosts in the cache?

void hcache_move_entries hostcache_t to,
hostcache_t from
[static]
 

Move entries from one hostcache to another.

This only works if the target hostcache is empty.

gboolean hcache_node_is_bad const host_addr_t  addr  ) 
 

Returns:
TRUE if the host is in one of the "bad hosts" caches.

void hcache_prune hcache_type_t  type  ) 
 

Remove hosts that exceed our maximum.

This can be called on HCACHE_FRESH_ANY and on HCACHE_FRESH_ULTRA.

If too many hosts are in the cache, then it will prune the HCACHE_FRESH_XXX list. Only after HCACHE_FRESH_XXX is empty HCACHE_VALID_XXX will be moved to HCACHE_FRESH_XXX and then it is purged.

gboolean hcache_read_finished void   ) 
 

Check whether the host caches are completely read.

This should be used before contacting a GWebCache at start-up time.

Returns:
TRUE if the host caches have been completely loaded and FALSE if the loading is still in process.

void hcache_remove gnet_host_t h  )  [static]
 

Remove host from cache.

After removing hcache_require_caught is called.

void hcache_remove_all hostcache_t hc  )  [static]
 

Remove all entries from hostcache.

gboolean hcache_require_caught hostcache_t hc  )  [static]
 

Make sure we have some host available in HCACHE_FRESH_ANY and HCACHE_FRESH_ULTRA.

If one of the both is empty, all hosts from HCACHE_VALID_XXX are moved to HCACHE_VALID_XXX. When caught on other hcaches than FRESH_ANY and FRESH_ULTRA nothing happens.

Returns:
TRUE if host are available in hc after the call.

void hcache_retrieve hostcache_t hc,
const gchar *  filename
[static]
 

Loads caught hosts from text file.

void hcache_retrieve_all void   ) 
 

Load hostcache data from disk.

void hcache_shutdown void   ) 
 

Shutdown host caches.

gint hcache_size host_type_t  type  ) 
 

Returns:
the amount of hosts in the cache.

gint32 hcache_slots_left hcache_type_t  type  )  [static]
 

Returns:
the number of slots which can be added to the given type.
Note:
Several types share common pools. Adding a host of one type may affect the number of slots left on other types.

void hcache_store hcache_type_t  type,
const gchar *  filename,
hcache_type_t  extra
[static]
 

Persist hostcache to disk.

If `extra' is not HCACHE_NONE, it is appended after the dump of `type'.

void hcache_store_if_dirty host_type_t  type  ) 
 

Save hostcache data to disk, for the relevant host type.

void hcache_timer time_t  now  ) 
 

Host cache timer.

const gchar* hcache_type_to_string hcache_type_t  type  ) 
 

Convert host cache type to string.

void hcache_unlink_host hostcache_t hc,
gnet_host_t host
[static]
 

Remove host from a hostcache.

void hcache_update_low_on_pongs void   )  [static]
 

void hcache_write FILE *  f,
hostcache_t hc
[static]
 

Write all data from cache to supplied file.

hostcache_entry_t* hce_alloc void   )  [static]
 

void hce_free struct hostcache_entry hce  )  [static]
 

const gchar* host_type_to_string host_type_t  type  ) 
 

Convert host type to string.

RCSID "$Id:hcache.  c,
v 1.31 2005/11/09 19:16:53 cbiere Exp $" 
 

void read_ctx_free gpointer  u  )  [static]
 

Dispose of the read context.

void read_done hostcache_t hc  )  [static]
 

Read is finished.

bgret_t read_step gpointer  unused_h,
gpointer  u,
gint  ticks
[static]
 

One reading step.

void start_mass_update hostcache_t hc  )  [static]
 

Initiate mass update of host cache.

While mass updates are in progress, the hosts_in_catcher property will not be updated.

void stop_mass_update hostcache_t hc  )  [static]
 

End mass update of host cache.

If a hostcache has already been freed when this function is called, it will not tigger a property update for that hostcache and all of its group (ANY, ULTRA, BAD).


Variable Documentation

gpointer bg_reader[HCACHE_MAX] [static]
 

hostcache_t* caches[HCACHE_MAX] [static]
 

gboolean hcache_close_running = FALSE [static]
 

const gchar* const host_type_names[HOST_MAX] [static]
 

Initial value:

 {
    "any",
    "ultra",
}

GHashTable* ht_known_hosts = NULL [static]
 

Hashtable: IP/Port -> Metadata.

const gchar* const names[HCACHE_MAX] [static]
 

Initial value:

 {
    "fresh regular",
    "valid regular",
    "fresh ultra",
    "valid ultra",
    "timeout",
    "busy",
    "unstable",
    "none",
}
Names of the host caches.

Note:
Has to be in the same order as in the hcache_type_t definition in gnet_nodes.h.

guint stats[HCACHE_STATS_MAX] [static]
 


Generated on Sun Feb 12 10:50:03 2006 for Gtk-Gnutella by doxygen 1.3.6