AdminRegistration.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <oasys/util/ScratchBuffer.h>
00040 
00041 #include "AdminRegistration.h"
00042 #include "RegistrationTable.h"
00043 #include "bundling/BundleDaemon.h"
00044 #include "bundling/BundleProtocol.h"
00045 #include "bundling/CustodySignal.h"
00046 #include "routing/BundleRouter.h"
00047 
00048 namespace dtn {
00049 
00050 AdminRegistration::AdminRegistration()
00051     : Registration(ADMIN_REGID,
00052                    BundleDaemon::instance()->local_eid(),
00053                    Registration::DEFER,
00054                    0)
00055 {
00056     logpathf("/reg/admin");
00057 }
00058 
00059 void
00060 AdminRegistration::deliver_bundle(Bundle* bundle)
00061 {
00062     u_char typecode;
00063 
00064     size_t payload_len = bundle->payload_.length();
00065     oasys::ScratchBuffer<u_char*, 256> scratch(payload_len);
00066     const u_char* payload_buf = 
00067         bundle->payload_.read_data(0, payload_len, scratch.buf(payload_len));
00068     
00069     log_debug("got %zu byte bundle", payload_len);
00070         
00071     if (payload_len == 0) {
00072         log_err("admin registration got 0 byte bundle *%p", bundle);
00073         goto done;
00074     }
00075 
00076     /*
00077      * As outlined in the bundle specification, the first four bits of
00078      * all administrative bundles hold the type code, with the
00079      * following values:
00080      *
00081      * 0x1     - bundle status report
00082      * 0x2     - custodial signal
00083      * 0x3     - echo request
00084      * 0x4     - null request
00085      * 0x5     - announce
00086      * (other) - reserved
00087      */
00088     typecode = payload_buf[0] >> 4;
00089     
00090     switch(typecode) {
00091     case BundleProtocol::ADMIN_STATUS_REPORT:
00092     {
00093         log_err("status report *%p received at admin registration", bundle);
00094         break;
00095     }
00096     
00097     case BundleProtocol::ADMIN_CUSTODY_SIGNAL:
00098     {
00099         log_info("ADMIN_CUSTODY_SIGNAL *%p received", bundle);
00100         CustodySignal::data_t data;
00101         
00102         bool ok = CustodySignal::parse_custody_signal(&data, payload_buf, payload_len);
00103         if (!ok) {
00104             log_err("malformed custody signal *%p", bundle);
00105             break;
00106         }
00107 
00108         BundleDaemon::post(new CustodySignalEvent(data));
00109 
00110         break;
00111     }
00112     case BundleProtocol::ADMIN_ECHO:
00113     {
00114         log_info("ADMIN_ECHO from %s", bundle->source_.c_str());
00115 
00116         Bundle* reply = new Bundle();
00117         reply->source_.assign(bundle->dest_);
00118         reply->dest_.assign(bundle->source_);
00119         reply->replyto_.assign(EndpointID::NULL_EID());
00120         reply->custodian_.assign(EndpointID::NULL_EID());
00121         reply->expiration_ = bundle->expiration_;
00122 
00123         // we impose an arbitrary cap of 1K on the payload data to be echoed
00124         size_t payload_len = bundle->payload_.length();
00125 
00126         u_char buf[1024];
00127         const u_char* bp = bundle->payload_.read_data(0, payload_len, buf);
00128         reply->payload_.set_data(bp, payload_len);
00129         
00130         BundleDaemon::post(new BundleReceivedEvent(reply, EVENTSRC_ADMIN, payload_len));
00131         
00132         break;
00133     }   
00134     case BundleProtocol::ADMIN_NULL:
00135     {
00136         log_info("ADMIN_NULL from %s", bundle->source_.c_str());
00137         break;
00138     }
00139     case BundleProtocol::ADMIN_ANNOUNCE:
00140     {
00141         log_info("ADMIN_ANNOUNCE from %s", bundle->source_.c_str());
00142         break;
00143     }
00144         
00145     default:
00146         log_warn("unexpected admin bundle with type 0x%x *%p",
00147                  typecode, bundle);
00148     }    
00149 
00150  done:
00151     BundleDaemon::post(new BundleDeliveredEvent(bundle, this));
00152 }
00153 
00154 
00155 } // namespace dtn

Generated on Fri Dec 22 14:47:57 2006 for DTN Reference Implementation by  doxygen 1.5.1