-
Notifications
You must be signed in to change notification settings - Fork 29
/
StunClient.cs
1137 lines (984 loc) · 44.3 KB
/
StunClient.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
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
namespace p2pcopy
{
/// <summary>
/// This enum specifies STUN message type.
/// </summary>
internal enum StunMessageType
{
/// <summary>
/// STUN message is binding request.
/// </summary>
BindingRequest = 0x0001,
/// <summary>
/// STUN message is binding request response.
/// </summary>
BindingResponse = 0x0101,
/// <summary>
/// STUN message is binding requesr error response.
/// </summary>
BindingErrorResponse = 0x0111,
/// <summary>
/// STUN message is "shared secret" request.
/// </summary>
SharedSecretRequest = 0x0002,
/// <summary>
/// STUN message is "shared secret" request response.
/// </summary>
SharedSecretResponse = 0x0102,
/// <summary>
/// STUN message is "shared secret" request error response.
/// </summary>
SharedSecretErrorResponse = 0x0112,
}
/// <summary>
/// This class implements STUN ERROR-CODE. Defined in RFC 3489 11.2.9.
/// </summary>
internal class STUN_t_ErrorCode
{
int m_Code = 0;
string m_ReasonText = "";
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="code">Error code.</param>
/// <param name="reasonText">Reason text.</param>
internal STUN_t_ErrorCode(int code, string reasonText)
{
m_Code = code;
m_ReasonText = reasonText;
}
#region Properties Implementation
/// <summary>
/// Gets or sets error code.
/// </summary>
internal int Code
{
get { return m_Code; }
set { m_Code = value; }
}
/// <summary>
/// Gets reason text.
/// </summary>
internal string ReasonText
{
get { return m_ReasonText; }
set { m_ReasonText = value; }
}
#endregion
}
/// <summary>
/// This class implements STUN CHANGE-REQUEST attribute. Defined in RFC 3489 11.2.4.
/// </summary>
internal class StunChangeRequest
{
bool m_ChangeIP = true;
bool m_ChangePort = true;
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="changeIP">Specifies if STUN server must
/// send response to different IP than request was received.</param>
/// <param name="changePort">Specifies if STUN server must
/// send response to different port than request was received.</param>
internal StunChangeRequest(bool changeIP, bool changePort)
{
m_ChangeIP = changeIP;
m_ChangePort = changePort;
}
/// <summary>
/// Gets or sets if STUN server must send response
/// to different IP than request was received.
/// </summary>
internal bool ChangeIP
{
get { return m_ChangeIP; }
set { m_ChangeIP = value; }
}
/// <summary>
/// Gets or sets if STUN server must send response
/// to different port than request was received.
/// </summary>
internal bool ChangePort
{
get { return m_ChangePort; }
set { m_ChangePort = value; }
}
}
/// <summary>
/// Implements STUN message. Defined in RFC 3489.
/// </summary>
internal class StunMessage
{
/// <summary>
/// Specifies STUN attribute type.
/// </summary>
enum AttributeType
{
MappedAddress = 0x0001,
ResponseAddress = 0x0002,
ChangeRequest = 0x0003,
SourceAddress = 0x0004,
ChangedAddress = 0x0005,
Username = 0x0006,
Password = 0x0007,
MessageIntegrity = 0x0008,
ErrorCode = 0x0009,
UnknownAttribute = 0x000A,
ReflectedFrom = 0x000B,
XorMappedAddress = 0x8020,
XorOnly = 0x0021,
ServerName = 0x8022,
}
/// <summary>
/// Specifies IP address family.
/// </summary>
enum IPFamily
{
IPv4 = 0x01,
IPv6 = 0x02,
}
StunMessageType m_Type = StunMessageType.BindingRequest;
Guid m_pTransactionID = Guid.Empty;
IPEndPoint m_pMappedAddress = null;
IPEndPoint m_pResponseAddress = null;
StunChangeRequest m_pChangeRequest = null;
IPEndPoint m_pSourceAddress = null;
IPEndPoint m_pChangedAddress = null;
string m_UserName = null;
string m_Password = null;
STUN_t_ErrorCode m_pErrorCode = null;
IPEndPoint m_pReflectedFrom = null;
string m_ServerName = null;
/// <summary>
/// Default constructor.
/// </summary>
internal StunMessage()
{
m_pTransactionID = Guid.NewGuid();
}
/// <summary>
/// Parses STUN message from raw data packet.
/// </summary>
/// <param name="data">Raw STUN message.</param>
internal void Parse(byte[] data)
{
/* RFC 3489 11.1.
All STUN messages consist of a 20 byte header:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| STUN Message Type | Message Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Transaction ID
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
The message length is the count, in bytes, of the size of the
message, not including the 20 byte header.
*/
if (data.Length < 20)
{
throw new ArgumentException("Invalid STUN message value !");
}
int offset = 0;
//--- message header --------------------------------------------------
// STUN Message Type
int messageType = (data[offset++] << 8 | data[offset++]);
if (messageType == (int)StunMessageType.BindingErrorResponse)
{
m_Type = StunMessageType.BindingErrorResponse;
}
else if (messageType == (int)StunMessageType.BindingRequest)
{
m_Type = StunMessageType.BindingRequest;
}
else if (messageType == (int)StunMessageType.BindingResponse)
{
m_Type = StunMessageType.BindingResponse;
}
else if (messageType == (int)StunMessageType.SharedSecretErrorResponse)
{
m_Type = StunMessageType.SharedSecretErrorResponse;
}
else if (messageType == (int)StunMessageType.SharedSecretRequest)
{
m_Type = StunMessageType.SharedSecretRequest;
}
else if (messageType == (int)StunMessageType.SharedSecretResponse)
{
m_Type = StunMessageType.SharedSecretResponse;
}
else
{
throw new ArgumentException("Invalid STUN message type value !");
}
// Message Length
int messageLength = (data[offset++] << 8 | data[offset++]);
// Transaction ID
byte[] guid = new byte[16];
Array.Copy(data, offset, guid, 0, 16);
m_pTransactionID = new Guid(guid);
offset += 16;
//--- Message attributes ---------------------------------------------
while ((offset - 20) < messageLength)
{
ParseAttribute(data, ref offset);
}
}
/// <summary>
/// Converts this to raw STUN packet.
/// </summary>
/// <returns>Returns raw STUN packet.</returns>
internal byte[] ToByteData()
{
/* RFC 3489 11.1.
All STUN messages consist of a 20 byte header:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| STUN Message Type | Message Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Transaction ID
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
The message length is the count, in bytes, of the size of the
message, not including the 20 byte header.
*/
// We allocate 512 for header, that should be more than enough.
byte[] msg = new byte[512];
int offset = 0;
//--- message header -------------------------------------
// STUN Message Type (2 bytes)
msg[offset++] = (byte)((int)this.Type >> 8);
msg[offset++] = (byte)((int)this.Type & 0xFF);
// Message Length (2 bytes) will be assigned at last.
msg[offset++] = 0;
msg[offset++] = 0;
// Transaction ID (16 bytes)
Array.Copy(m_pTransactionID.ToByteArray(), 0, msg, offset, 16);
offset += 16;
//--- Message attributes ------------------------------------
/* RFC 3489 11.2.
After the header are 0 or more attributes. Each attribute is TLV
encoded, with a 16 bit type, 16 bit length, and variable value:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Value ....
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
if (this.MappedAddress != null)
{
StoreEndPoint(AttributeType.MappedAddress, this.MappedAddress, msg, ref offset);
}
else if (this.ResponseAddress != null)
{
StoreEndPoint(AttributeType.ResponseAddress, this.ResponseAddress, msg, ref offset);
}
else if (this.ChangeRequest != null)
{
/*
The CHANGE-REQUEST attribute is used by the client to request that
the server use a different address and/or port when sending the
response. The attribute is 32 bits long, although only two bits (A
and B) are used:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A B 0|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
The meaning of the flags is:
A: This is the "change IP" flag. If true, it requests the server
to send the Binding Response with a different IP address than the
one the Binding Request was received on.
B: This is the "change port" flag. If true, it requests the
server to send the Binding Response with a different port than the
one the Binding Request was received on.
*/
// Attribute header
msg[offset++] = (int)AttributeType.ChangeRequest >> 8;
msg[offset++] = (int)AttributeType.ChangeRequest & 0xFF;
msg[offset++] = 0;
msg[offset++] = 4;
msg[offset++] = 0;
msg[offset++] = 0;
msg[offset++] = 0;
msg[offset++] = (byte)(Convert.ToInt32(this.ChangeRequest.ChangeIP) << 2 | Convert.ToInt32(this.ChangeRequest.ChangePort) << 1);
}
else if (this.SourceAddress != null)
{
StoreEndPoint(AttributeType.SourceAddress, this.SourceAddress, msg, ref offset);
}
else if (this.ChangedAddress != null)
{
StoreEndPoint(AttributeType.ChangedAddress, this.ChangedAddress, msg, ref offset);
}
else if (this.UserName != null)
{
byte[] userBytes = Encoding.ASCII.GetBytes(this.UserName);
// Attribute header
msg[offset++] = (int)AttributeType.Username >> 8;
msg[offset++] = (int)AttributeType.Username & 0xFF;
msg[offset++] = (byte)(userBytes.Length >> 8);
msg[offset++] = (byte)(userBytes.Length & 0xFF);
Array.Copy(userBytes, 0, msg, offset, userBytes.Length);
offset += userBytes.Length;
}
else if (this.Password != null)
{
byte[] userBytes = Encoding.ASCII.GetBytes(this.UserName);
// Attribute header
msg[offset++] = (int)AttributeType.Password >> 8;
msg[offset++] = (int)AttributeType.Password & 0xFF;
msg[offset++] = (byte)(userBytes.Length >> 8);
msg[offset++] = (byte)(userBytes.Length & 0xFF);
Array.Copy(userBytes, 0, msg, offset, userBytes.Length);
offset += userBytes.Length;
}
else if (this.ErrorCode != null)
{
/* 3489 11.2.9.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 0 |Class| Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reason Phrase (variable) ..
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
byte[] reasonBytes = Encoding.ASCII.GetBytes(this.ErrorCode.ReasonText);
// Header
msg[offset++] = 0;
msg[offset++] = (int)AttributeType.ErrorCode;
msg[offset++] = 0;
msg[offset++] = (byte)(4 + reasonBytes.Length);
// Empty
msg[offset++] = 0;
msg[offset++] = 0;
// Class
msg[offset++] = (byte)Math.Floor((double)(this.ErrorCode.Code / 100));
// Number
msg[offset++] = (byte)(this.ErrorCode.Code & 0xFF);
// ReasonPhrase
Array.Copy(reasonBytes, msg, reasonBytes.Length);
offset += reasonBytes.Length;
}
else if (this.ReflectedFrom != null)
{
StoreEndPoint(AttributeType.ReflectedFrom, this.ReflectedFrom, msg, ref offset);
}
// Update Message Length. NOTE: 20 bytes header not included.
msg[2] = (byte)((offset - 20) >> 8);
msg[3] = (byte)((offset - 20) & 0xFF);
// Make reatval with actual size.
byte[] retVal = new byte[offset];
Array.Copy(msg, retVal, retVal.Length);
return retVal;
}
/// <summary>
/// Parses attribute from data.
/// </summary>
/// <param name="data">SIP message data.</param>
/// <param name="offset">Offset in data.</param>
void ParseAttribute(byte[] data, ref int offset)
{
/* RFC 3489 11.2.
Each attribute is TLV encoded, with a 16 bit type, 16 bit length, and variable value:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Value ....
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
// Type
AttributeType type = (AttributeType)(data[offset++] << 8 | data[offset++]);
// Length
int length = (data[offset++] << 8 | data[offset++]);
// MAPPED-ADDRESS
if (type == AttributeType.MappedAddress)
{
m_pMappedAddress = ParseEndPoint(data, ref offset);
}
// RESPONSE-ADDRESS
else if (type == AttributeType.ResponseAddress)
{
m_pResponseAddress = ParseEndPoint(data, ref offset);
}
// CHANGE-REQUEST
else if (type == AttributeType.ChangeRequest)
{
/*
The CHANGE-REQUEST attribute is used by the client to request that
the server use a different address and/or port when sending the
response. The attribute is 32 bits long, although only two bits (A
and B) are used:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A B 0|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
The meaning of the flags is:
A: This is the "change IP" flag. If true, it requests the server
to send the Binding Response with a different IP address than the
one the Binding Request was received on.
B: This is the "change port" flag. If true, it requests the
server to send the Binding Response with a different port than the
one the Binding Request was received on.
*/
// Skip 3 bytes
offset += 3;
m_pChangeRequest = new StunChangeRequest((data[offset] & 4) != 0, (data[offset] & 2) != 0);
offset++;
}
// SOURCE-ADDRESS
else if (type == AttributeType.SourceAddress)
{
m_pSourceAddress = ParseEndPoint(data, ref offset);
}
// CHANGED-ADDRESS
else if (type == AttributeType.ChangedAddress)
{
m_pChangedAddress = ParseEndPoint(data, ref offset);
}
// USERNAME
else if (type == AttributeType.Username)
{
m_UserName = Encoding.Default.GetString(data, offset, length);
offset += length;
}
// PASSWORD
else if (type == AttributeType.Password)
{
m_Password = Encoding.Default.GetString(data, offset, length);
offset += length;
}
// MESSAGE-INTEGRITY
else if (type == AttributeType.MessageIntegrity)
{
offset += length;
}
// ERROR-CODE
else if (type == AttributeType.ErrorCode)
{
/* 3489 11.2.9.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 0 |Class| Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reason Phrase (variable) ..
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
int errorCode = (data[offset + 2] & 0x7) * 100 + (data[offset + 3] & 0xFF);
m_pErrorCode = new STUN_t_ErrorCode(errorCode, Encoding.Default.GetString(data, offset + 4, length - 4));
offset += length;
}
// UNKNOWN-ATTRIBUTES
else if (type == AttributeType.UnknownAttribute)
{
offset += length;
}
// REFLECTED-FROM
else if (type == AttributeType.ReflectedFrom)
{
m_pReflectedFrom = ParseEndPoint(data, ref offset);
}
// XorMappedAddress
// XorOnly
// ServerName
else if (type == AttributeType.ServerName)
{
m_ServerName = Encoding.Default.GetString(data, offset, length);
offset += length;
}
// Unknown
else
{
offset += length;
}
}
/// <summary>
/// Pasrses IP endpoint attribute.
/// </summary>
/// <param name="data">STUN message data.</param>
/// <param name="offset">Offset in data.</param>
/// <returns>Returns parsed IP end point.</returns>
IPEndPoint ParseEndPoint(byte[] data, ref int offset)
{
/*
It consists of an eight bit address family, and a sixteen bit
port, followed by a fixed length value representing the IP address.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|x x x x x x x x| Family | Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
// Skip family
offset++;
offset++;
// Port
int port = (data[offset++] << 8 | data[offset++]);
// Address
byte[] ip = new byte[4];
ip[0] = data[offset++];
ip[1] = data[offset++];
ip[2] = data[offset++];
ip[3] = data[offset++];
return new IPEndPoint(new IPAddress(ip), port);
}
/// <summary>
/// Stores ip end point attribute to buffer.
/// </summary>
/// <param name="type">Attribute type.</param>
/// <param name="endPoint">IP end point.</param>
/// <param name="message">Buffer where to store.</param>
/// <param name="offset">Offset in buffer.</param>
void StoreEndPoint(AttributeType type, IPEndPoint endPoint, byte[] message, ref int offset)
{
/*
It consists of an eight bit address family, and a sixteen bit
port, followed by a fixed length value representing the IP address.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|x x x x x x x x| Family | Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
// Header
message[offset++] = (byte)((int)type >> 8);
message[offset++] = (byte)((int)type & 0xFF);
message[offset++] = 0;
message[offset++] = 8;
// Unused
message[offset++] = 0;
// Family
message[offset++] = (byte)IPFamily.IPv4;
// Port
message[offset++] = (byte)(endPoint.Port >> 8);
message[offset++] = (byte)(endPoint.Port & 0xFF);
// Address
byte[] ipBytes = endPoint.Address.GetAddressBytes();
message[offset++] = ipBytes[0];
message[offset++] = ipBytes[0];
message[offset++] = ipBytes[0];
message[offset++] = ipBytes[0];
}
/// <summary>
/// Gets STUN message type.
/// </summary>
internal StunMessageType Type
{
get { return m_Type; }
set { m_Type = value; }
}
/// <summary>
/// Gets transaction ID.
/// </summary>
internal Guid TransactionID
{
get { return m_pTransactionID; }
}
/// <summary>
/// Gets or sets IP end point what was actually connected to STUN server. Returns null if not specified.
/// </summary>
internal IPEndPoint MappedAddress
{
get { return m_pMappedAddress; }
set { m_pMappedAddress = value; }
}
/// <summary>
/// Gets or sets IP end point where to STUN client likes to receive response.
/// Value null means not specified.
/// </summary>
internal IPEndPoint ResponseAddress
{
get { return m_pResponseAddress; }
set { m_pResponseAddress = value; }
}
/// <summary>
/// Gets or sets how and where STUN server must send response back to STUN client.
/// Value null means not specified.
/// </summary>
internal StunChangeRequest ChangeRequest
{
get { return m_pChangeRequest; }
set { m_pChangeRequest = value; }
}
/// <summary>
/// Gets or sets STUN server IP end point what sent response to STUN client. Value null
/// means not specified.
/// </summary>
internal IPEndPoint SourceAddress
{
get { return m_pSourceAddress; }
set { m_pSourceAddress = value; }
}
/// <summary>
/// Gets or sets IP end point where STUN server will send response back to STUN client
/// if the "change IP" and "change port" flags had been set in the ChangeRequest.
/// </summary>
internal IPEndPoint ChangedAddress
{
get { return m_pChangedAddress; }
set { m_pChangedAddress = value; }
}
/// <summary>
/// Gets or sets user name. Value null means not specified.
/// </summary>
internal string UserName
{
get { return m_UserName; }
set { m_UserName = value; }
}
/// <summary>
/// Gets or sets password. Value null means not specified.
/// </summary>
internal string Password
{
get { return m_Password; }
set { m_Password = value; }
}
//internal MessageIntegrity
/// <summary>
/// Gets or sets error info. Returns null if not specified.
/// </summary>
internal STUN_t_ErrorCode ErrorCode
{
get { return m_pErrorCode; }
set { m_pErrorCode = value; }
}
/// <summary>
/// Gets or sets IP endpoint from which IP end point STUN server got STUN client request.
/// Value null means not specified.
/// </summary>
internal IPEndPoint ReflectedFrom
{
get { return m_pReflectedFrom; }
set { m_pReflectedFrom = value; }
}
}
/// <summary>
/// This class implements STUN client. Defined in RFC 3489.
/// </summary>
/// <example>
/// <code>
/// // Create new socket for STUN client.
/// Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
/// socket.Bind(new IPEndPoint(IPAddress.Any,0));
///
/// // Query STUN server
/// STUN_Result result = STUN_Client.Query("stunserver.org",3478,socket);
/// if(result.NetType != STUN_NetType.UdpBlocked){
/// // UDP blocked or !!!! bad STUN server
/// }
/// else{
/// IPEndPoint publicEP = result.PublicEndPoint;
/// // Do your stuff
/// }
/// </code>
/// </example>
internal class StunClient
{
#region static method Query
/// <summary>
/// Gets NAT info from STUN server.
/// </summary>
/// <param name="host">STUN server name or IP.</param>
/// <param name="port">STUN server port. Default port is 3478.</param>
/// <param name="socket">UDP socket to use.</param>
/// <returns>Returns UDP netwrok info.</returns>
/// <exception cref="Exception">Throws exception if unexpected error happens.</exception>
internal static StunResult Query(string host, int port, Socket socket)
{
if (host == null)
{
throw new ArgumentNullException("host");
}
if (socket == null)
{
throw new ArgumentNullException("socket");
}
if (port < 1)
{
throw new ArgumentException("Port value must be >= 1 !");
}
IPEndPoint remoteEndPoint = new IPEndPoint(Dns.GetHostAddresses(host)[0], port);
if (remoteEndPoint.AddressFamily != AddressFamily.InterNetwork)
{
return new StunResult(StunNetType.UdpBlocked, null);
}
socket.ReceiveTimeout = 3000;
socket.SendTimeout = 3000;
/*
In test I, the client sends a STUN Binding Request to a server, without any flags set in the
CHANGE-REQUEST attribute, and without the RESPONSE-ADDRESS attribute. This causes the server
to send the response back to the address and port that the request came from.
In test II, the client sends a Binding Request with both the "change IP" and "change port" flags
from the CHANGE-REQUEST attribute set.
In test III, the client sends a Binding Request with only the "change port" flag set.
+--------+
| Test |
| I |
+--------+
|
|
V
/\ /\
N / \ Y / \ Y +--------+
UDP <-------/Resp\--------->/ IP \------------->| Test |
Blocked \ ? / \Same/ | II |
\ / \? / +--------+
\/ \/ |
| N |
| V
V /\
+--------+ Sym. N / \
| Test | UDP <---/Resp\
| II | Firewall \ ? /
+--------+ \ /
| \/
V |Y
/\ /\ |
Symmetric N / \ +--------+ N / \ V
NAT <--- / IP \<-----| Test |<--- /Resp\ Open
\Same/ | I | \ ? / Internet
\? / +--------+ \ /
\/ \/
| |Y
| |
| V
| Full
| Cone
V /\
+--------+ / \ Y
| Test |------>/Resp\---->Restricted
| III | \ ? /
+--------+ \ /
\/
|N
| Port
+------>Restricted
*/
// Test I
StunMessage test1 = new StunMessage();
test1.Type = StunMessageType.BindingRequest;
StunMessage test1response = DoTransaction(test1, socket, remoteEndPoint);
// UDP blocked.
if (test1response == null)
{
return new StunResult(StunNetType.UdpBlocked, null);
}
else
{
// Test II
StunMessage test2 = new StunMessage();
test2.Type = StunMessageType.BindingRequest;
test2.ChangeRequest = new StunChangeRequest(true, true);
// No NAT.
if (socket.LocalEndPoint.Equals(test1response.MappedAddress))
{
StunMessage test2Response = DoTransaction(test2, socket, remoteEndPoint);
// Open Internet.
if (test2Response != null)
{
return new StunResult(StunNetType.OpenInternet, test1response.MappedAddress);
}
// Symmetric UDP firewall.
else
{
return new StunResult(StunNetType.SymmetricUdpFirewall, test1response.MappedAddress);
}
}
// NAT
else
{
StunMessage test2Response = DoTransaction(test2, socket, remoteEndPoint);
// Full cone NAT.
if (test2Response != null)
{
return new StunResult(StunNetType.FullCone, test1response.MappedAddress);
}
else
{
/*
If no response is received, it performs test I again, but this time, does so to
the address and port from the CHANGED-ADDRESS attribute from the response to test I.
*/
// Test I(II)
StunMessage test12 = new StunMessage();
test12.Type = StunMessageType.BindingRequest;
StunMessage test12Response = DoTransaction(test12, socket, test1response.ChangedAddress);
if (test12Response == null)
{
throw new Exception("STUN Test I(II) didn't get response!");
}
else
{
// Symmetric NAT
if (!test12Response.MappedAddress.Equals(test1response.MappedAddress))
{
return new StunResult(StunNetType.Symmetric, test1response.MappedAddress);
}
else
{
// Test III
StunMessage test3 = new StunMessage();
test3.Type = StunMessageType.BindingRequest;
test3.ChangeRequest = new StunChangeRequest(false, true);
StunMessage test3Response = DoTransaction(test3, socket, test1response.ChangedAddress);
// Restricted
if (test3Response != null)
{
return new StunResult(StunNetType.RestrictedCone, test1response.MappedAddress);
}
// Port restricted
else
{
return new StunResult(StunNetType.PortRestrictedCone, test1response.MappedAddress);
}
}
}
}
}
}
}
#endregion
#region method DoTransaction
/// <summary>