{
}
-void *threadWrapper(void *cloud) {
- CloudComm *c = (CloudComm *) cloud;
- c->localServerWorkerFunction();
- return NULL;
-}
-
/**
* Constructor for actual use. Takes in the url and password.
*/
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);
}
}
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;
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());
/* send the request */
int total = strlen(message);
- loopWrite(sockfd, message, total);
+ loopWrite(&wc.client, message, total);
free(message);
return wc;
}
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;
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;
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) |
((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) |
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;
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;
}
//throw new Error("Invalid response");
myerror("Invalid response\n");
}
- close(wc.fd);
+ wc.client.stop();
timer->endTime();
salt = saltTmp;
}
bool CloudComm::getSalt() {
- WebConnection wc = {-1, -1};
+ WebConnection wc;
+ wc.numBytes = -1;
IoTString *urlstr = NULL;
// try {
}
if (wc.numBytes == 0) {
timer->endTime();
- close(wc.fd);
+ wc.client.stop();
return false;
}
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();
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");
}
delete resptype;
Array<Slot *> *tmp = processSlots(&wc);
- close(wc.fd);
+ wc.client.stop();
return tmp;
/* } catch (SocketTimeoutException *e) {
timer->endTime();
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());
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);
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;