1 #include "config.h"
2
3 #include <razorback/debug.h>
4 #include <razorback/messages.h>
5 #include <razorback/log.h>
6 #include <razorback/uuids.h>
7 #include <razorback/string_list.h>
8 #include <razorback/json_buffer.h>
9
10 #include "messages/core.h"
11 #include "messages/cnc/core.h"
12 #include "binary_buffer.h"
13
14 #include <string.h>
15
16 static bool Hello_Deserialize_Binary(struct Message *message);
17 static bool Hello_Deserialize_Json(struct Message *message);
18 static bool Hello_Deserialize(struct Message *message, int mode);
19 static bool Hello_Serialize_Binary(struct Message *message);
20 static bool Hello_Serialize_Json(struct Message *message);
21 static bool Hello_Serialize(struct Message *message, int mode);
22 static void Hello_Destroy (struct Message *message);
23
24 static struct MessageHandler handler = {
25 MESSAGE_TYPE_HELLO,
26 Hello_Serialize,
27 Hello_Deserialize,
28 Hello_Destroy
29 };
30
31 // core.h
32 void
Message_CnC_Hello_Init(void)33 Message_CnC_Hello_Init(void)
34 {
35 Message_Register_Handler(&handler);
36 }
37
38 static bool
Hello_Deserialize_Binary(struct Message * message)39 Hello_Deserialize_Binary(struct Message *message)
40 {
41 struct BinaryBuffer *buffer;
42 struct MessageHello *hello;
43 uuid_t dispatcher;
44 UUID_Get_UUID(NUGGET_TYPE_DISPATCHER, UUID_TYPE_NUGGET_TYPE, dispatcher);
45
46 ASSERT(message != NULL);
47 if (message == NULL)
48 return false;
49
50 if ((buffer = BinaryBuffer_CreateFromMessage(message)) == NULL)
51 return false;
52
53 hello = message->message;
54
55 if (!BinaryBuffer_Get_UUID
56 (buffer, hello->uuidNuggetType))
57 {
58 buffer->pBuffer = NULL;
59 BinaryBuffer_Destroy (buffer);
60 rzb_log (LOG_ERR,
61 "%s: failed due to failure of BinaryBuffer_Get_UUID", __func__);
62 return false;
63 }
64
65 if (!BinaryBuffer_Get_UUID(buffer, hello->uuidApplicationType))
66 {
67 buffer->pBuffer = NULL;
68 BinaryBuffer_Destroy (buffer);
69 rzb_log (LOG_ERR,
70 "%s: failed due to failure of BinaryBuffer_Get_UUID", __func__);
71 return false;
72 }
73 if (!BinaryBuffer_Get_uint8_t(buffer, &hello->locality))
74 {
75 buffer->pBuffer = NULL;
76 BinaryBuffer_Destroy (buffer);
77 rzb_log (LOG_ERR,
78 "%s: failed due to failure of BinaryBuffer_Get_UUID", __func__);
79 return false;
80 }
81 if (uuid_compare(hello->uuidNuggetType, dispatcher) == 0)
82 {
83
84 if (!(BinaryBuffer_Get_uint8_t(buffer, &hello->priority) &&
85 BinaryBuffer_Get_uint8_t(buffer, &hello->protocol) &&
86 BinaryBuffer_Get_uint16_t(buffer, &hello->port) &&
87 BinaryBuffer_Get_uint32_t(buffer, &hello->flags)))
88 {
89 buffer->pBuffer = NULL;
90 BinaryBuffer_Destroy (buffer);
91 rzb_log (LOG_ERR,
92 "%s: failed due to failure of BinaryBuffer_Get_uint8", __func__);
93 return false;
94 }
95 // Address List
96 if (!BinaryBuffer_Get_StringList(buffer, &hello->addressList))
97 {
98 buffer->pBuffer = NULL;
99 BinaryBuffer_Destroy (buffer);
100 rzb_log (LOG_ERR,
101 "%s: failed due to failure of BinaryBuffer_Get_StringList", __func__);
102 return false;
103 }
104
105 }
106
107 buffer->pBuffer = NULL;
108 BinaryBuffer_Destroy (buffer);
109
110 return true;
111 }
112 static bool
Hello_Deserialize_Json(struct Message * message)113 Hello_Deserialize_Json(struct Message *message)
114 {
115 struct MessageHello *hello;
116 json_object *msg;
117 uuid_t dispatcher;
118 UUID_Get_UUID(NUGGET_TYPE_DISPATCHER, UUID_TYPE_NUGGET_TYPE, dispatcher);
119
120 ASSERT(message != NULL);
121 if (message == NULL)
122 return false;
123
124 if ((msg = json_tokener_parse((char *)message->serialized)) == NULL)
125 return false;
126
127 hello = message->message;
128
129 if (!JsonBuffer_Get_UUID
130 (msg, "Nugget_Type", hello->uuidNuggetType))
131 {
132 json_object_put(msg);
133 rzb_log (LOG_ERR,
134 "%s: failed due to failure of JsonBuffer_Get_UUID", __func__);
135 return false;
136 }
137
138 if (!JsonBuffer_Get_UUID(msg, "App_Type", hello->uuidApplicationType))
139 {
140 json_object_put(msg);
141 rzb_log (LOG_ERR,
142 "%s: failed due to failure of JsonBuffer_Get_UUID", __func__);
143 return false;
144 }
145 if (!JsonBuffer_Get_uint8_t(msg, "Locality", &hello->locality))
146 {
147 json_object_put(msg);
148 rzb_log (LOG_ERR,
149 "%s: failed due to failure of JsonBuffer_Get_UUID", __func__);
150 return false;
151 }
152 if (uuid_compare(hello->uuidNuggetType, dispatcher) == 0)
153 {
154 if (!(JsonBuffer_Get_uint8_t(msg, "Priority", &hello->priority) &&
155 JsonBuffer_Get_uint8_t(msg, "Protocol", &hello->protocol) &&
156 JsonBuffer_Get_uint16_t(msg, "Port", &hello->port) &&
157 JsonBuffer_Get_uint32_t(msg, "Flags", &hello->flags)))
158 {
159 json_object_put(msg);
160 rzb_log (LOG_ERR,
161 "%s: failed due to failure of JsonBuffer_Get_uint8", __func__);
162 return false;
163 }
164 // Address List
165 if (!JsonBuffer_Get_StringList(msg, "Address_List", &hello->addressList))
166 {
167 json_object_put(msg);
168 rzb_log (LOG_ERR,
169 "%s: failed due to failure of JsonBuffer_Get_StringList", __func__);
170 return false;
171 }
172
173 }
174 json_object_put(msg);
175
176 return true;
177 }
178
179 static bool
Hello_Deserialize(struct Message * message,int mode)180 Hello_Deserialize(struct Message *message, int mode)
181 {
182 ASSERT(message != NULL);
183 if ( message == NULL )
184 return false;
185
186 if ((message->message = calloc(1,sizeof(struct MessageHello))) == NULL)
187 return false;
188
189 switch (mode)
190 {
191 case MESSAGE_MODE_BIN:
192 return Hello_Deserialize_Binary(message);
193 case MESSAGE_MODE_JSON:
194 return Hello_Deserialize_Json(message);
195 default:
196 rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
197 return false;
198 }
199 return false;
200 }
201
202 SO_PUBLIC struct Message *
MessageHello_Initialize(struct RazorbackContext * context)203 MessageHello_Initialize (struct RazorbackContext *context)
204 {
205 struct Message * msg;
206 struct MessageHello *message;
207 if ((msg = Message_Create_Broadcast(MESSAGE_TYPE_HELLO, MESSAGE_VERSION_1, sizeof(struct MessageHello), context->uuidNuggetId)) == NULL)
208 return NULL;
209
210 message = msg->message;
211
212 uuid_copy (message->uuidNuggetType, context->uuidNuggetType);
213 uuid_copy (message->uuidApplicationType, context->uuidApplicationType);
214 message->locality = context->locality;
215 if ((context->iFlags & CONTEXT_FLAG_DISPATCHER) == CONTEXT_FLAG_DISPATCHER)
216 {
217 message->flags = context->dispatcherFlags;
218 message->priority = context->dispatcherPriority;
219 message->port = context->dispatcherPort;
220 message->protocol = context->dispatcherProtocol;
221 if( (message->addressList = List_Clone(context->dispatcherAddressList)) == NULL)
222 {
223 Hello_Destroy(msg);
224 return NULL;
225 }
226 }
227
228 msg->destroy = Hello_Destroy;
229 msg->deserialize=Hello_Deserialize;
230 msg->serialize=Hello_Serialize;
231
232 return msg;
233 }
234
235 static void
Hello_Destroy(struct Message * msg)236 Hello_Destroy (struct Message *msg)
237 {
238 struct MessageHello *message;
239
240 ASSERT (msg != NULL);
241 if (msg == NULL)
242 return;
243 message = msg->message;
244
245 if(message->addressList != NULL)
246 List_Destroy (message->addressList);
247
248 Message_Destroy(msg);
249 }
250
251
252 static bool
Hello_Serialize_Binary(struct Message * message)253 Hello_Serialize_Binary(struct Message *message)
254 {
255 struct MessageHello *hello;
256 struct BinaryBuffer *buffer;
257 uuid_t dispatcher;
258 UUID_Get_UUID(NUGGET_TYPE_DISPATCHER, UUID_TYPE_NUGGET_TYPE, dispatcher);
259
260 ASSERT(message != NULL);
261 if (message == NULL)
262 return false;
263
264 hello = message->message;
265
266 message->length = (sizeof(uuid_t) * 2) + sizeof(uint8_t);
267 if (uuid_compare(hello->uuidApplicationType, dispatcher) == 0)
268 {
269 message->length += sizeof(uint8_t) +
270 sizeof(uint32_t) + StringList_Size(hello->addressList);
271 }
272
273 if ((buffer = BinaryBuffer_Create(message->length)) == NULL)
274 return false;
275
276 if (!BinaryBuffer_Put_UUID
277 (buffer, hello->uuidNuggetType))
278 {
279 BinaryBuffer_Destroy (buffer);
280 rzb_log (LOG_ERR,
281 "%s: failed due to failure of BinaryBuffer_Put_UUID", __func__);
282 return false;
283 }
284 if (!BinaryBuffer_Put_UUID
285 (buffer, hello->uuidApplicationType))
286 {
287 BinaryBuffer_Destroy (buffer);
288 rzb_log (LOG_ERR,
289 "%s: failed due to failure of BinaryBuffer_Put_UUID", __func__);
290 return false;
291 }
292 if (!BinaryBuffer_Put_uint8_t(buffer, hello->locality))
293 {
294 BinaryBuffer_Destroy (buffer);
295 rzb_log (LOG_ERR,
296 "%s: failed due to failure of BinaryBuffer_Get_UUID", __func__);
297 return false;
298 }
299 if (uuid_compare(hello->uuidNuggetType, dispatcher) == 0)
300 {
301
302 if (!(BinaryBuffer_Put_uint8_t(buffer, hello->priority) &&
303 BinaryBuffer_Put_uint8_t(buffer, hello->protocol) &&
304 BinaryBuffer_Put_uint16_t(buffer, hello->port) &&
305 BinaryBuffer_Put_uint32_t(buffer, hello->flags)))
306 {
307 BinaryBuffer_Destroy (buffer);
308 rzb_log (LOG_ERR,
309 "%s: failed due to failure of BinaryBuffer_Put_uint8", __func__);
310 return false;
311 }
312 // Address List
313 if (!BinaryBuffer_Put_StringList(buffer, hello->addressList))
314 {
315 BinaryBuffer_Destroy (buffer);
316 rzb_log (LOG_ERR,
317 "%s: failed due to failure of BinaryBuffer_Put_StringList", __func__);
318 return false;
319 }
320 }
321
322 message->serialized = buffer->pBuffer;
323 buffer->pBuffer = NULL;
324 BinaryBuffer_Destroy(buffer);
325 return true;
326 }
327
328 static bool
Hello_Serialize_Json(struct Message * message)329 Hello_Serialize_Json(struct Message *message)
330 {
331 struct MessageHello *hello;
332 json_object *msg;
333 const char * wire;
334 uuid_t dispatcher;
335 UUID_Get_UUID(NUGGET_TYPE_DISPATCHER, UUID_TYPE_NUGGET_TYPE, dispatcher);
336
337 ASSERT(message != NULL);
338 if (message == NULL)
339 return false;
340
341 hello = message->message;
342
343 if ((msg = json_object_new_object()) == NULL)
344 return false;
345
346 if (!JsonBuffer_Put_UUID
347 (msg, "Nugget_Type", hello->uuidNuggetType))
348 {
349 json_object_put(msg);
350 rzb_log (LOG_ERR,
351 "%s: failed due to failure of JsonBuffer_Put_UUID", __func__);
352 return false;
353 }
354 if (!JsonBuffer_Put_UUID
355 (msg, "App_Type", hello->uuidApplicationType))
356 {
357 json_object_put(msg);
358 rzb_log (LOG_ERR,
359 "%s: failed due to failure of JsonBuffer_Put_UUID", __func__);
360 return false;
361 }
362
363 if (!JsonBuffer_Put_uint8_t(msg, "Locality", hello->locality))
364 {
365 json_object_put(msg);
366 rzb_log (LOG_ERR,
367 "%s: failed due to failure of JsonBuffer_Get_UUID", __func__);
368 return false;
369 }
370 if (uuid_compare(hello->uuidNuggetType, dispatcher) == 0)
371 {
372 if (!(JsonBuffer_Put_uint8_t(msg, "Priority", hello->priority) &&
373 JsonBuffer_Put_uint8_t(msg, "Protocol", hello->protocol) &&
374 JsonBuffer_Put_uint16_t(msg, "Port", hello->port) &&
375 JsonBuffer_Put_uint32_t(msg, "Flags", hello->flags)))
376 {
377 json_object_put(msg);
378 rzb_log (LOG_ERR,
379 "%s: failed due to failure of JsonBuffer_Put_uint8", __func__);
380 return false;
381 }
382 // Address List
383 if (!JsonBuffer_Put_StringList(msg, "Address_List", hello->addressList))
384 {
385 json_object_put(msg);
386 rzb_log (LOG_ERR,
387 "%s: failed due to failure of JsonBuffer_Put_StringList", __func__);
388 return false;
389 }
390
391 }
392
393 wire = json_object_to_json_string(msg);
394 message->length = strlen(wire);
395 if ((message->serialized = calloc(message->length+1, sizeof(char))) == NULL)
396 {
397 json_object_put(msg);
398 return false;
399 }
400 strcpy((char *)message->serialized, wire);
401 json_object_put(msg);
402
403 return true;
404 }
405
406
407 static bool
Hello_Serialize(struct Message * message,int mode)408 Hello_Serialize(struct Message *message, int mode)
409 {
410 ASSERT(message != NULL);
411 if ( message == NULL )
412 return false;
413
414 switch (mode)
415 {
416 case MESSAGE_MODE_BIN:
417 return Hello_Serialize_Binary(message);
418 case MESSAGE_MODE_JSON:
419 return Hello_Serialize_Json(message);
420 default:
421 rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
422 return false;
423 }
424 return false;
425 }
426