checkpoint
authorbdemsky <bdemsky@uci.edu>
Thu, 29 Mar 2018 14:27:31 +0000 (07:27 -0700)
committerbdemsky <bdemsky@uci.edu>
Thu, 29 Mar 2018 14:27:31 +0000 (07:27 -0700)
12 files changed:
version2/src/C/CloudComm.cpp
version2/src/C/CloudComm.h
version2/src/C/KeyValue.cpp
version2/src/C/LocalComm.cpp
version2/src/C/SecureRandom.cpp
version2/src/C/Slot.cpp
version2/src/C/SlotBuffer.cpp
version2/src/C/SlotIndexer.cpp
version2/src/C/Table.cpp
version2/src/C/aes.cpp
version2/src/C/array.h
version2/src/C/common.h

index 8fe0ceeb256eddc802a076f3e6289798f0e5a90d..4f97029cf3eda2c1217fff3bbfa06b8b4073db78 100644 (file)
 #include "ByteBuffer.h"
 #include "aes.h"
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <netinet/tcp.h>
+//#include <sys/socket.h>
+//#include <arpa/inet.h>
+//#include <netinet/tcp.h>
 #include <unistd.h>
-#include <netdb.h>
+//#include <netdb.h>
 
 /**
  * Empty Constructor needed for child class.
@@ -58,9 +58,9 @@ CloudComm::CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password,
        timer(TimingSingleton_getInstance()),
        getslot(new Array<char>("getslot", 7)),
        putslot(new Array<char>("putslot", 7)) {
-       if (listeningPort > 0) {
+       /*      if (listeningPort > 0) {
                pthread_create(&localServerThread, NULL, threadWrapper, this);
-       }
+               }*/
 }
 
 CloudComm::~CloudComm() {
@@ -84,15 +84,11 @@ CloudComm::~CloudComm() {
  * Generates Key from password.
  */
 AESKey *CloudComm::initKey() {
-       try {
-               AESKey *key = new AESKey(password->internalBytes(),
-                                                                                                                salt,
-                                                                                                                65536,
-                                                                                                                128);
-               return key;
-       } catch (Exception *e) {
-               throw new Error("Failed generating key.");
-       }
+       AESKey *key = new AESKey(password->internalBytes(),
+                                                                                                        salt,
+                                                                                                        65536,
+                                                                                                        128);
+       return key;
 }
 
 /**
@@ -116,15 +112,11 @@ void CloudComm::initCrypt() {
        if (password == NULL) {
                return;
        }
-       try {
-               key = initKey();
-               delete password;
-               password = NULL;// drop password
-               mac = new Mac();
-               mac->init(key);
-       } catch (Exception *e) {
-               throw new Error("Failed To Initialize Ciphers");
-       }
+       key = initKey();
+       delete password;
+       password = NULL;// drop password
+       mac = new Mac();
+       mac->init(key);
 }
 
 /*
@@ -143,10 +135,10 @@ IoTString *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t m
        return urlstr;
 }
 
-void loopWrite(int fd, char *array, int bytestowrite) {
+void loopWrite(TCPClient client, char *array, int bytestowrite) {
        int byteswritten = 0;
        while (bytestowrite) {
-               int bytes = write(fd, &array[byteswritten], bytestowrite);
+               int bytes = client.write(&array[byteswritten], bytestowrite);
                if (bytes >= 0) {
                        byteswritten += bytes;
                        bytestowrite -= bytes;
@@ -157,10 +149,10 @@ void loopWrite(int fd, char *array, int bytestowrite) {
        }
 }
 
-void loopRead(int fd, char *array, int bytestoread) {
+void loopRead(TCPClient client, char *array, int bytestoread) {
        int bytesread = 0;
        while (bytestoread) {
-               int bytes = read(fd, &array[bytesread], bytestoread);
+               int bytes = client.read(&array[bytesread], bytestoread);
                if (bytes >= 0) {
                        bytesread += bytes;
                        bytestoread -= bytes;
@@ -203,38 +195,23 @@ WebConnection openURL(IoTString *url) {
        memcpy(&message[endpost + post + url->length() + hostlen - i], host, i - 7);
        sprintf(&message[endpost + post + url->length() + hostlen - 7], "\r\n");
 
-       /* create the socket */
-       int sockfd = socket(AF_INET, SOCK_STREAM, 0);
-       if (sockfd < 0) {printf("ERROR opening socket\n"); exit(-1);}
-
-       /* lookup the ip address */
-       struct hostent *server = gethostbyname(host);
-       free(host);
-
-       if (server == NULL) {printf("ERROR, no such host"); exit(-1);}
 
-       /* fill in the structure */
-       struct sockaddr_in serv_addr;
+       WebConnection wc;
+       wc.numBytes = -1;
 
-       memset(&serv_addr,0,sizeof(serv_addr));
-       serv_addr.sin_family = AF_INET;
-       serv_addr.sin_port = htons(80);
-       memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);
-
-       /* connect the socket */
-       if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
-               printf("ERROR connecting");
-               exit(-1);
+       if (!wc.client.connect(host, 80)) {
+               myerror("ERROR connecting\n");
        }
-
+       free(host);
+       
        /* send the request */
        int total = strlen(message);
        loopWrite(sockfd, message, total);
        free(message);
-       return (WebConnection) {sockfd, -1};
+       return wc;
 }
 
