/usr/include/turn/TurnMsgLib.h is in coturn 4.2.1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 | /*
* 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(u08bits *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(u08bits *buf, size_t sz, u16bits 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, u16bits 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 u08bits *getRawBuffer(size_t &sz) const throw(WrongStunAttrFormatException) {
int len = stun_attr_get_len(_sar);
if(len<0)
throw WrongStunAttrFormatException();
sz = (size_t)len;
const u08bits *value = stun_attr_get_value(_sar);
return value;
}
friend class StunAttr;
private:
u08bits *_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 u08bits *ptr = iter.getRawBuffer(sz);
if(sz>=0xFFFF)
throw WrongStunAttrFormatException();
int at = iter.getType();
if(at<0)
throw WrongStunAttrFormatException();
_attr_type = (u16bits)at;
_sz = sz;
_value=(u08bits*)turn_malloc(_sz);
if(ptr)
ns_bcopy(ptr,_value,_sz);
}
/**
* Destructor
*/
virtual ~StunAttr() {
if(_value)
turn_free(_value,_sz);
}
/**
* Return raw data representation of the attribute
*/
const u08bits *getRawValue(size_t &sz) const {
sz=_sz;
return _value;
}
/**
* Set raw data value
*/
void setRawValue(u08bits *value, size_t sz) throw(WrongStunAttrFormatException) {
if(sz>0xFFFF)
throw WrongStunAttrFormatException();
if(_value)
turn_free(_value,_sz);
_sz = sz;
_value=(u08bits*)turn_malloc(_sz);
if(value)
ns_bcopy(value,_value,_sz);
}
/**
* Get attribute type
*/
u16bits getType() const {
return _attr_type;
}
/**
* Set attribute type
*/
void setType(u16bits at) {
_attr_type = at;
}
/**
* Add attribute to a message
*/
template<class T>
int addToMsg(T &msg) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
if(!_attr_type)
throw WrongStunAttrFormatException();
u08bits *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(u08bits *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:
u16bits _attr_type;
u08bits *_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() {}
u16bits getChannelNumber() const {
return _cn;
}
void setChannelNumber(u16bits cn) {
_cn = cn;
}
protected:
virtual int addToBuffer(u08bits *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_channel_number_str(buffer,&sz,_cn);
}
private:
u16bits _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() {}
u08bits getEvenPort() const {
return _ep;
}
void setEvenPort(u08bits ep) {
_ep = ep;
}
protected:
virtual int addToBuffer(u08bits *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_str(buffer, &sz, STUN_ATTRIBUTE_EVEN_PORT, &_ep, 1);
}
private:
u08bits _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() {}
u64bits getReservationToken() const {
return _rt;
}
void setReservationToken(u64bits rt) {
_rt = rt;
}
protected:
virtual int addToBuffer(u08bits *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, (u08bits*) (&reservation_token), 8);
}
private:
u64bits _rt;
};
/**
* This attribute class is used for all address attributes
*/
class StunAttrAddr : public StunAttr {
public:
StunAttrAddr(u16bits 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 u08bits *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(u08bits *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(u08bits *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 = (u16bits)rp;
}
virtual ~StunAttrResponsePort() {}
u16bits getResponsePort() const {
return _rp;
}
void setResponsePort(u16bits p) {
_rp = p;
}
protected:
virtual int addToBuffer(u08bits *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_response_port_str(buffer, &sz, _rp);
}
private:
u16bits _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 = (u16bits)p;
}
virtual ~StunAttrPadding() {}
u16bits getPadding() const {
return _p;
}
/**
* Set length of padding
*/
void setPadding(u16bits p) {
_p = p;
}
protected:
virtual int addToBuffer(u08bits *buffer, size_t &sz) throw(WrongStunAttrFormatException, WrongStunBufferFormatException) {
return stun_attr_add_padding_str(buffer, &sz, _p);
}
private:
u16bits _p;
};
/**
* Generic "STUN Message" class, base class for all messages
*/
class StunMsg {
public:
/**
* Empty constructor
*/
StunMsg() {
_allocated_sz = 0xFFFF;
_buffer = (u08bits*)turn_malloc(_allocated_sz);
_deallocate = true;
_sz = 0;
_constructed = 0;
}
/**
* Construct message over raw buffer.
* Parameter "construct" is true if the buffer is initialized.
*/
StunMsg(u08bits *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) {
turn_free(_buffer, _allocated_sz);
}
}
/**
* Initialize buffer
*/
void construct() {
constructBuffer();
}
/**
* Checks if the message is properly constructed
*/
bool isValid() {
return check();
}
/**
* get raw buffer
*/
u08bits *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(u08bits *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(u08bits *buffer, size_t sz) {
return stun_is_indication_str(buffer, sz);
}
static bool isRequest(u08bits *buffer, size_t sz) {
return stun_is_request_str(buffer, sz);
}
static bool isSuccessResponse(u08bits *buffer, size_t sz) {
return stun_is_success_response_str(buffer, sz);
}
static bool isErrorResponse(u08bits *buffer, size_t sz,
int &err_code, u08bits *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 u08bits* buf, size_t sz,
int &err_code, u08bits *err_msg, size_t err_msg_size,
u08bits *realm, u08bits *nonce,
u08bits *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(u08bits *buffer, size_t sz) {
return is_channel_msg_str(buffer, sz);
}
/**
* Check if the fingerprint is present.
*/
static bool isFingerprintPresent(u08bits *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(u08bits *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();
u08bits *suname=(u08bits*)strdup(uname.c_str());
u08bits *srealm=(u08bits*)strdup(realm.c_str());
u08bits *supwd=(u08bits*)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();
u08bits *suname=(u08bits*)strdup(uname.c_str());
u08bits *srealm=(u08bits*)strdup(realm.c_str());
u08bits *supwd=(u08bits*)strdup(upwd.c_str());
u08bits *snonce=(u08bits*)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();
u08bits *suname=(u08bits*)strdup(uname.c_str());
u08bits *supwd=(u08bits*)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:
u08bits *_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(u16bits method) : _method(method) {};
StunMsgRequest(u08bits *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
*/
u16bits getMethod() const {
return _method;
}
/**
* Set method
*/
void setMethod(u16bits 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(u32bits lifetime, int af4, int af6, u08bits transport, int mobile) {
stun_set_allocate_request_str(_buffer, &_sz, lifetime, af4, af6, transport, mobile);
}
/**
* Construct channel bind request
*/
void constructChannelBindRequest(const ioa_addr &peer_addr, u16bits 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:
u16bits _method;
};
/**
* Class for STUN/TURN responses
*/
class StunMsgResponse : public StunMsg {
public:
StunMsgResponse(u16bits method, stun_tid &tid) : _method(method), _err(0), _reason(""), _tid(tid) {};
StunMsgResponse(u16bits method, int error_code, std::string reason, stun_tid &tid) :
_method(method), _err(error_code), _reason(reason), _tid(tid) {
};
StunMsgResponse(u08bits *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)) {
u08bits 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);
}
}
u16bits getMethod() const {
return _method;
}
void setMethod(u16bits 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;
u08bits err_msg[1025];
size_t err_msg_size=sizeof(err_msg);
u08bits srealm[0xFFFF];
u08bits 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 u08bits *reason) {
stun_set_binding_response_str(_buffer, &_sz, &tid,
&reflexive_addr, error_code,
reason, 0 , 0);
}
bool isBindingResponse() const {
return stun_is_binding_response_str(_buffer,_sz);
}
/**
* Construct allocate response
*/
void constructAllocateResponse(stun_tid &tid,
const ioa_addr &relayed_addr1,
const ioa_addr &relayed_addr2,
const ioa_addr &reflexive_addr,
u32bits lifetime, int error_code, const u08bits *reason,
u64bits reservation_token, char *mobile_id) {
stun_set_allocate_response_str(_buffer, &_sz, &tid,
&relayed_addr1, &relayed_addr2,
&reflexive_addr,
lifetime, error_code, reason,
reservation_token, mobile_id);
}
/**
* Construct channel bind response
*/
void constructChannelBindResponse(stun_tid &tid, int error_code, const u08bits *reason) {
stun_set_channel_bind_response_str(_buffer, &_sz, &tid, error_code, reason);
}
protected:
virtual void constructBuffer() {
if(_err) {
stun_init_error_response_str(_method, _buffer, &_sz, _err, (const u08bits*)_reason.c_str(), &_tid);
} else {
stun_init_success_response_str(_method, _buffer, &_sz, &_tid);
}
_constructed = true;
}
virtual bool check() {
if(!_constructed)
return false;
if(!stun_is_success_response_str(_buffer,_sz)) {
u08bits errtxt[0xFFFF];
int cerr=0;
if(!stun_is_error_response_str(_buffer,_sz,&cerr,errtxt,sizeof(errtxt))) {
throw WrongStunBufferFormatException();
}
if(cerr != _err) {
throw WrongStunBufferFormatException();
}
}
if(_method != stun_get_method_str(_buffer,_sz)) {
return false;
}
return true;
}
private:
u16bits _method;
int _err;
std::string _reason;
stun_tid _tid;
};
/**
* Class for STUN/TURN indications
*/
class StunMsgIndication : public StunMsg {
public:
StunMsgIndication(u16bits method) : _method(method) {};
StunMsgIndication(u08bits *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_indication_str(buffer,sz)) {
throw WrongStunBufferFormatException();
}
_method = stun_get_method_str(buffer,sz);
}
}
virtual ~StunMsgIndication() {}
u16bits getMethod() const {
return _method;
}
void setMethod(u16bits method) {
_method = method;
}
protected:
virtual void constructBuffer() {
stun_init_indication_str(_method,_buffer,&_sz);
_constructed = true;
}
virtual bool check() {
if(!_constructed)
return false;
if(!stun_is_indication_str(_buffer,_sz)) {
return false;
}
if(_method != stun_get_method_str(_buffer,_sz)) {
return false;
}
return true;
}
private:
u16bits _method;
};
/**
* Channel message
*/
class StunMsgChannel : public StunMsg {
public:
StunMsgChannel(u16bits cn, int length) : _cn(cn), _len(length) {};
StunMsgChannel(u08bits *buffer, size_t total_sz, size_t sz, bool constructed)
throw(WrongStunBufferFormatException) :
StunMsg(buffer,total_sz,sz,constructed),_cn(0) {
if(constructed) {
if(!stun_is_channel_message_str(buffer,&_sz,&_cn,0)) {
throw WrongStunBufferFormatException();
}
if(_sz>0xFFFF || _sz<4)
throw WrongStunBufferFormatException();
_len = _sz-4;
} else {
if(total_sz>0xFFFF || total_sz<4)
throw WrongStunBufferFormatException();
_len = 0;
}
}
virtual ~StunMsgChannel() {}
u16bits getChannelNumber() const {
return _cn;
}
void setChannelNumber(u16bits cn) {
_cn = cn;
}
/**
* Get length of message itself (excluding the 4 channel number bytes)
*/
size_t getLength() const {
return _len;
}
/**
* Set length of message itself (excluding the 4 channel number bytes)
*/
void setLength(size_t len) {
_len = len;
}
protected:
virtual void constructBuffer() {
stun_init_channel_message_str(_cn,_buffer,&_sz,(int)_len,0);
_constructed = true;
}
virtual bool check() {
if(!_constructed)
return false;
u16bits cn = 0;
if(!stun_is_channel_message_str(_buffer,&_sz,&cn,0)) {
return false;
}
if(_cn != cn) {
return false;
}
return true;
}
private:
u16bits _cn;
size_t _len;
};
};
/* namespace */
#endif
/* __LIB_TURN_MSG_CPP__ */
|