Wiselib
wiselib.testing/algorithms/gke/keylevels_share.h
Go to the documentation of this file.
00001 #ifndef _KEYLEVELS_SHARE_H
00002 #define _KEYLEVELS_SHARE_H
00003 
00004 #define COMMON_SEED 0
00005 #ifndef SHAWN
00006   #define KEYSHARE_SIZE 16
00007   #define KEYPOOL_SIZE 250
00008 #else
00009   #warning tutaj czary zeby sie z pliku czytalo
00010   #define KEYSHARE_SIZE _getFileKeyshareSize()
00011   #define KEYPOOL_SIZE _getFileKeypoolSize()
00012 #endif
00013 
00014 #define KEY_LEVELS 3
00015 #define SMALL_KEY_SIZE 16
00016 #define KEY_INFO_SIZE 3 //key index, key level, key value
00017 
00018 
00019 #include "util/pstl/map_static_vector.h"
00020 #include "keylevels_main.h"
00021 
00022 #ifdef SHAWN
00023 #include <string>
00024 #include "sys/simulation/simulation_controller.h"
00025 #include "sys/processor.h"
00026 #include "external_interface/shawn/shawn_types.h"
00027 #include "sys/taggings/basic_tags.h"
00028 #include "sys/taggings/group_tag.h"
00029 using namespace shawn;
00030 
00031 #endif
00032 
00033 namespace wiselib {
00034 
00035 typedef wiselib::OSMODEL Os;
00036 typedef Os::Rand Random;
00037 
00038 typedef struct p {
00039    uint16_t key_index;
00040    uint16_t key_level;
00041    uint8_t key_value[SMALL_KEY_SIZE];
00042 } key;
00043 
00044 typedef struct ki {
00045    uint16_t key_index;
00046    uint16_t key_level;
00047 } key_info;
00048 
00049 typedef struct sk {
00050    uint8_t key_data[GROUP_KEY_SIZE];
00051    bool have_key;
00052    Random *random_;
00053 
00054    void leader_init(Random* random) {
00055       //random->srand(0);
00056       init(random);
00057       have_key = true;
00058       for (int i = 0; i < GROUP_KEY_SIZE; i++) {
00059          key_data[i] = (uint8_t) (*random)() % 256;
00060          //key_data[i] = (uint8_t) 170;
00061       }
00062    }
00063 
00064    void init(Random* random) {
00065       random_ = random;
00066       have_key = false;
00067    }
00068 
00069    void set_key(uint8_t* key_address) {
00070       memcpy(&key_data, key_address, GROUP_KEY_SIZE);
00071       have_key = true;
00072    }
00073 } group_key;
00074 
00075 template<typename OsModel_P, typename Radio_P,
00076 typename Debug_P = typename OsModel_P::Debug>
00077 class KeyShare {
00078 
00079 public:
00080    typedef OsModel_P OsModel;
00081    typedef Radio_P Radio;
00082    typedef Debug_P Debug;
00083 
00084    //typedef typename wiselib::set_static<OsModel, key, KEYSHARE_SIZE> keyshare_t;
00085    typedef typename Radio::node_id_t node_id_t;
00086    typedef typename wiselib::MapStaticVector<OsModel, node_id_t, key, NODES_MAX> trusted_links_t;
00087 
00088    enum ReturnValues {
00089       SUCCESS = OsModel::SUCCESS,
00090       ERR_UNSPEC = OsModel::ERR_UNSPEC
00091    };
00092 
00093    KeyShare() 
00094    { 
00095 #ifdef SHAWN
00096    // PTB czytanie danych z pliku
00097    if(!(keyshare = (key*) malloc(KEYSHARE_SIZE * sizeof(key))))
00098    {
00099       fprintf(0, "No memory for keyshare!\n");
00100       exit(-1);
00101    }
00102 #endif
00103    }
00104 
00105 
00106 
00107    void variation_on_SDBMHash(uint8_t* data, unsigned int len) {
00108       uint8_t hash = 0;
00109       int i = 0;
00110 
00111 #ifdef HASH_DEBUG
00112       debug_->debug("[KLS] {%d} HASH: in: ",radio_->id());
00113       print_key_value(data,len);
00114       debug_->debug("\n");
00115 #endif
00116       for(unsigned int j=0;j<len;j++) {
00117          i=j;
00118          hash = data[i] + (hash << 6) + (hash << 16) - hash;
00119          (++i)%=len;
00120          hash = data[i] + (hash << 6) + (hash << 16) - hash;
00121          data[j] = hash;
00122       }
00123 #ifdef KEYLEVELS_SHARE_DEBUG
00124       debug_->debug("[KLS] {%d} HASH: out: ",radio_->id());
00125       print_key_value(data,len);
00126       debug_->debug("\n");
00127 #endif
00128    }
00129 
00130    int init(Radio& radio, Debug& debug, Random& random) {
00131       radio_ = &radio;
00132       debug_ = &debug;
00133       random_ = &random;
00134       //#ifdef SHAWN
00135       // readKeysFromXML();
00136       //#else
00137       fillKeyshareWithFakeKeys();
00138       fillKeyshareWithKeys(random_);
00139 //    fillKeyshareWithTestKeys(random_);
00140       //#endif
00141       return SUCCESS;
00142    }
00143 
00144    void fillKeyshareWithKeys(Random* random) {
00145       fillKeyshareWithKeyIndexesAndLevels(random);
00146       fillKeyshareWithKeyValues(random);
00147 #ifdef KEYLEVELS_SHARE_DEBUG
00148       listKeyshare();
00149 #endif
00150    }
00151 
00152    void fillKeyshareWithKeyIndexesAndLevels(Random* random) {
00153       bool already;
00154       random->srand(radio_->id());
00155       uint16_t key_index, key_level;
00156       for (unsigned int i = 0; i < KEYSHARE_SIZE; i++) {
00157          already = true;
00158          while (already) {
00159             key_index = (*random)() % KEYPOOL_SIZE;
00160             already = false;
00161             if (owns_key(key_index)) {
00162                already = true;
00163             }
00164             //for(unsigned int j=0; j<i; j++){
00165             // if (keyshare[j].key_index == key_index){
00166             //    already = true;
00167             //    break;
00168             // }
00169             //}
00170          }
00171          key_level = (*random)() % KEY_LEVELS;
00172          keyshare[i].key_index = key_index;
00173          keyshare[i].key_level = key_level;
00174       }
00175    }
00176 
00177    void fillKeyshareWithKeyValues(Random* random) {
00178 
00179       random->srand(COMMON_SEED);
00180       uint8_t temp_key[SMALL_KEY_SIZE];
00181       int i=0; //key pool iterator
00182       int k=0; //key value iterator
00183       int l=0; //key level iterator
00184       for (i = 0; i < KEYPOOL_SIZE; i++) {
00185          for(k=0;k<SMALL_KEY_SIZE;k++) {
00186             temp_key[k] = (*random)();
00187          }
00188 #ifdef KEYLEVELS_SHARE_DEBUG
00189          debug_->debug("[KLS] {%d} pool key[%d] = ", radio_->id(), i);
00190          print_key_value(temp_key, SMALL_KEY_SIZE);
00191          debug_->debug("\n");
00192 #endif
00193          if (owns_key(i)) {
00194             for(k=0;k<SMALL_KEY_SIZE;k++) {
00195                get_key(i)->key_value[k]=temp_key[k];
00196             }
00197             for(l=0; l<get_key(i)->key_level; l++) {
00198 #ifdef HASH_DEBUG
00199                debug_->debug("[KLS] {%d} pool key[%d] level %d = ",radio_->id(),i,l);
00200                print_key_value(get_key(i)->key_value,SMALL_KEY_SIZE);
00201                debug_->debug("\n");
00202 #endif
00203                variation_on_SDBMHash(get_key(i)->key_value,SMALL_KEY_SIZE);
00204             }
00205 #ifdef HASH_DEBUG
00206             debug_->debug("[KLS] {%d} pool key[%d] level %d = ",radio_->id(),i,get_key(i)->key_level);
00207             print_key_value(get_key(i)->key_value,SMALL_KEY_SIZE);
00208             debug_->debug("\n");
00209 #endif
00210          }
00211       }
00212    }
00213 
00214    void fillKeyshareWithTestKeyIndexesAndLevels() {
00215 #ifndef SHAWN
00216       if(radio_->id() == 3261)
00217       {
00218          keyshare[0].key_index = 1;
00219          keyshare[0].key_level = 0;
00220          keyshare[1].key_index = 2;
00221          keyshare[1].key_level = 1;
00222       }else if(radio_->id() == 3234)
00223       {
00224          keyshare[0].key_index = 2;
00225          keyshare[0].key_level = 4;
00226          keyshare[1].key_index = 3;
00227          keyshare[1].key_level = 0;
00228       }else if(radio_->id() == 0x1c2c)
00229       {
00230          keyshare[0].key_index = 5;
00231          keyshare[0].key_level = 0;
00232          keyshare[1].key_index = 3;
00233          keyshare[1].key_level = 0;
00234       }
00235       return;
00236 #endif
00237 #ifdef SHAWN
00238       switch (radio_->id()) {
00239          case 0:
00240          keyshare[0].key_index = 1;
00241          keyshare[0].key_level = 0;
00242          keyshare[1].key_index = 2;
00243          keyshare[1].key_level = 7;
00244          break;
00245          case 1:
00246          keyshare[0].key_index = 2;
00247          keyshare[0].key_level = 4;
00248          keyshare[1].key_index = 3;
00249          keyshare[1].key_level = 0;
00250          break;
00251          case 2:
00252          keyshare[0].key_index = 5;
00253          keyshare[0].key_level = 0;
00254          keyshare[1].key_index = 3;
00255          keyshare[1].key_level = 0;
00256          break;
00257          case 3:
00258          keyshare[0].key_index = 5;
00259          keyshare[0].key_level = 0;
00260          keyshare[1].key_index = 6;
00261          keyshare[1].key_level = 0;
00262          break;
00263          case 4:
00264          keyshare[0].key_index = 2;
00265          keyshare[0].key_level = 0;
00266          keyshare[1].key_index = 8;
00267          keyshare[1].key_level = 0;
00268          break;
00269          case 5:
00270          keyshare[0].key_index = 2;
00271          keyshare[0].key_level = 0;
00272          keyshare[1].key_index = 9;
00273          keyshare[1].key_level = 0;
00274          break;
00275          case 6:
00276          keyshare[0].key_index = 6;
00277          keyshare[0].key_level = 0;
00278          keyshare[1].key_index = 10;
00279          keyshare[1].key_level = 0;
00280          break;
00281          case 7:
00282          keyshare[0].key_index = 4;
00283          keyshare[0].key_level = 0;
00284          keyshare[1].key_index = 3;
00285          keyshare[1].key_level = 0;
00286          break;
00287          case 8:
00288          keyshare[0].key_index = 4;
00289          keyshare[0].key_level = 0;
00290          keyshare[1].key_index = 5;
00291          keyshare[1].key_level = 0;
00292          break;
00293          case 9:
00294          keyshare[0].key_index = 1;
00295          keyshare[0].key_level = 0;
00296          keyshare[1].key_index = 11;
00297          keyshare[1].key_level = 0;
00298          break;
00299       }
00300 #endif
00301 
00302    }
00303 
00304    void fillKeyshareWithFakeKeys() {
00305 
00306       for (unsigned int i = 0; i < KEYSHARE_SIZE; i++) {
00307          uint16_t key_index, key_level;
00308          bool already = true;
00309 
00310          while (already) {
00311             key_index = random() % KEYPOOL_SIZE;
00312             already = false;
00313 
00314             for (unsigned int j = 0; j < i; j++) {
00315                if (keyshare[j].key_index == key_index) {
00316                   already = true;
00317                   break;
00318                }
00319             }
00320          }
00321 
00322          key_level = random() % KEY_LEVELS;
00323          keyshare[i].key_index = key_index;
00324          keyshare[i].key_level = key_level;
00325       }
00326    }
00327 
00328    void fillKeyshareWithTestKeys(Random* random)
00329    {
00330       fillKeyshareWithTestKeyIndexesAndLevels();
00331       fillKeyshareWithKeyValues(random);
00332    }
00333 
00334 #ifndef SHAWN
00335    int random() { return 1; }
00336 #endif
00337 
00338    unsigned int get_keyshare_size() { return KEYSHARE_SIZE; }
00339 
00340    void listKeyshare() 
00341    {
00342       for (unsigned int i = 0; i < KEYSHARE_SIZE; i++) 
00343       {
00344          debug_->debug("[KLS] Key: (%d,%d): ", keyshare[i].key_index,keyshare[i].key_level);
00345          print_key(keyshare[i].key_value,SMALL_KEY_SIZE);
00346          debug_->debug("\n");
00347       }
00348    }
00349 
00350    key* get_key(uint16_t key_index)
00351    {
00352       for (unsigned int i = 0; i < KEYSHARE_SIZE; i++) {
00353          if (keyshare[i].key_index == key_index) {
00354             return &keyshare[i];
00355          }
00356       }
00357       return NULL;
00358    }
00359 
00360    bool owns_key(uint16_t key_index)
00361    {
00362       return get_key(key_index) != NULL;
00363    }
00364 
00365    bool trusted_link_exists(node_id_t node)
00366    {
00367       return trusted_links_.find(node) != trusted_links_.end();
00368    }
00369 
00370    void put_trusted_link(node_id_t node, key link)
00371    {
00372 #ifdef KEYLEVELS_SHARE_DEBUG
00373       debug_->debug("[KLS] {%d} put_trusted_link: ",radio_->id());
00374       print_key_value(link.key_value,SMALL_KEY_SIZE);
00375       debug_->debug("\n");
00376 #endif
00377       trusted_links_[node] = link;
00378 
00379    }
00380 
00381    key* get_key_info(node_id_t node) 
00382    {
00383       if (trusted_link_exists(node)) {
00384          return &trusted_links_[node];
00385       }
00386       return NULL;
00387    }
00388 
00389    void print_key(uint8_t* key, int size) 
00390    {
00391       for (int i = 0; i < size; i++, key++) {
00392          debug_->debug("%d", *key);
00393       }
00394    }
00395 
00396    void print_key_value(uint8_t* value, uint8_t size) 
00397    {
00398          for (int i = 0; i < size; i++) {
00399             debug_->debug("%d:", value[i]);
00400          }
00401          debug_->debug("\n");
00402    }
00403 
00404    void print_all_key_info(key* k) 
00405    {
00406          debug_->debug("[KLS] {%d} KEY: (%d,%d) ", radio_->id(), k->key_index,
00407                k->key_level);
00408          print_key_value(k->key_value, SMALL_KEY_SIZE);
00409    }
00410 
00411 #ifdef SHAWN
00412    void readKeysFromXML()
00413    {
00414       ShawnOs& os_ = radio_->os();
00415       const shawn::GroupTag *gt = NULL;
00416       int _lvl, _id, keyRdy = 0;
00417       std::string _kstr;
00418 
00419       shawn::ConstTagHandle th = os_.proc->owner().find_tag("keyset");
00420       if(th != NULL)
00421       {
00422          gt = dynamic_cast<const shawn::GroupTag*>(th.get());
00423       } else { }
00424       if(gt != NULL)
00425       {
00426          shawn::TagContainer::tag_iterator ti = gt->begin_tags();
00427          unsigned int i = 0;
00428          while(ti != gt->end_tags())
00429          {
00430             shawn::TagHandle tth = ti->second;
00431             const shawn::GroupTag* ggt = dynamic_cast<const shawn::GroupTag*>(tth.get());
00432             if(ggt != NULL)
00433             {
00434                shawn::TagContainer::tag_iterator tti = ggt->begin_tags();
00435                while(tti != ggt->end_tags())
00436                {
00437                   TagHandle ttth = tti->second;
00438                   if(ttth->name() == "key")
00439                   {
00440                      _kstr = ttth->encoded_content().c_str();
00441                      keyRdy++;
00442                   }
00443                   else if(ttth->name() == "level")
00444                   {
00445                      _lvl = atoi(ttth->encoded_content().c_str());
00446                      keyRdy++;
00447                   }
00448                   else if(ttth->name() == "id")
00449                   {
00450                      _id = atoi(ttth->encoded_content().c_str());
00451                      keyRdy++;
00452                   }
00453                   else
00454                   {}
00455                   tti++;
00456                }
00457                if(keyRdy == 3)
00458                {
00459                   if(i < KEYSHARE_SIZE)
00460                   {
00461                      keyshare[i].key_index = _id;
00462                      keyshare[i].key_level = _lvl;
00463                      i++;
00464                   }
00465                }
00466                keyRdy = 0;
00467             }
00468             ti++;
00469          }
00470       }
00471    }
00472 #endif
00473 
00474 // PTB czytanie danych z pliku
00475 #ifndef SHAWN
00476    key keyshare[KEYSHARE_SIZE];
00477 #else
00478    key* keyshare;
00479 #endif
00480 
00481 private:
00482    typename Radio::self_pointer_t radio_;
00483    typename Debug::self_pointer_t debug_;
00484    typename Random::self_pointer_t random_;
00485 
00486 
00487    trusted_links_t trusted_links_;
00488 };
00489 
00490 } /* namespace wiselib */
00491 
00492 #endif  /* _KEYLEVELS_SHARE_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines