-
Notifications
You must be signed in to change notification settings - Fork 45
/
hdhomerun_discover.c
1804 lines (1492 loc) · 53.1 KB
/
hdhomerun_discover.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* hdhomerun_discover.c
*
* Copyright © 2006-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"
struct hdhomerun_discover2_device_type_t {
struct hdhomerun_discover2_device_type_t *next;
uint32_t device_type;
};
struct hdhomerun_discover2_device_if_t {
struct hdhomerun_discover2_device_if_t *next;
struct sockaddr_storage ip_addr;
char *base_url;
char *lineup_url;
char *storage_url;
uint8_t priority;
};
struct hdhomerun_discover2_device_t {
struct hdhomerun_discover2_device_t *next;
struct hdhomerun_discover2_device_if_t *if_list;
struct hdhomerun_discover2_device_type_t *type_list;
uint32_t device_id;
uint8_t tuner_count;
char *device_auth;
char *storage_id;
};
struct hdhomerun_discover_sock_t {
struct hdhomerun_discover_sock_t *next;
struct hdhomerun_discover_sock_t *recv_next;
struct hdhomerun_sock_t *sock;
struct sockaddr_storage local_ip;
uint32_t ifindex;
uint32_t ipv4_subnet_mask;
bool active;
};
struct hdhomerun_discover_t {
struct hdhomerun_discover2_device_t *device_list;
struct hdhomerun_discover_sock_t *ipv6_socks;
struct hdhomerun_discover_sock_t *ipv4_socks;
struct hdhomerun_discover_sock_t *ipv6_localhost;
struct hdhomerun_discover_sock_t *ipv4_localhost;
struct hdhomerun_pkt_t tx_pkt;
struct hdhomerun_pkt_t rx_pkt;
struct hdhomerun_debug_t *dbg;
};
static uint8_t hdhomerun_discover_ipv6_linklocal_multicast_ip[16] = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x76 };
static uint8_t hdhomerun_discover_ipv6_sitelocal_multicast_ip[16] = { 0xFF, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x76 };
static void hdhomerun_discover_sock_free(struct hdhomerun_discover_sock_t *dss)
{
hdhomerun_sock_destroy(dss->sock);
free(dss);
}
static uint16_t hdhomerun_discover_get_local_port(struct hdhomerun_discover_t *ds)
{
if (ds->ipv6_socks) {
return hdhomerun_sock_getsockname_port(ds->ipv6_socks->sock);
}
if (ds->ipv4_socks) {
return hdhomerun_sock_getsockname_port(ds->ipv4_socks->sock);
}
if (ds->ipv6_localhost) {
return hdhomerun_sock_getsockname_port(ds->ipv6_localhost->sock);
}
if (ds->ipv4_localhost) {
return hdhomerun_sock_getsockname_port(ds->ipv4_localhost->sock);
}
return 0;
}
static void hdhomerun_discover_sock_add_ipv6(void *arg, uint32_t ifindex, const struct sockaddr *local_ip, uint8_t cidr)
{
if (local_ip->sa_family != AF_INET6) {
return;
}
struct hdhomerun_discover_t *ds = (struct hdhomerun_discover_t *)arg;
struct sockaddr_in6 *local_ip_in6 = (struct sockaddr_in6 *)local_ip;
char local_ip_str[64];
hdhomerun_sock_sockaddr_to_ip_str(local_ip_str, local_ip, true);
if (hdhomerun_sock_sockaddr_is_addr(local_ip)) {
hdhomerun_debug_printf(ds->dbg, "discover: local ip %s/%u\n", local_ip_str, cidr);
}
struct hdhomerun_discover_sock_t **pprev = &ds->ipv6_socks;
struct hdhomerun_discover_sock_t *p = ds->ipv6_socks;
while (p) {
struct sockaddr_in6 *p_ip = (struct sockaddr_in6 *)&p->local_ip;
if ((p->ifindex == ifindex) && (memcmp(p_ip->sin6_addr.s6_addr, local_ip_in6->sin6_addr.s6_addr, 16) == 0)) {
p->active = true;
return;
}
pprev = &p->next;
p = p->next;
}
/* Create socket. */
struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
if (!dss) {
hdhomerun_debug_printf(ds->dbg, "discover: resource error\n");
return;
}
dss->sock = hdhomerun_sock_create_udp_ex(AF_INET6);
if (!dss->sock) {
hdhomerun_debug_printf(ds->dbg, "discover: failed to allocate socket (%d)\n", hdhomerun_sock_getlasterror());
free(dss);
return;
}
hdhomerun_sock_set_ttl(dss->sock, 64);
/* Bind socket. */
local_ip_in6->sin6_port = htons(hdhomerun_discover_get_local_port(ds));
if (!hdhomerun_sock_bind_ex(dss->sock, local_ip, true)) {
hdhomerun_debug_printf(ds->dbg, "discover: failed to bind to local ip %s (%d)\n", local_ip_str, hdhomerun_sock_getlasterror());
hdhomerun_discover_sock_free(dss);
return;
}
/* Success. */
memcpy(&dss->local_ip, local_ip_in6, sizeof(struct sockaddr_in6));
dss->ifindex = ifindex;
dss->active = true;
*pprev = dss;
}
static void hdhomerun_discover_sock_add_ipv4(void *arg, uint32_t ifindex, const struct sockaddr *local_ip, uint8_t cidr)
{
if (local_ip->sa_family != AF_INET) {
return;
}
struct hdhomerun_discover_t *ds = (struct hdhomerun_discover_t *)arg;
struct sockaddr_in *local_ip_in = (struct sockaddr_in *)local_ip;
uint32_t detected_subnet_mask = 0xFFFFFFFF << (32 - cidr);
char local_ip_str[64];
hdhomerun_sock_sockaddr_to_ip_str(local_ip_str, local_ip, true);
if (hdhomerun_sock_sockaddr_is_addr(local_ip)) {
hdhomerun_debug_printf(ds->dbg, "discover: local ip %s/%u\n", local_ip_str, cidr);
}
struct hdhomerun_discover_sock_t **pprev = &ds->ipv4_socks;
struct hdhomerun_discover_sock_t *p = ds->ipv4_socks;
while (p) {
struct sockaddr_in *p_ip = (struct sockaddr_in *)&p->local_ip;
if ((p->ifindex == ifindex) && (p_ip->sin_addr.s_addr == local_ip_in->sin_addr.s_addr) && (p->ipv4_subnet_mask == detected_subnet_mask)) {
p->active = true;
return;
}
pprev = &p->next;
p = p->next;
}
/* Create socket. */
struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
if (!dss) {
hdhomerun_debug_printf(ds->dbg, "discover: resource error\n");
return;
}
dss->sock = hdhomerun_sock_create_udp_ex(AF_INET);
if (!dss->sock) {
hdhomerun_debug_printf(ds->dbg, "discover: failed to allocate socket (%d)\n", hdhomerun_sock_getlasterror());
free(dss);
return;
}
hdhomerun_sock_set_ttl(dss->sock, 64);
/* Bind socket. */
local_ip_in->sin_port = htons(hdhomerun_discover_get_local_port(ds));
if (!hdhomerun_sock_bind_ex(dss->sock, local_ip, true)) {
hdhomerun_debug_printf(ds->dbg, "discover: failed to bind to local ip %s (%d)\n", local_ip_str, hdhomerun_sock_getlasterror());
hdhomerun_discover_sock_free(dss);
return;
}
/* Success. */
memcpy(&dss->local_ip, local_ip_in, sizeof(struct sockaddr_in));
dss->ifindex = ifindex;
dss->ipv4_subnet_mask = detected_subnet_mask;
dss->active = true;
*pprev = dss;
}
static void hdhomerun_discover_device_if_free(struct hdhomerun_discover2_device_if_t *device_if)
{
if (device_if->base_url) {
free(device_if->base_url);
}
if (device_if->lineup_url) {
free(device_if->lineup_url);
}
if (device_if->storage_url) {
free(device_if->storage_url);
}
free(device_if);
}
static void hdhomerun_discover_device_free(struct hdhomerun_discover2_device_t *device)
{
while (device->if_list) {
struct hdhomerun_discover2_device_if_t *device_if = device->if_list;
device->if_list = device_if->next;
hdhomerun_discover_device_if_free(device_if);
}
while (device->type_list) {
struct hdhomerun_discover2_device_type_t *device_type = device->type_list;
device->type_list = device_type->next;
free(device_type);
}
if (device->device_auth) {
free(device->device_auth);
}
if (device->storage_id) {
free(device->storage_id);
}
free(device);
}
static void hdhomerun_discover_free_device_list(struct hdhomerun_discover_t *ds)
{
while (ds->device_list) {
struct hdhomerun_discover2_device_t *device = ds->device_list;
ds->device_list = device->next;
hdhomerun_discover_device_free(device);
}
}
void hdhomerun_discover_destroy(struct hdhomerun_discover_t *ds)
{
hdhomerun_discover_free_device_list(ds);
while (ds->ipv6_socks) {
struct hdhomerun_discover_sock_t *dss = ds->ipv6_socks;
ds->ipv6_socks = dss->next;
hdhomerun_discover_sock_free(dss);
}
while (ds->ipv4_socks) {
struct hdhomerun_discover_sock_t *dss = ds->ipv4_socks;
ds->ipv4_socks = dss->next;
hdhomerun_discover_sock_free(dss);
}
free(ds);
}
struct hdhomerun_discover_t *hdhomerun_discover_create(struct hdhomerun_debug_t *dbg)
{
struct hdhomerun_discover_t *ds = (struct hdhomerun_discover_t *)calloc(1, sizeof(struct hdhomerun_discover_t));
if (!ds) {
return NULL;
}
ds->dbg = dbg;
return ds;
}
static void hdhomerun_discover_sock_detect_ipv6(struct hdhomerun_discover_t *ds)
{
struct hdhomerun_discover_sock_t *default_dss = ds->ipv6_socks;
if (!default_dss) {
/* Create a routable socket (always first entry). */
struct sockaddr_in6 ipv6_addr;
memset(&ipv6_addr, 0, sizeof(ipv6_addr));
ipv6_addr.sin6_family = AF_INET6;
hdhomerun_discover_sock_add_ipv6(ds, 0, (const struct sockaddr *)&ipv6_addr, 0);
default_dss = ds->ipv6_socks;
if (!default_dss) {
return;
}
}
struct hdhomerun_discover_sock_t *p = default_dss->next;
while (p) {
p->active = false;
p = p->next;
}
hdhomerun_local_ip_info2(AF_INET6, hdhomerun_discover_sock_add_ipv6, ds);
}
static void hdhomerun_discover_sock_detect_ipv4(struct hdhomerun_discover_t *ds)
{
struct hdhomerun_discover_sock_t *default_dss = ds->ipv4_socks;
if (!default_dss) {
/* Create a routable socket (always first entry). */
struct sockaddr_in ipv4_addr;
memset(&ipv4_addr, 0, sizeof(ipv4_addr));
ipv4_addr.sin_family = AF_INET;
hdhomerun_discover_sock_add_ipv4(ds, 0, (const struct sockaddr *)&ipv4_addr, 0);
default_dss = ds->ipv4_socks;
if (!default_dss) {
return;
}
}
struct hdhomerun_discover_sock_t *p = default_dss->next;
while (p) {
p->active = false;
p = p->next;
}
hdhomerun_local_ip_info2(AF_INET, hdhomerun_discover_sock_add_ipv4, ds);
}
static void hdhomerun_discover_sock_detect_ipv6_localhost(struct hdhomerun_discover_t *ds)
{
if (ds->ipv6_localhost) {
return;
}
struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
if (!dss) {
return;
}
dss->sock = hdhomerun_sock_create_udp_ex(AF_INET6);
if (!dss->sock) {
free(dss);
return;
}
struct sockaddr_in6 *sock_addr = (struct sockaddr_in6 *)&dss->local_ip;
sock_addr->sin6_family = AF_INET6;
sock_addr->sin6_addr.s6_addr[15] = 1;
sock_addr->sin6_port = htons(hdhomerun_discover_get_local_port(ds));
if (!hdhomerun_sock_bind_ex(dss->sock, (const struct sockaddr *)sock_addr, true)) {
hdhomerun_discover_sock_free(dss);
return;
}
dss->active = true;
ds->ipv6_localhost = dss;
}
static void hdhomerun_discover_sock_detect_ipv4_localhost(struct hdhomerun_discover_t *ds)
{
if (ds->ipv4_localhost) {
return;
}
struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
if (!dss) {
return;
}
dss->sock = hdhomerun_sock_create_udp_ex(AF_INET);
if (!dss->sock) {
free(dss);
return;
}
struct sockaddr_in *sock_addr = (struct sockaddr_in *)&dss->local_ip;
sock_addr->sin_family = AF_INET;
sock_addr->sin_addr.s_addr = htonl(0x7F000001);
sock_addr->sin_port = htons(hdhomerun_discover_get_local_port(ds));
dss->ipv4_subnet_mask = 0xFF000000;
if (!hdhomerun_sock_bind_ex(dss->sock, (const struct sockaddr *)sock_addr, true)) {
hdhomerun_discover_sock_free(dss);
return;
}
dss->active = true;
ds->ipv4_localhost = dss;
}
static void hdhomerun_discover_sock_detect_all_from_flags(struct hdhomerun_discover_t *ds, uint32_t flags)
{
if (flags & HDHOMERUN_DISCOVER_FLAGS_IPV6_LOCALHOST) {
hdhomerun_discover_sock_detect_ipv6_localhost(ds);
}
if (flags & (HDHOMERUN_DISCOVER_FLAGS_IPV6_GENERAL | HDHOMERUN_DISCOVER_FLAGS_IPV6_LINKLOCAL)) {
hdhomerun_discover_sock_detect_ipv6(ds);
}
if (flags & HDHOMERUN_DISCOVER_FLAGS_IPV4_LOCALHOST) {
hdhomerun_discover_sock_detect_ipv4_localhost(ds);
}
if (flags & HDHOMERUN_DISCOVER_FLAGS_IPV4_GENERAL) {
hdhomerun_discover_sock_detect_ipv4(ds);
}
}
static void hdhomerun_discover_sock_flush_list(struct hdhomerun_discover_t *ds, struct hdhomerun_discover_sock_t *list)
{
struct hdhomerun_pkt_t *rx_pkt = &ds->rx_pkt;
hdhomerun_pkt_reset(rx_pkt);
struct hdhomerun_discover_sock_t *dss = list;
while (dss) {
if (!dss->active) {
dss = dss->next;
continue;
}
while (1) {
struct sockaddr_storage remote_addr;
size_t length = rx_pkt->limit - rx_pkt->end;
if (!hdhomerun_sock_recvfrom_ex(dss->sock, &remote_addr, rx_pkt->end, &length, 0)) {
break;
}
}
dss = dss->next;
}
}
static void hdhomerun_discover_sock_recv_list_append_list(struct hdhomerun_discover_sock_t **recv_list, struct hdhomerun_discover_sock_t *list)
{
struct hdhomerun_discover_sock_t *dss = list;
while (dss) {
if (!dss->active) {
dss = dss->next;
continue;
}
dss->recv_next = *recv_list;
*recv_list = dss;
dss = dss->next;
}
}
static bool hdhomerun_discover_send_request(struct hdhomerun_discover_t *ds, struct hdhomerun_discover_sock_t *dss, const struct sockaddr *remote_addr, const uint32_t device_types[], size_t device_types_count, uint32_t device_id)
{
if (device_types_count == 0) {
return false;
}
struct hdhomerun_pkt_t *tx_pkt = &ds->tx_pkt;
hdhomerun_pkt_reset(tx_pkt);
if (device_types_count == 1) {
hdhomerun_pkt_write_u8(tx_pkt, HDHOMERUN_TAG_DEVICE_TYPE);
hdhomerun_pkt_write_var_length(tx_pkt, 4);
hdhomerun_pkt_write_u32(tx_pkt, device_types[0]);
} else {
hdhomerun_pkt_write_u8(tx_pkt, HDHOMERUN_TAG_MULTI_TYPE);
hdhomerun_pkt_write_var_length(tx_pkt, device_types_count * 4);
while (device_types_count--) {
hdhomerun_pkt_write_u32(tx_pkt, *device_types++);
}
}
if (device_id != HDHOMERUN_DEVICE_ID_WILDCARD) {
hdhomerun_pkt_write_u8(tx_pkt, HDHOMERUN_TAG_DEVICE_ID);
hdhomerun_pkt_write_var_length(tx_pkt, 4);
hdhomerun_pkt_write_u32(tx_pkt, device_id);
}
hdhomerun_pkt_seal_frame(tx_pkt, HDHOMERUN_TYPE_DISCOVER_REQ);
char local_ip_str[64];
char remote_ip_str[64];
hdhomerun_sock_sockaddr_to_ip_str(local_ip_str, (const struct sockaddr *)&dss->local_ip, true);
hdhomerun_sock_sockaddr_to_ip_str(remote_ip_str, remote_addr, true);
hdhomerun_debug_printf(ds->dbg, "discover: send to %s via %s\n", remote_ip_str, local_ip_str);
return hdhomerun_sock_sendto_ex(dss->sock, remote_addr, tx_pkt->start, tx_pkt->end - tx_pkt->start, 0);
}
static void hdhomerun_discover_send_ipv6_targeted(struct hdhomerun_discover_t *ds, const struct sockaddr *target_addr, uint32_t flags, const uint32_t device_types[], size_t device_types_count, uint32_t device_id, struct hdhomerun_discover_sock_t **recv_list)
{
struct hdhomerun_discover_sock_t *default_dss = ds->ipv6_socks;
if (!default_dss) {
return;
}
hdhomerun_discover_sock_flush_list(ds, ds->ipv6_socks);
hdhomerun_discover_sock_recv_list_append_list(recv_list, ds->ipv6_socks);
struct sockaddr_in6 target_addr_in;
memcpy(&target_addr_in, target_addr, sizeof(target_addr_in));
target_addr_in.sin6_port = htons(HDHOMERUN_DISCOVER_UDP_PORT);
/* ipv6 linklocal - send out every interface if the scope id isn't provided */
if (hdhomerun_sock_sockaddr_is_ipv6_linklocal(target_addr) && (target_addr_in.sin6_scope_id == 0)) {
struct hdhomerun_discover_sock_t *dss = default_dss->next;
while (dss) {
if (!dss->active) {
dss = dss->next;
continue;
}
if (!hdhomerun_sock_sockaddr_is_ipv6_linklocal((const struct sockaddr *)&dss->local_ip)) {
dss = dss->next;
continue;
}
struct sockaddr_in6 *local_addr_in = (struct sockaddr_in6 *)&dss->local_ip;
target_addr_in.sin6_scope_id = local_addr_in->sin6_scope_id;
if (!hdhomerun_discover_send_request(ds, dss, (const struct sockaddr *)&target_addr_in, device_types, device_types_count, device_id)) {
dss = dss->next;
continue;
}
dss = dss->next;
}
return;
}
hdhomerun_discover_send_request(ds, default_dss, (const struct sockaddr *)&target_addr_in, device_types, device_types_count, device_id);
}
static void hdhomerun_discover_send_ipv4_targeted(struct hdhomerun_discover_t *ds, const struct sockaddr *target_addr, uint32_t flags, const uint32_t device_types[], size_t device_types_count, uint32_t device_id, struct hdhomerun_discover_sock_t **recv_list)
{
struct hdhomerun_discover_sock_t *default_dss = ds->ipv4_socks;
if (!default_dss) {
return;
}
hdhomerun_discover_sock_flush_list(ds, ds->ipv4_socks);
hdhomerun_discover_sock_recv_list_append_list(recv_list, ds->ipv4_socks);
struct sockaddr_in target_addr_in;
memcpy(&target_addr_in, target_addr, sizeof(target_addr_in));
target_addr_in.sin_port = htons(HDHOMERUN_DISCOVER_UDP_PORT);
uint32_t target_ipv4 = ntohl(target_addr_in.sin_addr.s_addr);
bool local_subnet_send = false;
struct hdhomerun_discover_sock_t *dss = default_dss->next;
while (dss) {
if (!dss->active) {
dss = dss->next;
continue;
}
if (dss->ipv4_subnet_mask == 0) {
dss = dss->next;
continue;
}
struct sockaddr_in *local_ip_in = (struct sockaddr_in *)&dss->local_ip;
uint32_t local_ipv4 = ntohl(local_ip_in->sin_addr.s_addr);
if ((target_ipv4 & dss->ipv4_subnet_mask) != (local_ipv4 & dss->ipv4_subnet_mask)) {
dss = dss->next;
continue;
}
if (!hdhomerun_discover_send_request(ds, dss, (const struct sockaddr *)&target_addr_in, device_types, device_types_count, device_id)) {
dss = dss->next;
continue;
}
local_subnet_send = true;
dss = dss->next;
}
if (local_subnet_send) {
return;
}
if (hdhomerun_sock_sockaddr_is_ipv4_autoip(target_addr)) {
return;
}
hdhomerun_discover_send_request(ds, default_dss, (const struct sockaddr *)&target_addr_in, device_types, device_types_count, device_id);
}
static void hdhomerun_discover_send_ipv6_multicast(struct hdhomerun_discover_t *ds, uint32_t flags, const uint32_t device_types[], size_t device_types_count, uint32_t device_id, struct hdhomerun_discover_sock_t **recv_list)
{
struct hdhomerun_discover_sock_t *default_dss = ds->ipv6_socks;
if (!default_dss) {
return;
}
hdhomerun_discover_sock_flush_list(ds, ds->ipv6_socks);
hdhomerun_discover_sock_recv_list_append_list(recv_list, ds->ipv6_socks);
if (flags & HDHOMERUN_DISCOVER_FLAGS_IPV6_LINKLOCAL) {
struct sockaddr_in6 sock_addr_in;
memset(&sock_addr_in, 0, sizeof(sock_addr_in));
sock_addr_in.sin6_family = AF_INET6;
memcpy(sock_addr_in.sin6_addr.s6_addr, hdhomerun_discover_ipv6_linklocal_multicast_ip, 16);
sock_addr_in.sin6_port = htons(HDHOMERUN_DISCOVER_UDP_PORT);
struct hdhomerun_discover_sock_t *dss = default_dss->next;
while (dss) {
if (!dss->active) {
dss = dss->next;
continue;
}
if (!hdhomerun_sock_sockaddr_is_ipv6_linklocal((const struct sockaddr *)&dss->local_ip)) {
dss = dss->next;
continue;
}
hdhomerun_sock_set_ipv6_multicast_ifindex(dss->sock, dss->ifindex);
struct sockaddr_in6 *local_ip_in = (struct sockaddr_in6 *)&dss->local_ip;
sock_addr_in.sin6_scope_id = local_ip_in->sin6_scope_id;
if (!hdhomerun_discover_send_request(ds, dss, (const struct sockaddr *)&sock_addr_in, device_types, device_types_count, device_id)) {
dss = dss->next;
continue;
}
dss = dss->next;
}
}
if (flags & HDHOMERUN_DISCOVER_FLAGS_IPV6_GENERAL) {
struct sockaddr_in6 sock_addr_in;
memset(&sock_addr_in, 0, sizeof(sock_addr_in));
sock_addr_in.sin6_family = AF_INET6;
memcpy(sock_addr_in.sin6_addr.s6_addr, hdhomerun_discover_ipv6_sitelocal_multicast_ip, 16);
sock_addr_in.sin6_port = htons(HDHOMERUN_DISCOVER_UDP_PORT);
struct hdhomerun_discover_sock_t *dss = default_dss->next;
while (dss) {
if (!dss->active) {
dss = dss->next;
continue;
}
if (hdhomerun_sock_sockaddr_is_ipv6_linklocal((const struct sockaddr *)&dss->local_ip)) {
dss = dss->next;
continue;
}
hdhomerun_sock_set_ipv6_multicast_ifindex(dss->sock, dss->ifindex);
struct sockaddr_in6 *local_ip_in = (struct sockaddr_in6 *)&dss->local_ip;
sock_addr_in.sin6_scope_id = local_ip_in->sin6_scope_id;
if (!hdhomerun_discover_send_request(ds, dss, (const struct sockaddr *)&sock_addr_in, device_types, device_types_count, device_id)) {
dss = dss->next;
continue;
}
dss = dss->next;
}
}
}
static void hdhomerun_discover_send_ipv4_broadcast(struct hdhomerun_discover_t *ds, uint32_t flags, const uint32_t device_types[], size_t device_types_count, uint32_t device_id, struct hdhomerun_discover_sock_t **recv_list)
{
struct hdhomerun_discover_sock_t *default_dss = ds->ipv4_socks;
if (!default_dss) {
return;
}
hdhomerun_discover_sock_flush_list(ds, ds->ipv4_socks);
hdhomerun_discover_sock_recv_list_append_list(recv_list, ds->ipv4_socks);
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(0xFFFFFFFF);
sock_addr_in.sin_port = htons(HDHOMERUN_DISCOVER_UDP_PORT);
struct hdhomerun_discover_sock_t *dss = default_dss->next;
while (dss) {
if (!dss->active) {
dss = dss->next;
continue;
}
#if defined(IP_ONESBCAST)
struct sockaddr_in *local_ip_in = (struct sockaddr_in *)&dss->local_ip;
uint32_t local_ip_val = ntohl(local_ip_in->sin_addr.s_addr);
uint32_t subnet_broadcast = local_ip_val | ~dss->ipv4_subnet_mask;
if ((subnet_broadcast == 0) || (subnet_broadcast >= 0xE0000000)) {
dss = dss->next;
continue;
}
hdhomerun_sock_set_ipv4_onesbcast(dss->sock, 1);
sock_addr_in.sin_addr.s_addr = htonl(subnet_broadcast);
#endif
bool send_ok = hdhomerun_discover_send_request(ds, dss, (const struct sockaddr *)&sock_addr_in, device_types, device_types_count, device_id);
hdhomerun_sock_set_ipv4_onesbcast(dss->sock, 0);
if (!send_ok) {
dss = dss->next;
continue;
}
dss = dss->next;
}
}
static void hdhomerun_discover_send_ipv6_localhost(struct hdhomerun_discover_t *ds, const struct sockaddr *target_addr, uint32_t flags, const uint32_t device_types[], size_t device_types_count, uint32_t device_id, struct hdhomerun_discover_sock_t **recv_list)
{
struct hdhomerun_discover_sock_t *localhost_dss = ds->ipv6_localhost;
if (!localhost_dss) {
return;
}
hdhomerun_discover_sock_flush_list(ds, localhost_dss);
hdhomerun_discover_sock_recv_list_append_list(recv_list, localhost_dss);
struct sockaddr_in6 sock_addr_in;
if (target_addr) {
memcpy(&sock_addr_in, target_addr, sizeof(sock_addr_in));
} else {
memset(&sock_addr_in, 0, sizeof(sock_addr_in));
sock_addr_in.sin6_family = AF_INET6;
sock_addr_in.sin6_addr.s6_addr[15] = 1;
}
sock_addr_in.sin6_port = htons(HDHOMERUN_DISCOVER_UDP_PORT);
hdhomerun_discover_send_request(ds, localhost_dss, (const struct sockaddr *)&sock_addr_in, device_types, device_types_count, device_id);
}
static void hdhomerun_discover_send_ipv4_localhost(struct hdhomerun_discover_t *ds, const struct sockaddr *target_addr, uint32_t flags, const uint32_t device_types[], size_t device_types_count, uint32_t device_id, struct hdhomerun_discover_sock_t **recv_list)
{
struct hdhomerun_discover_sock_t *localhost_dss = ds->ipv4_localhost;
if (!localhost_dss) {
return;
}
hdhomerun_discover_sock_flush_list(ds, localhost_dss);
hdhomerun_discover_sock_recv_list_append_list(recv_list, localhost_dss);
struct sockaddr_in sock_addr_in;
if (target_addr) {
memcpy(&sock_addr_in, target_addr, sizeof(sock_addr_in));
} else {
memset(&sock_addr_in, 0, sizeof(sock_addr_in));
sock_addr_in.sin_family = AF_INET;
sock_addr_in.sin_addr.s_addr = htonl(0x7F000001);
}
sock_addr_in.sin_port = htons(HDHOMERUN_DISCOVER_UDP_PORT);
hdhomerun_discover_send_request(ds, localhost_dss, (const struct sockaddr *)&sock_addr_in, device_types, device_types_count, device_id);
}
static uint8_t hdhomerun_discover_compute_device_if_priority(struct hdhomerun_discover2_device_if_t *device_if)
{
if (hdhomerun_sock_sockaddr_is_ipv6_localhost((const struct sockaddr *)&device_if->ip_addr)) {
return 0; /* highest priority */
}
if (hdhomerun_sock_sockaddr_is_ipv4_localhost((const struct sockaddr *)&device_if->ip_addr)) {
return 1;
}
if (device_if->ip_addr.ss_family == AF_INET6) {
if (hdhomerun_sock_sockaddr_is_ipv6_global((const struct sockaddr *)&device_if->ip_addr)) {
return 2;
}
if (!hdhomerun_sock_sockaddr_is_ipv6_linklocal((const struct sockaddr *)&device_if->ip_addr)) {
return 3;
}
return 4;
}
if (!hdhomerun_sock_sockaddr_is_ipv4_autoip((const struct sockaddr *)&device_if->ip_addr)) {
return 5;
}
return 6;
}
static void hdhomerun_discover_recv_internal_device_type(struct hdhomerun_discover2_device_t *device, struct hdhomerun_pkt_t *rx_pkt)
{
uint32_t device_type = hdhomerun_pkt_read_u32(rx_pkt);
if ((device_type == 0) || (device_type == HDHOMERUN_DEVICE_TYPE_WILDCARD)) {
return;
}
struct hdhomerun_discover2_device_type_t **pprev = &device->type_list;
struct hdhomerun_discover2_device_type_t *p = device->type_list;
while (p) {
if (p->device_type >= device_type) {
if (p->device_type > device_type) {
break;
}
return;
}
pprev = &p->next;
p = p->next;
}
struct hdhomerun_discover2_device_type_t *new_type = (struct hdhomerun_discover2_device_type_t *)calloc(sizeof(struct hdhomerun_discover2_device_type_t), 1);
if (!new_type) {
return;
}
new_type->device_type = device_type;
new_type->next = *pprev;
*pprev = new_type;
}
static void hdhomerun_discover_recv_internal_string(char **poutput, struct hdhomerun_pkt_t *rx_pkt, size_t len)
{
if (*poutput) {
return;
}
char *str = (char *)malloc(len + 1);
if (!str) {
return;
}
hdhomerun_pkt_read_mem(rx_pkt, str, len);
str[len] = 0;
*poutput = str;
}
static void hdhomerun_discover_recv_internal_auth_bin(char **poutput, struct hdhomerun_pkt_t *rx_pkt, size_t len)
{
if (*poutput) {
return;
}
if (len != 18) {
return;
}
char *str = (char *)malloc(24 + 1);
if (!str) {
return;
}
int i;
for (i = 0; i < 24; i += 4) {
static const char hdhomerun_discover_recv_base64_encode_table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
uint32_t raw24;
raw24 = (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 16;
raw24 |= (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 8;
raw24 |= (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 0;
str[i + 0] = hdhomerun_discover_recv_base64_encode_table[(raw24 >> 18) & 0x3F];
str[i + 1] = hdhomerun_discover_recv_base64_encode_table[(raw24 >> 12) & 0x3F];
str[i + 2] = hdhomerun_discover_recv_base64_encode_table[(raw24 >> 6) & 0x3F];
str[i + 3] = hdhomerun_discover_recv_base64_encode_table[(raw24 >> 0) & 0x3F];
}
str[24] = 0;
*poutput = str;
}
static void hdhomerun_discover_recv_fixup_tuner_count(struct hdhomerun_discover_t *ds, struct hdhomerun_discover2_device_t *device)
{
if (!hdhomerun_discover2_device_is_type(device, HDHOMERUN_DEVICE_TYPE_TUNER)) {
return;
}
if (device->tuner_count > 0) {
return;
}
switch (device->device_id >> 20) {
case 0x102:
device->tuner_count = 1;
break;
case 0x100:
case 0x101:
case 0x121:
device->tuner_count = 2;
break;
default:
break;
}
}
static void hdhomerun_discover_recv_fixup_base_url(struct hdhomerun_discover_t *ds, struct hdhomerun_discover2_device_t *device)
{
if (!hdhomerun_discover2_device_is_type(device, HDHOMERUN_DEVICE_TYPE_TUNER)) {
return;
}
struct hdhomerun_discover2_device_if_t *device_if = device->if_list;
if (device_if->base_url) {
return;
}
if (device_if->ip_addr.ss_family != AF_INET) {
return;
}
device_if->base_url = (char *)malloc(32);
if (!device_if->base_url) {
return;
}
struct sockaddr_in *sock_addr_in = (struct sockaddr_in *)&device_if->ip_addr;
uint32_t ip = ntohl(sock_addr_in->sin_addr.s_addr);
hdhomerun_sprintf(device_if->base_url, device_if->base_url + 32, "http://%u.%u.%u.%u:80",
(ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, (ip >> 0) & 0xFF
);
}
static bool hdhomerun_discover_recv_internal(struct hdhomerun_discover_t *ds, struct hdhomerun_pkt_t *rx_pkt, struct hdhomerun_discover2_device_t *device)
{
uint16_t type;
if (hdhomerun_pkt_open_frame(rx_pkt, &type) <= 0) {
return false;
}
if (type != HDHOMERUN_TYPE_DISCOVER_RPY) {
return false;
}
struct hdhomerun_discover2_device_if_t *device_if = device->if_list;
while (1) {
uint8_t tag;
size_t len;
uint8_t *next = hdhomerun_pkt_read_tlv(rx_pkt, &tag, &len);
if (!next) {
break;
}
switch (tag) {
case HDHOMERUN_TAG_DEVICE_TYPE:
if (len != 4) {
break;
}
hdhomerun_discover_recv_internal_device_type(device, rx_pkt);
break;
case HDHOMERUN_TAG_MULTI_TYPE:
while (len >= 4) {
hdhomerun_discover_recv_internal_device_type(device, rx_pkt);
len -= 4;
}
break;
case HDHOMERUN_TAG_DEVICE_ID:
if (len != 4) {
break;
}
device->device_id = hdhomerun_pkt_read_u32(rx_pkt);
break;
case HDHOMERUN_TAG_TUNER_COUNT:
if (len != 1) {
break;
}
device->tuner_count = hdhomerun_pkt_read_u8(rx_pkt);
break;
case HDHOMERUN_TAG_DEVICE_AUTH_STR:
hdhomerun_discover_recv_internal_string(&device->device_auth, rx_pkt, len);
break;
case HDHOMERUN_TAG_DEVICE_AUTH_BIN_DEPRECATED:
hdhomerun_discover_recv_internal_auth_bin(&device->device_auth, rx_pkt, len);
break;
case HDHOMERUN_TAG_BASE_URL:
hdhomerun_discover_recv_internal_string(&device_if->base_url, rx_pkt, len);
break;
case HDHOMERUN_TAG_STORAGE_ID:
hdhomerun_discover_recv_internal_string(&device->storage_id, rx_pkt, len);
break;