Commit 0b5bd47d authored by Marios Choudary's avatar Marios Choudary
Browse files

Added function to serialize SMKEXT4MRecord structure

parent 62f7d5d6
......@@ -30,6 +30,12 @@ SMKEXT4MRecord* GetSMKEXT4MRecord(SMKEX_T4M_Type type, uint8_t version, unsigned
/*
* Serialize a SMKEXT4MRecord object
*
* Arguments:
* @len: length of serialised object
* @rec: input record for serialization
*
* @Returns: address of serialised object, NULL otherwise.
*/
unsigned char* SerializeSMKEXT4MRecord(SMKEXT4MRecord *rec);
unsigned char * SerializeSMKEXT4MRecord(unsigned int *len, const SMKEXT4MRecord *rec);
......@@ -84,14 +84,24 @@ int Smkex::sendKeyOrNonce(unsigned char *pointer, unsigned int length, MpString
// Encode SMKEX data
SMKEXT4MRecord* rec = GetSMKEXT4MRecord(SMKEX_T4M_Type::handshake, SMKEX_T4M_PROTOCOL_VERSION, pointer, length);
if(rec == NULL)
return 0;
unsigned int sdlen = 0;
unsigned char *data = SerializeSMKEXT4MRecord(&sdlen, rec);
if (data==NULL)
{
free(rec);
return 0;
}
//MpBuffer payloadSmkex((uint8_t*) pointer, length);
MpBuffer payloadSmkex((uint8_t*) rec, length);
MpBuffer payloadSmkex((uint8_t*) data, length);
MpMsgPayload messageSmkex(buddy, payloadSmkex, 1, 5, 1, /*int creationDate,*/ MP_TYPE_MESSAGE, false);
MpService::instance()->getAutoResend()->addMessage(messageSmkex);
// Erase SMKEX structure (we assume that addMessage copies the data)
// Erase SMKEX record data (we assume that addMessage copies the data)
free(data);
free(rec);
return 1; // seems successful
......@@ -238,10 +248,15 @@ void Smkex::onMsgReceived(MpString const& serial, const uint8_t* msg, uint32_t m
std::cout << " Smkex: Message (read as char*) is exactly ----" <<message<< "---- and its length is ---"<<msgLen<<"---\n\n\n\n\n";
// decapsulate SMKEX
// decapsulate SMKEX and fix pointer for data
SMKEXT4MRecord *rec = (SMKEXT4MRecord*)msg;
  • Serializarea produce un unsigned char *. Deci deserializarea trebuie sa transforme primirea tot într-un unsigned char * nu intr-un pointer spre un record. (altfel msg[4] va cauta dupa 4 locuri de record-uri nu după 4 locuri de unsigned char)

    Deserializarea se poate verifica si fara a trimite si primi mesaje (creand o instanta noua a structurii din mesajul serializat) si cred ca ar trebui facuta intr-o functie separata.

    Edited by Liliana GRIGORIU (106898)
Please register or sign in to reply
rec->data = (unsigned char*)&msg[4];
// print stuff about rec, e.g. rec->len, etc.
std::cout << " SMKEX Record type: " << rec->type << endl;
std::cout << " SMKEX Record version: " << rec->version << endl;
std::cout << " SMKEX Record length: " << rec->length << endl;
std::cout << " SMKEX Record data: " << rec->data << endl;
if(msgLen==SESSION_NONCE_LENGTH+1 || msgLen==SESSION_NONCE_LENGTH){
......
......@@ -21,3 +21,26 @@ SMKEXT4MRecord* GetSMKEXT4MRecord(SMKEX_T4M_Type type, uint8_t version, unsigned
return rec;
}
unsigned char * SerializeSMKEXT4MRecord(unsigned int *len, const SMKEXT4MRecord *rec)
{
unsigned char* dest;
*len = 4 + rec->length;
dest = (unsigned char*)malloc(*len);
if (dest==NULL)
{
*len = 0;
return NULL;
}
memcpy(dest, rec, 1);
memcpy(dest+1, &(rec->version), 1);
memcpy(dest+2, &(rec->length), 2);
memcpy(dest+4, rec->data, rec->length);
return dest;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment