| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef P2P_BASE_STUN_DICTIONARY_H_ |
| 12 | #define P2P_BASE_STUN_DICTIONARY_H_ |
| 13 |
| 14 | #include <deque> |
| 15 | #include <map> |
| 16 | #include <memory> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 |
| 20 | #include "api/rtc_error.h" |
| 21 | #include "api/transport/stun.h" |
| 22 |
| 23 | namespace cricket { |
| 24 |
| 25 | // A StunDictionaryView is a dictionary of StunAttributes. |
| 26 | // - the StunAttributes can be read using the |Get|-methods. |
| 27 | // - the dictionary is updated by using the |ApplyDelta|-method. |
| 28 | // |
| 29 | // A StunDictionaryWriter is used to create |delta|s for the |ApplyDelta|-method |
| 30 | // - It keeps track of which updates has been applied at StunDictionaryView. |
| 31 | // - It optionally keeps a local StunDictionaryView contains modification made |
| 32 | // `locally` |
| 33 | // |
| 34 | // A pair StunDictionaryView(A)/StunDictionaryWriter(B) are linked so that |
| 35 | // modifications to B is transfered to A using the STUN_ATTR_GOOG_DELTA |
| 36 | // (StunByteStringAttribute) and the modification is ack:ed using |
| 37 | // STUN_ATTR_GOOG_DELTA_ACK (StunUInt64Attribute). |
| 38 | // |
| 39 | // Note: |
| 40 | // 1) It is possible to update one StunDictionaryView from multiple writers, |
| 41 | // but this only works of the different writers write disjoint keys (which |
| 42 | // is not checked/enforced by these classes). |
| 43 | // 2) The opposite, one writer updating multiple StunDictionaryView, is not |
| 44 | // possible. |
| 45 | class StunDictionaryView { |
| 46 | public: |
| 47 | // A reserved key used to transport the version number |
| 48 | static constexpr uint16_t kVersionKey = 0xFFFF; |
| 49 |
| 50 | // A magic number used when transporting deltas. |
| 51 | static constexpr uint16_t kDeltaMagic = 0x7788; |
| 52 |
| 53 | // The version number for the delta format. |
| 54 | static constexpr uint16_t kDeltaVersion = 0x1; |
| 55 |
| 56 | // Gets the desired attribute value, or NULL if no such attribute type exists. |
| 57 | // The pointer returned is guaranteed to be valid until ApplyDelta is called. |
| 58 | const StunAddressAttribute* GetAddress(int key) const; |
| 59 | const StunUInt32Attribute* GetUInt32(int key) const; |
| 60 | const StunUInt64Attribute* GetUInt64(int key) const; |
| 61 | const StunByteStringAttribute* GetByteString(int key) const; |
| 62 | const StunUInt16ListAttribute* GetUInt16List(int key) const; |
| 63 |
| 64 | bool empty() const { return attrs_.empty(); } |
| 65 | size_t size() const { return attrs_.size(); } |
| 66 | int bytes_stored() const { return bytes_stored_; } |
| 67 | void set_max_bytes_stored(int max_bytes_stored) { |
| 68 | max_bytes_stored_ = max_bytes_stored; |
| 69 | } |
| 70 |
| 71 | // Apply a delta and return |
| 72 | // a pair with |
| 73 | // - StunUInt64Attribute to ack the |delta|. |
| 74 | // - vector of keys that was modified. |
| 75 | webrtc::RTCErrorOr< |
| 76 | std::pair<std::unique_ptr<StunUInt64Attribute>, std::vector<uint16_t>>> |
| 77 | ApplyDelta(const StunByteStringAttribute& delta); |
| 78 |
| 79 | private: |
| 80 | friend class StunDictionaryWriter; |
| 81 |
| 82 | const StunAttribute* GetOrNull( |
| 83 | int key, |
| 84 | absl::optional<StunAttributeValueType> = absl::nullopt) const; |
| 85 | size_t GetLength(int key) const; |
| 86 | static webrtc::RTCErrorOr< |
| 87 | std::pair<uint64_t, std::deque<std::unique_ptr<StunAttribute>>>> |
| 88 | ParseDelta(const StunByteStringAttribute& delta); |
| 89 |
| 90 | std::map<uint16_t, std::unique_ptr<StunAttribute>> attrs_; |
| 91 | std::map<uint16_t, uint64_t> version_per_key_; |
| 92 |
| 93 | int max_bytes_stored_ = 16384; |
| 94 | int bytes_stored_ = 0; |
| 95 | }; |
| 96 |
| 97 | class StunDictionaryWriter { |
| 98 | public: |
| 99 | StunDictionaryWriter() { |
| 100 | dictionary_ = std::make_unique<StunDictionaryView>(); |
| 101 | } |
| 102 | explicit StunDictionaryWriter( |
| 103 | std::unique_ptr<StunDictionaryView> dictionary) { |
| 104 | dictionary_ = std::move(dictionary); |
| 105 | } |
| 106 |
| 107 | // A pending modification. |
| 108 | template <typename T> |
| 109 | class Modification { |
| 110 | public: |
| 111 | ~Modification() { commit(); } |
| 112 |
| 113 | T* operator->() { return attr_.get(); } |
| 114 |
| 115 | void abort() { attr_ = nullptr; } |
| 116 | void commit() { |
| 117 | if (attr_) { |
| 118 | writer_->Set(std::move(attr_)); |
| 119 | } |
| 120 | } |
| 121 |
| 122 | private: |
| 123 | friend class StunDictionaryWriter; |
| 124 | Modification(StunDictionaryWriter* writer, std::unique_ptr<T> attr) |
| 125 | : writer_(writer), attr_(std::move(attr)) {} |
| 126 | StunDictionaryWriter* writer_; |
| 127 | std::unique_ptr<T> attr_; |
| 128 |
| 129 | Modification(const Modification<T>&) = |
| 130 | delete; // not copyable (but movable). |
| 131 | Modification& operator=(Modification<T>&) = |
| 132 | delete; // not copyable (but movable). |
| 133 | }; |
| 134 |
| 135 | // Record a modification. |
| 136 | Modification<StunAddressAttribute> SetAddress(int key) { |
| 137 | return Modification<StunAddressAttribute>( |
| 138 | this, StunAttribute::CreateAddress(key)); |
| 139 | } |
| 140 | Modification<StunUInt32Attribute> SetUInt32(int key) { |
| 141 | return Modification<StunUInt32Attribute>(this, |
| 142 | StunAttribute::CreateUInt32(key)); |
| 143 | } |
| 144 | Modification<StunUInt64Attribute> SetUInt64(int key) { |
| 145 | return Modification<StunUInt64Attribute>(this, |
| 146 | StunAttribute::CreateUInt64(key)); |
| 147 | } |
| 148 | Modification<StunByteStringAttribute> SetByteString(int key) { |
| 149 | return Modification<StunByteStringAttribute>( |
| 150 | this, StunAttribute::CreateByteString(key)); |
| 151 | } |
| 152 | Modification<StunUInt16ListAttribute> SetUInt16List(int key) { |
| 153 | return Modification<StunUInt16ListAttribute>( |
| 154 | this, StunAttribute::CreateUInt16ListAttribute(key)); |
| 155 | } |
| 156 |
| 157 | // Delete a key. |
| 158 | void Delete(int key); |
| 159 |
| 160 | // Check if a key has a pending change (i.e a change |
| 161 | // that has not been acked). |
| 162 | bool Pending(int key) const; |
| 163 |
| 164 | // Return number of of pending modifications. |
| 165 | int Pending() const; |
| 166 |
| 167 | // Create an StunByteStringAttribute containing the pending (e.g not ack:ed) |
| 168 | // modifications. |
| 169 | std::unique_ptr<StunByteStringAttribute> CreateDelta(); |
| 170 |
| 171 | // Apply an delta ack. |
| 172 | void ApplyDeltaAck(const StunUInt64Attribute&); |
| 173 |
| 174 | // Return pointer to (optional) StunDictionaryView. |
| 175 | const StunDictionaryView* dictionary() { return dictionary_.get(); } |
| 176 | const StunDictionaryView* operator->() { return dictionary_.get(); } |
| 177 |
| 178 | // Disable writer, |
| 179 | // i.e CreateDelta always return null, and no modifications are made. |
| 180 | // This is called if remote peer does not support GOOG_DELTA. |
| 181 | void Disable(); |
| 182 | bool disabled() const { return disabled_; } |
| 183 |
| 184 | private: |
| 185 | void Set(std::unique_ptr<StunAttribute> attr); |
| 186 |
| 187 | bool disabled_ = false; |
| 188 |
| 189 | // version of modification. |
| 190 | int64_t version_ = 1; |
| 191 |
| 192 | // (optional) StunDictionaryView. |
| 193 | std::unique_ptr<StunDictionaryView> dictionary_; |
| 194 |
| 195 | // sorted list of changes that has not been yet been ack:ed. |
| 196 | std::vector<std::pair<uint64_t, const StunAttribute*>> pending_; |
| 197 |
| 198 | // tombstones, i.e values that has been deleted but not yet acked. |
| 199 | std::map<uint16_t, std::unique_ptr<StunAttribute>> tombstones_; |
| 200 | }; |
| 201 |
| 202 | } // namespace cricket |
| 203 |
| 204 | #endif // P2P_BASE_STUN_DICTIONARY_H_ |
| 205 |