servlib/reg/AdminRegistration.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2004-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 <oasys/util/ScratchBuffer.h>
       
    22 
       
    23 #include "AdminRegistration.h"
       
    24 #include "RegistrationTable.h"
       
    25 #include "bundling/BundleDaemon.h"
       
    26 #include "bundling/BundleProtocol.h"
       
    27 #include "bundling/CustodySignal.h"
       
    28 #include "routing/BundleRouter.h"
       
    29 
       
    30 namespace dtn {
       
    31 
       
    32 AdminRegistration::AdminRegistration()
       
    33     : Registration(ADMIN_REGID,
       
    34                    BundleDaemon::instance()->local_eid(),
       
    35                    Registration::DEFER, 0, 0)
       
    36 {
       
    37     logpathf("/dtn/reg/admin");
       
    38     set_active(true);
       
    39 }
       
    40 
       
    41 void
       
    42 AdminRegistration::deliver_bundle(Bundle* bundle)
       
    43 {
       
    44     u_char typecode;
       
    45 
       
    46     size_t payload_len = bundle->payload().length();
       
    47     oasys::ScratchBuffer<u_char*, 256> scratch(payload_len);
       
    48     const u_char* payload_buf = 
       
    49         bundle->payload().read_data(0, payload_len, scratch.buf(payload_len));
       
    50     
       
    51     log_debug("got %zu byte bundle", payload_len);
       
    52         
       
    53     if (payload_len == 0) {
       
    54         log_err("admin registration got 0 byte *%p", bundle);
       
    55         goto done;
       
    56     }
       
    57 
       
    58     if (!bundle->is_admin()) {
       
    59         log_warn("non-admin *%p sent to local eid", bundle);
       
    60         goto done;
       
    61     }
       
    62 
       
    63     /*
       
    64      * As outlined in the bundle specification, the first four bits of
       
    65      * all administrative bundles hold the type code, with the
       
    66      * following values:
       
    67      *
       
    68      * 0x1     - bundle status report
       
    69      * 0x2     - custodial signal
       
    70      * 0x3     - echo request
       
    71      * 0x4     - null request
       
    72      * 0x5     - announce
       
    73      * (other) - reserved
       
    74      */
       
    75     typecode = payload_buf[0] >> 4;
       
    76     
       
    77     switch(typecode) {
       
    78     case BundleProtocol::ADMIN_STATUS_REPORT:
       
    79     {
       
    80         log_err("status report *%p received at admin registration", bundle);
       
    81         break;
       
    82     }
       
    83     
       
    84     case BundleProtocol::ADMIN_CUSTODY_SIGNAL:
       
    85     {
       
    86         log_info("ADMIN_CUSTODY_SIGNAL *%p received", bundle);
       
    87         CustodySignal::data_t data;
       
    88         
       
    89         bool ok = CustodySignal::parse_custody_signal(&data, payload_buf, payload_len);
       
    90         if (!ok) {
       
    91             log_err("malformed custody signal *%p", bundle);
       
    92             break;
       
    93         }
       
    94 
       
    95         BundleDaemon::post(new CustodySignalEvent(data));
       
    96 
       
    97         break;
       
    98     }
       
    99     case BundleProtocol::ADMIN_ANNOUNCE:
       
   100     {
       
   101         log_info("ADMIN_ANNOUNCE from %s", bundle->source().c_str());
       
   102         break;
       
   103     }
       
   104         
       
   105     default:
       
   106         log_warn("unexpected admin bundle with type 0x%x *%p",
       
   107                  typecode, bundle);
       
   108     }    
       
   109 
       
   110  done:
       
   111     BundleDaemon::post(new BundleDeliveredEvent(bundle, this));
       
   112 }
       
   113 
       
   114 
       
   115 } // namespace dtn