#971 split wake on lan code

Add wol.c and wol.h that implements WakeOnLan.
master
OpenGnSys Support Team 2020-07-06 11:12:43 +02:00
parent 06af0c26f4
commit 96b02b5424
4 changed files with 93 additions and 69 deletions

View File

@ -12,4 +12,5 @@ ogserver_SOURCES= src/ogAdmServer.c \
src/rest.c \
src/client.c \
src/json.c \
src/ogAdmLib.c
src/ogAdmLib.c \
src/wol.c

View File

@ -14,9 +14,9 @@
#include "client.h"
#include "json.h"
#include "schedule.h"
#include "wol.h"
#include <syslog.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@ -483,70 +483,6 @@ bool Levanta(char *ptrIP[], char *ptrMacs[], char *ptrNetmasks[], int lon,
return true;
}
#define OG_WOL_SEQUENCE 6
#define OG_WOL_MACADDR_LEN 6
#define OG_WOL_REPEAT 16
struct wol_msg {
char secuencia_FF[OG_WOL_SEQUENCE];
char macbin[OG_WOL_REPEAT][OG_WOL_MACADDR_LEN];
};
static bool wake_up_broadcast(int sd, struct sockaddr_in *client,
const struct wol_msg *msg)
{
struct sockaddr_in *broadcast_addr;
struct ifaddrs *ifaddr, *ifa;
int ret;
if (getifaddrs(&ifaddr) < 0) {
syslog(LOG_ERR, "cannot get list of addresses\n");
return false;
}
client->sin_addr.s_addr = htonl(INADDR_BROADCAST);
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL ||
ifa->ifa_addr->sa_family != AF_INET ||
strcmp(ifa->ifa_name, interface) != 0)
continue;
broadcast_addr =
(struct sockaddr_in *)ifa->ifa_ifu.ifu_broadaddr;
client->sin_addr.s_addr = broadcast_addr->sin_addr.s_addr;
break;
}
freeifaddrs(ifaddr);
ret = sendto(sd, msg, sizeof(*msg), 0,
(struct sockaddr *)client, sizeof(*client));
if (ret < 0) {
syslog(LOG_ERR, "failed to send broadcast wol\n");
return false;
}
return true;
}
static bool wake_up_unicast(int sd, struct sockaddr_in *client,
const struct wol_msg *msg,
const struct in_addr *addr)
{
int ret;
client->sin_addr.s_addr = addr->s_addr;
ret = sendto(sd, msg, sizeof(*msg), 0,
(struct sockaddr *)client, sizeof(*client));
if (ret < 0) {
syslog(LOG_ERR, "failed to send unicast wol\n");
return false;
}
return true;
}
enum wol_delivery_type {
OG_WOL_BROADCAST = 1,
OG_WOL_UNICAST = 2
@ -609,11 +545,11 @@ bool WakeUp(int s, char* iph, char *mac, char *netmask, char *mar)
switch (atoi(mar)) {
case OG_WOL_BROADCAST:
ret = wake_up_broadcast(s, &WakeUpCliente, &Trama_WakeUp);
ret &= wake_up_unicast(s, &WakeUpCliente, &Trama_WakeUp,
&broadcast_addr);
ret &= wake_up_send(s, &WakeUpCliente, &Trama_WakeUp,
&broadcast_addr);
break;
case OG_WOL_UNICAST:
ret = wake_up_unicast(s, &WakeUpCliente, &Trama_WakeUp, &addr);
ret = wake_up_send(s, &WakeUpCliente, &Trama_WakeUp, &addr);
break;
default:
syslog(LOG_ERR, "unknown wol type\n");

67
src/wol.c 100644
View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2020 Soleta Networks <info@soleta.eu>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, version 3.
*/
#include <sys/types.h>
#include <ifaddrs.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <syslog.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include "wol.h"
#include "ogAdmServer.h"
bool wake_up_send(int sd, struct sockaddr_in *client,
const struct wol_msg *msg, const struct in_addr *addr)
{
int ret;
client->sin_addr.s_addr = addr->s_addr;
ret = sendto(sd, msg, sizeof(*msg), 0,
(struct sockaddr *)client, sizeof(*client));
if (ret < 0) {
syslog(LOG_ERR, "failed to send wol\n");
return false;
}
return true;
}
bool 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;
}
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL ||
ifa->ifa_addr->sa_family != AF_INET ||
strcmp(ifa->ifa_name, interface) != 0)
continue;
broadcast_addr =
(struct sockaddr_in *)ifa->ifa_ifu.ifu_broadaddr;
addr.sin_addr.s_addr = broadcast_addr->sin_addr.s_addr;
break;
}
freeifaddrs(ifaddr);
return wake_up_send(sd, client, msg, &addr.sin_addr);
}

20
src/wol.h 100644
View File

@ -0,0 +1,20 @@
#ifndef _OG_WOL_H_
#define _OG_WOL_H_
#define OG_WOL_SEQUENCE 6
#define OG_WOL_MACADDR_LEN 6
#define OG_WOL_REPEAT 16
#include <stdbool.h>
struct wol_msg {
char secuencia_FF[OG_WOL_SEQUENCE];
char macbin[OG_WOL_REPEAT][OG_WOL_MACADDR_LEN];
};
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);
#endif