From a2d3bedf1f1548c49dd3ad68b4d9719f61a27c3a Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sun, 15 Dec 2019 23:26:02 +0000 Subject: [peerdist] Allow for the use of a hosted cache server Allow a PeerDist hosted cache server to be specified via the ${peerhost} setting, e.g.: # Use 192.168.0.1 as hosted cache server set peerhost 192.168.0.1 Note that this simply treats the hosted cache server as a permanently discovered peer for all segments. Signed-off-by: Michael Brown --- src/net/peerdisc.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/src/net/peerdisc.c b/src/net/peerdisc.c index 20ac2427..55e3f7fa 100644 --- a/src/net/peerdisc.c +++ b/src/net/peerdisc.c @@ -37,6 +37,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include #include #include +#include #include #include @@ -72,6 +73,9 @@ static LIST_HEAD ( peerdisc_segments ); */ unsigned int peerdisc_timeout_secs = PEERDISC_DEFAULT_TIMEOUT_SECS; +/** Hosted cache server */ +static char *peerhost; + static struct peerdisc_segment * peerdisc_find ( const char *id ); static int peerdisc_discovered ( struct peerdisc_segment *segment, const char *location ); @@ -442,6 +446,7 @@ static struct peerdisc_segment * peerdisc_create ( const char *id ) { char *uuid_copy; char *id_copy; unsigned int i; + int rc; /* Generate a random message UUID. This does not require high * quality randomness. @@ -458,7 +463,7 @@ static struct peerdisc_segment * peerdisc_create ( const char *id ) { /* Allocate and initialise structure */ segment = zalloc ( sizeof ( *segment ) + id_len + uuid_len ); if ( ! segment ) - return NULL; + goto err_alloc; id_copy = ( ( ( void * ) segment ) + sizeof ( *segment ) ); memcpy ( id_copy, id, id_len ); uuid_copy = ( ( ( void * ) id_copy ) + id_len ); @@ -469,14 +474,30 @@ static struct peerdisc_segment * peerdisc_create ( const char *id ) { INIT_LIST_HEAD ( &segment->peers ); INIT_LIST_HEAD ( &segment->clients ); timer_init ( &segment->timer, peerdisc_expired, &segment->refcnt ); - DBGC2 ( segment, "PEERDISC %p discovering %s\n", segment, segment->id ); - /* Start discovery timer */ - start_timer_nodelay ( &segment->timer ); + /* Add hosted cache server or initiate discovery */ + if ( peerhost ) { + + /* Add hosted cache server to list of peers */ + if ( ( rc = peerdisc_discovered ( segment, peerhost ) ) != 0 ) + goto err_peerhost; + + } else { + + /* Start discovery timer */ + start_timer_nodelay ( &segment->timer ); + DBGC2 ( segment, "PEERDISC %p discovering %s\n", + segment, segment->id ); + } /* Add to list of segments, transfer reference to list, and return */ list_add_tail ( &segment->list, &peerdisc_segments ); return segment; + + err_peerhost: + ref_put ( &segment->refcnt ); + err_alloc: + return NULL; } /** @@ -579,3 +600,43 @@ void peerdisc_close ( struct peerdisc_client *peerdisc ) { if ( list_empty ( &peerdisc_segments ) ) peerdisc_socket_close ( 0 ); } + +/****************************************************************************** + * + * Settings + * + ****************************************************************************** + */ + +/** PeerDist hosted cache server setting */ +const struct setting peerhost_setting __setting ( SETTING_MISC, peerhost ) = { + .name = "peerhost", + .description = "PeerDist hosted cache", + .type = &setting_type_string, +}; + +/** + * Apply PeerDist discovery settings + * + * @ret rc Return status code + */ +static int apply_peerdisc_settings ( void ) { + + /* Free any existing hosted cache server */ + free ( peerhost ); + peerhost = NULL; + + /* Fetch hosted cache server */ + fetch_string_setting_copy ( NULL, &peerhost_setting, &peerhost ); + if ( peerhost ) { + DBGC ( &peerhost, "PEERDISC using hosted cache %s\n", + peerhost ); + } + + return 0; +} + +/** PeerDist discovery settings applicator */ +struct settings_applicator peerdisc_applicator __settings_applicator = { + .apply = apply_peerdisc_settings, +}; -- cgit v1.2.3-55-g7522