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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 class StunErrorCodeAttribute; | 125 class StunErrorCodeAttribute; |
126 class StunUInt16ListAttribute; | 126 class StunUInt16ListAttribute; |
127 | 127 |
128 // Records a complete STUN/TURN message. Each message consists of a type and | 128 // Records a complete STUN/TURN message. Each message consists of a type and |
129 // any number of attributes. Each attribute is parsed into an instance of an | 129 // any number of attributes. Each attribute is parsed into an instance of an |
130 // appropriate class (see above). The Get* methods will return instances of | 130 // appropriate class (see above). The Get* methods will return instances of |
131 // that attribute class. | 131 // that attribute class. |
132 class StunMessage { | 132 class StunMessage { |
133 public: | 133 public: |
134 StunMessage(); | 134 StunMessage(); |
135 virtual ~StunMessage(); | 135 virtual ~StunMessage() = default; |
136 | 136 |
137 int type() const { return type_; } | 137 int type() const { return type_; } |
138 size_t length() const { return length_; } | 138 size_t length() const { return length_; } |
139 const std::string& transaction_id() const { return transaction_id_; } | 139 const std::string& transaction_id() const { return transaction_id_; } |
140 | 140 |
141 // Returns true if the message confirms to RFC3489 rather than | 141 // Returns true if the message confirms to RFC3489 rather than |
142 // RFC5389. The main difference between two version of the STUN | 142 // RFC5389. The main difference between two version of the STUN |
143 // protocol is the presence of the magic cookie and different length | 143 // protocol is the presence of the magic cookie and different length |
144 // of transaction ID. For outgoing packets version of the protocol | 144 // of transaction ID. For outgoing packets version of the protocol |
145 // is determined by the lengths of the transaction ID. | 145 // is determined by the lengths of the transaction ID. |
146 bool IsLegacy() const; | 146 bool IsLegacy() const; |
147 | 147 |
148 void SetType(int type) { type_ = static_cast<uint16_t>(type); } | 148 void SetType(int type) { type_ = static_cast<uint16_t>(type); } |
149 bool SetTransactionID(const std::string& str); | 149 bool SetTransactionID(const std::string& str); |
150 | 150 |
151 // Gets the desired attribute value, or NULL if no such attribute type exists. | 151 // Gets the desired attribute value, or NULL if no such attribute type exists. |
152 const StunAddressAttribute* GetAddress(int type) const; | 152 const StunAddressAttribute* GetAddress(int type) const; |
153 const StunUInt32Attribute* GetUInt32(int type) const; | 153 const StunUInt32Attribute* GetUInt32(int type) const; |
154 const StunUInt64Attribute* GetUInt64(int type) const; | 154 const StunUInt64Attribute* GetUInt64(int type) const; |
155 const StunByteStringAttribute* GetByteString(int type) const; | 155 const StunByteStringAttribute* GetByteString(int type) const; |
156 | 156 |
157 // Gets these specific attribute values. | 157 // Gets these specific attribute values. |
158 const StunErrorCodeAttribute* GetErrorCode() const; | 158 const StunErrorCodeAttribute* GetErrorCode() const; |
159 const StunUInt16ListAttribute* GetUnknownAttributes() const; | 159 const StunUInt16ListAttribute* GetUnknownAttributes() const; |
160 | 160 |
161 // Takes ownership of the specified attribute and adds it to the message. | 161 // Takes ownership of the specified attribute and adds it to the message. |
| 162 // TODO(zstein): Take a unique_ptr instead of a raw pointer. |
162 void AddAttribute(StunAttribute* attr); | 163 void AddAttribute(StunAttribute* attr); |
163 | 164 |
164 // Validates that a raw STUN message has a correct MESSAGE-INTEGRITY value. | 165 // Validates that a raw STUN message has a correct MESSAGE-INTEGRITY value. |
165 // This can't currently be done on a StunMessage, since it is affected by | 166 // This can't currently be done on a StunMessage, since it is affected by |
166 // padding data (which we discard when reading a StunMessage). | 167 // padding data (which we discard when reading a StunMessage). |
167 static bool ValidateMessageIntegrity(const char* data, size_t size, | 168 static bool ValidateMessageIntegrity(const char* data, size_t size, |
168 const std::string& password); | 169 const std::string& password); |
169 // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message. | 170 // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message. |
170 bool AddMessageIntegrity(const std::string& password); | 171 bool AddMessageIntegrity(const std::string& password); |
171 bool AddMessageIntegrity(const char* key, size_t keylen); | 172 bool AddMessageIntegrity(const char* key, size_t keylen); |
(...skipping 20 matching lines...) Expand all Loading... |
192 virtual StunAttributeValueType GetAttributeValueType(int type) const; | 193 virtual StunAttributeValueType GetAttributeValueType(int type) const; |
193 | 194 |
194 private: | 195 private: |
195 StunAttribute* CreateAttribute(int type, size_t length) /* const*/; | 196 StunAttribute* CreateAttribute(int type, size_t length) /* const*/; |
196 const StunAttribute* GetAttribute(int type) const; | 197 const StunAttribute* GetAttribute(int type) const; |
197 static bool IsValidTransactionId(const std::string& transaction_id); | 198 static bool IsValidTransactionId(const std::string& transaction_id); |
198 | 199 |
199 uint16_t type_; | 200 uint16_t type_; |
200 uint16_t length_; | 201 uint16_t length_; |
201 std::string transaction_id_; | 202 std::string transaction_id_; |
202 std::vector<StunAttribute*>* attrs_; | 203 std::vector<std::unique_ptr<StunAttribute>> attrs_; |
203 }; | 204 }; |
204 | 205 |
205 // Base class for all STUN/TURN attributes. | 206 // Base class for all STUN/TURN attributes. |
206 class StunAttribute { | 207 class StunAttribute { |
207 public: | 208 public: |
208 virtual ~StunAttribute() { | 209 virtual ~StunAttribute() { |
209 } | 210 } |
210 | 211 |
211 int type() const { return type_; } | 212 int type() const { return type_; } |
212 size_t length() const { return length_; } | 213 size_t length() const { return length_; } |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 case STUN_ATTR_ICE_CONTROLLING: return STUN_VALUE_UINT64; | 631 case STUN_ATTR_ICE_CONTROLLING: return STUN_VALUE_UINT64; |
631 default: return StunMessage::GetAttributeValueType(type); | 632 default: return StunMessage::GetAttributeValueType(type); |
632 } | 633 } |
633 } | 634 } |
634 virtual StunMessage* CreateNew() const { return new IceMessage(); } | 635 virtual StunMessage* CreateNew() const { return new IceMessage(); } |
635 }; | 636 }; |
636 | 637 |
637 } // namespace cricket | 638 } // namespace cricket |
638 | 639 |
639 #endif // WEBRTC_P2P_BASE_STUN_H_ | 640 #endif // WEBRTC_P2P_BASE_STUN_H_ |
OLD | NEW |