#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.
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.
{ initKey(prefixL, keyL); }
I3Identifier::I3Identifier | ( | const I3Identifier & | id | ) |
Copy constructor.
id | Identifier to copy |
Definition at line 46 of file I3Identifier.cc.
{ initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH); *this = id; }
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.
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().
{ return OverlayKey(key, prefixLength / 8); }
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().
{ uint8_t temp[20]; CSHA1 sha1; int size1, size2; sha1.Reset(); sha1.Update((uint8_t*)p.c_str(), p.size()); sha1.Final(); sha1.GetHash(temp); clear(); size1 = prefixLength / 8; if (size1 > 20) size1 = 20; memcpy(key, temp, size1); name = p + ":0"; if (o.size() == 0) return; name = p + ":" + o; sha1.Reset(); sha1.Update((uint8_t*)o.c_str(), o.size()); sha1.Final(); sha1.GetHash(temp); clear(); size2 = (keyLength - prefixLength) / 8; if (size2 > 20) size2 = 20; memcpy(key + size1, temp, size2); }
void I3Identifier::createRandomKey | ( | ) |
Definition at line 162 of file I3Identifier.cc.
Referenced by I3Triggers::deliver(), I3LatencyStretch::initializeI3(), and I3BaseApp::retrieveClosestIdentifier().
{ createRandomPrefix(); createRandomSuffix(); }
void I3Identifier::createRandomPrefix | ( | ) |
Definition at line 168 of file I3Identifier.cc.
Referenced by createRandomKey().
{ for (int i = 0; i < prefixLength / 8; i++) { key[i] = intrand(256); } }
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().
{ for (int i = prefixLength / 8; i < keyLength / 8; i++) { key[i] = intrand(256); } }
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.
{ return keyLength; }
std::string I3Identifier::getName | ( | ) |
Definition at line 190 of file I3Identifier.cc.
{ return name; }
int I3Identifier::getPrefixLength | ( | ) | const |
Returns the prefix length in bits.
Definition at line 71 of file I3Identifier.cc.
{ return prefixLength; }
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().
{ prefixLength = prefixL; keyLength = keyL; key = new unsigned char[keyLength / 8]; }
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.
{ return memcmp(key, id.key, prefixLength / 8) == 0; }
int I3Identifier::length | ( | ) | const |
Definition at line 182 of file I3Identifier.cc.
Referenced by I3Trigger::length(), and I3SubIdentifier::length().
{ return 16 + keyLength; }
bool I3Identifier::operator< | ( | const I3Identifier & | id | ) | const |
"Less than" comparation function
Definition at line 86 of file I3Identifier.cc.
{ return compareTo(id) < 0; }
I3Identifier & I3Identifier::operator= | ( | const I3Identifier & | id | ) |
bool I3Identifier::operator== | ( | const I3Identifier & | id | ) | const |
"Equals" comparation function
Definition at line 96 of file I3Identifier.cc.
{ return compareTo(id) == 0; }
bool I3Identifier::operator> | ( | const I3Identifier & | id | ) | const |
"Greater than" comparation function
Definition at line 91 of file I3Identifier.cc.
{ return compareTo(id) > 0; }
void I3Identifier::setName | ( | std::string | s | ) |
Definition at line 186 of file I3Identifier.cc.
Referenced by I3LatencyStretch::initializeI3(), I3HostMobility::initializeI3(), and I3BaseApp::retrieveClosestIdentifier().
{ name = s; }
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.
{ bool allzeros; const char hex[] = "0123456789abcdef"; string s0, s1; if (id.name.length() != 0) { os << "(" << id.name << ") "; } for (int i = 0; i < id.prefixLength / 8; i++) { os << hex[id.key[i] >> 4]; os << hex[id.key[i] & 0xf]; } os << ':'; allzeros = true; for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) { if (id.key[i] != 0) { allzeros = false; break; } } if (allzeros) { os << "0..."; } else { for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) { os << hex[id.key[i] >> 4]; os << hex[id.key[i] & 0xf]; } } return os; }
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().