servlib/bundling/UnknownBlockProcessor.cc
changeset 0 2b3e5ec03512
child 5 1849bf57d910
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2006 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #  include <dtn-config.h>
       
    19 #endif
       
    20 
       
    21 #include "UnknownBlockProcessor.h"
       
    22 
       
    23 #include "BlockInfo.h"
       
    24 #include "BundleProtocol.h"
       
    25 #include "Bundle.h"
       
    26 
       
    27 namespace dtn {
       
    28 
       
    29 template <> UnknownBlockProcessor*
       
    30 oasys::Singleton<UnknownBlockProcessor>::instance_ = NULL;
       
    31 
       
    32 //----------------------------------------------------------------------
       
    33 UnknownBlockProcessor::UnknownBlockProcessor()
       
    34     : BlockProcessor(BundleProtocol::UNKNOWN_BLOCK)
       
    35       // typecode is ignored for this processor
       
    36       // pl -- this raises the interesting situation where
       
    37       // source->type()                  returns the actual type, and
       
    38       // source->owner_->block_type()    will return UNKNOWN_BLOCK
       
    39 {
       
    40 }
       
    41 
       
    42 //----------------------------------------------------------------------
       
    43 int
       
    44 UnknownBlockProcessor::prepare(const Bundle*    bundle,
       
    45                                BlockInfoVec*    xmit_blocks,
       
    46                                const BlockInfo* source,
       
    47                                const LinkRef&   link,
       
    48                                list_owner_t     list)
       
    49 {
       
    50     ASSERT(source != NULL);
       
    51     ASSERT(source->owner() == this);
       
    52 
       
    53     if (source->flags() & BundleProtocol::BLOCK_FLAG_DISCARD_BLOCK_ONERROR) {
       
    54         return BP_FAIL;
       
    55     }
       
    56 
       
    57     // If we're called for this type then security is not enabled
       
    58     // and we should NEVER forward BAB
       
    59     if (source->type() == BundleProtocol::BUNDLE_AUTHENTICATION_BLOCK) {
       
    60         return BP_FAIL;
       
    61     }
       
    62 
       
    63     return BlockProcessor::prepare(bundle, xmit_blocks, source, link, list);
       
    64 }
       
    65 
       
    66 //----------------------------------------------------------------------
       
    67 int
       
    68 UnknownBlockProcessor::generate(const Bundle*  bundle,
       
    69                                 BlockInfoVec*  xmit_blocks,
       
    70                                 BlockInfo*     block,
       
    71                                 const LinkRef& link,
       
    72                                 bool           last)
       
    73 {
       
    74     (void)bundle;
       
    75     (void)link;
       
    76     (void)xmit_blocks;
       
    77     
       
    78     // This can only be called if there was a corresponding block in
       
    79     // the input path
       
    80     const BlockInfo* source = block->source();
       
    81     ASSERT(source != NULL);
       
    82     ASSERT(source->owner() == this);
       
    83 
       
    84     // We shouldn't be here if the block has the following flags set
       
    85     ASSERT((source->flags() &
       
    86             BundleProtocol::BLOCK_FLAG_DISCARD_BUNDLE_ONERROR) == 0);
       
    87     ASSERT((source->flags() &
       
    88             BundleProtocol::BLOCK_FLAG_DISCARD_BLOCK_ONERROR) == 0);
       
    89     
       
    90     // The source better have some contents, but doesn't need to have
       
    91     // any data necessarily
       
    92     ASSERT(source->contents().len() != 0);
       
    93     ASSERT(source->data_offset() != 0);
       
    94     
       
    95     u_int8_t flags = source->flags();
       
    96     if (last) {
       
    97         flags |= BundleProtocol::BLOCK_FLAG_LAST_BLOCK;
       
    98     } else {
       
    99         flags &= ~BundleProtocol::BLOCK_FLAG_LAST_BLOCK;
       
   100     }
       
   101     flags |= BundleProtocol::BLOCK_FLAG_FORWARDED_UNPROCESSED;
       
   102     
       
   103     block->set_eid_list(source->eid_list());
       
   104 
       
   105     generate_preamble(xmit_blocks, block, source->type(), flags,
       
   106                       source->data_length());
       
   107     ASSERT(block->data_length() == source->data_length());
       
   108     
       
   109     BlockInfo::DataBuffer* contents = block->writable_contents();
       
   110     contents->reserve(block->full_length());
       
   111     memcpy(contents->buf()          + block->data_offset(),
       
   112            source->contents().buf() + source->data_offset(),
       
   113            block->data_length());
       
   114     contents->set_len(block->full_length());
       
   115 
       
   116     return BP_SUCCESS;
       
   117 }
       
   118 
       
   119 //----------------------------------------------------------------------
       
   120 bool
       
   121 UnknownBlockProcessor::validate(const Bundle*           bundle,
       
   122                                 BlockInfoVec*           block_list,
       
   123                                 BlockInfo*              block,
       
   124                                 status_report_reason_t* reception_reason,
       
   125                                 status_report_reason_t* deletion_reason)
       
   126 {
       
   127     // check for generic block errors
       
   128     if (!BlockProcessor::validate(bundle, block_list, block,
       
   129                                   reception_reason, deletion_reason)) {
       
   130         return false;
       
   131     }
       
   132 
       
   133     // extension blocks of unknown type are considered to be "invalid"
       
   134     if (block->flags() & BundleProtocol::BLOCK_FLAG_REPORT_ONERROR) {
       
   135         *reception_reason = BundleProtocol::REASON_BLOCK_UNINTELLIGIBLE;
       
   136     }
       
   137 
       
   138     if (block->flags() & BundleProtocol::BLOCK_FLAG_DISCARD_BUNDLE_ONERROR) {
       
   139         *deletion_reason = BundleProtocol::REASON_BLOCK_UNINTELLIGIBLE;
       
   140         return false;
       
   141     }
       
   142 
       
   143     return true;
       
   144 }
       
   145 
       
   146 } // namespace dtn