summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/ogAdmLib.h2
-rw-r--r--src/ogAdmServer.c76
-rw-r--r--src/ogAdmServer.h1
-rw-r--r--src/rest.c24
-rw-r--r--src/wol.c60
-rw-r--r--src/wol.h11
6 files changed, 74 insertions, 100 deletions
diff --git a/src/ogAdmLib.h b/src/ogAdmLib.h
index 1845c2b..3e7de97 100644
--- a/src/ogAdmLib.h
+++ b/src/ogAdmLib.h
@@ -34,8 +34,6 @@
#define ANNOREF 2009 // Año de referencia base
-#define PUERTO_WAKEUP 9 // Puerto wake up
-
#define MAXHARDWARE 128 // Máximos elementos hardware a detectar
#define MAXSOFTWARE 8096 // Máximos elementos software a detectar
diff --git a/src/ogAdmServer.c b/src/ogAdmServer.c
index 2b64254..8c6702f 100644
--- a/src/ogAdmServer.c
+++ b/src/ogAdmServer.c
@@ -271,82 +271,6 @@ int checkDato(struct og_dbi *dbi, char *dato, const char *tabla,
return (identificador);
}
-enum wol_delivery_type {
- OG_WOL_BROADCAST = 1,
- OG_WOL_UNICAST = 2
-};
-
-//_____________________________________________________________________________________________________________
-// Función: WakeUp
-//
-// Descripción:
-// Enciende el ordenador cuya MAC se pasa como parámetro
-// Parámetros:
-// - s : Socket para enviar trama magic packet
-// - iph : Cadena con la dirección ip
-// - mac : Cadena con la dirección mac en formato XXXXXXXXXXXX
-// - 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 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];
- char HDaddress_bin[OG_WOL_MACADDR_LEN];
- struct sockaddr_in WakeUpCliente;
- struct wol_msg Trama_WakeUp;
- bool ret;
- int i;
-
- if (!inet_aton(iph, &addr)) {
- syslog(LOG_ERR, "bad IP address\n");
- return false;
- }
-
- if (!inet_aton(netmask, &netmask_addr)) {
- syslog(LOG_ERR, "bad netmask address: %s\n", netmask);
- return false;
- }
-
- broadcast_addr.s_addr = addr.s_addr | ~netmask_addr.s_addr;
-
- for (i = 0; i < 6; i++) // Primera secuencia de la trama Wake Up (0xFFFFFFFFFFFF)
- Trama_WakeUp.secuencia_FF[i] = 0xFF;
-
- sscanf(mac, "%02x%02x%02x%02x%02x%02x",
- &macaddr[0], &macaddr[1], &macaddr[2],
- &macaddr[3], &macaddr[4], &macaddr[5]);
-
- for (i = 0; i < 6; i++)
- HDaddress_bin[i] = (uint8_t)macaddr[i];
-
- for (i = 0; i < 16; i++) // Segunda secuencia de la trama Wake Up , repetir 16 veces su la MAC
- memcpy(&Trama_WakeUp.macbin[i][0], &HDaddress_bin, 6);
-
- /* Creación de socket del cliente que recibe la trama magic packet */
- WakeUpCliente.sin_family = AF_INET;
- WakeUpCliente.sin_port = htons((short) PUERTO_WAKEUP);
-
- switch (atoi(mar)) {
- case OG_WOL_BROADCAST:
- ret = wake_up_broadcast(s, &WakeUpCliente, &Trama_WakeUp);
- ret &= wake_up_send(s, &WakeUpCliente, &Trama_WakeUp,
- &broadcast_addr);
- break;
- case OG_WOL_UNICAST:
- ret = wake_up_send(s, &WakeUpCliente, &Trama_WakeUp, &addr);
- break;
- default:
- syslog(LOG_ERR, "unknown wol type\n");
- ret = false;
- break;
- }
- return ret;
-}
-
// ________________________________________________________________________________________________________
// Función: actualizaCreacionImagen
//
diff --git a/src/ogAdmServer.h b/src/ogAdmServer.h
index 0768561..59c7ea1 100644
--- a/src/ogAdmServer.h
+++ b/src/ogAdmServer.h
@@ -23,7 +23,6 @@
struct og_dbi;
bool actualizaConfiguracion(struct og_dbi *,char* ,int);
-bool WakeUp(int, const char *, const char *, const char *, const char *);
bool actualizaCreacionImagen(struct og_dbi *,char*,char*,char*,char*,char*,char*);
bool actualizaHardware(struct og_dbi *dbi, char* ,char*,char*,char*);
bool cuestionPerfilHardware(struct og_dbi *dbi,char*,char*,int,char*,char*,int *,int);
diff --git a/src/rest.c b/src/rest.c
index 1a5afa1..99db577 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -512,8 +512,8 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params)
{
char ips_str[(OG_DB_IP_MAXLEN + 1) * OG_CLIENTS_MAX + 1] = {};
struct og_client_wol *cli_wol;
+ struct in_addr addr, netmask;
int ips_str_len = 0;
- struct in_addr addr;
const char *msglog;
struct og_dbi *dbi;
int err = 0, i = 0;
@@ -593,7 +593,7 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params)
continue;
if (inet_aton(params->ips_array[i], &addr) < 0)
- goto err_out;
+ continue;
cli_wol = og_client_wol_find(&addr);
if (cli_wol) {
@@ -607,8 +607,11 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params)
list_add_tail(&cli_wol->list, &client_wol_list);
- if (!WakeUp(sd, params->ips_array[i], params->mac_array[i],
- params->netmask_array[i], params->wol_type)) {
+ if (inet_aton(params->netmask_array[i], &netmask) < 0)
+ continue;
+
+ if (wake_up(sd, &addr, &netmask, params->mac_array[i],
+ atoi(params->wol_type)) < 0) {
syslog(LOG_ERR, "Failed to send wol packet to %s\n",
params->ips_array[i]);
continue;
@@ -3164,8 +3167,9 @@ void og_schedule_run(unsigned int task_id, unsigned int schedule_id,
enum og_schedule_type type)
{
struct og_msg_params params = {};
- bool duplicated = false;
+ struct in_addr addr, netmask;
struct og_cmd *cmd, *next;
+ bool duplicated = false;
struct og_dbi *dbi;
unsigned int i;
int sd;
@@ -3215,10 +3219,14 @@ void og_schedule_run(unsigned int task_id, unsigned int schedule_id,
continue;
for (i = 0; i < cmd->params.ips_array_len; i++) {
- if (!WakeUp(sd, cmd->params.ips_array[i],
+ if (inet_aton(cmd->params.ips_array[i], &addr) < 0)
+ continue;
+ if (inet_aton(cmd->params.netmask_array[i], &netmask) < 0)
+ continue;
+
+ if (wake_up(sd, &addr, &netmask,
cmd->params.mac_array[i],
- cmd->params.netmask_array[i],
- cmd->params.wol_type)) {
+ atoi(cmd->params.wol_type)) < 0) {
syslog(LOG_ERR, "Failed to send wol packet to %s\n",
params.ips_array[i]);
continue;
diff --git a/src/wol.c b/src/wol.c
index ad9991a..7f6464f 100644
--- a/src/wol.c
+++ b/src/wol.c
@@ -43,8 +43,8 @@ int wol_socket_open(void)
return s;
}
-bool wake_up_send(int sd, struct sockaddr_in *client,
- const struct wol_msg *msg, const struct in_addr *addr)
+static int wake_up_send(int sd, struct sockaddr_in *client,
+ const struct wol_msg *msg, const struct in_addr *addr)
{
int ret;
@@ -54,21 +54,21 @@ bool wake_up_send(int sd, struct sockaddr_in *client,
(struct sockaddr *)client, sizeof(*client));
if (ret < 0) {
syslog(LOG_ERR, "failed to send wol\n");
- return false;
+ return -1;
}
- return true;
+ return 0;
}
-bool wake_up_broadcast(int sd, struct sockaddr_in *client,
- const struct wol_msg *msg)
+static int wake_up_broadcast(int sd, struct sockaddr_in *client,
+ const struct wol_msg *msg)
{
struct sockaddr_in *broadcast_addr, addr = {};
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) < 0) {
syslog(LOG_ERR, "cannot get list of addresses\n");
- return false;
+ return -1;
}
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
@@ -89,6 +89,52 @@ bool wake_up_broadcast(int sd, struct sockaddr_in *client,
return wake_up_send(sd, client, msg, &addr.sin_addr);
}
+enum wol_delivery_type {
+ OG_WOL_BROADCAST = 1,
+ OG_WOL_UNICAST = 2
+};
+
+int wake_up(int s, const struct in_addr *addr, const struct in_addr *netmask,
+ const char *mac, uint32_t wol_delivery_type)
+{
+ uint32_t mac_addr_u32[OG_WOL_MACADDR_LEN];
+ uint8_t mac_addr[OG_WOL_MACADDR_LEN];
+ struct sockaddr_in dest = {
+ .sin_family = AF_INET,
+ .sin_port = htons(OG_WOL_PORT),
+ };
+ struct in_addr broadcast_addr;
+ struct wol_msg msg = {};
+ int ret, i;
+
+ memset(msg.wol_sequence_ff, 0xff, OG_WOL_SEQUENCE);
+ sscanf(mac, "%02x%02x%02x%02x%02x%02x",
+ &mac_addr_u32[0], &mac_addr_u32[1], &mac_addr_u32[2],
+ &mac_addr_u32[3], &mac_addr_u32[4], &mac_addr_u32[5]);
+
+ for (i = 0; i < OG_WOL_MACADDR_LEN; i++)
+ mac_addr[i] = mac_addr_u32[i];
+ for (i = 0; i < OG_WOL_REPEAT; i++)
+ memcpy(&msg.mac_addr[i][0], mac_addr, OG_WOL_MACADDR_LEN);
+
+ switch (wol_delivery_type) {
+ case OG_WOL_BROADCAST:
+ ret = wake_up_broadcast(s, &dest, &msg);
+ broadcast_addr.s_addr = addr->s_addr | ~netmask->s_addr;
+ ret |= wake_up_send(s, &dest, &msg, &broadcast_addr);
+ break;
+ case OG_WOL_UNICAST:
+ ret = wake_up_send(s, &dest, &msg, addr);
+ break;
+ default:
+ syslog(LOG_ERR, "unknown wol type\n");
+ ret = -1;
+ break;
+ }
+
+ return ret;
+}
+
#define OG_WOL_CLIENT_TIMEOUT 60.
static void og_client_wol_timer_cb(struct ev_loop *loop, ev_timer *timer,
diff --git a/src/wol.h b/src/wol.h
index a245592..8a6c974 100644
--- a/src/wol.h
+++ b/src/wol.h
@@ -4,21 +4,20 @@
#define OG_WOL_SEQUENCE 6
#define OG_WOL_MACADDR_LEN 6
#define OG_WOL_REPEAT 16
+#define OG_WOL_PORT 9
#include "list.h"
#include <ev.h>
#include <stdbool.h>
struct wol_msg {
- char secuencia_FF[OG_WOL_SEQUENCE];
- char macbin[OG_WOL_REPEAT][OG_WOL_MACADDR_LEN];
+ char wol_sequence_ff[OG_WOL_SEQUENCE];
+ char mac_addr[OG_WOL_REPEAT][OG_WOL_MACADDR_LEN];
};
int wol_socket_open(void);
-bool wake_up_send(int sd, struct sockaddr_in *client,
- const struct wol_msg *msg, const struct in_addr *addr);
-bool wake_up_broadcast(int sd, struct sockaddr_in *client,
- const struct wol_msg *msg);
+int wake_up(int s, const struct in_addr *addr, const struct in_addr *netmask,
+ const char *mac, uint32_t wol_delivery_type);
struct og_client_wol {
struct list_head list;