IBR-DTNSuite  0.8
ibrdtn/ibrdtn/security/StrictSerializer.cpp
Go to the documentation of this file.
00001 #include "ibrdtn/security/StrictSerializer.h"
00002 #include "ibrdtn/security/BundleAuthenticationBlock.h"
00003 #include "ibrdtn/security/PayloadIntegrityBlock.h"
00004 #include "ibrdtn/data/Block.h"
00005 #include "ibrdtn/data/Bundle.h"
00006 
00007 namespace dtn
00008 {
00009         namespace security
00010         {
00011                 StrictSerializer::StrictSerializer(std::ostream& stream, const dtn::security::SecurityBlock::BLOCK_TYPES type, const bool with_correlator, const u_int64_t correlator)
00012                  : DefaultSerializer(stream), _block_type(type), _with_correlator(with_correlator), _correlator(correlator)
00013                 {
00014                 }
00015 
00016                 StrictSerializer::~StrictSerializer()
00017                 {
00018                 }
00019 
00020                 dtn::data::Serializer& StrictSerializer::operator<<(const dtn::data::Bundle& bundle)
00021                 {
00022                         // rebuild the dictionary
00023                         rebuildDictionary(bundle);
00024 
00025                         // serialize the primary block
00026                         (dtn::data::DefaultSerializer&)(*this) << static_cast<const dtn::data::PrimaryBlock&>(bundle);
00027 
00028                         // serialize all secondary blocks
00029                         std::list<refcnt_ptr<dtn::data::Block> > list = bundle._blocks._blocks;
00030                         std::list<refcnt_ptr<dtn::data::Block> >::const_iterator iter = list.begin();
00031 
00032                         // skip all blocks before the correlator
00033                         for (; _with_correlator && iter != list.end(); iter++)
00034                         {
00035                                 const dtn::data::Block &b = (*(*iter));
00036                                 if (b.getType() == SecurityBlock::BUNDLE_AUTHENTICATION_BLOCK || b.getType() == SecurityBlock::PAYLOAD_INTEGRITY_BLOCK)
00037                                 {
00038                                         const dtn::security::SecurityBlock& sb = dynamic_cast<const dtn::security::SecurityBlock&>(*(*iter));
00039                                         if ((sb._ciphersuite_flags & SecurityBlock::CONTAINS_CORRELATOR) && sb._correlator == _correlator)
00040                                                 break;
00041                                 }
00042                         }
00043 
00044                         // consume the first block. this block may have the same correlator set, 
00045                         // we are searching for in the loop
00046                         (*this) << (*(*iter));
00047                         iter++;
00048 
00049                         // serialize the remaining block
00050                         for (; iter != list.end(); iter++)
00051                         {
00052                                 const dtn::data::Block &b = (*(*iter));
00053                                 (*this) << b;
00054 
00055                                 try {
00056                                         const dtn::security::SecurityBlock &sb = dynamic_cast<const dtn::security::SecurityBlock&>(b);
00057 
00058                                         if ( (sb.getType() == SecurityBlock::BUNDLE_AUTHENTICATION_BLOCK) || (sb.getType() == SecurityBlock::PAYLOAD_INTEGRITY_BLOCK) )
00059                                         {
00060                                                 // until the block with the second correlator is reached
00061                                                 if (_with_correlator && (sb._ciphersuite_flags & SecurityBlock::CONTAINS_CORRELATOR) && sb._correlator == _correlator) break;
00062                                         }
00063                                 } catch (const std::bad_cast&) { };
00064                         }
00065 
00066                         return *this;
00067                 }
00068 
00069                 dtn::data::Serializer& StrictSerializer::operator<<(const dtn::data::Block &obj)
00070                 {
00071                         _stream << obj._blocktype;
00072                         _stream << dtn::data::SDNV(obj._procflags);
00073 
00074 #ifdef __DEVELOPMENT_ASSERTIONS__
00075                         // test: BLOCK_CONTAINS_EIDS => (_eids.size() > 0)
00076                         assert(!obj.get(dtn::data::Block::BLOCK_CONTAINS_EIDS) || (obj._eids.size() > 0));
00077 #endif
00078 
00079                         if (obj.get(dtn::data::Block::BLOCK_CONTAINS_EIDS))
00080                         {
00081                                 _stream << dtn::data::SDNV(obj._eids.size());
00082                                 for (std::list<dtn::data::EID>::const_iterator it = obj._eids.begin(); it != obj._eids.end(); it++)
00083                                 {
00084                                         pair<size_t, size_t> offsets;
00085 
00086                                         if (_compressable)
00087                                         {
00088                                                 offsets = (*it).getCompressed();
00089                                         }
00090                                         else
00091                                         {
00092                                                 offsets = _dictionary.getRef(*it);
00093                                         }
00094 
00095                                         _stream << dtn::data::SDNV(offsets.first);
00096                                         _stream << dtn::data::SDNV(offsets.second);
00097                                 }
00098                         }
00099 
00100                         // write size of the payload in the block
00101                         _stream << dtn::data::SDNV(obj.getLength_strict());
00102 
00103                         size_t slength = 0;
00104                         obj.serialize_strict(_stream, slength);
00105 
00106                         return (*this);
00107                 }
00108         }
00109 }