From: bdemsky Date: Sat, 20 Jan 2018 06:08:50 +0000 (-0800) Subject: edits X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=commitdiff_plain;h=9b3061d1723f6ae447f58e6cc83c9854fdd104be edits --- diff --git a/version2/src/C/CloudComm.cc b/version2/src/C/CloudComm.cc index 452317f..9a15732 100644 --- a/version2/src/C/CloudComm.cc +++ b/version2/src/C/CloudComm.cc @@ -53,11 +53,11 @@ CloudComm::CloudComm(Table *_table, IoTString *_baseurl, IoTString *_password, */ SecretKeySpec *CloudComm::initKey() { try { - PBEKeySpec keyspec = new PBEKeySpec(password->internalBytes(), - salt, - 65536, - 128); - SecretKey tmpkey = SecretKeyFactory_getInstance("PBKDF2WithHmacSHA256")->generateSecret(keyspec); + PBEKeySpec *keyspec = new PBEKeySpec(password->internalBytes(), + salt, + 65536, + 128); + SecretKey *tmpkey = SecretKeyFactory_getInstance("PBKDF2WithHmacSHA256")->generateSecret(keyspec); return new SecretKeySpec(tmpkey->getEncoded(), "AES"); } catch (Exception *e) { throw new Error("Failed generating key."); @@ -82,7 +82,6 @@ void CloudComm::initSecurity() { * Inits the HMAC generator. */ void CloudComm::initCrypt() { - if (password == NULL) { return; } @@ -123,19 +122,15 @@ void CloudComm::setSalt() { printf("%d\n", (int)saltTmp->get(i) & 255); } - URL *url = new URL(baseurl + "?req=setsalt"); - timer->startTime(); - URLConnection con = url->openConnection(); - HttpURLConnection http = (HttpURLConnection) con; + URLConnection *con = url->openConnection(); + HttpURLConnection *http = (HttpURLConnection *) con; http->setRequestMethod("POST"); http->setFixedLengthStreamingMode(saltTmp->length()); http->setDoOutput(true); http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS); - - http->connect(); OutputStream *os = http->getOutputStream(); @@ -143,18 +138,17 @@ void CloudComm::setSalt() { os->flush(); int responsecode = http->getResponseCode(); - if (responsecode != HttpURLConnection.HTTP_OK) { + if (responsecode != HttpURLConnection_HTTP_OK) { // TODO: Remove this print - System.out.println(responsecode); + printf("%d\n", responsecode); throw new Error("Invalid response"); } timer->endTime(); - salt = saltTmp; } catch (Exception *e) { timer->endTime(); - throw new ServerException("Failed setting salt", ServerException.TypeConnectTimeout); + throw new ServerException("Failed setting salt", ServerException_TypeConnectTimeout); } } @@ -169,36 +163,28 @@ bool CloudComm::getSalt() { throw new Error("getSlot failed"); } try { - timer->startTime(); con = url->openConnection(); - http = (HttpURLConnection) con; + http = (HttpURLConnection *) con; http->setRequestMethod("POST"); http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS); http->setReadTimeout(CloudComm_TIMEOUT_MILLIS); - - http->connect(); timer->endTime(); } catch (SocketTimeoutException *e) { timer->endTime(); - throw new ServerException("getSalt failed", ServerException.TypeConnectTimeout); + throw new ServerException("getSalt failed", ServerException_TypeConnectTimeout); } catch (Exception *e) { throw new Error("getSlot failed"); } try { - timer->startTime(); - - int responsecode = http.getResponseCode(); - if (responsecode != HttpURLConnection.HTTP_OK) { - // TODO: Remove this print - // System.out.println(responsecode); + int responsecode = http->getResponseCode(); + if (responsecode != HttpURLConnection_HTTP_OK) { throw new Error("Invalid response"); } - - InputStream is = http->getInputStream(); + InputStream *is = http->getInputStream(); if (is->available() > 0) { DataInputStream *dis = new DataInputStream(is); int salt_length = dis->readInt(); @@ -206,24 +192,21 @@ bool CloudComm::getSalt() { dis->readFully(tmp); salt = tmp; timer->endTime(); - return true; } else { timer->endTime(); - return false; } } catch (SocketTimeoutException *e) { timer->endTime(); - - throw new ServerException("getSalt failed", ServerException.TypeInputTimeout); + throw new ServerException("getSalt failed", ServerException_TypeInputTimeout); } catch (Exception *e) { throw new Error("getSlot failed"); } } Array *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber) { - ByteBuffer buffer = ByteBuffer.allocate(CloudComm_IV_SIZE); + ByteBuffer *buffer = ByteBuffer_allocate(CloudComm_IV_SIZE); buffer->putLong(machineId); int64_t localSequenceNumberShifted = localSequenceNumber << 16; buffer->putLong(localSequenceNumberShifted); @@ -232,24 +215,22 @@ Array *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber) Array *CloudComm::encryptSlotAndPrependIV(Array *rawData, Array *ivBytes) { try { - IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); - Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); - cipher->init(Cipher.ENCRYPT_MODE, key, ivSpec); + IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes); + Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding"); + cipher->init(Cipher_ENCRYPT_MODE, key, ivSpec); Array *encryptedBytes = cipher->doFinal(rawData); Array *chars = new Array(encryptedBytes->length() + CloudComm_IV_SIZE); - System_arraycopy(ivBytes, 0, chars, 0, ivBytes.length()); - System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes.length); + System_arraycopy(ivBytes, 0, chars, 0, ivBytes->length()); + System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes->length()); return chars; - } catch (Exception *e) { throw new Error("Failed To Encrypt"); } } - Array *CloudComm::stripIVAndDecryptSlot(Array *rawData) { try { Array *ivBytes = new Array(CloudComm_IV_SIZE); @@ -258,17 +239,14 @@ Array *CloudComm::stripIVAndDecryptSlot(Array *rawData) { System_arraycopy(rawData, CloudComm_IV_SIZE, encryptedBytes, 0, encryptedBytes->length); IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes); - Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding"); cipher->init(Cipher_DECRYPT_MODE, key, ivSpec); return cipher->doFinal(encryptedBytes); - } catch (Exception *e) { throw new Error("Failed To Decrypt"); } } - /* * API for putting a slot into the queue. Returns NULL on success. * On failure, the server will send slots with newer sequence @@ -278,53 +256,41 @@ Array *CloudComm::putSlot(Slot *slot, int max) { try { if (salt == NULL) { if (!getSalt()) { - throw new ServerException("putSlot failed", ServerException.TypeSalt); + throw new ServerException("putSlot failed", ServerException_TypeSalt); } initCrypt(); } int64_t sequencenumber = slot->getSequenceNumber(); Array *slotBytes = slot->encode(mac); - Array *chars = encryptSlotAndPrependIV(slotBytes, slot->getSlotCryptIV()); - URL *url = buildRequest(true, sequencenumber, max); - timer->startTime(); URLConnection *con = url->openConnection(); HttpURLConnection *http = (HttpURLConnection *) con; - http->setRequestMethod("POST"); http->setFixedLengthStreamingMode(chars->length); http->setDoOutput(true); http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS); http->setReadTimeout(CloudComm_TIMEOUT_MILLIS); http->connect(); - OutputStream *os = http->getOutputStream(); os->write(chars); os->flush(); - timer->endTime(); - - } catch (ServerException *e) { timer->endTime(); - throw e; } catch (SocketTimeoutException *e) { timer->endTime(); - - throw new ServerException("putSlot failed", ServerException.TypeConnectTimeout); + throw new ServerException("putSlot failed", ServerException_TypeConnectTimeout); } catch (Exception *e) { throw new Error("putSlot failed"); } - - try { timer->startTime(); - InputStream is = http->getInputStream(); + InputStream *is = http->getInputStream(); DataInputStream *dis = new DataInputStream(is); Array *resptype = new Array(7); dis->readFully(resptype); @@ -336,7 +302,6 @@ Array *CloudComm::putSlot(Slot *slot, int max) { return NULL; } else throw new Error("Bad response to putslot"); - } catch (SocketTimeoutException *e) { timer->endTime(); throw new ServerException("putSlot failed", ServerException->TypeInputTimeout); @@ -353,7 +318,7 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { try { if (salt == NULL) { if (!getSalt()) { - throw new ServerException("getSlots failed", ServerException.TypeSalt); + throw new ServerException("getSlots failed", ServerException_TypeSalt); } initCrypt(); } @@ -361,20 +326,15 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { URL *url = buildRequest(false, sequencenumber, 0); timer->startTime(); URLConnection *con = url->openConnection(); - HttpURLConnection *http = (HttpURLConnection) con; + HttpURLConnection *http = (HttpURLConnection *) con; http->setRequestMethod("POST"); http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS); http->setReadTimeout(CloudComm_TIMEOUT_MILLIS); - - - http->connect(); timer->endTime(); - } catch (SocketTimeoutException *e) { timer->endTime(); - - throw new ServerException("getSlots failed", ServerException.TypeConnectTimeout); + throw new ServerException("getSlots failed", ServerException_TypeConnectTimeout); } catch (ServerException *e) { timer->endTime(); @@ -384,23 +344,19 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { } try { - timer->startTime(); InputStream *is = http->getInputStream(); DataInputStream *dis = new DataInputStream(is); Array *resptype = new Array(7); - dis->readFully(resptype); timer->endTime(); - - if (!resptype->equals("getslot".getBytes())) + if (!resptype->equals("getslot"->getBytes())) throw new Error("Bad Response: " + new String(resptype)); return processSlots(dis); } catch (SocketTimeoutException *e) { timer->endTime(); - - throw new ServerException("getSlots failed", ServerException.TypeInputTimeout); + throw new ServerException("getSlots failed", ServerException_TypeInputTimeout); } catch (Exception *e) { throw new Error("getSlots failed"); } @@ -413,15 +369,13 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { Array *CloudComm::processSlots(DataInputStream *dis) { int numberofslots = dis->readInt(); Array *sizesofslots = new Array(numberofslots); - Array *slots = new Array(numberofslots); + for (int i = 0; i < numberofslots; i++) sizesofslots->set(i, dis->readInt()); - for (int i = 0; i < numberofslots; i++) { Array *rawData = new Array(sizesofslots->get(i)); dis->readFully(rawData); - Array *data = stripIVAndDecryptSlot(rawData); slots->set(i, Slot_decode(table, data, mac)); } @@ -430,17 +384,15 @@ Array *CloudComm::processSlots(DataInputStream *dis) { } Array *sendLocalData(Array *sendData, int64_t localSequenceNumber, String host, int port) { - if (salt == NULL) { + if (salt == NULL) return NULL; - } try { printf("Passing Locally\n"); - mac->update(sendData); Array *genmac = mac->doFinal(); Array *totalData = new Array(sendData->length() + genmac->length()); - System_arraycopy(sendData, 0, totalData, 0, sendData.length()); - System - arraycopy(genmac, 0, totalData, sendData.length, genmac->length()); + System_arraycopy(sendData, 0, totalData, 0, sendData->length()); + System_arraycopy(genmac, 0, totalData, sendData->length(), genmac->length()); // Encrypt the data for sending Array *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber()); @@ -461,20 +413,17 @@ Array *sendLocalData(Array *sendData, int64_t localSequenceNumber, S int lengthOfReturnData = input->readInt(); Array *returnData = new Array(lengthOfReturnData); input->readFully(returnData); - timer->endTime(); - returnData = stripIVAndDecryptSlot(returnData); // We are done with this socket socket->close(); - mac->update(returnData, 0, returnData->length - HMAC_SIZE); Array *realmac = mac->doFinal(); Array *recmac = new Array(HMAC_SIZE); System_arraycopy(returnData, returnData->length - realmac->length, recmac, 0, realmac->length); - if (!Arrays->equals(recmac, realmac)) + if (!recmac->equals(realmac)) throw new Error("Local Error: Invalid HMAC! Potential Attack!"); Array *returnData2 = new Array(lengthOfReturnData - recmac->length()); @@ -504,7 +453,6 @@ void CloudComm::localServerWorkerFunction() { try { // Accept incoming socket Socket *socket = inputSocket->accept(); - DataInputStream *input = new DataInputStream(socket->getInputStream()); DataOutputStream *output = new DataOutputStream(socket->getOutputStream()); @@ -512,12 +460,10 @@ void CloudComm::localServerWorkerFunction() { int dataSize = input->readInt(); Array *readData = new Array(dataSize); input->readFully(readData); - timer->endTime(); // Decrypt the data readData = stripIVAndDecryptSlot(readData); - mac->update(readData, 0, readData->length - HMAC_SIZE); Array *genmac = mac->doFinal(); Array *recmac = new Array(HMAC_SIZE); @@ -531,8 +477,6 @@ void CloudComm::localServerWorkerFunction() { // Process the data Array *sendData = table->acceptDataFromLocal(returnData); - - mac->update(sendData); Array *realmac = mac->doFinal(); Array *totalData = new Array(sendData->length() + realmac->length()); @@ -543,7 +487,6 @@ void CloudComm::localServerWorkerFunction() { Array *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber()); Array *encryptedData = encryptSlotAndPrependIV(totalData, iv); - timer->startTime(); // Send data to output (length of data, the data) output->writeInt(encryptedData->length()); @@ -576,4 +519,3 @@ void CloudComm::close() { } } } - diff --git a/version2/src/C/Commit.cc b/version2/src/C/Commit.cc index 3672e08..34d51dc 100644 --- a/version2/src/C/Commit.cc +++ b/version2/src/C/Commit.cc @@ -1,11 +1,14 @@ -#include "commit.h" +#include "Commit.h" +#include "CommitPart.h" +#include "ByteBuffer.h" +#include "KeyValue.h" Commit::Commit() : parts(new Hashtable()), missingParts(NULL), fldisComplete(false), hasLastPart(false), - keyValueUpdateSet(new HashSet()), + keyValueUpdateSet(new Hashset()), isDead(false), sequenceNumber(-1), machineId(-1), @@ -19,7 +22,7 @@ Commit::Commit(int64_t _sequenceNumber, int64_t _machineId, int64_t _transaction missingParts(NULL), fldisComplete(true), hasLastPart(false), - keyValueUpdateSet(new HashSet()), + keyValueUpdateSet(new Hashset()), isDead(false), sequenceNumber(_sequenceNumber), machineId(_machineId), @@ -27,8 +30,7 @@ Commit::Commit(int64_t _sequenceNumber, int64_t _machineId, int64_t _transaction liveKeys(new Hashset) { } -void Commit::addPartDecode(CommitPart newPart) { - +void Commit::addPartDecode(CommitPart *newPart) { if (isDead) { // If dead then just kill this part and move on newPart->setDead(); @@ -41,7 +43,7 @@ void Commit::addPartDecode(CommitPart newPart) { // Set dead the old one since the new one is a rescued version of this part previoslySeenPart->setDead(); } else if (newPart->isLastPart()) { - missingParts = new HashSet(); + missingParts = new Hashset(); hasLastPart = true; for (int i = 0; i < newPart->getPartNumber(); i++) { @@ -117,7 +119,7 @@ void Commit::setDead() { // Make all the parts of this transaction dead for (int32_t partNumber : parts->keySet()) { - CommitPart part = parts->get(partNumber); + CommitPart *part = parts->get(partNumber); part->setDead(); } } @@ -149,7 +151,7 @@ void Commit::createCommitParts() { // Copy to a smaller version Array *partData = new Array(copySize); - System->arraycopy(charData, currentPosition, partData, 0, copySize); + System_arraycopy(charData, currentPosition, partData, 0, copySize); CommitPart part = new CommitPart(NULL, machineId, sequenceNumber, transactionSequenceNumber, commitPartCount, partData, isLastPart); parts->put(part->getPartNumber(), part); @@ -176,7 +178,7 @@ void Commit::decodeCommitData() { // Stitch all the data sections together for (int i = 0; i < parts->keySet()->size(); i++) { CommitPart *tp = parts->get(i); - System->arraycopy(tp->getData(), 0, combinedData, currentPosition, tp->getDataSize()); + System_arraycopy(tp->getData(), 0, combinedData, currentPosition, tp->getDataSize()); currentPosition += tp->getDataSize(); } @@ -188,7 +190,7 @@ void Commit::decodeCommitData() { // Decode all the updates key values for (int i = 0; i < numberOfKVUpdates; i++) { - KeyValue *kv = (KeyValue *)KeyValue->decode(bbDecode); + KeyValue *kv = (KeyValue *)KeyValue_decode(bbDecode); keyValueUpdateSet->add(kv); liveKeys->add(kv->getKey()); } @@ -223,10 +225,8 @@ void Commit::setKVsMap(Hashtable *newKVs) { keyValueUpdateSet->addAll(newKVs->values()); liveKeys->addAll(newKVs->keySet()); - } - Commit *Commit_merge(Commit *newer, Commit *older, int64_t newSequenceNumber) { if (older == NULL) { diff --git a/version2/src/C/Commit.h b/version2/src/C/Commit.h index 752293c..4de3d22 100644 --- a/version2/src/C/Commit.h +++ b/version2/src/C/Commit.h @@ -4,8 +4,8 @@ class Commit { private: - Hashtable *parts; - Hashset *missingParts; + Hashtable *parts; + Hashset *missingParts; bool fldisComplete; bool hasLastPart; Hashset *keyValueUpdateSet; @@ -20,11 +20,10 @@ private: public: Commit(); Commit(int64_t _sequenceNumber, int64_t _machineId, int64_t _transactionSequenceNumber); - void addPartDecode(CommitPart *newPart); int64_t getSequenceNumber(); int64_t getTransactionSequenceNumber(); - Hashtable *getParts(); + Hashtable *getParts(); void addKV(KeyValue *kv); void invalidateKey(IoTString *key); Hashset *getKeyValueUpdateSet(); @@ -36,6 +35,7 @@ public: CommitPart *getPart(int32_t index); void createCommitParts(); void decodeCommitData(); + friend Commit *Commit_merge(Commit *newer, Commit *older, int64_t newSequenceNumber); }; Commit *Commit_merge(Commit *newer, Commit *older, int64_t newSequenceNumber); diff --git a/version2/src/C/hashset.h b/version2/src/C/hashset.h index a518de6..d37ac3b 100644 --- a/version2/src/C/hashset.h +++ b/version2/src/C/hashset.h @@ -106,7 +106,7 @@ public: return copy; } - void reset() { + void clear() { Linknode<_Key> *tmp = list; while (tmp != NULL) { Linknode<_Key> *tmpnext = tmp->next; @@ -114,7 +114,7 @@ public: tmp = tmpnext; } list = tail = NULL; - table->reset(); + table->clear(); } void resetAndDelete() { @@ -217,7 +217,7 @@ public: } unsigned int size() const { - return table->getSize(); + return table->size(); } bool isEmpty() const { diff --git a/version2/src/C/hashtable.h b/version2/src/C/hashtable.h index c642804..b610154 100644 --- a/version2/src/C/hashtable.h +++ b/version2/src/C/hashtable.h @@ -76,7 +76,7 @@ public: capacitymask = initialcapacity - 1; threshold = (unsigned int)(initialcapacity * loadfactor); - size = 0; // Initial number of elements in the hash + Size = 0; // Initial number of elements in the hash } /** @brief Hash table destructor */ @@ -107,13 +107,13 @@ public: } /** @brief Reset the table to its initial state. */ - void reset() { + void clear() { memset(table, 0, capacity * sizeof(struct Hashlistnode<_Key, _Val>)); if (zero) { ourfree(zero); zero = NULL; } - size = 0; + Size = 0; } /** Doesn't work with zero value */ @@ -142,7 +142,7 @@ public: ourfree(zero); zero = NULL; } - size = 0; + Size = 0; } void resetAndDeleteVals() { @@ -162,7 +162,7 @@ public: ourfree(zero); zero = NULL; } - size = 0; + Size = 0; } void resetAndFreeVals() { @@ -182,7 +182,7 @@ public: ourfree(zero); zero = NULL; } - size = 0; + Size = 0; } /** @@ -196,7 +196,7 @@ public: _Val oldval; if (!zero) { zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>)); - size++; + Size++; oldval = (_Val) 0; } else oldval = zero->val; @@ -205,7 +205,7 @@ public: return oldval; } - if (size > threshold) + if (Size > threshold) resize(capacity << 1); struct Hashlistnode<_Key, _Val> *search; @@ -231,7 +231,7 @@ public: search->key = key; search->val = val; search->hashcode = hashcode; - size++; + Size++; return (_Val) 0; } @@ -287,7 +287,7 @@ public: _Val v = zero->val; ourfree(zero); zero = NULL; - size--; + Size--; return v; } } @@ -308,7 +308,7 @@ public: //empty out this bin search->val = (_Val) 1; search->key = 0; - size--; + Size--; return v; } index++; @@ -316,8 +316,8 @@ public: return (_Val)0; } - unsigned int getSize() const { - return size; + unsigned int size() const { + return Size; } @@ -400,7 +400,7 @@ public: struct Hashlistnode<_Key, _Val> *table; struct Hashlistnode<_Key, _Val> *zero; unsigned int capacity; - unsigned int size; + unsigned int Size; private: unsigned int capacitymask; unsigned int threshold;