📄 stun.cc
字号:
StunByteStringAttribute* StunAttribute::CreateByteString(uint16 type) { switch (type) { case STUN_ATTR_USERNAME: case STUN_ATTR_PASSWORD: case STUN_ATTR_MESSAGE_INTEGRITY: case STUN_ATTR_DATA: case STUN_ATTR_MAGIC_COOKIE: return new StunByteStringAttribute(type, 0); default: assert(false); return 0; }}StunErrorCodeAttribute* StunAttribute::CreateErrorCode() { return new StunErrorCodeAttribute( STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE);}StunUInt16ListAttribute* StunAttribute::CreateUnknownAttributes() { return new StunUInt16ListAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES, 0);}StunTransportPrefsAttribute* StunAttribute::CreateTransportPrefs() { return new StunTransportPrefsAttribute( STUN_ATTR_TRANSPORT_PREFERENCES, StunTransportPrefsAttribute::SIZE1);}StunAddressAttribute::StunAddressAttribute(uint16 type) : StunAttribute(type, SIZE), family_(0), port_(0), ip_(0) {}bool StunAddressAttribute::Read(ByteBuffer* buf) { uint8 dummy; if (!buf->ReadUInt8(dummy)) return false; if (!buf->ReadUInt8(family_)) return false; if (!buf->ReadUInt16(port_)) return false; if (!buf->ReadUInt32(ip_)) return false; return true;}void StunAddressAttribute::Write(ByteBuffer* buf) const { buf->WriteUInt8(0); buf->WriteUInt8(family_); buf->WriteUInt16(port_); buf->WriteUInt32(ip_);}StunUInt32Attribute::StunUInt32Attribute(uint16 type) : StunAttribute(type, SIZE), bits_(0) {}bool StunUInt32Attribute::GetBit(int index) const { assert((0 <= index) && (index < 32)); return static_cast<bool>((bits_ >> index) & 0x1);}void StunUInt32Attribute::SetBit(int index, bool value) { assert((0 <= index) && (index < 32)); bits_ &= ~(1 << index); bits_ |= value ? (1 << index) : 0;}bool StunUInt32Attribute::Read(ByteBuffer* buf) { if (!buf->ReadUInt32(bits_)) return false; return true;}void StunUInt32Attribute::Write(ByteBuffer* buf) const { buf->WriteUInt32(bits_);}StunByteStringAttribute::StunByteStringAttribute(uint16 type, uint16 length) : StunAttribute(type, length), bytes_(0) {}StunByteStringAttribute::~StunByteStringAttribute() { delete [] bytes_;}void StunByteStringAttribute::SetBytes(char* bytes, uint16 length) { delete [] bytes_; bytes_ = bytes; SetLength(length);}void StunByteStringAttribute::CopyBytes(const char* bytes) { CopyBytes(bytes, (uint16)strlen(bytes));}void StunByteStringAttribute::CopyBytes(const void* bytes, uint16 length) { char* new_bytes = new char[length]; std::memcpy(new_bytes, bytes, length); SetBytes(new_bytes, length);}uint8 StunByteStringAttribute::GetByte(int index) const { assert(bytes_); assert((0 <= index) && (index < length())); return static_cast<uint8>(bytes_[index]);}void StunByteStringAttribute::SetByte(int index, uint8 value) { assert(bytes_); assert((0 <= index) && (index < length())); bytes_[index] = value;}bool StunByteStringAttribute::Read(ByteBuffer* buf) { bytes_ = new char[length()]; if (!buf->ReadBytes(bytes_, length())) return false; return true;}void StunByteStringAttribute::Write(ByteBuffer* buf) const { buf->WriteBytes(bytes_, length());}StunErrorCodeAttribute::StunErrorCodeAttribute(uint16 type, uint16 length) : StunAttribute(type, length), class_(0), number_(0) {}StunErrorCodeAttribute::~StunErrorCodeAttribute() {}void StunErrorCodeAttribute::SetErrorCode(uint32 code) { class_ = (uint8)((code >> 8) & 0x7); number_ = (uint8)(code & 0xff);}void StunErrorCodeAttribute::SetReason(const std::string& reason) { SetLength(MIN_SIZE + (uint16)reason.size()); reason_ = reason;}bool StunErrorCodeAttribute::Read(ByteBuffer* buf) { uint32 val; if (!buf->ReadUInt32(val)) return false; if ((val >> 11) != 0) LOG(LERROR) << "error-code bits not zero"; SetErrorCode(val); if (!buf->ReadString(reason_, length() - 4)) return false; return true;}void StunErrorCodeAttribute::Write(ByteBuffer* buf) const { buf->WriteUInt32(error_code()); buf->WriteString(reason_);}StunUInt16ListAttribute::StunUInt16ListAttribute(uint16 type, uint16 length) : StunAttribute(type, length) { attr_types_ = new std::vector<uint16>();}StunUInt16ListAttribute::~StunUInt16ListAttribute() { delete attr_types_;}size_t StunUInt16ListAttribute::Size() const { return attr_types_->size();}uint16 StunUInt16ListAttribute::GetType(int index) const { return (*attr_types_)[index];}void StunUInt16ListAttribute::SetType(int index, uint16 value) { (*attr_types_)[index] = value;}void StunUInt16ListAttribute::AddType(uint16 value) { attr_types_->push_back(value); SetLength((uint16)attr_types_->size() * 2);}bool StunUInt16ListAttribute::Read(ByteBuffer* buf) { for (int i = 0; i < length() / 2; i++) { uint16 attr; if (!buf->ReadUInt16(attr)) return false; attr_types_->push_back(attr); } return true;}void StunUInt16ListAttribute::Write(ByteBuffer* buf) const { for (unsigned i = 0; i < attr_types_->size(); i++) buf->WriteUInt16((*attr_types_)[i]);}StunTransportPrefsAttribute::StunTransportPrefsAttribute( uint16 type, uint16 length) : StunAttribute(type, length), preallocate_(false), prefs_(0), addr_(0) { }StunTransportPrefsAttribute::~StunTransportPrefsAttribute() { delete addr_;}void StunTransportPrefsAttribute::SetPreallocateAddress( StunAddressAttribute* addr) { if (!addr) { preallocate_ = false; addr_ = 0; SetLength(SIZE1); } else { preallocate_ = true; addr_ = addr; SetLength(SIZE2); }}bool StunTransportPrefsAttribute::Read(ByteBuffer* buf) { uint32 val; if (!buf->ReadUInt32(val)) return false; if ((val >> 3) != 0) LOG(LERROR) << "transport-preferences bits not zero"; preallocate_ = static_cast<bool>((val >> 2) & 0x1); prefs_ = (uint8)(val & 0x3); if (preallocate_ && (prefs_ == 3)) LOG(LERROR) << "transport-preferences imcompatible P and Typ"; if (!preallocate_) { if (length() != StunUInt32Attribute::SIZE) return false; } else { if (length() != StunUInt32Attribute::SIZE + StunAddressAttribute::SIZE) return false; addr_ = new StunAddressAttribute(STUN_ATTR_SOURCE_ADDRESS); addr_->Read(buf); } return true;}void StunTransportPrefsAttribute::Write(ByteBuffer* buf) const { buf->WriteUInt32((preallocate_ ? 4 : 0) | prefs_); if (preallocate_) addr_->Write(buf);}StunMessageType GetStunResponseType(StunMessageType request_type) { switch (request_type) { case STUN_SHARED_SECRET_REQUEST: return STUN_SHARED_SECRET_RESPONSE; case STUN_ALLOCATE_REQUEST: return STUN_ALLOCATE_RESPONSE; case STUN_SEND_REQUEST: return STUN_SEND_RESPONSE; default: return STUN_BINDING_RESPONSE; }}StunMessageType GetStunErrorResponseType(StunMessageType request_type) { switch (request_type) { case STUN_SHARED_SECRET_REQUEST: return STUN_SHARED_SECRET_ERROR_RESPONSE; case STUN_ALLOCATE_REQUEST: return STUN_ALLOCATE_ERROR_RESPONSE; case STUN_SEND_REQUEST: return STUN_SEND_ERROR_RESPONSE; default: return STUN_BINDING_ERROR_RESPONSE; }}} // namespace cricket
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -