forked from ka9q/ka9q-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multicast.c
995 lines (897 loc) · 29.1 KB
/
multicast.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
// Multicast socket and RTP utility routines for ka9q-radio
// Copyright 2018-2023 Phil Karn, KA9Q
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <net/if.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <ifaddrs.h>
#include <fcntl.h>
#include <errno.h>
#if defined(linux)
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <bsd/string.h>
#endif
#ifdef __APPLE__
#include <net/if_dl.h>
#endif
#include "multicast.h"
#include "misc.h"
static int ipv4_join_group(int const fd,void const * const sock,char const * const iface);
static int ipv6_join_group(int const fd,void const * const sock,char const * const iface);
static void set_local_options(int);
static void set_ipv4_options(int fd,int mcast_ttl,int tos);
static void set_ipv6_options(int const fd,int const mcast_ttl,int const tos);
struct pt_table PT_table[128] = {
{ 0, 0, 0 }, // 0
{ 0, 0, 0 }, // 1
{ 0, 0, 0 }, // 2
{ 0, 0, 0 }, // 3
{ 0, 0, 0 }, // 4
{ 0, 0, 0 }, // 5
{ 0, 0, 0 }, // 6
{ 0, 0, 0 }, // 7
{ 0, 0, 0 }, // 8
{ 0, 0, 0 }, // 9
{ 44100, 2, S16BE }, // 10
{ 44100, 1, S16BE }, // 11
{ 0, 0, 0 }, // 12
{ 0, 0, 0 }, // 13
{ 0, 0, 0 }, // 14
{ 0, 0, 0 }, // 15
{ 0, 0, 0 }, // 16
{ 0, 0, 0 }, // 17
{ 0, 0, 0 }, // 18
{ 0, 0, 0 }, // 19
{ 0, 0, 0 }, // 20
{ 0, 0, 0 }, // 21
{ 0, 0, 0 }, // 22
{ 0, 0, 0 }, // 23
{ 0, 0, 0 }, // 24
{ 0, 0, 0 }, // 25
{ 0, 0, 0 }, // 26
{ 0, 0, 0 }, // 27
{ 0, 0, 0 }, // 28
{ 0, 0, 0 }, // 29
{ 0, 0, 0 }, // 30
{ 0, 0, 0 }, // 31
{ 0, 0, 0 }, // 32
{ 0, 0, 0 }, // 33
{ 0, 0, 0 }, // 34
{ 0, 0, 0 }, // 35
{ 0, 0, 0 }, // 36
{ 0, 0, 0 }, // 37
{ 0, 0, 0 }, // 38
{ 0, 0, 0 }, // 39
{ 0, 0, 0 }, // 40
{ 0, 0, 0 }, // 41
{ 0, 0, 0 }, // 42
{ 0, 0, 0 }, // 43
{ 0, 0, 0 }, // 44
{ 0, 0, 0 }, // 45
{ 0, 0, 0 }, // 46
{ 0, 0, 0 }, // 47
{ 0, 0, 0 }, // 48
{ 0, 0, 0 }, // 49
{ 0, 0, 0 }, // 50
{ 0, 0, 0 }, // 51
{ 0, 0, 0 }, // 52
{ 0, 0, 0 }, // 53
{ 0, 0, 0 }, // 54
{ 0, 0, 0 }, // 55
{ 0, 0, 0 }, // 56
{ 0, 0, 0 }, // 57
{ 0, 0, 0 }, // 58
{ 0, 0, 0 }, // 59
{ 0, 0, 0 }, // 60
{ 0, 0, 0 }, // 61
{ 0, 0, 0 }, // 62
{ 0, 0, 0 }, // 63
{ 0, 0, 0 }, // 64
{ 0, 0, 0 }, // 65
{ 0, 0, 0 }, // 66
{ 0, 0, 0 }, // 67
{ 0, 0, 0 }, // 68
{ 0, 0, 0 }, // 69
{ 0, 0, 0 }, // 70
{ 0, 0, 0 }, // 71
{ 0, 0, 0 }, // 72
{ 0, 0, 0 }, // 73
{ 0, 0, 0 }, // 74
{ 0, 0, 0 }, // 75
{ 0, 0, 0 }, // 76
{ 0, 0, 0 }, // 77
{ 0, 0, 0 }, // 78
{ 0, 0, 0 }, // 79
{ 0, 0, 0 }, // 80
{ 0, 0, 0 }, // 81
{ 0, 0, 0 }, // 82
{ 0, 0, 0 }, // 83
{ 0, 0, 0 }, // 84
{ 0, 0, 0 }, // 85
{ 0, 0, 0 }, // 86
{ 0, 0, 0 }, // 87
{ 0, 0, 0 }, // 88
{ 0, 0, 0 }, // 89
{ 0, 0, 0 }, // 90
{ 0, 0, 0 }, // 91
{ 0, 0, 0 }, // 92
{ 0, 0, 0 }, // 93
{ 0, 0, 0 }, // 94
{ 0, 0, 0 }, // 95
{ 0, 0, 0 }, // 96
{ 0, 0, 0 }, // 97
{ 0, 0, 0 }, // 98
{ 0, 0, 0 }, // 99
{ 0, 0, 0 }, // 100
{ 0, 0, 0 }, // 101
{ 0, 0, 0 }, // 102
{ 0, 0, 0 }, // 103
{ 0, 0, 0 }, // 104
{ 0, 0, 0 }, // 105
{ 0, 0, 0 }, // 106
{ 0, 0, 0 }, // 107
{ 0, 0, 0 }, // 108
{ 0, 0, 0 }, // 109
{ 0, 0, 0 }, // 110
{ 48000, 2, OPUS }, // 111 Opus always uses a 48K virtual sample rate
{ 48000, 1, S16BE }, // 112
{ 48000, 2, S16BE }, // 113
{ 0, 0, 0 }, // 114
{ 0, 0, 0 }, // 115
{ 24000, 1, S16BE }, // 116
{ 24000, 2, S16BE }, // 117
{ 0, 0, 0 }, // 118
{ 16000, 1, S16BE }, // 119
{ 16000, 2, S16BE }, // 120
{ 0, 0, 0 }, // 121
{ 12000, 1, S16BE }, // 122
{ 12000, 2, S16BE }, // 123
{ 0, 0, 0 }, // 124
{ 8000, 1, S16BE }, // 125
{ 8000, 2, S16BE }, // 126
{ 0, 0, 0 }, // 127
};
#define AX25_PT (96) // NON-standard payload type for my raw AX.25 frames - clean this up and remove
#define OPUS_PT (111) // Hard-coded NON-standard payload type for OPUS (should be dynamic with sdp)
int const Opus_pt = OPUS_PT;
int const AX25_pt = AX25_PT;
// Add an encoding to the RTP payload type table
// The mappings are typically extracted from a radiod status channel and kept in a table so they can
// be changed midstream without losing anything
int add_pt(int type, int samprate, int channels, enum encoding encoding){
if(encoding == NO_ENCODING)
return -1;
if(encoding == OPUS){
// Force Opus to fixed values
samprate = 48000;
channels = 2;
}
if(type >= 0 && type < 128){
PT_table[type].channels = channels;
PT_table[type].samprate = samprate;
PT_table[type].encoding = encoding;
return 0;
} else
return -1;
}
// This is a bit messy. Is there a better way?
char const *Default_mcast_iface;
// Set up multicast socket for input or output
// Target points to string in the form of "domain[:port][,iface]"
// If target and sock are both non-null, the target will be resolved and copied into the sock structure
// If sock is null, the results of resolving target will not be stored there
// If target is null and sock is non-null, the existing sock structure contents will be used
// when output = 1, connect to the multicast address so we can simply send() to it without specifying a destination
// when output = 0, bind to it so we'll accept incoming packets
// Add parameter 'offset' (normally 0) to port number; this will be 1 when sending RTCP messages
// (Can we set up a socket for both input and output??)
int setup_mcast(char const * const target,struct sockaddr *sock,int const output,int const ttl,int const tos,int const offset,int tries){
if(target == NULL && sock == NULL)
return -1; // At least one must be supplied
if(sock == NULL){
sock = alloca(sizeof(struct sockaddr_storage));
memset(sock,0,sizeof(struct sockaddr_storage));
}
char iface[1024];
iface[0] = '\0';
if(target){
int ret = resolve_mcast(target,sock,DEFAULT_RTP_PORT+offset,iface,sizeof(iface),tries);
if(ret == -1)
return -1;
}
if(strlen(iface) == 0 && Default_mcast_iface != NULL)
strlcpy(iface,Default_mcast_iface,sizeof(iface));
if(output == 0)
return listen_mcast(sock,iface);
else
return connect_mcast(sock,iface,ttl,tos);
}
// Join an existing socket to a multicast group without connecting it
// Primarily useful for solving the smart switch problem described in connect_mcast() with unconnected sockets used with sendto()
// Since many channels may send to the same multicast group, the joins can often fail with harmless "address already in use" messages
// Note: only the IP address is significant, the port number is ignored
int join_group(int fd,struct sockaddr const * const sock, char const * const iface,int const ttl,int const tos){
if(fd == -1 || sock == NULL)
return -1;
switch(sock->sa_family){
case AF_INET:
set_ipv4_options(fd,ttl,tos);
if(ipv4_join_group(fd,sock,iface) != 0)
fprintf(stderr,"connect_mcast join_group failed\n");
break;
case AF_INET6:
set_ipv6_options(fd,ttl,tos);
if(ipv6_join_group(fd,sock,iface) != 0)
fprintf(stderr,"connect_mcast join_group failed\n");
break;
default:
return -1;
}
return 0;
}
// Create a socket for sending to a multicast group
int connect_mcast(void const * const s,char const * const iface,int const ttl,int const tos){
if(s == NULL)
return -1;
struct sockaddr const *sock = s;
int fd = socket(sock->sa_family,SOCK_DGRAM,0);
if(fd == -1)
return -1;
// Better to drop a packet than to block real-time processing
fcntl(fd,F_SETFL,O_NONBLOCK);
set_local_options(fd);
// Strictly speaking, it is not necessary to join a multicast group to which we only send.
// But this creates a problem with "smart" switches that do IGMP snooping.
// They have a setting to handle what happens with unregistered
// multicast groups (groups to which no IGMP messages are seen.)
// Discarding unregistered multicast breaks IPv6 multicast, which breaks ALL of IPv6
// because neighbor discovery uses multicast.
// It can also break IPv4 mDNS, though hardwiring 224.0.0.251 to flood can fix this.
// But if the switches are set to pass unregistered multicasts, then IPv4 multicasts
// that aren't subscribed to by anybody are flooded everywhere!
// We avoid that by subscribing to our own multicasts.
if(join_group(fd,sock,iface,ttl,tos) == -1)
return -1;
if(connect(fd,sock,sizeof(struct sockaddr)) == -1){
close(fd);
return -1;
}
return fd;
}
// Create a listening socket on specified socket, using specified interface
// Interface may be null
int listen_mcast(void const *s,char const *iface){
if(s == NULL)
return -1;
struct sockaddr const *sock = s;
int const fd = socket(sock->sa_family,SOCK_DGRAM,0);
if(fd == -1){
perror("setup_mcast socket");
return -1;
}
if(join_group(fd,sock,iface,-1,-1) == -1){
close(fd);
return -1;
}
if((bind(fd,sock,sizeof(struct sockaddr)) != 0)){
perror("listen mcast bind");
close(fd);
return -1;
}
return fd;
}
// Resolve a multicast target string in the form "name[:port][,iface]"
// If "name" is not qualified (no periods) then .local will be appended by default
// If :port is not specified, port field in result will be zero
int resolve_mcast(char const *target,void *sock,int default_port,char *iface,int iface_len,int tries){
if(target == NULL || strlen(target) == 0 || sock == NULL)
return -1;
char host[PATH_MAX]; // Maximum legal DNS name length?
strlcpy(host,target,sizeof(host));
// Look for ,iface at end of target. If present, delimit and copy to user
char *ifp = strrchr(host,',');
if(ifp != NULL){
// ,iface field found
*ifp++ = '\0'; // Zap ',' with null to end preceding string
}
if(iface != NULL && iface_len > 0){
if(ifp == NULL)
*iface = '\0';
else
strlcpy(iface,ifp,iface_len);
}
// Look for :port
char *port;
if((port = strrchr(host,':')) != NULL){
*port++ = '\0';
}
struct addrinfo *results;
int try;
// If no domain zone is specified, assume .local (i.e., for multicast DNS)
char full_host[PATH_MAX+6];
if(strchr(host,'.') == NULL)
snprintf(full_host,sizeof(full_host),"%s.local",host);
else
strlcpy(full_host,host,sizeof(full_host));
for(try=0;tries == 0 || try != tries;try++){
results = NULL;
struct addrinfo hints;
memset(&hints,0,sizeof(hints));
#if 1
// Using hints.ai_family = AF_UNSPEC generates both A and AAAA queries
// but even when the A query is answered the library times out and retransmits the AAAA
// query several times. So do only an A (IPv4) query the first time
hints.ai_family = (try == 0) ? AF_INET : AF_UNSPEC;
#else
// using AF_INET often fails on loopback.
// Did this get changed recently in getaddrinfo()?
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_ADDRCONFIG;
int const ecode = getaddrinfo(full_host,port,&hints,&results);
if(ecode == 0)
break;
if(try == 0) // Don't pollute the syslog
fprintf(stderr,"resolve_mcast getaddrinfo(host=%s, port=%s): %s. Retrying.\n",full_host,port,gai_strerror(ecode));
sleep(10);
}
if(tries != 0 && try == tries)
return -1;
if(try > 0) // Don't leave them hanging: report success after failure
fprintf(stderr,"resolve_mcast getaddrinfo(host=%s, port=%s) succeeded\n",full_host,port);
// Use first entry on list -- much simpler
// I previously tried each entry in turn until one succeeded, but with UDP sockets and
// flags set to only return supported addresses, how could any of them fail?
memcpy(sock,results->ai_addr,results->ai_addrlen);
if(port == NULL){
// Insert default port
setportnumber(sock,default_port);
}
freeaddrinfo(results); results = NULL;
return 0;
}
// Convert RTP header from network (wire) big-endian format to internal host structure
// Written to be insensitive to host byte order and C structure layout and padding
// Use of unsigned formats is important to avoid unwanted sign extension
void const *ntoh_rtp(struct rtp_header * const rtp,void const * const data){
uint32_t const *dp = data;
uint32_t const w = ntohl(*dp++);
rtp->version = w >> 30;
rtp->pad = (w >> 29) & 1;
rtp->extension = (w >> 28) & 1;
rtp->cc = (w >> 24) & 0xf;
rtp->marker = (w >> 23) & 1;
rtp->type = (w >> 16) & 0x7f;
rtp->seq = w & 0xffff;
rtp->timestamp = ntohl(*dp++);
rtp->ssrc = ntohl(*dp++);
for(int i=0; i<rtp->cc; i++)
rtp->csrc[i] = ntohl(*dp++);
if(rtp->extension){
int ext_len = ntohl(*dp++) & 0xffff; // Ignore any extension, but skip over it
dp += ext_len;
}
return dp;
}
// Convert RTP header from internal host structure to network (wire) big-endian format
// Written to be insensitive to host byte order and C structure layout and padding
void *hton_rtp(void * const data, struct rtp_header const * const rtp){
uint32_t *dp = data;
int cc = rtp->cc & 0xf; // Ensure in range, <= 15
*dp++ = htonl(RTP_VERS << 30 | rtp->pad << 29 | rtp->extension << 28 | cc << 24 | rtp->marker << 23
| (rtp->type & 0x7f) << 16 | rtp->seq);
*dp++ = htonl(rtp->timestamp);
*dp++ = htonl(rtp->ssrc);
for(int i=0; i < cc ; i++)
*dp++ = htonl(rtp->csrc[i]);
return dp;
}
// Process sequence number and timestamp in incoming RTP header:
// count dropped and duplicated packets, but it gets confused
// Determine timestamp jump from the next expected one
int rtp_process(struct rtp_state * const state,struct rtp_header const * const rtp,int const sampcnt){
if(rtp->ssrc != state->ssrc){
// Normally this will happen only on the first packet in a session since
// the caller demuxes the SSRC to multiple instances.
// But a single-instance, interactive application like 'radio' lets the SSRC
// change so it doesn't have to restart when the stream sender does.
state->init = false;
state->ssrc = rtp->ssrc; // Must be filtered elsewhere if you want it
}
if(!state->init){
state->packets = 0;
state->seq = rtp->seq;
state->timestamp = rtp->timestamp;
state->dupes = 0;
state->drops = 0;
state->init = true;
}
state->packets++;
// Sequence number check
int const seq_step = (int16_t)(rtp->seq - state->seq);
if(seq_step != 0){
if(seq_step < 0)
state->dupes++;
else
state->drops += seq_step;
}
state->seq = rtp->seq + 1;
int const time_step = (int32_t)(rtp->timestamp - state->timestamp);
state->timestamp = rtp->timestamp + sampcnt;
return time_step;
}
// Convert binary sockaddr structure (v4 or v6 or unix) to printable numeric string
char *formataddr(char *result,int size,void const *s){
struct sockaddr const *sa = (struct sockaddr *)s;
result[0] = '\0';
switch(sa->sa_family){
case AF_INET:
{
struct sockaddr_in const *sin = (struct sockaddr_in *)sa;
inet_ntop(AF_INET,&sin->sin_addr,result,size);
}
break;
case AF_INET6:
{
struct sockaddr_in6 const *sin = (struct sockaddr_in6 *)sa;
inet_ntop(AF_INET6,&sin->sin6_addr,result,size);
}
break;
}
return result;
}
// Convert binary sockaddr structure to printable host:port string
// cache result, as getnameinfo can be very slow when it doesn't get a reverse DNS hit
// Needs locks to be made thread safe
struct inverse_cache {
struct inverse_cache *next;
struct inverse_cache *prev;
struct sockaddr_storage sock;
char hostport [NI_MAXHOST+NI_MAXSERV+5];
};
static struct inverse_cache *Inverse_cache_table; // Head of cache linked list
// We actually take a sockaddr *, but can also accept a sockaddr_in *, sockaddr_in6 * and sockaddr_storage *
// so to make it easier for callers we just take a void * and avoid pointer casts that impair readability
char const *formatsock(void const *s){
// Determine actual length (and type) of binary socket structure (IPv4/IPv6)
int slen = 0;
struct sockaddr const * const sa = (struct sockaddr *)s;
if(sa == NULL)
return NULL;
switch(sa->sa_family){
case AF_INET:
slen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
slen = sizeof(struct sockaddr_in6);
break;
default: // shouldn't happen unless uninitialized
return NULL;
}
for(struct inverse_cache *ic = Inverse_cache_table; ic != NULL; ic = ic->next){
if(address_match(&ic->sock,sa) && getportnumber(&ic->sock) == getportnumber(sa)){
if(ic->prev == NULL)
return ic->hostport; // Already at top of list
// move to top of list so it'll be faster to find if we look for it again soon
ic->prev->next = ic->next;
if(ic->next)
ic->next->prev = ic->prev;
ic->next = Inverse_cache_table;
ic->next->prev = ic;
ic->prev = NULL;
Inverse_cache_table = ic;
return ic->hostport;
}
}
// Not in list yet, add at top
struct inverse_cache * const ic = (struct inverse_cache *)calloc(1,sizeof(*ic));
assert(ic != NULL); // Malloc failures are rare
char host[NI_MAXHOST],port[NI_MAXSERV];
memset(host,0,sizeof(host));
memset(port,0,sizeof(port));
getnameinfo(sa,slen,
host,NI_MAXHOST,
port,NI_MAXSERV,
NI_NOFQDN|NI_NUMERICHOST|NI_NUMERICSERV);
//NI_NOFQDN|NI_NUMERICSERV);
snprintf(ic->hostport,sizeof(ic->hostport),"%s:%s",host,port);
assert(slen < sizeof(ic->sock));
memcpy(&ic->sock,sa,slen);
// Put at head of table
ic->next = Inverse_cache_table;
if(ic->next)
ic->next->prev = ic;
Inverse_cache_table = ic;
return ic->hostport;
}
int samprate_from_pt(int const type){
if(type < 0 || type > 127)
return 0;
return PT_table[type].samprate;
}
int channels_from_pt(int const type){
if(type < 0 || type > 127)
return 0;
return PT_table[type].channels;
}
enum encoding encoding_from_pt(int const type){
if(type < 0 || type > 127)
return NO_ENCODING;
return PT_table[type].encoding;
}
// Dynamically create a new one if not found
// Should lock the table when it's modified
// Use for sending only! Receivers need to build a table for each sender
int pt_from_info(int samprate,int channels,enum encoding encoding){
if(samprate <= 0 || channels <= 0 || channels > 2 || encoding == NO_ENCODING || encoding >= UNUSED_ENCODING)
return -1;
if(encoding == OPUS){
// Force Opus to fixed values
channels = 2;
samprate = 48000;
}
// Search table for existing entry, otherwise create new entry
for(int type=0; type < 128; type++){
if(PT_table[type].samprate == samprate && PT_table[type].channels == channels && PT_table[type].encoding == encoding)
return type;
}
for(int type=96; type < 128; type++){ // Allocate a new type in the dynamic range
if(PT_table[type].samprate == 0){
// allocate it
if(add_pt(type,samprate,channels,encoding) == -1)
return -1;
return type;
}
}
return -1;
}
// Compare IP addresses in sockaddr structures for equality
int address_match(void const *arg1,void const *arg2){
struct sockaddr const *s1 = (struct sockaddr *)arg1;
struct sockaddr const *s2 = (struct sockaddr *)arg2;
if(s1->sa_family != s2->sa_family)
return 0;
switch(s1->sa_family){
case AF_INET:
{
struct sockaddr_in const *sinp1 = (struct sockaddr_in *)arg1;
struct sockaddr_in const *sinp2 = (struct sockaddr_in *)arg2;
if(memcmp(&sinp1->sin_addr,&sinp2->sin_addr,sizeof(sinp1->sin_addr)) == 0)
return 1;
}
break;
case AF_INET6:
{
struct sockaddr_in6 const *sinp1 = (struct sockaddr_in6 *)arg1;
struct sockaddr_in6 const *sinp2 = (struct sockaddr_in6 *)arg2;
if(memcmp(&sinp1->sin6_addr,&sinp2->sin6_addr,sizeof(sinp1->sin6_addr)) == 0)
return 1;
}
break;
}
return 0;
}
// Return port number (in HOST order) in a sockaddr structure
// Return -1 on error
int getportnumber(void const *arg){
if(arg == NULL)
return -1;
struct sockaddr const *sock = (struct sockaddr *)arg;
switch(sock->sa_family){
case AF_INET:
{
struct sockaddr_in const *sin = (struct sockaddr_in *)sock;
return ntohs(sin->sin_port);
}
break;
case AF_INET6:
{
struct sockaddr_in6 const *sin6 = (struct sockaddr_in6 *)sock;
return ntohs(sin6->sin6_port);
}
break;
default:
return -1;
}
}
// Set the port number on a sockaddr structure
// Port number argument is in HOST order
int setportnumber(void *s,uint16_t port){
if(s == NULL)
return -1;
struct sockaddr *sock = s;
switch(sock->sa_family){
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *)sock;
sin->sin_port = htons(port);
}
break;
case AF_INET6:
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sock;
sin6->sin6_port = htons(port);
}
break;
default:
return -1;
}
return 0;
}
// Set options on UNIX socket
static void set_local_options(int const fd){
// Failures here are not fatal
int const reuse = true; // bool doesn't work for some reason
if(setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,&reuse,sizeof(reuse)) != 0)
perror("so_reuseport failed");
if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) != 0)
perror("so_reuseaddr failed");
struct linger linger;
linger.l_onoff = 0;
linger.l_linger = 0;
if(setsockopt(fd,SOL_SOCKET,SO_LINGER,&linger,sizeof(linger)) != 0)
perror("so_linger failed");
}
// Set options on IPv4 multicast socket
static void set_ipv4_options(int const fd,int const mcast_ttl,int const tos){
// Failures here are not fatal
#ifdef IP_FREEBIND
int const freebind = true;
if(setsockopt(fd,IPPROTO_IP,IP_FREEBIND,&freebind,sizeof(freebind)) != 0)
perror("freebind failed");
#endif
int const reuse = true; // bool doesn't work for some reason
if(setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,&reuse,sizeof(reuse)) != 0)
perror("so_reuseport failed");
if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) != 0)
perror("so_reuseaddr failed");
struct linger linger;
linger.l_onoff = 0;
linger.l_linger = 0;
if(setsockopt(fd,SOL_SOCKET,SO_LINGER,&linger,sizeof(linger)) != 0)
perror("so_linger failed");
if(mcast_ttl >= 0){
if(setsockopt(fd,IPPROTO_IP,IP_MULTICAST_TTL,&mcast_ttl,sizeof(mcast_ttl)) != 0)
perror("so_ttl failed");
}
uint8_t const loop = true;
if(setsockopt(fd,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop)) != 0)
perror("so_loop failed");
if(tos >= 0){
// Only needed on output
if(setsockopt(fd,IPPROTO_IP,IP_TOS,&tos,sizeof(tos)) != 0)
perror("so_tos failed");
}
}
// Set options on IPv6 multicast socket
static void set_ipv6_options(int const fd,int const mcast_ttl,int const tos){
// Failures here are not fatal
int const reuse = true; // bool doesn't work for some reason
if(setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,&reuse,sizeof(reuse)) != 0)
perror("so_reuseport failed");
if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) != 0)
perror("so_reuseaddr failed");
struct linger linger;
linger.l_onoff = 0;
linger.l_linger = 0;
if(setsockopt(fd,SOL_SOCKET,SO_LINGER,&linger,sizeof(linger)) != 0)
perror("so_linger failed");
if(mcast_ttl >= 0){
// Only needed on output
uint8_t const ttl = mcast_ttl;
if(setsockopt(fd,IPPROTO_IPV6,IPV6_MULTICAST_HOPS,&ttl,sizeof(ttl)) != 0)
perror("so_ttl failed");
}
uint8_t const loop = 1;
if(setsockopt(fd,IPPROTO_IPV6,IPV6_MULTICAST_LOOP,&loop,sizeof(loop)) != 0)
perror("so_loop failed");
if(tos >= 0){
// Only needed on output
if(setsockopt(fd,IPPROTO_IPV6,IPV6_TCLASS,&tos,sizeof(tos)) != 0)
perror("so_tos failed");
}
}
// Join a socket to a multicast group
static int ipv4_join_group(int const fd,void const * const sock,char const * const iface){
if(fd < 0 || sock == NULL)
return -1;
// Ensure it's a multicast address
// Is this check really necessary?
// Maybe the setsockopt would just fail cleanly if it's not
struct sockaddr_in const * const sin = (struct sockaddr_in *)sock;
if(!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
return -1;
struct ip_mreqn mreqn;
mreqn.imr_multiaddr = sin->sin_addr;
mreqn.imr_address.s_addr = INADDR_ANY;
if(iface == NULL || strlen(iface) == 0)
mreqn.imr_ifindex = 0;
else
mreqn.imr_ifindex = if_nametoindex(iface);
if(setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) != 0 && errno != EADDRINUSE){
perror("multicast v4 join");
return -1;
}
return 0;
}
static int ipv6_join_group(int const fd,void const * const sock,char const * const iface){
if(fd < 0 || sock == NULL)
return -1;
// Ensure it's a multicast address
// Is this check really necessary?
// Maybe the setsockopt would just fail cleanly if it's not
struct sockaddr_in6 const * const sin6 = (struct sockaddr_in6 *)sock;
if(!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
return -1;
struct ipv6_mreq ipv6_mreq;
ipv6_mreq.ipv6mr_multiaddr = sin6->sin6_addr;
if(iface == NULL || strlen(iface) == 0)
ipv6_mreq.ipv6mr_interface = 0; // Default interface
else
ipv6_mreq.ipv6mr_interface = if_nametoindex(iface);
// Doesn't seem to be defined on Mac OSX, but is supposed to be synonymous with IPV6_JOIN_GROUP
#ifndef IPV6_ADD_MEMBERSHIP
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#endif
if(setsockopt(fd,IPPROTO_IP,IPV6_ADD_MEMBERSHIP,&ipv6_mreq,sizeof(ipv6_mreq)) != 0 && errno != EADDRINUSE){
perror("multicast v6 join");
return -1;
}
return 0;
}
static struct {
int flag;
char const *name;
} flags[] = {{IFF_UP,"UP"},
{IFF_BROADCAST,"BROADCAST"},
{IFF_DEBUG,"DEBUG"},
{IFF_LOOPBACK,"LOOPBACK"},
{IFF_POINTOPOINT,"PTP"},
{IFF_RUNNING,"RUNNING"},
{IFF_NOARP,"NOARP"},
{IFF_PROMISC,"PROMISC"},
{IFF_NOTRAILERS,"NOTRAILERS"},
{IFF_ALLMULTI,"ALLMULTI"},
#ifdef IFF_MASTER
{IFF_MASTER,"MASTER"},
#endif
#ifdef IFF_SLAVE
{IFF_SLAVE,"SLAVE"},
#endif
{IFF_MULTICAST,"MULTICAST"},
#ifdef IFF_PORTSEL
{IFF_PORTSEL,"PORTSEL"},
#endif
#ifdef IFF_AUOMEDIA
{IFF_AUTOMEDIA,"AUTOMEDIA"},
#endif
#ifdef IFF_DYNAMIC
{IFF_DYNAMIC,"DYNAMIC"},
#endif
#ifdef IFF_LOWER_UP
{IFF_LOWER_UP,"LOWER_UP"},
#endif
#ifdef IFF_DORMANT
{IFF_DORMANT,"DORMANT"},
#endif
#ifdef IFF_ECHO
{IFF_ECHO,"ECHO"},
#endif
{0, NULL},
};
// Dump list of interfaces
void dump_interfaces(void){
struct ifaddrs *ifap = NULL;
getifaddrs(&ifap);
fprintf(stdout,"Interface list:\n");
for(struct ifaddrs const *i = ifap; i != NULL; i = i->ifa_next){
int const family = i->ifa_addr->sa_family;
char const *familyname = NULL;
int socksize = 0;
switch(family){
case AF_INET:
familyname = "AF_INET";
socksize = sizeof(struct sockaddr_in);
break;
case AF_INET6:
familyname = "AF_INET6";
socksize = sizeof(struct sockaddr_in6);
break;
#ifdef AF_LINK
case AF_LINK:
familyname = "AF_LINK";
socksize = sizeof(struct sockaddr_dl);
break;
#endif
#ifdef AF_PACKET
case AF_PACKET:
familyname = "AF_PACKET";
socksize = sizeof(struct sockaddr_ll);
break;
#endif
default:
familyname = "?";
break;
}
fprintf(stdout,"%s %s(%d)",i->ifa_name,familyname,family);
char host[NI_MAXHOST];
if(i->ifa_addr && getnameinfo(i->ifa_addr,socksize,host,NI_MAXHOST,NULL,0,NI_NUMERICHOST) == 0)
fprintf(stdout," addr %s",host);
if(i->ifa_dstaddr && getnameinfo(i->ifa_dstaddr,socksize,host,NI_MAXHOST,NULL,0,NI_NUMERICHOST) == 0)
fprintf(stdout," dstaddr %s",host);
if(i->ifa_netmask && getnameinfo(i->ifa_netmask,socksize,host,NI_MAXHOST,NULL,0,NI_NUMERICHOST) == 0)
fprintf(stdout," mask %s",host);
if(i->ifa_data)
fprintf(stdout," data %p",i->ifa_data);
const int f = i->ifa_flags;
for(int j=0;flags[j].flag != 0; j++){
if(f & flags[j].flag)
fprintf(stdout," %s",flags[j].name);
}
fprintf(stdout,"\n");
}
fprintf(stdout,"end of list\n");
freeifaddrs(ifap);
ifap = NULL;
}
char const *encoding_string(enum encoding e){
switch(e){
default:
case NO_ENCODING:
return "none";
case S16LE:
return "s16le";
case S16BE:
return "s16be";
case OPUS:
return "opus";
case F32LE:
return "f32le";
case AX25:
return "ax.25";
case F16LE:
return "f16le";
}
}
enum encoding parse_encoding(char const *str){
if(strcasecmp(str,"s16be") == 0 || strcasecmp(str,"s16") == 0 || strcasecmp(str,"int") == 0)
return S16BE;
else if(strcasecmp(str,"s16le") == 0)
return S16LE;
else if(strcasecmp(str,"f32") == 0 || strcasecmp(str,"float") == 0 || strcasecmp(str,"f32le") == 0)
return F32LE;
else if(strcasecmp(str,"f16") == 0 || strcasecmp(str,"f16le") == 0)
return F16LE;
else if(strcasecmp(str,"opus") == 0)
return OPUS;
else if(strcasecmp(str,"ax25") == 0 || strcasecmp(str,"ax.25") == 0)
return AX25;
else
return NO_ENCODING;
}
// Generate a multicast address in the 239.0.0.0/8 administratively scoped block
// avoiding 239.0.0.0/24 and 239.128.0.0/24 since these map at the link layer
// into the same Ethernet multicast MAC addresses as the 224.0.0.0/8 multicast control block
// that is not snooped by switches
uint32_t make_maddr(char const *arg){
// uint32_t addr = (239U << 24) | (ElfHashString(arg) & 0xffffff); // poor performance when last byte is always the same (.)
uint32_t addr = (239U << 24) | (fnv1hash((uint8_t *)arg,strlen(arg)) & 0xffffff);
// avoid 239.0.0.0/24 and 239.128.0.0/24 since they map to the same
// Ethernet multicast MAC addresses as 224.0.0.0/24, the internet control block
// This increases the risk of collision slightly (512 out of 16 M)
if((addr & 0x007fff00) == 0)
addr |= (addr & 0xff) << 8;
if((addr & 0x007fff00) == 0)
addr |= 0x00100000; // Small chance of this for a random address
return addr;
}