All code written
[iotcloud.git] / version2 / src / C / CloudComm.cc
index 4e4000fae9ee310be4836cbb3b83a021ef902005..dc2835507966b311950773da54347ce3921028eb 100644 (file)
@@ -9,8 +9,13 @@
 #include "Slot.h"
 #include "Crypto.h"
 #include "ByteBuffer.h"
+#include "aes.h"
+#include <sys/types.h>
 #include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/tcp.h>
 #include <unistd.h>
+#include <netdb.h>
 
 /**
  * Empty Constructor needed for child class.
@@ -32,7 +37,7 @@ CloudComm::CloudComm() :
 {
 }
 
-void * threadWrapper(void * cloud) {
+void *threadWrapper(void *cloud) {
        CloudComm *c = (CloudComm *) cloud;
        c->localServerWorkerFunction();
        return NULL;
@@ -63,10 +68,10 @@ CloudComm::CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password,
  */
 AESKey *CloudComm::initKey() {
        try {
-               AESKey * key = new AESKey(password->internalBytes(),
-                                                                                                                       salt,
-                                                                                                                       65536,
-                                                                                                                       128);
+               AESKey *key = new AESKey(password->internalBytes(),
+                                                                                                                salt,
+                                                                                                                65536,
+                                                                                                                128);
                return key;
        } catch (Exception *e) {
                throw new Error("Failed generating key.");
@@ -97,7 +102,7 @@ void CloudComm::initCrypt() {
        try {
                key = initKey();
                password = NULL;// drop password
-               mac = Mac_getInstance("HmacSHA256");
+               mac = new Mac();
                mac->init(key);
        } catch (Exception *e) {
                throw new Error("Failed To Initialize Ciphers");
@@ -119,42 +124,238 @@ IoTString *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t m
        return urlstr;
 }
 
-int openURL(IoTString *url, bool isPost) {
-       return 0;
+void loopWrite(int fd, char * array, int bytestowrite) {
+       int byteswritten = 0;
+       while (bytestowrite) {
+               int bytes = write(fd, & array[byteswritten], bytestowrite);
+               if (bytes >= 0) {
+                       byteswritten += bytes;
+                       bytestowrite -= bytes;
+               } else {
+                       printf("Error in write\n");
+                       exit(-1);
+               }
+       }
+}
+
+void loopRead(int fd, char * array, int bytestoread) {
+       int bytesread = 0;
+       while (bytestoread) {
+               int bytes = read(fd, & array[bytesread], bytestoread);
+               if (bytes >= 0) {
+                       bytesread += bytes;
+                       bytestoread -= bytes;
+               } else {
+                       printf("Error in read\n");
+                       exit(-1);
+               }
+       }
 }
 
-int createSocket(IoTString *host, int port) {
-       return 0;
+int openURL(IoTString *url) {
+       if (url->length() < 7 || memcmp(url->internalBytes()->internalArray(), "http://", 7)) {
+               printf("BOGUS URL\n");
+               exit(-1);
+       }
+       int i = 7;
+       for(; i < url->length(); i++)
+               if (url->get(i) == '/')
+                       break;
+
+       if ( i == url->length()) {
+               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);
+
+       char * message = (char *)malloc(sizeof("POST  HTTP/1.1\r\n") + sizeof("Host: \r\n") + 2*url->length());
+       
+       /* fill in the parameters */
+       int post = sprintf(message,"POST ");
+       /* copy data */
+       memcpy(&message[post], url->internalBytes()->internalArray(), url->length());
+       int endpost = sprintf(&message[post+url->length()], " HTTP/1.1\r\n");
+
+       int hostlen = sprintf(&message[endpost + post + url->length()], "Host: ");
+       memcpy(&message[endpost + post + url->length()+hostlen], url->internalBytes()->internalArray(), url->length());
+       sprintf(&message[endpost + post + 2*url->length()+hostlen], "\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;
+
+       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);
+       }
+
+       /* send the request */
+       int total = strlen(message);
+       loopWrite(sockfd, message, total);
+
+       return sockfd;
+}
+
+int 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);}
+       
+       /* 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);
+       }
+       
+       return sockfd;
 }
 
 int createSocket(int port) {
-       return 0;
+       int fd;
+       struct sockaddr_in sin;
+
+  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;
 }
 
 int acceptSocket(int socket) {
-       return 0;
+  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) {}
+void writeSocketData(int fd, Array<char> *data) {
+       loopWrite(fd, data->internalArray(), data->length());
+}
 
-void writeSocketInt(int fd, int value) {}
+void writeSocketInt(int fd, int32_t value) {
+       char array[4];
+       array[0] = value >> 24;
+       array[1] = (value >> 16) & 0xff;
+       array[2] = (value >> 8) & 0xff;
+       array[3] = (value >> 8) & 0xff;
+       loopWrite(fd, array, 4);
+}
 
-int readSocketInt(int fd) {return 0;}
+int readSocketInt(int fd) {
+       char array[4];
+       loopRead(fd, array, 4);
+       return (((int32_t) array[0]) << 24) |
+               (((int32_t) array[1]) << 16) |
+               (((int32_t) array[2]) << 8) |
+               ((int32_t) array[3]);
+}
+
+void readSocketData(int fd, Array<char> *data) {
+       loopRead(fd, data->internalArray(), data->length());
+}
 
-void readSocketData(int fd, Array<char> *data) {}
+void writeURLDataAndClose(int fd, Array<char> *data) {
+       dprintf(fd, "Content-Length: %d\r\n\r\n", data->length());
+       loopWrite(fd, data->internalArray(), data->length());
+}
 
-void writeURLData(int fd, Array<char> *data) {
+void closeURLReq(int fd) {
+       dprintf(fd, "\r\n");
 }
 
-void readURLData(int fd, Array<char> * output) {
+void readURLData(int fd, Array<char> *output) {
+       loopRead(fd, output->internalArray(), output->length());
 }
 
 int readURLInt(int fd) {
-       return 0;
+       char array[4];
+       loopRead(fd, array, 4);
+       return (((int32_t) array[0]) << 24) |
+               (((int32_t) array[1]) << 16) |
+               (((int32_t) array[2]) << 8) |
+               ((int32_t) array[3]);
 }
 
 int getResponseCode(int fd) {
-       return 0;
+       char response[600];
+       int offset = 0;
+       char newchar;
+       while(true) {
+               int bytes = read(fd, &newchar, 1);
+               if (bytes <= 0)
+                       break;
+               if (offset == (sizeof(response) - 1)) {
+                       printf("Response too long");
+                       exit(-1);
+               }
+               response[offset++] = newchar;
+               if (newchar == '\n')
+                       break;
+       }
+       response[offset] = 0;
+       int ver1 = 0, ver2 = 0, respcode = 0;
+       sscanf(response, "HTTP-%d.%d %d", &ver1, &ver2, &respcode);
+       return respcode;
 }
 
 void CloudComm::setSalt() {
@@ -176,8 +377,8 @@ void CloudComm::setSalt() {
                free(buffer);
 
                timer->startTime();
-               fd = openURL(urlstr, true);
-               writeURLData(fd, saltTmp);
+               fd = openURL(urlstr);
+               writeURLDataAndClose(fd, saltTmp);
 
                int responsecode = getResponseCode(fd);
                if (responsecode != HttpURLConnection_HTTP_OK) {
@@ -195,7 +396,7 @@ void CloudComm::setSalt() {
 bool CloudComm::getSalt() {
        int fd = -1;
        IoTString *urlstr = NULL;
-       
+
        try {
                char *buffer = (char *) malloc(baseurl->length() + 100);
                memcpy(buffer, baseurl->internalBytes(), baseurl->length());
@@ -208,7 +409,8 @@ bool CloudComm::getSalt() {
        }
        try {
                timer->startTime();
-               fd = openURL(urlstr, true);
+               fd = openURL(urlstr);
+               closeURLReq(fd);
                timer->endTime();
        } catch (SocketTimeoutException *e) {
                timer->endTime();
@@ -245,12 +447,16 @@ Array<char> *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber)
        return buffer->array();
 }
 
-Array<char> *AESEncrypt(Array<char> * ivBytes, AESKey * key, Array<char> * data) {
-       return NULL;
+Array<char> *AESEncrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
+       Array<char> * output=new Array<char>(data->length());
+       aes_encrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *) output->internalArray(), (WORD *)key->getKey()->internalArray(), key->getKey()->length()/(sizeof(WORD)/sizeof(BYTE)), (BYTE *)ivBytes->internalArray());
+       return output;
 }
 
-Array<char> *AESDecrypt(Array<char> * ivBytes, AESKey * key, Array<char> * data) {
-       return NULL;
+Array<char> *AESDecrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
+       Array<char> * output=new Array<char>(data->length());
+       aes_decrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *)output->internalArray(), (WORD *)key->getKey()->internalArray(), key->getKey()->length()/(sizeof(WORD)/sizeof(BYTE)), (BYTE *)ivBytes->internalArray());
+       return output;
 }
 
 Array<char> *CloudComm::encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes) {
@@ -298,8 +504,8 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
                Array<char> *chars = encryptSlotAndPrependIV(slotBytes, slot->getSlotCryptIV());
                IoTString *url = buildRequest(true, sequencenumber, max);
                timer->startTime();
-               fd = openURL(url, true);
-               writeURLData(fd, chars);
+               fd = openURL(url);
+               writeURLDataAndClose(fd, chars);
                timer->endTime();
        } catch (ServerException *e) {
                timer->endTime();
@@ -312,6 +518,7 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
        }
 
        try {
+               int respcode = getResponseCode(fd);
                timer->startTime();
                Array<char> *resptype = new Array<char>(7);
                readURLData(fd, resptype);
@@ -347,7 +554,8 @@ Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
 
                IoTString *url = buildRequest(false, sequencenumber, 0);
                timer->startTime();
-               fd = openURL(url, true);
+               fd = openURL(url);
+               closeURLReq(fd);
                timer->endTime();
        } catch (SocketTimeoutException *e) {
                timer->endTime();
@@ -362,6 +570,7 @@ Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
 
        try {
                timer->startTime();
+               int responsecode = getResponseCode(fd);
                Array<char> *resptype = new Array<char>(7);
                readURLData(fd, resptype);
                timer->endTime();
@@ -397,7 +606,7 @@ Array<Slot *> *CloudComm::processSlots(int fd) {
        return slots;
 }
 
-Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString * host, int port) {
+Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString *host, int port) {
        if (salt == NULL)
                return NULL;
        try {