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 "PreviousHopBlockProcessor.h"
00022 #include "Bundle.h"
00023 #include "BundleDaemon.h"
00024 #include "BundleProtocol.h"
00025 #include "contacts/Link.h"
00026
00027 namespace dtn {
00028
00029
00030 PreviousHopBlockProcessor::PreviousHopBlockProcessor()
00031 : BlockProcessor(BundleProtocol::PREVIOUS_HOP_BLOCK)
00032 {
00033 }
00034
00035
00036 int
00037 PreviousHopBlockProcessor::prepare(const Bundle* bundle,
00038 BlockInfoVec* xmit_blocks,
00039 const BlockInfo* source,
00040 const LinkRef& link,
00041 list_owner_t list)
00042 {
00043 if (link == NULL || !link->params().prevhop_hdr_) {
00044 return BP_FAIL;
00045 }
00046
00047 return BlockProcessor::prepare(bundle, xmit_blocks, source, link, list);
00048 }
00049
00050
00051 int
00052 PreviousHopBlockProcessor::generate(const Bundle* bundle,
00053 BlockInfoVec* xmit_blocks,
00054 BlockInfo* block,
00055 const LinkRef& link,
00056 bool last)
00057 {
00058 (void)bundle;
00059 (void)link;
00060 (void)xmit_blocks;
00061
00062
00063
00064
00065
00066
00067 ASSERT(link->params().prevhop_hdr_);
00068
00069 BundleDaemon* bd = BundleDaemon::instance();
00070 size_t length = bd->local_eid().length();
00071
00072 generate_preamble(xmit_blocks,
00073 block,
00074 BundleProtocol::PREVIOUS_HOP_BLOCK,
00075 BundleProtocol::BLOCK_FLAG_DISCARD_BUNDLE_ONERROR |
00076 (last ? BundleProtocol::BLOCK_FLAG_LAST_BLOCK : 0),
00077 length);
00078
00079 BlockInfo::DataBuffer* contents = block->writable_contents();
00080 contents->reserve(block->data_offset() + length);
00081 contents->set_len(block->data_offset() + length);
00082 memcpy(contents->buf() + block->data_offset(),
00083 bd->local_eid().data(), length);
00084
00085 return BP_SUCCESS;
00086 }
00087
00088
00089 int
00090 PreviousHopBlockProcessor::consume(Bundle* bundle,
00091 BlockInfo* block,
00092 u_char* buf,
00093 size_t len)
00094 {
00095 int cc = BlockProcessor::consume(bundle, block, buf, len);
00096
00097 if (cc == -1) {
00098 return -1;
00099 }
00100
00101 if (! block->complete()) {
00102 ASSERT(cc == (int)len);
00103 return cc;
00104 }
00105
00106 if (! bundle->mutable_prevhop()->
00107 assign((char*)block->data(), block->data_length()))
00108 {
00109 log_err_p("/dtn/bundle/protocol",
00110 "error parsing previous hop eid '%.*s",
00111 block->data_length(), block->data());
00112 return -1;
00113 }
00114
00115 return cc;
00116 }
00117
00118
00119 }