-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathWakeOnLanManager.cpp
More file actions
209 lines (174 loc) · 6.8 KB
/
WakeOnLanManager.cpp
File metadata and controls
209 lines (174 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "WakeOnLanManager.hpp"
#include "Data.hpp"
#include "Settings.hpp"
#include <borealis.hpp>
#include <cerrno>
#include <cstring>
#if defined(__linux) || defined(__APPLE__) || defined(__SWITCH__) || defined(__vita__)
#define UNIX_SOCKS
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#elif defined(_WIN32)
#define WIN32_SOCKS
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#if defined(__SWITCH__)
#include <switch.h>
#endif
#if defined(UNIX_SOCKS) || defined(WIN32_SOCKS)
static Data mac_string_to_bytes(std::string mac) {
std::string str = mac;
std::string pattern = ":";
std::string::size_type i = str.find(pattern);
while (i != std::string::npos) {
str.erase(i, pattern.length());
i = str.find(pattern, i);
}
return Data((unsigned char*)str.c_str(), str.length()).hex_to_bytes();
}
static Data create_payload(const Host& host) {
Data payload;
// 6 bytes of FF
uint8_t header = 0xFF;
for (int i = 0; i < 6; i++) {
payload = payload.append(Data(&header, 1));
}
// 16 repetitions of MAC address
Data mac_address = mac_string_to_bytes(host.mac);
for (int i = 0; i < 16; i++) {
payload = payload.append(mac_address);
}
return payload;
}
#endif
#if defined(UNIX_SOCKS)
GSResult<bool> send_packet_unix(const Host& host, const Data& payload) {
struct sockaddr_in udpClient{}, udpServer{};
int broadcast = 1;
int udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSocket == -1) {
brls::Logger::error(
"WakeOnLanManager: An error was encountered creating "
"the UDP socket: '{}'",
strerror(errno));
return GSResult<bool>::failure(
"An error was encountered creating the UDP socket: " +
std::string(strerror(errno)));
}
int setsock_result = setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast);
if (setsock_result == -1) {
brls::Logger::error(
"WakeOnLanManager: Failed to set socket options: '{}'",
strerror(errno));
return GSResult<bool>::failure("Failed to set socket options: " +
std::string(strerror(errno)));
}
// Set parameters
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = 0;
// Bind socket
int bind_result =
bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));
if (bind_result == -1) {
brls::Logger::error("WakeOnLanManager: Failed to bind socket: '{}'",
strerror(errno));
return GSResult<bool>::failure("Failed to bind socket: " +
std::string(strerror(errno)));
}
// Send to local broadcast address
udpServer.sin_family = AF_INET;
uint32_t ip, subnet_mask;
nifmGetCurrentIpConfigInfo(&ip, &subnet_mask, nullptr, nullptr, nullptr);
udpServer.sin_addr.s_addr = ip | ~subnet_mask; // Local broadcast address
udpServer.sin_port = htons(9);
brls::Logger::info("WakeOnLanManager: Sending magic packet to local broadcast address: '{}'",
inet_ntoa(udpServer.sin_addr));
// Send the WoL packet to the local broadcast address
ssize_t result = sendto(udpSocket, payload.bytes(), sizeof(unsigned char) * 102, 0,
(struct sockaddr*)&udpServer, sizeof(udpServer));
if (result == -1) {
brls::Logger::error(
"WakeOnLanManager: Failed to send magic packet to socket: '{}'",
strerror(errno));
return GSResult<bool>::failure(
"Failed to send magic packet to socket: " +
std::string(strerror(errno)));
}
// Send to the public IP address
udpServer.sin_addr.s_addr = inet_addr(host.address.c_str());
brls::Logger::info("WakeOnLanManager: Sending magic packet to public IP: '{}'",
inet_ntoa(udpServer.sin_addr));
result = sendto(udpSocket, payload.bytes(), sizeof(unsigned char) * 102, 0,
(struct sockaddr*)&udpServer, sizeof(udpServer));
if (result == -1) {
brls::Logger::error(
"WakeOnLanManager: Failed to send magic packet to socket: '{}'",
strerror(errno));
return GSResult<bool>::failure(
"Failed to send magic packet to socket: " +
std::string(strerror(errno)));
}
return GSResult<bool>::success(true);
}
#elif defined(_WIN32)
GSResult<bool> send_packet_win32(const Host& host, const Data& payload) {
struct sockaddr_in udpClient, udpServer;
int broadcast = 1;
WSADATA data;
SOCKET udpSocket;
// Setup broadcast socket
WSAStartup(MAKEWORD(2, 2), &data);
udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast,
sizeof(broadcast)) == -1) {
return GSResult<bool>::failure("Failed to setup a broadcast socket");
}
// Set parameters
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = htons(0);
// Bind socket
bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));
// Send to local broadcast address
udpServer.sin_family = AF_INET;
uint32_t ip, subnet_mask;
nifmGetCurrentIpConfigInfo(&ip, &subnet_mask, nullptr, nullptr, nullptr);
udpServer.sin_addr.s_addr = ip | ~subnet_mask; // Local broadcast address
udpServer.sin_port = htons(9);
brls::Logger::info("WakeOnLanManager: Sending magic packet to local broadcast address: '{}'",
inet_ntoa(udpServer.sin_addr));
// Send the WoL packet to the local broadcast address
sendto(udpSocket, (const char*)payload.bytes(), sizeof(unsigned char) * 102,
0, (struct sockaddr*)&udpServer, sizeof(udpServer));
// Send to the public IP address
udpServer.sin_addr.s_addr = inet_addr(host.address.c_str());
brls::Logger::info("WakeOnLanManager: Sending magic packet to public IP: '{}'",
inet_ntoa(udpServer.sin_addr));
sendto(udpSocket, (const char*)payload.bytes(), sizeof(unsigned char) * 102,
0, (struct sockaddr*)&udpServer, sizeof(udpServer));
return GSResult<bool>::success(true);
}
#endif
bool WakeOnLanManager::can_wake_up_host(const Host& host) {
#if defined(UNIX_SOCKS) || defined(WIN32_SOCKS)
return true;
#else
return false;
#endif
}
GSResult<bool> WakeOnLanManager::wake_up_host(const Host& host) {
Data payload = create_payload(host);
#if defined(UNIX_SOCKS)
return send_packet_unix(host, payload);
#elif defined(_WIN32)
return send_packet_win32(host, payload);
#endif
return GSResult<bool>::failure("Wake up host not supported...");
}