summaryrefslogtreecommitdiffstats
path: root/src/dbi.c
diff options
context:
space:
mode:
authorOpenGnSys Support Team <soporte-og@soleta.eu>2024-08-21 20:19:23 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-08-21 21:58:48 +0200
commit5eba9f4e1b44fb0d4b4962c2241fe9391e202d7b (patch)
tree1940f98fcf9d24b1046e425b77eddc5bab624ee4 /src/dbi.c
parent4e0f9aac9e2883a992469f5b2b231ecb14b30183 (diff)
rest: allow repository to have more than one IP address
Repository can have more than one single IP address. * Add alias field to database to represent the extra IPs that are attached to the repository, update schema and add version 9. * Use og_dbi_get_repository_ip() to infer the repository IP address. * Add helper functions (src/repo.c) to build a list of repositories and update rest API to use it.
Diffstat (limited to 'src/dbi.c')
-rw-r--r--src/dbi.c74
1 files changed, 64 insertions, 10 deletions
diff --git a/src/dbi.c b/src/dbi.c
index 9eb1a39..f4f9ffa 100644
--- a/src/dbi.c
+++ b/src/dbi.c
@@ -270,34 +270,88 @@ bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image)
}
int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id,
- char *repository_ip)
+ const char *client_ip, char *repository_ip)
{
- const char *msglog, *dbi_repository_ip;
+ const char *msglog, *netmask_ip, *repo_ip;
+ struct in_addr client, netmask, repo;
+ 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 ip FROM repositorios WHERE idrepositorio = %u",
- repo_id);
+ "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 query database (%s:%d) %s\n",
+ 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 ip FROM repositorios WHERE idrepositorio = %u OR alias = %u",
+ repo_id, repo_id);
+ 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) {
+ repo_ip = dbi_result_get_string(result, "ip");
+
+ if (inet_aton(repo_ip, &repo) < 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 != (repo.s_addr & netmask.s_addr))
+ continue;
+
+ found = true;
+ break;
+ }
+
+ if (!found) {
syslog(LOG_ERR,
- "repository with id %u does not exist (%s:%d) %s\n",
- repo_id, __func__, __LINE__, msglog);
+ "cannot find repository IP with repository ID %u for client %s (%s:%d)\n",
+ repo_id, client_ip, __func__, __LINE__);
dbi_result_free(result);
return -1;
}
- dbi_repository_ip = dbi_result_get_string(result, "ip");
- snprintf(repository_ip, OG_DB_IP_MAXLEN + 1, "%s", dbi_repository_ip);
-
+ snprintf(repository_ip, OG_DB_IP_MAXLEN + 1, "%s", repo_ip);
dbi_result_free(result);
+ syslog(LOG_INFO, "using repository with ID %u and IP %s for computer %s\n",
+ repo_id, repository_ip, client_ip);
+
return 0;
}