Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WOL and WOW depending on the platform #212

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:

Expand Down Expand Up @@ -54,4 +55,4 @@ jobs:
uses: actions/upload-artifact@master
with:
name: Debug.elf
path: build/switch/Moonlight.elf
path: build/switch/Moonlight.elf
62 changes: 40 additions & 22 deletions app/src/streaming/WakeOnLanManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static Data create_payload(const Host& host) {
payload = payload.append(Data(&header, 1));
}

// 16 repitiions of MAC address
// 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);
Expand Down Expand Up @@ -98,25 +98,40 @@ GSResult<bool> send_packet_unix(const Host& host, const Data& payload) {
std::string(strerror(errno)));
}

// Set server end point (the broadcast addres)
udpServer.sin_family = AF_INET;
// Send to local broadcast address
#if defined(__SWITCH__)
uint32_t ip, subnet_mask;
// Get the current IP address and subnet mask to calculate subnet broadcast address
nifmGetCurrentIpConfigInfo(&ip, &subnet_mask, nullptr, nullptr, nullptr);
udpServer.sin_addr.s_addr = ip | ~subnet_mask;
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = ip | ~subnet_mask; // Local broadcast address
brls::Logger::info("WakeOnLanManager: Sending magic packet to local broadcast address: '{}'",
inet_ntoa(udpServer.sin_addr));
#else
udpServer.sin_addr.s_addr = inet_addr(host.address.c_str());
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = htonl(INADDR_BROADCAST); // Default broadcast address for other platforms
brls::Logger::info("WakeOnLanManager: Sending magic packet to default broadcast address");
#endif
udpServer.sin_port = htons(9);

brls::Logger::info("WakeOnLanManager: Sending magic packet to: '{}'",
// 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));

// Send the packet
ssize_t result =
sendto(udpSocket, payload.bytes(), sizeof(unsigned char) * 102, 0,
(struct sockaddr*)&udpServer, sizeof(udpServer));
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: '{}'",
Expand All @@ -125,6 +140,7 @@ GSResult<bool> send_packet_unix(const Host& host, const Data& payload) {
"Failed to send magic packet to socket: " +
std::string(strerror(errno)));
}

return GSResult<bool>::success(true);
}
#elif defined(_WIN32)
Expand All @@ -151,24 +167,26 @@ GSResult<bool> send_packet_win32(const Host& host, const Data& payload) {
// Bind socket
bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));

// Set server end point (the broadcast addres)
// Send to local broadcast address
udpServer.sin_family = AF_INET;
#if defined(__SWITCH__)
uint32_t ip, subnet_mask;
// Get the current IP address and subnet mask to calculate subnet broadcast address
nifmGetCurrentIpConfigInfo(&ip, &subnet_mask, nullptr, nullptr, nullptr);
udpServer.sin_addr.s_addr = ip | ~subnet_mask;
#else
udpServer.sin_addr.s_addr = inet_addr(host.address.c_str());
#endif
udpServer.sin_addr.s_addr = htonl(INADDR_BROADCAST); // Default broadcast address
udpServer.sin_port = htons(9);

brls::Logger::info("WakeOnLanManager: Sending magic packet to: '{}'",
brls::Logger::info("WakeOnLanManager: Sending magic packet to local broadcast address: '{}'",
inet_ntoa(udpServer.sin_addr));

// Send the packet
// 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
Expand Down