From 826ad11e419db90b66b3f76a90b54df021bb39fc Mon Sep 17 00:00:00 2001 From: "William A. Kennington III" Date: Sun, 24 Aug 2014 19:49:27 -0700 Subject: [PATCH] utils: Refactor get_name's functionality into util for global access --- src/net_setup.c | 33 ++++----------------------------- src/utils.c | 36 ++++++++++++++++++++++++++++++++++++ src/utils.h | 2 ++ 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/net_setup.c b/src/net_setup.c index d83c5723..665e3c66 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -402,41 +402,16 @@ void load_all_nodes(void) { char *get_name(void) { char *name = NULL; + char *returned_name; get_config_string(lookup_config(config_tree, "Name"), &name); if(!name) return NULL; - if(*name == '$') { - char *envname = getenv(name + 1); - char hostname[32] = ""; - if(!envname) { - if(strcmp(name + 1, "HOST")) { - logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1); - return false; - } - if(gethostname(hostname, sizeof hostname) || !*hostname) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno)); - return false; - } - hostname[31] = 0; - envname = hostname; - } - free(name); - name = xstrdup(envname); - for(char *c = name; *c; c++) - if(!isalnum(*c)) - *c = '_'; - } - - if(!check_id(name)) { - logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!"); - free(name); - return false; - } - - return name; + returned_name = replace_name(name); + free(name); + return returned_name; } bool setup_myself_reloadable(void) { diff --git a/src/utils.c b/src/utils.c index 0d7bc02f..797bc3f5 100644 --- a/src/utils.c +++ b/src/utils.c @@ -19,6 +19,7 @@ */ #include "system.h" +#include "xalloc.h" #include "../src/logger.h" #include "utils.h" @@ -179,3 +180,38 @@ unsigned int bitfield_to_int(const void *bitfield, size_t size) { memcpy(&value, bitfield, size); return value; } + +char *replace_name(const char *name) { + char *ret_name; + + if (name[0] == '$') { + char *envname = getenv(name + 1); + char hostname[HOST_NAME_MAX+1]; + if (!envname) { + if (strcmp(name + 1, "HOST")) { + logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1); + return NULL; + } + if (gethostname(hostname, sizeof hostname) || !*hostname) { + logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno)); + return NULL; + } + hostname[HOST_NAME_MAX] = 0; + envname = hostname; + } + ret_name = xstrdup(envname); + for (char *c = ret_name; *c; c++) + if (!isalnum(*c)) + *c = '_'; + } else { + ret_name = xstrdup(name); + } + + if (!check_id(ret_name)) { + logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!"); + free(ret_name); + return NULL; + } + + return ret_name; +} diff --git a/src/utils.h b/src/utils.h index 7e519f4a..28afc4ef 100644 --- a/src/utils.h +++ b/src/utils.h @@ -50,4 +50,6 @@ extern const char *winerror(int); extern unsigned int bitfield_to_int(const void *bitfield, size_t size); +char *replace_name(const char *name); + #endif /* __TINC_UTILS_H__ */ -- 2.20.1