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