OverSim
I3Identifier Class Reference

#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
I3Identifieroperator= (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.

Detailed Description

Definition at line 40 of file I3Identifier.h.

Constructor & Destructor Documentation

I3Identifier::I3Identifier ( )

Constructor.

Definition at line 36 of file I3Identifier.cc.

I3Identifier::I3Identifier ( unsigned char  b)

Constructs an identifier filled with a byte.

Parameters
bByte to fill with
I3Identifier::I3Identifier ( int  prefixL,
int  keyL 
)

Constructor for variable prefix length, key length.

Parameters
prefixLPrefix length
keyLKey length

Definition at line 41 of file I3Identifier.cc.

{
initKey(prefixL, keyL);
}
I3Identifier::I3Identifier ( const I3Identifier id)

Copy constructor.

Parameters
idIdentifier to copy

Definition at line 46 of file I3Identifier.cc.

I3Identifier::I3Identifier ( std::string  s)

Constructs an identifier from the hash of a string.

Parameters
sString to be hashed

Definition at line 52 of file I3Identifier.cc.

I3Identifier::~I3Identifier ( )

Destructor.

Definition at line 230 of file I3Identifier.cc.

{
if (key == 0) {
cout << "Warning: key already deleted." << endl;
}
delete[] key;
key = 0;
}

Member Function Documentation

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.

Returns
An overlay key

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(), and I3SubIdentifier::setIPAddress().

{
memset(key, 0, keyLength / 8);
}
int I3Identifier::compareTo ( const I3Identifier id) const

Comparation function.

Definition at line 81 of file I3Identifier.cc.

Referenced by I3Trigger::compareTo(), and I3SubIdentifier::compareTo().

{
return memcmp(key, id.key, keyLength / 8);
}
void I3Identifier::createFromHash ( const std::string &  s,
const std::string &  o = "" 
)

Creates an identifier from the hash of a string.

Parameters
sString to be hashed to form the prefix
oString to be hashed to form the remaining bits

Definition at line 129 of file I3Identifier.cc.

Referenced by I3HostMobility::discoverPartners(), I3LatencyStretch::handleTimerEvent(), and I3SessionClient::handleTimerEvent().

{
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 ( )
void I3Identifier::createRandomPrefix ( )

Definition at line 168 of file I3Identifier.cc.

{
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 I3HostMobility::discoverPartners(), I3LatencyStretch::handleTimerEvent(), I3SessionClient::handleTimerEvent(), 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!)

Parameters
idIdentifier to be compared to

Definition at line 113 of file I3Identifier.cc.

Referenced by I3::findClosestMatch().

{
int index;
for (index = 0; index < keyLength; index++) {
if (key[index] != id.key[index]) break;
}
return (keyLength - index) * 256 + (key[index] ^ id.key[index]);
}
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.

Parameters
prefixLPrefix length
keyLKey (identifier) length

Definition at line 29 of file I3Identifier.cc.

{
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.

{
for (int i = 0; i < keyLength / 8; i++) {
if (key[i] != 0) return true;
}
return false;
}
bool I3Identifier::isMatch ( const I3Identifier id) const

Checks if this identifier's first prefixLength bits equal those of id.

Parameters
idIdentifier 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 I3SubIdentifier::length(), and I3Trigger::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)

Copy operator.

Definition at line 101 of file I3Identifier.cc.

{
memcpy(key, id.key, keyLength / 8);
name = id.name;
return *this;
}
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 I3BaseApp::retrieveClosestIdentifier().

{
name = s;
}

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const I3Identifier id 
)
friend

String stream output operator.

Parameters
osString stream
idI3Identifier to be output
Returns
os parameter

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;
}

Member Data Documentation

unsigned char* I3Identifier::key
protected

Identifier bits.

Definition at line 142 of file I3Identifier.h.

unsigned short I3Identifier::keyLength
protected

Size of identifier in bits.

Definition at line 148 of file I3Identifier.h.

std::string I3Identifier::name
protected

Definition at line 150 of file I3Identifier.h.

unsigned short I3Identifier::prefixLength
protected

Size of prefix in bits.

Definition at line 145 of file I3Identifier.h.


The documentation for this class was generated from the following files: