-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTag.cpp
290 lines (248 loc) · 6.95 KB
/
Tag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using namespace std;
#include <string.h>
#include <iostream>
#include "Tag.h"
#include "hash.h"
#include "test.hpp"
//Message Functions
uint8_t Tag::getAttrHash(int i) {
return _attrHash[i];
}
uint8_t Tag::getValHash(int i) {
return _valHash[i];
}
uint8_t Tag::getRHash(int i) {
return _rHash[i];
}
string Tag::getR() {
return _r;
}
// == Operator
bool Tag::operator==(const Tag &other)
{
return memcmp(_attrHash, other._attrHash, HASH_SIZE) == 0;
// bool valBool = memcmp(_valHash, other._valHash, HASH_SIZE) == 0;
// return attrBool && valBool;
}
//Matched
void Tag::matched()
{
_match = true;
}
bool Tag::isReal()
{
return _real;
}
bool Tag::isMatch()
{
return _match;
}
/* Generates a hash for both the attribute and val and then stores it in the
* Tag structure.
* @param tag - a Tag object
* @param key - private key
*/
void Tag::genHash(uint8_t *key)
{
KeyedHash(key, HASH_SIZE, (uint8_t *)_attr, _attrLen, _attrHash, HASH_SIZE);
KeyedHash(key, HASH_SIZE, (uint8_t *)_val, _valLen, _valHash, HASH_SIZE);
}
void Tag::genSubHash(uint8_t *key){
genHash(key);
_r = randomString(HASH_SIZE);
KeyedHash(_valHash, HASH_SIZE, (uint8_t *)_r.c_str(), HASH_SIZE, _rHash, HASH_SIZE);
}
bool Tag::compareRHash(Tag interest){
uint8_t tempHash[HASH_SIZE];
KeyedHash(_valHash, HASH_SIZE, (uint8_t *)interest._r.c_str(), HASH_SIZE, tempHash, HASH_SIZE);
// KeyedHash(_valHash, HASH_SIZE, (uint8_t *)interest._r.c_str(), HASH_SIZE, _rHash, HASH_SIZE);
return memcmp(tempHash, interest._rHash, HASH_SIZE) == 0;
}
/* ----------- CONTRUCTORS ----------- */
//Tag and Interest Constructor
Tag::Tag(const char *newAttr, const char *newVal, bool realBool, bool isPublisher, char opr)
{
strcpy(_attr, newAttr);
strcpy(_val, newVal);
_attrLen = strlen(newAttr);
_valLen = strlen(newVal);
_isPublisher = isPublisher;
_real = realBool;
_opr = opr;
}
Tag::Tag(uint8_t *attrHash, uint8_t *valHash, bool isPublisher, uint8_t *rHash, string r)
{
memcpy(_attrHash, attrHash,HASH_SIZE);
if(isPublisher){
memcpy(_valHash, valHash,HASH_SIZE);
} else {
memcpy(_rHash, rHash,HASH_SIZE);
_r = r;
}
_isPublisher = isPublisher;
}
//Empty Constructor
Tag::Tag(){};
//Deconstructor
Tag::~Tag(){};
//Copy Constructor
Tag::Tag(const Tag &other)
{
//Copy Attributes and Values
memcpy(_attr, other._attr, INPUT_SIZE);
memcpy(_val, other._val, INPUT_SIZE);
//Copy Hashes of attributes and values
memcpy(_attrHash, other._attrHash, HASH_SIZE);
memcpy(_valHash, other._valHash, HASH_SIZE);
memcpy(_rHash, other._rHash, HASH_SIZE);
//Copy Attributes and Value lengths
_attrLen = other._attrLen;
_valLen = other._valLen;
//Remaining stuff copied
_isPublisher = other._isPublisher;
_opr = other._opr;
_match = other._match;
_r = other._r;
_real = other._real;
}
//Copy Assignment
Tag &Tag::operator=(const Tag &other)
{
//Delete Old elements
memset(_attr, 0, INPUT_SIZE);
memset(_val, 0, INPUT_SIZE);
memset(_attrHash, 0, HASH_SIZE);
memset(_valHash, 0, HASH_SIZE);
memset(_rHash, 0, HASH_SIZE);
//Copy Attributes and Values
memcpy(_attr, other._attr, INPUT_SIZE);
memcpy(_val, other._val, INPUT_SIZE);
//Copy Hashes of attributes and values
memcpy(_attrHash, other._attrHash, HASH_SIZE);
memcpy(_valHash, other._valHash, HASH_SIZE);
memcpy(_rHash, other._rHash, HASH_SIZE);
//Copy Attributes and Value lengths
_attrLen = other._attrLen;
_valLen = other._valLen;
//Remaining stuff copied
_isPublisher = other._isPublisher;
_opr = other._opr;
_match = other._match;
_r = other._r;
_real = other._real;
}
//Move Contructor
Tag::Tag(Tag &&other)
{
//Move Attributes and Values
memcpy(_attr, other._attr, INPUT_SIZE);
memcpy(_val, other._val, INPUT_SIZE);
memset(other._attr, 0, INPUT_SIZE);
memset(other._val, 0, INPUT_SIZE);
//Copy Hashes of attributes and values
memcpy(_attrHash, other._attrHash, HASH_SIZE);
memcpy(_valHash, other._valHash, HASH_SIZE);
memcpy(_rHash, other._rHash, HASH_SIZE);
memset(other._attrHash, 0, HASH_SIZE);
memset(other._valHash, 0, HASH_SIZE);
memset(other._rHash, 0, HASH_SIZE);
//Copy Attributes and Value lengths
_attrLen = other._attrLen;
_valLen = other._valLen;
other._attrLen = 0;
other._valLen = 0;
//Remaining stuff copied
_isPublisher = other._isPublisher;
_opr = other._opr;
_match = other._match;
_r = other._r;
_real = other._real;
other._isPublisher = 0;
other._opr = 0;
other._match = 0;
other._r = "";
other._real = 0;
}
//Move Assignment
Tag &Tag::operator=(Tag &&other)
{
//Delete old elements
memset(_attr, 0, INPUT_SIZE);
memset(_val, 0, INPUT_SIZE);
memset(_attrHash, 0, HASH_SIZE);
memset(_valHash, 0, HASH_SIZE);
memset(_rHash, 0, HASH_SIZE);
//Move Attributes and Values
memcpy(_attr, other._attr, INPUT_SIZE);
memcpy(_val, other._val, INPUT_SIZE);
memset(other._attr, 0, INPUT_SIZE);
memset(other._val, 0, INPUT_SIZE);
//Copy Hashes of attributes and values
memcpy(_attrHash, other._attrHash, HASH_SIZE);
memcpy(_valHash, other._valHash, HASH_SIZE);
memcpy(_rHash, other._rHash, HASH_SIZE);
memset(other._attrHash, 0, HASH_SIZE);
memset(other._valHash, 0, HASH_SIZE);
memset(other._rHash, 0, HASH_SIZE);
//Copy Attributes and Value lengths
_attrLen = other._attrLen;
_valLen = other._valLen;
other._attrLen = 0;
other._valLen = 0;
//Remaining stuff copied
_isPublisher = other._isPublisher;
_opr = other._opr;
_match = other._match;
_r = other._r;
_real = other._real;
other._isPublisher = 0;
other._opr = 0;
other._match = 0;
other._r = "";
other._real = 0;
}
/* ----------- DEBUG FUNCTIONS ----------- */
/*Prints out all important tag information
*/
void Tag::print(bool attr, bool val, bool attrHash, bool valHash, bool extra)
{
cout << " -------------------- " << endl;
if (_isPublisher)
cout << "THIS IS A TAG" << endl;
else
cout << "THIS IS AN INTEREST" << endl;
if (attr)
{
cout << "Attribue: " << (char *)_attr << " Length: " << _attrLen << endl;
}
if (val)
{
cout << "Value: " << (char *)_val << " Length: " << _valLen << endl;
}
if (extra)
{
if (!_isPublisher)
{
cout << "Matched: " << _match << endl;
}
}
if (attrHash)
{
cout << "Attribute Hash:" << endl;
for (int i = 0; i < HASH_SIZE; i++)
{
printf("0x%x ", _attrHash[i]);
}
cout << endl;
}
if (valHash)
{
cout << "Value Hash:" << endl;
for (int i = 0; i < HASH_SIZE; i++)
{
printf("0x%x ", _valHash[i]);
}
cout << endl;
}
cout << " -------------------- " << endl;
}