GlobalNodeList.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include <iostream>
00025 
00026 #include <omnetpp.h>
00027 
00028 #include <NotificationBoard.h>
00029 #include <BinaryValue.h>
00030 #include <OverlayKey.h>
00031 #include <PeerInfo.h>
00032 #include <BaseOverlay.h>
00033 #include <GlobalStatisticsAccess.h>
00034 #include <hashWatch.h>
00035 #include <BootstrapList.h>
00036 
00037 #include "GlobalNodeList.h"
00038 
00039 Define_Module(GlobalNodeList);
00040 
00041 std::ostream& operator<<(std::ostream& os, const bootstrapEntry entry)
00042 {
00043     NodeHandle* nodeHandle = dynamic_cast<NodeHandle*>(entry.node);
00044 
00045     os << "Address: " << entry.node->ip
00046        << " Port: " << entry.node->port;
00047 
00048     if (nodeHandle) {
00049         os << " NodeId: " << nodeHandle->key;
00050     }
00051 
00052     os << " ModuleID: "
00053        << entry.info->getModuleID() << " Bootstrapped: "
00054        << (entry.info->isBootstrapped() ? "true" : "false") << " TypeID: "
00055        << (entry.info->getTypeID());
00056 
00057     return os;
00058 }
00059 
00060 void GlobalNodeList::initialize()
00061 {
00062     maxNumberOfKeys = par("maxNumberOfKeys");
00063     keyProbability = par("keyProbability");
00064     WATCH_UNORDERED_MAP(peerSet);
00065     WATCH_VECTOR(keyList);
00066     WATCH(bootstrappedPeerSize);
00067     WATCH(bootstrappedMaliciousNodes);
00068     WATCH(maliciousNodes);
00069     createKeyList(maxNumberOfKeys);
00070     bootstrappedPeerSize = 0;
00071     for (int i = 0; i < MAX_NODETYPES; i++)
00072         bootstrappedPeerSizePerType[i] = 0;
00073     bootstrappedMaliciousNodes = 0;
00074     maliciousNodes = 0;
00075     preKilledNodes = 0;
00076 
00077     if (par("maliciousNodeChange")) {
00078         if ((double) par("maliciousNodeProbability") > 0)
00079             error("maliciousNodeProbability and maliciousNodeChange are not supported concurrently");
00080 
00081         cMessage* msg = new cMessage("maliciousNodeChange");
00082         scheduleAt(simTime() + (int) par("maliciousNodeChangeStartTime"), msg);
00083         maliciousNodesVector.setName("MaliciousNodeRate");
00084         maliciousNodesVector.record(0);
00085         maliciousNodeRatio = 0;
00086     }
00087 
00088     min_ip = 0xFFFFFFFF;
00089     max_ip = 0x00000000;
00090 
00091     for (int i=0; i<MAX_NODETYPES; i++) {
00092         for (int j=0; j<MAX_NODETYPES; j++) {
00093             connectionMatrix[i][j] = true;
00094         }
00095     }
00096 
00097     globalStatistics = GlobalStatisticsAccess().get();
00098 
00099     cMessage* timer = new cMessage("oracleTimer");
00100 
00101     scheduleAt(simTime(), timer);
00102 }
00103 
00104 void GlobalNodeList::handleMessage(cMessage* msg)
00105 {
00106     if (msg->isName("maliciousNodeChange")) {
00107         double newRatio = maliciousNodeRatio + (double) par("maliciousNodeChangeRate"); // ratio to obtain
00108         if (maliciousNodeRatio < (double) par("maliciousNodeChangeStartValue"))
00109             newRatio = (double) par("maliciousNodeChangeStartValue");
00110 
00111         if (newRatio < (double) par("maliciousNodeChangeStopValue")) // schedule next event
00112             scheduleAt(simTime() + (int) par("maliciousNodeChangeInterval"), msg);
00113 
00114         int nodesNeeded = (int) (((double) par("maliciousNodeChangeRate")) * peerSet.size());
00115 
00116         EV << "[GlobalNodeList::handleMessage()]\n"
00117            << "    Changing " << nodesNeeded << " nodes to be malicious"
00118            << endl;
00119 
00120         for (int i = 0; i < nodesNeeded; i++) {
00121             // search a node that is not yet malicious
00122             NodeHandle node;
00123             do {
00124                 node = getRandomNode(0, false);
00125             } while (isMalicious(node));
00126 
00127             setMalicious(node, true);
00128         }
00129 
00130         maliciousNodesVector.record(newRatio);
00131         maliciousNodeRatio = newRatio;
00132 
00133         return;
00134     }
00135 
00136     else if (msg->isName("oracleTimer")) {
00137         RECORD_STATS(globalStatistics->recordOutVector(
00138                      "GlobalNodeList: Number of nodes", peerSet.size()));
00139         scheduleAt(simTime() + 50, msg);
00140     } else {
00141         opp_error("GlobalNodeList::handleMessage: Unknown message type!");
00142     }
00143 }
00144 
00145 const NodeHandle& GlobalNodeList::getBootstrapNode(const NodeHandle &node)
00146 {
00147     uint32_t nodeType;
00148     PeerHashMap::iterator it;
00149 
00150     // always prefer boot node from the same partition
00151     // if there is no such node, go through all
00152     // connected partitions until a bootstrap node is found
00153     if (!node.isUnspecified()) {
00154         it = peerSet.find(node.getAddress());
00155 
00156         // this should never happen
00157         if (it == peerSet.end()) {
00158            return getRandomNode(0, true);
00159         }
00160 
00161         nodeType = it->second.info->getTypeID();
00162         const NodeHandle &tempNode1 = getRandomNode(nodeType, true);
00163 
00164         if (tempNode1.isUnspecified()) {
00165             for (uint32_t i = 1; i < MAX_NODETYPES; i++) {
00166                 if (i == nodeType)
00167                     continue;
00168 
00169                 if (connectionMatrix[nodeType][i]) {
00170                     const NodeHandle &tempNode2 = getRandomNode(i, true);
00171                     if (!tempNode2.isUnspecified())
00172                         return tempNode2;
00173                 }
00174             }
00175             return NodeHandle::UNSPECIFIED_NODE;
00176         } else {
00177             return tempNode1;
00178         }
00179     } else {
00180         return getRandomNode(0, true);
00181     }
00182 }
00183 
00184 const NodeHandle& GlobalNodeList::getRandomNode(uint32_t nodeType, bool bootstrappedNeeded)
00185 {
00186     if (peerSet.size() == 0)
00187         return NodeHandle::UNSPECIFIED_NODE;
00188     if (bootstrappedNeeded && bootstrappedPeerSize == 0)
00189         return NodeHandle::UNSPECIFIED_NODE;
00190     if (nodeType && bootstrappedPeerSizePerType[nodeType] == 0)
00191         return NodeHandle::UNSPECIFIED_NODE;
00192     else {
00193         // return random TransportAddress in O(log n)
00194         PeerHashMap::iterator it = peerSet.end();
00195         bootstrapEntry tempEntry = {NULL, NULL};
00196 
00197         while(it == peerSet.end() ||(nodeType && (it->second.info->getTypeID() != nodeType))
00198                 || (bootstrappedNeeded && !it->second.info->isBootstrapped())) {
00199             IPvXAddress randomAddr(intuniform(min_ip, max_ip));
00200 
00201             it = peerSet.find(randomAddr);
00202 
00203             if (it == peerSet.end()) {
00204                 it = peerSet.insert(std::make_pair(randomAddr,tempEntry)).first;
00205                 peerSet.erase(it++);
00206             }
00207 
00208             if (it == peerSet.end())
00209                 it = peerSet.begin();
00210         }
00211 
00212         if (dynamic_cast<NodeHandle*>(it->second.node)) {
00213             return *dynamic_cast<NodeHandle*>(it->second.node);
00214         } else {
00215             return NodeHandle::UNSPECIFIED_NODE;
00216         }
00217     }
00218 }
00219 
00220 void GlobalNodeList::sendNotificationToAllPeers(int category)
00221 {
00222     PeerHashMap::iterator it;
00223     for (it = peerSet.begin(); it != peerSet.end(); it++) {
00224         NotificationBoard* nb = check_and_cast<NotificationBoard*>(
00225                 simulation.getModule(it->second.info->getModuleID())
00226                 ->getSubmodule("notificationBoard"));
00227 
00228         nb->fireChangeNotification(category);
00229     }
00230 }
00231 
00232 void GlobalNodeList::addPeer(const IPvXAddress& ip, PeerInfo* info)
00233 {
00234     bootstrapEntry temp;
00235     temp.node = new TransportAddress(ip);
00236     temp.info = info;
00237     temp.info->setPreKilled(false);
00238 
00239     peerSet.insert(std::make_pair(temp.node->ip, temp));
00240     // set bounds for random node retrieval
00241     if (ip.get4().getInt() < min_ip) min_ip = ip.get4().getInt();
00242     if (ip.get4().getInt() > max_ip) max_ip = ip.get4().getInt();
00243 
00244     if (uniform(0, 1) < (double) par("maliciousNodeProbability") ||
00245             (par("maliciousNodeChange") && uniform(0, 1) < maliciousNodeRatio)) {
00246         setMalicious(*temp.node, true);
00247     }
00248 }
00249 
00250 void GlobalNodeList::registerPeer(const TransportAddress& peer)
00251 {
00252     PeerHashMap::iterator it = peerSet.find(peer.ip);
00253     if (it == peerSet.end())
00254         error("unable to bootstrap peer, peer is not in peer set");
00255     else {
00256         PeerInfo* info = it->second.info;
00257 
00258         if (!info->isBootstrapped()) {
00259             bootstrappedPeerSize++;
00260             bootstrappedPeerSizePerType[info->getTypeID()]++;
00261             info->setBootstrapped();
00262 
00263             if (info->isMalicious())
00264                 bootstrappedMaliciousNodes++;
00265         }
00266 
00267         delete it->second.node;
00268         peerSet.erase(it);
00269 
00270         bootstrapEntry temp;
00271         temp.node = new TransportAddress(peer);
00272         temp.info = info;
00273         peerSet.insert(std::make_pair(temp.node->ip, temp));
00274     }
00275 }
00276 
00277 void GlobalNodeList::registerPeer(const NodeHandle& peer)
00278 {
00279     PeerHashMap::iterator it = peerSet.find(peer.ip);
00280     if (it == peerSet.end())
00281         error("unable to bootstrap peer, peer is not in peer set");
00282     else {
00283         PeerInfo* info = it->second.info;
00284 
00285         if (!info->isBootstrapped()) {
00286             bootstrappedPeerSize++;
00287             bootstrappedPeerSizePerType[info->getTypeID()]++;
00288             info->setBootstrapped();
00289 
00290             if (info->isMalicious())
00291                 bootstrappedMaliciousNodes++;
00292         }
00293 
00294         delete it->second.node;
00295         peerSet.erase(it);
00296 
00297         bootstrapEntry temp;
00298         temp.node = new NodeHandle(peer);
00299         temp.info = info;
00300         peerSet.insert(std::make_pair(temp.node->ip, temp));
00301     }
00302 }
00303 
00304 void GlobalNodeList::removePeer(const TransportAddress& peer)
00305 {
00306     PeerHashMap::iterator it = peerSet.find(peer.ip);
00307     if(it != peerSet.end() && it->second.info->isBootstrapped()) {
00308         bootstrappedPeerSize--;
00309         bootstrappedPeerSizePerType[it->second.info->getTypeID()]--;
00310         it->second.info->setBootstrapped(false);
00311 
00312         if(it->second.info->isMalicious())
00313             bootstrappedMaliciousNodes--;
00314 
00315         it->second.info->setBootstrapped(false);
00316     }
00317 }
00318 
00319 void GlobalNodeList::killPeer(const IPvXAddress& ip)
00320 {
00321     PeerHashMap::iterator it = peerSet.find(ip);
00322     if(it != peerSet.end()) {
00323         if(it->second.info->isBootstrapped()) {
00324             bootstrappedPeerSize--;
00325             bootstrappedPeerSizePerType[it->second.info->getTypeID()]--;
00326 
00327             if(it->second.info->isMalicious())
00328                 bootstrappedMaliciousNodes--;
00329 
00330             it->second.info->setBootstrapped(false);
00331         }
00332 
00333         if (it->second.info->isPreKilled()) {
00334             it->second.info->setPreKilled(false);
00335             preKilledNodes--;
00336         }
00337 
00338         delete it->second.node;
00339         delete it->second.info;
00340         peerSet.erase(it);
00341     }
00342 }
00343 
00344 void GlobalNodeList::createKeyList(uint32_t size)
00345 {
00346     for (uint32_t i = 0; i < size; i++)
00347         keyList.push_back(OverlayKey::random());
00348 }
00349 
00350 GlobalNodeList::KeyList* GlobalNodeList::getKeyList(uint32_t maximumKeys)
00351 {
00352     if (maximumKeys > keyList.size()) {
00353         maximumKeys = keyList.size();
00354     }
00355     // copy keylist to temporary keylist
00356     KeyList tmpKeyList;
00357     tmpKeyList.clear();
00358 
00359     for (uint32_t i=0; i < keyList.size(); i++) {
00360         tmpKeyList.push_back(keyList[i]);
00361     }
00362 
00363     KeyList* returnList = new KeyList;
00364 
00365     for (uint32_t i=0; i < ((float)maximumKeys * keyProbability); i++) {
00366         uint32_t index = intuniform(0, tmpKeyList.size()-1);
00367 
00368         returnList->push_back(tmpKeyList[index]);
00369         tmpKeyList.erase(tmpKeyList.begin()+index);
00370     }
00371 
00372     return returnList;
00373 }
00374 
00375 const OverlayKey& GlobalNodeList::getRandomKeyListItem() const
00376 {
00377     return keyList[intuniform(0,keyList.size()-1)];
00378 }
00379 
00380 PeerInfo* GlobalNodeList::getPeerInfo(const TransportAddress& peer)
00381 {
00382     PeerHashMap::iterator it = peerSet.find(peer.ip);
00383 
00384     if (it == peerSet.end())
00385         return NULL;
00386     else
00387         return it->second.info;
00388 }
00389 
00390 PeerInfo* GlobalNodeList::getPeerInfo(const IPvXAddress& ip)
00391 {
00392     PeerHashMap::iterator it = peerSet.find(ip);
00393 
00394     if (it == peerSet.end())
00395         return NULL;
00396     else
00397         return it->second.info;
00398 }
00399 
00400 PeerInfo* GlobalNodeList::getRandomPeerInfo(uint32_t nodeType,
00401                                              bool bootstrappedNeeded) {
00402     // return random TransportAddress in O(log n)
00403     PeerHashMap::iterator it;
00404     bootstrapEntry tempEntry = {NULL, NULL};
00405 
00406     IPvXAddress randomAddr(intuniform(min_ip, max_ip));
00407 
00408     it = peerSet.find(randomAddr);
00409     if (it == peerSet.end()) {
00410         it = peerSet.insert(std::make_pair(randomAddr,tempEntry)).first;
00411         peerSet.erase(it++);
00412     }
00413 
00414     if (it == peerSet.end())
00415         it = peerSet.begin();
00416 
00417     // if nodeType != 0, search for next node with the given type
00418     if (nodeType) {
00419         while((nodeType && (it->second.info->getTypeID() != nodeType))
00420               || (bootstrappedNeeded && !it->second.info->isBootstrapped())) {
00421             ++it;
00422             if (it == peerSet.end()) it = peerSet.begin();
00423         }
00424     }
00425 
00426     return it->second.info;
00427 }
00428 
00429 void GlobalNodeList::setPreKilled(const TransportAddress& address)
00430 {
00431     PeerInfo* peer = getPeerInfo(address);
00432 
00433     if ((peer != NULL) && !(peer->isPreKilled())) {
00434         preKilledNodes++;
00435         peer->setPreKilled(true);
00436     }
00437 }
00438 
00439 TransportAddress* GlobalNodeList::getRandomAliveNode(uint32_t nodeType)
00440 {
00441     if (peerSet.size() <= preKilledNodes) {
00442         // all nodes are already marked for deletion;
00443         return NULL;
00444     } else {
00445         // return random address in O(log n)
00446         PeerHashMap::iterator it;
00447         bootstrapEntry tempEntry = {NULL, NULL};
00448 
00449         IPvXAddress randomAddr(intuniform(min_ip, max_ip));
00450 
00451         it = peerSet.find(randomAddr);
00452 
00453         if (it == peerSet.end()) {
00454             it = peerSet.insert(std::make_pair(randomAddr,tempEntry)).first;
00455             peerSet.erase(it++);
00456         }
00457 
00458         if (it == peerSet.end()) {
00459             it = peerSet.begin();
00460         }
00461 
00462         while ((nodeType && (it->second.info->getTypeID() != nodeType))
00463                || it->second.info->isPreKilled()) {
00464             it++;
00465             if (it == peerSet.end()) {
00466                 it = peerSet.begin();
00467             }
00468         }
00469 
00470         return it->second.node;
00471     }
00472 
00473     return NULL;
00474 }
00475 
00476 void GlobalNodeList::setMalicious(const TransportAddress& address, bool malicious)
00477 {
00478     PeerInfo* peer = getPeerInfo(address);
00479 
00480     if (peer != NULL) {
00481         if(malicious && !peer->isMalicious()) {
00482             maliciousNodes++;
00483             if (peer->isBootstrapped()) {
00484                 bootstrappedMaliciousNodes++;
00485             }
00486         }
00487 
00488         if (!malicious && peer->isMalicious()) {
00489             maliciousNodes--;
00490             if (peer->isBootstrapped()) {
00491                 bootstrappedMaliciousNodes--;
00492             }
00493         }
00494         peer->setMalicious(malicious);
00495     }
00496 }
00497 
00498 bool GlobalNodeList::isMalicious(const TransportAddress& address)
00499 {
00500     PeerInfo* peer = getPeerInfo(address);
00501 
00502     if(peer != NULL)
00503         return peer->isMalicious();
00504 
00505     return false;
00506 }
00507 
00508 void GlobalNodeList::setOverlayReadyIcon(const TransportAddress& address,
00509         bool ready)
00510 {
00511     if (ev.isGUI()) {
00512         const char* color;
00513 
00514         if (ready) {
00515             // change color if node is malicious
00516             color = isMalicious(address) ? "green" : "";
00517         } else {
00518             color = "red";
00519         }
00520 
00521         PeerInfo* info = getPeerInfo(address);
00522 
00523         if(info != NULL) {
00524             simulation.getModule(info->getModuleID())
00525             ->getDisplayString().setTagArg("i2", 1, color);
00526         }
00527     }
00528 }
00529 
00530 bool GlobalNodeList::areNodeTypesConnected(uint32_t a, uint32_t b)
00531 {
00532     if ((a > MAX_NODETYPES) || (b > MAX_NODETYPES)) {
00533         throw cRuntimeError("GlobalNodeList::areNodeTypesConnected(): nodeType "
00534               "bigger then MAX_NODETYPES");
00535     }
00536 
00537     return connectionMatrix[a][b];
00538 }
00539 
00540 void GlobalNodeList::connectNodeTypes(uint32_t a, uint32_t b)
00541 {
00542     if ((a > MAX_NODETYPES) || (b > MAX_NODETYPES)) {
00543         throw cRuntimeError("GlobalNodeList::connectNodeTypes(): nodeType "
00544               "bigger then MAX_NODETYPES");
00545     }
00546 
00547     connectionMatrix[a][b]=true;
00548 
00549     EV << "[GlobalNodeList::connectNodeTypes()]\n"
00550        << "    Connecting " << a << "->" << b
00551        << endl;
00552 
00553 }
00554 
00555 void GlobalNodeList::disconnectNodeTypes(uint32_t a, uint32_t b)
00556 {
00557     if ((a > MAX_NODETYPES) || (b > MAX_NODETYPES)) {
00558         throw cRuntimeError("GlobalNodeList::disconnectNodeTypes(): nodeType "
00559               "bigger then MAX_NODETYPES");
00560     }
00561 
00562     connectionMatrix[a][b]=false;
00563 
00564     EV << "[GlobalNodeList::disconnectNodeTypes()]\n"
00565        << "    Disconnecting " << a << "->" << b
00566        << endl;
00567 
00568 }
00569 
00570 void GlobalNodeList::mergeBootstrapNodes(int toPartition, int fromPartition,
00571                                           int numNodes)
00572 {
00573     BootstrapList* bootstrapList =
00574         check_and_cast<BootstrapList*>(simulation.getModule(
00575             getRandomPeerInfo(toPartition, false)->getModuleID())->
00576             getSubmodule("bootstrapList"));
00577 
00578     bootstrapList->insertBootstrapCandidate(getRandomNode(fromPartition, true),
00579                                        DNSSD);
00580 }
00581 
00582 GlobalNodeList::~GlobalNodeList()
00583 {
00584     PeerHashMap::iterator it;
00585     for(it = peerSet.begin(); it != peerSet.end(); ++it) {
00586         delete it->second.node;
00587         delete it->second.info;
00588     }
00589 }
00590 

Generated on Wed Mar 25 16:32:48 2009 for OverSim by  doxygen 1.5.5