-
Notifications
You must be signed in to change notification settings - Fork 409
/
SIPHeader.cs
2410 lines (2167 loc) · 101 KB
/
SIPHeader.cs
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
//-----------------------------------------------------------------------------
// Filename: SIPHeader.cs
//
// Description: SIP Header.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 17 Sep 2005 Aaron Clauson Created, Dublin, Ireland.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using SIPSorcery.Sys;
namespace SIPSorcery.SIP
{
/// <summary>
/// The Via header only has parameters, no headers. Parameters of from ...;name=value;name2=value2
/// Specific parameters: ttl, maddr, received, branch.
///
/// From page 179 of RFC3261:
/// "Even though this specification mandates that the branch parameter be
/// present in all requests, the BNF for the header field indicates that
/// it is optional."
///
/// The branch parameter on a Via therefore appears to be optionally mandatory?!
///
/// Any SIP application element that uses transactions depends on the branch parameter for transaction matching.
/// Only the top Via header branch is used for transactions though so if the request has made it to this stack
/// with missing branches then in theory it should be safe to proceed. It will be left up to the SIPTransaction
/// class to reject any SIP requests that are missing the necessary branch.
/// </summary>
public class SIPViaHeader
{
private static char m_paramDelimChar = ';';
private static char m_hostDelimChar = ':';
private static string m_receivedKey = SIPHeaderAncillary.SIP_HEADERANC_RECEIVED;
private static string m_rportKey = SIPHeaderAncillary.SIP_HEADERANC_RPORT;
private static string m_branchKey = SIPHeaderAncillary.SIP_HEADERANC_BRANCH;
/// <summary>
/// Special SIP Via header that is recognised by the SIP transport classes Send methods. At send time this header will be replaced by
/// one with IP end point details that reflect the socket the request or response was sent from.
/// </summary>
public static SIPViaHeader GetDefaultSIPViaHeader(SIPProtocolsEnum protocol = SIPProtocolsEnum.udp)
{
return new SIPViaHeader(new IPEndPoint(IPAddress.Any, 0), CallProperties.CreateBranchId(), protocol);
}
public string Version;
public SIPProtocolsEnum Transport;
public string Host;
public int Port = 0;
public string Branch
{
get
{
if (ViaParameters != null && ViaParameters.Has(m_branchKey))
{
return ViaParameters.Get(m_branchKey);
}
else
{
return null;
}
}
set { ViaParameters.Set(m_branchKey, value); }
}
public string ReceivedFromIPAddress // IP Address contained in the received parameter.
{
get
{
if (ViaParameters != null && ViaParameters.Has(m_receivedKey))
{
return ViaParameters.Get(m_receivedKey);
}
else
{
return null;
}
}
set { ViaParameters.Set(m_receivedKey, value); }
}
public int ReceivedFromPort // Port contained in the rport parameter.
{
get
{
if (ViaParameters != null && ViaParameters.Has(m_rportKey))
{
return Convert.ToInt32(ViaParameters.Get(m_rportKey));
}
else
{
return 0;
}
}
set { ViaParameters.Set(m_rportKey, value.ToString()); }
}
public SIPParameters ViaParameters = new SIPParameters(null, m_paramDelimChar);
public string ContactAddress // This the address placed into the Via header by the User Agent.
{
get
{
if (IPSocket.TryParseIPEndPoint(Host, out var ipEndPoint))
{
if (ipEndPoint.Port == 0)
{
if (Port != 0)
{
ipEndPoint.Port = Port;
return ipEndPoint.ToString();
}
else
{
if (ipEndPoint.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
{
return "[" + ipEndPoint.Address.ToString() + "]";
}
else
{
return ipEndPoint.Address.ToString();
}
}
}
else
{
return ipEndPoint.ToString();
}
}
else if (Port != 0)
{
return Host + ":" + Port;
}
else
{
return Host;
}
}
}
public string ReceivedFromAddress // This is the socket the request was received on and is a combination of the Host and Received fields.
{
get
{
if (ReceivedFromIPAddress != null && ReceivedFromPort != 0)
{
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ReceivedFromIPAddress), ReceivedFromPort);
return ep.ToString();
}
else if (ReceivedFromIPAddress != null && Port != 0)
{
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ReceivedFromIPAddress), Port);
return ep.ToString();
}
else if (ReceivedFromIPAddress != null)
{
return ReceivedFromIPAddress;
}
else if (ReceivedFromPort != 0)
{
if (IPAddress.TryParse(Host, out IPAddress hostip))
{
IPEndPoint ep = new IPEndPoint(hostip, ReceivedFromPort);
return ep.ToString();
}
else
{
return Host + ":" + ReceivedFromPort;
}
}
else if (Port != 0)
{
if (IPAddress.TryParse(Host, out IPAddress hostip))
{
IPEndPoint ep = new IPEndPoint(hostip, Port);
return ep.ToString();
}
else
{
return Host + ":" + Port;
}
}
else
{
return Host;
}
}
}
public SIPViaHeader()
{ }
public SIPViaHeader(string contactIPAddress, int contactPort, string branch, SIPProtocolsEnum protocol)
{
Version = SIPConstants.SIP_FULLVERSION_STRING;
Transport = protocol;
Host = contactIPAddress;
Port = contactPort;
Branch = branch;
ViaParameters.Set(m_rportKey, null);
}
public SIPViaHeader(string contactIPAddress, int contactPort, string branch) :
this(contactIPAddress, contactPort, branch, SIPProtocolsEnum.udp)
{ }
public SIPViaHeader(IPEndPoint contactEndPoint, string branch) :
this(contactEndPoint.Address.ToString(), contactEndPoint.Port, branch, SIPProtocolsEnum.udp)
{ }
public SIPViaHeader(SIPEndPoint localEndPoint, string branch) :
this(localEndPoint.GetIPEndPoint(), branch, localEndPoint.Protocol)
{ }
public SIPViaHeader(string contactEndPoint, string branch) :
this(IPSocket.ParseSocketString(contactEndPoint), branch, SIPProtocolsEnum.udp)
{ }
public SIPViaHeader(IPEndPoint contactEndPoint, string branch, SIPProtocolsEnum protocol) :
this(contactEndPoint.Address.ToString(), contactEndPoint.Port, branch, protocol)
{ }
public static SIPViaHeader[] ParseSIPViaHeader(string viaHeaderStr)
{
List<SIPViaHeader> viaHeadersList = new List<SIPViaHeader>();
if (!viaHeaderStr.IsNullOrBlank())
{
viaHeaderStr = viaHeaderStr.Trim();
// Multiple Via headers can be contained in a single line by separating them with a comma.
string[] viaHeaders = SIPParameters.GetKeyValuePairsFromQuoted(viaHeaderStr, ',');
foreach (string viaHeaderStrItem in viaHeaders)
{
if (viaHeaderStrItem == null || viaHeaderStrItem.Trim().Length == 0)
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "No Contact address.");
}
else
{
SIPViaHeader viaHeader = new SIPViaHeader();
string header = viaHeaderStrItem.Trim();
int firstSpacePosn = header.IndexOf(" ");
if (firstSpacePosn == -1)
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "No Contact address.");
}
else
{
string versionAndTransport = header.Substring(0, firstSpacePosn);
viaHeader.Version = versionAndTransport.Substring(0, versionAndTransport.LastIndexOf('/'));
viaHeader.Transport = SIPProtocolsType.GetProtocolType(versionAndTransport.Substring(versionAndTransport.LastIndexOf('/') + 1));
string nextField = header.Substring(firstSpacePosn, header.Length - firstSpacePosn).Trim();
int delimIndex = nextField.IndexOf(';');
string contactAddress = null;
// Some user agents include branch but have the semi-colon missing, that's easy to cope with by replacing "branch" with ";branch".
if (delimIndex == -1 && nextField.Contains(m_branchKey))
{
nextField = nextField.Replace(m_branchKey, ";" + m_branchKey);
delimIndex = nextField.IndexOf(';');
}
if (delimIndex == -1)
{
//logger.LogWarning("Via header missing semi-colon: " + header + ".");
//parserError = SIPValidationError.NoBranchOnVia;
//return null;
contactAddress = nextField.Trim();
}
else
{
contactAddress = nextField.Substring(0, delimIndex).Trim();
viaHeader.ViaParameters = new SIPParameters(nextField.Substring(delimIndex, nextField.Length - delimIndex), m_paramDelimChar);
}
if (contactAddress == null || contactAddress.Trim().Length == 0)
{
// Check that the branch parameter is present, without it the Via header is illegal.
//if (!viaHeader.ViaParameters.Has(m_branchKey))
//{
// logger.LogWarning("Via header missing branch: " + header + ".");
// parserError = SIPValidationError.NoBranchOnVia;
// return null;
//}
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "No Contact address.");
}
// Parse the contact address.
if (IPSocket.TryParseIPEndPoint(contactAddress, out var ipEndPoint))
{
viaHeader.Host = ipEndPoint.Address.ToString();
if (ipEndPoint.Port != 0)
{
viaHeader.Port = ipEndPoint.Port;
}
}
else
{
// Now parsing non IP address contact addresses.
int colonIndex = contactAddress.IndexOf(m_hostDelimChar);
if (colonIndex != -1)
{
viaHeader.Host = contactAddress.Substring(0, colonIndex);
if (!Int32.TryParse(contactAddress.Substring(colonIndex + 1), out viaHeader.Port))
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "Non-numeric port for IP address.");
}
else if (viaHeader.Port > IPEndPoint.MaxPort)
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "The port specified in a Via header exceeded the maximum allowed.");
}
}
else
{
viaHeader.Host = contactAddress;
}
}
viaHeadersList.Add(viaHeader);
}
}
}
}
if (viaHeadersList.Count > 0)
{
return viaHeadersList.ToArray();
}
else
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "Via list was empty.");
}
}
public new string ToString()
{
string sipViaHeader = SIPHeaders.SIP_HEADER_VIA + ": " + this.Version + "/" + this.Transport.ToString().ToUpper() + " " + ContactAddress;
sipViaHeader += (ViaParameters != null && ViaParameters.Count > 0) ? ViaParameters.ToString() : null;
return sipViaHeader;
}
}
/// <bnf>
/// From = ( "From" / "f" ) HCOLON from-spec
/// from-spec = ( name-addr / addr-spec ) *( SEMI from-param )
/// from-param = tag-param / generic-param
/// name-addr = [ display-name ] LAQUOT addr-spec RAQUOT
/// addr-spec = SIP-URI / SIPS-URI / absoluteURI
/// tag-param = "tag" EQUAL token
/// generic-param = token [ EQUAL gen-value ]
/// gen-value = token / host / quoted-string
/// </bnf>
/// <remarks>
/// The From header only has parameters, no headers. Parameters of from ...;name=value;name2=value2.
/// Specific parameters: tag.
/// </remarks>
public class SIPFromHeader
{
//public const string DEFAULT_FROM_NAME = SIPConstants.SIP_DEFAULT_USERNAME;
public const string DEFAULT_FROM_URI = SIPConstants.SIP_DEFAULT_FROMURI;
public const string PARAMETER_TAG = SIPHeaderAncillary.SIP_HEADERANC_TAG;
/// <summary>
/// Special SIP From header that is recognised by the SIP transport classes Send methods. At send time this header will be replaced by
/// one with IP end point details that reflect the socket the request or response was sent from.
/// </summary>
public static SIPFromHeader GetDefaultSIPFromHeader(SIPSchemesEnum scheme)
{
return new SIPFromHeader(null, new SIPURI(scheme, IPAddress.Any, 0), CallProperties.CreateNewTag());
}
public string FromName
{
get { return m_userField.Name; }
set { m_userField.Name = value; }
}
public SIPURI FromURI
{
get { return m_userField.URI; }
set { m_userField.URI = value; }
}
public string FromTag
{
get { return FromParameters.Get(PARAMETER_TAG); }
set
{
if (value != null && value.Trim().Length > 0)
{
FromParameters.Set(PARAMETER_TAG, value);
}
else
{
if (FromParameters.Has(PARAMETER_TAG))
{
FromParameters.Remove(PARAMETER_TAG);
}
}
}
}
public SIPParameters FromParameters
{
get { return m_userField.Parameters; }
set { m_userField.Parameters = value; }
}
private SIPUserField m_userField = new SIPUserField();
public SIPUserField FromUserField
{
get { return m_userField; }
set { m_userField = value; }
}
private SIPFromHeader()
{ }
public SIPFromHeader(string fromName, SIPURI fromURI, string fromTag)
{
m_userField = new SIPUserField(fromName, fromURI, null);
FromTag = fromTag;
}
public static SIPFromHeader ParseFromHeader(string fromHeaderStr)
{
try
{
SIPFromHeader fromHeader = new SIPFromHeader();
fromHeader.m_userField = SIPUserField.ParseSIPUserField(fromHeaderStr);
return fromHeader;
}
catch (ArgumentException argExcp)
{
throw new SIPValidationException(SIPValidationFieldsEnum.FromHeader, argExcp.Message);
}
catch
{
throw new SIPValidationException(SIPValidationFieldsEnum.FromHeader, "The SIP From header was invalid.");
}
}
public override string ToString()
{
return m_userField.ToString();
}
/// <summary>
/// Returns a friendly description of the caller that's suitable for humans. Leaves out
/// all the parameters etc.
/// </summary>
/// <returns>A string representing a friendly description of the From header.</returns>
public string FriendlyDescription()
{
string caller = FromURI.ToAOR();
caller = (!string.IsNullOrEmpty(FromName)) ? FromName + " " + caller : caller;
return caller;
}
}
/// <bnf>
/// To = ( "To" / "t" ) HCOLON ( name-addr / addr-spec ) *( SEMI to-param )
/// to-param = tag-param / generic-param
/// name-addr = [ display-name ] LAQUOT addr-spec RAQUOT
/// addr-spec = SIP-URI / SIPS-URI / absoluteURI
/// tag-param = "tag" EQUAL token
/// generic-param = token [ EQUAL gen-value ]
/// gen-value = token / host / quoted-string
/// </bnf>
/// <remarks>
/// The To header only has parameters, no headers. Parameters of from ...;name=value;name2=value2.
/// Specific parameters: tag.
/// </remarks>
public class SIPToHeader
{
public const string PARAMETER_TAG = SIPHeaderAncillary.SIP_HEADERANC_TAG;
public string ToName
{
get { return m_userField.Name; }
set { m_userField.Name = value; }
}
public SIPURI ToURI
{
get { return m_userField.URI; }
set { m_userField.URI = value; }
}
public string ToTag
{
get { return ToParameters.Get(PARAMETER_TAG); }
set
{
if (value != null && value.Trim().Length > 0)
{
ToParameters.Set(PARAMETER_TAG, value);
}
else
{
if (ToParameters.Has(PARAMETER_TAG))
{
ToParameters.Remove(PARAMETER_TAG);
}
}
}
}
public SIPParameters ToParameters
{
get { return m_userField.Parameters; }
set { m_userField.Parameters = value; }
}
private SIPUserField m_userField;
public SIPUserField ToUserField
{
get { return m_userField; }
set { m_userField = value; }
}
private SIPToHeader()
{ }
public SIPToHeader(string toName, SIPURI toURI, string toTag)
{
m_userField = new SIPUserField(toName, toURI, null);
ToTag = toTag;
}
public static SIPToHeader ParseToHeader(string toHeaderStr)
{
try
{
SIPToHeader toHeader = new SIPToHeader();
toHeader.m_userField = SIPUserField.ParseSIPUserField(toHeaderStr);
return toHeader;
}
catch (ArgumentException argExcp)
{
throw new SIPValidationException(SIPValidationFieldsEnum.ToHeader, argExcp.Message);
}
catch
{
throw new SIPValidationException(SIPValidationFieldsEnum.ToHeader, "The SIP To header was invalid.");
}
}
public override string ToString()
{
return m_userField.ToString();
}
}
/// <bnf>
/// Contact = ("Contact" / "m" ) HCOLON ( STAR / (contact-param *(COMMA contact-param)))
/// contact-param = (name-addr / addr-spec) *(SEMI contact-params)
/// name-addr = [ display-name ] LAQUOT addr-spec RAQUOT
/// addr-spec = SIP-URI / SIPS-URI / absoluteURI
/// display-name = *(token LWS)/ quoted-string
///
/// contact-params = c-p-q / c-p-expires / contact-extension
/// c-p-q = "q" EQUAL qvalue
/// c-p-expires = "expires" EQUAL delta-seconds
/// contact-extension = generic-param
/// delta-seconds = 1*DIGIT
/// generic-param = token [ EQUAL gen-value ]
/// gen-value = token / host / quoted-string
/// </bnf>
/// <remarks>
/// The Contact header only has parameters, no headers. Parameters of from ...;name=value;name2=value2
/// Specific parameters: q, expires.
/// </remarks>
[DataContract]
public class SIPContactHeader
{
public const string EXPIRES_PARAMETER_KEY = "expires";
public const string QVALUE_PARAMETER_KEY = "q";
//private static char[] m_nonStandardURIDelimChars = new char[] { '\n', '\r', ' ' }; // Characters that can delimit a SIP URI, supposed to be > but it is sometimes missing.
/// <summary>
/// Special SIP contact header that is recognised by the SIP transport classes Send methods. At send time this header will be replaced by
/// one with IP end point details that reflect the socket the request or response was sent from.
/// </summary>
public static SIPContactHeader GetDefaultSIPContactHeader(SIPSchemesEnum scheme)
{
return new SIPContactHeader(null, new SIPURI(scheme, IPAddress.Any, 0));
}
public string RawHeader;
public string ContactName
{
get { return m_userField.Name; }
set { m_userField.Name = value; }
}
public SIPURI ContactURI
{
get { return m_userField.URI; }
set { m_userField.URI = value; }
}
public SIPParameters ContactParameters
{
get { return m_userField.Parameters; }
set { m_userField.Parameters = value; }
}
// A value of -1 indicates the header did not contain an expires parameter setting.
public long Expires
{
get
{
long expires = -1;
if (ContactParameters.Has(EXPIRES_PARAMETER_KEY))
{
string expiresStr = ContactParameters.Get(EXPIRES_PARAMETER_KEY);
Int64.TryParse(expiresStr, out expires);
if (expires > UInt32.MaxValue)
{
expires = UInt32.MaxValue;
}
}
return expires;
}
set { ContactParameters.Set(EXPIRES_PARAMETER_KEY, value.ToString()); }
}
public string Q
{
get { return ContactParameters.Get(QVALUE_PARAMETER_KEY); }
set { ContactParameters.Set(QVALUE_PARAMETER_KEY, value); }
}
private SIPUserField m_userField;
private SIPContactHeader()
{ }
public SIPContactHeader(string contactName, SIPURI contactURI)
{
m_userField = new SIPUserField(contactName, contactURI, null);
}
public SIPContactHeader(SIPUserField contactUserField)
{
m_userField = contactUserField;
}
public static List<SIPContactHeader> ParseContactHeader(string contactHeaderStr)
{
try
{
if (contactHeaderStr == null || contactHeaderStr.Trim().Length == 0)
{
return null;
}
//string[] contactHeaders = null;
//// Broken User Agent fix (Aastra looking at you!)
//if (contactHeaderStr.IndexOf('<') != -1 && contactHeaderStr.IndexOf('>') == -1)
//{
// int nonStandardDelimPosn = contactHeaderStr.IndexOfAny(m_nonStandardURIDelimChars);
// if (nonStandardDelimPosn != -1)
// {
// // Add on the missing RQUOT and ignore whatever the rest of the header is.
// contactHeaders = new string[] { contactHeaderStr.Substring(0, nonStandardDelimPosn) + ">" };
// }
// else
// {
// // Can't work out what is going on with this header bomb out.
// throw new SIPValidationException(SIPValidationFieldsEnum.ContactHeader, "Contact header invalid.");
// }
//}
//else
//{
// contactHeaders = SIPParameters.GetKeyValuePairsFromQuoted(contactHeaderStr, ',');
//}
string[] contactHeaders = SIPParameters.GetKeyValuePairsFromQuoted(contactHeaderStr, ',');
List<SIPContactHeader> contactHeaderList = new List<SIPContactHeader>();
foreach (string contactHeaderItemStr in contactHeaders)
{
SIPContactHeader contactHeader = new SIPContactHeader();
contactHeader.RawHeader = contactHeaderStr;
contactHeader.m_userField = SIPUserField.ParseSIPUserField(contactHeaderItemStr);
contactHeaderList.Add(contactHeader);
}
return contactHeaderList;
}
catch (SIPValidationException)
{
throw;
}
catch (Exception excp)
{
throw new SIPValidationException(SIPValidationFieldsEnum.ContactHeader, "Contact header invalid, parse failed. " + excp.Message);
}
}
public static List<SIPContactHeader> CreateSIPContactList(SIPURI sipURI)
{
List<SIPContactHeader> contactHeaderList = new List<SIPContactHeader>();
contactHeaderList.Add(new SIPContactHeader(null, sipURI));
return contactHeaderList;
}
/// <summary>
/// Compares two contact headers to determine contact address equality.
/// </summary>
public static bool AreEqual(SIPContactHeader contact1, SIPContactHeader contact2)
{
if (!SIPURI.AreEqual(contact1.ContactURI, contact2.ContactURI))
{
return false;
}
else
{
// Compare invariant parameters.
string[] contact1Keys = contact1.ContactParameters.GetKeys();
if (contact1Keys != null && contact1Keys.Length > 0)
{
foreach (string key in contact1Keys)
{
if (key == EXPIRES_PARAMETER_KEY || key == QVALUE_PARAMETER_KEY)
{
continue;
}
else if (contact1.ContactParameters.Get(key) != contact2.ContactParameters.Get(key))
{
return false;
}
}
}
// Need to do the reverse as well
string[] contact2Keys = contact2.ContactParameters.GetKeys();
if (contact2Keys != null && contact2Keys.Length > 0)
{
foreach (string key in contact2Keys)
{
if (key == EXPIRES_PARAMETER_KEY || key == QVALUE_PARAMETER_KEY)
{
continue;
}
else if (contact2.ContactParameters.Get(key) != contact1.ContactParameters.Get(key))
{
return false;
}
}
}
}
return true;
}
public override string ToString()
{
if (m_userField.URI.Host == SIPConstants.SIP_REGISTER_REMOVEALL)
{
return SIPConstants.SIP_REGISTER_REMOVEALL;
}
else
{
//if (m_userField.URI.Protocol == SIPProtocolsEnum.UDP)
//{
return m_userField.ToString();
//}
//else
//{
// return m_userField.ToContactString();
//}
}
}
public SIPContactHeader CopyOf()
{
SIPContactHeader copy = new SIPContactHeader();
copy.RawHeader = RawHeader;
copy.m_userField = m_userField.CopyOf();
return copy;
}
}
public class SIPAuthenticationHeader
{
public SIPAuthorisationDigest SIPDigest;
public string Value;
public SIPAuthorisationHeadersEnum AuthorisationType;
private SIPAuthenticationHeader() : this(new SIPAuthorisationDigest())
{
}
public SIPAuthenticationHeader(SIPAuthorisationDigest sipDigest)
{
SIPDigest = sipDigest;
Value = string.Empty;
AuthorisationType = sipDigest?.AuthorisationType ?? SIPAuthorisationHeadersEnum.Authorize;
}
public SIPAuthenticationHeader(SIPAuthorisationHeadersEnum authorisationType, string realm, string nonce)
{
SIPDigest = new SIPAuthorisationDigest(authorisationType)
{
Realm = realm,
Nonce = nonce
};
Value = string.Empty;
AuthorisationType = authorisationType;
}
public static SIPAuthenticationHeader ParseSIPAuthenticationHeader(SIPAuthorisationHeadersEnum authorizationType, string headerValue)
{
try
{
var authHeader = new SIPAuthenticationHeader
{
Value = headerValue
};
if (headerValue.StartsWith(SIPAuthorisationDigest.METHOD))
{
authHeader.SIPDigest = SIPAuthorisationDigest.ParseAuthorisationDigest(authorizationType, headerValue);
}
else
{
authHeader.SIPDigest = new SIPAuthorisationDigest(SIPAuthorisationHeadersEnum.Unknown);
}
authHeader.AuthorisationType = authHeader.SIPDigest.AuthorisationType;
return authHeader;
}
catch
{
throw new ApplicationException("Error parsing SIP authentication header request, " + headerValue);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string BuildAuthorisationHeaderName(SIPAuthorisationHeadersEnum authorisationHeaderType)
{
string authHeader = null;
if (authorisationHeaderType == SIPAuthorisationHeadersEnum.Authorize)
{
authHeader = SIPHeaders.SIP_HEADER_AUTHORIZATION + ": ";
}
else if (authorisationHeaderType == SIPAuthorisationHeadersEnum.ProxyAuthenticate)
{
authHeader = SIPHeaders.SIP_HEADER_PROXYAUTHENTICATION + ": ";
}
else if (authorisationHeaderType == SIPAuthorisationHeadersEnum.ProxyAuthorization)
{
authHeader = SIPHeaders.SIP_HEADER_PROXYAUTHORIZATION + ": ";
}
else if (authorisationHeaderType == SIPAuthorisationHeadersEnum.WWWAuthenticate)
{
authHeader = SIPHeaders.SIP_HEADER_WWWAUTHENTICATE + ": ";
}
else
{
authHeader = SIPHeaders.SIP_HEADER_AUTHORIZATION + ": ";
}
return authHeader;
}
public override string ToString()
{
if (SIPDigest != null)
{
var authorisationHeaderType = (SIPDigest.AuthorisationResponseType != SIPAuthorisationHeadersEnum.Unknown) ? SIPDigest.AuthorisationResponseType : SIPDigest.AuthorisationType;
string authHeader = BuildAuthorisationHeaderName(authorisationHeaderType);
return authHeader + SIPDigest.ToString();
}
else if (!string.IsNullOrEmpty(Value))
{
string authHeader = BuildAuthorisationHeaderName(AuthorisationType);
return authHeader + Value;
}
else
{
return null;
}
}
}
/// <summary>
/// The SIPRoute class is used to represent both Route and Record-Route headers.
/// </summary>
/// <bnf>
/// Route = "Route" HCOLON route-param *(COMMA route-param)
/// route-param = name-addr *( SEMI rr-param )
///
/// Record-Route = "Record-Route" HCOLON rec-route *(COMMA rec-route)
/// rec-route = name-addr *( SEMI rr-param )
/// rr-param = generic-param
///
/// name-addr = [ display-name ] LAQUOT addr-spec RAQUOT
/// addr-spec = SIP-URI / SIPS-URI / absoluteURI
/// display-name = *(token LWS)/ quoted-string
/// generic-param = token [ EQUAL gen-value ]
/// gen-value = token / host / quoted-string
/// </bnf>
/// <remarks>
/// The Route and Record-Route headers only have parameters, no headers. Parameters of from ...;name=value;name2=value2
/// There are no specific parameters.
/// </remarks>
public class SIPRoute
{
private static string m_looseRouterParameter = SIPConstants.SIP_LOOSEROUTER_PARAMETER;
private static char[] m_angles = new char[] { '<', '>' };
private SIPUserField m_userField;
public string Host
{
get { return m_userField.URI.Host; }
set { m_userField.URI.Host = value; }
}
public SIPURI URI
{
get { return m_userField.URI; }
}
public bool IsStrictRouter
{
get { return !m_userField.URI.Parameters.Has(m_looseRouterParameter); }
set
{
if (value)
{
m_userField.URI.Parameters.Remove(m_looseRouterParameter);
}
else
{
m_userField.URI.Parameters.Set(m_looseRouterParameter, null);
}
}
}
private SIPRoute()
{ }
public SIPRoute(string host)
{
if (host.IsNullOrBlank())
{
throw new SIPValidationException(SIPValidationFieldsEnum.RouteHeader, "Cannot create a Route from an blank string.");
}
m_userField = SIPUserField.ParseSIPUserField(host);
}
public SIPRoute(string host, bool looseRouter)
{
if (host.IsNullOrBlank())
{
throw new SIPValidationException(SIPValidationFieldsEnum.RouteHeader, "Cannot create a Route from an blank string.");
}
m_userField = SIPUserField.ParseSIPUserField(host);