This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdhomerun_sock_windows.c
462 lines (382 loc) · 11.5 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
/*
* hdhomerun_sock_windows.c
*
* Copyright © 2010 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 3 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, see <http://www.gnu.org/licenses/>.
*
* As a special exception to the GNU Lesser General Public License,
* you may link, statically or dynamically, an application with a
* publicly distributed version of the Library to produce an
* executable file containing portions of the Library, and
* distribute that executable file under terms of your choice,
* without any of the additional requirements listed in clause 4 of
* the GNU Lesser General Public License.
*
* By "a publicly distributed version of the Library", we mean
* either the unmodified Library as distributed by Silicondust, or a
* modified version of the Library that is distributed under the
* conditions defined in the GNU Lesser General Public License.
*/
/*
* Implementation notes:
*
* API specifies timeout for each operation (or zero for non-blocking).
*
* It is not possible to rely on the OS socket timeout as this will fail to
* detect the command-response situation where data is sent successfully and
* the other end chooses not to send a response (other than the TCP ack).
*
* Windows supports select() however native WSA events are used to:
* - avoid problems with socket numbers above 1024.
* - wait without allowing other events handlers to run (important for use
* with win7 WMC).
*/
#include "hdhomerun.h"
#include <windows.h>
#include <iphlpapi.h>
int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count)
{
PIP_ADAPTER_INFO AdapterInfo;
ULONG AdapterInfoLength = sizeof(IP_ADAPTER_INFO) * 16;
while (1) {
AdapterInfo = (IP_ADAPTER_INFO *)malloc(AdapterInfoLength);
if (!AdapterInfo) {
return -1;
}
ULONG LengthNeeded = AdapterInfoLength;
DWORD Ret = GetAdaptersInfo(AdapterInfo, &LengthNeeded);
if (Ret == NO_ERROR) {
break;
}
free(AdapterInfo);
if (Ret != ERROR_BUFFER_OVERFLOW) {
return -1;
}
if (AdapterInfoLength >= LengthNeeded) {
return -1;
}
AdapterInfoLength = LengthNeeded;
}
int count = 0;
PIP_ADAPTER_INFO Adapter = AdapterInfo;
while (Adapter) {
IP_ADDR_STRING *IPAddr = &Adapter->IpAddressList;
while (IPAddr) {
uint32_t ip_addr = ntohl(inet_addr(IPAddr->IpAddress.String));
uint32_t subnet_mask = ntohl(inet_addr(IPAddr->IpMask.String));
if (ip_addr == 0) {
IPAddr = IPAddr->Next;
continue;
}
if (count < max_count) {
struct hdhomerun_local_ip_info_t *ip_info = &ip_info_list[count];
ip_info->ip_addr = ip_addr;
ip_info->subnet_mask = subnet_mask;
}
count++;
IPAddr = IPAddr->Next;
}
if (count >= max_count) {
break;
}
Adapter = Adapter->Next;
}
free(AdapterInfo);
return count;
}
hdhomerun_sock_t hdhomerun_sock_create_udp(void)
{
/* Create socket. */
hdhomerun_sock_t sock = (hdhomerun_sock_t)socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
return HDHOMERUN_SOCK_INVALID;
}
/* Set non-blocking */
unsigned long mode = 1;
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
closesocket(sock);
return HDHOMERUN_SOCK_INVALID;
}
/* Allow broadcast. */
int sock_opt = 1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&sock_opt, sizeof(sock_opt));
/* Success. */
return sock;
}
hdhomerun_sock_t hdhomerun_sock_create_tcp(void)
{
/* Create socket. */
hdhomerun_sock_t sock = (hdhomerun_sock_t)socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
return HDHOMERUN_SOCK_INVALID;
}
/* Set non-blocking */
unsigned long mode = 1;
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
closesocket(sock);
return HDHOMERUN_SOCK_INVALID;
}
/* Success. */
return sock;
}
void hdhomerun_sock_destroy(hdhomerun_sock_t sock)
{
closesocket(sock);
}
int hdhomerun_sock_getlasterror(void)
{
return WSAGetLastError();
}
uint32_t hdhomerun_sock_getsockname_addr(hdhomerun_sock_t sock)
{
struct sockaddr_in sock_addr;
int sockaddr_size = sizeof(sock_addr);
if (getsockname(sock, (struct sockaddr *)&sock_addr, &sockaddr_size) != 0) {
return 0;
}
return ntohl(sock_addr.sin_addr.s_addr);
}
uint16_t hdhomerun_sock_getsockname_port(hdhomerun_sock_t sock)
{
struct sockaddr_in sock_addr;
int sockaddr_size = sizeof(sock_addr);
if (getsockname(sock, (struct sockaddr *)&sock_addr, &sockaddr_size) != 0) {
return 0;
}
return ntohs(sock_addr.sin_port);
}
uint32_t hdhomerun_sock_getpeername_addr(hdhomerun_sock_t sock)
{
struct sockaddr_in sock_addr;
int sockaddr_size = sizeof(sock_addr);
if (getpeername(sock, (struct sockaddr *)&sock_addr, &sockaddr_size) != 0) {
return 0;
}
return ntohl(sock_addr.sin_addr.s_addr);
}
uint32_t hdhomerun_sock_getaddrinfo_addr(hdhomerun_sock_t sock, const char *name)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo *sock_info;
if (getaddrinfo(name, "", &hints, &sock_info) != 0) {
return 0;
}
struct sockaddr_in *sock_addr = (struct sockaddr_in *)sock_info->ai_addr;
uint32_t addr = ntohl(sock_addr->sin_addr.s_addr);
freeaddrinfo(sock_info);
return addr;
}
bool_t hdhomerun_sock_join_multicast_group(hdhomerun_sock_t sock, uint32_t multicast_ip, uint32_t local_ip)
{
struct ip_mreq imr;
memset(&imr, 0, sizeof(imr));
imr.imr_multiaddr.s_addr = htonl(multicast_ip);
imr.imr_interface.s_addr = htonl(local_ip);
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&imr, sizeof(imr)) != 0) {
return FALSE;
}
return TRUE;
}
bool_t hdhomerun_sock_leave_multicast_group(hdhomerun_sock_t sock, uint32_t multicast_ip, uint32_t local_ip)
{
struct ip_mreq imr;
memset(&imr, 0, sizeof(imr));
imr.imr_multiaddr.s_addr = htonl(multicast_ip);
imr.imr_interface.s_addr = htonl(local_ip);
if (setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const char *)&imr, sizeof(imr)) != 0) {
return FALSE;
}
return TRUE;
}
bool_t hdhomerun_sock_bind(hdhomerun_sock_t sock, uint32_t local_addr, uint16_t local_port, bool_t allow_reuse)
{
int sock_opt = allow_reuse;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_opt, sizeof(sock_opt));
struct sockaddr_in sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = htonl(local_addr);
sock_addr.sin_port = htons(local_port);
if (bind(sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) != 0) {
return FALSE;
}
return TRUE;
}
bool_t hdhomerun_sock_connect(hdhomerun_sock_t sock, uint32_t remote_addr, uint16_t remote_port, uint64_t timeout)
{
/* Connect (non-blocking). */
struct sockaddr_in sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = htonl(remote_addr);
sock_addr.sin_port = htons(remote_port);
if (connect(sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) != 0) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return FALSE;
}
}
/* Wait for connect to complete (both success and failure will signal). */
WSAEVENT wsa_event = WSACreateEvent();
if (wsa_event == WSA_INVALID_EVENT) {
return FALSE;
}
if (WSAEventSelect(sock, wsa_event, FD_WRITE | FD_CLOSE) == SOCKET_ERROR) {
WSACloseEvent(wsa_event);
return FALSE;
}
DWORD ret = WaitForSingleObjectEx(wsa_event, (DWORD)timeout, FALSE);
WSACloseEvent(wsa_event);
if (ret != WAIT_OBJECT_0) {
return FALSE;
}
/* Detect success/failure. */
wsa_event = WSACreateEvent();
if (wsa_event == WSA_INVALID_EVENT) {
return FALSE;
}
if (WSAEventSelect(sock, wsa_event, FD_CLOSE) == SOCKET_ERROR) {
WSACloseEvent(wsa_event);
return FALSE;
}
ret = WaitForSingleObjectEx(wsa_event, 0, FALSE);
WSACloseEvent(wsa_event);
if (ret == WAIT_OBJECT_0) {
return FALSE;
}
return TRUE;
}
static bool_t hdhomerun_sock_wait_for_event(hdhomerun_sock_t sock, long event_type, uint64_t stop_time)
{
uint64_t current_time = getcurrenttime();
if (current_time >= stop_time) {
return FALSE;
}
WSAEVENT wsa_event = WSACreateEvent();
if (wsa_event == WSA_INVALID_EVENT) {
return FALSE;
}
if (WSAEventSelect(sock, wsa_event, event_type) == SOCKET_ERROR) {
WSACloseEvent(wsa_event);
return FALSE;
}
DWORD ret = WaitForSingleObjectEx(wsa_event, (DWORD)(stop_time - current_time), FALSE);
WSACloseEvent(wsa_event);
if (ret != WAIT_OBJECT_0) {
return FALSE;
}
return TRUE;
}
bool_t hdhomerun_sock_send(hdhomerun_sock_t sock, const void *data, size_t length, uint64_t timeout)
{
uint64_t stop_time = getcurrenttime() + timeout;
const uint8_t *ptr = (uint8_t *)data;
while (1) {
int ret = send(sock, (char *)ptr, (int)length, 0);
if (ret <= 0) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return FALSE;
}
if (!hdhomerun_sock_wait_for_event(sock, FD_WRITE | FD_CLOSE, stop_time)) {
return FALSE;
}
continue;
}
if (ret < (int)length) {
ptr += ret;
length -= ret;
continue;
}
return TRUE;
}
}
bool_t hdhomerun_sock_sendto(hdhomerun_sock_t sock, uint32_t remote_addr, uint16_t remote_port, const void *data, size_t length, uint64_t timeout)
{
uint64_t stop_time = getcurrenttime() + timeout;
const uint8_t *ptr = (uint8_t *)data;
while (1) {
struct sockaddr_in sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = htonl(remote_addr);
sock_addr.sin_port = htons(remote_port);
int ret = sendto(sock, (char *)ptr, (int)length, 0, (struct sockaddr *)&sock_addr, sizeof(sock_addr));
if (ret <= 0) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return FALSE;
}
if (!hdhomerun_sock_wait_for_event(sock, FD_WRITE | FD_CLOSE, stop_time)) {
return FALSE;
}
continue;
}
if (ret < (int)length) {
ptr += ret;
length -= ret;
continue;
}
return TRUE;
}
}
bool_t hdhomerun_sock_recv(hdhomerun_sock_t sock, void *data, size_t *length, uint64_t timeout)
{
uint64_t stop_time = getcurrenttime() + timeout;
while (1) {
int ret = recv(sock, (char *)data, (int)(*length), 0);
if (ret < 0) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return FALSE;
}
if (!hdhomerun_sock_wait_for_event(sock, FD_READ | FD_CLOSE, stop_time)) {
return FALSE;
}
continue;
}
if (ret == 0) {
return FALSE;
}
*length = ret;
return TRUE;
}
}
bool_t hdhomerun_sock_recvfrom(hdhomerun_sock_t sock, uint32_t *remote_addr, uint16_t *remote_port, void *data, size_t *length, uint64_t timeout)
{
uint64_t stop_time = getcurrenttime() + timeout;
while (1) {
struct sockaddr_in sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
int sockaddr_size = sizeof(sock_addr);
int ret = recvfrom(sock, (char *)data, (int)(*length), 0, (struct sockaddr *)&sock_addr, &sockaddr_size);
if (ret < 0) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return FALSE;
}
if (!hdhomerun_sock_wait_for_event(sock, FD_READ | FD_CLOSE, stop_time)) {
return FALSE;
}
continue;
}
if (ret == 0) {
return FALSE;
}
*remote_addr = ntohl(sock_addr.sin_addr.s_addr);
*remote_port = ntohs(sock_addr.sin_port);
*length = ret;
return TRUE;
}
}