-
Notifications
You must be signed in to change notification settings - Fork 45
/
hdhomerun_sock.c
453 lines (369 loc) · 13.1 KB
/
hdhomerun_sock.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
/*
* hdhomerun_sock.c
*
* Copyright © 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"
#if defined(_WIN32)
#include <netioapi.h>
#else
#include <net/if.h>
#endif
#if defined(_WINRT)
static inline uint32_t if_nametoindex(const char *ifname) { return 0; }
#endif
bool hdhomerun_sock_sockaddr_is_addr(const struct sockaddr *addr)
{
if (addr->sa_family == AF_INET6) {
static uint8_t hdhomerun_sock_ipv6_zero_bytes[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
const struct sockaddr_in6 *addr_in6 = (const struct sockaddr_in6 *)addr;
return (memcmp(addr_in6->sin6_addr.s6_addr, hdhomerun_sock_ipv6_zero_bytes, 16) != 0);
}
if (addr->sa_family == AF_INET) {
const struct sockaddr_in *addr_in = (const struct sockaddr_in *)addr;
return (addr_in->sin_addr.s_addr != 0);
}
return false;
}
bool hdhomerun_sock_sockaddr_is_multicast(const struct sockaddr *ip_addr)
{
if (ip_addr->sa_family == AF_INET6) {
const struct sockaddr_in6 *addr_in6 = (const struct sockaddr_in6 *)ip_addr;
return (addr_in6->sin6_addr.s6_addr[0] == 0xFF);
}
if (ip_addr->sa_family == AF_INET) {
const struct sockaddr_in *addr_in = (const struct sockaddr_in *)ip_addr;
uint32_t v = ntohl(addr_in->sin_addr.s_addr);
return (v >= 0xE0000000) && (v < 0xF0000000);
}
return false;
}
bool hdhomerun_sock_sockaddr_is_ipv4_localhost(const struct sockaddr *ip_addr)
{
if (ip_addr->sa_family != AF_INET) {
return false;
}
const struct sockaddr_in *sock_addr_in = (const struct sockaddr_in *)ip_addr;
uint8_t *addr_bytes = (uint8_t *)&sock_addr_in->sin_addr.s_addr;
return (addr_bytes[0] == 0x7F);
}
bool hdhomerun_sock_sockaddr_is_ipv4_autoip(const struct sockaddr *ip_addr)
{
if (ip_addr->sa_family != AF_INET) {
return false;
}
const struct sockaddr_in *sock_addr_in = (const struct sockaddr_in *)ip_addr;
uint8_t *addr_bytes = (uint8_t *)&sock_addr_in->sin_addr.s_addr;
return (addr_bytes[0] == 169) && (addr_bytes[1] == 254);
}
bool hdhomerun_sock_sockaddr_is_ipv6_localhost(const struct sockaddr *ip_addr)
{
if (ip_addr->sa_family != AF_INET6) {
return false;
}
const struct sockaddr_in6 *sock_addr_in6 = (const struct sockaddr_in6 *)ip_addr;
uint8_t *addr_bytes = (uint8_t *)sock_addr_in6->sin6_addr.s6_addr;
static uint8_t hdhomerun_discover_ipv6_localhost_bytes[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
return (memcmp(addr_bytes, hdhomerun_discover_ipv6_localhost_bytes, 16) == 0);
}
bool hdhomerun_sock_sockaddr_is_ipv6_linklocal(const struct sockaddr *addr)
{
if (addr->sa_family != AF_INET6) {
return false;
}
const struct sockaddr_in6 *addr_in6 = (const struct sockaddr_in6 *)addr;
uint8_t *addr_bytes = (uint8_t *)addr_in6->sin6_addr.s6_addr;
return (addr_bytes[0] == 0xFE) && ((addr_bytes[1] & 0xC0) == 0x80);
}
bool hdhomerun_sock_sockaddr_is_ipv6_global(const struct sockaddr *ip_addr)
{
if (ip_addr->sa_family != AF_INET6) {
return false;
}
const struct sockaddr_in6 *sock_addr_in6 = (const struct sockaddr_in6 *)ip_addr;
uint8_t *addr_bytes = (uint8_t *)sock_addr_in6->sin6_addr.s6_addr;
return ((addr_bytes[0] & 0xE0) == 0x20);
}
uint16_t hdhomerun_sock_sockaddr_get_port(const struct sockaddr *addr)
{
if (addr->sa_family == AF_INET6) {
const struct sockaddr_in6 *addr_in6 = (const struct sockaddr_in6 *)addr;
return ntohs(addr_in6->sin6_port);
}
if (addr->sa_family == AF_INET) {
const struct sockaddr_in *addr_in = (const struct sockaddr_in *)addr;
return ntohs(addr_in->sin_port);
}
return 0;
}
void hdhomerun_sock_sockaddr_set_port(struct sockaddr *addr, uint16_t port)
{
if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
addr_in6->sin6_port = htons(port);
return;
}
if (addr->sa_family == AF_INET) {
struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
addr_in->sin_port = htons(port);
return;
}
}
void hdhomerun_sock_sockaddr_copy(struct sockaddr_storage *result, const struct sockaddr *addr)
{
memset(result, 0, sizeof(struct sockaddr_storage));
if (addr->sa_family == AF_INET6) {
memcpy(result, addr, sizeof(struct sockaddr_in6));
}
if (addr->sa_family == AF_INET) {
memcpy(result, addr, sizeof(struct sockaddr_in));
}
}
void hdhomerun_sock_sockaddr_to_ip_str(char ip_str[64], const struct sockaddr *ip_addr, bool include_ipv6_scope_id)
{
size_t ip_str_size = 64;
if (ip_addr->sa_family == AF_INET6) {
const struct sockaddr_in6 *ip_addr_in6 = (const struct sockaddr_in6 *)ip_addr;
inet_ntop(AF_INET6, ip_addr_in6->sin6_addr.s6_addr, ip_str, ip_str_size);
if (include_ipv6_scope_id && (ip_addr_in6->sin6_scope_id != 0)) {
hdhomerun_sprintf(strchr(ip_str, 0), ip_str + ip_str_size, "%%%u", ip_addr_in6->sin6_scope_id);
}
return;
}
if (ip_addr->sa_family == AF_INET) {
const struct sockaddr_in *ip_addr_in = (const struct sockaddr_in *)ip_addr;
inet_ntop(AF_INET, &ip_addr_in->sin_addr.s_addr, ip_str, ip_str_size);
return;
}
ip_str[0] = 0;
}
bool hdhomerun_sock_ip_str_to_sockaddr(const char *ip_str, struct sockaddr_storage *result)
{
memset(result, 0, sizeof(struct sockaddr_storage));
struct sockaddr_in *result_in = (struct sockaddr_in *)result;
if (inet_pton(AF_INET, ip_str, &result_in->sin_addr) == 1) {
result_in->sin_family = AF_INET;
return true;
}
bool framed = (*ip_str == '[');
bool ifname_present = (strchr(ip_str, '%') != NULL);
if (!framed && !ifname_present) {
struct sockaddr_in6 *result_in6 = (struct sockaddr_in6 *)result;
if (inet_pton(AF_INET6, ip_str, &result_in6->sin6_addr) == 1) {
result_in6->sin6_family = AF_INET6;
return true;
}
return false;
}
if (framed) {
ip_str++;
}
char ip_str_tmp[64];
if (!hdhomerun_sprintf(ip_str_tmp, ip_str_tmp + sizeof(ip_str_tmp), "%s", ip_str)) {
return false;
}
if (framed) {
char *end = strchr(ip_str_tmp, ']');
if (!end) {
return false;
}
*end++ = 0;
if (*end) {
return false;
}
}
char *ifname = NULL;
if (ifname_present) {
ifname = strchr(ip_str_tmp, '%');
*ifname++ = 0;
}
struct sockaddr_in6 *result_in6 = (struct sockaddr_in6 *)result;
if (inet_pton(AF_INET6, ip_str_tmp, &result_in6->sin6_addr) != 1) {
return false;
}
result_in6->sin6_family = AF_INET6;
if (!ifname_present) {
return true;
}
char *end;
result_in6->sin6_scope_id = (uint32_t)strtoul(ifname, &end, 10);
if ((result_in6->sin6_scope_id > 0) && (*end == 0)) {
return true;
}
result_in6->sin6_scope_id = if_nametoindex(ifname);
return (result_in6->sin6_scope_id > 0);
}
struct hdhomerun_local_ip_info_state_t {
struct hdhomerun_local_ip_info_t *ip_info;
int max_count;
int count;
};
static void hdhomerun_local_ip_info_callback(void *arg, uint32_t ifindex, const struct sockaddr *local_ip, uint8_t cidr)
{
struct hdhomerun_local_ip_info_state_t *state = (struct hdhomerun_local_ip_info_state_t *)arg;
if (state->count >= state->max_count) {
state->count++;
return;
}
const struct sockaddr_in *local_ip_in = (const struct sockaddr_in *)local_ip;
state->ip_info->ip_addr = ntohl(local_ip_in->sin_addr.s_addr);
state->ip_info->subnet_mask = 0xFFFFFFFF << (32 - cidr);
state->ip_info++;
state->count++;
}
int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count)
{
struct hdhomerun_local_ip_info_state_t state;
state.ip_info = ip_info_list;
state.max_count = max_count;
state.count = 0;
if (!hdhomerun_local_ip_info2(AF_INET, hdhomerun_local_ip_info_callback, &state)) {
return -1;
}
return state.count;
}
struct hdhomerun_sock_t *hdhomerun_sock_create_udp(void)
{
return hdhomerun_sock_create_udp_ex(AF_INET);
}
struct hdhomerun_sock_t *hdhomerun_sock_create_tcp(void)
{
return hdhomerun_sock_create_tcp_ex(AF_INET);
}
uint32_t hdhomerun_sock_getsockname_addr(struct hdhomerun_sock_t *sock)
{
struct sockaddr_storage sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
if (!hdhomerun_sock_getsockname_addr_ex(sock, &sock_addr)) {
return 0;
}
if (sock_addr.ss_family != AF_INET) {
return 0;
}
struct sockaddr_in *sock_addr_in = (struct sockaddr_in *)&sock_addr;
return ntohl(sock_addr_in->sin_addr.s_addr);
}
uint32_t hdhomerun_sock_getpeername_addr(struct hdhomerun_sock_t *sock)
{
struct sockaddr_storage sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
if (!hdhomerun_sock_getpeername_addr_ex(sock, &sock_addr)) {
return 0;
}
if (sock_addr.ss_family != AF_INET) {
return 0;
}
struct sockaddr_in *sock_addr_in = (struct sockaddr_in *)&sock_addr;
return ntohl(sock_addr_in->sin_addr.s_addr);
}
uint32_t hdhomerun_sock_getaddrinfo_addr(struct hdhomerun_sock_t *sock, const char *name)
{
struct sockaddr_storage sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
if (!hdhomerun_sock_getaddrinfo_addr_ex(AF_INET, name, &sock_addr)) {
return 0;
}
if (sock_addr.ss_family != AF_INET) {
return 0;
}
struct sockaddr_in *sock_addr_in = (struct sockaddr_in *)&sock_addr;
return ntohl(sock_addr_in->sin_addr.s_addr);
}
bool hdhomerun_sock_getaddrinfo_addr_ex(int af, const char *name, struct sockaddr_storage *result)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = af;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo *sock_info;
if (getaddrinfo(name, NULL, &hints, &sock_info) != 0) {
return false;
}
if ((size_t)sock_info->ai_addrlen > sizeof(struct sockaddr_storage)) {
return false;
}
memcpy(result, sock_info->ai_addr, sock_info->ai_addrlen);
freeaddrinfo(sock_info);
return true;
}
bool hdhomerun_sock_join_multicast_group(struct hdhomerun_sock_t *sock, uint32_t multicast_ip, uint32_t local_ip)
{
struct sockaddr_in multicast_addr;
memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = htonl(multicast_ip);
struct sockaddr_in local_addr;
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(local_ip);
return hdhomerun_sock_join_multicast_group_ex(sock, (const struct sockaddr *)&multicast_addr, (const struct sockaddr *)&local_addr);
}
bool hdhomerun_sock_leave_multicast_group(struct hdhomerun_sock_t *sock, uint32_t multicast_ip, uint32_t local_ip)
{
struct sockaddr_in multicast_addr;
memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = htonl(multicast_ip);
struct sockaddr_in local_addr;
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(local_ip);
return hdhomerun_sock_leave_multicast_group_ex(sock, (const struct sockaddr *)&multicast_addr, (const struct sockaddr *)&local_addr);
}
bool hdhomerun_sock_bind(struct hdhomerun_sock_t *sock, uint32_t local_addr, uint16_t local_port, bool allow_reuse)
{
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);
return hdhomerun_sock_bind_ex(sock, (const struct sockaddr *)&sock_addr, allow_reuse);
}
bool hdhomerun_sock_connect(struct hdhomerun_sock_t *sock, uint32_t remote_addr, uint16_t remote_port, uint64_t timeout)
{
struct sockaddr_in sock_addr_in;
memset(&sock_addr_in, 0, sizeof(sock_addr_in));
sock_addr_in.sin_family = AF_INET;
sock_addr_in.sin_addr.s_addr = htonl(remote_addr);
sock_addr_in.sin_port = htons(remote_port);
return hdhomerun_sock_connect_ex(sock, (const struct sockaddr *)&sock_addr_in, timeout);
}
bool hdhomerun_sock_sendto(struct hdhomerun_sock_t *sock, uint32_t remote_addr, uint16_t remote_port, const void *data, size_t length, uint64_t timeout)
{
struct sockaddr_in sock_addr_in;
memset(&sock_addr_in, 0, sizeof(sock_addr_in));
sock_addr_in.sin_family = AF_INET;
sock_addr_in.sin_addr.s_addr = htonl(remote_addr);
sock_addr_in.sin_port = htons(remote_port);
return hdhomerun_sock_sendto_ex(sock, (const struct sockaddr *)&sock_addr_in, data, length, timeout);
}
bool hdhomerun_sock_recvfrom(struct hdhomerun_sock_t *sock, uint32_t *remote_addr, uint16_t *remote_port, void *data, size_t *length, uint64_t timeout)
{
struct sockaddr_storage sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
if (!hdhomerun_sock_recvfrom_ex(sock, &sock_addr, data, length, timeout)) {
return false;
}
if (sock_addr.ss_family != AF_INET) {
return false;
}
struct sockaddr_in *sock_addr_in = (struct sockaddr_in *)&sock_addr;
*remote_addr = ntohl(sock_addr_in->sin_addr.s_addr);
*remote_port = ntohs(sock_addr_in->sin_port);
return true;
}