edits
[iotcloud.git] / version2 / src / C / CloudComm.cc
index 54b92da7569ffc383114c986660a4e8fd0a6cfc8..edc07069109a0578795863dccfe9ab6ba2bba4c9 100644 (file)
@@ -86,7 +86,6 @@ void CloudComm::initCrypt() {
        if (password == NULL) {
                return;
        }
-
        try {
                key = initKey();
                password = NULL;// drop password
@@ -101,33 +100,40 @@ void CloudComm::initCrypt() {
  * Builds the URL for the given request.
  */
 URL *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries) {
-       IoTString *reqstring = isput ? "req=putslot" : "req=getslot";
-       IoTString *urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
+       const char *reqstring = isput ? "req=putslot" : "req=getslot";
+       char * buffer = (char *) malloc(baseurl->length() + 200);
+       memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+       int offset = baseurl->length();
+  offset+=sprintf(&buffer[offset], "?%s&seq=%" PRId64, reqstring, sequencenumber);
        if (maxentries != 0)
-               urlstr += "&max=" + maxentries;
+               sprintf(&buffer[offset], "&max=%" PRId64, maxentries);
+       IoTString *urlstr = new IoTString(buffer);
+       free(buffer);
        return new URL(urlstr);
 }
 
 void CloudComm::setSalt() {
-
        if (salt != NULL) {
-               // Salt already sent to server so dont set it again
+               // Salt already sent to server so don't set it again
                return;
        }
-
+       
        try {
                Array<char> *saltTmp = new Array<char>(CloudComm_SALT_SIZE);
                random->nextBytes(saltTmp);
 
-               for (int i = 0; i < CloudComm_SALT_SIZE; i++) {
-                       printf("%d\n", (int)saltTmp->get(i) & 255);
-               }
-
-               URL *url = new URL(baseurl + "?req=setsalt");
+               char * buffer = (char *) malloc(baseurl->length() + 100);
+               memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+               int offset = baseurl->length();
+               offset+=sprintf(&buffer[offset], "?req=setsalt");
+               IoTString *urlstr = new IoTString(buffer);
+               free(buffer);
+               
+               URL *url = new URL(urlstr);
                timer->startTime();
                URLConnection *con = url->openConnection();
                HttpURLConnection *http = (HttpURLConnection *) con;
-
+               
                http->setRequestMethod("POST");
                http->setFixedLengthStreamingMode(saltTmp->length());
                http->setDoOutput(true);
@@ -140,8 +146,6 @@ void CloudComm::setSalt() {
 
                int responsecode = http->getResponseCode();
                if (responsecode != HttpURLConnection_HTTP_OK) {
-                       // TODO: Remove this print
-                       printf("%d\n", responsecode);
                        throw new Error("Invalid response");
                }
 
@@ -159,7 +163,14 @@ bool CloudComm::getSalt() {
        HttpURLConnection *http = NULL;
 
        try {
-               url = new URL(baseurl + "?req=getsalt");
+               char * buffer = (char *) malloc(baseurl->length() + 100);
+               memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+               int offset = baseurl->length();
+               offset+=sprintf(&buffer[offset], "?req=getsalt");
+               IoTString *urlstr = new IoTString(buffer);
+               free(buffer);
+
+               url = new URL(urlstr);
        } catch (Exception *e) {
                throw new Error("getSlot failed");
        }
@@ -219,9 +230,7 @@ Array<char> *CloudComm::encryptSlotAndPrependIV(Array<char> *rawData, Array<char
                IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes);
                Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding");
                cipher->init(Cipher_ENCRYPT_MODE, key, ivSpec);
-
                Array<char> *encryptedBytes = cipher->doFinal(rawData);
-
                Array<char> *chars = new Array<char>(encryptedBytes->length() + CloudComm_IV_SIZE);
                System_arraycopy(ivBytes, 0, chars, 0, ivBytes->length());
                System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes->length());
@@ -238,7 +247,6 @@ Array<char> *CloudComm::stripIVAndDecryptSlot(Array<char> *rawData) {
                Array<char> *encryptedBytes = new Array<char>(rawData->length() - CloudComm_IV_SIZE);
                System_arraycopy(rawData, 0, ivBytes, 0, CloudComm_IV_SIZE);
                System_arraycopy(rawData, CloudComm_IV_SIZE, encryptedBytes, 0, encryptedBytes->length);
-
                IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes);
                Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding");
                cipher->init(Cipher_DECRYPT_MODE, key, ivSpec);