From 0631b0e8e6fdf77d0bbe6715e0cb2c4bdb872b12 Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Fri, 10 Jul 2020 13:13:59 +0200 Subject: #988 Add DB port option to ogserver.json config file This patch provides functionality to select a database port. It also adds a default IP for the database. --- cfg/ogserver.json | 2 ++ src/cfg.c | 4 ++++ src/cfg.h | 1 + src/dbi.c | 1 + src/dbi.h | 1 + src/ogAdmServer.c | 5 +++++ src/ogAdmServer.h | 1 + 7 files changed, 15 insertions(+) diff --git a/cfg/ogserver.json b/cfg/ogserver.json index 009d436..20ae5ac 100644 --- a/cfg/ogserver.json +++ b/cfg/ogserver.json @@ -5,6 +5,8 @@ "api_token": "5a5ca1172136299640a9f47469237e0a" }, "database" : { + "ip": "127.0.0.1", + "port": 3306, "name" : "opengnsys", "user" : "mysql", "pass" : "mysql" diff --git a/src/cfg.c b/src/cfg.c index 54067b0..d19ab15 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -57,6 +57,9 @@ static int parse_json_db(struct og_server_cfg *cfg, json_t *element) } else if (!strcmp(key, "name")) { if (og_json_parse_string(value, &cfg->db.name) < 0) return -1; + } else if (!strcmp(key, "port")) { + if (og_json_parse_uint(value, &cfg->db.port) < 0) + return -1; } else { syslog(LOG_ERR, "unknown key `%s' in db\n", key); return -1; @@ -162,4 +165,5 @@ void from_json_to_legacy(struct og_server_cfg *cfg) snprintf(catalog, sizeof(catalog), cfg->db.name); snprintf(interface, sizeof(interface), cfg->wol.interface); snprintf(auth_token, sizeof(auth_token), cfg->rest.api_token); + snprintf(db_port, sizeof(db_port), "%u", cfg->db.port); } diff --git a/src/cfg.h b/src/cfg.h index d489d5f..5b89db2 100644 --- a/src/cfg.h +++ b/src/cfg.h @@ -6,6 +6,7 @@ struct og_server_cfg { const char *user; const char *pass; const char *ip; + unsigned int port; const char *name; } db; struct { diff --git a/src/dbi.c b/src/dbi.c index 6640f50..e0fe738 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -24,6 +24,7 @@ struct og_dbi *og_dbi_open(struct og_dbi_config *config) } dbi_conn_set_option(dbi->conn, "host", config->host); + dbi_conn_set_option(dbi->conn, "port", config->port); dbi_conn_set_option(dbi->conn, "username", config->user); dbi_conn_set_option(dbi->conn, "password", config->passwd); dbi_conn_set_option(dbi->conn, "dbname", config->database); diff --git a/src/dbi.h b/src/dbi.h index 30327a7..4f1d81c 100644 --- a/src/dbi.h +++ b/src/dbi.h @@ -7,6 +7,7 @@ struct og_dbi_config { const char *user; const char *passwd; const char *host; + const char *port; const char *database; }; diff --git a/src/ogAdmServer.c b/src/ogAdmServer.c index f8a50ae..3ba5b30 100644 --- a/src/ogAdmServer.c +++ b/src/ogAdmServer.c @@ -31,6 +31,7 @@ char interface[4096]; // Interface name char auth_token[4096]; // API token char servidoradm[4096]; // Dirección IP del servidor de administración char puerto[4096]; // Puerto de comunicación +char db_port[4096]; SOCKETCL tbsockets[MAXIMOS_CLIENTES]; @@ -38,6 +39,7 @@ struct og_dbi_config dbi_config = { .user = usuario, .passwd = pasguor, .host = datasource, + .port = db_port, .database = catalog, }; @@ -101,6 +103,9 @@ bool tomaConfiguracion(const char *filecfg) line = fgets(buf, sizeof(buf), fcfg); } + /* Default value to preserve legacy config file support */ + snprintf(db_port, sizeof(db_port), "3306"); + fclose(fcfg); if (!servidoradm[0]) { diff --git a/src/ogAdmServer.h b/src/ogAdmServer.h index 7e5fcd3..f51b7be 100644 --- a/src/ogAdmServer.h +++ b/src/ogAdmServer.h @@ -29,6 +29,7 @@ extern char interface[4096]; extern char api_token[4096]; extern char servidoradm[4096]; extern char puerto[4096]; +extern char db_port[4096]; struct og_client; -- cgit v1.2.3-18-g5258