From: Guus Sliepen Date: Mon, 14 Aug 2017 19:23:50 +0000 (+0200) Subject: Only keep at most 5 Address hints. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=eeef1270f5cc3c6872676d57c6d2befe372a6f20;p=meshlink Only keep at most 5 Address hints. --- diff --git a/src/conf.c b/src/conf.c index c5c3264e..ceca04db 100644 --- a/src/conf.c +++ b/src/conf.c @@ -383,8 +383,8 @@ bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t * return write_config_file(config_tree, filename); } -bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, bool replace) { - assert(mesh && name && key && (replace || value)); +bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, int trim) { + assert(mesh && name && key); char filename[PATH_MAX]; char tmpname[PATH_MAX]; @@ -410,7 +410,12 @@ bool modify_config_file(struct meshlink_handle *mesh, const char *name, const ch char buf[4096]; char *sep; - bool found = false; + int found = 0; + + if(value) { + fprintf(fw, "%s = %s\n", key, value); + found++; + } while(readline(fr, buf, sizeof(buf))) { if(!*buf || *buf == '#') @@ -426,20 +431,13 @@ bool modify_config_file(struct meshlink_handle *mesh, const char *name, const ch goto copy; } - if(!value) { - found = true; + // We found the key and the value. We already added it at the top, so ignore this one. + if(sep[1] == '=' && sep[2] == ' ' && !strcmp(sep + 3, value)) continue; - } - - // We found the key and the value. Keep one copy around. - if(sep[1] == '=' && sep[2] == ' ' && !strcmp(sep + 3, value)) { - if(found) - continue; - found = true; - } // We found the key but with a different value, delete it if wanted. - if(!found && replace) + found++; + if((!value || trim) && found > trim) continue; *sep = ' '; @@ -453,10 +451,6 @@ copy: fclose(fr); - // Add new key/value pair if necessary - if(!found && value) - fprintf(fw, "%s = %s\n", key, value); - if(ferror(fw)) error = true; @@ -488,5 +482,5 @@ copy: } bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) { - return modify_config_file(mesh, name, key, value, false); + return modify_config_file(mesh, name, key, value, 0); } diff --git a/src/conf.h b/src/conf.h index 715f9965..fd48ea64 100644 --- a/src/conf.h +++ b/src/conf.h @@ -52,7 +52,7 @@ extern bool write_config_file(const struct splay_tree_t *, const char *); extern bool read_server_config(struct meshlink_handle *mesh); extern bool read_host_config(struct meshlink_handle *mesh, struct splay_tree_t *, const char *); extern bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *, const char *); -extern bool modify_config_file(struct meshlink_handle *mesh, const char *, const char *, const char *, bool); -extern bool append_config_file(struct meshlink_handle *mesh, const char *, const char *, const char *); +extern bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, int trim); +extern bool append_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value); #endif diff --git a/src/meshlink.c b/src/meshlink.c index 6e03e63b..5d75e068 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -2085,7 +2085,7 @@ void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const if(host && port) { xasprintf(&str, "%s %s", host, port); if((strncmp("fe80", host, 4) != 0) && (strncmp("127.", host, 4) != 0) && (strcmp("localhost", host) != 0)) - append_config_file(mesh, node->name, "Address", str); + modify_config_file(mesh, node->name, "Address", str, 5); else logger(mesh, MESHLINK_DEBUG, "Not adding Link Local IPv6 Address to config\n"); }