edits
[iotcloud.git] / version2 / src / C / CloudComm.h
1 #ifndef CLOUDCOMM_H
2 #define CLOUDCOMM_H
3
4 #include "common.h"
5 #include <pthread.h>
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 #define HttpURLConnection_HTTP_OK 200
20
21
22 class CloudComm {
23 private:
24         IoTString *baseurl;
25         AESKey *key;
26         Mac *mac;
27         IoTString *password;
28         SecureRandom *random;
29         Array<char> *salt;
30         Table *table;
31         int32_t listeningPort;
32         pthread_t localServerThread;
33         bool doEnd;
34         TimingSingleton *timer;
35         Array<char> *getslot;
36         Array<char> *putslot;
37
38         /**
39          * Generates Key from password.
40          */
41         AESKey *initKey();
42
43         /**
44          * Inits the HMAC generator.
45          */
46         void initCrypt();
47
48         /*
49          * Builds the URL for the given request.
50          */
51         IoTString *buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries);
52         void setSalt();
53         bool getSalt();
54         Array<char> *createIV(int64_t machineId, int64_t localSequenceNumber);
55         Array<char> *encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes);
56         Array<char> *stripIVAndDecryptSlot(Array<char> *rawData);
57         Array<Slot *> *processSlots(int fd);
58
59
60 public:
61         /**
62          * Empty Constructor needed for child class.
63          */
64         CloudComm();
65
66         /**
67          * Constructor for actual use. Takes in the url and password.
68          */
69         CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password, int _listeningPort);
70         ~CloudComm();
71         
72         /**
73          * Inits all the security stuff
74          */
75         void initSecurity();
76
77         /*
78          * API for putting a slot into the queue.  Returns NULL on success.
79          * On failure, the server will send slots with newer sequence
80          * numbers.
81          */
82         Array<Slot *> *putSlot(Slot *slot, int max);
83
84         /**
85          * Request the server to send all slots with the given
86          * sequencenumber or newer.
87          */
88         Array<Slot *> *getSlots(int64_t sequencenumber);
89
90
91         /**
92          * Method that actually handles building Slot objects from the
93          * server response.  Shared by both putSlot and getSlots.
94          */
95
96         Array<char> *sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString *host, int port);
97         void closeCloud();
98         void localServerWorkerFunction();
99 };
100 #endif