#include "Slot.h"
-Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, char* _prevhmac, char* _hmac, int64_t _localSequenceNumber) {
- seqnum = _seqnum;
- machineid = _machineid;
- prevhmac = _prevhmac;
- hmac = _hmac;
- entries = new Vector<Entry*>();
- livecount = 1;
- seqnumlive = true;
- freespace = SLOT_SIZE - getBaseSize();
- table = _table;
- localSequenceNumber = _localSequenceNumber;
+Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, char *_prevhmac, char *_hmac, int64_t _localSequenceNumber) {
+ seqnum = _seqnum;
+ machineid = _machineid;
+ prevhmac = _prevhmac;
+ hmac = _hmac;
+ entries = new Vector<Entry *>();
+ livecount = 1;
+ seqnumlive = true;
+ freespace = SLOT_SIZE - getBaseSize();
+ table = _table;
+ localSequenceNumber = _localSequenceNumber;
}
-Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, char* _prevhmac, int64_t _localSequenceNumber) {
- this(_table, _seqnum, _machineid, _prevhmac, NULL, _localSequenceNumber);
+Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, char *_prevhmac, int64_t _localSequenceNumber) {
+ this(_table, _seqnum, _machineid, _prevhmac, NULL, _localSequenceNumber);
}
Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, int64_t _localSequenceNumber) {
- this(_table, _seqnum, _machineid, new char[HMAC_SIZE], NULL, _localSequenceNumber);
+ this(_table, _seqnum, _machineid, new char[HMAC_SIZE], NULL, _localSequenceNumber);
}
-Entry * Slot::addEntry(Entry * e) {
- e = e->getCopy(this);
- entries->add(e);
- livecount++;
- freespace -= e->getSize();
- return e;
+Entry *Slot::addEntry(Entry *e) {
+ e = e->getCopy(this);
+ entries->add(e);
+ livecount++;
+ freespace -= e->getSize();
+ return e;
}
void Slot::removeEntry(Entry *e) {
- entries->remove(e);
- livecount--;
- freespace += e->getSize();
+ entries->remove(e);
+ livecount--;
+ freespace += e->getSize();
}
void Slot::addShallowEntry(Entry *e) {
- entries->add(e);
- livecount++;
- freespace -= e->getSize();
+ entries->add(e);
+ livecount++;
+ freespace -= e->getSize();
}
/**
* using its reserved space. */
bool Slot::hasSpace(Entry *e) {
- int newfreespace = freespace - e->getSize();
- return newfreespace >= 0;
+ int newfreespace = freespace - e->getSize();
+ return newfreespace >= 0;
}
-Vector<Entry*> * Slot::getEntries() {
- return entries;
+Vector<Entry *> *Slot::getEntries() {
+ return entries;
}
-Slot * Slotdecode(Table * table, char* array, Mac * mac) {
- mac->update(array, HMAC_SIZE, array.length - HMAC_SIZE);
- char* realmac = mac->doFinal();
-
- ByteBuffer * bb = ByteBuffer_wrap(array);
- char* hmac = new char[HMAC_SIZE];
- char* prevhmac = new char[HMAC_SIZE];
- bb->get(hmac);
- bb->get(prevhmac);
- if (!Arrays.equals(realmac, hmac))
- throw new Error("Server Error: Invalid HMAC! Potential Attack!");
-
- int64_t seqnum = bb->getLong();
- int64_t machineid = bb->getLong();
- int numentries = bb->getInt();
- Slot slot = new Slot(table, seqnum, machineid, prevhmac, hmac, -1);
-
- for (int i = 0; i < numentries; i++) {
- slot->addShallowEntry(Entry->decode(slot, bb));
- }
-
- return slot;
+Slot *Slotdecode(Table *table, char *array, Mac *mac) {
+ mac->update(array, HMAC_SIZE, array.length - HMAC_SIZE);
+ char *realmac = mac->doFinal();
+
+ ByteBuffer *bb = ByteBuffer_wrap(array);
+ char *hmac = new char[HMAC_SIZE];
+ char *prevhmac = new char[HMAC_SIZE];
+ bb->get(hmac);
+ bb->get(prevhmac);
+ if (!Arrays.equals(realmac, hmac))
+ throw new Error("Server Error: Invalid HMAC! Potential Attack!");
+
+ int64_t seqnum = bb->getLong();
+ int64_t machineid = bb->getLong();
+ int numentries = bb->getInt();
+ Slot slot = new Slot(table, seqnum, machineid, prevhmac, hmac, -1);
+
+ for (int i = 0; i < numentries; i++) {
+ slot->addShallowEntry(Entry->decode(slot, bb));
+ }
+
+ return slot;
}
-char* Slot::encode(Mac mac) {
- char* array = new char[SLOT_SIZE];
- ByteBuffer * bb = ByteBuffer_wrap(array);
- /* Leave space for the slot HMAC. */
- bb->position(HMAC_SIZE);
- bb->put(prevhmac);
- bb->putLong(seqnum);
- bb->putLong(machineid);
- bb->putInt(entries->size());
- for (Entry entry : entries) {
- entry->encode(bb);
- }
- /* Compute our HMAC */
- mac->update(array, HMAC_SIZE, array.length - HMAC_SIZE);
- char* realmac = mac->doFinal();
- hmac = realmac;
- bb->position(0);
- bb->put(realmac);
- return array;
+char *Slot::encode(Mac mac) {
+ char *array = new char[SLOT_SIZE];
+ ByteBuffer *bb = ByteBuffer_wrap(array);
+ /* Leave space for the slot HMAC. */
+ bb->position(HMAC_SIZE);
+ bb->put(prevhmac);
+ bb->putLong(seqnum);
+ bb->putLong(machineid);
+ bb->putInt(entries->size());
+ for (Entry entry : entries) {
+ entry->encode(bb);
+ }
+ /* Compute our HMAC */
+ mac->update(array, HMAC_SIZE, array.length - HMAC_SIZE);
+ char *realmac = mac->doFinal();
+ hmac = realmac;
+ bb->position(0);
+ bb->put(realmac);
+ return array;
}
* itself.
*/
-Vector<Entry*> *Slot::getLiveEntries(bool resize) {
- Vector<Entry*> *liveEntries = new Vector<Entry*>();
- for (Entry *entry : entries) {
- if (entry->isLive()) {
- if (!resize || entry->getType() != Entry->TypeTableStatus)
- liveEntries->add(entry);
- }
- }
-
- if (seqnumlive && !resize)
- liveEntries->add(new LastMessage(this, machineid, seqnum));
-
- return liveEntries;
+Vector<Entry *> *Slot::getLiveEntries(bool resize) {
+ Vector<Entry *> *liveEntries = new Vector<Entry *>();
+ for (Entry *entry : entries) {
+ if (entry->isLive()) {
+ if (!resize || entry->getType() != Entry->TypeTableStatus)
+ liveEntries->add(entry);
+ }
+ }
+
+ if (seqnumlive && !resize)
+ liveEntries->add(new LastMessage(this, machineid, seqnum));
+
+ return liveEntries;
}
*/
void Slot::setDead() {
- seqnumlive = false;
- decrementLiveCount();
+ seqnumlive = false;
+ decrementLiveCount();
}
/**
*/
void Slot::decrementLiveCount() {
- livecount--;
- if (livecount == 0) {
- table->decrementLiveCount();
- }
+ livecount--;
+ if (livecount == 0) {
+ table->decrementLiveCount();
+ }
}
-char* Slot::getSlotCryptIV() {
- ByteBuffer * buffer = ByteBuffer_allocate(CloudComm.IV_SIZE);
- buffer->putLong(machineid);
- int64_t localSequenceNumberShift = localSequenceNumber << 16;
- buffer->putLong(localSequenceNumberShift);
- return buffer->array();
+char *Slot::getSlotCryptIV() {
+ ByteBuffer *buffer = ByteBuffer_allocate(CloudComm.IV_SIZE);
+ buffer->putLong(machineid);
+ int64_t localSequenceNumberShift = localSequenceNumber << 16;
+ buffer->putLong(localSequenceNumberShift);
+ return buffer->array();
}