diff options
Diffstat (limited to 'src/dbi.c')
-rw-r--r-- | src/dbi.c | 87 |
1 files changed, 85 insertions, 2 deletions
@@ -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) { |