forked from coturn/coturn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TurnMsgLib.h
1197 lines (1062 loc) · 28 KB
/
TurnMsgLib.h
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
/*
* Copyright (C) 2011, 2012, 2013 Citrix Systems
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef __LIB_TURN_MSG_CPP__
#define __LIB_TURN_MSG_CPP__
#include "ns_turn_ioaddr.h"
#include "ns_turn_msg.h"
#include <string>
namespace turn {
class StunAttr;
/**
* Exception "end of buffer"
*/
class EndOfStunMsgException {
public:
EndOfStunMsgException() {}
virtual ~EndOfStunMsgException() {}
};
/**
* Exception "wrong format of StunAttr"
*/
class WrongStunAttrFormatException {
public:
WrongStunAttrFormatException() {}
virtual ~WrongStunAttrFormatException() {}
};
/**
* Exception "wrong format of StunBuffer"
*/
class WrongStunBufferFormatException {
public:
WrongStunBufferFormatException() {}
virtual ~WrongStunBufferFormatException() {}
};
/**
* Iterator class for attributes
*/
class StunAttrIterator {
public:
/**
* Iterator constructor: creates iterator on raw messagebuffer.
*/
StunAttrIterator(uint8_t *buf, size_t sz) throw (WrongStunBufferFormatException) :
_buf(buf), _sz(sz) {
if(!stun_is_command_message_str(_buf, _sz)) {
throw WrongStunBufferFormatException();
}
_sar = stun_attr_get_first_str(_buf, _sz);
}
/**
* Iterator constructor: create iterator over message.
*/
template<class T>
StunAttrIterator(T &msg) throw (WrongStunBufferFormatException) :
_buf(msg.getRawBuffer()), _sz(msg.getSize()) {
if(!stun_is_command_message_str(_buf, _sz)) {
throw WrongStunBufferFormatException();
}
_sar = stun_attr_get_first_str(_buf, _sz);
}
/**
* Iterator constructor: creates iterator over raw buffer, starting from first
* location of an attribute of particular type.
*/
StunAttrIterator(uint8_t *buf, size_t sz, uint16_t attr_type) throw (WrongStunBufferFormatException) :
_buf(buf), _sz(sz) {
if(!stun_is_command_message_str(_buf, _sz)) {
throw WrongStunBufferFormatException();
}
_sar = stun_attr_get_first_by_type_str(_buf, _sz, attr_type);
}
/**
* Iterator constructor: creates iterator over message, starting from first
* location of an attribute of particular type.
*/
template<class T>
StunAttrIterator(T &msg, uint16_t attr_type) throw (WrongStunBufferFormatException) :
_buf(msg.getRawBuffer()), _sz(msg.getSize()) {
if(!stun_is_command_message_str(_buf, _sz)) {
throw WrongStunBufferFormatException();
}
_sar = stun_attr_get_first_by_type_str(_buf, _sz, attr_type);
}
/**
* Moves iterator to next attribute location
*/
void next() throw(EndOfStunMsgException) {
if(!_sar) {
throw EndOfStunMsgException();
}
_sar = stun_attr_get_next_str(_buf,_sz,_sar);
}
/**
* Is the iterator finished
*/
bool eof() const {
return (!_sar);
}
/**
* Is the iterator at an address attribute
*/
bool isAddr() const {
return stun_attr_is_addr(_sar);
}
/**
* Return address family attribute value (if the iterator at the "address family" attribute.
*/
int getAddressFamily() const {
return stun_get_requested_address_family(_sar);
}
/**
* Get attribute type
*/
int getType() const {
return stun_attr_get_type(_sar);
}
/**
* Destructor
*/
virtual ~StunAttrIterator() {}
/**
* Return raw memroy field of the attribute value.
* If the attribute value length is zero (0), then return NULL.
*/
const uint8_t *getRawBuffer(size_t &sz) const throw(WrongStunAttrFormatException) {
int len = stun_attr_get_len(_sar);
if(len<0)
throw WrongStunAttrFormatException();
sz = (size_t)len;
const uint8_t *value = stun_attr_get_value(_sar);
return value;
}
friend class StunAttr;
private:
uint8_t *_buf;
size_t _sz;
stun_attr_ref _sar;
};
/**
* Root class of all STUN attributes.
* Can be also used for a generic attribute object.
*/
class StunAttr {
public:
/**
* Empty constructor
*/
StunAttr() : _attr_type(0), _value(0), _sz(0) {}
/**
* Constructs attribute from iterator
*/
StunAttr(const StunAttrIterator &iter) throw(WrongStunAttrFormatException, EndOfStunMsgException) {
if(iter.eof()) {
throw EndOfStunMsgException();
}
size_t sz = 0;
const uint8_t *ptr = iter.getRawBuffer(sz);
if(sz>=0xFFFF)
throw WrongStunAttrFormatException();
int at = iter.getType();
if(at<0)
throw WrongStunAttrFormatException();
_attr_type = (uint16_t)at;
_sz = sz;
_value=(uint8_t*)malloc(_sz);
if(ptr)
bcopy(ptr,_value,_sz);
}
/**
* Destructor
*/
virtual ~StunAttr() {
if(_value)
free(_value,_sz);
}
/**
* Return raw data representation of the attribute
*/
const uint8_t *getRawValue(size_t &sz) const {
sz=_sz;
return _value;
}
/**
* Set raw data value
*/
void setRawValue(uint8_t *value, size_t sz) throw(WrongStunAttrFormatException) {
if(sz>0xFFFF)
throw WrongStunAttrFormatException();
if(_value)
free(_value,_sz);
_sz = sz;
_value=(uint8_t*)malloc(_sz);
if(value)
bcopy(value,_value,_sz);
}
/**
* Get attribute type
*/
uint16_t getType() const {
return _attr_type;
}
/**
* Set attribute type
*/
void setType(uint16_t at) {
_attr_type = at;
}
/**
* Add attribute to a message
*/
template<class T>
int addToMsg(T &msg) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
if(!_attr_type)
throw WrongStunAttrFormatException();
uint8_t *buffer = msg.getRawBuffer();
if(buffer) {
size_t sz = msg.getSize();
if(addToBuffer(buffer, sz)<0) {
throw WrongStunBufferFormatException();
}
msg.setSize(sz);
return 0;
}
throw WrongStunBufferFormatException();
}
protected:
/**
* Virtual function member to add attribute to a raw buffer
*/
virtual int addToBuffer(uint8_t *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
if(buffer) {
if(!_value)
throw WrongStunAttrFormatException();
if(stun_attr_add_str(buffer, &sz, _attr_type, _value, _sz)<0) {
throw WrongStunBufferFormatException();
}
return 0;
}
throw WrongStunBufferFormatException();
}
/**
* Get low-level iterator object
*/
static stun_attr_ref getSar(const StunAttrIterator &iter) {
return iter._sar;
}
private:
uint16_t _attr_type;
uint8_t *_value;
size_t _sz;
};
/**
* Channel number attribute class
*/
class StunAttrChannelNumber : public StunAttr {
public:
StunAttrChannelNumber() : _cn(0) {
setType(STUN_ATTRIBUTE_CHANNEL_NUMBER);
}
StunAttrChannelNumber(const StunAttrIterator &iter)
throw(WrongStunAttrFormatException, EndOfStunMsgException) :
StunAttr(iter) {
if(iter.eof())
throw EndOfStunMsgException();
_cn = stun_attr_get_channel_number(getSar(iter));
if(!_cn)
throw WrongStunAttrFormatException();
}
virtual ~StunAttrChannelNumber() {}
uint16_t getChannelNumber() const {
return _cn;
}
void setChannelNumber(uint16_t cn) {
_cn = cn;
}
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_channel_number_str(buffer,&sz,_cn);
}
private:
uint16_t _cn;
};
/**
* Even port attribute class
*/
class StunAttrEvenPort : public StunAttr {
public:
StunAttrEvenPort() : _ep(0) {
setType(STUN_ATTRIBUTE_EVEN_PORT);
}
StunAttrEvenPort(const StunAttrIterator &iter)
throw(WrongStunAttrFormatException, EndOfStunMsgException) :
StunAttr(iter) {
if(iter.eof())
throw EndOfStunMsgException();
_ep = stun_attr_get_even_port(getSar(iter));
}
virtual ~StunAttrEvenPort() {}
uint8_t getEvenPort() const {
return _ep;
}
void setEvenPort(uint8_t ep) {
_ep = ep;
}
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_str(buffer, &sz, STUN_ATTRIBUTE_EVEN_PORT, &_ep, 1);
}
private:
uint8_t _ep;
};
/**
* Reservation token attribute class
*/
class StunAttrReservationToken : public StunAttr {
public:
StunAttrReservationToken() : _rt(0) {
setType(STUN_ATTRIBUTE_RESERVATION_TOKEN);
}
StunAttrReservationToken(const StunAttrIterator &iter)
throw(WrongStunAttrFormatException, EndOfStunMsgException) :
StunAttr(iter) {
if(iter.eof())
throw EndOfStunMsgException();
_rt = stun_attr_get_reservation_token_value(getSar(iter));
}
virtual ~StunAttrReservationToken() {}
uint64_t getReservationToken() const {
return _rt;
}
void setReservationToken(uint64_t rt) {
_rt = rt;
}
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
uint64_t reservation_token = ioa_ntoh64(_rt);
return stun_attr_add_str(buffer, &sz, STUN_ATTRIBUTE_RESERVATION_TOKEN, (uint8_t*) (&reservation_token), 8);
}
private:
uint64_t _rt;
};
/**
* This attribute class is used for all address attributes
*/
class StunAttrAddr : public StunAttr {
public:
StunAttrAddr(uint16_t attr_type = 0) {
addr_set_any(&_addr);
setType(attr_type);
}
StunAttrAddr(const StunAttrIterator &iter)
throw(WrongStunAttrFormatException, EndOfStunMsgException) :
StunAttr(iter) {
if(iter.eof())
throw EndOfStunMsgException();
size_t sz = 0;
const uint8_t *buf = iter.getRawBuffer(sz);
if(stun_attr_get_addr_str(buf,sz,getSar(iter),&_addr,NULL)<0) {
throw WrongStunAttrFormatException();
}
}
virtual ~StunAttrAddr() {}
void getAddr(ioa_addr &addr) const {
addr_cpy(&addr,&_addr);
}
void setAddr(ioa_addr &addr) {
addr_cpy(&_addr,&addr);
}
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_addr_str(buffer, &sz, getType(), &_addr);
}
private:
ioa_addr _addr;
};
/**
* Change Request attribute class
*/
class StunAttrChangeRequest : public StunAttr {
public:
StunAttrChangeRequest() : _changeIp(0), _changePort(0) {
setType(STUN_ATTRIBUTE_CHANGE_REQUEST);
}
StunAttrChangeRequest(const StunAttrIterator &iter)
throw(WrongStunAttrFormatException, EndOfStunMsgException) :
StunAttr(iter) {
if(iter.eof())
throw EndOfStunMsgException();
if(stun_attr_get_change_request_str(getSar(iter), &_changeIp, &_changePort)<0) {
throw WrongStunAttrFormatException();
}
}
virtual ~StunAttrChangeRequest() {}
bool getChangeIp() const {
return _changeIp;
}
void setChangeIp(bool ci) {
if(ci)
_changeIp = 1;
else
_changeIp = 0;
}
bool getChangePort() const {
return _changePort;
}
void setChangePort(bool cp) {
if(cp)
_changePort = 1;
else
_changePort = 0;
}
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_change_request_str(buffer, &sz, _changeIp, _changePort);
}
private:
int _changeIp;
int _changePort;
};
/**
* Change Request attribute class
*/
class StunAttrResponsePort : public StunAttr {
public:
StunAttrResponsePort() : _rp(0) {
setType(STUN_ATTRIBUTE_RESPONSE_PORT);
}
StunAttrResponsePort(const StunAttrIterator &iter)
throw(WrongStunAttrFormatException, EndOfStunMsgException) :
StunAttr(iter) {
if(iter.eof())
throw EndOfStunMsgException();
int rp = stun_attr_get_response_port_str(getSar(iter));
if(rp<0) {
throw WrongStunAttrFormatException();
}
_rp = (uint16_t)rp;
}
virtual ~StunAttrResponsePort() {}
uint16_t getResponsePort() const {
return _rp;
}
void setResponsePort(uint16_t p) {
_rp = p;
}
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_response_port_str(buffer, &sz, _rp);
}
private:
uint16_t _rp;
};
/**
* Padding attribute class
*/
class StunAttrPadding : public StunAttr {
public:
StunAttrPadding() : _p(0) {
setType(STUN_ATTRIBUTE_PADDING);
}
StunAttrPadding(const StunAttrIterator &iter)
throw(WrongStunAttrFormatException, EndOfStunMsgException) :
StunAttr(iter) {
if(iter.eof())
throw EndOfStunMsgException();
int p = stun_attr_get_padding_len_str(getSar(iter));
if(p<0) {
throw WrongStunAttrFormatException();
}
_p = (uint16_t)p;
}
virtual ~StunAttrPadding() {}
uint16_t getPadding() const {
return _p;
}
/**
* Set length of padding
*/
void setPadding(uint16_t p) {
_p = p;
}
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_padding_str(buffer, &sz, _p);
}
private:
uint16_t _p;
};
/**
* Generic "STUN Message" class, base class for all messages
*/
class StunMsg {
public:
/**
* Empty constructor
*/
StunMsg() {
_allocated_sz = 0xFFFF;
_buffer = (uint8_t*)malloc(_allocated_sz);
_deallocate = true;
_sz = 0;
_constructed = 0;
}
/**
* Construct message over raw buffer.
* Parameter "construct" is true if the buffer is initialized.
*/
StunMsg(uint8_t *buffer, size_t total_sz, size_t sz, bool constructed) :
_buffer(buffer), _deallocate(false), _allocated_sz(total_sz),
_sz(sz), _constructed(constructed) {}
/**
* Destructor
*/
virtual ~StunMsg() {
if(_deallocate && _buffer) {
free(_buffer, _allocated_sz);
}
}
/**
* Initialize buffer
*/
void construct() {
constructBuffer();
}
/**
* Checks if the message is properly constructed
*/
bool isValid() {
return check();
}
/**
* get raw buffer
*/
uint8_t *getRawBuffer() {
return _buffer;
}
/**
* Get message size in the buffer (message can be mnuch smaller than the whole buffer)
*/
size_t getSize() const {
return _sz;
}
/**
* Set message size
*/
void setSize(size_t sz) throw(WrongStunBufferFormatException) {
if(sz>_allocated_sz)
throw WrongStunBufferFormatException();
_sz = sz;
}
/**
* Check if the raw buffer is a TURN "command" (request, response or indication).
*/
static bool isCommand(uint8_t *buffer, size_t sz) {
return stun_is_command_message_str(buffer, sz);
}
/**
* Check if the current message object is a "command" (request, response, or indication).
*/
bool isCommand() const {
return stun_is_command_message_str(_buffer, _sz);
}
static bool isIndication(uint8_t *buffer, size_t sz) {
return stun_is_indication_str(buffer, sz);
}
static bool isRequest(uint8_t *buffer, size_t sz) {
return stun_is_request_str(buffer, sz);
}
static bool isSuccessResponse(uint8_t *buffer, size_t sz) {
return stun_is_success_response_str(buffer, sz);
}
static bool isErrorResponse(uint8_t *buffer, size_t sz,
int &err_code, uint8_t *err_msg, size_t err_msg_size) {
return stun_is_error_response_str(buffer, sz, &err_code, err_msg, err_msg_size);
}
/**
* Check if the raw buffer is a challenge response (the one with 401 error and realm and nonce values).
*/
static bool isChallengeResponse(const uint8_t* buf, size_t sz,
int &err_code, uint8_t *err_msg, size_t err_msg_size,
uint8_t *realm, uint8_t *nonce,
uint8_t *server_name, int *oauth) {
return stun_is_challenge_response_str(buf, sz, &err_code, err_msg, err_msg_size, realm, nonce, server_name, oauth);
}
/**
* Check if the message is a channel message
*/
static bool isChannel(uint8_t *buffer, size_t sz) {
return is_channel_msg_str(buffer, sz);
}
/**
* Check if the fingerprint is present.
*/
static bool isFingerprintPresent(uint8_t *buffer, size_t sz) {
if(!stun_is_command_message_str(buffer,sz))
return false;
stun_attr_ref sar = stun_attr_get_first_by_type_str(buffer, sz, STUN_ATTRIBUTE_FINGERPRINT);
if(!sar)
return false;
return true;
}
/**
* Check the fingerprint
*/
static bool checkFingerprint(uint8_t *buffer, size_t sz) {
return stun_is_command_message_full_check_str(buffer, sz, 1, NULL);
}
/**
* Add attribute to the message
*/
int addAttr(StunAttr &attr) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return attr.addToMsg(*this);
}
/**
* Get transaction ID
*/
virtual stun_tid getTid() const throw(WrongStunBufferFormatException) {
if(!_constructed || !isCommand())
throw WrongStunBufferFormatException();
stun_tid tid;
stun_tid_from_message_str(_buffer,_sz,&tid);
return tid;
}
/**
* Set transaction ID
*/
virtual void setTid(stun_tid &tid) throw(WrongStunBufferFormatException) {
if(!_constructed || !isCommand())
throw WrongStunBufferFormatException();
stun_tid_message_cpy(_buffer, &tid);
}
/**
* Add fingerprint to the message
*/
void addFingerprint() throw(WrongStunBufferFormatException) {
if(!_constructed || !isCommand())
throw WrongStunBufferFormatException();
stun_attr_add_fingerprint_str(_buffer,&_sz);
}
/**
* Check message integrity, in secure communications.
*/
bool checkMessageIntegrity(turn_credential_type ct, std::string &uname, std::string &realm, std::string &upwd) const
throw(WrongStunBufferFormatException) {
if(!_constructed || !isCommand())
throw WrongStunBufferFormatException();
uint8_t *suname=(uint8_t*)strdup(uname.c_str());
uint8_t *srealm=(uint8_t*)strdup(realm.c_str());
uint8_t *supwd=(uint8_t*)strdup(upwd.c_str());
SHATYPE sht = SHATYPE_SHA1;
bool ret = (0< stun_check_message_integrity_str(ct,_buffer, _sz, suname, srealm, supwd, sht));
free(suname);
free(srealm);
free(supwd);
return ret;
}
/**
* Adds long-term message integrity data to the message.
*/
void addLTMessageIntegrity(std::string &uname, std::string &realm, std::string &upwd, std::string &nonce)
throw(WrongStunBufferFormatException) {
if(!_constructed || !isCommand())
throw WrongStunBufferFormatException();
uint8_t *suname=(uint8_t*)strdup(uname.c_str());
uint8_t *srealm=(uint8_t*)strdup(realm.c_str());
uint8_t *supwd=(uint8_t*)strdup(upwd.c_str());
uint8_t *snonce=(uint8_t*)strdup(nonce.c_str());
stun_attr_add_integrity_by_user_str(_buffer, &_sz, suname, srealm, supwd, snonce, SHATYPE_SHA1);
free(suname);
free(srealm);
free(supwd);
free(snonce);
}
/**
* Adds short-term message integrity data to the message.
*/
void addSTMessageIntegrity(std::string &uname, std::string &upwd)
throw(WrongStunBufferFormatException) {
if(!_constructed || !isCommand())
throw WrongStunBufferFormatException();
uint8_t *suname=(uint8_t*)strdup(uname.c_str());
uint8_t *supwd=(uint8_t*)strdup(upwd.c_str());
stun_attr_add_integrity_by_user_short_term_str(_buffer, &_sz, suname, supwd, SHATYPE_SHA1);
free(suname);
free(supwd);
}
protected:
virtual void constructBuffer() = 0;
virtual bool check() = 0;
protected:
uint8_t *_buffer;
bool _deallocate;
size_t _allocated_sz;
size_t _sz;
bool _constructed;
};
/**
* Class that represents the "request" flavor of STUN/TURN messages.
*/
class StunMsgRequest : public StunMsg {
public:
StunMsgRequest(uint16_t method) : _method(method) {};
StunMsgRequest(uint8_t *buffer, size_t total_sz, size_t sz, bool constructed)
throw(WrongStunBufferFormatException) :
StunMsg(buffer,total_sz,sz,constructed),_method(0) {
if(constructed) {
if(!stun_is_request_str(buffer,sz)) {
throw WrongStunBufferFormatException();
}
_method = stun_get_method_str(buffer,sz);
}
}
virtual ~StunMsgRequest() {}
/**
* Get request method
*/
uint16_t getMethod() const {
return _method;
}
/**
* Set method
*/
void setMethod(uint16_t method) {
_method = method;
}
/**
* Construct binding request
*/
void constructBindingRequest() {
stun_set_binding_request_str(_buffer, &_sz);
}
bool isBindingRequest() const {
return stun_is_binding_request_str(_buffer,_sz,0);
}
/**
* Construct allocate request
*/
void constructAllocateRequest(uint32_t lifetime, int af4, int af6, uint8_t transport, int mobile, const char* rt, int ep) {
stun_set_allocate_request_str(_buffer, &_sz, lifetime, af4, af6, transport, mobile, rt, ep);
}
/**
* Construct channel bind request
*/
void constructChannelBindRequest(const ioa_addr &peer_addr, uint16_t channel_number) {
stun_set_channel_bind_request_str(_buffer, &_sz,
&peer_addr, channel_number);
}
protected:
virtual void constructBuffer() {
stun_init_request_str(_method,_buffer,&_sz);
_constructed = true;
}
virtual bool check() {
if(!_constructed)
return false;
if(!stun_is_request_str(_buffer,_sz)) {
return false;
}
if(_method != stun_get_method_str(_buffer,_sz)) {
return false;
}
return true;
}
private:
uint16_t _method;
};
/**
* Class for STUN/TURN responses
*/
class StunMsgResponse : public StunMsg {
public:
StunMsgResponse(uint16_t method, stun_tid &tid) : _method(method), _err(0), _reason(""), _tid(tid) {};
StunMsgResponse(uint16_t method, int error_code, std::string reason, stun_tid &tid) :
_method(method), _err(error_code), _reason(reason), _tid(tid) {
};
StunMsgResponse(uint8_t *buffer, size_t total_sz, size_t sz, bool constructed)
throw(WrongStunBufferFormatException) :
StunMsg(buffer,total_sz,sz,constructed),_method(0),_err(0),_reason("") {
if(constructed) {
if(!stun_is_success_response_str(buffer,sz)) {
uint8_t errtxt[0xFFFF];
if(!stun_is_error_response_str(buffer,sz,&_err,errtxt,sizeof(errtxt))) {
throw WrongStunBufferFormatException();
}
_reason = (char*)errtxt;
}
_method = stun_get_method_str(buffer,sz);
stun_tid_from_message_str(_buffer,_sz,&_tid);
}
}
uint16_t getMethod() const {
return _method;
}
void setMethod(uint16_t method) {
_method = method;
}
/**
* Get error code
*/
int getError() const {
return _err;
}
/**
* Set error code
*/
void setError(int err) {
_err = err;
}
/**
* Get error message
*/
std::string getReason() const {
return _reason;
}
/**
* Set error message
*/
void setReason(std::string reason) {
_reason = reason;
}
/**
* Set transaction ID
*/
void setTid(stun_tid &tid) throw(WrongStunBufferFormatException) {
_tid = tid;
}
/**
* Get transaction ID
*/
virtual stun_tid getTid() const throw(WrongStunBufferFormatException) {
return _tid;
}
/**
* Check if this is a challenge response, and return realm and nonce
*/
bool isChallenge(std::string &realm, std::string &nonce) const {
bool ret = false;
if(_constructed) {
int err_code;
uint8_t err_msg[1025];
size_t err_msg_size=sizeof(err_msg);
uint8_t srealm[0xFFFF];
uint8_t snonce[0xFFFF];
ret = stun_is_challenge_response_str(_buffer, _sz, &err_code, err_msg, err_msg_size, srealm, snonce, NULL, NULL);
if(ret) {
realm = (char*)srealm;
nonce = (char*)snonce;
}
}
return ret;
}
bool isChallenge() const {
std::string realm, nonce;
return isChallenge(realm, nonce);
}
/**
* Check if this is a success response
*/
bool isSuccess() const {
return (_err == 0);
}
/**
* Construct binding response
*/
void constructBindingResponse(stun_tid &tid,
const ioa_addr &reflexive_addr, int error_code,
const uint8_t *reason) {