summaryrefslogtreecommitdiffstats
path: root/src/dbi.c
diff options
context:
space:
mode:
authorOpenGnSys Support Team <soporte-og@soleta.eu>2024-08-28 12:45:46 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-08-28 15:28:47 +0200
commitcd9650108af1eb882ca11858249dc428087667ff (patch)
tree40496a1e3c277feafd9fe2997a633c19acf52f5b /src/dbi.c
parent9ee9d66b7c7c8e1c4e5b70ccaf5dae74b31a91f9 (diff)
src: infer server IP when it has more than one IP address
infer server IP for a given client. User does not have to attach a client to a given server IP anymore through ordenadores.identorno. this simplifies previous work to allow a server to have more than one IP address a0a347068285 ('#1074 rest: set_mode: add support for different ogserver addresses') 44745a3f2287 ('rest: add POST client/server method') POST /client/server is removed, it is only used by ogcli and explicit association between server and client is not required. server_id json attribute is also removed in GET client/info.
Diffstat (limited to 'src/dbi.c')
-rw-r--r--src/dbi.c87
1 files changed, 85 insertions, 2 deletions
diff --git a/src/dbi.c b/src/dbi.c
index f4f9ffa..8122e6e 100644
--- a/src/dbi.c
+++ b/src/dbi.c
@@ -76,7 +76,6 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
" ordenadores.oglivedir,"
" ordenadores.inremotepc,"
" ordenadores.maintenance,"
- " ordenadores.identorno,"
" aulas.netmask,"
" centros.idcentro "
"FROM ordenadores "
@@ -105,7 +104,6 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
dbi_result_get_string(result, "ip"));
snprintf(computer->mac, sizeof(computer->mac), "%s",
dbi_result_get_string(result, "mac"));
- computer->server_id = dbi_result_get_uint(result, "identorno");
computer->room = dbi_result_get_uint(result, "idaula");
computer->center = dbi_result_get_uint(result, "idcentro");
computer->hardware_id = dbi_result_get_uint(result, "idperfilhard");
@@ -269,6 +267,91 @@ bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image)
return false;
}
+int og_dbi_get_server_ip(const struct og_dbi *dbi, const char *client_ip,
+ char *res_server_ip)
+{
+ const char *msglog, *netmask_ip, *server_ip;
+ struct in_addr client, netmask, server;
+ bool found = false;
+ dbi_result result;
+
+ if (inet_aton(client_ip, &client) < 0) {
+ syslog(LOG_ERR, "failed to parse client IP (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT aulas.netmask FROM aulas "
+ "INNER JOIN ordenadores ON ordenadores.idaula = aulas.idaula "
+ "WHERE ordenadores.ip = '%s'", client_ip);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to get netmask (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+
+ if (!dbi_result_next_row(result)) {
+ syslog(LOG_ERR, "netmask does not exist in database (%s:%d)\n",
+ __func__, __LINE__);
+ dbi_result_free(result);
+ return -1;
+ }
+
+ netmask_ip = dbi_result_get_string(result, "netmask");
+ if (inet_aton(netmask_ip, &netmask) < 0) {
+ syslog(LOG_ERR, "failed to parse netmask (%s:%d)\n",
+ __func__, __LINE__);
+ dbi_result_free(result);
+ return -1;
+ }
+
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT ipserveradm FROM entornos");
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+
+ while (dbi_result_next_row(result) > 0) {
+ server_ip = dbi_result_get_string(result, "ipserveradm");
+
+ if (inet_aton(server_ip, &server) < 0) {
+ syslog(LOG_ERR, "failed to get repository IP (%s:%d)\n",
+ __func__, __LINE__);
+ dbi_result_free(result);
+ return -1;
+ }
+ client.s_addr &= netmask.s_addr;
+
+ if (client.s_addr != (server.s_addr & netmask.s_addr))
+ continue;
+
+ found = true;
+ break;
+ }
+
+ if (!found) {
+ syslog(LOG_ERR,
+ "cannot find server IP for client %s (%s:%d)\n",
+ client_ip, __func__, __LINE__);
+ dbi_result_free(result);
+ return -1;
+ }
+
+ snprintf(res_server_ip, OG_DB_IP_MAXLEN + 1, "%s", server_ip);
+ dbi_result_free(result);
+
+ syslog(LOG_INFO, "using server IP %s for computer %s\n",
+ res_server_ip, client_ip);
+
+ return 0;
+}
int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id,
const char *client_ip, char *repository_ip)
{