-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbroadcast.cpp
More file actions
243 lines (201 loc) · 6.98 KB
/
broadcast.cpp
File metadata and controls
243 lines (201 loc) · 6.98 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "mach/broadcast.hpp"
#include <iostream>
#include <cstring>
#include <spdlog/spdlog.h>
#include <string>
#ifdef _WIN32
#include <winsock2.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <unistd.h>
#endif
static std::string broadcastIp = "";
#ifdef _WIN32
static std::string getBroadcastAddress() {
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> adapter_addresses;
ULONG size = 0;
DWORD result = GetAdaptersAddresses(AF_INET, 0, nullptr, nullptr, &size);
if (result == ERROR_BUFFER_OVERFLOW) {
adapter_addresses = std::make_unique<IP_ADAPTER_ADDRESSES[]>(size);
result = GetAdaptersAddresses(AF_INET, 0, nullptr, adapter_addresses.get(), &size);
}
if (result != ERROR_SUCCESS) {
spdlog::error("MACH: GetAdaptersAddresses failed with error: {}", result);
return "";
}
std::string broadcast_address;
IP_ADAPTER_ADDRESSES* adapter = adapter_addresses.get();
// Find an adapter where the assigned ip is 192.168.x.x.
while (adapter) {
IP_ADAPTER_UNICAST_ADDRESS* unicast_address = adapter->FirstUnicastAddress;
while (unicast_address) {
sockaddr_in* addr = (sockaddr_in*)unicast_address->Address.lpSockaddr;
if (!addr || addr->sin_family != AF_INET || addr->sin_addr.S_un.S_un_b.s_b1 != 192 || addr->sin_addr.S_un.S_un_b.s_b2 != 168) {
unicast_address = unicast_address->Next;
continue;
}
char ip_str[INET_ADDRSTRLEN];
char mask_str[INET_ADDRSTRLEN];
// Convert IP address to string.
inet_ntop(AF_INET, &addr->sin_addr, ip_str, INET_ADDRSTRLEN);
// Get the subnet mask.
DWORD prefixLength = unicast_address->OnLinkPrefixLength;
DWORD maskLong = 0xFFFFFFFF << (32 - prefixLength);
sockaddr_in mask_addr = { 0 };
mask_addr.sin_addr.s_addr = htonl(maskLong);
inet_ntop(AF_INET, &mask_addr.sin_addr, mask_str, INET_ADDRSTRLEN);
// Convert to in_addr.
in_addr ip, mask, broadcast;
inet_pton(AF_INET, ip_str, &ip);
inet_pton(AF_INET, mask_str, &mask);
// Calculate broadcast address
broadcast.s_addr = (ip.s_addr & mask.s_addr) | (~mask.s_addr);
char broadcast_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &broadcast, broadcast_ip, INET_ADDRSTRLEN);
broadcast_address = std::string(broadcast_ip);
break;
}
if (!broadcast_address.empty()) {
break;
}
adapter = adapter->Next;
}
return broadcast_address;
}
#else
static std::string getBroadcastAddress() {
struct ifaddrs* ifaddr;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
spdlog::error("MACH: Failed to get network interfaces!");
return "";
}
std::string broadcast_address = "";
// Find an adapter where the assigned ip is 192.168.x.x.
for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) {
continue;
}
struct sockaddr_in* addr = (struct sockaddr_in*)ifa->ifa_addr;
// int first = addr->sin_addr.s_addr >> 24;
// int second = addr->sin_addr.s_addr >> 16 & 0xFF;
// int third = addr->sin_addr.s_addr >> 8 & 0xFF;
// int fourth = addr->sin_addr.s_addr & 0xFF;
// spdlog::info("Found: {}.{}.{}.{}", first, second, third, fourth);
if (addr->sin_addr.s_addr & 0xFF != 192 || (addr->sin_addr.s_addr >> 8 & 0xFF) != 168) {
continue;
}
struct sockaddr_in* netmask = (struct sockaddr_in*)ifa->ifa_netmask;
struct sockaddr_in broadcast;
broadcast.sin_family = AF_INET;
// Calculate broadcast address.
broadcast.sin_addr.s_addr = (addr->sin_addr.s_addr & netmask->sin_addr.s_addr) | (~netmask->sin_addr.s_addr);
char broadcast_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &broadcast.sin_addr, broadcast_ip, INET_ADDRSTRLEN);
broadcast_address = std::string(broadcast_ip);
break;
}
freeifaddrs(ifaddr);
return broadcast_address;
}
#endif
static int initSocket() {
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
spdlog::error("MACH: Broadcast WSAStartup failed.");
return -1;
}
#endif
return 0;
}
static void cleanupSocket() {
#ifdef _WIN32
WSACleanup();
#endif
}
static int createBroadcastSocket() {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
spdlog::error("MACH: Broadcast socket creation failed.");
return -1;
}
int broadcastEnable = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcastEnable, sizeof(broadcastEnable)) < 0) {
spdlog::error("MACH: Error in setting broadcast options.");
#ifdef _WIN32
closesocket(sock);
#else
close(sock);
#endif
return -1;
}
return sock;
}
static struct sockaddr_in setupBroadcastAddress(std::string broadcastIp) {
struct sockaddr_in broadcastAddr = {};
broadcastAddr.sin_family = AF_INET;
broadcastAddr.sin_port = htons(6969);
broadcastAddr.sin_addr.s_addr = inet_addr(broadcastIp.c_str());
return broadcastAddr;
}
static void sendBroadcastMessage(int sock, const struct sockaddr_in& broadcastAddr) {
std::string message("MACH Engine Computer");
if (sendto(sock, message.c_str(), message.length(), 0, (struct sockaddr *)&broadcastAddr, sizeof(broadcastAddr)) < 0) {
spdlog::error("MACH: Broadcast send failed.");
#ifdef _WIN32
closesocket(sock);
#else
close(sock);
#endif
return;
}
// spdlog::info("MACH: Broadcast message sent.");
}
static void closeSocket(int sock) {
#ifdef _WIN32
closesocket(sock);
#else
close(sock);
#endif
}
static int broadcastServer() {
if (initSocket() < 0) {
return -1;
}
int sock = createBroadcastSocket();
if (sock < 0) {
cleanupSocket();
return -1;
}
std::string newBroadcastIp = getBroadcastAddress();
if (broadcastIp != newBroadcastIp) {
broadcastIp = newBroadcastIp;
spdlog::info("MACH: Broadcasting server on broadcast IP: {}", broadcastIp);
}
struct sockaddr_in broadcastAddr = setupBroadcastAddress(broadcastIp);
sendBroadcastMessage(sock, broadcastAddr);
closeSocket(sock);
cleanupSocket();
return 0;
}
/**
* Broadcasts the server to the network on a 5 second interval.
*/
static void _startBroadcastingServer() {
while (true) {
broadcastServer();
std::this_thread::sleep_for(std::chrono::seconds(5));
}
}
void startBroadcastingServer() {
std::thread(_startBroadcastingServer).detach();
}