1 #include "config.h"
2
3 #include <razorback/debug.h>
4 #include <razorback/messages.h>
5 #include <razorback/log.h>
6 #include <razorback/block.h>
7 #include <razorback/event.h>
8 #include <razorback/json_buffer.h>
9
10
11 #include "messages/core.h"
12 #include "binary_buffer.h"
13
14 #include <string.h>
15
16 static void InspectionSubmission_Destroy (struct Message *message);
17 static bool InspectionSubmission_Deserialize_Binary(struct Message *message);
18 static bool InspectionSubmission_Deserialize_Json(struct Message *message);
19 static bool InspectionSubmission_Deserialize(struct Message *message, int mode);
20 static bool InspectionSubmission_Serialize_Binary(struct Message *message);
21 static bool InspectionSubmission_Serialize_Json(struct Message *message);
22 static bool InspectionSubmission_Serialize(struct Message *message, int mode);
23
24 static struct MessageHandler handler = {
25 MESSAGE_TYPE_INSPECTION,
26 InspectionSubmission_Serialize,
27 InspectionSubmission_Deserialize,
28 InspectionSubmission_Destroy
29 };
30
31 //core.h
32 void
MessageInspectionSubmission_Init(void)33 MessageInspectionSubmission_Init(void)
34 {
35 Message_Register_Handler(&handler);
36 }
37
38 SO_PUBLIC struct Message *
MessageInspectionSubmission_Initialize(const struct Event * p_pEvent,uint32_t p_iReason,uint32_t localityCount,uint8_t * localities)39 MessageInspectionSubmission_Initialize (
40 const struct Event *p_pEvent,
41 uint32_t p_iReason,
42 uint32_t localityCount,
43 uint8_t *localities)
44 {
45 struct Message * msg;
46 struct MessageInspectionSubmission *message;
47
48 ASSERT (p_pEvent != NULL);
49 if (p_pEvent == NULL)
50 return NULL;
51
52 if ((msg = Message_Create(MESSAGE_TYPE_INSPECTION, MESSAGE_VERSION_1, sizeof(struct MessageInspectionSubmission))) == NULL)
53 return NULL;
54 message = msg->message;
55
56 if ((message->pBlock = Block_Clone(p_pEvent->pBlock)) == NULL)
57 {
58 rzb_log(LOG_ERR, "%s: Failed to clone block", __func__);
59 InspectionSubmission_Destroy(msg);
60 return NULL;
61 }
62
63 message->iReason = p_iReason;
64 if ((message->eventId = EventId_Clone(p_pEvent->pId)) == NULL)
65 {
66 rzb_log(LOG_ERR, "%s: Failed to clone event id", __func__);
67 InspectionSubmission_Destroy(msg);
68 return NULL;
69 }
70 message->pEventMetadata = List_Clone (p_pEvent->pMetaDataList);
71 if (message->pEventMetadata == NULL)
72 {
73 rzb_log(LOG_ERR, "%s: Failed to clone metadata list", __func__);
74 InspectionSubmission_Destroy(msg);
75 return NULL;
76 }
77 message->localityCount = localityCount;
78 if (localityCount > 0)
79 {
80 if ((message->localityList = calloc(localityCount, sizeof(uint8_t))) == NULL)
81 {
82 rzb_log(LOG_ERR, "%s: Failed to clone locality list", __func__);
83 InspectionSubmission_Destroy(msg);
84 return NULL;
85 }
86 memcpy(message->localityList, localities, localityCount);
87 }
88 msg->destroy=InspectionSubmission_Destroy;
89 msg->deserialize=InspectionSubmission_Deserialize;
90 msg->serialize=InspectionSubmission_Serialize;
91
92 return msg;
93 }
94
95 static void
InspectionSubmission_Destroy(struct Message * message)96 InspectionSubmission_Destroy (struct Message
97 *message)
98 {
99 struct MessageInspectionSubmission *msg;
100 ASSERT (message != NULL);
101 if (message == NULL)
102 return;
103
104 msg = (struct MessageInspectionSubmission *)message->message;
105
106 // destroy any malloc'd components
107 if (msg->pBlock != NULL)
108 Block_Destroy (msg->pBlock);
109 if (msg->eventId != NULL)
110 EventId_Destroy(msg->eventId);
111 if (msg->pEventMetadata != NULL)
112 List_Destroy(msg->pEventMetadata);
113 if (msg->localityList != NULL)
114 free(msg->localityList);
115
116 Message_Destroy(message);
117 }
118
119 static bool
InspectionSubmission_Deserialize_Binary(struct Message * message)120 InspectionSubmission_Deserialize_Binary(struct Message *message)
121 {
122 struct BinaryBuffer *buffer;
123 struct MessageInspectionSubmission *submit;
124 uint32_t i;
125 ASSERT(message != NULL);
126 if (message == NULL)
127 return false;
128
129 if ((buffer = BinaryBuffer_CreateFromMessage(message)) == NULL)
130 return false;
131
132 submit = (struct MessageInspectionSubmission *)message->message;
133
134 if (!BinaryBuffer_Get_uint32_t (buffer, &submit->iReason))
135 {
136 buffer->pBuffer = NULL;
137 BinaryBuffer_Destroy (buffer);
138 return false;
139 }
140 if (!BinaryBuffer_Get_EventId (buffer, &submit->eventId))
141 {
142 buffer->pBuffer = NULL;
143 BinaryBuffer_Destroy (buffer);
144 return false;
145 }
146 if (!BinaryBuffer_Get_NTLVList (buffer, &submit->pEventMetadata))
147 {
148 buffer->pBuffer = NULL;
149 BinaryBuffer_Destroy (buffer);
150 return false;
151 }
152
153 if (!BinaryBuffer_Get_Block (buffer, &submit->pBlock))
154 {
155 buffer->pBuffer = NULL;
156 BinaryBuffer_Destroy (buffer);
157 return false;
158 }
159 if (!BinaryBuffer_Get_uint32_t (buffer, &submit->localityCount))
160 {
161 BinaryBuffer_Destroy (buffer);
162 return false;
163 }
164 if (submit->localityCount > 0)
165 if ((submit->localityList = calloc(submit->localityCount, sizeof(uint8_t))) == NULL)
166 {
167 BinaryBuffer_Destroy (buffer);
168 return false;
169 }
170
171 for (i = 0; i < submit->localityCount; i++)
172 {
173 if (!BinaryBuffer_Get_uint8_t (buffer, &(submit->localityList[i])))
174 {
175 BinaryBuffer_Destroy (buffer);
176 return false;
177 }
178 }
179 buffer->pBuffer = NULL;
180 BinaryBuffer_Destroy (buffer);
181
182 return true;
183 }
184
185 static bool
InspectionSubmission_Deserialize_Json(struct Message * message)186 InspectionSubmission_Deserialize_Json(struct Message *message)
187 {
188 struct MessageInspectionSubmission *submit;
189 json_object *msg;
190
191 ASSERT(message != NULL);
192 if (message == NULL)
193 return false;
194
195 if ((msg = json_tokener_parse((char *)message->serialized)) == NULL)
196 return false;
197
198 submit = message->message;
199
200 if (!JsonBuffer_Get_uint32_t (msg, "Reason", &submit->iReason))
201 {
202 json_object_put(msg);
203 return false;
204 }
205 if (!JsonBuffer_Get_EventId (msg, "Event_ID", &submit->eventId))
206 {
207 json_object_put(msg);
208 return false;
209 }
210 if (!JsonBuffer_Get_NTLVList (msg, "Event_Metadata", &submit->pEventMetadata))
211 {
212 json_object_put(msg);
213 return false;
214 }
215
216 if (!JsonBuffer_Get_Block (msg, "Block", &submit->pBlock))
217 {
218 json_object_put(msg);
219 return false;
220 }
221 if (!JsonBuffer_Get_uint8List (msg, "Avaliable_Localities", &submit->localityList, &submit->localityCount))
222 {
223 json_object_put(msg);
224 return false;
225 }
226 json_object_put(msg);
227 return true;
228 }
229
230 static bool
InspectionSubmission_Deserialize(struct Message * message,int mode)231 InspectionSubmission_Deserialize(struct Message *message, int mode)
232 {
233 ASSERT(message != NULL);
234 if ( message == NULL )
235 return false;
236
237 if ((message->message = calloc(1,sizeof(struct MessageInspectionSubmission))) == NULL)
238 return false;
239
240 switch (mode)
241 {
242 case MESSAGE_MODE_BIN:
243 return InspectionSubmission_Deserialize_Binary(message);
244 case MESSAGE_MODE_JSON:
245 return InspectionSubmission_Deserialize_Json(message);
246 break;
247 default:
248 rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
249 return false;
250 }
251 return false;
252 }
253
254
255 static bool
InspectionSubmission_Serialize_Binary(struct Message * message)256 InspectionSubmission_Serialize_Binary(struct Message *message)
257 {
258 struct MessageInspectionSubmission *submit;
259 struct BinaryBuffer *buffer;
260 uint32_t i;
261
262 ASSERT(message != NULL);
263 if (message == NULL)
264 return false;
265
266 submit = (struct MessageInspectionSubmission *)message->message;
267
268 message->length = Block_BinaryLength (submit->pBlock) +
269 NTLVList_Size (submit->pEventMetadata) +
270 sizeof(struct EventId) + //Source nugget ID
271 (uint32_t) sizeof (submit->iReason) +
272 sizeof(uint32_t) + submit->localityCount;
273
274
275 if ((buffer = BinaryBuffer_Create(message->length)) == NULL)
276 return false;
277
278 if (!BinaryBuffer_Put_uint32_t (buffer, submit->iReason))
279 {
280 BinaryBuffer_Destroy (buffer);
281 return false;
282 }
283 if (!BinaryBuffer_Put_EventId (buffer, submit->eventId))
284 {
285 BinaryBuffer_Destroy (buffer);
286 return false;
287 }
288 if (!BinaryBuffer_Put_NTLVList (buffer, submit->pEventMetadata))
289 {
290 BinaryBuffer_Destroy (buffer);
291 return false;
292 }
293
294 if (!BinaryBuffer_Put_Block (buffer, submit->pBlock))
295 {
296 BinaryBuffer_Destroy (buffer);
297 return false;
298 }
299 if (!BinaryBuffer_Put_uint32_t (buffer, submit->localityCount))
300 {
301 BinaryBuffer_Destroy (buffer);
302 return false;
303 }
304 for (i = 0; i < submit->localityCount; i++)
305 {
306 if (!BinaryBuffer_Put_uint8_t (buffer, submit->localityList[i]))
307 {
308 BinaryBuffer_Destroy (buffer);
309 return false;
310 }
311 }
312 message->serialized = buffer->pBuffer;
313 buffer->pBuffer = NULL;
314 BinaryBuffer_Destroy(buffer);
315 return true;
316 }
317
318 static bool
InspectionSubmission_Serialize_Json(struct Message * message)319 InspectionSubmission_Serialize_Json(struct Message *message)
320 {
321 struct MessageInspectionSubmission *submit;
322 json_object *msg;
323 const char * wire;
324
325 ASSERT(message != NULL);
326 if (message == NULL)
327 return false;
328
329 submit = message->message;
330
331 if ((msg = json_object_new_object()) == NULL)
332 return false;
333
334
335 if (!JsonBuffer_Put_uint32_t (msg, "Reason", submit->iReason))
336 {
337 json_object_put(msg);
338 return false;
339 }
340 if (!JsonBuffer_Put_EventId (msg, "Event_ID", submit->eventId))
341 {
342 json_object_put(msg);
343 return false;
344 }
345 if (!JsonBuffer_Put_NTLVList (msg, "Event_Metadata", submit->pEventMetadata))
346 {
347 json_object_put(msg);
348 return false;
349 }
350
351 if (!JsonBuffer_Put_Block (msg, "Block", submit->pBlock))
352 {
353 json_object_put(msg);
354 return false;
355 }
356 if (!JsonBuffer_Put_uint8List (msg, "Avaliable_Localities", submit->localityList, submit->localityCount))
357 {
358 json_object_put(msg);
359 return false;
360 }
361
362 wire = json_object_to_json_string(msg);
363 message->length=strlen(wire);
364 if ((message->serialized = calloc(message->length+1, sizeof(char))) == NULL)
365 {
366 json_object_put(msg);
367 return false;
368 }
369 strcpy((char *)message->serialized, wire);
370 json_object_put(msg);
371 return true;
372 }
373
374 static bool
InspectionSubmission_Serialize(struct Message * message,int mode)375 InspectionSubmission_Serialize(struct Message *message, int mode)
376 {
377 ASSERT(message != NULL);
378 if ( message == NULL )
379 return false;
380
381 switch (mode)
382 {
383 case MESSAGE_MODE_BIN:
384 return InspectionSubmission_Serialize_Binary(message);
385 case MESSAGE_MODE_JSON:
386 return InspectionSubmission_Serialize_Json(message);
387 default:
388 rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
389 return false;
390 }
391 return false;
392 }
393