source: ogServer-Git/src/wol.c @ 1855b68

Last change on this file since 1855b68 was 96b02b5, checked in by OpenGnSys Support Team <soporte-og@…>, 4 years ago

#971 split wake on lan code

Add wol.c and wol.h that implements WakeOnLan?.

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/*
2 * Copyright (C) 2020 Soleta Networks <info@soleta.eu>
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Affero General Public License as published by the
6 * Free Software Foundation, version 3.
7 */
8#include <sys/types.h>
9#include <ifaddrs.h>
10#include <string.h>
11#include <netinet/in.h>
12#include <sys/socket.h>
13#include <syslog.h>
14#include <stddef.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <sys/types.h>
18#include <sys/socket.h>
19#include <fcntl.h>
20#include "wol.h"
21#include "ogAdmServer.h"
22
23bool wake_up_send(int sd, struct sockaddr_in *client,
24                  const struct wol_msg *msg, const struct in_addr *addr)
25{
26        int ret;
27
28        client->sin_addr.s_addr = addr->s_addr;
29
30        ret = sendto(sd, msg, sizeof(*msg), 0,
31                     (struct sockaddr *)client, sizeof(*client));
32        if (ret < 0) {
33                syslog(LOG_ERR, "failed to send wol\n");
34                return false;
35        }
36
37        return true;
38}
39
40bool wake_up_broadcast(int sd, struct sockaddr_in *client,
41                       const struct wol_msg *msg)
42{
43        struct sockaddr_in *broadcast_addr, addr = {};
44        struct ifaddrs *ifaddr, *ifa;
45
46        if (getifaddrs(&ifaddr) < 0) {
47                syslog(LOG_ERR, "cannot get list of addresses\n");
48                return false;
49        }
50
51        addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
52
53        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
54                if (ifa->ifa_addr == NULL ||
55                    ifa->ifa_addr->sa_family != AF_INET ||
56                    strcmp(ifa->ifa_name, interface) != 0)
57                        continue;
58
59                broadcast_addr =
60                        (struct sockaddr_in *)ifa->ifa_ifu.ifu_broadaddr;
61                addr.sin_addr.s_addr = broadcast_addr->sin_addr.s_addr;
62                break;
63        }
64        freeifaddrs(ifaddr);
65
66        return wake_up_send(sd, client, msg, &addr.sin_addr);
67}
Note: See TracBrowser for help on using the repository browser.