From 96b02b5424db61c32386c48fe05ec3375d8a84f5 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Mon, 6 Jul 2020 11:12:43 +0200 Subject: #971 split wake on lan code Add wol.c and wol.h that implements WakeOnLan. --- src/ogAdmServer.c | 72 ++++--------------------------------------------------- src/wol.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/wol.h | 20 ++++++++++++++++ 3 files changed, 91 insertions(+), 68 deletions(-) create mode 100644 src/wol.c create mode 100644 src/wol.h (limited to 'src') diff --git a/src/ogAdmServer.c b/src/ogAdmServer.c index f063eb1..f8a50ae 100644 --- a/src/ogAdmServer.c +++ b/src/ogAdmServer.c @@ -14,9 +14,9 @@ #include "client.h" #include "json.h" #include "schedule.h" +#include "wol.h" #include #include -#include #include #include #include @@ -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"); diff --git a/src/wol.c b/src/wol.c new file mode 100644 index 0000000..79bfaa3 --- /dev/null +++ b/src/wol.c @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2020 Soleta Networks + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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); +} diff --git a/src/wol.h b/src/wol.h new file mode 100644 index 0000000..c15cf3a --- /dev/null +++ b/src/wol.h @@ -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 + +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 -- cgit v1.2.3-18-g5258