summaryrefslogtreecommitdiffstats
path: root/src/dbi.c
diff options
context:
space:
mode:
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;
}