From 80f7c875671ce7215af10e2b73c52f224b33e734 Mon Sep 17 00:00:00 2001 From: Ron Pedde Date: Wed, 24 May 2006 03:53:22 +0000 Subject: [PATCH] Make default server name based on hostname, fixing #111 --- src/conf.c | 35 ++++++++++++++++++++++++++++++++++- src/conf.h | 4 ++++ src/dispatch.c | 4 ++-- src/main.c | 3 +-- src/plugin.c | 13 ++++++++++++- 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/conf.c b/src/conf.c index 3d83622a..957a8ba2 100644 --- a/src/conf.c +++ b/src/conf.c @@ -37,6 +37,10 @@ #include #include +#ifdef HAVE_UNISTD_H +# include +#endif + #ifdef HAVE_SYS_PARAM_H # include #endif @@ -51,6 +55,11 @@ #include "webserver.h" #include "xml-rpc.h" + +#ifndef HOST_NAME_MAX +# define HOST_NAME_MAX 255 +#endif + /** Globals */ //static int ecode; static LL_HANDLE conf_main=NULL; @@ -99,7 +108,7 @@ static CONF_ELEMENTS conf_elements[] = { { 0, 0, CONF_T_STRING,"general","db_type" }, { 0, 0, CONF_T_EXISTPATH,"general","db_parms" }, /* this isn't right */ { 0, 0, CONF_T_INT,"general","debuglevel" }, - { 1, 0, CONF_T_STRING,"general","servername" }, + { 0, 0, CONF_T_STRING,"general","servername" }, { 0, 0, CONF_T_INT,"general","rescan_interval" }, { 0, 0, CONF_T_INT,"general","always_scan" }, { 0, 1, CONF_T_INT,"general","latin1_tags" }, @@ -1445,3 +1454,27 @@ int _conf_xml_dump(XMLSTRUCT *pxml, LL *pll, int sublevel, char *parent) { char *conf_get_filename(void) { return conf_main_file; } + + +/** + * this is an ugly block of crap to carry around every + * time one wants the servername. + */ +char *conf_get_servername(void) { + char *retval; + char newname[HOST_NAME_MAX + 1 + 16]; /* " Firefly Server" */ + + gethostname(newname,HOST_NAME_MAX); + retval = strchr(newname,'.'); + if(retval) *retval = '\0'; + + retval = newname; + while(*retval) { + *retval = tolower(*retval); + retval++; + } + + strcat(newname," Firefly Server"); + + return conf_alloc_string("general","servername",newname); +} diff --git a/src/conf.h b/src/conf.h index dfdeefcd..aad40ea0 100644 --- a/src/conf.h +++ b/src/conf.h @@ -59,4 +59,8 @@ extern char *conf_get_filename(void); #include "webserver.h" extern int conf_xml_dump(WS_CONNINFO *pwsc); +/* FIXME: well used config reading stuff... */ +extern char *conf_get_servername(void); + + #endif /* _CONF_H_ */ diff --git a/src/dispatch.c b/src/dispatch.c index 4e831d6f..e8ffecde 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -1480,7 +1480,7 @@ void dispatch_dbinfo(WS_CONNINFO *pwsc, DBQUERYINFO *pqi) { int count; char *servername; - servername = conf_alloc_string("general","servername","mt-daapd"); + servername = conf_get_servername(); namelen=(int) strlen(servername); current += db_dmap_add_container(current,"avdb",105 + namelen); @@ -1574,7 +1574,7 @@ void dispatch_server_info(WS_CONNINFO *pwsc, DBQUERYINFO *pqi) { int actual_length; int supports_update; - servername = conf_alloc_string("general","servername","mt-daapd"); + servername = conf_get_servername(); supports_update = conf_get_int("daap","supports_update",1); actual_length=130 + (int) strlen(servername); diff --git a/src/main.c b/src/main.c index ed6caeec..52bea133 100644 --- a/src/main.c +++ b/src/main.c @@ -429,8 +429,7 @@ int main(int argc, char *argv[]) { #ifndef WITHOUT_MDNS if(config.use_mdns) { /* register services */ - /* FIXME: get default name from hostname (os_hostname()?) */ - servername = conf_alloc_string("general","servername","mt-daapd"); + servername = conf_get_servername(); memset(txtrecord,0,sizeof(txtrecord)); txt_add(txtrecord,"txtvers=1"); diff --git a/src/plugin.c b/src/plugin.c index c6b8af31..eac0381f 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -556,7 +556,18 @@ char *pi_server_ver(void) { } int pi_server_name(char *name, int *len) { - return conf_get_string("general","servername","unknown",name, len); + char *servername; + + servername = conf_get_servername(); + if((servername) && (strlen(servername) < (size_t)len)) { + strcpy(name,servername); + } else { + if((size_t)len > strlen("Firefly Media Server")) + strcpy(name,"Firefly Media Server"); + } + + free(servername); + return CONF_E_SUCCESS; } int pi_db_count(void) {