From 41ac15d1007e1e00b0aeacf9dff67a60ed3488b4 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Sun, 19 Dec 2021 21:18:13 +0100 Subject: #915 Remove useless WoL shim code Levanta() is not required, iterate over the array of IP address and make direct calls to WakeUp(). This is also implicitly fixing up a memleak in og_cmd_wol(). --- src/ogAdmServer.c | 36 +----------------------------------- src/ogAdmServer.h | 3 +-- src/rest.c | 54 +++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/ogAdmServer.c b/src/ogAdmServer.c index 17c9d39..6f6de54 100644 --- a/src/ogAdmServer.c +++ b/src/ogAdmServer.c @@ -271,40 +271,6 @@ int checkDato(struct og_dbi *dbi, char *dato, const char *tabla, return (identificador); } -// ________________________________________________________________________________________________________ -// Función: Levanta -// -// Descripción: -// Enciende ordenadores a través de la red cuyas macs se pasan como parámetro -// Parámetros: -// - iph: Cadena de direcciones ip separadas por ";" -// - mac: Cadena de direcciones mac separadas por ";" -// - mar: Método de arranque (1=Broadcast, 2=Unicast) -// Devuelve: -// true: Si el proceso es correcto -// false: En caso de ocurrir algún error -// ________________________________________________________________________________________________________ - -bool Levanta(char *ptrIP[], char *ptrMacs[], char *ptrNetmasks[], int lon, - char *mar) -{ - int i, s; - - s = wol_socket_open(); - if (s < 0) - return false; - - for (i = 0; i < lon; i++) { - if (!WakeUp(s, ptrIP[i], ptrMacs[i], ptrNetmasks[i], mar)) { - syslog(LOG_ERR, "problem sending magic packet\n"); - close(s); - return false; - } - } - close(s); - return true; -} - enum wol_delivery_type { OG_WOL_BROADCAST = 1, OG_WOL_UNICAST = 2 @@ -325,7 +291,7 @@ enum wol_delivery_type { // false: En caso de ocurrir algún error //_____________________________________________________________________________________________________________ // -bool WakeUp(int s, char* iph, char *mac, char *netmask, char *mar) +bool WakeUp(int s, const char *iph, const char *mac, const char *netmask, const char *mar) { struct in_addr addr, netmask_addr, broadcast_addr ={}; unsigned int macaddr[OG_WOL_MACADDR_LEN]; diff --git a/src/ogAdmServer.h b/src/ogAdmServer.h index 77e5683..d818289 100644 --- a/src/ogAdmServer.h +++ b/src/ogAdmServer.h @@ -23,8 +23,7 @@ struct og_dbi; bool actualizaConfiguracion(struct og_dbi *,char* ,int); -bool Levanta(char**, char**, char**, int, char*); -bool WakeUp(int,char*,char*,char*,char*); +bool WakeUp(int, const char *, const char *, const char *, const char *); bool actualizaCreacionImagen(struct og_dbi *,char*,char*,char*,char*,char*,char*); bool actualizaRestauracionImagen(struct og_dbi *,char*,char*,char*,char*,char*); bool actualizaHardware(struct og_dbi *dbi, char* ,char*,char*,char*); diff --git a/src/rest.c b/src/rest.c index e2dbbb0..1a5afa1 100644 --- a/src/rest.c +++ b/src/rest.c @@ -520,6 +520,7 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params) dbi_result result; const char *key; json_t *value; + int sd; if (json_typeof(element) != JSON_OBJECT) return -1; @@ -580,12 +581,19 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params) if (i == 0) return 0; + sd = wol_socket_open(); + if (sd < 0) { + syslog(LOG_ERR, "cannot open wol socket (%s:%d)\n", + __func__, __LINE__); + goto err_free_params; + } + for (i = 0; i < params->ips_array_len; i++) { if (og_client_find(params->ips_array[i])) continue; if (inet_aton(params->ips_array[i], &addr) < 0) - return -1; + goto err_out; cli_wol = og_client_wol_find(&addr); if (cli_wol) { @@ -595,16 +603,20 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params) cli_wol = og_client_wol_create(&addr); if (!cli_wol) - return -1; + goto err_out; list_add_tail(&cli_wol->list, &client_wol_list); - } - - if (!Levanta((char **)params->ips_array, (char **)params->mac_array, - (char **)params->netmask_array, i, - (char *)params->wol_type)) - return -1; + if (!WakeUp(sd, params->ips_array[i], params->mac_array[i], + params->netmask_array[i], params->wol_type)) { + syslog(LOG_ERR, "Failed to send wol packet to %s\n", + params->ips_array[i]); + continue; + } + } +err_out: + close(sd); +err_free_params: for (i = 0; i < params->ips_array_len; ++i) { free((void *)params->ips_array[i]); free((void *)params->mac_array[i]); @@ -3156,6 +3168,7 @@ void og_schedule_run(unsigned int task_id, unsigned int schedule_id, struct og_cmd *cmd, *next; struct og_dbi *dbi; unsigned int i; + int sd; dbi = og_dbi_open(&ogconfig.db); if (!dbi) { @@ -3190,23 +3203,38 @@ void og_schedule_run(unsigned int task_id, unsigned int schedule_id, duplicated = false; } + sd = wol_socket_open(); + if (sd < 0) { + syslog(LOG_ERR, "cannot open wol socket (%s:%d)\n", + __func__, __LINE__); + goto err_out; + } + list_for_each_entry_safe(cmd, next, &cmd_list, list) { if (cmd->type != OG_CMD_WOL) continue; - if (Levanta((char **)cmd->params.ips_array, - (char **)cmd->params.mac_array, - (char **)cmd->params.netmask_array, - cmd->params.ips_array_len, - (char *)cmd->params.wol_type)) + for (i = 0; i < cmd->params.ips_array_len; i++) { + if (!WakeUp(sd, cmd->params.ips_array[i], + cmd->params.mac_array[i], + cmd->params.netmask_array[i], + cmd->params.wol_type)) { + syslog(LOG_ERR, "Failed to send wol packet to %s\n", + params.ips_array[i]); + continue; + } og_dbi_update_action(cmd->id, true); + } list_del(&cmd->list); og_cmd_free(cmd); } + close(sd); + og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, ¶ms, NULL); +err_out: for (i = 0; i < params.ips_array_len; i++) free((void *)params.ips_array[i]); } -- cgit v1.2.3-18-g5258