edits
[iotcloud.git] / version2 / src / C / CloudComm.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 4f97029..9ecbd97
@@ -36,12 +36,6 @@ CloudComm::CloudComm() :
 {
 }
 
-void *threadWrapper(void *cloud) {
-       CloudComm *c = (CloudComm *) cloud;
-       c->localServerWorkerFunction();
-       return NULL;
-}
-
 /**
  * Constructor for actual use. Takes in the url and password.
  */
@@ -135,29 +129,29 @@ IoTString *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t m
        return urlstr;
 }
 
-void loopWrite(TCPClient client, char *array, int bytestowrite) {
+void loopWrite(TCPClient client, char *array, int bytestowrite) {
        int byteswritten = 0;
        while (bytestowrite) {
-               int bytes = client.write(&array[byteswritten], bytestowrite);
+               int bytes = client->write((const unsigned char *) &array[byteswritten], bytestowrite);
                if (bytes >= 0) {
                        byteswritten += bytes;
                        bytestowrite -= bytes;
                } else {
-                       printf("Error in write\n");
+                       //printf("Error in write\n");
                        exit(-1);
                }
        }
 }
 
-void loopRead(TCPClient client, char *array, int bytestoread) {
+void loopRead(TCPClient client, char *array, int bytestoread) {
        int bytesread = 0;
        while (bytestoread) {
-               int bytes = client.read(&array[bytesread], bytestoread);
+               int bytes = client->read((unsigned char *) &array[bytesread], bytestoread);
                if (bytes >= 0) {
                        bytesread += bytes;
                        bytestoread -= bytes;
                } else {
-                       printf("Error in read\n");
+                       //printf("Error in read\n");
                        exit(-1);
                }
        }
@@ -165,7 +159,7 @@ void loopRead(TCPClient client, char *array, int bytestoread) {
 
 WebConnection openURL(IoTString *url) {
        if (url->length() < 7 || memcmp(url->internalBytes()->internalArray(), "http://", 7)) {
-               printf("BOGUS URL\n");
+               //printf("BOGUS URL\n");
                exit(-1);
        }
        int i = 7;
@@ -174,14 +168,14 @@ WebConnection openURL(IoTString *url) {
                        break;
 
        if ( i == url->length()) {
-               printf("ERROR in openURL\n");
+               //printf("ERROR in openURL\n");
                exit(-1);
        }
 
        char *host = (char *) malloc(i - 6);
        memcpy(host, &url->internalBytes()->internalArray()[7], i - 7);
        host[i - 7] = 0;
-       printf("%s\n", host);
+       //printf("%s\n", host);
 
        char *message = (char *)malloc(sizeof("POST  HTTP/1.1\r\n") + sizeof("Host: \r\n") + 2 * url->length());
 
@@ -206,7 +200,7 @@ WebConnection openURL(IoTString *url) {
        
        /* send the request */
        int total = strlen(message);
-       loopWrite(sockfd, message, total);
+       loopWrite(&wc.client, message, total);
        free(message);
        return wc;
 }
@@ -215,12 +209,7 @@ TCPClient createSocket(IoTString *name, int port) {
        char *host = (char *) malloc(name->length() + 1);
        memcpy(host, name->internalBytes()->internalArray(), name->length());
        host[name->length()] = 0;
-       printf("%s\n", host);
-       /* How big is the message? */
-
-       /* create the socket */
-       int sockfd = socket(AF_INET, SOCK_STREAM, 0);
-       if (sockfd < 0) {printf("ERROR opening socket\n"); exit(-1);}
+       //printf("%s\n", host);
 
        /* lookup the ip address */
        TCPClient client;
@@ -232,21 +221,11 @@ TCPClient createSocket(IoTString *name, int port) {
        return client;
 }
 
-TCPServer createSocket(int port) {
-       int fd;
-       struct sockaddr_in sin;
-       TCPServer server = TCPServer(port);
-       server.begin();
-
-       return server;
-}
-
-
-void writeSocketData(int fd, Array<char> *data) {
+void writeSocketData(TCPClient * fd, Array<char> *data) {
        loopWrite(fd, data->internalArray(), data->length());
 }
 
-void writeSocketInt(int fd, int32_t value) {
+void writeSocketInt(TCPClient * fd, int32_t value) {
        char array[4];
        array[0] = value >> 24;
        array[1] = (value >> 16) & 0xff;
@@ -255,7 +234,7 @@ void writeSocketInt(int fd, int32_t value) {
        loopWrite(fd, array, 4);
 }
 
-int readSocketInt(int fd) {
+int readSocketInt(TCPClient * fd) {
        char array[4];
        loopRead(fd, array, 4);
        return (((int32_t)(unsigned char) array[0]) << 24) |
@@ -264,26 +243,28 @@ int readSocketInt(int fd) {
                                 ((int32_t)(unsigned char) array[3]);
 }
 
-void readSocketData(int fd, Array<char> *data) {
+void readSocketData(TCPClient * fd, Array<char> *data) {
        loopRead(fd, data->internalArray(), data->length());
 }
 
 void writeURLDataAndClose(WebConnection *wc, Array<char> *data) {
-       dprintf(wc->fd, "Content-Length: %d\r\n\r\n", data->length());
-       loopWrite(wc->fd, data->internalArray(), data->length());
+       char buffer[300];
+       sprintf(buffer, "Content-Length: %d\r\n\r\n", data->length());
+       wc->client.print(buffer);
+       loopWrite(&wc->client, data->internalArray(), data->length());
 }
 
 void closeURLReq(WebConnection *wc) {
-       dprintf(wc->fd, "\r\n");
+       wc->client.println("");
 }
 
 void readURLData(WebConnection *wc, Array<char> *output) {
-       loopRead(wc->fd, output->internalArray(), output->length());
+       loopRead(&wc->client, output->internalArray(), output->length());
 }
 
 int readURLInt(WebConnection *wc) {
        char array[4];
-       loopRead(wc->fd, array, 4);
+       loopRead(&wc->client, array, 4);
        return (((int32_t)(unsigned char) array[0]) << 24) |
                                 (((int32_t)(unsigned char) array[1]) << 16) |
                                 (((int32_t)(unsigned char) array[2]) << 8) |
@@ -294,11 +275,11 @@ void readLine(WebConnection *wc, char *response, int numBytes) {
        int offset = 0;
        char newchar;
        while (true) {
-               int bytes = read(wc->fd, &newchar, 1);
+               int bytes = wc->client.read((unsigned char *) &newchar, 1);
                if (bytes <= 0)
                        break;
                if (offset == (numBytes - 1)) {
-                       printf("Response too long");
+                       //printf("Response too long");
                        exit(-1);
                }
                response[offset++] = newchar;
@@ -313,7 +294,7 @@ int getResponseCode(WebConnection *wc) {
        readLine(wc, response, sizeof(response));
        int ver1 = 0, ver2 = 0, respcode = 0;
        sscanf(response, "HTTP/%d.%d %d", &ver1, &ver2, &respcode);
-       printf("Response code %d\n", respcode);
+       //printf("Response code %d\n", respcode);
        return respcode;
 }
 
@@ -360,7 +341,7 @@ void CloudComm::setSalt() {
                        //throw new Error("Invalid response");
                        myerror("Invalid response\n");
                }
-               close(wc.fd);
+               wc.client.stop();
 
                timer->endTime();
                salt = saltTmp;
@@ -371,7 +352,8 @@ void CloudComm::setSalt() {
 }
 
 bool CloudComm::getSalt() {
-       WebConnection wc = {-1, -1};
+       WebConnection wc;
+       wc.numBytes = -1;
        IoTString *urlstr = NULL;
 
        //      try {
@@ -412,7 +394,7 @@ bool CloudComm::getSalt() {
                }
                if (wc.numBytes == 0) {
                        timer->endTime();
-                       close(wc.fd);
+                       wc.client.stop();
                        return false;
                }
 
@@ -420,7 +402,7 @@ bool CloudComm::getSalt() {
                int salt_length = readURLInt(&wc);
                Array<char> *tmp = new Array<char>(salt_length);
                readURLData(&wc, tmp);
-               close(wc.fd);
+               wc.client.stop();
 
                salt = tmp;
                timer->endTime();
@@ -532,15 +514,15 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
                if (resptype->equals(getslot)) {
                        delete resptype;
                        Array<Slot *> *tmp = processSlots(&wc);
-                       close(wc.fd);
+                       wc.client.stop();
                        return tmp;
                } else if (resptype->equals(putslot)) {
                        delete resptype;
-                       close(wc.fd);
+                       wc.client.stop();
                        return NULL;
                } else {
                        delete resptype;
-                       close(wc.fd);
+                       wc.client.stop();
                        //throw new Error("Bad response to putslot");
                        myerror("Bad response to putslot\n");
                }
@@ -602,7 +584,7 @@ Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
 
                delete resptype;
                Array<Slot *> *tmp = processSlots(&wc);
-               close(wc.fd);
+               wc.client.stop();
                return tmp;
                /*      } catch (SocketTimeoutException *e) {
                timer->endTime();
@@ -640,7 +622,7 @@ Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequen
        if (salt == NULL)
                return NULL;
        //      try {
-               printf("Passing Locally\n");
+       //printf("Passing Locally\n");
                mac->update(sendData, 0, sendData->length());
                Array<char> *genmac = mac->doFinal();
                Array<char> *totalData = new Array<char>(sendData->length() + genmac->length());
@@ -652,21 +634,21 @@ Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequen
                Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
 
                // Open a TCP socket connection to a local device
-               int socket = createSocket(host, port);
+               TCPClient socket = createSocket(host, port);
 
                timer->startTime();
                // Send data to output (length of data, the data)
-               writeSocketInt(socket, encryptedData->length());
-               writeSocketData(socket, encryptedData);
+               writeSocketInt(&socket, encryptedData->length());
+               writeSocketData(&socket, encryptedData);
 
-               int lengthOfReturnData = readSocketInt(socket);
+               int lengthOfReturnData = readSocketInt(&socket);
                Array<char> *returnData = new Array<char>(lengthOfReturnData);
-               readSocketData(socket, returnData);
+               readSocketData(&socket, returnData);
                timer->endTime();
                returnData = stripIVAndDecryptSlot(returnData);
 
                // We are done with this socket
-               close(socket);
+               socket.stop();
                mac->update(returnData, 0, returnData->length() - CloudComm_HMAC_SIZE);
                Array<char> *realmac = mac->doFinal();
                Array<char> *recmac = new Array<char>(CloudComm_HMAC_SIZE);
@@ -687,71 +669,6 @@ Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequen
        return NULL;
 }
 
-void CloudComm::localServerWorkerFunction() {
-       /*      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);
-                       Array<char> *genmac = mac->doFinal();
-                       Array<char> *recmac = new Array<char>(CloudComm_HMAC_SIZE);
-                       System_arraycopy(readData, readData->length() - recmac->length(), recmac, 0, recmac->length());
-
-                       if (!recmac->equals(genmac))
-                               //                              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());
-
-                       // Process the data
-                       Array<char> *sendData = table->acceptDataFromLocal(returnData);
-                       mac->update(sendData, 0, sendData->length());
-                       Array<char> *realmac = mac->doFinal();
-                       Array<char> *totalData = new Array<char>(sendData->length() + realmac->length());
-                       System_arraycopy(sendData, 0, totalData, 0, sendData->length());
-                       System_arraycopy(realmac, 0, totalData, sendData->length(), realmac->length());
-
-                       // Encrypt the data for sending
-                       Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
-                       Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
-
-                       timer->startTime();
-                       // Send data to output (length of data, the data)
-                       writeSocketInt(socket, encryptedData->length());
-                       writeSocketData(socket, encryptedData);
-                       close(socket);
-               } catch (Exception *e) {
-               }
-       }
-       
-       if (inputSocket != -1) {
-               try {
-                       close(inputSocket);
-               } catch (Exception *e) {
-                       throw new Error("Local server close failure...");
-               }
-               }*/
-}
-
 void CloudComm::closeCloud() {
        doEnd = true;