| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 bool AddMessageIntegrity(const char* key, size_t keylen); | 173 bool AddMessageIntegrity(const char* key, size_t keylen); |
| 174 | 174 |
| 175 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT. | 175 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT. |
| 176 static bool ValidateFingerprint(const char* data, size_t size); | 176 static bool ValidateFingerprint(const char* data, size_t size); |
| 177 | 177 |
| 178 // Adds a FINGERPRINT attribute that is valid for the current message. | 178 // Adds a FINGERPRINT attribute that is valid for the current message. |
| 179 bool AddFingerprint(); | 179 bool AddFingerprint(); |
| 180 | 180 |
| 181 // Parses the STUN packet in the given buffer and records it here. The | 181 // Parses the STUN packet in the given buffer and records it here. The |
| 182 // return value indicates whether this was successful. | 182 // return value indicates whether this was successful. |
| 183 bool Read(rtc::ByteBuffer* buf); | 183 bool Read(rtc::ByteBufferReader* buf); |
| 184 | 184 |
| 185 // Writes this object into a STUN packet. The return value indicates whether | 185 // Writes this object into a STUN packet. The return value indicates whether |
| 186 // this was successful. | 186 // this was successful. |
| 187 bool Write(rtc::ByteBuffer* buf) const; | 187 bool Write(rtc::ByteBufferWriter* buf) const; |
| 188 | 188 |
| 189 // Creates an empty message. Overridable by derived classes. | 189 // Creates an empty message. Overridable by derived classes. |
| 190 virtual StunMessage* CreateNew() const { return new StunMessage(); } | 190 virtual StunMessage* CreateNew() const { return new StunMessage(); } |
| 191 | 191 |
| 192 protected: | 192 protected: |
| 193 // Verifies that the given attribute is allowed for this message. | 193 // Verifies that the given attribute is allowed for this message. |
| 194 virtual StunAttributeValueType GetAttributeValueType(int type) const; | 194 virtual StunAttributeValueType GetAttributeValueType(int type) const; |
| 195 | 195 |
| 196 private: | 196 private: |
| 197 StunAttribute* CreateAttribute(int type, size_t length) /* const*/; | 197 StunAttribute* CreateAttribute(int type, size_t length) /* const*/; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 214 size_t length() const { return length_; } | 214 size_t length() const { return length_; } |
| 215 | 215 |
| 216 // Return the type of this attribute. | 216 // Return the type of this attribute. |
| 217 virtual StunAttributeValueType value_type() const = 0; | 217 virtual StunAttributeValueType value_type() const = 0; |
| 218 | 218 |
| 219 // Only XorAddressAttribute needs this so far. | 219 // Only XorAddressAttribute needs this so far. |
| 220 virtual void SetOwner(StunMessage* owner) {} | 220 virtual void SetOwner(StunMessage* owner) {} |
| 221 | 221 |
| 222 // Reads the body (not the type or length) for this type of attribute from | 222 // Reads the body (not the type or length) for this type of attribute from |
| 223 // the given buffer. Return value is true if successful. | 223 // the given buffer. Return value is true if successful. |
| 224 virtual bool Read(rtc::ByteBuffer* buf) = 0; | 224 virtual bool Read(rtc::ByteBufferReader* buf) = 0; |
| 225 | 225 |
| 226 // Writes the body (not the type or length) to the given buffer. Return | 226 // Writes the body (not the type or length) to the given buffer. Return |
| 227 // value is true if successful. | 227 // value is true if successful. |
| 228 virtual bool Write(rtc::ByteBuffer* buf) const = 0; | 228 virtual bool Write(rtc::ByteBufferWriter* buf) const = 0; |
| 229 | 229 |
| 230 // Creates an attribute object with the given type and smallest length. | 230 // Creates an attribute object with the given type and smallest length. |
| 231 static StunAttribute* Create(StunAttributeValueType value_type, | 231 static StunAttribute* Create(StunAttributeValueType value_type, |
| 232 uint16_t type, | 232 uint16_t type, |
| 233 uint16_t length, | 233 uint16_t length, |
| 234 StunMessage* owner); | 234 StunMessage* owner); |
| 235 // TODO: Allow these create functions to take parameters, to reduce | 235 // TODO: Allow these create functions to take parameters, to reduce |
| 236 // the amount of work callers need to do to initialize attributes. | 236 // the amount of work callers need to do to initialize attributes. |
| 237 static StunAddressAttribute* CreateAddress(uint16_t type); | 237 static StunAddressAttribute* CreateAddress(uint16_t type); |
| 238 static StunXorAddressAttribute* CreateXorAddress(uint16_t type); | 238 static StunXorAddressAttribute* CreateXorAddress(uint16_t type); |
| 239 static StunUInt32Attribute* CreateUInt32(uint16_t type); | 239 static StunUInt32Attribute* CreateUInt32(uint16_t type); |
| 240 static StunUInt64Attribute* CreateUInt64(uint16_t type); | 240 static StunUInt64Attribute* CreateUInt64(uint16_t type); |
| 241 static StunByteStringAttribute* CreateByteString(uint16_t type); | 241 static StunByteStringAttribute* CreateByteString(uint16_t type); |
| 242 static StunErrorCodeAttribute* CreateErrorCode(); | 242 static StunErrorCodeAttribute* CreateErrorCode(); |
| 243 static StunUInt16ListAttribute* CreateUnknownAttributes(); | 243 static StunUInt16ListAttribute* CreateUnknownAttributes(); |
| 244 | 244 |
| 245 protected: | 245 protected: |
| 246 StunAttribute(uint16_t type, uint16_t length); | 246 StunAttribute(uint16_t type, uint16_t length); |
| 247 void SetLength(uint16_t length) { length_ = length; } | 247 void SetLength(uint16_t length) { length_ = length; } |
| 248 void WritePadding(rtc::ByteBuffer* buf) const; | 248 void WritePadding(rtc::ByteBufferWriter* buf) const; |
| 249 void ConsumePadding(rtc::ByteBuffer* buf) const; | 249 void ConsumePadding(rtc::ByteBufferReader* buf) const; |
| 250 | 250 |
| 251 private: | 251 private: |
| 252 uint16_t type_; | 252 uint16_t type_; |
| 253 uint16_t length_; | 253 uint16_t length_; |
| 254 }; | 254 }; |
| 255 | 255 |
| 256 // Implements STUN attributes that record an Internet address. | 256 // Implements STUN attributes that record an Internet address. |
| 257 class StunAddressAttribute : public StunAttribute { | 257 class StunAddressAttribute : public StunAttribute { |
| 258 public: | 258 public: |
| 259 static const uint16_t SIZE_UNDEF = 0; | 259 static const uint16_t SIZE_UNDEF = 0; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 283 void SetAddress(const rtc::SocketAddress& addr) { | 283 void SetAddress(const rtc::SocketAddress& addr) { |
| 284 address_ = addr; | 284 address_ = addr; |
| 285 EnsureAddressLength(); | 285 EnsureAddressLength(); |
| 286 } | 286 } |
| 287 void SetIP(const rtc::IPAddress& ip) { | 287 void SetIP(const rtc::IPAddress& ip) { |
| 288 address_.SetIP(ip); | 288 address_.SetIP(ip); |
| 289 EnsureAddressLength(); | 289 EnsureAddressLength(); |
| 290 } | 290 } |
| 291 void SetPort(uint16_t port) { address_.SetPort(port); } | 291 void SetPort(uint16_t port) { address_.SetPort(port); } |
| 292 | 292 |
| 293 virtual bool Read(rtc::ByteBuffer* buf); | 293 virtual bool Read(rtc::ByteBufferReader* buf); |
| 294 virtual bool Write(rtc::ByteBuffer* buf) const; | 294 virtual bool Write(rtc::ByteBufferWriter* buf) const; |
| 295 | 295 |
| 296 private: | 296 private: |
| 297 void EnsureAddressLength() { | 297 void EnsureAddressLength() { |
| 298 switch (family()) { | 298 switch (family()) { |
| 299 case STUN_ADDRESS_IPV4: { | 299 case STUN_ADDRESS_IPV4: { |
| 300 SetLength(SIZE_IP4); | 300 SetLength(SIZE_IP4); |
| 301 break; | 301 break; |
| 302 } | 302 } |
| 303 case STUN_ADDRESS_IPV6: { | 303 case STUN_ADDRESS_IPV6: { |
| 304 SetLength(SIZE_IP6); | 304 SetLength(SIZE_IP6); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 320 public: | 320 public: |
| 321 StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr); | 321 StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr); |
| 322 StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner); | 322 StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner); |
| 323 | 323 |
| 324 virtual StunAttributeValueType value_type() const { | 324 virtual StunAttributeValueType value_type() const { |
| 325 return STUN_VALUE_XOR_ADDRESS; | 325 return STUN_VALUE_XOR_ADDRESS; |
| 326 } | 326 } |
| 327 virtual void SetOwner(StunMessage* owner) { | 327 virtual void SetOwner(StunMessage* owner) { |
| 328 owner_ = owner; | 328 owner_ = owner; |
| 329 } | 329 } |
| 330 virtual bool Read(rtc::ByteBuffer* buf); | 330 virtual bool Read(rtc::ByteBufferReader* buf); |
| 331 virtual bool Write(rtc::ByteBuffer* buf) const; | 331 virtual bool Write(rtc::ByteBufferWriter* buf) const; |
| 332 | 332 |
| 333 private: | 333 private: |
| 334 rtc::IPAddress GetXoredIP() const; | 334 rtc::IPAddress GetXoredIP() const; |
| 335 StunMessage* owner_; | 335 StunMessage* owner_; |
| 336 }; | 336 }; |
| 337 | 337 |
| 338 // Implements STUN attributes that record a 32-bit integer. | 338 // Implements STUN attributes that record a 32-bit integer. |
| 339 class StunUInt32Attribute : public StunAttribute { | 339 class StunUInt32Attribute : public StunAttribute { |
| 340 public: | 340 public: |
| 341 static const uint16_t SIZE = 4; | 341 static const uint16_t SIZE = 4; |
| 342 StunUInt32Attribute(uint16_t type, uint32_t value); | 342 StunUInt32Attribute(uint16_t type, uint32_t value); |
| 343 explicit StunUInt32Attribute(uint16_t type); | 343 explicit StunUInt32Attribute(uint16_t type); |
| 344 | 344 |
| 345 virtual StunAttributeValueType value_type() const { | 345 virtual StunAttributeValueType value_type() const { |
| 346 return STUN_VALUE_UINT32; | 346 return STUN_VALUE_UINT32; |
| 347 } | 347 } |
| 348 | 348 |
| 349 uint32_t value() const { return bits_; } | 349 uint32_t value() const { return bits_; } |
| 350 void SetValue(uint32_t bits) { bits_ = bits; } | 350 void SetValue(uint32_t bits) { bits_ = bits; } |
| 351 | 351 |
| 352 bool GetBit(size_t index) const; | 352 bool GetBit(size_t index) const; |
| 353 void SetBit(size_t index, bool value); | 353 void SetBit(size_t index, bool value); |
| 354 | 354 |
| 355 virtual bool Read(rtc::ByteBuffer* buf); | 355 virtual bool Read(rtc::ByteBufferReader* buf); |
| 356 virtual bool Write(rtc::ByteBuffer* buf) const; | 356 virtual bool Write(rtc::ByteBufferWriter* buf) const; |
| 357 | 357 |
| 358 private: | 358 private: |
| 359 uint32_t bits_; | 359 uint32_t bits_; |
| 360 }; | 360 }; |
| 361 | 361 |
| 362 class StunUInt64Attribute : public StunAttribute { | 362 class StunUInt64Attribute : public StunAttribute { |
| 363 public: | 363 public: |
| 364 static const uint16_t SIZE = 8; | 364 static const uint16_t SIZE = 8; |
| 365 StunUInt64Attribute(uint16_t type, uint64_t value); | 365 StunUInt64Attribute(uint16_t type, uint64_t value); |
| 366 explicit StunUInt64Attribute(uint16_t type); | 366 explicit StunUInt64Attribute(uint16_t type); |
| 367 | 367 |
| 368 virtual StunAttributeValueType value_type() const { | 368 virtual StunAttributeValueType value_type() const { |
| 369 return STUN_VALUE_UINT64; | 369 return STUN_VALUE_UINT64; |
| 370 } | 370 } |
| 371 | 371 |
| 372 uint64_t value() const { return bits_; } | 372 uint64_t value() const { return bits_; } |
| 373 void SetValue(uint64_t bits) { bits_ = bits; } | 373 void SetValue(uint64_t bits) { bits_ = bits; } |
| 374 | 374 |
| 375 virtual bool Read(rtc::ByteBuffer* buf); | 375 virtual bool Read(rtc::ByteBufferReader* buf); |
| 376 virtual bool Write(rtc::ByteBuffer* buf) const; | 376 virtual bool Write(rtc::ByteBufferWriter* buf) const; |
| 377 | 377 |
| 378 private: | 378 private: |
| 379 uint64_t bits_; | 379 uint64_t bits_; |
| 380 }; | 380 }; |
| 381 | 381 |
| 382 // Implements STUN attributes that record an arbitrary byte string. | 382 // Implements STUN attributes that record an arbitrary byte string. |
| 383 class StunByteStringAttribute : public StunAttribute { | 383 class StunByteStringAttribute : public StunAttribute { |
| 384 public: | 384 public: |
| 385 explicit StunByteStringAttribute(uint16_t type); | 385 explicit StunByteStringAttribute(uint16_t type); |
| 386 StunByteStringAttribute(uint16_t type, const std::string& str); | 386 StunByteStringAttribute(uint16_t type, const std::string& str); |
| 387 StunByteStringAttribute(uint16_t type, const void* bytes, size_t length); | 387 StunByteStringAttribute(uint16_t type, const void* bytes, size_t length); |
| 388 StunByteStringAttribute(uint16_t type, uint16_t length); | 388 StunByteStringAttribute(uint16_t type, uint16_t length); |
| 389 ~StunByteStringAttribute(); | 389 ~StunByteStringAttribute(); |
| 390 | 390 |
| 391 virtual StunAttributeValueType value_type() const { | 391 virtual StunAttributeValueType value_type() const { |
| 392 return STUN_VALUE_BYTE_STRING; | 392 return STUN_VALUE_BYTE_STRING; |
| 393 } | 393 } |
| 394 | 394 |
| 395 const char* bytes() const { return bytes_; } | 395 const char* bytes() const { return bytes_; } |
| 396 std::string GetString() const { return std::string(bytes_, length()); } | 396 std::string GetString() const { return std::string(bytes_, length()); } |
| 397 | 397 |
| 398 void CopyBytes(const char* bytes); // uses strlen | 398 void CopyBytes(const char* bytes); // uses strlen |
| 399 void CopyBytes(const void* bytes, size_t length); | 399 void CopyBytes(const void* bytes, size_t length); |
| 400 | 400 |
| 401 uint8_t GetByte(size_t index) const; | 401 uint8_t GetByte(size_t index) const; |
| 402 void SetByte(size_t index, uint8_t value); | 402 void SetByte(size_t index, uint8_t value); |
| 403 | 403 |
| 404 virtual bool Read(rtc::ByteBuffer* buf); | 404 virtual bool Read(rtc::ByteBufferReader* buf); |
| 405 virtual bool Write(rtc::ByteBuffer* buf) const; | 405 virtual bool Write(rtc::ByteBufferWriter* buf) const; |
| 406 | 406 |
| 407 private: | 407 private: |
| 408 void SetBytes(char* bytes, size_t length); | 408 void SetBytes(char* bytes, size_t length); |
| 409 | 409 |
| 410 char* bytes_; | 410 char* bytes_; |
| 411 }; | 411 }; |
| 412 | 412 |
| 413 // Implements STUN attributes that record an error code. | 413 // Implements STUN attributes that record an error code. |
| 414 class StunErrorCodeAttribute : public StunAttribute { | 414 class StunErrorCodeAttribute : public StunAttribute { |
| 415 public: | 415 public: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 427 void SetCode(int code); | 427 void SetCode(int code); |
| 428 | 428 |
| 429 // The individual error components. | 429 // The individual error components. |
| 430 int eclass() const { return class_; } | 430 int eclass() const { return class_; } |
| 431 int number() const { return number_; } | 431 int number() const { return number_; } |
| 432 const std::string& reason() const { return reason_; } | 432 const std::string& reason() const { return reason_; } |
| 433 void SetClass(uint8_t eclass) { class_ = eclass; } | 433 void SetClass(uint8_t eclass) { class_ = eclass; } |
| 434 void SetNumber(uint8_t number) { number_ = number; } | 434 void SetNumber(uint8_t number) { number_ = number; } |
| 435 void SetReason(const std::string& reason); | 435 void SetReason(const std::string& reason); |
| 436 | 436 |
| 437 bool Read(rtc::ByteBuffer* buf); | 437 bool Read(rtc::ByteBufferReader* buf); |
| 438 bool Write(rtc::ByteBuffer* buf) const; | 438 bool Write(rtc::ByteBufferWriter* buf) const; |
| 439 | 439 |
| 440 private: | 440 private: |
| 441 uint8_t class_; | 441 uint8_t class_; |
| 442 uint8_t number_; | 442 uint8_t number_; |
| 443 std::string reason_; | 443 std::string reason_; |
| 444 }; | 444 }; |
| 445 | 445 |
| 446 // Implements STUN attributes that record a list of attribute names. | 446 // Implements STUN attributes that record a list of attribute names. |
| 447 class StunUInt16ListAttribute : public StunAttribute { | 447 class StunUInt16ListAttribute : public StunAttribute { |
| 448 public: | 448 public: |
| 449 StunUInt16ListAttribute(uint16_t type, uint16_t length); | 449 StunUInt16ListAttribute(uint16_t type, uint16_t length); |
| 450 ~StunUInt16ListAttribute(); | 450 ~StunUInt16ListAttribute(); |
| 451 | 451 |
| 452 virtual StunAttributeValueType value_type() const { | 452 virtual StunAttributeValueType value_type() const { |
| 453 return STUN_VALUE_UINT16_LIST; | 453 return STUN_VALUE_UINT16_LIST; |
| 454 } | 454 } |
| 455 | 455 |
| 456 size_t Size() const; | 456 size_t Size() const; |
| 457 uint16_t GetType(int index) const; | 457 uint16_t GetType(int index) const; |
| 458 void SetType(int index, uint16_t value); | 458 void SetType(int index, uint16_t value); |
| 459 void AddType(uint16_t value); | 459 void AddType(uint16_t value); |
| 460 | 460 |
| 461 bool Read(rtc::ByteBuffer* buf); | 461 bool Read(rtc::ByteBufferReader* buf); |
| 462 bool Write(rtc::ByteBuffer* buf) const; | 462 bool Write(rtc::ByteBufferWriter* buf) const; |
| 463 | 463 |
| 464 private: | 464 private: |
| 465 std::vector<uint16_t>* attr_types_; | 465 std::vector<uint16_t>* attr_types_; |
| 466 }; | 466 }; |
| 467 | 467 |
| 468 // Returns the (successful) response type for the given request type. | 468 // Returns the (successful) response type for the given request type. |
| 469 // Returns -1 if |request_type| is not a valid request type. | 469 // Returns -1 if |request_type| is not a valid request type. |
| 470 int GetStunSuccessResponseType(int request_type); | 470 int GetStunSuccessResponseType(int request_type); |
| 471 | 471 |
| 472 // Returns the error response type for the given request type. | 472 // Returns the error response type for the given request type. |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 case STUN_ATTR_ICE_CONTROLLING: return STUN_VALUE_UINT64; | 628 case STUN_ATTR_ICE_CONTROLLING: return STUN_VALUE_UINT64; |
| 629 default: return StunMessage::GetAttributeValueType(type); | 629 default: return StunMessage::GetAttributeValueType(type); |
| 630 } | 630 } |
| 631 } | 631 } |
| 632 virtual StunMessage* CreateNew() const { return new IceMessage(); } | 632 virtual StunMessage* CreateNew() const { return new IceMessage(); } |
| 633 }; | 633 }; |
| 634 | 634 |
| 635 } // namespace cricket | 635 } // namespace cricket |
| 636 | 636 |
| 637 #endif // WEBRTC_P2P_BASE_STUN_H_ | 637 #endif // WEBRTC_P2P_BASE_STUN_H_ |
| OLD | NEW |