From 7837be7f1513ebeb723298625ce9814d697f1c80 Mon Sep 17 00:00:00 2001 From: Niklas Hofmann Date: Sat, 9 Aug 2014 16:34:43 +0200 Subject: [PATCH] persistence of DeviceClass --- src/conf.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ src/conf.h | 4 +++ src/net_packet.c | 4 +-- src/net_setup.c | 56 +++++++++++++++++++++++++++++++++ src/protocol_auth.c | 2 ++ src/protocol_edge.c | 2 ++ 6 files changed, 142 insertions(+), 2 deletions(-) diff --git a/src/conf.c b/src/conf.c index e7626227..ad4b28b8 100644 --- a/src/conf.c +++ b/src/conf.c @@ -144,6 +144,22 @@ bool get_config_int(const config_t *cfg, int *result) { return false; } +bool set_config_int(config_t *cfg, int val) +{ + if(!cfg) + return false; + + char val_str[1024]; + snprintf(val_str, sizeof(val_str), "%d", val); + + if(cfg->value) + free(cfg->value); + + cfg->value = xstrdup(val_str); + + return true; +} + bool get_config_string(const config_t *cfg, char **result) { if(!cfg) return false; @@ -153,6 +169,19 @@ bool get_config_string(const config_t *cfg, char **result) { return true; } +bool set_config_string(config_t *cfg, const char* val) +{ + if(!cfg) + return false; + + if(cfg->value) + free(cfg->value); + + cfg->value = xstrdup(val); + + return true; +} + bool get_config_address(const config_t *cfg, struct addrinfo **result) { struct addrinfo *ai; @@ -291,6 +320,45 @@ bool read_config_file(splay_tree_t *config_tree, const char *fname) { return result; } +bool write_config_file(const struct splay_tree_t *config_tree, const char *fname) +{ + FILE *fp; + + fp = fopen(fname, "w+"); + + if(!fp) { + logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno)); + return false; + } + + for splay_each(config_t, cnf, config_tree) + { + if(fwrite(cnf->variable, sizeof(char), strlen(cnf->variable), fp) < strlen(cnf->variable)) { + logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); + return false; + } + + if(fwrite(" = ", sizeof(char), 3, fp) < 3) { + logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); + return false; + } + + if(fwrite(cnf->value, sizeof(char), strlen(cnf->value), fp) < strlen(cnf->value)) { + logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); + return false; + } + + if(fwrite("\n", sizeof(char), 1, fp) < 1) { + logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); + return false; + } + } + + fclose(fp); + + return true; +} + bool read_server_config(meshlink_handle_t *mesh) { char filename[PATH_MAX]; bool x; @@ -315,6 +383,14 @@ bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const return x; } +bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *config_tree, const char *name) +{ + char filename[PATH_MAX]; + + snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); + return write_config_file(config_tree, filename); +} + bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) { char filename[PATH_MAX]; snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); diff --git a/src/conf.h b/src/conf.h index 7c0335d7..c3cf6f7d 100644 --- a/src/conf.h +++ b/src/conf.h @@ -40,14 +40,18 @@ extern config_t *lookup_config(struct splay_tree_t *, char *); extern config_t *lookup_config_next(struct splay_tree_t *, const config_t *); extern bool get_config_bool(const config_t *, bool *); extern bool get_config_int(const config_t *, int *); +extern bool set_config_int(config_t *, int); extern bool get_config_string(const config_t *, char **); +extern bool set_config_string(config_t *, const char *); extern bool get_config_address(const config_t *, struct addrinfo **); extern config_t *parse_config_line(char *, const char *, int); extern bool read_config_file(struct splay_tree_t *, const char *); +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 append_config_file(struct meshlink_handle *mesh, const char *, const char *, const char *); #endif /* __MESHLINK_CONF_H__ */ diff --git a/src/net_packet.c b/src/net_packet.c index 467156aa..ca43d62c 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -132,7 +132,7 @@ static void send_mtu_probe_handler(event_loop_t *loop, void *data) { packet.len = len; n->status.broadcast = i >= 4 && n->mtuprobes <= 10 && n->prevedge; - logger(mesh, MESHLINK_INFO, "Sending MTU probe length %d to %s (%s)", len, n->name, n->hostname); + logger(mesh, MESHLINK_DEBUG, "Sending MTU probe length %d to %s (%s)", len, n->name, n->hostname); send_udppacket(mesh, n, &packet); } @@ -163,7 +163,7 @@ void send_mtu_probe(meshlink_handle_t *mesh, node_t *n) { } static void mtu_probe_h(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *packet, uint16_t len) { - logger(mesh, MESHLINK_INFO, "Got MTU probe length %d from %s (%s)", packet->len, n->name, n->hostname); + logger(mesh, MESHLINK_DEBUG, "Got MTU probe length %d from %s (%s)", packet->len, n->name, n->hostname); if(!packet->data[0]) { /* It's a probe request, send back a reply */ diff --git a/src/net_setup.c b/src/net_setup.c index a7ffa828..68b8ed74 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -124,6 +124,61 @@ static bool read_invitation_key(meshlink_handle_t *mesh) { return mesh->invitation_key; } +bool node_read_dclass(meshlink_handle_t *mesh, node_t *n) { + if(n->dclass != 0) + return true; + + splay_tree_t *config_tree; + char *p; + + init_configuration(&config_tree); + if(!read_host_config(mesh, config_tree, n->name)) + goto exit; + + if(get_config_string(lookup_config(config_tree, "DeviceClass"), &p)) { + n->dclass = atoi(p); + free(p); + } + +exit: + exit_configuration(&config_tree); + return n->dclass != 0; +} + +bool node_write_dclass(meshlink_handle_t *mesh, node_t *n) { + + if(n->dclass == 0) + return false; + + bool result = false; + + splay_tree_t *config_tree; + init_configuration(&config_tree); + + // ignore read errors; in case the file does not exist we will create it + read_host_config(mesh, config_tree, n->name); + + config_t* cnf = lookup_config(config_tree, "DeviceClass"); + + if(!cnf) + { + cnf = new_config(); + cnf->variable = xstrdup("DeviceClass"); + config_add(config_tree, cnf); + } + + set_config_int(cnf, n->dclass); + + if(!write_host_config(mesh, config_tree, n->name)) + goto fail; + + result = true; + +fail: + exit_configuration(&config_tree); + return result; +} + void load_all_nodes(meshlink_handle_t *mesh) { DIR *dir; struct dirent *ent; @@ -146,6 +201,7 @@ void load_all_nodes(meshlink_handle_t *mesh) { n = new_node(); n->name = xstrdup(ent->d_name); + node_read_dclass(mesh, n); node_add(mesh, n); } diff --git a/src/protocol_auth.c b/src/protocol_auth.c index 349d9032..d72257b7 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -412,6 +412,8 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { } n->dclass = dclass; + node_write_dclass(mesh, n); + n->connection = c; c->node = n; if(!(c->options & options & OPTION_PMTU_DISCOVERY)) { diff --git a/src/protocol_edge.c b/src/protocol_edge.c index 5ab15863..3852ad65 100644 --- a/src/protocol_edge.c +++ b/src/protocol_edge.c @@ -91,6 +91,7 @@ bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { } from->dclass = from_dclass; + node_write_dclass(mesh, from); if(!to) { to = new_node(); @@ -99,6 +100,7 @@ bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { } to->dclass = to_dclass; + node_write_dclass(mesh, to); /* Convert addresses */ -- 2.39.2