Wiselib
wiselib.testing/algorithms/radio/secure/secure_radio.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 /*
00021  * Author: Henning Hasemann <hasemann@ibr.cs.tu-bs.de>
00022  */
00023 
00024 #ifndef __ALGORITHMS_RADIO_SECURE_RADIO_H__
00025 #define __ALGORITHMS_RADIO_SECURE_RADIO_H__
00026 
00027 #include "util/base_classes/radio_base.h"
00028 
00029 #include <iostream>
00030 
00031 namespace wiselib {
00032    
00033    template<typename OsModel_P, typename Radio_P, typename Cipher_P>
00034    class SecureRadio
00035       : public RadioBase<OsModel_P, typename Radio_P::node_id_t, typename Radio_P::size_t, typename Radio_P::block_data_t>
00036    {
00037       public:
00038          typedef OsModel_P OsModel;
00039          typedef Radio_P Radio;
00040          typedef Cipher_P cipher_t;
00041          typedef SecureRadio<OsModel, Radio, cipher_t> self_type;
00042          
00043          typedef typename Radio::node_id_t node_id_t;
00044          typedef typename Radio::size_t size_t;
00045          typedef typename Radio::block_data_t block_data_t;
00046          typedef typename Radio::message_id_t message_id_t;
00047          
00048          enum ReturnValues {
00049             SUCCESS = OsModel::SUCCESS //, ERR_UNSPEC = OsModel::ERR_UNSPEC
00050          };
00051          
00052          enum SpecialNodeIds {
00053             BROADCAST_ADDRESS = Radio::BROADCAST_ADDRESS,
00054             NULL_NODE_ID = Radio::NULL_NODE_ID,
00055          };
00056          
00057          enum Restrictions {
00058             MAX_MESSAGE_LENGTH = Radio::MAX_MESSAGE_LENGTH
00059          };
00060          
00061          int init(Radio& radio, cipher_t& cipher) {
00062             radio_ = &radio;
00063             cipher_ = &cipher;
00064             //radio_->template reg_recv_callback<SecureRadio, &SecureRadio::receive>(this);
00065             return SUCCESS;
00066          }
00067          
00068          void enable_radio() {
00069             radio_->enable_radio();
00070             radio_->template reg_recv_callback<SecureRadio, &SecureRadio::receive>(this);
00071          }
00072          void disable_radio() { radio_->disable_radio(); }
00073          node_id_t id() { return radio_->id(); }
00074          
00075          void send(node_id_t receiver, size_t size, block_data_t* data);
00076          void receive(node_id_t from, size_t size, block_data_t* data);
00077          
00078       private:
00079          typename Radio::self_pointer_t radio_;
00080          cipher_t* cipher_;
00081    };
00082    
00083    template<typename OsModel_P, typename Radio_P, typename Cipher_P>
00084    void
00085    SecureRadio<OsModel_P, Radio_P, Cipher_P>::
00086 	send(node_id_t receiver, size_t size, block_data_t* data) {
00087       block_data_t buffer[size];
00088       cipher_->encrypt(data, buffer, size);
00089       radio_->send(receiver, size, buffer);
00090    }
00091    
00092    template<typename OsModel_P, typename Radio_P, typename Cipher_P>
00093    void
00094    SecureRadio<OsModel_P, Radio_P, Cipher_P>::
00095 	receive(node_id_t sender, size_t size, block_data_t* data) {
00096       block_data_t buffer[size];
00097       cipher_->decrypt(data, buffer, size);
00098       notify_receivers(sender, size, buffer);
00099    }
00100 
00101 } // namespace
00102 
00103 #endif // __ALGORITHMS_RADIO_SECURE_RADIO_H__
00104 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines