summaryrefslogtreecommitdiffstats
path: root/libtransport/src/protocols/manifest_incremental_indexer_bytestream.cc
blob: 168aa57afc3a8777011bc26afe93b1d10d9ac5b1 (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
/*
 * 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 <implementation/socket_consumer.h>
#include <protocols/errors.h>
#include <protocols/manifest_incremental_indexer_bytestream.h>
#include <protocols/transport_protocol.h>

#include <cmath>
#include <deque>

namespace transport {

namespace protocol {

using namespace interface;

ManifestIncrementalIndexer::ManifestIncrementalIndexer(
    implementation::ConsumerSocket *icn_socket, TransportProtocol *transport)
    : IncrementalIndexer(icn_socket, transport),
      suffix_strategy_(utils::SuffixStrategyFactory::getSuffixStrategy(
          NextSegmentCalculationStrategy::INCREMENTAL, next_download_suffix_,
          0)) {}

void ManifestIncrementalIndexer::onContentObject(
    core::Interest &interest, core::ContentObject &content_object,
    bool reassembly) {
  switch (content_object.getPayloadType()) {
    case PayloadType::DATA: {
      DLOG_IF(INFO, VLOG_IS_ON(3))
          << "Received content " << content_object.getName();
      onUntrustedContentObject(interest, content_object, reassembly);
      break;
    }
    case PayloadType::MANIFEST: {
      DLOG_IF(INFO, VLOG_IS_ON(3))
          << "Received manifest " << content_object.getName();
      onUntrustedManifest(interest, content_object, reassembly);
      break;
    }
    default: {
      return;
    }
  }
}

void ManifestIncrementalIndexer::onUntrustedManifest(
    core::Interest &interest, core::ContentObject &content_object,
    bool reassembly) {
  auto manifest =
      std::make_unique<ContentObjectManifest>(std::move(content_object));

  auth::VerificationPolicy policy = verifier_->verifyPackets(manifest.get());

  manifest->decode();

  if (policy != auth::VerificationPolicy::ACCEPT) {
    transport_->onContentReassembled(
        make_error_code(protocol_error::session_aborted));
    return;
  }

  processTrustedManifest(interest, std::move(manifest), reassembly);
}

void ManifestIncrementalIndexer::processTrustedManifest(
    core::Interest &interest, std::unique_ptr<ContentObjectManifest> manifest,
    bool reassembly) {
  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: {
      suffix_strategy_->setFinalSuffix(manifest->getFinalBlockNumber());

      // The packets to verify with the received manifest
      std::vector<auth::PacketPtr> packets;

      // Convert the received manifest to a map of packet suffixes to hashes
      auth::Verifier::SuffixMap current_manifest =
          core::ContentObjectManifest::getSuffixMap(manifest.get());

      // Update 'suffix_map_' with new hashes from the received manifest and
      // build 'packets'
      for (auto it = current_manifest.begin(); it != current_manifest.end();) {
        if (unverified_segments_.find(it->first) ==
            unverified_segments_.end()) {
          suffix_map_[it->first] = std::move(it->second);
          current_manifest.erase(it++);
          continue;
        }

        packets.push_back(std::get<1>(unverified_segments_[it->first]).get());
        it++;
      }

      // Verify unverified segments using the received manifest
      auth::Verifier::PolicyMap policies =
          verifier_->verifyPackets(packets, current_manifest);

      for (unsigned int i = 0; i < packets.size(); ++i) {
        auth::Suffix suffix = packets[i]->getName().getSuffix();

        auto it = unverified_segments_.find(suffix);

        if (policies[suffix] != auth::VerificationPolicy::UNKNOWN) {
          unverified_segments_.erase(it);
          continue;
        }

        applyPolicy(*std::get<0>(it->second), *std::get<1>(it->second),
                    std::get<2>(it->second), policies[suffix]);
      }

      if (reassembly) {
        reassembly_->reassemble(std::move(manifest));
      }
      break;
    }
    case core::ManifestType::FLIC_MANIFEST: {
      throw errors::NotImplementedException();
    }
    case core::ManifestType::FINAL_CHUNK_NUMBER: {
      throw errors::NotImplementedException();
    }
  }
}

void ManifestIncrementalIndexer::onUntrustedContentObject(
    Interest &interest, ContentObject &content_object, bool reassembly) {
  auth::Suffix suffix = content_object.getName().getSuffix();
  auth::VerificationPolicy policy =
      verifier_->verifyPackets(&content_object, suffix_map_);

  switch (policy) {
    case auth::VerificationPolicy::UNKNOWN: {
      unverified_segments_[suffix] =
          std::make_tuple(interest.shared_from_this(),
                          content_object.shared_from_this(), reassembly);
      break;
    }
    default: {
      suffix_map_.erase(suffix);
      break;
    }
  }

  applyPolicy(interest, content_object, reassembly, policy);
}

void ManifestIncrementalIndexer::applyPolicy(
    core::Interest &interest, core::ContentObject &content_object,
    bool reassembly, auth::VerificationPolicy policy) {
  assert(reassembly_);
  switch (policy) {
    case auth::VerificationPolicy::ACCEPT: {
      if (reassembly && !reassembly_->reassembleUnverified()) {
        reassembly_->reassemble(content_object);
      }
      break;
    }
    case auth::VerificationPolicy::DROP: {
      transport_->onPacketDropped(
          interest, content_object,
          make_error_code(protocol_error::verification_failed));
      break;
    }
    case auth::VerificationPolicy::ABORT: {
      transport_->onContentReassembled(
          make_error_code(protocol_error::session_aborted));
      break;
    }
    case auth::VerificationPolicy::UNKNOWN: {
      if (reassembly && reassembly_->reassembleUnverified()) {
        reassembly_->reassemble(content_object);
      }
    }
  }
}

uint32_t ManifestIncrementalIndexer::checkNextSuffix() {
  return suffix_strategy_->getNextSuffix();
}

uint32_t ManifestIncrementalIndexer::getNextSuffix() {
  auto ret = suffix_strategy_->getNextSuffix();

  if (ret <= suffix_strategy_->getFinalSuffix() &&
      ret != utils::SuffixStrategy::INVALID_SUFFIX) {
    suffix_queue_.push(ret);
    return ret;
  }

  return Indexer::invalid_index;
}

uint32_t ManifestIncrementalIndexer::getFinalSuffix() {
  return suffix_strategy_->getFinalSuffix();
}

bool ManifestIncrementalIndexer::isFinalSuffixDiscovered() {
  return IncrementalIndexer::isFinalSuffixDiscovered();
}

uint32_t ManifestIncrementalIndexer::getNextReassemblySegment() {
  if (suffix_queue_.empty()) {
    return Indexer::invalid_index;
  }

  auto ret = suffix_queue_.front();
  suffix_queue_.pop();
  return ret;
}

void ManifestIncrementalIndexer::reset() {
  IncrementalIndexer::reset();
  suffix_map_.clear();
  unverified_segments_.clear();
  SuffixQueue empty;
  std::swap(suffix_queue_, empty);
  suffix_strategy_->reset(first_suffix_);
}

}  // namespace protocol

}  // namespace transport