-int createSocket(IoTString *name, int port) {
+TCPClient createSocket(IoTString *name, int port) {
        char *host = (char *) malloc(name->length() + 1);
        memcpy(host, name->internalBytes()->internalArray(), name->length());
        host[name->length()] = 0;
@@ -246,66 +223,24 @@ int createSocket(IoTString *name, int port) {
        if (sockfd < 0) {printf("ERROR opening socket\n"); exit(-1);}
 
        /* lookup the ip address */
-       struct hostent *server = gethostbyname(host);
-       free(host);
-
-       if (server == NULL) {printf("ERROR, no such host"); exit(-1);}
-
-       /* fill in the structure */
-       struct sockaddr_in serv_addr;
-
-       memset(&serv_addr,0,sizeof(serv_addr));
-       serv_addr.sin_family = AF_INET;
-       serv_addr.sin_port = htons(port);
-       memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);
-
-       /* connect the socket */
-       if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
-               printf("ERROR connecting");
-               exit(-1);
+       TCPClient client;
+       if (!client.connect(host, port)) {
+               myerror("ERROR connecting\n");
        }
 
-       return sockfd;
+       free(host);
+       return client;
 }
 
-int createSocket(int port) {
+TCPServer createSocket(int port) {
        int fd;
        struct sockaddr_in sin;
+       TCPServer server = TCPServer(port);
+       server.begin();
 
-       bzero(&sin, sizeof(sin));
-       sin.sin_family = AF_INET;
-       sin.sin_port = htons(port);
-       sin.sin_addr.s_addr = htonl(INADDR_ANY);
-       fd = socket(AF_INET, SOCK_STREAM, 0);
-       int n = 1;
-       if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
-               close(fd);
-               printf("Create Socket Error\n");
-               exit(-1);
-       }
-       if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               close(fd);
-               exit(-1);
-       }
-       if (listen(fd, 5) < 0) {
-               close(fd);
-               exit(-1);
-       }
-       return fd;
+       return server;
 }
 
