-
Notifications
You must be signed in to change notification settings - Fork 21
/
WinIPBroadcast.c
502 lines (393 loc) · 13 KB
/
WinIPBroadcast.c
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright (C) 2009 Etienne Dechamps
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// WinIPBroadcast 1.6 by Etienne Dechamps <etienne@edechamps.fr>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <Winsock2.h>
#include <Mswsock.h>
#include <Ws2tcpip.h>
#include <Iphlpapi.h>
#include <windows.h>
#define SERVICE_NAME TEXT("WinIPBroadcast")
#define SERVICE_DESC (TEXT("Sends global IP broadcast packets to all network interfaces instead of just the preferred route. If this service is disabled, applications using global IP broadcast (e.g. server browsers) might not function properly."))
#define IP_HEADER_SIZE 20
#define IP_SRCADDR_POS 12
#define IP_DSTADDR_POS 16
#define IP_TTL_POS 8
#define UDP_HEADER_SIZE 8
#define UDP_CHECKSUM_POS 6
#define FORWARDTABLE_INITIAL_SIZE 4096
HANDLE mainThread;
SERVICE_STATUS_HANDLE serviceStatus;
ULONG loopbackAddress;
ULONG broadcastAddress;
SOCKET listenSocket;
PMIB_IPFORWARDTABLE forwardTable;
ULONG forwardTableSize;
void quit(void);
LPTSTR errorString(int err)
{
static TCHAR buffer[1024];
if (!FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
0,
buffer,
sizeof(buffer),
NULL
))
wcsncpy_s(buffer, sizeof(buffer), TEXT("(cannot format error message)"), sizeof(buffer));
return buffer;
}
void socketError(LPTSTR func, BOOL fatal)
{
fwprintf(stderr, TEXT("%serror: %s failed with error code %d: %s"), (fatal ? TEXT("fatal ") : TEXT("")), func, WSAGetLastError(), errorString(WSAGetLastError()));
if (fatal)
quit();
}
void systemError(LPTSTR func, BOOL fatal)
{
fwprintf(stderr, TEXT("%serror: %s failed with error code %d: %s"), (fatal ? TEXT("fatal ") : TEXT("")), func, GetLastError(), errorString(GetLastError()));
if (fatal)
quit();
}
void initListenSocket(void)
{
struct sockaddr_in addr;
listenSocket = WSASocket(AF_INET, SOCK_RAW, IPPROTO_UDP, NULL, 0, 0);
if (listenSocket == INVALID_SOCKET)
socketError(TEXT("WSASocket"), TRUE);
addr.sin_family = AF_INET;
addr.sin_port = 0;
addr.sin_addr.s_addr = loopbackAddress;
if (bind(listenSocket, (SOCKADDR *)&addr, sizeof(addr)) == SOCKET_ERROR)
socketError(TEXT("bind"), TRUE);
}
void getForwardTable()
{
int result;
retry:
result = GetIpForwardTable(forwardTable, &forwardTableSize, FALSE);
switch (result)
{
case NO_ERROR:
break;
case ERROR_INSUFFICIENT_BUFFER:
free(forwardTable);
forwardTable = malloc(forwardTableSize);
goto retry;
default:
fwprintf(stderr, TEXT("error: GetIpForwardTable failed with error code %d: %s"), result, errorString(result));
quit();
}
}
BOOL findLocalAddressInBroadcastRoutes(const ULONG *srcAddress) {
for (DWORD i = 0; i < forwardTable->dwNumEntries; i++)
{
PMIB_IPFORWARDROW row = &forwardTable->table[i];
if (row->dwForwardDest != broadcastAddress)
continue;
if (row->dwForwardMask != ULONG_MAX)
continue;
if (row->dwForwardType != MIB_IPROUTE_TYPE_DIRECT)
continue;
if (row->dwForwardNextHop == *srcAddress)
return TRUE;
}
return FALSE;
}
DWORD getBroadcastPacket(char *buffer, size_t size, ULONG *srcAddress)
{
DWORD len;
WSABUF wsaBuffer;
DWORD flags;
wsaBuffer.buf = buffer;
wsaBuffer.len = size;
flags = 0;
getForwardTable();
while (TRUE)
{
if (WSARecv(listenSocket, &wsaBuffer, 1, &len, &flags, NULL, NULL) != 0)
socketError(TEXT("WSARecvMsg"), TRUE);
if (len < IP_HEADER_SIZE + UDP_HEADER_SIZE)
continue;
if (*((ULONG *)&buffer[IP_DSTADDR_POS]) != broadcastAddress)
continue;
*srcAddress = *((ULONG *) &buffer[IP_SRCADDR_POS]);
// We will also receive broadcast packets coming from the outside. Filter those out.
if (!findLocalAddressInBroadcastRoutes(srcAddress))
continue;
break;
}
return len;
}
void computeUdpChecksum(char *payload, uint16_t payloadSize, DWORD srcAddress, DWORD dstAddress)
{
WORD *buf = (WORD *)payload;
uint16_t length = payloadSize;
WORD *src = (WORD *)&srcAddress, *dst = (WORD *)&dstAddress;
DWORD checksum;
*(WORD *)&payload[UDP_CHECKSUM_POS] = 0;
checksum = 0;
while (length > 1)
{
checksum += *buf++;
if (checksum & 0x80000000)
checksum = (checksum & 0xFFFF) + (checksum >> 16);
length -= 2;
}
if (length & 1)
checksum += *((unsigned char *)buf);
checksum += *(src++);
checksum += *src;
checksum += *(dst++);
checksum += *dst;
checksum += htons(IPPROTO_UDP);
checksum += htons(payloadSize);
while (checksum >> 16)
checksum = (checksum & 0xFFFF) + (checksum >> 16);
*(WORD *)&payload[UDP_CHECKSUM_POS] = (WORD)(~checksum);
}
void sendBroadcast(ULONG srcAddress, char *payload, uint16_t payloadSize)
{
SOCKET socket;
WSABUF wsaBuffer;
ULONG block;
BOOL broadcastOpt;
DWORD len;
struct sockaddr_in srcAddr, dstAddr;
socket = WSASocket(AF_INET, SOCK_RAW, IPPROTO_UDP, NULL, 0, 0);
if (socket == INVALID_SOCKET)
{
socketError(TEXT("WSASocket"), FALSE);
closesocket(socket);
return;
}
block = 0;
if (WSAIoctl(socket, FIONBIO, &block, sizeof(block), NULL, 0, &len, NULL, NULL) == SOCKET_ERROR)
{
socketError(TEXT("WSAIoctl(FIONBIO)"), FALSE);
closesocket(socket);
return;
}
srcAddr.sin_family = AF_INET;
srcAddr.sin_port = 0;
srcAddr.sin_addr.s_addr = srcAddress;
if (bind(socket, (SOCKADDR *)&srcAddr, sizeof(srcAddr)) == SOCKET_ERROR)
{
socketError(TEXT("bind"), FALSE);
closesocket(socket);
return;
}
broadcastOpt = TRUE;
if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&broadcastOpt, sizeof(broadcastOpt)) == SOCKET_ERROR)
{
socketError(TEXT("setsockopt(SO_BROADCAST)"), FALSE);
closesocket(socket);
return;
}
// Any packet we send here will also appear in the input. This can result in an infinite relay loop.
// To avoid this, we explicitly set the minimum possible TTL. Incoming packets with this TTL will not be relayed.
//
// TODO: this is arguably not the best way to solve this problem, because it could also result in packets
// getting dropped along the route. Theoretically it should make no difference because broadcast packets can't be
// routed anyway (TTL or not), but one can imagine special setups where that might matter. There is also the
// problem that if the sending process has the same idea and also sets the TTL to 1, its packets won't be relayed.
// A better way would be to implement some kind of content-based deduplication, but that's way more complex.
DWORD ttl = 1;
if (setsockopt(socket, IPPROTO_IP, IP_TTL, (char*)&ttl, sizeof(ttl)) == SOCKET_ERROR)
{
socketError(TEXT("setsockopt(IP_TTL)"), FALSE);
closesocket(socket);
return;
}
dstAddr.sin_family = AF_INET;
dstAddr.sin_port = 0;
dstAddr.sin_addr.s_addr = broadcastAddress;
computeUdpChecksum(payload, payloadSize, srcAddress, broadcastAddress);
wsaBuffer.len = payloadSize;
wsaBuffer.buf = payload;
if (WSASendTo(socket, &wsaBuffer, 1, &len, 0, (SOCKADDR *)&dstAddr, sizeof(dstAddr), NULL, NULL) != 0)
if (WSAGetLastError() != WSAEWOULDBLOCK)
socketError(TEXT("WSASend"), FALSE);
closesocket(socket);
}
void relayBroadcast(char *payload, uint16_t payloadSize, ULONG srcAddress)
{
DWORD i;
PMIB_IPFORWARDROW row;
for (i = 0; i < forwardTable->dwNumEntries; i++)
{
row = &forwardTable->table[i];
if (row->dwForwardDest != broadcastAddress)
continue;
if (row->dwForwardMask != ULONG_MAX)
continue;
if (row->dwForwardType != MIB_IPROUTE_TYPE_DIRECT)
continue;
if (row->dwForwardNextHop == loopbackAddress || row->dwForwardNextHop == srcAddress)
continue;
sendBroadcast(row->dwForwardNextHop, payload, payloadSize);
}
}
void loop()
{
int result;
WORD version;
WSADATA wsaData;
char buffer[4096];
DWORD len;
ULONG srcAddress;
version = MAKEWORD(2, 2);
result = WSAStartup(version, &wsaData);
if (result != 0)
socketError(TEXT("WSAStartup"), TRUE);
loopbackAddress = inet_addr("127.0.0.1");
broadcastAddress = inet_addr("255.255.255.255");
forwardTable = malloc(FORWARDTABLE_INITIAL_SIZE);
forwardTableSize = FORWARDTABLE_INITIAL_SIZE;
initListenSocket();
while (TRUE)
{
len = getBroadcastPacket(buffer, sizeof(buffer), &srcAddress);
// If TTL is one, drop the packet to avoid relay loops.
if (*((uint8_t*)&buffer[IP_TTL_POS]) <= 1)
continue;
relayBroadcast(buffer + IP_HEADER_SIZE, (uint16_t)(len) - IP_HEADER_SIZE, srcAddress);
}
}
void serviceReport(DWORD state, DWORD exitCode)
{
SERVICE_STATUS status;
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
status.dwCurrentState = state;
status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
status.dwWin32ExitCode = exitCode;
status.dwCheckPoint = 0;
status.dwWaitHint = 0;
SetServiceStatus(serviceStatus, &status);
}
VOID WINAPI serviceControlHandler(DWORD controlCode)
{
switch (controlCode)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
serviceReport(SERVICE_STOPPED, NO_ERROR);
if (mainThread)
TerminateThread(mainThread, 0);
else
ExitProcess(0);
break;
case SERVICE_CONTROL_INTERROGATE:
serviceReport(SERVICE_RUNNING, NO_ERROR);
break;
}
}
VOID WINAPI serviceMain(DWORD argc, LPTSTR *argv)
{
serviceStatus = RegisterServiceCtrlHandler(SERVICE_NAME, serviceControlHandler);
if (!serviceStatus)
systemError(TEXT("RegisterServiceCtrlHandler"), FALSE);
serviceReport(SERVICE_RUNNING, NO_ERROR);
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &mainThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
loop();
}
void serviceInstall(void)
{
SC_HANDLE manager;
SC_HANDLE service;
TCHAR path[MAX_PATH];
SERVICE_DESCRIPTION description;
if (!GetModuleFileName(NULL, path, MAX_PATH))
systemError(TEXT("GetModuleFilename"), TRUE);
manager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (!manager)
systemError(TEXT("OpenSCManager"), TRUE);
service = CreateService(manager, SERVICE_NAME, TEXT("WinIPBroadcast"), SERVICE_CHANGE_CONFIG, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, path, NULL, NULL, NULL, TEXT("NT AUTHORITY\\LocalService"), NULL);
if (!service)
{
CloseServiceHandle(manager);
systemError(TEXT("CreateService"), TRUE);
}
description.lpDescription = SERVICE_DESC;
ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
SERVICE_REQUIRED_PRIVILEGES_INFO required_privileges = { TEXT("SeChangeNotifyPrivilege\0") };
ChangeServiceConfig2(service, SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO, &required_privileges);
SERVICE_SID_INFO sid = { SERVICE_SID_TYPE_UNRESTRICTED };
ChangeServiceConfig2(service, SERVICE_CONFIG_SERVICE_SID_INFO, &sid);
CloseServiceHandle(service);
CloseServiceHandle(manager);
}
void serviceRemove(void)
{
SC_HANDLE manager;
SC_HANDLE service;
manager = OpenSCManager(NULL, NULL, 0);
if (!manager)
systemError(TEXT("OpenSCManager"), TRUE);
service = OpenService(manager, TEXT("WinIPBroadcast"), DELETE);
if (!service)
{
CloseServiceHandle(manager);
systemError(TEXT("OpenService"), TRUE);
}
if (!DeleteService(service))
systemError(TEXT("DeleteService"), TRUE);
CloseServiceHandle(service);
CloseServiceHandle(manager);
}
void usage(void)
{
fwprintf(stderr, TEXT("usage: WinIPBroadcast < install | remove | run >\n"));
fwprintf(stderr, TEXT("WinIPBroadcast 1.6 by Etienne Dechamps <etienne@edechamps.fr>\n"));
fwprintf(stderr, TEXT("https://github.com/dechamps/WinIPBroadcast\n"));
quit();
}
int main(int argc, char* argv[])
{
SERVICE_TABLE_ENTRY dispatchTable[] =
{
{ SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)serviceMain },
{ NULL, NULL }
};
mainThread = NULL;
serviceStatus = NULL;
if (!StartServiceCtrlDispatcher(dispatchTable))
{
if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
systemError(TEXT("StartServiceCtrlDispatcher"), TRUE);
}
else
return 0;
if (argc < 2)
usage();
if (!_strcmpi(argv[1], "install"))
serviceInstall();
else if (!_strcmpi(argv[1], "remove"))
serviceRemove();
else if (!_strcmpi(argv[1], "run"))
loop();
else
usage();
return 0;
}
void quit(void)
{
if (serviceStatus)
serviceReport(SERVICE_STOPPED, NO_ERROR);
ExitProcess(1);
}