2f825083214a26608087b08b656d575c59004119
[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
36         /**
37          * Generates Key from password.
38          */
39         AESKey *initKey();
40
41         /**
42          * Inits the HMAC generator.
43          */
44         void initCrypt();
45
46         /*
47          * Builds the URL for the given request.
48          */
49         IoTString *buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries);
50         void setSalt();
51         bool getSalt();
52         Array<char> *createIV(int64_t machineId, int64_t localSequenceNumber);
53         Array<char> *encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes);
54         Array<char> *stripIVAndDecryptSlot(Array<char> *rawData);
55         Array<Slot *> *processSlots(int fd);
56
57
58 public:
59         /**
60          * Empty Constructor needed for child class.
61          */
62         CloudComm();
63
64         /**
65          * Constructor for actual use. Takes in the url and password.
66          */
67         CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password, int _listeningPort);
68
69         /**
70          * Inits all the security stuff
71          */
72         void initSecurity();
73
74         /*
75          * API for putting a slot into the queue.  Returns NULL on success.
76          * On failure, the server will send slots with newer sequence
77          * numbers.
78          */
79         Array<Slot *> *putSlot(Slot *slot, int max);
80
81         /**
82          * Request the server to send all slots with the given
83          * sequencenumber or newer.
84          */
85         Array<Slot *> *getSlots(int64_t sequencenumber);
86
87
88         /**
89          * Method that actually handles building Slot objects from the
90          * server response.  Shared by both putSlot and getSlots.
91          */
92
93         Array<char> *sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString *host, int port);
94         void close();
95         void localServerWorkerFunction();
96 };
97 #endif