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