All code written
authorbdemsky <bdemsky@uci.edu>
Mon, 5 Mar 2018 17:33:29 +0000 (09:33 -0800)
committerbdemsky <bdemsky@uci.edu>
Mon, 5 Mar 2018 18:17:24 +0000 (10:17 -0800)
version2/src/C/CloudComm.cc
version2/src/C/IoTString.h

index 3b5fb970199962959a18895dc03824a3b923ebf4..dc2835507966b311950773da54347ce3921028eb 100644 (file)
 #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.
@@ -120,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 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 *host, int port) {
-       return 0;
+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) {}
+void readSocketData(int fd, Array<char> *data) {
+       loopRead(fd, data->internalArray(), data->length());
+}
+
+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) {
+       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() {
@@ -177,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) {
@@ -209,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();
@@ -303,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();
@@ -317,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);
@@ -352,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();
@@ -367,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();
index 2d7a14f9ef476b967ac79baef03a0464f199d7dd..cb2d68b1af6dd13ddebf8ebcf2f95faef0bca912 100644 (file)
@@ -50,6 +50,8 @@ public:
                delete array;
        }
 
+       char get(uint i) {return array->get(i);}
+       
        /**
         * Internal method to grab a reference to our char array.  Caller
         * must not modify it.