#include <I3Identifier.h>
Public Member Functions | |
I3Identifier () | |
Constructor. | |
I3Identifier (unsigned char b) | |
Constructs an identifier filled with a byte. | |
I3Identifier (int prefixL, int keyL) | |
Constructor for variable prefix length, key length. | |
I3Identifier (const I3Identifier &id) | |
Copy constructor. | |
I3Identifier (std::string s) | |
Constructs an identifier from the hash of a string. | |
int | getKeyLength () const |
Returns the key length (total length) in bits. | |
int | getPrefixLength () const |
Returns the prefix length in bits. | |
void | clear () |
Sets all bits to 0. | |
int | compareTo (const I3Identifier &) const |
Comparation function. | |
bool | operator< (const I3Identifier &) const |
"Less than" comparation function | |
bool | operator> (const I3Identifier &) const |
"Greater than" comparation function | |
bool | operator== (const I3Identifier &) const |
"Equals" comparation function | |
I3Identifier & | operator= (const I3Identifier &) |
Copy operator. | |
bool | isClear () |
Checks if this identifier has been cleared. | |
bool | isMatch (const I3Identifier &id) const |
Checks if this identifier's first prefixLength bits equal those of id. | |
int | distanceTo (const I3Identifier &id) const |
Returns the "distance to" a identifier. | |
void | createFromHash (const std::string &s, const std::string &o="") |
Creates an identifier from the hash of a string. | |
void | createRandomKey () |
void | createRandomPrefix () |
void | createRandomSuffix () |
int | length () const |
OverlayKey | asOverlayKey () const |
Creates an OverlayKey from an identifier, to be used in the overlay underneath. | |
void | setName (std::string s) |
std::string | getName () |
~I3Identifier () | |
Destructor. | |
Protected Member Functions | |
void | initKey (int prefixL, int keyL) |
Inits the key with a given prefix length and key length. | |
Protected Attributes | |
unsigned char * | key |
Identifier bits. | |
unsigned short | prefixLength |
Size of prefix in bits. | |
unsigned short | keyLength |
Size of identifier in bits. | |
std::string | name |
Friends | |
std::ostream & | operator<< (std::ostream &os, const I3Identifier &id) |
String stream output operator. |
Definition at line 40 of file I3Identifier.h.
I3Identifier::I3Identifier | ( | ) |
Constructor.
Definition at line 36 of file I3Identifier.cc.
00037 { 00038 initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH); 00039 }
I3Identifier::I3Identifier | ( | unsigned char | b | ) |
Constructs an identifier filled with a byte.
b | Byte to fill with |
I3Identifier::I3Identifier | ( | int | prefixL, | |
int | keyL | |||
) |
Constructor for variable prefix length, key length.
prefixL | Prefix length | |
keyL | Key length |
Definition at line 41 of file I3Identifier.cc.
00042 { 00043 initKey(prefixL, keyL); 00044 }
I3Identifier::I3Identifier | ( | const I3Identifier & | id | ) |
Copy constructor.
id | Identifier to copy |
Definition at line 46 of file I3Identifier.cc.
00047 { 00048 initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH); 00049 *this = id; 00050 }
I3Identifier::I3Identifier | ( | std::string | s | ) |
Constructs an identifier from the hash of a string.
s | String to be hashed |
Definition at line 52 of file I3Identifier.cc.
00053 { 00054 initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH); 00055 createFromHash(s); 00056 }
I3Identifier::~I3Identifier | ( | ) |
Destructor.
Definition at line 230 of file I3Identifier.cc.
OverlayKey I3Identifier::asOverlayKey | ( | ) | const |
Creates an OverlayKey from an identifier, to be used in the overlay underneath.
No hashing is done, the first min(OverlayKey::keySize, keyLength) bits are copied directly.
Definition at line 124 of file I3Identifier.cc.
Referenced by I3::handleUDPMessage(), and I3::sendPacket().
00125 { 00126 return OverlayKey(key, prefixLength / 8); 00127 }
void I3Identifier::clear | ( | ) |
Sets all bits to 0.
Definition at line 58 of file I3Identifier.cc.
Referenced by I3Trigger::clear(), createFromHash(), and I3SubIdentifier::setIPAddress().
int I3Identifier::compareTo | ( | const I3Identifier & | id | ) | const |
Comparation function.
Definition at line 81 of file I3Identifier.cc.
Referenced by I3Trigger::compareTo(), I3SubIdentifier::compareTo(), operator<(), operator==(), and operator>().
void I3Identifier::createFromHash | ( | const std::string & | s, | |
const std::string & | o = "" | |||
) |
Creates an identifier from the hash of a string.
s | String to be hashed to form the prefix | |
o | String to be hashed to form the remaining bits |
Definition at line 129 of file I3Identifier.cc.
Referenced by I3HostMobility::discoverPartners(), I3SessionClient::handleTimerEvent(), I3LatencyStretch::handleTimerEvent(), I3Identifier(), I3SessionClient::initializeApp(), I3Triggers::initializeI3(), I3SessionClient::initializeI3(), I3SessionServer::initializeI3(), I3LatencyStretch::initializeI3(), and I3HostMobility::initializeI3().
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 }
void I3Identifier::createRandomKey | ( | ) |
Definition at line 162 of file I3Identifier.cc.
Referenced by I3Triggers::deliver(), I3LatencyStretch::initializeI3(), and I3BaseApp::retrieveClosestIdentifier().
00163 { 00164 createRandomPrefix(); 00165 createRandomSuffix(); 00166 }
void I3Identifier::createRandomPrefix | ( | ) |
Definition at line 168 of file I3Identifier.cc.
Referenced by createRandomKey().
00169 { 00170 for (int i = 0; i < prefixLength / 8; i++) { 00171 key[i] = intrand(256); 00172 } 00173 }
void I3Identifier::createRandomSuffix | ( | ) |
Definition at line 175 of file I3Identifier.cc.
Referenced by createRandomKey(), I3HostMobility::discoverPartners(), I3SessionClient::handleTimerEvent(), I3LatencyStretch::handleTimerEvent(), I3SessionClient::initializeI3(), I3LatencyStretch::initializeI3(), I3HostMobility::initializeI3(), and I3Anycast::initializeI3().
00176 { 00177 for (int i = prefixLength / 8; i < keyLength / 8; i++) { 00178 key[i] = intrand(256); 00179 } 00180 }
int I3Identifier::distanceTo | ( | const I3Identifier & | id | ) | const |
Returns the "distance to" a identifier.
This is used when many triggers match an identifier, to check which is the biggest prefix match. The distance is defined as the index of the byte (counting from the end) which is the first that is not equal, multiplied by 256, then added the XOR'ing of the differing byte. That way an earlier differing bits are considered "further" from later differing ones (notice that this distance is not a real metric!)
id | Identifier to be compared to |
Definition at line 113 of file I3Identifier.cc.
int I3Identifier::getKeyLength | ( | ) | const |
Returns the key length (total length) in bits.
Definition at line 76 of file I3Identifier.cc.
00077 { 00078 return keyLength; 00079 }
std::string I3Identifier::getName | ( | ) |
Definition at line 190 of file I3Identifier.cc.
00190 { 00191 return name; 00192 }
int I3Identifier::getPrefixLength | ( | ) | const |
Returns the prefix length in bits.
Definition at line 71 of file I3Identifier.cc.
00072 { 00073 return prefixLength; 00074 }
void I3Identifier::initKey | ( | int | prefixL, | |
int | keyL | |||
) | [protected] |
Inits the key with a given prefix length and key length.
prefixL | Prefix length | |
keyL | Key (identifier) length |
Definition at line 29 of file I3Identifier.cc.
Referenced by I3Identifier().
00030 { 00031 prefixLength = prefixL; 00032 keyLength = keyL; 00033 key = new unsigned char[keyLength / 8]; 00034 }
bool I3Identifier::isClear | ( | ) |
Checks if this identifier has been cleared.
Definition at line 63 of file I3Identifier.cc.
bool I3Identifier::isMatch | ( | const I3Identifier & | id | ) | const |
Checks if this identifier's first prefixLength bits equal those of id.
id | Identifier to be matched against |
Definition at line 108 of file I3Identifier.cc.
00109 { 00110 return memcmp(key, id.key, prefixLength / 8) == 0; 00111 }
int I3Identifier::length | ( | ) | const |
Definition at line 182 of file I3Identifier.cc.
Referenced by I3Trigger::length(), and I3SubIdentifier::length().
00182 { 00183 return 16 + keyLength; 00184 }
bool I3Identifier::operator< | ( | const I3Identifier & | id | ) | const |
"Less than" comparation function
Definition at line 86 of file I3Identifier.cc.
00087 { 00088 return compareTo(id) < 0; 00089 }
I3Identifier & I3Identifier::operator= | ( | const I3Identifier & | id | ) |
bool I3Identifier::operator== | ( | const I3Identifier & | id | ) | const |
"Equals" comparation function
Definition at line 96 of file I3Identifier.cc.
00097 { 00098 return compareTo(id) == 0; 00099 }
bool I3Identifier::operator> | ( | const I3Identifier & | id | ) | const |
"Greater than" comparation function
Definition at line 91 of file I3Identifier.cc.
00092 { 00093 return compareTo(id) > 0; 00094 }
void I3Identifier::setName | ( | std::string | s | ) |
Definition at line 186 of file I3Identifier.cc.
Referenced by I3LatencyStretch::initializeI3(), I3HostMobility::initializeI3(), and I3BaseApp::retrieveClosestIdentifier().
00186 { 00187 name = s; 00188 }
std::ostream& operator<< | ( | std::ostream & | os, | |
const I3Identifier & | id | |||
) | [friend] |
String stream output operator.
os | String stream | |
id | I3Identifier to be output |
Definition at line 195 of file I3Identifier.cc.
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 }
unsigned char* I3Identifier::key [protected] |
Identifier bits.
Definition at line 142 of file I3Identifier.h.
Referenced by asOverlayKey(), clear(), compareTo(), createFromHash(), createRandomPrefix(), createRandomSuffix(), distanceTo(), initKey(), isClear(), isMatch(), operator=(), and ~I3Identifier().
unsigned short I3Identifier::keyLength [protected] |
Size of identifier in bits.
Definition at line 148 of file I3Identifier.h.
Referenced by clear(), compareTo(), createFromHash(), createRandomSuffix(), distanceTo(), getKeyLength(), initKey(), isClear(), length(), and operator=().
std::string I3Identifier::name [protected] |
Definition at line 150 of file I3Identifier.h.
Referenced by createFromHash(), getName(), operator=(), and setName().
unsigned short I3Identifier::prefixLength [protected] |
Size of prefix in bits.
Definition at line 145 of file I3Identifier.h.
Referenced by asOverlayKey(), createFromHash(), createRandomPrefix(), createRandomSuffix(), getPrefixLength(), initKey(), and isMatch().