Wiselib
wiselib.testing/algorithms/group-key/keylevels_main.h
Go to the documentation of this file.
00001 #ifndef _KEYLEVELS_MAIN_H
00002 #define _KEYLEVELS_MAIN_H
00003 
00004 // maximal connectivity (key-wise) that can be reached by a node
00005 #define NODES_MAX 20
00006 
00007 #define WAIT_FOR_ROUTING 25000
00008 //[PTB]  disable (0) enable (1) crypto
00009 #define CRYPTO 1
00010 
00011 #include "util/serialization/simple_types.h"
00012 #include "util/base_classes/radio_base.h"
00013 
00014 #include "keylevels_message.h"
00015 #include "keylevels_share.h"
00016 #include "ttl_flooding.h"
00017 
00018 namespace wiselib {
00019 
00020 // a struct to hold group key with some test data 
00021 struct gkmsg 
00022 {  
00023    uint8_t hash;
00024    uint8_t enc_key_data[GROUP_KEY_SIZE]; 
00025    uint8_t test[16];
00026 };
00027 
00028 
00029 template<typename OsModel_P,
00030       typename Radio_P,
00031       typename Routing_P,
00032       typename Crypto_P,
00033       typename Clustering_P,
00034       typename Debug_P = typename OsModel_P::Debug,
00035       typename Timer_P = typename OsModel_P::Timer,
00036       typename Random_P = typename OsModel_P::Rand>
00037 class Keylevels: public RadioBase<OsModel_P, typename Radio_P::node_id_t,
00038       typename Radio_P::size_t, typename Radio_P::block_data_t> {
00039 public:
00040    typedef OsModel_P OsModel;
00041    typedef Radio_P Radio;
00042    typedef Routing_P Routing;
00043    typedef Crypto_P Crypto;
00044    typedef Debug_P Debug;
00045    typedef Timer_P Timer;
00046    typedef Random_P Random;
00047    typedef Clustering_P Clustering;
00048 
00049    typedef Keylevels<OsModel, Radio, Routing, Crypto, Clustering, Debug, Timer, Random> self_type;
00050    typedef self_type* self_pointer_t;
00051 
00052    typedef typename Radio::node_id_t node_id_t;
00053    typedef typename Radio::size_t size_t;
00054    typedef typename Radio::block_data_t block_data_t;
00055    typedef typename Radio::message_id_t message_id_t;
00056 
00057    typedef KeylevelsMessage<OsModel, Radio> KeylevelMessage;
00058 
00059    typedef KeyShare<OsModel, Radio> keyshare_t;
00060    typedef TTLFlooding<OsModel, Radio, Debug, NODES_MAX> radio_ttl_t;
00061 
00062    enum ReturnValues {
00063       SUCCESS = OsModel::SUCCESS
00064    };
00065 
00066    Keylevels() 
00067    {
00068 #ifdef GROUP_KEY_EVALUATION
00069       keyOfferMsgCnt_in  = 0;
00070       keyOfferMsgCnt_out = 0;
00071       groupKeyMsgCnt_in  = 0; 
00072       groupKeyMsgCnt_out = 0;
00073       neighborMsgCnt_in  = 0;
00074       neighborMsgCnt_out = 0;
00075       groupKeyFailed     = 0;
00076 #endif
00077       nnTimerSet = false;
00078       _wait_for_routing = WAIT_FOR_ROUTING;
00079    }
00080 
00081    int init(Radio& radio, Routing& routing, Crypto& crypto, Clustering& clustering, Debug& debug,
00082          Timer& timer, Random& random) {
00083       radio_ = &radio;
00084       routing_ = &routing;
00085       crypto_ = &crypto;
00086       debug_ = &debug;
00087       timer_ = &timer;
00088       random_ = &random;
00089       clustering_ = &clustering;
00090       if ( clustering_->is_cluster_head() )
00091       {
00092 #ifdef KEYLEVELS_DEBUG_SOFT
00093          debug_->debug("[KL] {%d} leader (KS: %d, KP: %d, KL: %d)",
00094             radio_->id(),
00095             KEYSHARE_SIZE, KEYPOOL_SIZE, KEY_LEVELS
00096             );
00097 #endif
00098 #ifdef KEYLEVELS_DEMO   
00099          debug_->debug("GKE_LEADER;%x",radio_->id());
00100          
00101 #ifdef SHAWN
00102                         debug_->debug("\n");
00103 #endif
00104 #endif
00105          group_key_.leader_init(random_);
00106          leader_offer_sent = false; // [PTB]
00107          notify_receivers(0, (size_t) GROUP_KEY_SIZE,
00108                (block_data_t*) &(group_key_.key_data));
00109 
00110       }
00111       else
00112       {
00113 #ifdef KEYLEVELS_DEBUG_SOFT
00114          debug_->debug("[KL] {%d} not a leader (KS: %d, KP: %d, KL: %d)",
00115              radio_->id(),
00116             KEYSHARE_SIZE, KEYPOOL_SIZE, KEY_LEVELS
00117             );
00118 #endif
00119          group_key_.init(random_);
00120          leader_offer_sent = true;  // [PTB]
00121       }
00122 
00123       keyshare_.init(*radio_, *debug_, *random_);
00124 
00125 #ifdef KEYLEVELS_DEBUG
00126       keyshare_.listKeyshare();
00127 #endif
00128 
00129 #ifdef KEYLEVELS_DEMO
00130       keyshare_.compactListKeyshare(radio_->id());
00131 #ifdef SHAWN
00132                 debug_->debug("\n");
00133 #endif
00134 #endif 
00135 
00136       routing_->template reg_recv_callback<self_type,
00137             &self_type::message_received> (this);
00138 
00139       /*
00140        *  PTB: odkomentuj init_ttl_op() zakomentuj timer_-> ... zeby wylaczyc czekanie na sasiadow
00141        */
00142 
00143       have_neighbors = true;
00144 //    timer_->template set_timer<self_type,
00145 //          &self_type::init_ttl_op> (10, this, 0);
00146       init_ttl_op(0);
00147 
00148 /*
00149       timer_->template set_timer<self_type,
00150             &self_type::start_neighbor_seek_init> (10, this, 0);
00151 */
00152       return SUCCESS;
00153    }
00154 
00155 
00156    
00157 /*
00158    // prepare neighborhood discovery
00159    void start_neighbor_seek_init(void*) {
00160       have_neighbors = false;
00161       radio_->template reg_recv_callback<self_type, &self_type::neighbor_message_received> (this);
00162       start_neighbor_seek(this);
00163    }
00164 
00165    void neighbor_message_received(node_id_t from, size_t size,
00166          block_data_t* data) {
00167 
00168       if(from == radio_->id())
00169          return;
00170 
00171       KeylevelMessage* message = (KeylevelMessage *) data;
00172 
00173       // drop msgs from other clusters
00174       if (message->get_cluster() != clustering_->cluster_id())
00175          return;
00176 
00177       if (message->message_type() == NEIGHBORHOOD_ACK) {
00178 #ifdef KEYLEVELS_DEBUG
00179          debug_->debug("[KL] {%d} received NEIGHBORHOOD_ACK from %d\n", radio_->id(), from);
00180 #endif
00181 #ifdef GROUP_KEY_EVALUATION
00182          // counting ---------------------------------------------
00183          neighborMsgCnt_in++;
00184 #endif
00185          if (have_neighbors)  return;  
00186 
00187          have_neighbors = true;
00188          init_ttl_op();
00189 
00190       } else if (message->message_type() == NEIGHBORHOOD_SEEK) {
00191 #ifdef KEYLEVELS_DEBUG
00192          debug_->debug("[KL] {%d} sending NEIGHBORHOOD_ACK to %d\n", radio_->id(), from);
00193 #endif
00194          KeylevelMessage msg;
00195          msg.set_message_type(NEIGHBORHOOD_ACK);
00196          msg.set_source(radio_->id());
00197          msg.set_destination(from);
00198          msg.set_payload(0, NULL);
00199          msg.set_cluster(clustering_->cluster_id());
00200          radio_->send(from, msg.buffer_size(), (block_data_t*) &msg);
00201 #ifdef GROUP_KEY_EVALUATION
00202          // counting ---------------------------------------------
00203          neighborMsgCnt_out++;
00204 #endif
00205          if (have_neighbors) {
00206             return;
00207          }
00208          have_neighbors = true;
00209          init_ttl_op();
00210       }
00211    }
00212 
00213    // do neighbor discovery 
00214    void start_neighbor_seek(void*)
00215    {
00216       if (have_neighbors) return;  // okay, we've already got someone to talk to
00217 
00218       // prepare neighbor seek message and send it; go to sleep waiting for response 
00219 #ifdef KEYLEVELS_DEBUG
00220       debug_->debug("[KL] {%d} broadcasting NEIGHBORHOOD_SEEK\n", radio_->id());
00221 #endif
00222       KeylevelMessage message;
00223       message.set_message_type(NEIGHBORHOOD_SEEK);
00224       message.set_source(radio_->id());
00225       message.set_destination(radio_->BROADCAST_ADDRESS);
00226       message.set_cluster(clustering_->cluster_id());
00227       message.set_payload(0, NULL);
00228       radio_->send(radio_->BROADCAST_ADDRESS, message.buffer_size(),(block_data_t*) &message);
00229 #ifdef GROUP_KEY_EVALUATION
00230       neighborMsgCnt_out++;
00231 #endif
00232       timer_->template set_timer<self_type, &self_type::start_neighbor_seek> (
00233             15000, this, 0);
00234    }
00235 */
00239    void init_ttl_op(void*d=NULL)
00240    {
00241       ttl_.init(*radio_, *debug_);
00242       ttl_.set_notify_all_on_path(false);
00243       ttl_.set_cluster(clustering_->cluster_id());
00244       ttl_.enable_radio();
00245       ttl_.template reg_recv_callback<self_type,
00246             &self_type::key_offer_received> (this);
00247 
00248       if(clustering_->is_cluster_head())
00249          timer_->template set_timer<self_type, &self_type::seek_for_group_key> (
00250             _wait_for_routing, this, 0);
00251       else
00252          timer_->template set_timer<self_type, &self_type::seek_for_group_key> (
00253             _wait_for_routing + (radio_->id() % 5) * 1000, this, 0);
00254    }
00255 
00256    void seek_for_group_key(void*)
00257    {
00258       if (!group_key_found() || !leader_offer_sent)
00259       {
00260          increase_seek_ttl();
00261 
00262 #ifdef KEYLEVELS_DEBUG
00263          debug_->debug("[KL] {%d} setting TTL = %d\n", radio_->id(), ttl_.get_ttl());
00264 #endif
00265 
00266          broadcast_all_keys_with_ttl();
00267 
00268          timer_->template set_timer<self_type,
00269                &self_type::seek_for_group_key> (
00270                get_current_seek_timeout(), this, 0);
00271       }
00272    }
00273 
00274    group_key* get_group_key()
00275    {
00276       if (group_key_.have_key)
00277          return &group_key_;
00278 
00279       return NULL;
00280    }
00281 
00282    inline int get_current_seek_timeout()
00283    {
00284       return (3 * get_current_ttl() * 1000) + 2000;
00285    }
00286 
00287    inline int get_current_ttl()
00288    {
00289       return ttl_.get_ttl();
00290    }
00291 
00292    inline void increase_seek_ttl()
00293    {
00294       return ttl_.set_ttl(ttl_.get_ttl() + 1);
00295    }
00296 
00297    inline bool group_key_found()
00298    {
00299       return group_key_.have_key;
00300    }
00301 
00302    void enable_radio() {   radio_->enable_radio();    }
00303 
00304    void disable_radio()
00305    { }
00306 
00307 
00311    void broadcast_all_keys_with_ttl()
00312    {
00313 
00314       key_info allKeys[KEYSHARE_SIZE+1];
00315    
00316       // checksum
00317       uint16_t sumKi = 0;
00318       for(int i=1; i < KEYSHARE_SIZE; i++)
00319       {
00320          allKeys[i].key_index = keyshare_.keyshare[i].key_index;
00321          allKeys[i].key_level = keyshare_.keyshare[i].key_level;
00322          // count checksum
00323          sumKi ^= allKeys[i].key_index;
00324       }
00325       // test field [PTB]
00326       allKeys[0].key_index = sumKi;
00327       allKeys[0].key_level = 0; // nothing to send here, so send 0
00328       ttl_.send(1000 * ttl_.get_ttl() + 100 * radio_->id() + allKeys[1].key_index,
00329                sizeof(key_info)*KEYSHARE_SIZE+1, 
00330                (block_data_t*) allKeys);
00331 #ifdef GROUP_KEY_EVALUATION
00332       keyOfferMsgCnt_out++;
00333 #endif
00334 #ifdef KEYLEVELS_DEBUG_SOFT
00335       debug_->debug("[KL] {%d} sending key offer with MSG_ID = %d\n", 
00336                radio_->id(), 
00337                1000*ttl_.get_ttl()+100*radio_->id()+allKeys[1].key_index
00338                  );
00339 #endif
00340 
00341    }
00342 
00343    void send_group_key_to_node( void *d )
00344    {
00345       send_group_key( *(node_id_t*)d );
00346    }
00347 
00348 
00349    void send_group_key_to_all_trusted_neighbors(void *d=NULL)
00350    {
00351       typename keyshare_t::trusted_links_it it = keyshare_.tl_start();
00352       while(it != keyshare_.tl_end())
00353       {
00354          send_group_key(it->first);
00355          it++;
00356       }
00357       nnTimerSet = false;
00358 
00359    }
00360 
00361    /* builds a KEY_ACK message and tries to send it to $receiver
00362     * @return true if message was properly routed
00363     *          false if routing does not reach $receiver
00364     */
00365    bool send_key_ack(node_id_t receiver, key_info* key_info)
00366    {
00367 #ifdef KEYLEVELS_DEBUG_SOFT
00368       debug_->debug("[KL] {%d} sending ACK (%d,%d) to Node %d", 
00369             radio_->id(), 
00370             key_info->key_index, 
00371             key_info->key_level, 
00372             receiver
00373               );
00374 #ifdef SHAWN
00375       debug_->debug("\n");
00376 #endif
00377 #endif
00378 
00379       KeylevelMessage message;
00380       message.set_message_type(KEY_ACK);
00381       message.set_source(radio_->id());
00382       message.set_destination(receiver);
00383       message.set_cluster(clustering_->cluster_id());
00384       message.set_payload(sizeof(key_info), (block_data_t*) key_info);
00385 
00386 //    if(Routing::SUCCESS != routing_->send(receiver, message.buffer_size(),
00387 //          (block_data_t*) &message))
00388 //       return false;
00389       routing_->send(receiver, message.buffer_size(),
00390             (block_data_t*) &message);
00391 #ifdef GROUP_KEY_EVALUATION
00392       keyOfferMsgCnt_out++;
00393 #endif
00394       return true;
00395    }
00396 
00397 
00398    bool send_group_key(node_id_t receiver)
00399    {
00400 #ifdef KEYLEVELS_DEBUG_SOFT
00401       debug_->debug("[KL] {%d} sending group key to Node %d\n", radio_->id(), receiver);
00402 #endif
00403 #ifdef KEYLEVELS_DEMO
00404 //    debug_->debug("GKE_GKB;%x;%x",radio_->id(),receiver);
00405 #ifdef SHAWN
00406                 debug_->debug("\n");
00407 #endif
00408 #endif
00409       struct gkmsg gk;
00410 
00411       KeylevelMessage message;
00412       message.set_message_type(GROUP_KEY);
00413       message.set_source(radio_->id());
00414       message.set_destination(receiver);
00415       message.set_cluster(clustering_->cluster_id());
00416       uint8_t testString[16] = {1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0};    
00417 
00418 #ifdef KEYLEVELS_USE_CRYPTO
00419       crypto_->key_setup(SMALL_KEY_SIZE * 8,
00420             keyshare_.get_key_info(receiver)->key_value);
00421       crypto_->encrypt(group_key_.key_data, gk.enc_key_data);
00422       crypto_->encrypt(testString, gk.test);
00423 
00424 #else
00425       memcpy(gk.enc_key_data, group_key_.key_data, GROUP_KEY_SIZE);
00426       memcpy(gk.test, testString, 16);
00427 #endif
00428       message.set_payload(sizeof(gkmsg), (block_data_t*) &gk);
00429 
00430 #ifdef KEYLEVELS_DEBUG_SOFT
00431       debug_->debug("[KL] {%d} sending GK testmsg (size: %d) to %d (%d:%d): %d (%d) %d (%d) %d (%d)", 
00432                radio_->id(),
00433                sizeof(gkmsg),
00434                receiver,
00435                keyshare_.get_key_info(receiver)->key_index,
00436                keyshare_.get_key_info(receiver)->key_level,
00437                testString[0],
00438                gk.test[0],
00439                testString[1],
00440                gk.test[1],
00441                testString[2],
00442                gk.test[2]
00443             );
00444 #ifdef SHAWN
00445          debug_->debug("\n");
00446 #endif
00447 #endif
00448 
00449 
00450 
00451 //    if(Routing::SUCCESS != routing_->send(receiver, message.buffer_size(),(block_data_t*) &message))
00452 //    {
00453 //       return false;
00454 //    }
00455       routing_->send(receiver, message.buffer_size(),(block_data_t*) &message);
00456 #ifdef GROUP_KEY_EVALUATION
00457       groupKeyMsgCnt_out++;
00458 #endif
00459       return true;
00460    }
00461 
00462    void message_received(node_id_t from, size_t size, block_data_t* data)
00463    {
00464       KeylevelMessage* message = (KeylevelMessage *) data;
00465 
00466       if(message->get_cluster() != clustering_->cluster_id())
00467          return;
00468 
00469 
00470       if (message->message_type() == KEY_ACK)
00471       {
00472 #ifdef GROUP_KEY_EVALUATION
00473          keyOfferMsgCnt_in++;
00474 #endif
00475       key_info ka, *key_ack;
00476       key_ack = &ka;
00477       memcpy(key_ack, message->payload(), sizeof(key_info));   
00478 #ifdef KEYLEVELS_DEBUG_SOFT
00479          debug_->debug("[KL] {%d} received KEY_ACK (%d,%d) from Node %d", 
00480             radio_->id(),
00481             key_ack->key_index,
00482             key_ack->key_level,
00483             from);
00484 #ifdef SHAWN
00485          debug_->debug("\n");
00486 #endif
00487 #endif      
00488          key k;
00489          k.key_index = key_ack->key_index;
00490          memcpy(k.key_value,
00491                keyshare_.get_key(key_ack->key_index)->key_value,
00492                SMALL_KEY_SIZE);
00493          int ld = key_ack->key_level
00494                - keyshare_.get_key(key_ack->key_index)->key_level;
00495          if (ld >= 0)
00496          {
00497             k.key_level = key_ack->key_level;
00498             for (int i = 0; i < ld; i++)
00499                keyshare_.variation_on_SDBMHash(k.key_value, SMALL_KEY_SIZE);
00500          }
00501          else
00502          {
00503             k.key_level = keyshare_.get_key(key_ack->key_index)->key_level;
00504          }
00505          if(check_for_better_trusted_link(from, &k)){
00506             store_new_trusted_link(from, &k);
00507          }
00508 
00509       }
00510       else if (message->message_type() == GROUP_KEY)
00511       {
00512 #ifdef KEYLEVELS_DEBUG_SOFT
00513          debug_->debug("[KL] {%d} received GROUP_KEY message from Node %d", radio_->id(), from);
00514 #ifdef SHAWN
00515          debug_->debug("\n");
00516 #endif
00517 #endif
00518 #ifdef GROUP_KEY_EVALUATION
00519          groupKeyMsgCnt_in++;
00520 #endif
00521          // don't bother if already have group key
00522          if (group_key_.have_key)
00523          { return; }
00524          // drop from not trusted nodes [PTB]
00525          if (!keyshare_.trusted_link_exists(from))
00526          { return; }
00527 
00528       struct gkmsg gk;
00529 
00530       memcpy(&gk, message->payload(), sizeof(gkmsg));
00531 
00532       uint8_t dec_key_data[SMALL_KEY_SIZE];
00533 
00534 #ifdef KEYLEVELS_USE_CRYPTO
00535       crypto_->enable();
00536       crypto_->key_setup(SMALL_KEY_SIZE * 8,
00537                (uint8_t*) keyshare_.get_key_info(from)->key_value);
00538       crypto_->decrypt(gk.test, dec_key_data);
00539 #else
00540       memcpy(dec_key_data,gk.test,GROUP_KEY_SIZE);
00541 #endif
00542 
00543 #ifdef KEYLEVELS_DEBUG_SOFT
00544       debug_->debug("[KL] {%d} decrypting testmsg (size: %d) from %d (%d,%d): %d (%d) %d (%d) %d (%d)", 
00545                radio_->id(),
00546                sizeof(gkmsg),
00547                from,
00548                keyshare_.get_key_info(from)->key_index,
00549                keyshare_.get_key_info(from)->key_level,
00550                dec_key_data[0],
00551                gk.test[0],
00552                dec_key_data[1],
00553                gk.test[1],
00554                dec_key_data[2],
00555                gk.test[2]
00556             );
00557 #ifdef SHAWN
00558          debug_->debug("\n");
00559 #endif
00560 #endif
00561          if(dec_key_data[0] != 1 || dec_key_data[1] != 2 || dec_key_data[2] != 3)
00562          {
00563 #ifdef KEYLEVELS_DEBUG_SOFT
00564             debug_->debug("[KL] {%d} this key message is impoperly encrypted!\n", radio_->id());
00565 #ifdef SHAWN
00566          debug_->debug("\n");
00567 #endif
00568 #endif
00569 #ifdef GROUP_KEY_EVALUATION
00570             groupKeyFailed++;
00571 #endif
00572             return;
00573          }
00574          // integrity test OK, decrypt the group key now
00575 /*
00576 #ifdef KEYLEVELS_USE_CRYPTO
00577          crypto_->decrypt(gk.enc_key_data, dec_key_data);
00578 #else
00579          memcpy(dec_key_data, gk.enc_key_data,GROUP_KEY_SIZE); 
00580 #endif
00581 */
00582          if (!group_key_.have_key)
00583          {
00584 #ifdef KEYLEVELS_USE_CRYPTO
00585          crypto_->decrypt(gk.enc_key_data, dec_key_data);
00586 #else
00587          memcpy(dec_key_data, gk.enc_key_data,GROUP_KEY_SIZE); 
00588 #endif
00589 
00590 
00591             group_key_.set_key(dec_key_data);
00592 #ifdef KEYLEVELS_DEBUG
00593             print_key_value(group_key_.key_data, GROUP_KEY_SIZE);
00594 #endif
00595 
00596 
00597 /*
00598             timer_->template set_timer<self_type,
00599                &self_type::send_group_key_to_all_trusted_neighbors> 
00600                (5000, this, 0);
00601 */
00602             notify_receivers(0, (size_t) GROUP_KEY_SIZE,
00603                   (block_data_t*) &(group_key_.key_data));
00604 
00605             notify_neighbors();
00606 
00607          }
00608       }
00609       else
00610       {
00611 #ifdef KEYLEVELS_DEBUG
00612          debug_->debug("[KL] {%d} message_received: received UNKNOWN message from Node %d\n", 
00613                   radio_->id(), 
00614                   from);
00615 #endif
00616       }
00617    }
00618 
00619 
00620    void notify_neighbors(void* d = NULL)
00621    {
00622       if(nnTimerSet) return;
00623       nnTimerSet = true;
00624       timer_->template set_timer<self_type,
00625             &self_type::send_group_key_to_all_trusted_neighbors> 
00626             (5000, this, 0);
00627    }
00628 
00629    void key_offer_received(node_id_t from, size_t size, block_data_t* data)
00630    {
00631 #ifdef KEYLEVELS_DEBUG_SOFT
00632       debug_->debug("[KL] {%d} received key offer from Node %d", radio_->id(), from);
00633 #ifdef SHAWN
00634          debug_->debug("\n");
00635 #endif
00636 #endif
00637 #ifdef GROUP_KEY_EVALUATION
00638       keyOfferMsgCnt_in++;
00639 #endif
00640       key_info allKeys[KEYSHARE_SIZE+1];
00641 
00642      memcpy(&allKeys, data, sizeof(key_info)*KEYSHARE_SIZE+1);
00643 
00644      // checksum 
00645      uint16_t sumKi = 0;
00646      for(int i=1;i<KEYSHARE_SIZE;i++)
00647      {
00648       sumKi ^= allKeys[i].key_index;
00649      }
00650      if(allKeys[0].key_index != sumKi )
00651      {
00652 #ifdef KEYLEVELS_DEBUG
00653       debug_->debug("[KL] {%d} received corrupted key offer from Node %d", 
00654             radio_->id(), 
00655             from);
00656 #endif
00657       return;
00658      }   
00659 
00660 
00661       for(unsigned int i=1; i<KEYSHARE_SIZE; ++i)
00662       {
00663          if(keyshare_.owns_key(allKeys[i].key_index))
00664          {
00665          key commonKey, *myKey;
00666          myKey = keyshare_.get_key(allKeys[i].key_index);
00667 #ifdef KEYLEVELS_DEBUG
00668          debug_->debug("[KL] {%d} found common key (%d,%d):(%d,%d) with Node %d\n",
00669                radio_->id(),
00670                myKey->key_index,
00671                myKey->key_level,
00672                allKeys[i].key_index,
00673                allKeys[i].key_level,
00674                from
00675                );
00676 #endif         
00677 
00678          commonKey.key_index = allKeys[i].key_index;
00679          memcpy(     commonKey.key_value,
00680                myKey->key_value,
00681                SMALL_KEY_SIZE
00682             );
00683          int ld = allKeys[i].key_level - myKey->key_level;
00684          if (ld >= 0)      // adjusting our key up to match common keylevel
00685          {
00686             // get his level
00687             commonKey.key_level = allKeys[i].key_level;
00688             // apply hash to our key
00689             for (int i = 0; i < ld; i++)
00690             {
00691                keyshare_.variation_on_SDBMHash(commonKey.key_value, SMALL_KEY_SIZE);
00692             }
00693          }
00694          else        // our key has higher level which will be used
00695          {
00696             commonKey.key_level = myKey->key_level;
00697          }
00698 
00699          if(check_for_better_trusted_link(from, &commonKey))
00700          {
00701             key_info key_ack;
00702             key_ack.key_index = commonKey.key_index;
00703             key_ack.key_level = commonKey.key_level;
00704 
00705             if(!send_key_ack(from, &key_ack))
00706                return;
00707 
00708             store_new_trusted_link(from, &commonKey);
00709          }
00710          if (group_key_found())
00711          {
00712 
00713 //          uint32_t buf = 0;
00714 //          memcpy(&buf, &from, 2);
00715          // for (;;)
00716          //    if i->id == from
00717          //    (void*)&(*i)
00718 /*
00719             timer_->template set_timer<self_type,
00720                &self_type::send_group_key_to_all_trusted_neighbors> 
00721                (5000, this, 0);
00722 */
00723             notify_neighbors();
00724          }
00725          // one key is enough, don't look anymore
00726          return;
00727          }
00728       }
00729    }  
00730 
00731    //returns true only if new or better(with smaller key index) link has been established
00732    bool check_for_better_trusted_link(node_id_t node, key* key)
00733    {
00734       if (!keyshare_.trusted_link_exists(node))
00735       {
00736 #ifdef KEYLEVELS_DEBUG
00737          debug_->debug("[KL] {%d} found better secure link with Node %d\n", radio_->id(), node);
00738 #endif
00739          return true;
00740       }
00741       else
00742       {
00743          if (key->key_index < keyshare_.get_key_info(node)->key_index)
00744          {
00745 #ifdef KEYLEVELS_DEBUG
00746          debug_->debug("[KL] {%d} found better secure link with Node %d\n", radio_->id(), node);
00747 #endif
00748             return true;
00749          }
00750          return false;
00751       }
00752    }
00753 
00754    void store_new_trusted_link(node_id_t node, key* key){
00755 #ifdef KEYLEVELS_DEBUG_SOFT
00756       debug_->debug("[KL] {%d} stored (%d, %d) for Node %d",
00757             radio_->id(),
00758             key->key_index,
00759             key->key_level,
00760             node
00761             );
00762 #ifdef SHAWN
00763          debug_->debug("\n");
00764 #endif
00765 #endif
00766 
00767 #ifdef KEYLEVELS_DEMO
00768       debug_->debug("GKE_SL;%x;%x;%d;%d",radio_->id(),node,key->key_index,key->key_level);
00769 #ifdef SHAWN
00770                         debug_->debug("\n");
00771 #endif
00772 #endif 
00773       keyshare_.put_trusted_link(node, *key);
00774    }
00775 
00776 #ifdef KEYLEVELS_DEBUG_SOFT
00777    void print_key_value(uint8_t* value, uint8_t size)
00778    {
00779 #ifdef SHAWN
00780       for (int i = 0; i < size; i++)
00781       {
00782          debug_->debug("%d:", value[i]);
00783       }
00784       debug_->debug("\n");
00785 #else
00786       debug_->debug("%d:%d:%d:%d:%d ...", value[0], value[1], value[2], value[3], value[4]);
00787 #endif
00788    }
00789 #endif
00790 
00791 #ifdef KEYLEVELS_DEBUG
00792    void print_all_key_info(key* k)
00793    {
00794       debug_->debug("[KL] {%d} KEY: (%d,%d) \n", radio_->id(), k->key_index, k->key_level);
00795       print_key_value(k->key_value, SMALL_KEY_SIZE);
00796    }
00797 #endif
00798 
00799 #ifdef GROUP_KEY_EVALUATION
00800    //------------ fields for stats [PTB] -------------------------------------------
00801    int neighborMsgCnt_in, neighborMsgCnt_out;   // messages for seeking neighbors
00802    int keyOfferMsgCnt_in, keyOfferMsgCnt_out;   // messages with key offer
00803    int groupKeyMsgCnt_in, groupKeyMsgCnt_out;   // messages referring to groupkey
00804    int groupKeyFailed;           // mis-encrypted group key messages counter [PTB]
00805 #endif
00806 
00807    inline void wait_for_routing(uint32_t msecs)
00808    {
00809       _wait_for_routing = msecs;
00810 
00811    }
00812 
00813 private:
00814    typename Radio::self_pointer_t radio_;
00815    typename Debug::self_pointer_t debug_;
00816    typename Timer::self_pointer_t timer_;
00817    typename Routing::self_pointer_t routing_;
00818    typename Random::self_pointer_t random_;
00819 
00820    keyshare_t keyshare_;
00821    group_key group_key_;
00822    Crypto* crypto_;
00823    Clustering* clustering_;
00824 
00825    uint32_t _wait_for_routing;
00826 
00827    radio_ttl_t ttl_;
00828 
00829    bool have_neighbors;
00830    bool leader_offer_sent;
00831    bool nnTimerSet;
00832 };
00833 
00834 } /* namespace wiselib */
00835 
00836 #endif  /* _KEYLEVELS_MAIN_H  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines