Wiselib
wiselib.testing/algorithms/routing/sec/sec_routing.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 
00020 #ifndef __ALGORITHMS_ROUTING_SECROUTING_H__
00021 #define __ALGORITHMS_ROUTING_SECROUTING_H__
00022 
00023 #include "util/base_classes/routing_base.h"
00024 
00025 namespace wiselib
00026 {
00036    template<typename OsModel_P,
00037             typename Radio_P,
00038             typename crypto_P,
00039             typename Routing_P>
00040    class SecRouting
00041       : public RoutingBase<OsModel_P, Radio_P>
00042    {
00043    public:
00044       typedef OsModel_P OsModel;
00045       typedef Radio_P Radio;
00046       typedef crypto_P crypto_t;
00047       typedef Routing_P routing_t;
00048       typedef SecRouting<OsModel, Radio_P,crypto_P, Routing_P> self_type;
00049 
00050       typedef typename Radio::node_id_t node_id_t;
00051       typedef typename Radio::size_t size_t;
00052       typedef typename Radio::block_data_t block_data_t;
00053       typedef uint8_t message_id_t;
00054       // --------------------------------------------------------------------
00055       enum SpecialNodeIds {
00056          BROADCAST_ADDRESS = Radio_P::BROADCAST_ADDRESS, 
00057          NULL_NODE_ID      = Radio_P::NULL_NODE_ID      
00058       };
00059       // --------------------------------------------------------------------
00060       enum Restrictions {
00061          MAX_MESSAGE_LENGTH = Radio_P::MAX_MESSAGE_LENGTH   
00062       };
00063       // --------------------------------------------------------------------
00066       SecRouting();
00067       ~SecRouting();
00069       // --------------------------------------------------------------------
00070       int init( routing_t& routing, crypto_t& crypto )
00071       { routing_ = &routing; crypto_ = &crypto; }
00072       // --------------------------------------------------------------------
00075 
00077       void enable_radio( void );
00080       void disable_radio( void );
00083       void send( node_id_t receiver, size_t len, block_data_t *data );
00086       void receive( node_id_t from, size_t len, block_data_t *data );
00089       typename Radio::node_id_t id()
00090       {
00091          return radio_->id();
00092       };
00094 
00095    private:
00096       routing_t* routing_;
00097       crypto_t* crypto_;
00098    };
00099    // -----------------------------------------------------------------------
00100    // -----------------------------------------------------------------------
00101    // -----------------------------------------------------------------------
00102    template<typename OsModel_P,
00103             typename Radio_P,
00104             typename crypto_P,
00105             typename Routing_P>
00106    SecRouting<OsModel_P, Radio_P,crypto_P, Routing_P>::
00107    SecRouting()
00108    {}
00109    // -----------------------------------------------------------------------
00110    template<typename OsModel_P,
00111             typename Radio_P,
00112             typename crypto_P,
00113             typename Routing_P>
00114    SecRouting<OsModel_P, Radio_P,crypto_P, Routing_P>::
00115    ~SecRouting()
00116    {}
00117    // -----------------------------------------------------------------------
00118    template<typename OsModel_P,
00119             typename Radio_P,
00120             typename crypto_P,
00121             typename Routing_P>
00122    inline void
00123    SecRouting<OsModel_P,Radio_P ,crypto_P, Routing_P>::
00124    enable_radio( void )
00125    {
00126       routing_->enable_radio();
00127       routing_->reg_recv_callback<self_type,&self_type::receive>(this);
00128       crypto_->enable();
00129    }
00130    // -----------------------------------------------------------------------
00131    template<typename OsModel_P,
00132             typename Radio_P,
00133             typename crypto_P,
00134             typename Routing_P>
00135    void
00136    SecRouting<OsModel_P, Radio_P,crypto_P, Routing_P>::
00137    disable_radio( void )
00138    {
00139 
00140    }
00141    // -----------------------------------------------------------------------
00142    template<typename OsModel_P,
00143             typename Radio_P,
00144             typename crypto_P,
00145             typename Routing_P>
00146    void
00147    SecRouting<OsModel_P, Radio_P,crypto_P, Routing_P>::
00148    send( node_id_t receiver, size_t len, block_data_t *data )
00149    {
00150       uint8 b[len];
00151       crypto_->encrypt( data, b, len );
00152       routing_->send( receiver, len, b );
00153    }
00154    // -----------------------------------------------------------------------
00155    template<typename OsModel_P,
00156             typename Radio_P,
00157             typename crypto_P,
00158             typename Routing_P>
00159    void
00160    SecRouting<OsModel_P,Radio_P ,crypto_P, Routing_P>::
00161    receive( node_id_t from, size_t len, block_data_t *data )
00162    {
00163       uint8 b[len];
00164       crypto_->decrypt( data, b, len);
00165       notify_receivers( from, len, b );
00166    }
00167 }
00168 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines