00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include "PayloadBlockProcessor.h"
00022 #include "Bundle.h"
00023 #include "BundleProtocol.h"
00024
00025 namespace dtn {
00026
00027
00028 PayloadBlockProcessor::PayloadBlockProcessor()
00029 : BlockProcessor(BundleProtocol::PAYLOAD_BLOCK)
00030 {
00031 }
00032
00033
00034 int
00035 PayloadBlockProcessor::consume(Bundle* bundle,
00036 BlockInfo* block,
00037 u_char* buf,
00038 size_t len)
00039 {
00040 static const char* log = "/dtn/bundle/protocol";
00041 (void)log;
00042
00043 BlockInfoVec* recv_blocks = bundle->mutable_recv_blocks();
00044 size_t consumed = 0;
00045 if (block->data_offset() == 0) {
00046 int cc = consume_preamble(recv_blocks, block, buf, len);
00047 if (cc == -1) {
00048 return -1;
00049 }
00050
00051 buf += cc;
00052 len -= cc;
00053
00054 consumed += cc;
00055
00056 ASSERT(bundle->payload().length() == 0);
00057 }
00058
00059
00060
00061 if (block->data_offset() == 0) {
00062 ASSERT(len == 0);
00063 return consumed;
00064 }
00065
00066
00067
00068 if (bundle->payload().location() == BundlePayload::NODATA) {
00069 block->set_complete(true);
00070 return consumed;
00071 }
00072
00073
00074
00075 if (block->data_offset() != 0 && block->data_length() == 0) {
00076 block->set_complete(true);
00077 return consumed;
00078 }
00079
00080
00081 if (len == 0) {
00082 return consumed;
00083 }
00084
00085
00086
00087 ASSERT(block->contents().len() == block->data_offset());
00088
00089
00090
00091 ASSERT(block->data_length() > bundle->payload().length());
00092
00093 size_t rcvd = bundle->payload().length();
00094 size_t remainder = block->data_length() - rcvd;
00095 size_t tocopy;
00096
00097 if (len >= remainder) {
00098 block->set_complete(true);
00099 tocopy = remainder;
00100 } else {
00101 tocopy = len;
00102 }
00103
00104 bundle->mutable_payload()->set_length(rcvd + tocopy);
00105 bundle->mutable_payload()->write_data(buf, rcvd, tocopy);
00106
00107 consumed += tocopy;
00108
00109 log_debug_p(log, "PayloadBlockProcessor consumed %zu/%u (%s)",
00110 consumed, block->full_length(),
00111 block->complete() ? "complete" : "not complete");
00112
00113 return consumed;
00114 }
00115
00116
00117 bool
00118 PayloadBlockProcessor::validate(const Bundle* bundle,
00119 BlockInfoVec* block_list,
00120 BlockInfo* block,
00121 status_report_reason_t* reception_reason,
00122 status_report_reason_t* deletion_reason)
00123 {
00124 static const char* log = "/dtn/bundle/protocol";
00125
00126
00127 if (!BlockProcessor::validate(bundle, block_list, block,
00128 reception_reason, deletion_reason)) {
00129 return false;
00130 }
00131
00132 if (!block->complete()) {
00133
00134
00135
00136 if (block->data_offset() == 0
00137
00138
00139
00140 || (block->data_length() != 0 &&
00141 bundle->payload().length() == 0)
00142
00143
00144
00145 || bundle->do_not_fragment())
00146 {
00147 log_err_p(log, "payload incomplete and cannot be fragmented");
00148 *deletion_reason = BundleProtocol::REASON_BLOCK_UNINTELLIGIBLE;
00149 return false;
00150 }
00151 }
00152
00153 return true;
00154 }
00155
00156
00157 int
00158 PayloadBlockProcessor::generate(const Bundle* bundle,
00159 BlockInfoVec* xmit_blocks,
00160 BlockInfo* block,
00161 const LinkRef& link,
00162 bool last)
00163 {
00164 (void)link;
00165 (void)xmit_blocks;
00166
00167
00168
00169 generate_preamble(xmit_blocks,
00170 block,
00171 BundleProtocol::PAYLOAD_BLOCK,
00172 last ? BundleProtocol::BLOCK_FLAG_LAST_BLOCK : 0,
00173 bundle->payload().length());
00174
00175 return BP_SUCCESS;
00176 }
00177
00178
00179 void
00180 PayloadBlockProcessor::produce(const Bundle* bundle,
00181 const BlockInfo* block,
00182 u_char* buf,
00183 size_t offset,
00184 size_t len)
00185 {
00186
00187 if (offset < block->data_offset()) {
00188 size_t tocopy = std::min(len, block->data_offset() - offset);
00189 memcpy(buf, block->contents().buf() + offset, tocopy);
00190 buf += tocopy;
00191 offset += tocopy;
00192 len -= tocopy;
00193 }
00194
00195 if (len == 0)
00196 return;
00197
00198
00199 size_t payload_offset = offset - block->data_offset();
00200
00201 size_t tocopy = std::min(len, bundle->payload().length() - payload_offset);
00202 bundle->payload().read_data(payload_offset, tocopy, buf);
00203
00204 return;
00205 }
00206
00207
00208 void
00209 PayloadBlockProcessor::process(process_func* func,
00210 const Bundle* bundle,
00211 const BlockInfo* caller_block,
00212 const BlockInfo* target_block,
00213 size_t offset,
00214 size_t len,
00215 OpaqueContext* context)
00216 {
00217 const u_char* buf;
00218 u_char work[1024];
00219
00220 size_t len_to_do = 0;
00221
00222
00223
00224
00225
00226
00227 if (offset < target_block->data_offset()) {
00228 len_to_do = std::min(len, target_block->data_offset() - offset);
00229
00230
00231 buf = target_block->contents().buf() + offset;
00232
00233
00234 (*func)(bundle, caller_block, target_block, buf, len_to_do, context);
00235 buf += len_to_do;
00236 offset += len_to_do;
00237 len -= len_to_do;
00238 }
00239
00240 if (len == 0)
00241 return;
00242
00243
00244 size_t payload_offset = offset - target_block->data_offset();
00245 size_t remaining = std::min(len, bundle->payload().length() - payload_offset);
00246 size_t outlen;
00247
00248 buf = work;
00249
00250 while ( remaining > 0 ) {
00251 outlen = 0;
00252 len_to_do = std::min(remaining, sizeof(work));
00253 buf = bundle->payload().read_data(payload_offset, len_to_do, work);
00254
00255
00256 (*func)(bundle, caller_block, target_block, buf, len_to_do, context);
00257
00258 payload_offset += len_to_do;
00259 remaining -= len_to_do;
00260 }
00261 }
00262
00263
00264 bool
00265 PayloadBlockProcessor::mutate(mutate_func* func,
00266 Bundle* bundle,
00267 const BlockInfo* caller_block,
00268 BlockInfo* target_block,
00269 size_t offset,
00270 size_t len,
00271 OpaqueContext* r)
00272 {
00273 bool changed = false;
00274 u_char* buf;
00275 u_char work[1024];
00276
00277 size_t len_to_do = 0;
00278
00279
00280
00281
00282
00283
00284 if (offset < target_block->data_offset()) {
00285 len_to_do = std::min(len, target_block->data_offset() - offset);
00286
00287 buf = target_block->contents().buf() + offset;
00288
00289 changed = (*func)(bundle, caller_block, target_block, buf, len_to_do, r);
00290 buf += len_to_do;
00291 offset += len_to_do;
00292 len -= len_to_do;
00293 }
00294
00295 if (len == 0)
00296 return changed;
00297
00298
00299 size_t payload_offset = offset - target_block->data_offset();
00300 size_t remaining = std::min(len, bundle->payload().length() - payload_offset);
00301 size_t outlen;
00302
00303 buf = work;
00304
00305 while ( remaining > 0 ) {
00306 outlen = 0;
00307 len_to_do = std::min(remaining, sizeof(work));
00308 bundle->payload().read_data(payload_offset, len_to_do, buf);
00309
00310
00311 bool chunk_changed =
00312 (*func)(bundle, caller_block, target_block, buf, len_to_do, r);
00313
00314
00315 if ( chunk_changed )
00316 bundle->mutable_payload()->
00317 write_data(buf, payload_offset, len_to_do);
00318
00319 changed |= chunk_changed;
00320
00321 payload_offset += len_to_do;
00322 remaining -= len_to_do;
00323 }
00324
00325 return changed;
00326 }
00327
00328 }