-int acceptSocket(int socket) {
-       struct sockaddr_in sin;
-       unsigned int sinlen = sizeof(sin);
-       int newfd = accept(socket, (struct sockaddr *)&sin, &sinlen);
-       int flag = 1;
-       setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
-       if (newfd < 0) {
-               printf("Accept Error\n");
-               exit(-1);
-       }
-       return newfd;
-}
 
 void writeSocketData(int fd, Array<char> *data) {
        loopWrite(fd, data->internalArray(), data->length());
@@ -404,7 +339,7 @@ void CloudComm::setSalt() {
        }
 
        WebConnection wc = {-1, -1};
-       try {
+       //      try {
                Array<char> *saltTmp = new Array<char>(CloudComm_SALT_SIZE);
                random->nextBytes(saltTmp);
 
@@ -422,40 +357,41 @@ void CloudComm::setSalt() {
 
                int responsecode = getResponseCode(&wc);
                if (responsecode != HttpURLConnection_HTTP_OK) {
-                       throw new Error("Invalid response");
+                       //throw new Error("Invalid response");
+                       myerror("Invalid response\n");
                }
                close(wc.fd);
 
                timer->endTime();
                salt = saltTmp;
-       } catch (Exception *e) {
+               /*      } catch (Exception *e) {
                timer->endTime();
                throw new ServerException("Failed setting salt", ServerException_TypeConnectTimeout);
-       }
+               }*/
 }
 
 bool CloudComm::getSalt() {
        WebConnection wc = {-1, -1};
        IoTString *urlstr = NULL;
 
-       try {
+       //      try {
                char *buffer = (char *) malloc(baseurl->length() + 100);
                memcpy(buffer, baseurl->internalBytes()->internalArray(), baseurl->length());
                int offset = baseurl->length();
                offset += sprintf(&buffer[offset], "?req=getsalt");
                urlstr = new IoTString(buffer);
                free(buffer);
-       } catch (Exception *e) {
+               /*      } catch (Exception *e) {
                throw new Error("getSlot failed");
-       }
-       try {
+               }*/
+               //      try {
                timer->startTime();
                wc = openURL(urlstr);
                delete urlstr;
                urlstr = NULL;
                closeURLReq(&wc);
                timer->endTime();
-       } catch (SocketTimeoutException *e) {
+               /*      } catch (SocketTimeoutException *e) {
                if (urlstr)
                        delete urlstr;
                timer->endTime();
@@ -464,14 +400,15 @@ bool CloudComm::getSalt() {
                if (urlstr)
                        delete urlstr;
                throw new Error("getSlot failed");
-       }
+               }*/
 
-       try {
+               //      try {
                timer->startTime();
                int responsecode = getResponseCode(&wc);
                readHeaders(&wc);
                if (responsecode != HttpURLConnection_HTTP_OK) {
-                       throw new Error("Invalid response");
+                       //throw new Error("Invalid response");
+                       myerror("Invalid response\n");
                }
                if (wc.numBytes == 0) {
                        timer->endTime();
@@ -488,12 +425,12 @@ bool CloudComm::getSalt() {
                salt = tmp;
                timer->endTime();
                return true;
-       } catch (SocketTimeoutException *e) {
+               /*      } catch (SocketTimeoutException *e) {
                timer->endTime();
                throw new ServerException("getSalt failed", ServerException_TypeInputTimeout);
        } catch (Exception *e) {
                throw new Error("getSlot failed");
-       }
+               }*/
 }
 
 Array<char> *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber) {
@@ -517,20 +454,20 @@ Array<char> *AESDecrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
 }
 
 Array<char> *CloudComm::encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes) {
-       try {
+       //      try {
                Array<char> *encryptedBytes = AESEncrypt(ivBytes, key, rawData);
                Array<char> *chars = new Array<char>(encryptedBytes->length() + CloudComm_IV_SIZE);
                System_arraycopy(ivBytes, 0, chars, 0, ivBytes->length());
                System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes->length());
                delete encryptedBytes;
                return chars;
-       } catch (Exception *e) {
+               /*      } catch (Exception *e) {
                throw new Error("Failed To Encrypt");
-       }
+               }*/
 }
 
 Array<char> *CloudComm::stripIVAndDecryptSlot(Array<char> *rawData) {
-       try {
+       //      try {
                Array<char> *ivBytes = new Array<char>(CloudComm_IV_SIZE);
                Array<char> *encryptedBytes = new Array<char>(rawData->length() - CloudComm_IV_SIZE);
                System_arraycopy(rawData, 0, ivBytes, 0, CloudComm_IV_SIZE);
@@ -539,9 +476,9 @@ Array<char> *CloudComm::stripIVAndDecryptSlot(Array<char> *rawData) {
                delete encryptedBytes;
                delete ivBytes;
                return data;
-       } catch (Exception *e) {
+               /*      } catch (Exception *e) {
                throw new Error("Failed To Decrypt");
-       }
+               }*/
 }
 
 /*
@@ -551,10 +488,11 @@ Array<char> *CloudComm::stripIVAndDecryptSlot(Array<char> *rawData) {
  */
 Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
        WebConnection wc = {-1, -1};
-       try {
+       //      try {
                if (salt == NULL) {
                        if (!getSalt()) {
-                               throw new ServerException("putSlot failed", ServerException_TypeSalt);
+                               //                              throw new ServerException("putSlot failed", ServerException_TypeSalt);
+                               myerror("putSlot failed\n");
                        }
                        initCrypt();
                }
@@ -572,7 +510,7 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
                writeURLDataAndClose(&wc, chars);
                delete chars;
                timer->endTime();
-       } catch (ServerException *e) {
+               /*      } catch (ServerException *e) {
                timer->endTime();
                throw e;
        } catch (SocketTimeoutException *e) {
@@ -580,10 +518,10 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
                throw new ServerException("putSlot failed", ServerException_TypeConnectTimeout);
        } catch (Exception *e) {
                throw new Error("putSlot failed");
-       }
+               }*/
 
        Array<char> *resptype = NULL;
-       try {
+       //      try {
                int respcode = getResponseCode(&wc);
                readHeaders(&wc);
                timer->startTime();
@@ -603,9 +541,10 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
                } else {
                        delete resptype;
                        close(wc.fd);
-                       throw new Error("Bad response to putslot");
+                       //throw new Error("Bad response to putslot");
+                       myerror("Bad response to putslot\n");
                }
-       } catch (SocketTimeoutException *e) {
+               /*      } catch (SocketTimeoutException *e) {
                if (resptype != NULL)
                        delete resptype;
                timer->endTime();
@@ -615,7 +554,7 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
                if (resptype != NULL)
                        delete resptype;
                throw new Error("putSlot failed");
-       }
+               }*/
 }
 
 /**
@@ -624,10 +563,11 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
  */
 Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
        WebConnection wc = {-1, -1};
-       try {
+       //      try {
                if (salt == NULL) {
                        if (!getSalt()) {
-                               throw new ServerException("getSlots failed", ServerException_TypeSalt);
+                               //throw new ServerException("getSlots failed", ServerException_TypeSalt);
+                               myerror("getSlots failed\n");
                        }
                        initCrypt();
                }
@@ -638,7 +578,7 @@ Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
                delete url;
                closeURLReq(&wc);
                timer->endTime();
-       } catch (SocketTimeoutException *e) {
+               /*      } catch (SocketTimeoutException *e) {
                timer->endTime();
                throw new ServerException("getSlots failed", ServerException_TypeConnectTimeout);
        } catch (ServerException *e) {
@@ -647,9 +587,9 @@ Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
                throw e;
        } catch (Exception *e) {
                throw new Error("getSlots failed");
-       }
+               }*/
 
-       try {
+               //      try {
                timer->startTime();
                int responsecode = getResponseCode(&wc);
                readHeaders(&wc);
@@ -657,19 +597,20 @@ Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
                readURLData(&wc, resptype);
                timer->endTime();
                if (!resptype->equals(getslot))
-                       throw new Error("Bad Response: ");
+                       //                      throw new Error("Bad Response: ");
+                       myerror("Bad Response: \n");
 
                delete resptype;
                Array<Slot *> *tmp = processSlots(&wc);
                close(wc.fd);
                return tmp;
-       } catch (SocketTimeoutException *e) {
+               /*      } catch (SocketTimeoutException *e) {
                timer->endTime();
                close(wc.fd);
                throw new ServerException("getSlots failed", ServerException_TypeInputTimeout);
        } catch (Exception *e) {
                throw new Error("getSlots failed");
-       }
+               }*/
 }
 
 /**
@@ -698,7 +639,7 @@ Array<Slot *> *CloudComm::processSlots(WebConnection *wc) {
 Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString *host, int port) {
        if (salt == NULL)
                return NULL;
-       try {
+       //      try {
                printf("Passing Locally\n");
                mac->update(sendData, 0, sendData->length());
                Array<char> *genmac = mac->doFinal();
@@ -732,40 +673,41 @@ Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequen
                System_arraycopy(returnData, returnData->length() - realmac->length(), recmac, 0, realmac->length());
 
                if (!recmac->equals(realmac))
-                       throw new Error("Local Error: Invalid HMAC!  Potential Attack!");
+                       //                      throw new Error("Local Error: Invalid HMAC!  Potential Attack!");
+                       myerror("Local Error: Invalid HMAC!  Potential Attack!\n");
 
                Array<char> *returnData2 = new Array<char>(lengthOfReturnData - recmac->length());
                System_arraycopy(returnData, 0, returnData2, 0, returnData2->length());
 
                return returnData2;
-       } catch (Exception *e) {
+               /*      } catch (Exception *e) {
                printf("Exception\n");
-       }
+               }*/
 
        return NULL;
 }
 
 void CloudComm::localServerWorkerFunction() {
-       int inputSocket = -1;
-
+       /*      int inputSocket = -1;
+       
        try {
                // Local server socket
                inputSocket = createSocket(listeningPort);
        } catch (Exception *e) {
                throw new Error("Local server setup failure...");
        }
-
+       
        while (!doEnd) {
                try {
                        // Accept incoming socket
                        int socket = acceptSocket(inputSocket);
-
+                       
                        // Get the encrypted data from the server
                        int dataSize = readSocketInt(socket);
                        Array<char> *readData = new Array<char>(dataSize);
                        readSocketData(socket, readData);
                        timer->endTime();
-
+                       
                        // Decrypt the data
                        readData = stripIVAndDecryptSlot(readData);
                        mac->update(readData, 0, readData->length() - CloudComm_HMAC_SIZE);
@@ -774,7 +716,8 @@ void CloudComm::localServerWorkerFunction() {
                        System_arraycopy(readData, readData->length() - recmac->length(), recmac, 0, recmac->length());
 
                        if (!recmac->equals(genmac))
-                               throw new Error("Local Error: Invalid HMAC!  Potential Attack!");
+                               //                              throw new Error("Local Error: Invalid HMAC!  Potential Attack!");
+                               error("Local Error: Invalid HMAC!  Potential Attack!\n");
 
                        Array<char> *returnData = new Array<char>(readData->length() - recmac->length());
                        System_arraycopy(readData, 0, returnData, 0, returnData->length());
@@ -799,21 +742,21 @@ void CloudComm::localServerWorkerFunction() {
                } catch (Exception *e) {
                }
        }
-
+       
        if (inputSocket != -1) {
                try {
                        close(inputSocket);
                } catch (Exception *e) {
                        throw new Error("Local server close failure...");
                }
-       }
+               }*/
 }
 
 void CloudComm::closeCloud() {
        doEnd = true;
 
-       if (listeningPort > 0) {
+       /*      if (listeningPort > 0) {
                if (pthread_join(localServerThread, NULL) != 0)
                        throw new Error("Local Server thread join issue...");
-       }
+                       }*/
 }
index ce7cfa07023a3832252f2633cd1973b4b35e1541..35d5fcce68ad200eba75fd8d6427ce73be13025a 100644 (file)
@@ -2,7 +2,9 @@
 #define CLOUDCOMM_H
 
 #include "common.h"
-#include <pthread.h>
+#include "application.h"
+
+//#include <pthread.h>
 /**
  * This class provides a communication API to the webserver.  It also
  * validates the HMACs on the slots and handles encryption.
@@ -19,7 +21,7 @@
 #define HttpURLConnection_HTTP_OK 200
 
 typedef struct {
-       int fd;
+       TCPClient client;
        int numBytes;
 } WebConnection;
 
@@ -34,7 +36,7 @@ private:
        Array<char> *salt;
        Table *table;
        int32_t listeningPort;
-       pthread_t localServerThread;
+       //      pthread_t localServerThread;
        bool doEnd;
        TimingSingleton *timer;
        Array<char> *getslot;
index 8aea6f2900550a2b33e0712ea60b06842ed82ab4..13ecc34701dba9dddbadc423a085c15ea06706cd 100644 (file)
@@ -40,7 +40,7 @@ void KeyValue::encode(ByteBuffer *bb) {
        }
 }
 
-int KeyValue::getSize() {
+int32_t KeyValue::getSize() {
        if (value != NULL)
                return 2 * sizeof(int32_t) + key->length() + value->length();
        return 2 * sizeof(int32_t) + key->length();
index ac4c3439d94f8600e4fccdce942dfd755ce5254d..a019bc2e2337508f7ecc517432d01daf44b6d7d3 100644 (file)
@@ -10,7 +10,8 @@ Array<char> *LocalComm::sendDataToLocalDevice(int64_t deviceId, Array<char> *dat
        } else if (deviceId == t2->getMachineId()) {
                // return t2.localCommInput(data);
        } else {
-               throw new Error("Cannot send to deviceId using this local comm");
+               //throw new Error("Cannot send to deviceId using this local comm");
+               error("Cannot send to deviceId using this local comm");
        }
 
        return new Array<char>((uint32_t)0);
index 84f40c3b197647e1393a94f7d629978e4a16d0c9..9336b338394d388b0c0c0f568364eb28a0703d05 100644 (file)
@@ -1,6 +1,6 @@
 #include "SecureRandom.h"
 #include <stdlib.h>
-#include <bsd/stdlib.h>
+//#include <bsd/stdlib.h>
 
 SecureRandom::SecureRandom() {
 }
index 31cce578dd3e7bac90befcb6cca073a32818c8bf..2233b0e310e1f2da80257a402ff777d6462d887f 100644 (file)
@@ -97,7 +97,8 @@ Slot *Slot_decode(Table *table, Array<char> *array, Mac *mac) {
        bb->get(hmac);
        bb->get(prevhmac);
        if (!realmac->equals(hmac))
-               throw new Error("Server Error: Invalid HMAC!  Potential Attack!");
+               //throw new Error("Server Error: Invalid HMAC!  Potential Attack!");
+               error("Server Error: Invalid HMAC!  Potential Attack!\n");
        delete realmac;
        
        int64_t seqnum = bb->getLong();
index 06c46e901d8f60a5970025476480c29da18d7e66..de2a0b201406c985934f6ce7eed9be86559bf732 100644 (file)
@@ -24,13 +24,13 @@ SlotBuffer::~SlotBuffer() {
        delete array;
 }
 
-int SlotBuffer::size() {
+int32_t SlotBuffer::size() {
        if (head >= tail)
                return head - tail;
        return (array->length() + head) - tail;
 }
 
-int SlotBuffer::capacity() {
+int32_t SlotBuffer::capacity() {
        return array->length() - 1;
 }
 
index b49ca454e749953f152d9ef85f0caf75933675b9..e46f83ec1056c34700d40f448a1e378072aa27ec 100644 (file)
@@ -19,7 +19,8 @@ Slot *SlotIndexer::getSlot(int64_t seqnum) {
        if (seqnum >= firstslotseqnum) {
                int32_t offset = (int32_t) (seqnum - firstslotseqnum);
                if (((uint32_t)offset) >= updates->length())
-                       throw new Error("Invalid Slot Sequence Number Reference");
+                       //throw new Error("Invalid Slot Sequence Number Reference");
+                       myerror("Invalid Slot Sequence Number Reference");
                else
                        return updates->get(offset);
        } else
index 255ba3cf35bb3da7a02df7e59a9366fe88b472a0..896af0a0179fd7b5f9a35f0f03f50091b48d414f 100644 (file)
@@ -263,7 +263,7 @@ Table::~Table() {
        }
        delete pendingSendArbitrationEntriesToDelete;
        {
-               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+               SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *) getKeyIterator(transactionPartsSent);
                while (trit->hasNext()) {
                        Transaction *transaction = trit->next();
                        delete trit->currVal();
@@ -359,7 +359,8 @@ void Table::initTable() {
        } else {
                delete s;
                delete array;
-               throw new Error("Error on initialization");
+               //throw new Error("Error on initialization");
+               myerror("Error on initialization\n");
        }
 }
 
@@ -420,13 +421,15 @@ IoTString *Table::getCommittedAtomic(IoTString *key) {
        KeyValue *kv = committedKeyValueTable->get(key);
 
        if (!arbitratorTable->contains(key)) {
-               throw new Error("Key not Found.");
+               //              throw new Error("Key not Found.");
+               myerror("Key not found!\n");
        }
 
        // Make sure new key value pair matches the current arbitrator
        if (!pendingTransactionBuilder->checkArbitrator(arbitratorTable->get(key))) {
                // TODO: Maybe not throw en error
-               throw new Error("Not all Key Values Match Arbitrator.");
+               //throw new Error("Not all Key Values Match Arbitrator.");
+               myerror("Not all key values match arbitrator\n");
        }
 
        if (kv != NULL) {
@@ -440,13 +443,15 @@ IoTString *Table::getCommittedAtomic(IoTString *key) {
 
 IoTString *Table::getSpeculativeAtomic(IoTString *key) {
        if (!arbitratorTable->contains(key)) {
-               throw new Error("Key not Found.");
+               //throw new Error("Key not Found.");
+               myerror("Key not found\n");
        }
 
        // Make sure new key value pair matches the current arbitrator
        if (!pendingTransactionBuilder->checkArbitrator(arbitratorTable->get(key))) {
                // TODO: Maybe not throw en error
-               throw new Error("Not all Key Values Match Arbitrator.");
+               //throw new Error("Not all Key Values Match Arbitrator.");
+               myerror("Not all Key Values Match Arbitrator.\n");
        }
 
        KeyValue *kv = pendingTransactionSpeculatedKeyValueTable->get(key);
@@ -469,21 +474,21 @@ IoTString *Table::getSpeculativeAtomic(IoTString *key) {
 }
 
 bool Table::update()  {
-       try {
+       //try {
                Array<Slot *> *newSlots = cloud->getSlots(sequenceNumber + 1);
                validateAndUpdate(newSlots, false);
                delete newSlots;
                sendToServer(NULL);
                updateLiveTransactionsAndStatus();
                return true;
-       } catch (Exception *e) {
+               /*      } catch (Exception *e) {
                SetIterator<int64_t, Pair<IoTString *, int32_t> *> *kit = getKeyIterator(localCommunicationTable);
                while (kit->hasNext()) {
                        int64_t m = kit->next();
                        updateFromLocal(m);
                }
                delete kit;
-       }
+               }*/
 
        return false;
 }
@@ -513,13 +518,15 @@ void Table::startTransaction() {
 void Table::put(IoTString *key, IoTString *value) {
        // Make sure it is a valid key
        if (!arbitratorTable->contains(key)) {
-               throw new Error("Key not Found.");
+               //throw new Error("Key not Found.");
+               myerror("Key not Found.\n");
        }
 
        // Make sure new key value pair matches the current arbitrator
        if (!pendingTransactionBuilder->checkArbitrator(arbitratorTable->get(key))) {
                // TODO: Maybe not throw en error
-               throw new Error("Not all Key Values Match Arbitrator.");
+               //throw new Error("Not all Key Values Match Arbitrator.");
+               myerror("Not all Key Values Match Arbitrator.\n");
        }
 
        // Add the key value to this transaction
@@ -557,9 +564,9 @@ TransactionStatus *Table::commitTransaction() {
        
        pendingTransactionBuilder = new PendingTransaction(localMachineId);
 
-       try {
+       //      try {
                sendToServer(NULL);
-       } catch (ServerException *e) {
+               /*      } catch (ServerException *e) {
 
                Hashset<int64_t> *arbitratorTriedAndFailed = new Hashset<int64_t>();
                uint size = pendingTransactionQueue->size();
@@ -590,7 +597,7 @@ TransactionStatus *Table::commitTransaction() {
                        }
                }
                pendingTransactionQueue->setSize(oldindex);
-       }
+               }*/
 
        updateLiveStateFromLocal();
 
@@ -610,7 +617,7 @@ int64_t Table::getLocalSequenceNumber() {
 }
 
 void Table::processTransactionList(bool handlePartial) {
-       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
+       SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *)getKeyIterator(lastTransactionPartsSent);
        while (trit->hasNext()) {
                Transaction *transaction = trit->next();
                transaction->resetServerFailure();
@@ -670,7 +677,7 @@ NewKey * Table::handlePartialSend(NewKey * newKey) {
                        }
                }
                
-               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
+               SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *)getKeyIterator(lastTransactionPartsSent);
                while (trit->hasNext()) {
                        Transaction *transaction = trit->next();
                        transaction->resetServerFailure();
@@ -696,7 +703,7 @@ NewKey * Table::handlePartialSend(NewKey * newKey) {
 
                        processTransactionList(true);
                } else {
-                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
+                       SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *) getKeyIterator(lastTransactionPartsSent);
                        while (trit->hasNext()) {
                                Transaction *transaction = trit->next();
                                transaction->resetServerFailure();
@@ -718,7 +725,7 @@ NewKey * Table::handlePartialSend(NewKey * newKey) {
 void Table::clearSentParts() {
        // Clear the sent data since we are trying again
        pendingSendArbitrationEntriesToDelete->clear();
-       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+       SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *) getKeyIterator(transactionPartsSent);
        while (trit->hasNext()) {
                Transaction *transaction = trit->next();
                delete trit->currVal();
@@ -732,11 +739,12 @@ bool Table::sendToServer(NewKey *newKey) {
                newKey = handlePartialSend(newKey);
        }
 
-       try {
+       //      try {
                // While we have stuff that needs inserting into the block chain
                while ((pendingTransactionQueue->size() > 0) || (pendingSendArbitrationRounds->size() > 0) || (newKey != NULL)) {
                        if (hadPartialSendToServer) {
-                               throw new Error("Should Be error free");
+                               //                              throw new Error("Should Be error free");
+                               myerror("Should Be error free\n");
                        }
                        
                        // If there is a new key with same name then end
@@ -756,7 +764,7 @@ bool Table::sendToServer(NewKey *newKey) {
 
                        if (needsResize) {
                                // Reset which transaction to send
-                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                               SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *) getKeyIterator(transactionPartsSent);
                                while (trit->hasNext()) {
                                        Transaction *transaction = trit->next();
                                        transaction->resetNextPartToSend();
@@ -819,7 +827,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                processTransactionList(false);
                        } else {
                                // Reset which transaction to send
-                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                               SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *) getKeyIterator(transactionPartsSent);
                                while (trit->hasNext()) {
                                        Transaction *transaction = trit->next();
                                        transaction->resetNextPartToSend();
@@ -841,10 +849,10 @@ bool Table::sendToServer(NewKey *newKey) {
                        }
                        delete newSlots;
                }
-       } catch (ServerException *e) {
+               /*      } catch (ServerException *e) {
                if (e->getType() != ServerException_TypeInputTimeout) {
                        // Nothing was able to be sent to the server so just clear these data structures
-                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                       SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *) getKeyIterator(transactionPartsSent);
                        while (trit->hasNext()) {
                                Transaction *transaction = trit->next();
                                transaction->resetNextPartToSend();
@@ -860,7 +868,7 @@ bool Table::sendToServer(NewKey *newKey) {
                        hadPartialSendToServer = true;
 
                        // Nothing was able to be sent to the server so just clear these data structures
-                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                       SetIterator<Transaction *, Vector<int> *> *trit = (SetIterator<Transaction *, Vector<int> *> *) getKeyIterator(transactionPartsSent);
                        while (trit->hasNext()) {
                                Transaction *transaction = trit->next();
                                transaction->resetNextPartToSend();
@@ -872,7 +880,7 @@ bool Table::sendToServer(NewKey *newKey) {
                clearSentParts();
 
                throw e;
-       }
+               }*/
 
        return newKey == NULL;
 }
@@ -1201,7 +1209,8 @@ bool Table::sendSlotsToServer(Slot *slot, int newSize, bool isNewKey, bool *isIn
                return true;
        } else {
                if ((*array)->length() == 0) {
-                       throw new Error("Server Error: Did not send any slots");
+                       //                      throw new Error("Server Error: Did not send any slots");
+                       myerror("Server Error: Did not send any slots\n");
                }
 
                if (hadPartialSendToServer) {
@@ -1467,7 +1476,8 @@ void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal
        // client has seen
        int64_t firstSeqNum = newSlots->get(0)->getSequenceNumber();
        if (firstSeqNum <= sequenceNumber) {
-               throw new Error("Server Error: Sent older slots!");
+               //              throw new Error("Server Error: Sent older slots!");
+               myerror("Server Error: Sent older slots!\n");
        }
 
        // Create an object that can access both new slots and slots in our
@@ -1510,7 +1520,8 @@ void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal
                // hiding slots
                if (!machineSet->isEmpty()) {
                        delete machineSet;
-                       throw new Error("Missing record for machines: ");
+                       //throw new Error("Missing record for machines: ");
+                       myerror("Missing record for machines: \n");
                }
        }
        delete machineSet;
@@ -1602,7 +1613,8 @@ void Table::updateExpectedSize() {
  */
 void Table::checkNumSlots(int numberOfSlots) {
        if (numberOfSlots != expectedsize) {
-               throw new Error("Server Error: Server did not send all slots->  Expected: ");
+               //throw new Error("Server Error: Server did not send all slots->  Expected: ");
+               myerror("Server Error: Server did not send all slots->  Expected: \n");
        }
 }
 
@@ -2489,7 +2501,8 @@ void Table::processSlot(SlotIndexer *indexer, Slot *slot, bool acceptUpdatesToLo
                        processEntry((TableStatus *)entry, slot->getSequenceNumber());
                        break;
                default:
-                       throw new Error("Unrecognized type: ");
+                       //throw new Error("Unrecognized type: ");
+                       myerror("Unrecognized type: \n");
                }
        }
 }
@@ -2561,7 +2574,8 @@ void Table::processEntry(RejectedMessage *entry, SlotIndexer *indexer) {
                        // a rejected slot
                        int64_t slotMachineId = slot->getMachineID();
                        if (isequal != (slotMachineId == machineId)) {
-                               throw new Error("Server Error: Trying to insert rejected message for slot ");
+                               //throw new Error("Server Error: Trying to insert rejected message for slot ");
+                               myerror("Server Error: Trying to insert rejected message for slot\n");
                        }
                }
        }
@@ -2788,7 +2802,8 @@ void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liven
                } else if (livenessType == TypeSlot) {
                        ((Slot *)liveness)->setDead();
                } else {
-                       throw new Error("Unrecognized type");
+                       //throw new Error("Unrecognized type");
+                       myerror("Unrecognized type\n");
                }
        }
        // Get the old last message for this device
@@ -2811,7 +2826,8 @@ void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liven
                } else if (lastEntryType == TypeSlot) {
                        ((Slot *)lastEntry)->setDead();
                } else {
-                       throw new Error("Unrecognized type");
+                       //throw new Error("Unrecognized type");
+                       myerror("Unrecognized type\n");
                }
        }
        // Make sure the server is not playing any games
@@ -2819,17 +2835,20 @@ void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liven
                if (hadPartialSendToServer) {
                        // We were not making any updates and we had a machine mismatch
                        if (lastMessageSeqNum > seqNum && !acceptUpdatesToLocal) {
-                               throw new Error("Server Error: Mismatch on local machine sequence number, needed at least: ");
+                               //throw new Error("Server Error: Mismatch on local machine sequence number, needed at least: ");
+                               myerror("Server Error: Mismatch on local machine sequence number, needed at least: \n");
                        }
                } else {
                        // We were not making any updates and we had a machine mismatch
                        if (lastMessageSeqNum != seqNum && !acceptUpdatesToLocal) {
-                               throw new Error("Server Error: Mismatch on local machine sequence number, needed: ");
+                               //throw new Error("Server Error: Mismatch on local machine sequence number, needed: ");
+                               myerror("Server Error: Mismatch on local machine sequence number, needed: \n");
                        }
                }
        } else {
                if (lastMessageSeqNum > seqNum) {
-                       throw new Error("Server Error: Rollback on remote machine sequence number");
+                       //throw new Error("Server Error: Rollback on remote machine sequence number");
+                       myerror("Server Error: Rollback on remote machine sequence number\n");
                }
        }
 }
@@ -2858,6 +2877,7 @@ void Table::checkHMACChain(SlotIndexer *indexer, Array<Slot *> *newSlots) {
                Slot *prevSlot = indexer->getSlot(currSlot->getSequenceNumber() - 1);
                if (prevSlot != NULL &&
                                !prevSlot->getHMAC()->equals(currSlot->getPrevHMAC()))
-                       throw new Error("Server Error: Invalid HMAC Chain");
+                       //                      throw new Error("Server Error: Invalid HMAC Chain");
+                       myerror("Server Error: Invalid HMAC Chain\n");
        }
 }
index a917c7fce1831bf77401aea30f3467036507eab3..871100ee770fa82ae33263c04fb8675a6292911b 100644 (file)
@@ -17,7 +17,7 @@
 
 /*************************** HEADER FILES ***************************/
 #include <stdlib.h>
-#include <memory.h>
+#include <string.h>
 #include "aes.h"
 
 #include <stdio.h>
index 5e7acdba21d7fbc75b50661da73f18127cbe97bb..152791162a92fb3dadb230027a2646b44d061305 100644 (file)
@@ -3,8 +3,6 @@
 #include <inttypes.h>
 #include "common.h"
 
-typedef uint32_t uint;
-
 template<typename type>
 class Array {
 public:
index 7966477835d3cba23a07601a29997109512c9b32..134c13be3be89cbbb4037f81db936a8237f5fe2d 100644 (file)
@@ -1,7 +1,8 @@
 #ifndef COMMON_H
 #define COMMON_H
 #include <inttypes.h>
-typedef uint32_t uint;
+
+//typedef uint32_t uint;
 #define CMEMALLOC ;
 #define model_print printf
 #define ASSERT(expr) \
@@ -17,7 +18,12 @@ typedef uint32_t uint;
 #include "vector.h"
 #include "array.h"
 
-
+#define myerror(msg)                                                                                                                   \
+       do {                                                                                                                                                                    \
+               printf(msg);                                                                                                                            \
+               exit(-1);                                                                                                                                               \
+       } while(0)                                                                                                                                              
+               
 class TimingSingleton;
 class Abort;
 class Entry;