mirror of https://git.48k.eu/ogserver
#915 consolidate WoL sender function
This patch aims simplifies the WoL sender routine. A few related changes: - Replace goto err to continue if IP address is malformed - Use ret |= instead of ret &= to accumulate error code.master
parent
6e70916005
commit
216986e848
|
@ -34,8 +34,6 @@
|
||||||
|
|
||||||
#define ANNOREF 2009 // Año de referencia base
|
#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 MAXHARDWARE 128 // Máximos elementos hardware a detectar
|
||||||
#define MAXSOFTWARE 8096 // Máximos elementos software a detectar
|
#define MAXSOFTWARE 8096 // Máximos elementos software a detectar
|
||||||
|
|
||||||
|
|
|
@ -271,82 +271,6 @@ int checkDato(struct og_dbi *dbi, char *dato, const char *tabla,
|
||||||
return (identificador);
|
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
|
// Función: actualizaCreacionImagen
|
||||||
//
|
//
|
||||||
|
|
|
@ -23,7 +23,6 @@
|
||||||
struct og_dbi;
|
struct og_dbi;
|
||||||
|
|
||||||
bool actualizaConfiguracion(struct og_dbi *,char* ,int);
|
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 actualizaCreacionImagen(struct og_dbi *,char*,char*,char*,char*,char*,char*);
|
||||||
bool actualizaHardware(struct og_dbi *dbi, 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);
|
bool cuestionPerfilHardware(struct og_dbi *dbi,char*,char*,int,char*,char*,int *,int);
|
||||||
|
|
24
src/rest.c
24
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] = {};
|
char ips_str[(OG_DB_IP_MAXLEN + 1) * OG_CLIENTS_MAX + 1] = {};
|
||||||
struct og_client_wol *cli_wol;
|
struct og_client_wol *cli_wol;
|
||||||
|
struct in_addr addr, netmask;
|
||||||
int ips_str_len = 0;
|
int ips_str_len = 0;
|
||||||
struct in_addr addr;
|
|
||||||
const char *msglog;
|
const char *msglog;
|
||||||
struct og_dbi *dbi;
|
struct og_dbi *dbi;
|
||||||
int err = 0, i = 0;
|
int err = 0, i = 0;
|
||||||
|
@ -593,7 +593,7 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (inet_aton(params->ips_array[i], &addr) < 0)
|
if (inet_aton(params->ips_array[i], &addr) < 0)
|
||||||
goto err_out;
|
continue;
|
||||||
|
|
||||||
cli_wol = og_client_wol_find(&addr);
|
cli_wol = og_client_wol_find(&addr);
|
||||||
if (cli_wol) {
|
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);
|
list_add_tail(&cli_wol->list, &client_wol_list);
|
||||||
|
|
||||||
if (!WakeUp(sd, params->ips_array[i], params->mac_array[i],
|
if (inet_aton(params->netmask_array[i], &netmask) < 0)
|
||||||
params->netmask_array[i], params->wol_type)) {
|
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",
|
syslog(LOG_ERR, "Failed to send wol packet to %s\n",
|
||||||
params->ips_array[i]);
|
params->ips_array[i]);
|
||||||
continue;
|
continue;
|
||||||
|
@ -3164,8 +3167,9 @@ void og_schedule_run(unsigned int task_id, unsigned int schedule_id,
|
||||||
enum og_schedule_type type)
|
enum og_schedule_type type)
|
||||||
{
|
{
|
||||||
struct og_msg_params params = {};
|
struct og_msg_params params = {};
|
||||||
bool duplicated = false;
|
struct in_addr addr, netmask;
|
||||||
struct og_cmd *cmd, *next;
|
struct og_cmd *cmd, *next;
|
||||||
|
bool duplicated = false;
|
||||||
struct og_dbi *dbi;
|
struct og_dbi *dbi;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
int sd;
|
int sd;
|
||||||
|
@ -3215,10 +3219,14 @@ void og_schedule_run(unsigned int task_id, unsigned int schedule_id,
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
for (i = 0; i < cmd->params.ips_array_len; i++) {
|
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.mac_array[i],
|
||||||
cmd->params.netmask_array[i],
|
atoi(cmd->params.wol_type)) < 0) {
|
||||||
cmd->params.wol_type)) {
|
|
||||||
syslog(LOG_ERR, "Failed to send wol packet to %s\n",
|
syslog(LOG_ERR, "Failed to send wol packet to %s\n",
|
||||||
params.ips_array[i]);
|
params.ips_array[i]);
|
||||||
continue;
|
continue;
|
||||||
|
|
60
src/wol.c
60
src/wol.c
|
@ -43,8 +43,8 @@ int wol_socket_open(void)
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wake_up_send(int sd, struct sockaddr_in *client,
|
static int wake_up_send(int sd, struct sockaddr_in *client,
|
||||||
const struct wol_msg *msg, const struct in_addr *addr)
|
const struct wol_msg *msg, const struct in_addr *addr)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
@ -54,21 +54,21 @@ bool wake_up_send(int sd, struct sockaddr_in *client,
|
||||||
(struct sockaddr *)client, sizeof(*client));
|
(struct sockaddr *)client, sizeof(*client));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
syslog(LOG_ERR, "failed to send wol\n");
|
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,
|
static int wake_up_broadcast(int sd, struct sockaddr_in *client,
|
||||||
const struct wol_msg *msg)
|
const struct wol_msg *msg)
|
||||||
{
|
{
|
||||||
struct sockaddr_in *broadcast_addr, addr = {};
|
struct sockaddr_in *broadcast_addr, addr = {};
|
||||||
struct ifaddrs *ifaddr, *ifa;
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
|
|
||||||
if (getifaddrs(&ifaddr) < 0) {
|
if (getifaddrs(&ifaddr) < 0) {
|
||||||
syslog(LOG_ERR, "cannot get list of addresses\n");
|
syslog(LOG_ERR, "cannot get list of addresses\n");
|
||||||
return false;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
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);
|
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.
|
#define OG_WOL_CLIENT_TIMEOUT 60.
|
||||||
|
|
||||||
static void og_client_wol_timer_cb(struct ev_loop *loop, ev_timer *timer,
|
static void og_client_wol_timer_cb(struct ev_loop *loop, ev_timer *timer,
|
||||||
|
|
11
src/wol.h
11
src/wol.h
|
@ -4,21 +4,20 @@
|
||||||
#define OG_WOL_SEQUENCE 6
|
#define OG_WOL_SEQUENCE 6
|
||||||
#define OG_WOL_MACADDR_LEN 6
|
#define OG_WOL_MACADDR_LEN 6
|
||||||
#define OG_WOL_REPEAT 16
|
#define OG_WOL_REPEAT 16
|
||||||
|
#define OG_WOL_PORT 9
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include <ev.h>
|
#include <ev.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
struct wol_msg {
|
struct wol_msg {
|
||||||
char secuencia_FF[OG_WOL_SEQUENCE];
|
char wol_sequence_ff[OG_WOL_SEQUENCE];
|
||||||
char macbin[OG_WOL_REPEAT][OG_WOL_MACADDR_LEN];
|
char mac_addr[OG_WOL_REPEAT][OG_WOL_MACADDR_LEN];
|
||||||
};
|
};
|
||||||
|
|
||||||
int wol_socket_open(void);
|
int wol_socket_open(void);
|
||||||
bool wake_up_send(int sd, struct sockaddr_in *client,
|
int wake_up(int s, const struct in_addr *addr, const struct in_addr *netmask,
|
||||||
const struct wol_msg *msg, const struct in_addr *addr);
|
const char *mac, uint32_t wol_delivery_type);
|
||||||
bool wake_up_broadcast(int sd, struct sockaddr_in *client,
|
|
||||||
const struct wol_msg *msg);
|
|
||||||
|
|
||||||
struct og_client_wol {
|
struct og_client_wol {
|
||||||
struct list_head list;
|
struct list_head list;
|
||||||
|
|
Loading…
Reference in New Issue