From 4567bce90b38280dc6a217e5dfe2af20b3bdf95f Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Wed, 4 Sep 2024 11:43:09 +0200 Subject: src: fallback to use first repository/server found use first repository/server in the list in case no specific repository/server is found in the same network, this is a fallback that works if repository/server is reachable through gateway. --- src/dbi.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'src/dbi.c') diff --git a/src/dbi.c b/src/dbi.c index 8122e6e..78485a6 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -270,7 +270,7 @@ bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image) 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; + const char *msglog, *netmask_ip, *server_ip, *first_server_ip = NULL; struct in_addr client, netmask, server; bool found = false; dbi_result result; @@ -321,6 +321,9 @@ int og_dbi_get_server_ip(const struct og_dbi *dbi, const char *client_ip, while (dbi_result_next_row(result) > 0) { server_ip = dbi_result_get_string(result, "ipserveradm"); + if (server_ip[0] && !first_server_ip) + first_server_ip = server_ip; + if (inet_aton(server_ip, &server) < 0) { syslog(LOG_ERR, "failed to get repository IP (%s:%d)\n", __func__, __LINE__); @@ -336,26 +339,32 @@ int og_dbi_get_server_ip(const struct og_dbi *dbi, const char *client_ip, break; } - if (!found) { + if (!first_server_ip) { syslog(LOG_ERR, - "cannot find server IP for client %s (%s:%d)\n", + "cannot find any server IP for client %s (%s:%d)\n", client_ip, __func__, __LINE__); dbi_result_free(result); return -1; } + if (found) { + syslog(LOG_INFO, "using server IP %s for client %s\n", + server_ip, client_ip); + } else { + server_ip = first_server_ip; + syslog(LOG_INFO, "falling back to server %s for client %s\n", + server_ip, client_ip); + } + 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) { - const char *msglog, *netmask_ip, *repo_ip; + const char *msglog, *netmask_ip, *repo_ip, *first_repo_ip = NULL; struct in_addr client, netmask, repo; bool found = false; dbi_result result; @@ -407,6 +416,9 @@ int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id, while (dbi_result_next_row(result) > 0) { repo_ip = dbi_result_get_string(result, "ip"); + if (repo_ip[0] && !first_repo_ip) + first_repo_ip = repo_ip; + if (inet_aton(repo_ip, &repo) < 0) { syslog(LOG_ERR, "failed to get repository IP (%s:%d)\n", __func__, __LINE__); @@ -422,19 +434,25 @@ int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id, break; } - if (!found) { + if (!first_repo_ip) { syslog(LOG_ERR, - "cannot find repository IP with repository ID %u for client %s (%s:%d)\n", + "cannot find any repository with ID %u for client %s (%s:%d)\n", repo_id, client_ip, __func__, __LINE__); dbi_result_free(result); return -1; } + if (found) { + syslog(LOG_INFO, "using repository with ID %u and IP %s for client %s\n", + repo_id, repo_ip, client_ip); + } else { + repo_ip = first_repo_ip; + syslog(LOG_INFO, "falling back to repository wth ID %u and IP %s for client %s\n", + repo_id, repo_ip, client_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; } -- cgit v1.2.3-18-g5258