00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "PreviousHopBlockProcessor.h"
00018 #include "Bundle.h"
00019 #include "BundleDaemon.h"
00020 #include "BundleProtocol.h"
00021 #include "contacts/Link.h"
00022
00023 namespace dtn {
00024
00025
00026 PreviousHopBlockProcessor::PreviousHopBlockProcessor()
00027 : BlockProcessor(BundleProtocol::PREVIOUS_HOP_BLOCK)
00028 {
00029 }
00030
00031
00032 void
00033 PreviousHopBlockProcessor::prepare(const Bundle* bundle,
00034 Link* link,
00035 BlockInfoVec* blocks,
00036 const BlockInfo* source)
00037 {
00038 if (link == NULL || !link->params().prevhop_hdr_) {
00039 return;
00040 }
00041
00042 BlockProcessor::prepare(bundle, link, blocks, source);
00043 }
00044
00045
00046 void
00047 PreviousHopBlockProcessor::generate(const Bundle* bundle,
00048 Link* link,
00049 BlockInfo* block,
00050 bool last)
00051 {
00052 (void)bundle;
00053 (void)link;
00054
00055
00056
00057
00058
00059
00060 ASSERT(link->params().prevhop_hdr_);
00061
00062 BundleDaemon* bd = BundleDaemon::instance();
00063 size_t length = bd->local_eid().length();
00064
00065 generate_preamble(block,
00066 BundleProtocol::PREVIOUS_HOP_BLOCK,
00067 BundleProtocol::BLOCK_FLAG_DISCARD_BUNDLE_ONERROR |
00068 (last ? BundleProtocol::BLOCK_FLAG_LAST_BLOCK : 0),
00069 length);
00070
00071 BlockInfo::DataBuffer* contents = block->writable_contents();
00072 contents->reserve(block->data_offset() + length);
00073 contents->set_len(block->data_offset() + length);
00074 memcpy(contents->buf() + block->data_offset(),
00075 bd->local_eid().data(), length);
00076 }
00077
00078
00079 int
00080 PreviousHopBlockProcessor::consume(Bundle* bundle, BlockInfo* block,
00081 u_char* buf, size_t len)
00082 {
00083 int cc = BlockProcessor::consume(bundle, block, buf, len);
00084
00085 if (cc == -1) {
00086 return -1;
00087 }
00088
00089 if (! block->complete()) {
00090 ASSERT(cc == (int)len);
00091 return cc;
00092 }
00093
00094 if (! bundle->prevhop_.assign((char*)block->data(), block->data_length())) {
00095 log_err_p("/dtn/bundle/protocol",
00096 "error parsing previous hop eid '%.*s",
00097 block->data_length(), block->data());
00098 return -1;
00099 }
00100
00101 return cc;
00102 }
00103
00104
00105 }