diff options
author | OpenGnSys Support Team <soporte-og@soleta.eu> | 2020-07-06 11:12:43 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2020-07-06 11:14:43 +0200 |
commit | 96b02b5424db61c32386c48fe05ec3375d8a84f5 (patch) | |
tree | e65fbc4e81effc9b245982c7de81d0ad0157cc96 | |
parent | 06af0c26f4c46088d8a6f97877f865527bcbc409 (diff) |
#971 split wake on lan code
Add wol.c and wol.h that implements WakeOnLan.
-rw-r--r-- | Makefile.am | 3 | ||||
-rw-r--r-- | src/ogAdmServer.c | 72 | ||||
-rw-r--r-- | src/wol.c | 67 | ||||
-rw-r--r-- | src/wol.h | 20 |
4 files changed, 93 insertions, 69 deletions
diff --git a/Makefile.am b/Makefile.am index 37b871e..5a38431 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 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 <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"); 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 <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); +} 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 <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 |