From: bdemsky Date: Mon, 5 Mar 2018 17:33:29 +0000 (-0800) Subject: All code written X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=commitdiff_plain;h=97874483ce2547c8061cebaac2d1f14adfde86aa;hp=8981ac1037b9381ff64428f9236cbf2d4954d1e2 All code written --- diff --git a/version2/src/C/CloudComm.cc b/version2/src/C/CloudComm.cc index 3b5fb97..dc28355 100644 --- a/version2/src/C/CloudComm.cc +++ b/version2/src/C/CloudComm.cc @@ -10,8 +10,12 @@ #include "Crypto.h" #include "ByteBuffer.h" #include "aes.h" +#include #include +#include +#include #include +#include /** * 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 *data) {} +void writeSocketData(int fd, Array *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 *data) {} +void readSocketData(int fd, Array *data) { + loopRead(fd, data->internalArray(), data->length()); +} + +void writeURLDataAndClose(int fd, Array *data) { + dprintf(fd, "Content-Length: %d\r\n\r\n", data->length()); + loopWrite(fd, data->internalArray(), data->length()); +} -void writeURLData(int fd, Array *data) { +void closeURLReq(int fd) { + dprintf(fd, "\r\n"); } void readURLData(int fd, Array *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 *CloudComm::putSlot(Slot *slot, int max) { Array *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 *CloudComm::putSlot(Slot *slot, int max) { } try { + int respcode = getResponseCode(fd); timer->startTime(); Array *resptype = new Array(7); readURLData(fd, resptype); @@ -352,7 +554,8 @@ Array *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 *CloudComm::getSlots(int64_t sequencenumber) { try { timer->startTime(); + int responsecode = getResponseCode(fd); Array *resptype = new Array(7); readURLData(fd, resptype); timer->endTime(); diff --git a/version2/src/C/IoTString.h b/version2/src/C/IoTString.h index 2d7a14f..cb2d68b 100644 --- a/version2/src/C/IoTString.h +++ b/version2/src/C/IoTString.h @@ -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.