-
Notifications
You must be signed in to change notification settings - Fork 45
/
hdhomerun_sock_windows.c
570 lines (465 loc) · 14.7 KB
/
hdhomerun_sock_windows.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
* hdhomerun_sock_windows.c
*
* Copyright © 2010-2022 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hdhomerun.h"
#include <iphlpapi.h>
struct hdhomerun_sock_t {
SOCKET sock;
HANDLE event;
long events_selected;
int af;
uint8_t ttl_set;
bool shutdown;
};
bool hdhomerun_local_ip_info2(int af, hdhomerun_local_ip_info2_callback_t callback, void *callback_arg)
{
IP_ADAPTER_ADDRESSES *adapter_addresses;
ULONG adapter_addresses_length = sizeof(IP_ADAPTER_ADDRESSES) * 16;
while (1) {
adapter_addresses = (IP_ADAPTER_ADDRESSES *)malloc(adapter_addresses_length);
if (!adapter_addresses) {
return false;
}
ULONG length_needed = adapter_addresses_length;
DWORD ret = GetAdaptersAddresses(af, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME, NULL, adapter_addresses, &length_needed);
if (ret == NO_ERROR) {
break;
}
free(adapter_addresses);
if (ret != ERROR_BUFFER_OVERFLOW) {
return false;
}
if (adapter_addresses_length >= length_needed) {
return false;
}
adapter_addresses_length = length_needed;
}
IP_ADAPTER_ADDRESSES *adapter = adapter_addresses;
while (adapter) {
if (adapter->OperStatus != 1) {
adapter = adapter->Next;
continue;
}
if (adapter->PhysicalAddressLength != 6) {
adapter = adapter->Next;
continue;
}
uint32_t ifindex = adapter->IfIndex;
if (ifindex == 0) {
adapter = adapter->Next;
continue;
}
IP_ADAPTER_UNICAST_ADDRESS *adapter_address = adapter->FirstUnicastAddress;
while (adapter_address) {
struct sockaddr *local_ip = adapter_address->Address.lpSockaddr;
if (!hdhomerun_sock_sockaddr_is_addr(local_ip)) {
adapter_address = adapter_address->Next;
continue;
}
if (adapter_address->Flags & IP_ADAPTER_ADDRESS_TRANSIENT) {
adapter_address = adapter_address->Next;
continue;
}
uint8_t cidr = adapter_address->OnLinkPrefixLength;
uint8_t cidr_fail = (local_ip->sa_family == AF_INET6) ? 128 : 32;
if ((cidr == 0) || (cidr >= cidr_fail)) {
adapter_address = adapter_address->Next;
continue;
}
callback(callback_arg, ifindex, local_ip, cidr);
adapter_address = adapter_address->Next;
}
adapter = adapter->Next;
}
free(adapter_addresses);
return true;
}
static struct hdhomerun_sock_t *hdhomerun_sock_create_internal(int af, int protocol)
{
struct hdhomerun_sock_t *sock = (struct hdhomerun_sock_t *)calloc(1, sizeof(struct hdhomerun_sock_t));
if (!sock) {
return NULL;
}
sock->af = af;
/* Create socket. */
sock->sock = socket(af, protocol, 0);
if (sock->sock == INVALID_SOCKET) {
free(sock);
return NULL;
}
/* Set non-blocking */
unsigned long mode = 1;
if (ioctlsocket(sock->sock, FIONBIO, &mode) != 0) {
hdhomerun_sock_destroy(sock);
return NULL;
}
/* Set ipv6 */
if (af == AF_INET6) {
int sock_opt_ipv6only = 1;
setsockopt(sock->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&sock_opt_ipv6only, sizeof(sock_opt_ipv6only));
}
/* Event */
sock->event = CreateEvent(NULL, false, false, NULL);
if (!sock->event) {
hdhomerun_sock_destroy(sock);
return NULL;
}
/* Success. */
return sock;
}
struct hdhomerun_sock_t *hdhomerun_sock_create_udp_ex(int af)
{
struct hdhomerun_sock_t *sock = hdhomerun_sock_create_internal(af, SOCK_DGRAM);
if (!sock) {
return NULL;
}
/* Allow broadcast. */
int sock_opt = 1;
setsockopt(sock->sock, SOL_SOCKET, SO_BROADCAST, (char *)&sock_opt, sizeof(sock_opt));
/* Success. */
return sock;
}
struct hdhomerun_sock_t *hdhomerun_sock_create_tcp_ex(int af)
{
return hdhomerun_sock_create_internal(af, SOCK_STREAM);
}
void hdhomerun_sock_destroy(struct hdhomerun_sock_t *sock)
{
if (sock->event) {
CloseHandle(sock->event);
}
closesocket(sock->sock);
free(sock);
}
void hdhomerun_sock_stop(struct hdhomerun_sock_t *sock)
{
sock->shutdown = true;
shutdown(sock->sock, SD_BOTH);
SetEvent(sock->event);
}
void hdhomerun_sock_set_send_buffer_size(struct hdhomerun_sock_t *sock, size_t size)
{
int size_opt = (int)size;
setsockopt(sock->sock, SOL_SOCKET, SO_SNDBUF, (char *)&size_opt, sizeof(size_opt));
}
void hdhomerun_sock_set_recv_buffer_size(struct hdhomerun_sock_t *sock, size_t size)
{
int size_opt = (int)size;
setsockopt(sock->sock, SOL_SOCKET, SO_RCVBUF, (char *)&size_opt, sizeof(size_opt));
}
void hdhomerun_sock_set_allow_reuse(struct hdhomerun_sock_t *sock)
{
int sock_opt = 1;
setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_opt, sizeof(sock_opt));
}
void hdhomerun_sock_set_ttl(struct hdhomerun_sock_t *sock, uint8_t ttl)
{
if (sock->ttl_set == ttl) {
return;
}
int sock_opt = (int)(unsigned int)ttl;
if (sock->af == AF_INET) {
setsockopt(sock->sock, IPPROTO_IP, IP_TTL, (char *)&sock_opt, sizeof(sock_opt));
setsockopt(sock->sock, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&sock_opt, sizeof(sock_opt));
}
if (sock->af == AF_INET6) {
setsockopt(sock->sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (char *)&sock_opt, sizeof(sock_opt));
setsockopt(sock->sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&sock_opt, sizeof(sock_opt));
}
sock->ttl_set = ttl;
}
void hdhomerun_sock_set_ipv4_onesbcast(struct hdhomerun_sock_t *sock, int v)
{
}
void hdhomerun_sock_set_ipv6_multicast_ifindex(struct hdhomerun_sock_t *sock, uint32_t ifindex)
{
setsockopt(sock->sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *)&ifindex, sizeof(ifindex));
}
int hdhomerun_sock_getlasterror(void)
{
return WSAGetLastError();
}
bool hdhomerun_sock_getsockname_addr_ex(struct hdhomerun_sock_t *sock, struct sockaddr_storage *result)
{
socklen_t sockaddr_size = sizeof(struct sockaddr_storage);
return (getsockname(sock->sock, (struct sockaddr *)result, &sockaddr_size) == 0);
}
uint16_t hdhomerun_sock_getsockname_port(struct hdhomerun_sock_t *sock)
{
struct sockaddr_storage sock_addr;
socklen_t sockaddr_size = sizeof(sock_addr);
if (getsockname(sock->sock, (struct sockaddr *)&sock_addr, &sockaddr_size) != 0) {
return 0;
}
if (sock_addr.ss_family == AF_INET) {
struct sockaddr_in *sock_addr_in = (struct sockaddr_in *)&sock_addr;
return ntohs(sock_addr_in->sin_port);
}
if (sock_addr.ss_family == AF_INET6) {
struct sockaddr_in6 *sock_addr_in = (struct sockaddr_in6 *)&sock_addr;
return ntohs(sock_addr_in->sin6_port);
}
return 0;
}
bool hdhomerun_sock_getpeername_addr_ex(struct hdhomerun_sock_t *sock, struct sockaddr_storage *result)
{
socklen_t sockaddr_size = sizeof(struct sockaddr_storage);
return (getpeername(sock->sock, (struct sockaddr *)result, &sockaddr_size) == 0);
}
bool hdhomerun_sock_join_multicast_group_ex(struct hdhomerun_sock_t *sock, const struct sockaddr *multicast_addr, const struct sockaddr *local_addr)
{
if (multicast_addr->sa_family == AF_INET6) {
const struct sockaddr_in6 *multicast_addr_in = (const struct sockaddr_in6 *)multicast_addr;
struct ipv6_mreq imr;
memset(&imr, 0, sizeof(imr));
memcpy(imr.ipv6mr_multiaddr.s6_addr, multicast_addr_in->sin6_addr.s6_addr, 16);
imr.ipv6mr_interface = multicast_addr_in->sin6_scope_id;
if (setsockopt(sock->sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (const char *)&imr, sizeof(imr)) != 0) {
return false;
}
return true;
}
if (multicast_addr->sa_family == AF_INET) {
const struct sockaddr_in *multicast_addr_in = (const struct sockaddr_in *)multicast_addr;
const struct sockaddr_in *local_addr_in = (const struct sockaddr_in *)local_addr;
struct ip_mreq imr;
memset(&imr, 0, sizeof(imr));
imr.imr_multiaddr.s_addr = multicast_addr_in->sin_addr.s_addr;
imr.imr_interface.s_addr = (local_addr->sa_family == AF_INET) ? local_addr_in->sin_addr.s_addr : 0;
if (setsockopt(sock->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&imr, sizeof(imr)) != 0) {
return false;
}
return true;
}
return false;
}
bool hdhomerun_sock_leave_multicast_group_ex(struct hdhomerun_sock_t *sock, const struct sockaddr *multicast_addr, const struct sockaddr *local_addr)
{
if (multicast_addr->sa_family == AF_INET6) {
const struct sockaddr_in6 *multicast_addr_in = (const struct sockaddr_in6 *)multicast_addr;
struct ipv6_mreq imr;
memset(&imr, 0, sizeof(imr));
memcpy(imr.ipv6mr_multiaddr.s6_addr, multicast_addr_in->sin6_addr.s6_addr, 16);
imr.ipv6mr_interface = multicast_addr_in->sin6_scope_id;
if (setsockopt(sock->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP, (const char *)&imr, sizeof(imr)) != 0) {
return false;
}
return true;
}
if (multicast_addr->sa_family == AF_INET) {
const struct sockaddr_in *multicast_addr_in = (const struct sockaddr_in *)multicast_addr;
const struct sockaddr_in *local_addr_in = (const struct sockaddr_in *)local_addr;
struct ip_mreq imr;
memset(&imr, 0, sizeof(imr));
imr.imr_multiaddr.s_addr = multicast_addr_in->sin_addr.s_addr;
imr.imr_interface.s_addr = (local_addr->sa_family == AF_INET) ? local_addr_in->sin_addr.s_addr : 0;
if (setsockopt(sock->sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const char *)&imr, sizeof(imr)) != 0) {
return false;
}
return true;
}
return false;
}
bool hdhomerun_sock_bind_ex(struct hdhomerun_sock_t *sock, const struct sockaddr *local_addr, bool allow_reuse)
{
socklen_t local_addr_size;
switch (local_addr->sa_family) {
case AF_INET6:
local_addr_size = (socklen_t)sizeof(struct sockaddr_in6);
break;
case AF_INET:
local_addr_size = (socklen_t)sizeof(struct sockaddr_in);
break;
default:
return false;
}
int sock_opt = allow_reuse;
setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_opt, sizeof(sock_opt));
if (bind(sock->sock, (const struct sockaddr *)local_addr, local_addr_size) != 0) {
return false;
}
return true;
}
static bool hdhomerun_sock_event_select(struct hdhomerun_sock_t *sock, long events)
{
if (sock->events_selected != events) {
if (WSAEventSelect(sock->sock, sock->event, events) == SOCKET_ERROR) {
return false;
}
sock->events_selected = events;
}
ResetEvent(sock->event);
if (sock->shutdown) {
return false;
}
return true;
}
bool hdhomerun_sock_connect_ex(struct hdhomerun_sock_t *sock, const struct sockaddr *remote_addr, uint64_t timeout)
{
socklen_t remote_addr_size;
switch (remote_addr->sa_family) {
case AF_INET6:
remote_addr_size = (socklen_t)sizeof(struct sockaddr_in6);
break;
case AF_INET:
remote_addr_size = (socklen_t)sizeof(struct sockaddr_in);
break;
default:
return false;
}
if (!hdhomerun_sock_event_select(sock, FD_WRITE | FD_CLOSE)) {
return false;
}
if (connect(sock->sock, remote_addr, remote_addr_size) != 0) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return false;
}
}
DWORD wait_ret = WaitForSingleObjectEx(sock->event, (DWORD)timeout, false);
if (wait_ret != WAIT_OBJECT_0) {
return false;
}
WSANETWORKEVENTS network_events;
if (WSAEnumNetworkEvents(sock->sock, sock->event, &network_events) == SOCKET_ERROR) {
return false;
}
if ((network_events.lNetworkEvents & FD_WRITE) == 0) {
return false;
}
if (network_events.lNetworkEvents & FD_CLOSE) {
return false;
}
return true;
}
bool hdhomerun_sock_send(struct hdhomerun_sock_t *sock, const void *data, size_t length, uint64_t timeout)
{
if (!hdhomerun_sock_event_select(sock, FD_WRITE | FD_CLOSE)) {
return false;
}
uint64_t stop_time = getcurrenttime() + timeout;
const uint8_t *ptr = (uint8_t *)data;
while (1) {
int ret = send(sock->sock, (char *)ptr, (int)length, 0);
if (ret >= (int)length) {
return true;
}
if ((ret == SOCKET_ERROR) && (WSAGetLastError() != WSAEWOULDBLOCK)) {
return false;
}
if (ret > 0) {
ptr += ret;
length -= ret;
}
uint64_t current_time = getcurrenttime();
if (current_time >= stop_time) {
return false;
}
if (WaitForSingleObjectEx(sock->event, (DWORD)(stop_time - current_time), false) != WAIT_OBJECT_0) {
return false;
}
}
}
bool hdhomerun_sock_sendto_ex(struct hdhomerun_sock_t *sock, const struct sockaddr *remote_addr, const void *data, size_t length, uint64_t timeout)
{
socklen_t remote_addr_size;
switch (remote_addr->sa_family) {
case AF_INET6:
remote_addr_size = (socklen_t)sizeof(struct sockaddr_in6);
break;
case AF_INET:
remote_addr_size = (socklen_t)sizeof(struct sockaddr_in);
break;
default:
return false;
}
if (!hdhomerun_sock_event_select(sock, FD_WRITE | FD_CLOSE)) {
return false;
}
int ret = sendto(sock->sock, (char *)data, (int)length, 0, remote_addr, remote_addr_size);
if (ret >= (int)length) {
return true;
}
if (ret >= 0) {
return false;
}
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return false;
}
if (WaitForSingleObjectEx(sock->event, (DWORD)timeout, false) != WAIT_OBJECT_0) {
return false;
}
ret = sendto(sock->sock, (char *)data, (int)length, 0, remote_addr, remote_addr_size);
if (ret >= (int)length) {
return true;
}
return false;
}
bool hdhomerun_sock_recv(struct hdhomerun_sock_t *sock, void *data, size_t *length, uint64_t timeout)
{
if (!hdhomerun_sock_event_select(sock, FD_READ | FD_CLOSE)) {
return false;
}
int ret = recv(sock->sock, (char *)data, (int)(*length), 0);
if (ret > 0) {
*length = ret;
return true;
}
if (ret == 0) {
return false;
}
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return false;
}
if (WaitForSingleObjectEx(sock->event, (DWORD)timeout, false) != WAIT_OBJECT_0) {
return false;
}
ret = recv(sock->sock, (char *)data, (int)(*length), 0);
if (ret > 0) {
*length = ret;
return true;
}
return false;
}
bool hdhomerun_sock_recvfrom_ex(struct hdhomerun_sock_t *sock, struct sockaddr_storage *remote_addr, void *data, size_t *length, uint64_t timeout)
{
if (!hdhomerun_sock_event_select(sock, FD_READ | FD_CLOSE)) {
return false;
}
socklen_t sockaddr_size = sizeof(struct sockaddr_storage);
int ret = recvfrom(sock->sock, (char *)data, (int)(*length), 0, (struct sockaddr *)remote_addr, &sockaddr_size);
if (ret > 0) {
*length = ret;
return true;
}
if (ret == 0) {
return false;
}
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return false;
}
if (WaitForSingleObjectEx(sock->event, (DWORD)timeout, false) != WAIT_OBJECT_0) {
return false;
}
ret = recvfrom(sock->sock, (char *)data, (int)(*length), 0, (struct sockaddr *)remote_addr, &sockaddr_size);
if (ret > 0) {
*length = ret;
return true;
}
return false;
}