2017-02-01 17:14:52 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-02-01 12:52:09 +00:00
|
|
|
|
2017-02-01 17:14:52 +00:00
|
|
|
#include "protobuf.h"
|
2017-02-01 12:52:09 +00:00
|
|
|
#include "libp2p/secio/propose.h"
|
|
|
|
|
|
|
|
// rand pubkey exchanges ciphers hashes
|
|
|
|
enum WireType secio_propose_message_fields[] = { WIRETYPE_LENGTH_DELIMITED, WIRETYPE_LENGTH_DELIMITED, WIRETYPE_LENGTH_DELIMITED, WIRETYPE_LENGTH_DELIMITED, WIRETYPE_LENGTH_DELIMITED };
|
|
|
|
|
|
|
|
struct Propose* libp2p_secio_propose_new() {
|
2017-02-01 17:14:52 +00:00
|
|
|
struct Propose* retVal = (struct Propose*)malloc(sizeof(struct Propose));
|
2017-02-01 12:52:09 +00:00
|
|
|
if (retVal == NULL)
|
|
|
|
return NULL;
|
|
|
|
memset((void*)retVal, 0, sizeof(struct Propose));
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void libp2p_secio_propose_free( struct Propose* in) {
|
|
|
|
if (in != NULL) {
|
|
|
|
if (in->rand != NULL)
|
|
|
|
free(in->rand);
|
|
|
|
if (in->public_key != NULL)
|
|
|
|
free(in->public_key);
|
|
|
|
if (in->ciphers != NULL)
|
|
|
|
free(in->ciphers);
|
|
|
|
if (in->exchanges != NULL)
|
|
|
|
free(in->exchanges);
|
|
|
|
if (in->hashes != NULL)
|
|
|
|
free(in->hashes);
|
|
|
|
free(in);
|
2017-02-01 17:14:52 +00:00
|
|
|
in = NULL;
|
2017-02-01 12:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:10:12 +00:00
|
|
|
int libp2p_secio_propose_set_property(void** to, size_t* to_size, const void* from, size_t from_size) {
|
2017-02-01 17:14:52 +00:00
|
|
|
if (*to != NULL)
|
|
|
|
free(*to);
|
|
|
|
*to = (void*)malloc(from_size);
|
|
|
|
if (*to == NULL)
|
|
|
|
return 0;
|
|
|
|
memcpy(*to, from, from_size);
|
|
|
|
*to_size = from_size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-01 12:52:09 +00:00
|
|
|
/**
|
|
|
|
* retrieves the approximate size of an encoded version of the passed in struct
|
|
|
|
* @param in the struct to look at
|
|
|
|
* @reutrns the size of buffer needed
|
|
|
|
*/
|
|
|
|
size_t libp2p_secio_propose_protobuf_encode_size(struct Propose* in) {
|
|
|
|
size_t retVal = 0;
|
|
|
|
retVal += 11 + in->rand_size;
|
|
|
|
retVal += 11 + in->public_key_size;
|
|
|
|
retVal += 11 + in->ciphers_size;
|
|
|
|
retVal += 11 + in->exchanges_size;
|
|
|
|
retVal += 11 + in->hashes_size;
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode the struct Propose in protobuf format
|
|
|
|
* @param in the struct to be encoded
|
|
|
|
* @param buffer where to put the results
|
|
|
|
* @param max_buffer_length the max to write
|
|
|
|
* @param bytes_written how many bytes were written to the buffer
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
|
|
|
int libp2p_secio_propose_protobuf_encode(struct Propose* in, unsigned char* buffer, size_t max_buffer_length, size_t* bytes_written) {
|
|
|
|
*bytes_written = 0;
|
|
|
|
size_t bytes_used;
|
|
|
|
// rand
|
2017-02-01 17:14:52 +00:00
|
|
|
if (!protobuf_encode_length_delimited(1, secio_propose_message_fields[0], (char*)in->rand, in->rand_size, &buffer[*bytes_written], max_buffer_length - *bytes_written, &bytes_used))
|
|
|
|
return 0;
|
2017-02-01 12:52:09 +00:00
|
|
|
*bytes_written += bytes_used;
|
|
|
|
// public key
|
2017-02-01 17:14:52 +00:00
|
|
|
if (!protobuf_encode_length_delimited(2, secio_propose_message_fields[1], (char*)in->public_key, in->public_key_size, &buffer[*bytes_written], max_buffer_length - *bytes_written, &bytes_used))
|
|
|
|
return 0;
|
2017-02-01 12:52:09 +00:00
|
|
|
*bytes_written += bytes_used;
|
|
|
|
// ciphers
|
|
|
|
if (!protobuf_encode_length_delimited(3, secio_propose_message_fields[2], in->ciphers, in->ciphers_size, &buffer[*bytes_written], max_buffer_length - *bytes_written, &bytes_used))
|
2017-02-01 17:14:52 +00:00
|
|
|
return 0;
|
2017-02-01 12:52:09 +00:00
|
|
|
*bytes_written += bytes_used;
|
|
|
|
// exchanges
|
|
|
|
if (!protobuf_encode_length_delimited(4, secio_propose_message_fields[3], in->exchanges, in->exchanges_size, &buffer[*bytes_written], max_buffer_length - *bytes_written, &bytes_used))
|
2017-02-01 17:14:52 +00:00
|
|
|
return 0;
|
2017-02-01 12:52:09 +00:00
|
|
|
*bytes_written += bytes_used;
|
|
|
|
// hashes
|
|
|
|
if (!protobuf_encode_length_delimited(5, secio_propose_message_fields[4], in->hashes, in->hashes_size, &buffer[*bytes_written], max_buffer_length - *bytes_written, &bytes_used))
|
2017-02-01 17:14:52 +00:00
|
|
|
return 0;
|
2017-02-01 12:52:09 +00:00
|
|
|
*bytes_written += bytes_used;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns a protobuf array into a Propose struct
|
|
|
|
* @param buffer the protobuf array
|
|
|
|
* @param buffer_length the length of the buffer
|
|
|
|
* @param out a pointer to the new struct Propose NOTE: this method allocates memory
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
|
|
|
int libp2p_secio_propose_protobuf_decode(unsigned char* buffer, size_t buffer_length, struct Propose** out) {
|
|
|
|
size_t pos = 0;
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
if (libp2p_secio_propose_new(out) == 0)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
while(pos < buffer_length) {
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
int field_no;
|
|
|
|
enum WireType field_type;
|
|
|
|
if (protobuf_decode_field_and_type(&buffer[pos], buffer_length, &field_no, &field_type, &bytes_read) == 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
pos += bytes_read;
|
|
|
|
switch(field_no) {
|
|
|
|
case (1): // rand
|
|
|
|
if (protobuf_decode_length_delimited(&buffer[pos], buffer_length - pos, (char**)&((*out)->rand), &((*out)->rand_size), &bytes_read) == 0)
|
|
|
|
goto exit;
|
|
|
|
pos += bytes_read;
|
|
|
|
break;
|
|
|
|
case (2): // public key
|
|
|
|
if (protobuf_decode_length_delimited(&buffer[pos], buffer_length - pos, (char**)&((*out)->public_key), &((*out)->public_key_size), &bytes_read) == 0)
|
|
|
|
goto exit;
|
|
|
|
pos += bytes_read;
|
|
|
|
break;
|
|
|
|
case (3): // ciphers
|
|
|
|
if (protobuf_decode_length_delimited(&buffer[pos], buffer_length - pos, (char**)&((*out)->ciphers), &((*out)->ciphers_size), &bytes_read) == 0)
|
|
|
|
goto exit;
|
|
|
|
pos += bytes_read;
|
|
|
|
break;
|
|
|
|
case (4): // exchanges
|
|
|
|
if (protobuf_decode_length_delimited(&buffer[pos], buffer_length - pos, (char**)&((*out)->exchanges), &((*out)->exchanges_size), &bytes_read) == 0)
|
|
|
|
goto exit;
|
|
|
|
pos += bytes_read;
|
|
|
|
break;
|
|
|
|
case (5): // hashes
|
|
|
|
if (protobuf_decode_length_delimited(&buffer[pos], buffer_length - pos, (char**)&((*out)->hashes), &((*out)->hashes_size), &bytes_read) == 0)
|
|
|
|
goto exit;
|
|
|
|
pos += bytes_read;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (retVal == 0) {
|
|
|
|
libp2p_secio_propose_free(*out);
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|