#include <unistd.h>
#include <stdlib.h>
+#define IL_PROTOCOL_MAGIC_WORD "michal"
+
int
encode_il_msg(char **buffer, const char *event)
{
int len;
char *p;
+ char *protocol_magic_word = IL_PROTOCOL_MAGIC_WORD;
/* allocate enough room to hold the message */
- len = 17 + len_string((char*)event);
+ len = 17 + len_string(protocol_magic_word) + len_string((char*)event);
if((*buffer = malloc(len)) == NULL) {
return(-1);
}
p += 17;
/* write rest of the message */
+ p = put_string(p, protocol_magic_word);
p = put_string(p, (char*)event);
return(p - *buffer);
decode_il_msg(char **event, const char *buf)
{
char *p;
+ char *protocol_magic_word=NULL;
+ int magic_word_check_failed = 0;
+
+ /* First check that the protocol 'magic' word is there */
+ p = get_string((char*)buf, &protocol_magic_word);
+ if (protocol_magic_word) {
+ if (strcmp (protocol_magic_word, IL_PROTOCOL_MAGIC_WORD) != 0) {
+ magic_word_check_failed = 1;
+ }
+ free(protocol_magic_word);
+ }
+
+ if (magic_word_check_failed != 0) return (-1);
- p = get_string((char*)buf, event);
+ p = get_string(p, event);
if(p == NULL) {
if(*event) { free(*event); *event = NULL; };
return(-1);
}
void testEncodeMsg() {
- CPPUNIT_ASSERT_EQUAL(len_msg, 26);
+ CPPUNIT_ASSERT_EQUAL(len_msg, 35);
CPPUNIT_ASSERT(buffer_msg != NULL);
CPPUNIT_ASSERT(!strncmp(buffer_msg, msg, len_msg));
}
char *s;
l = read_il_data(&s, test_reader);
- CPPUNIT_ASSERT_EQUAL(l, 9);
+ CPPUNIT_ASSERT_EQUAL(l, 18);
CPPUNIT_ASSERT(s != NULL);
- CPPUNIT_ASSERT(!strcmp(s, "6 zprava\n"));
+ CPPUNIT_ASSERT(!strcmp(s, "6 michal\n6 zprava\n"));
free(s);
}
}
};
-const char *IlMsgTest::msg = " 9\n6 zprava\n";
+const char *IlMsgTest::msg = " 18\n6 michal\n6 zprava\n";
const char *IlMsgTest::rep = " 14\n10\n20\n5 chyba\n";
int IlMsgTest::pos;