-
Notifications
You must be signed in to change notification settings - Fork 22
/
ProxyChannel.cxx
18975 lines (17348 loc) · 736 KB
/
ProxyChannel.cxx
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
//////////////////////////////////////////////////////////////////
//
// ProxyChannel.cxx
//
// Copyright (c) Citron Network Inc. 2001-2002
// Copyright (c) 2002-2024, Jan Willamowius
//
// This work is published under the GNU Public License version 2 (GPLv2)
// see file COPYING for details.
// We also explicitly grant the right to link this code
// with the OpenH323/H323Plus and OpenSSL library.
//
//////////////////////////////////////////////////////////////////
#define __APPLE_USE_RFC_3542 // MacOSX supposedly needs this to be able to set the UDP source IP
#include "config.h"
#include <ptlib.h>
#include <ptclib/random.h>
#include <q931.h>
#include <h245.h>
#include <h323pdu.h>
#include "snmp.h"
#include "gk.h"
#include "gk_const.h"
#include "h323util.h"
#include "Toolkit.h"
#include "stl_supp.h"
#include "gkacct.h"
#include "RasSrv.h"
#include "GkClient.h"
#include "Neighbor.h"
#include "sigmsg.h"
#include "ProxyChannel.h"
#include "GkStatus.h"
#include <queue>
#include <math.h> // needed for ceil() on Solaris 11, OpenBSD
#include <algorithm>
#ifdef HAS_AVAYA_SUPPORT
#include "avaya.h"
#endif
#ifdef H323_H450
#ifdef h323pluslib
#include "h450/h4501.h"
#include "h450/h4502.h"
#else
#include "h4501.h"
#include "h4502.h"
#endif
#include "SoftPBX.h"
#endif
#ifdef HAS_H460
#include <h460/h4601.h>
#ifdef HAS_H46018
#include <h460/h46018.h>
#include <h460/h46019.h>
#endif
#endif
#ifdef HAS_H235_MEDIA
#include "h235/h2351.h"
#include "h235/h2356.h"
#include "h235/h235crypto.h"
#endif
#if _WIN32 || _WIN64
#include <mswsock.h>
# ifndef SHUT_RDWR
# define SHUT_RDWR SD_BOTH
# endif
# ifndef SHUT_WR
# define SHUT_WR SD_SEND
# endif
#endif
#ifdef P_OPENBSD
#include <sys/uio.h>
#endif
using namespace std;
using Routing::Route;
const char* RoutedSec = "RoutedMode";
const char* TLSSec = "TLS";
const char* ProxySection = "Proxy";
char H225_ProtocolID[ProtocolID_BufferSize];
char H245_ProtocolID[ProtocolID_BufferSize];
const char *H225_Protocol_Version[MAX_H323_VERSION + 1] = {
"0.0.8.2250.0.0", // dummy, never used
"0.0.8.2250.0.1",
"0.0.8.2250.0.2",
"0.0.8.2250.0.3",
"0.0.8.2250.0.4",
"0.0.8.2250.0.5",
"0.0.8.2250.0.6",
"0.0.8.2250.0.7"
};
const char *H245_Protocol_Version[MAX_H323_VERSION + 1] = {
"0.0.8.245.0.0", // dummy, never used
"0.0.8.245.0.2",
"0.0.8.245.0.3",
"0.0.8.245.0.5",
"0.0.8.245.0.7",
"0.0.8.245.0.9",
"0.0.8.245.0.13",
"0.0.8.245.0.15"
};
#define UNDEFINED_PAYLOAD_TYPE 255
#define H46019_AUTO_DETECTION_WAIT 4000 // wait n millisec before doing H.460.19 port auto-detection
#define G722_1_OID "0.0.7.7221.1.0"
#define G722_1C_OID "0.0.7.7221.1.1.0"
#define G722_2_OID "0.0.7.7221.2.0"
namespace {
// default timeout (ms) for initial Setup message,
// if not specified in the config file
const long DEFAULT_SETUP_TIMEOUT = 8000;
// time to wait before deleting a closed socket
const long DEFAULT_SOCKET_CLEANUP_TIMEOUT = 5000;
// if socket bind fails, try next DEFAULT_NUM_SEQ_PORTS subsequent port numbers
const int DEFAULT_NUM_SEQ_PORTS = 500;
// maximum number of handler threads GnuGk will start (call signaling or RTP)
const unsigned MAX_HANDLER_NUMBER = 200;
const WORD RTP_BASE_HEADER_LEN = 12;
enum RTPSessionTypes { Unknown = 0, Audio, Video, Presentation, Data };
// RTCP functions used in UDPProxySocket and H46019Session
void ParseRTCP(const callptr & call, WORD sessionID, PIPSocket::Address fromIP, BYTE * wbuffer, WORD buflen);
void BuildReceiverReport(const callptr & call, WORD sessionID, const RTP_ControlFrame & frame, PINDEX offset, bool dst);
H245_H2250LogicalChannelParameters *GetLogicalChannelParameters(H245_OpenLogicalChannel & olc, bool & isReverseLC);
#ifdef HAS_H46018
bool odd(unsigned n) { return (n % 2) != 0; }
#endif
WORD RTP_HeaderSize(const BYTE * buffer, WORD len)
{
WORD header_size = RTP_BASE_HEADER_LEN;
// skip more if header has CSRC or extensions
bool has_extension = (((int)buffer[0] & 0x10) > 0);
int cc = (((int)buffer[0] & 0x0f) << 4);
if (cc)
header_size += (cc * 4);
if (has_extension && (len >= header_size + 4)) {
WORD extension_len = ((int)buffer[header_size+2] * 256) + (int)buffer[header_size+3] + 1;
header_size += (extension_len * 4);
}
//PTRACE(0, "JW extension=" << has_extension << " cc=" << cc << " header_size=" << header_size << " of len=" << len);
if (header_size > len) {
PTRACE(1, "RTP\tError: RTP packet shorter than header");
return min(len, RTP_BASE_HEADER_LEN);
}
return header_size;
}
template <class UUIE>
inline unsigned GetH225Version(const UUIE & uuie)
{
if (uuie.m_protocolIdentifier.GetSize() < 6)
return 0;
else
return uuie.m_protocolIdentifier[5];
}
H245_UnicastAddress *GetH245UnicastAddress(H245_TransportAddress & tsap)
{
if (tsap.GetTag() == H245_TransportAddress::e_unicastAddress) {
H245_UnicastAddress & uniaddr = tsap;
return &uniaddr;
}
return NULL;
}
inline H245_UnicastAddress & operator<<(H245_UnicastAddress & addr, const PIPSocket::Address & ip)
{
if (ip.GetVersion() == 6) {
addr.SetTag(H245_UnicastAddress::e_iP6Address);
H245_UnicastAddress_iP6Address & addrv6 = addr;
for (int i = 0; i < 16; ++i)
addrv6.m_network[i] = ip[i];
} else {
addr.SetTag(H245_UnicastAddress::e_iPAddress);
H245_UnicastAddress_iPAddress & addrv4 = addr;
for (int i = 0; i < 4; ++i)
addrv4.m_network[i] = ip[i];
}
return addr;
}
inline H245_UnicastAddress & operator<<(H245_UnicastAddress & addr, WORD port)
{
if (addr.GetTag() == H245_UnicastAddress::e_iPAddress) {
H245_UnicastAddress_iPAddress & addrv4 = addr;
addrv4.m_tsapIdentifier = port;
} else if (addr.GetTag() == H245_UnicastAddress::e_iP6Address) {
H245_UnicastAddress_iP6Address & addrv6 = addr;
addrv6.m_tsapIdentifier = port;
}
return addr;
}
inline const H245_UnicastAddress & operator>>(const H245_UnicastAddress & addr, PIPSocket::Address & ip)
{
if (addr.GetTag() == H245_UnicastAddress::e_iPAddress) {
const H245_UnicastAddress_iPAddress & addrv4 = addr;
ip = PIPSocket::Address(addrv4.m_network.GetSize(), addrv4.m_network);
} else if (addr.GetTag() == H245_UnicastAddress::e_iP6Address) {
const H245_UnicastAddress_iP6Address & addrv6 = addr;
ip = PIPSocket::Address(addrv6.m_network.GetSize(), addrv6.m_network);
}
return addr;
}
inline const H245_UnicastAddress & operator>>(const H245_UnicastAddress & addr, WORD & port)
{
if (addr.GetTag() == H245_UnicastAddress::e_iPAddress) {
const H245_UnicastAddress_iPAddress & addrv4 = addr;
port = (WORD)addrv4.m_tsapIdentifier;
} else if (addr.GetTag() == H245_UnicastAddress::e_iP6Address) {
const H245_UnicastAddress_iP6Address & addrv6 = addr;
port = (WORD)addrv6.m_tsapIdentifier;
}
return addr;
}
PString GetH245CodecName(const H245_AudioCapability & cap)
{
switch (cap.GetTag()) {
case H245_AudioCapability::e_g711Alaw64k:
return "G711A";
case H245_AudioCapability::e_g711Alaw56k:
return "G711A56";
case H245_AudioCapability::e_g711Ulaw64k:
return "G711U";
case H245_AudioCapability::e_g711Ulaw56k:
return "G711U56";
case H245_AudioCapability::e_g722_64k:
return "G72264";
case H245_AudioCapability::e_g722_56k:
return "G72256";
case H245_AudioCapability::e_g722_48k:
return "G72248";
case H245_AudioCapability::e_g7231:
return "G7231";
case H245_AudioCapability::e_g728:
return "G728";
case H245_AudioCapability::e_g729:
return "G729";
case H245_AudioCapability::e_g729AnnexA:
return "G729A";
case H245_AudioCapability::e_g729wAnnexB:
return "G729B";
case H245_AudioCapability::e_g729AnnexAwAnnexB:
return "G729AB";
case H245_AudioCapability::e_g7231AnnexCCapability:
return "G7231C";
case H245_AudioCapability::e_gsmFullRate:
return "GSMFR";
case H245_AudioCapability::e_gsmHalfRate:
return "GSMHR";
case H245_AudioCapability::e_gsmEnhancedFullRate:
return "GSMEFR";
case H245_AudioCapability::e_genericAudioCapability: {
const H245_GenericCapability & genericcap = cap;
if (genericcap.m_capabilityIdentifier.GetTag() == H245_CapabilityIdentifier::e_standard) {
const PASN_ObjectId & id = genericcap.m_capabilityIdentifier;
if (id == G722_1_OID)
return "G722.1";
if (id == G722_1C_OID)
return "G722.1C";
if (id == G722_2_OID)
return "G722.2";
}
return "GenericAudio";
}
}
return "Unknown";
}
PString GetH245CodecName(const H245_VideoCapability & cap)
{
switch (cap.GetTag()) {
case H245_VideoCapability::e_h261VideoCapability:
return "H.261";
case H245_VideoCapability::e_h263VideoCapability:
return "H.263";
case H245_VideoCapability::e_genericVideoCapability:
return "H.264";
case H245_VideoCapability::e_extendedVideoCapability:
return "H.239";
}
return "Unknown";
}
unsigned GetH245CodecBitrate(const H245_AudioCapability & cap)
{
switch (cap.GetTag()) {
case H245_AudioCapability::e_g711Alaw64k:
return 640;
case H245_AudioCapability::e_g711Alaw56k:
return 560;
case H245_AudioCapability::e_g711Ulaw64k:
return 640;
case H245_AudioCapability::e_g711Ulaw56k:
return 560;
case H245_AudioCapability::e_g722_64k:
return 640;
case H245_AudioCapability::e_g722_56k:
return 560;
case H245_AudioCapability::e_g722_48k:
return 480;
case H245_AudioCapability::e_g7231:
return 63;
case H245_AudioCapability::e_g728:
return 160;
case H245_AudioCapability::e_g729:
return 80;
case H245_AudioCapability::e_g729AnnexA:
return 80;
case H245_AudioCapability::e_g729wAnnexB:
return 80;
case H245_AudioCapability::e_g729AnnexAwAnnexB:
return 80;
case H245_AudioCapability::e_g7231AnnexCCapability:
return 63;
case H245_AudioCapability::e_gsmFullRate:
return 130;
case H245_AudioCapability::e_gsmHalfRate:
return 56;
case H245_AudioCapability::e_gsmEnhancedFullRate:
return 122;
case H245_AudioCapability::e_genericAudioCapability: {
const H245_GenericCapability & genericcap = cap;
if (genericcap.m_capabilityIdentifier.GetTag() == H245_CapabilityIdentifier::e_standard) {
const PASN_ObjectId & id = genericcap.m_capabilityIdentifier;
if (id == G722_1_OID)
return genericcap.m_maxBitRate / 100; // G.722.1 Annex A changes units to bit/s, not 100 bit/s !!!
}
return genericcap.m_maxBitRate;
}
}
return 0;
}
unsigned GetH245CodecBitrate(const H245_VideoCapability & cap)
{
switch (cap.GetTag()) {
case H245_VideoCapability::e_h261VideoCapability: {
const H245_H261VideoCapability & h261cap = cap;
return h261cap.m_maxBitRate;
}
case H245_VideoCapability::e_h263VideoCapability: {
const H245_H263VideoCapability & h263cap = cap;
return h263cap.m_maxBitRate;
}
case H245_VideoCapability::e_genericVideoCapability: {
const H245_GenericCapability & genericcap = cap;
return genericcap.m_maxBitRate;
}
}
return 0;
}
#ifdef HAS_H235_MEDIA
BYTE GetStaticAudioPayloadType(unsigned tag)
{
switch (tag) {
case H245_AudioCapability::e_g711Alaw64k:
case H245_AudioCapability::e_g711Alaw56k:
return 8;
case H245_AudioCapability::e_g711Ulaw64k:
case H245_AudioCapability::e_g711Ulaw56k:
return 0;
case H245_AudioCapability::e_g722_64k:
case H245_AudioCapability::e_g722_56k:
case H245_AudioCapability::e_g722_48k:
return 9;
case H245_AudioCapability::e_g7231:
case H245_AudioCapability::e_g7231AnnexCCapability:
return 4;
case H245_AudioCapability::e_g728:
return 15;
case H245_AudioCapability::e_g729:
case H245_AudioCapability::e_g729AnnexA:
case H245_AudioCapability::e_g729wAnnexB:
case H245_AudioCapability::e_g729AnnexAwAnnexB:
return 18;
case H245_AudioCapability::e_gsmFullRate:
case H245_AudioCapability::e_gsmHalfRate:
case H245_AudioCapability::e_gsmEnhancedFullRate:
return 3;
};
return UNDEFINED_PAYLOAD_TYPE;
};
BYTE GetStaticVideoPayloadType(unsigned tag)
{
switch (tag) {
case H245_VideoCapability::e_h261VideoCapability:
return 31;
case H245_VideoCapability::e_h263VideoCapability:
return 34;
};
return UNDEFINED_PAYLOAD_TYPE;
};
BYTE GetStaticPayloadType(const H245_DataType & type)
{
if (type.GetTag() == H245_DataType::e_audioData) {
const H245_AudioCapability & audioCap = type;
return GetStaticAudioPayloadType(audioCap.GetTag());
}
if (type.GetTag() == H245_DataType::e_videoData) {
const H245_VideoCapability & videoCap = type;
return GetStaticVideoPayloadType(videoCap.GetTag());
}
if (type.GetTag() == H245_DataType::e_h235Media) {
const H245_H235Media & h235data = type;
if (h235data.m_mediaType.GetTag() == H245_H235Media_mediaType::e_audioData) {
const H245_AudioCapability & audioCap = h235data.m_mediaType;
return GetStaticAudioPayloadType(audioCap.GetTag());
}
if (h235data.m_mediaType.GetTag() == H245_H235Media_mediaType::e_videoData) {
const H245_VideoCapability & videoCap = h235data.m_mediaType;
return GetStaticVideoPayloadType(videoCap.GetTag());
}
}
return UNDEFINED_PAYLOAD_TYPE;
}
// pick a random payload type, but not the old one or the plaintext PT
BYTE RandomPT(BYTE oldPT, BYTE plainPT)
{
BYTE newPT = oldPT;
while ((newPT == oldPT) || (newPT == plainPT)) {
newPT = PRandom::Number() % 254 + 1; // generate random in range [1-254]
}
return newPT;
}
bool IsOldH263(const H245_DataType & type)
{
if (type.GetTag() == H245_DataType::e_videoData) {
const H245_VideoCapability & videoCap = type;
if (videoCap.GetTag() == H245_VideoCapability::e_h263VideoCapability) {
const H245_H263VideoCapability & h263cap = videoCap;
return !h263cap.HasOptionalField(H245_H263VideoCapability::e_h263Options);
}
}
if (type.GetTag() == H245_DataType::e_h235Media) {
const H245_H235Media & h235data = type;
if (h235data.m_mediaType.GetTag() == H245_H235Media_mediaType::e_videoData) {
const H245_VideoCapability & videoCap = h235data.m_mediaType;
if (videoCap.GetTag() == H245_VideoCapability::e_h263VideoCapability) {
const H245_H263VideoCapability & h263cap = videoCap;
return !h263cap.HasOptionalField(H245_H263VideoCapability::e_h263Options);
}
}
}
return false;
}
#endif // HAS_H235_MEDIA
} // end of anonymous namespace
// send a UDP datagram and set the source IP (used only for RTP, RAS is using sockets bound to specific IPs)
// the method is highly OS specific
#if _WIN32 || _WIN64
ssize_t UDPSendWithSourceIP(int fd, void * data, size_t len, const IPAndPortAddress & toAddress, PIPSocket::Address * gkIP)
{
#ifdef hasIPV6
struct sockaddr_in6 dest;
#else
struct sockaddr_in dest;
#endif
// set dest address
PIPSocket::Address toIP;
WORD toPort = 0;
if (!IsSet(toAddress) || ! toAddress.GetIpAndPort(toIP, toPort)) {
PTRACE(5, "RTP\tSend error, toAddress not set");
return -1;
}
SetSockaddr(dest, toIP, toPort);
#if (_WIN32_WINNT >= WINDOWS_VISTA)
if (g_pfWSASendMsg && !g_disableSettingUDPSourceIP) {
// set source address
PIPSocket::Address src;
if (gkIP) {
src = * gkIP; // if we have a specific GK IP to use, use that
} else {
src = RasServer::Instance()->GetLocalAddress(toIP); // pick a source IP from gatekeeper interfaces
}
WSABUF wsabuf;
wsabuf.buf = (CHAR *)data;
wsabuf.len = len;
WSAMSG msg;
memset(&msg, 0, sizeof(msg));
msg.name = (struct sockaddr*)&dest;
size_t addr_len = sizeof(sockaddr_in);
#ifdef hasIPV6
if (toIP.GetVersion() == 6)
addr_len = sizeof(sockaddr_in6);
#endif // hasIPV6
msg.namelen = addr_len;
msg.lpBuffers = &wsabuf;
msg.dwBufferCount = 1;
WSACMSGHDR * cm = NULL;
char cmsg[WSA_CMSG_SPACE(sizeof(struct in_pktinfo))];
unsigned cmsg_data_size = sizeof(struct in_pktinfo);
memset(cmsg, 0, sizeof(cmsg));
msg.Control.buf = cmsg;
msg.Control.len = sizeof(cmsg);
if (!Toolkit::Instance()->IsIPv6Enabled() || (((struct sockaddr*)&dest)->sa_family == AF_INET)) {
// set IPv4 source
cm = WSA_CMSG_FIRSTHDR(&msg);
cm->cmsg_len = WSA_CMSG_LEN(cmsg_data_size);
cm->cmsg_level = IPPROTO_IP;
cm->cmsg_type = IP_PKTINFO;
{
struct in_pktinfo ipi = { 0 };
ipi.ipi_addr.s_addr = src;
memcpy(WSA_CMSG_DATA(cm), &ipi, sizeof(ipi));
}
}
#ifdef hasIPV6
if (Toolkit::Instance()->IsIPv6Enabled()) {
// set IPv6 source with IPV6_PKTINFO
WSACMSGHDR * cm = NULL;
char cmsg6[WSA_CMSG_SPACE(sizeof(struct in6_pktinfo))];
unsigned cmsg6_data_size = sizeof(struct in6_pktinfo);
memset(cmsg6, 0, sizeof(cmsg6));
msg.Control.buf = cmsg6;
msg.Control.len = sizeof(cmsg6);
cm = WSA_CMSG_FIRSTHDR(&msg);
cm->cmsg_len = WSA_CMSG_LEN(cmsg6_data_size);
cm->cmsg_level = IPPROTO_IPV6;
cm->cmsg_type = IPV6_PKTINFO;
{
struct in6_pktinfo ipi = { 0 };
struct sockaddr_in6 s6;
SetSockaddr(s6, src, 0);
ipi.ipi6_addr = s6.sin6_addr;
memcpy(WSA_CMSG_DATA(cm), &ipi, sizeof(ipi));
}
}
#endif // hasIPV6
DWORD bytesSent = 0;
int rc = g_pfWSASendMsg(fd, &msg, 0, &bytesSent, NULL, NULL);
if (rc == 0) {
return bytesSent;
} else {
int err = WSAGetLastError();
PTRACE(7, "RTP\tSend error " << err);
return -1;
}
} else
#endif // WINDOWS_VISTA
{
// on XP we fall back to sendto()
// we can't set the source addr and XP seems to have trouble with IPv6, but IPv4 works OK
return ::sendto(fd, (const char *)data, len, 0, (struct sockaddr *)&dest, sizeof(dest));
}
}
#else // Unix
ssize_t UDPSendWithSourceIP(int fd, void * data, size_t len, const IPAndPortAddress & toAddress, PIPSocket::Address * gkIP)
{
#ifdef hasIPV6
struct sockaddr_in6 dest;
#else
struct sockaddr_in dest;
#endif
// set dest address
PIPSocket::Address toIP;
WORD toPort = 0;
if (!IsSet(toAddress) || !toAddress.GetIpAndPort(toIP, toPort)) {
PTRACE(5, "RTP\tSend error, toAddress not set");
return -1;
}
SetSockaddr(dest, toIP, toPort);
if (g_disableSettingUDPSourceIP) {
size_t addr_len = sizeof(sockaddr_in);
#ifdef hasIPV6
if (toIP.GetVersion() == 6)
addr_len = sizeof(sockaddr_in6);
#endif // hasIPV6
ssize_t bytesSent = sendto(fd, (char *)data, len, 0, (struct sockaddr*)&dest, addr_len);
if (bytesSent < 0) {
PTRACE(5, "RTP\tSend error " << strerror(errno));
}
return bytesSent;
}
struct msghdr msgh;
struct cmsghdr *cmsg;
struct iovec iov = { };
char cbuf[256];
memset(&cbuf, 0, sizeof(cbuf)); // zero the buffer to shut up Valgrind
// Set up iov and msgh structures
memset(&msgh, 0, sizeof(struct msghdr));
iov.iov_base = data;
iov.iov_len = len;
msgh.msg_iov = &iov;
msgh.msg_iovlen = 1;
msgh.msg_name = (struct sockaddr*)&dest;
// must pass short len when sending to IPv4 address on Solaris 11, OpenBSD and NetBSD
// sizeof(dest) is OK on Linux and FreeBSD
size_t addr_len = sizeof(sockaddr_in);
#ifdef hasIPV6
if (toIP.GetVersion() == 6)
addr_len = sizeof(sockaddr_in6);
#endif // hasIPV6
msgh.msg_namelen = addr_len;
// set source address
PIPSocket::Address src;
if (gkIP) {
src = * gkIP; // if we have a specific GK IP to use, use that
} else {
src = RasServer::Instance()->GetLocalAddress(toIP); // pick a source IP from gatekeeper interfaces
}
#ifdef hasIPV6
if (Toolkit::Instance()->IsIPv6Enabled() && (src.GetVersion() == 6)) {
struct in6_pktinfo *pkt;
msgh.msg_control = cbuf;
msgh.msg_controllen = CMSG_SPACE(sizeof(*pkt));
cmsg = CMSG_FIRSTHDR(&msgh);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
pkt = (struct in6_pktinfo *) CMSG_DATA(cmsg);
memset(pkt, 0, sizeof(*pkt));
pkt->ipi6_addr = src;
msgh.msg_controllen = cmsg->cmsg_len;
} else
#endif // hasIPV6
{
#if defined(IP_PKTINFO) && !defined(P_NETBSD) // Linux and Solaris 11 (NetBSD 7 only has incomplete IP_PKTINFO support)
struct in_pktinfo *pkt;
msgh.msg_control = cbuf;
msgh.msg_controllen = CMSG_SPACE(sizeof(*pkt));
cmsg = CMSG_FIRSTHDR(&msgh);
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
pkt = (struct in_pktinfo *) CMSG_DATA(cmsg);
memset(pkt, 0, sizeof(*pkt));
pkt->ipi_spec_dst = src;
#else
#ifdef IP_SENDSRCADDR // FreeBSD
// FreeBSD doesn't allow IP_SENDSRCADDR on sockets bound to anything else than INADDR_ANY
bool skipSENDSRCADDR = false;
struct sockaddr name;
socklen_t name_len = sizeof(name);
if (getsockname(fd, &name, &name_len) >= 0) {
if (name.sa_family == AF_INET6) {
if (!IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)&name)->sin6_addr)) {
skipSENDSRCADDR = true;
}
} else {
if (((struct sockaddr_in *)&name)->sin_addr.s_addr != INADDR_ANY) {
skipSENDSRCADDR = true;
}
}
}
if (skipSENDSRCADDR) {
PTRACE(7, "JW RTP: Skipping IP_SENDSRCADDR on this socket");
} else {
struct in_addr *in;
msgh.msg_control = cbuf;
msgh.msg_controllen = CMSG_SPACE(sizeof(*in));
cmsg = CMSG_FIRSTHDR(&msgh);
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_SENDSRCADDR;
cmsg->cmsg_len = CMSG_LEN(sizeof(*in));
in = (struct in_addr *) CMSG_DATA(cmsg);
*in = src;
}
#endif // IP_SENDSRCADDR
#endif // IP_PKTINFO else
}
ssize_t bytesSent = sendmsg(fd, &msgh, 0);
if (bytesSent < 0) {
PTRACE(5, "RTP\tSend error: " << strerror(errno));
}
return bytesSent;
}
#endif
ssize_t UDPSendWithSourceIP(int fd, void * data, size_t len, const PIPSocket::Address & ip, WORD port, PIPSocket::Address * gkIP)
{
const IPAndPortAddress to(ip, port);
return UDPSendWithSourceIP(fd, data, len, to, gkIP);
}
#ifdef HAS_H46018
void RemoveH46019Descriptor(H225_ArrayOf_FeatureDescriptor & supportedFeatures, bool & senderSupportsH46019Multiplexing, bool & isH46019Client)
{
for (PINDEX i = 0; i < supportedFeatures.GetSize(); i++) {
H225_GenericIdentifier & id = supportedFeatures[i].m_id;
if (id.GetTag() == H225_GenericIdentifier::e_standard) {
PASN_Integer & asnInt = id;
if (asnInt.GetValue() == 19) {
senderSupportsH46019Multiplexing = false;
isH46019Client = true;
for (PINDEX p = 0; p < supportedFeatures[i].m_parameters.GetSize(); p++) {
if (supportedFeatures[i].m_parameters[p].m_id.GetTag() == H225_GenericIdentifier::e_standard) {
PASN_Integer & pInt = supportedFeatures[i].m_parameters[p].m_id;
if (pInt == 1) {
senderSupportsH46019Multiplexing = true;
}
if (pInt == 2) {
isH46019Client = false;
}
}
}
// delete, move others 1 up
for (PINDEX j = i + 1; j < supportedFeatures.GetSize(); j++) {
supportedFeatures[j - 1] = supportedFeatures[j];
}
supportedFeatures.SetSize(supportedFeatures.GetSize() - 1);
return;
}
}
}
}
#endif
#ifdef HAS_H46023
// Fix for H323plus 1.25 and prior with H.460.24 Multiplexing parameter being sent in the wrong field
// in the Alerting and Connect message
bool FixH46024Multiplexing(const H225_ArrayOf_GenericData & data, H225_FeatureSet & features)
{
for (PINDEX i = 0; i < data.GetSize(); i++) {
H225_GenericIdentifier & id = data[i].m_id;
if (id.GetTag() == H225_GenericIdentifier::e_standard) {
PASN_Integer & asnInt = id;
if (asnInt.GetValue() == 19) {
features.IncludeOptionalField(H225_FeatureSet::e_supportedFeatures);
H225_ArrayOf_FeatureDescriptor & supportedFeatures = features.m_supportedFeatures;
int sz = supportedFeatures.GetSize();
supportedFeatures.SetSize(sz + 1);
supportedFeatures[sz] = (const H225_FeatureDescriptor &)data[i];
PTRACE(4, "H46023\tCorrecting message in generic field");
return true;
}
}
}
return false;
}
bool HasH46024Descriptor(H225_ArrayOf_FeatureDescriptor & supportedFeatures)
{
bool found = false;
bool senderSupportsH46019Multiplexing = false;
for (PINDEX i = 0; i < supportedFeatures.GetSize(); i++) {
H225_GenericIdentifier & id = supportedFeatures[i].m_id;
if (id.GetTag() == H225_GenericIdentifier::e_standard) {
PASN_Integer & asnInt = id;
if (asnInt.GetValue() == 24)
found = true;
}
}
if (!found)
return false;
// if we are not interzone Multiplexing or don't support it remove the .19 Feature if it is there
for (PINDEX i = 0; i < supportedFeatures.GetSize(); i++) {
H225_GenericIdentifier & id = supportedFeatures[i].m_id;
if (id.GetTag() == H225_GenericIdentifier::e_standard) {
PASN_Integer & asnInt = id;
if (asnInt.GetValue() == 19) {
for (PINDEX p = 0; p < supportedFeatures[i].m_parameters.GetSize(); p++) {
if (supportedFeatures[i].m_parameters[p].m_id.GetTag() == H225_GenericIdentifier::e_standard) {
PASN_Integer & pInt = supportedFeatures[i].m_parameters[p].m_id;
if (pInt == 1)
senderSupportsH46019Multiplexing = true;
}
}
if (!senderSupportsH46019Multiplexing ||
!Toolkit::AsBool(GkConfig()->GetString(ProxySection, "RTPMultiplexing", "0"))) {
for (PINDEX j = i + 1; j < supportedFeatures.GetSize(); j++) {
supportedFeatures[j - 1] = supportedFeatures[j];
}
supportedFeatures.SetSize(supportedFeatures.GetSize() - 1);
}
}
}
}
return found;
}
bool IsH46024ProxyStrategy(CallRec::NatStrategy strat)
{
if (strat == CallRec::e_natUnknown
|| strat == CallRec::e_natLocalProxy
|| strat == CallRec::e_natFullProxy)
return true;
return false;
}
#endif
#ifdef HAS_H46024A
bool GetH245GenericStringOctetString(unsigned id, const H245_ArrayOf_GenericParameter & params, PString & str)
{
for (PINDEX i = 0; i < params.GetSize(); i++) {
const H245_GenericParameter & param = params[i];
const H245_ParameterIdentifier & idm = param.m_parameterIdentifier;
if (idm.GetTag() == H245_ParameterIdentifier::e_standard) {
const PASN_Integer & idx = idm;
if (idx == id) {
const H245_ParameterValue & genvalue = params[i].m_parameterValue;
if (genvalue.GetTag() == H245_ParameterValue::e_octetString) {
const PASN_OctetString & valg = genvalue;
PASN_IA5String data;
if (valg.DecodeSubType(data)) {
str = data;
return true;
}
}
}
}
}
PTRACE(4, "H46024A\tError finding String parameter " << id);
return false;
}
bool GetH245GenericUnsigned(unsigned id, const H245_ArrayOf_GenericParameter & params, unsigned & num)
{
for (PINDEX i = 0; i < params.GetSize(); i++) {
const H245_GenericParameter & param = params[i];
const H245_ParameterIdentifier & idm = param.m_parameterIdentifier;
if (idm.GetTag() == H245_ParameterIdentifier::e_standard) {
const PASN_Integer & idx = idm;
if (idx == id) {
const H245_ParameterValue & genvalue = params[i].m_parameterValue;
if (genvalue.GetTag() == H245_ParameterValue::e_unsigned32Min) {
const PASN_Integer & valg = genvalue;
num = valg;
return true;
}
}
}
}
PTRACE(4, "H46024A\tError finding unsigned parameter " << id);
return false;
}
bool GetH245TransportGenericOctetString(unsigned id, const H245_ArrayOf_GenericParameter & params, H323TransportAddress & str)
{
for (PINDEX i = 0; i < params.GetSize(); i++) {
const H245_GenericParameter & param = params[i];
const H245_ParameterIdentifier & idm = param.m_parameterIdentifier;
if (idm.GetTag() == H245_ParameterIdentifier::e_standard) {
const PASN_Integer & idx = idm;
if (idx == id) {
const H245_ParameterValue & genvalue = params[i].m_parameterValue;
if (genvalue.GetTag() == H245_ParameterValue::e_octetString) {
const PASN_OctetString & valg = genvalue;
H245_TransportAddress addr;
if (valg.DecodeSubType(addr)) {
str = H323TransportAddress(addr);
return true;
}
}
}
}
}
return false;
}
H245_GenericParameter & BuildH245GenericOctetString(H245_GenericParameter & param, unsigned id, const PASN_Object & data)
{
H245_ParameterIdentifier & idm = param.m_parameterIdentifier;
idm.SetTag(H245_ParameterIdentifier::e_standard);
PASN_Integer & idx = idm;
idx = id;
H245_ParameterValue & genvalue = param.m_parameterValue;
genvalue.SetTag(H245_ParameterValue::e_octetString);
PASN_OctetString & valg = genvalue;
valg.EncodeSubType(data);
return param;
}
H245_GenericParameter & BuildH245GenericOctetString(H245_GenericParameter & param, unsigned id, const H323TransportAddress & transport)
{
H245_TransportAddress data;
transport.SetPDU(data);
return BuildH245GenericOctetString(param, id, data);
}
H245_GenericParameter & BuildH245GenericUnsigned(H245_GenericParameter & param, unsigned id, unsigned val)
{
H245_ParameterIdentifier & idm = param.m_parameterIdentifier;
idm.SetTag(H245_ParameterIdentifier::e_standard);
PASN_Integer & idx = idm;
idx = id;
H245_ParameterValue & genvalue = param.m_parameterValue;
genvalue.SetTag(H245_ParameterValue::e_unsigned32Min);
PASN_Integer & xval = genvalue;
xval = val;
return param;
}
#endif
struct PortRange {
PortRange() : port(0), minport(0), maxport(0) { }
WORD GetPort();
int GetNumPorts() const;
void LoadConfig(const char *, const char *, const char * = "");
private:
PortRange(const PortRange &);
PortRange & operator=(const PortRange &);
private:
WORD port, minport, maxport;
PMutex mutex;
};
WORD PortRange::GetPort()
{
if (port == 0)
return 0;
PWaitAndSignal lock(mutex);
WORD result = port++;
if (port > maxport)
port = minport;
if (port < minport) // special case to check for 16-bit wrap around
port = minport;
if (port == 0)
port = 1;
return result;
}
int PortRange::GetNumPorts() const
{
return maxport - minport + 1;
}
void PortRange::LoadConfig(const char *sec, const char *setting, const char *def)
{
PStringArray cfgs = GkConfig()->GetString(sec, setting, def).Tokenise(",.:-/'", FALSE);
if (cfgs.GetSize() >= 2) {
minport = (WORD)cfgs[0].AsUnsigned(), maxport = (WORD)cfgs[1].AsUnsigned();
if (port < minport || port > maxport)
port = minport;
} else
port = 0;
PTRACE_IF(2, port, setting << ": " << minport << '-' << maxport);
}
static PortRange Q931PortRange;
static PortRange H245PortRange;
static PortRange T120PortRange;
static PortRange RTPPortRange;
class H245Socket : public TCPProxySocket {
public: