35d5fcce68ad200eba75fd8d6427ce73be13025a
[iotcloud.git] / version2 / src / C / CloudComm.h
1 #ifndef CLOUDCOMM_H
2 #define CLOUDCOMM_H
3
4 #include "common.h"
5 #include "application.h"
6
7 //#include <pthread.h>
8 /**
9  * This class provides a communication API to the webserver.  It also
10  * validates the HMACs on the slots and handles encryption.
11  * @author Brian Demsky <bdemsky@uci.edu>
12  * @version 1.0
13  */
14
15 #define CloudComm_SALT_SIZE 8
16 #define CloudComm_TIMEOUT_MILLIS 5000
17 ;       // 100
18 #define CloudComm_IV_SIZE 16
19 /** Sets the size for the HMAC. */
20 #define CloudComm_HMAC_SIZE 32
21 #define HttpURLConnection_HTTP_OK 200
22
23 typedef struct {
24         TCPClient client;
25         int numBytes;
26 } WebConnection;
27
28
29 class CloudComm {
30 private:
31         IoTString *baseurl;
32         AESKey *key;
33         Mac *mac;
34         IoTString *password;
35         SecureRandom *random;
36         Array<char> *salt;
37         Table *table;
38         int32_t listeningPort;
39         //      pthread_t localServerThread;
40         bool doEnd;
41         TimingSingleton *timer;
42         Array<char> *getslot;
43         Array<char> *putslot;
44
45         /**
46          * Generates Key from password.
47          */
48         AESKey *initKey();
49
50         /**
51          * Inits the HMAC generator.
52          */
53         void initCrypt();
54
55         /*
56          * Builds the URL for the given request.
57          */
58         IoTString *buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries);
59         void setSalt();
60         bool getSalt();
61         Array<char> *createIV(int64_t machineId, int64_t localSequenceNumber);
62         Array<char> *encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes);
63         Array<char> *stripIVAndDecryptSlot(Array<char> *rawData);
64         Array<Slot *> *processSlots(WebConnection *wc);
65
66
67 public:
68         /**
69          * Empty Constructor needed for child class.
70          */
71         CloudComm();
72
73         /**
74          * Constructor for actual use. Takes in the url and password.
75          */
76         CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password, int _listeningPort);
77         ~CloudComm();
78
79         /**
80          * Inits all the security stuff
81          */
82         void initSecurity();
83
84         /*
85          * API for putting a slot into the queue.  Returns NULL on success.
86          * On failure, the server will send slots with newer sequence
87          * numbers.
88          */
89         Array<Slot *> *putSlot(Slot *slot, int max);
90
91         /**
92          * Request the server to send all slots with the given
93          * sequencenumber or newer.
94          */
95         Array<Slot *> *getSlots(int64_t sequencenumber);
96
97
98         /**
99          * Method that actually handles building Slot objects from the
100          * server response.  Shared by both putSlot and getSlots.
101          */
102
103         Array<char> *sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString *host, int port);
104         void closeCloud();
105         void localServerWorkerFunction();
106 };
107 #endif