aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/hicn/transport/protocols/manifest_indexing_manager.cc
blob: 05cabc60da80e7f7b89b9b3c7876de72d64280f8 (plain)
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
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <hicn/transport/interfaces/socket_consumer.h>
#include <hicn/transport/protocols/manifest_indexing_manager.h>

#include <cmath>
#include <deque>

namespace transport {

namespace protocol {

using namespace interface;

ManifestIndexManager::ManifestIndexManager(
    interface::ConsumerSocket *icn_socket, TransportProtocol *next_interest)
    : IncrementalIndexManager(icn_socket),
      PacketManager<Interest>(1024),
      manifests_in_flight_(0),
      next_reassembly_segment_(suffix_queue_.end()),
      next_to_retrieve_segment_(suffix_queue_.end()),
      suffix_manifest_(core::NextSegmentCalculationStrategy::INCREMENTAL, 0),
      next_interest_(next_interest) {}

bool ManifestIndexManager::onManifest(
    core::ContentObject::Ptr &&content_object) {
  auto manifest =
      std::make_unique<ContentObjectManifest>(std::move(*content_object));

  bool manifest_verified = verification_manager_->onPacketToVerify(*manifest);

  if (manifest_verified) {
    manifest->decode();

    if (TRANSPORT_EXPECT_FALSE(manifest->getVersion() !=
                               core::ManifestVersion::VERSION_1)) {
      throw errors::RuntimeException("Received manifest with unknown version.");
    }

    switch (manifest->getManifestType()) {
      case core::ManifestType::INLINE_MANIFEST: {
        auto _it = manifest->getSuffixList().begin();
        auto _end = --manifest->getSuffixList().end();

        if (TRANSPORT_EXPECT_FALSE(manifest->isFinalManifest())) {
          _end++;
        }

        // Get final block number
        final_suffix_ = manifest->getFinalBlockNumber();

        suffix_hash_map_[_it->first] =
            std::make_pair(std::vector<uint8_t>(_it->second, _it->second + 32),
                           manifest->getHashAlgorithm());
        suffix_queue_.push_back(_it->first);

        // If the transport protocol finished the list of segments to retrieve,
        // reset the next_to_retrieve_segment_ iterator to the next segment
        // provided by this manifest.
        if (TRANSPORT_EXPECT_FALSE(next_to_retrieve_segment_ ==
                                   suffix_queue_.end())) {
          next_to_retrieve_segment_ = --suffix_queue_.end();
        }

        std::advance(_it, 1);
        for (; _it != _end; _it++) {
          suffix_hash_map_[_it->first] = std::make_pair(
              std::vector<uint8_t>(_it->second, _it->second + 32),
              manifest->getHashAlgorithm());
          suffix_queue_.push_back(_it->first);
        }

        if (TRANSPORT_EXPECT_FALSE(manifest->getName().getSuffix()) == 0) {
          // Set the iterators to the beginning of the suffix queue
          next_reassembly_segment_ = suffix_queue_.begin();
          // Set number of segments in manifests assuming the first one is full
          suffix_manifest_.setNbSegments(
              std::distance(manifest->getSuffixList().begin(),
                            manifest->getSuffixList().end()) -
              1);
          suffix_manifest_.setSuffixStrategy(
              manifest->getNextSegmentCalculationStrategy());
        } else if (manifests_in_flight_) {
          manifests_in_flight_--;
        }

        if (TRANSPORT_EXPECT_FALSE(manifest->isFinalManifest() ||
                                   suffix_manifest_.getSuffix() >
                                       final_suffix_)) {
          break;
        }

        // Get current window size
        double current_window_size = 0.;
        socket_->getSocketOption(GeneralTransportOptions::CURRENT_WINDOW_SIZE,
                                 current_window_size);

        // Get portal
        std::shared_ptr<interface::BasePortal> portal;
        socket_->getSocketOption(GeneralTransportOptions::PORTAL, portal);

        // Number of segments in manifest
        std::size_t segment_count = 0;

        // Manifest namespace
        Name &name = manifest->getWritableName();

        if (manifests_in_flight_ >= MAX_MANIFESTS_IN_FLIGHT) {
          break;
        }

        // Send as many manifest as required for filling window.
        do {
          segment_count += suffix_manifest_.getNbSegments();
          suffix_manifest_++;

          Interest::Ptr interest = getPacket();
          name.setSuffix(suffix_manifest_.getSuffix());
          interest->setName(name);

          uint32_t interest_lifetime;
          socket_->getSocketOption(GeneralTransportOptions::INTEREST_LIFETIME,
                                   interest_lifetime);
          interest->setLifetime(interest_lifetime);

          // Send requests for manifest out of the congestion window (no
          // in_flight_interests++)
          portal->sendInterest(
              std::move(interest),
              std::bind(&ManifestIndexManager::onManifestReceived, this,
                        std::placeholders::_1, std::placeholders::_2),
              std::bind(&ManifestIndexManager::onManifestTimeout, this,
                        std::placeholders::_1));
          manifests_in_flight_++;
        } while (segment_count < current_window_size &&
                 suffix_manifest_.getSuffix() < final_suffix_ &&
                 manifests_in_flight_ < MAX_MANIFESTS_IN_FLIGHT);

        break;
      }
      case core::ManifestType::FLIC_MANIFEST: {
        throw errors::NotImplementedException();
      }
      case core::ManifestType::FINAL_CHUNK_NUMBER: {
        throw errors::NotImplementedException();
      }
    }
  }

  return manifest_verified;
}

void ManifestIndexManager::onManifestReceived(Interest::Ptr &&i,
                                              ContentObject::Ptr &&c) {
  onManifest(std::move(c));
  if (next_interest_) {
    next_interest_->scheduleNextInterests();
  }
}

void ManifestIndexManager::onManifestTimeout(Interest::Ptr &&i) {
  const Name &n = i->getName();
  uint32_t segment = n.getSuffix();

  if (segment > final_suffix_) {
    return;
  }

  // Get portal
  std::shared_ptr<interface::BasePortal> portal;
  socket_->getSocketOption(GeneralTransportOptions::PORTAL, portal);

  // Send requests for manifest out of the congestion window (no
  // in_flight_interests++)
  portal->sendInterest(
      std::move(i),
      std::bind(&ManifestIndexManager::onManifestReceived, this,
                std::placeholders::_1, std::placeholders::_2),
      std::bind(&ManifestIndexManager::onManifestTimeout, this,
                std::placeholders::_1));
}

bool ManifestIndexManager::onContentObject(
    const core::ContentObject &content_object) {
  bool verify_signature;
  socket_->getSocketOption(GeneralTransportOptions::VERIFY_SIGNATURE,
                           verify_signature);

  if (!verify_signature) {
    return true;
  }

  uint64_t segment = content_object.getName().getSuffix();

  bool ret = false;

  auto it = suffix_hash_map_.find((const unsigned int)segment);
  if (it != suffix_hash_map_.end()) {
    auto hash_type = static_cast<utils::CryptoHashType>(it->second.second);
    auto data_packet_digest = content_object.computeDigest(it->second.second);
    auto data_packet_digest_bytes =
        data_packet_digest.getDigest<uint8_t>().data();
    std::vector<uint8_t> &manifest_digest_bytes = it->second.first;

    if (utils::CryptoHash::compareBinaryDigest(data_packet_digest_bytes,
                                               manifest_digest_bytes.data(),
                                               hash_type)) {
      suffix_hash_map_.erase(it);
      ret = true;
    } else {
      throw errors::RuntimeException(
          "Verification failure policy has to be implemented.");
    }
  }

  return ret;
}

uint32_t ManifestIndexManager::getNextSuffix() {
  if (TRANSPORT_EXPECT_FALSE(next_to_retrieve_segment_ ==
                             suffix_queue_.end())) {
    return invalid_index;
  }

  return *next_to_retrieve_segment_++;
}

uint32_t ManifestIndexManager::getFinalSuffix() { return final_suffix_; }

bool ManifestIndexManager::isFinalSuffixDiscovered() {
  return IncrementalIndexManager::isFinalSuffixDiscovered();
}

uint32_t ManifestIndexManager::getNextReassemblySegment() {
  if (TRANSPORT_EXPECT_FALSE(next_reassembly_segment_ == suffix_queue_.end())) {
    return invalid_index;
  }

  if (TRANSPORT_EXPECT_TRUE(next_reassembly_segment_ !=
                            suffix_queue_.begin())) {
    suffix_queue_.erase(std::prev(next_reassembly_segment_));
  }

  return *next_reassembly_segment_++;
}

void ManifestIndexManager::reset() {
  IncrementalIndexManager::reset();
  suffix_manifest_.reset(0);
  suffix_queue_.clear();
  suffix_hash_map_.clear();
}

}  // end namespace protocol

}  // end namespace transport