-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathhdhomerun_sock_posix.c
517 lines (420 loc) · 13.2 KB
/
hdhomerun_sock_posix.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
/*
* hdhomerun_sock_posix.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"
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
struct hdhomerun_sock_t {
int sock;
int af;
uint8_t ttl_set;
};
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 == -1) {
free(sock);
return NULL;
}
/* Set non-blocking */
if (fcntl(sock->sock, F_SETFL, O_NONBLOCK) != 0) {
hdhomerun_sock_destroy(sock);
return NULL;
}
/* Configure socket not to generate pipe-error signal (BSD/OSX). */
#if defined(SO_NOSIGPIPE)
int set = 1;
setsockopt(sock->sock, SOL_SOCKET, SO_NOSIGPIPE, (char *)&set, sizeof(set));
#endif
/* 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));
}
/* 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)
{
close(sock->sock);
free(sock);
}
void hdhomerun_sock_stop(struct hdhomerun_sock_t *sock)
{
shutdown(sock->sock, SHUT_RDWR);
}
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)
{
#if defined(IP_ONESBCAST)
setsockopt(sock->sock, IPPROTO_IP, IP_ONESBCAST, (char *)&v, sizeof(v));
#endif
}
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 errno;
}
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;
}
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 (connect(sock->sock, remote_addr, remote_addr_size) != 0) {
if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) {
return false;
}
}
struct pollfd poll_event;
poll_event.fd = sock->sock;
poll_event.events = POLLOUT;
poll_event.revents = 0;
if (poll(&poll_event, 1, (int)timeout) <= 0) {
return false;
}
if ((poll_event.revents & POLLOUT) == 0) {
return false;
}
return true;
}
bool hdhomerun_sock_send(struct hdhomerun_sock_t *sock, const void *data, size_t length, uint64_t timeout)
{
const uint8_t *ptr = (const uint8_t *)data;
ssize_t ret = send(sock->sock, ptr, length, MSG_NOSIGNAL);
if (ret >= (ssize_t)length) {
return true;
}
if ((ret < 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) {
return false;
}
if (ret > 0) {
ptr += ret;
length -= ret;
}
uint64_t stop_time = getcurrenttime() + timeout;
while (1) {
struct pollfd poll_event;
poll_event.fd = sock->sock;
poll_event.events = POLLOUT;
poll_event.revents = 0;
if (poll(&poll_event, 1, (int)timeout) <= 0) {
return false;
}
if ((poll_event.revents & POLLOUT) == 0) {
return false;
}
ret = send(sock->sock, ptr, length, MSG_NOSIGNAL);
if (ret >= (ssize_t)length) {
return true;
}
if ((ret < 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) {
return false;
}
if (ret > 0) {
ptr += ret;
length -= ret;
}
uint64_t current_time = getcurrenttime();
if (current_time >= stop_time) {
return false;
}
timeout = stop_time - current_time;
}
}
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;
}
const uint8_t *ptr = (const uint8_t *)data;
ssize_t ret = sendto(sock->sock, ptr, length, 0, remote_addr, remote_addr_size);
if (ret >= (ssize_t)length) {
return true;
}
if ((ret < 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) {
return false;
}
if (ret > 0) {
ptr += ret;
length -= ret;
}
uint64_t stop_time = getcurrenttime() + timeout;
while (1) {
struct pollfd poll_event;
poll_event.fd = sock->sock;
poll_event.events = POLLOUT;
poll_event.revents = 0;
if (poll(&poll_event, 1, (int)timeout) <= 0) {
return false;
}
if ((poll_event.revents & POLLOUT) == 0) {
return false;
}
ret = sendto(sock->sock, ptr, length, 0, remote_addr, remote_addr_size);
if (ret >= (ssize_t)length) {
return true;
}
if ((ret < 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) {
return false;
}
if (ret > 0) {
ptr += ret;
length -= ret;
}
uint64_t current_time = getcurrenttime();
if (current_time >= stop_time) {
return false;
}
timeout = stop_time - current_time;
}
}
bool hdhomerun_sock_recv(struct hdhomerun_sock_t *sock, void *data, size_t *length, uint64_t timeout)
{
ssize_t ret = recv(sock->sock, data, *length, 0);
if (ret > 0) {
*length = (size_t)ret;
return true;
}
if (ret == 0) {
return false;
}
if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) {
return false;
}
struct pollfd poll_event;
poll_event.fd = sock->sock;
poll_event.events = POLLIN;
poll_event.revents = 0;
if (poll(&poll_event, 1, (int)timeout) <= 0) {
return false;
}
if ((poll_event.revents & POLLIN) == 0) {
return false;
}
ret = recv(sock->sock, data, *length, 0);
if (ret > 0) {
*length = (size_t)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)
{
socklen_t sockaddr_size = sizeof(struct sockaddr_storage);
ssize_t ret = recvfrom(sock->sock, data, *length, 0, (struct sockaddr *)remote_addr, &sockaddr_size);
if (ret > 0) {
*length = (size_t)ret;
return true;
}
if (ret == 0) {
return false;
}
if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) {
return false;
}
struct pollfd poll_event;
poll_event.fd = sock->sock;
poll_event.events = POLLIN;
poll_event.revents = 0;
if (poll(&poll_event, 1, (int)timeout) <= 0) {
return false;
}
if ((poll_event.revents & POLLIN) == 0) {
return false;
}
ret = recvfrom(sock->sock, data, *length, 0, (struct sockaddr *)remote_addr, &sockaddr_size);
if (ret > 0) {
*length = (size_t)ret;
return true;
}
return false;
}