I3Identifier.cc

Go to the documentation of this file.
00001 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016 //
00017 
00024 #include "I3Identifier.h"
00025 #include "SHA1.h"
00026 
00027 using namespace std;
00028 
00029 void I3Identifier::initKey(int prefixL, int keyL)
00030 {
00031     prefixLength = prefixL;
00032     keyLength = keyL;
00033     key = new unsigned char[keyLength / 8];
00034 }
00035 
00036 I3Identifier::I3Identifier()
00037 {
00038     initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH);
00039 }
00040 
00041 I3Identifier::I3Identifier(int prefixL, int keyL)
00042 {
00043     initKey(prefixL, keyL);
00044 }
00045 
00046 I3Identifier::I3Identifier(const I3Identifier &id)
00047 {
00048     initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH);
00049     *this = id;
00050 }
00051 
00052 I3Identifier::I3Identifier(std::string s)
00053 {
00054     initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH);
00055     createFromHash(s);
00056 }
00057 
00058 void I3Identifier::clear()
00059 {
00060     memset(key, 0, keyLength / 8);
00061 }
00062 
00063 bool I3Identifier::isClear()
00064 {
00065     for (int i = 0; i < keyLength / 8; i++) {
00066         if (key[i] != 0) return true;
00067     }
00068     return false;
00069 }
00070 
00071 int I3Identifier::getPrefixLength() const
00072 {
00073     return prefixLength;
00074 }
00075 
00076 int I3Identifier::getKeyLength() const
00077 {
00078     return keyLength;
00079 }
00080 
00081 int I3Identifier::compareTo(const I3Identifier &id) const
00082 {
00083     return memcmp(key, id.key, keyLength / 8);
00084 }
00085 
00086 bool I3Identifier::operator <(const I3Identifier &id) const
00087 {
00088     return compareTo(id) < 0;
00089 }
00090 
00091 bool I3Identifier::operator >(const I3Identifier &id) const
00092 {
00093     return compareTo(id) > 0;
00094 }
00095 
00096 bool I3Identifier::operator ==(const I3Identifier &id) const
00097 {
00098     return compareTo(id) == 0;
00099 }
00100 
00101 I3Identifier &I3Identifier::operator =(const I3Identifier &id)
00102 {
00103     memcpy(key, id.key, keyLength / 8);
00104     name = id.name;
00105     return *this;
00106 }
00107 
00108 bool I3Identifier::isMatch(const I3Identifier &id) const
00109 {
00110     return memcmp(key, id.key, prefixLength / 8) == 0;
00111 }
00112 
00113 int I3Identifier::distanceTo(const I3Identifier &id) const
00114 {
00115     int index;
00116 
00117     for (index = 0; index < keyLength; index++) {
00118         if (key[index] != id.key[index]) break;
00119     }
00120 
00121     return (keyLength - index) * 256 + (key[index] ^ id.key[index]);
00122 }
00123 
00124 OverlayKey I3Identifier::asOverlayKey() const
00125 {
00126     return OverlayKey(key, prefixLength / 8);
00127 }
00128 
00129 void I3Identifier::createFromHash(const std::string &p, const std::string &o)
00130 {
00131     uint8_t temp[20];
00132     CSHA1 sha1;
00133     int size1, size2;
00134 
00135     sha1.Reset();
00136     sha1.Update((uint8_t*)p.c_str(), p.size());
00137     sha1.Final();
00138     sha1.GetHash(temp);
00139 
00140     clear();
00141     size1 = prefixLength / 8;
00142     if (size1 > 20) size1 = 20;
00143     memcpy(key, temp, size1);
00144 
00145     name = p + ":0";
00146 
00147     if (o.size() == 0) return;
00148 
00149     name = p + ":" + o;
00150 
00151     sha1.Reset();
00152     sha1.Update((uint8_t*)o.c_str(), o.size());
00153     sha1.Final();
00154     sha1.GetHash(temp);
00155 
00156     clear();
00157     size2 = (keyLength - prefixLength) / 8;
00158     if (size2 > 20) size2 = 20;
00159     memcpy(key + size1, temp, size2);
00160 }
00161 
00162 void I3Identifier::createRandomKey()
00163 {
00164     createRandomPrefix();
00165     createRandomSuffix();
00166 }
00167 
00168 void I3Identifier::createRandomPrefix()
00169 {
00170     for (int i = 0; i < prefixLength / 8; i++) {
00171         key[i] = intrand(256);
00172     }
00173 }
00174 
00175 void I3Identifier::createRandomSuffix()
00176 {
00177     for (int i = prefixLength / 8; i < keyLength / 8; i++) {
00178         key[i] = intrand(256);
00179     }
00180 }
00181 
00182 int I3Identifier::length() const {
00183     return 16 + keyLength;
00184 }
00185 
00186 void I3Identifier::setName(std::string s) {
00187     name = s;
00188 }
00189 
00190 std::string I3Identifier::getName() {
00191     return name;
00192 }
00193 
00194 
00195 std::ostream& operator<<(std::ostream& os, const I3Identifier& id)
00196 {
00197     bool allzeros;
00198     const char hex[] = "0123456789abcdef";
00199     string s0, s1;
00200 
00201     if (id.name.length() != 0) {
00202         os << "(" << id.name << ") ";
00203     }
00204 
00205     for (int i = 0; i < id.prefixLength / 8; i++) {
00206         os << hex[id.key[i] >> 4];
00207         os << hex[id.key[i] & 0xf];
00208     }
00209     os << ':';
00210 
00211     allzeros = true;
00212     for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) {
00213         if (id.key[i] != 0) {
00214             allzeros = false;
00215             break;
00216         }
00217     }
00218     if (allzeros) {
00219         os << "0...";
00220     } else {
00221         for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) {
00222             os << hex[id.key[i] >> 4];
00223             os << hex[id.key[i] & 0xf];
00224         }
00225     }
00226     return os;
00227 }
00228 
00229 
00230 I3Identifier::~I3Identifier()
00231 {
00232     if (key == 0) {
00233         cout << "Warning: key already deleted." << endl;
00234     }
00235     delete[] key;
00236     key = 0;
00237 }
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3