1 | /* |
---|
2 | * Copyright (C) 2020-2021 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; either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | */ |
---|
9 | #include <sys/types.h> |
---|
10 | #include <ifaddrs.h> |
---|
11 | #include <string.h> |
---|
12 | #include <netinet/in.h> |
---|
13 | #include <sys/socket.h> |
---|
14 | #include <syslog.h> |
---|
15 | #include <stddef.h> |
---|
16 | #include <sys/types.h> |
---|
17 | #include <sys/stat.h> |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/socket.h> |
---|
20 | #include <fcntl.h> |
---|
21 | #include "wol.h" |
---|
22 | #include "rest.h" |
---|
23 | #include "cfg.h" |
---|
24 | #include "ogAdmServer.h" |
---|
25 | |
---|
26 | int wol_socket_open(void) |
---|
27 | { |
---|
28 | unsigned int on = 1; |
---|
29 | int ret, s; |
---|
30 | |
---|
31 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
---|
32 | if (s < 0) { |
---|
33 | syslog(LOG_ERR, "cannot create socket for magic packet\n"); |
---|
34 | return -1; |
---|
35 | } |
---|
36 | ret = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (unsigned int *) &on, |
---|
37 | sizeof(on)); |
---|
38 | if (ret < 0) { |
---|
39 | syslog(LOG_ERR, "cannot set broadcast socket\n"); |
---|
40 | return -1; |
---|
41 | } |
---|
42 | |
---|
43 | return s; |
---|
44 | } |
---|
45 | |
---|
46 | bool wake_up_send(int sd, struct sockaddr_in *client, |
---|
47 | const struct wol_msg *msg, const struct in_addr *addr) |
---|
48 | { |
---|
49 | int ret; |
---|
50 | |
---|
51 | client->sin_addr.s_addr = addr->s_addr; |
---|
52 | |
---|
53 | ret = sendto(sd, msg, sizeof(*msg), 0, |
---|
54 | (struct sockaddr *)client, sizeof(*client)); |
---|
55 | if (ret < 0) { |
---|
56 | syslog(LOG_ERR, "failed to send wol\n"); |
---|
57 | return false; |
---|
58 | } |
---|
59 | |
---|
60 | return true; |
---|
61 | } |
---|
62 | |
---|
63 | bool wake_up_broadcast(int sd, struct sockaddr_in *client, |
---|
64 | const struct wol_msg *msg) |
---|
65 | { |
---|
66 | struct sockaddr_in *broadcast_addr, addr = {}; |
---|
67 | struct ifaddrs *ifaddr, *ifa; |
---|
68 | |
---|
69 | if (getifaddrs(&ifaddr) < 0) { |
---|
70 | syslog(LOG_ERR, "cannot get list of addresses\n"); |
---|
71 | return false; |
---|
72 | } |
---|
73 | |
---|
74 | addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
---|
75 | |
---|
76 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
---|
77 | if (ifa->ifa_addr == NULL || |
---|
78 | ifa->ifa_addr->sa_family != AF_INET || |
---|
79 | strcmp(ifa->ifa_name, ogconfig.wol.interface) != 0) |
---|
80 | continue; |
---|
81 | |
---|
82 | broadcast_addr = |
---|
83 | (struct sockaddr_in *)ifa->ifa_ifu.ifu_broadaddr; |
---|
84 | addr.sin_addr.s_addr = broadcast_addr->sin_addr.s_addr; |
---|
85 | break; |
---|
86 | } |
---|
87 | freeifaddrs(ifaddr); |
---|
88 | |
---|
89 | return wake_up_send(sd, client, msg, &addr.sin_addr); |
---|
90 | } |
---|
91 | |
---|
92 | #define OG_WOL_CLIENT_TIMEOUT 60. |
---|
93 | |
---|
94 | static void og_client_wol_timer_cb(struct ev_loop *loop, ev_timer *timer, |
---|
95 | int events) |
---|
96 | { |
---|
97 | struct og_client_wol *cli_wol; |
---|
98 | |
---|
99 | cli_wol = container_of(timer, struct og_client_wol, timer); |
---|
100 | |
---|
101 | syslog(LOG_ERR, "timeout WakeOnLAN request for client %s\n", |
---|
102 | inet_ntoa(cli_wol->addr)); |
---|
103 | og_client_wol_destroy(cli_wol); |
---|
104 | } |
---|
105 | |
---|
106 | struct og_client_wol *og_client_wol_create(const struct in_addr *addr) |
---|
107 | { |
---|
108 | struct og_client_wol *cli_wol; |
---|
109 | |
---|
110 | cli_wol = calloc(1, sizeof(struct og_client_wol)); |
---|
111 | if (!cli_wol) |
---|
112 | return NULL; |
---|
113 | |
---|
114 | cli_wol->addr = *addr; |
---|
115 | |
---|
116 | ev_init(&cli_wol->timer, og_client_wol_timer_cb); |
---|
117 | cli_wol->timer.repeat = OG_WOL_CLIENT_TIMEOUT; |
---|
118 | ev_timer_again(og_loop, &cli_wol->timer); |
---|
119 | |
---|
120 | return cli_wol; |
---|
121 | } |
---|
122 | |
---|
123 | void og_client_wol_refresh(struct og_client_wol *cli_wol) |
---|
124 | { |
---|
125 | ev_timer_again(og_loop, &cli_wol->timer); |
---|
126 | } |
---|
127 | |
---|
128 | void og_client_wol_destroy(struct og_client_wol *cli_wol) |
---|
129 | { |
---|
130 | ev_timer_stop(og_loop, &cli_wol->timer); |
---|
131 | list_del(&cli_wol->list); |
---|
132 | free(cli_wol); |
---|
133 | } |
---|
134 | |
---|
135 | const char *og_client_wol_status(const struct og_client_wol *wol) |
---|
136 | { |
---|
137 | return "WOL_SENT"; |
---|
138 | } |
---|